Michael Budash
Michael Budash

Reputation: 1

ArCore frame.hittest implementation or how to detect all screen corner points on the arcore Plane

I'm trying to draw a frame of the area where the photo was taken on the recognized plane. Sometimes frame.hitTest returns null. at the code bounds is points of shooted area.

 var lbPose = convertAndroidViewCoords(bounds[0], frame, plane)
  var rbPose = convertAndroidViewCoords(bounds[1], frame, plane)
  var rtPose = convertAndroidViewCoords(bounds[2], frame, plane)
  var ltPose = convertAndroidViewCoords(bounds[3], frame, plane)

  private fun convertAndroidViewCoords(point: PointF, frame: Frame, plane: Plane): Pose? {
        convertFloats[0] = point.x
        convertFloats[1] = point.y
        frame.transformCoordinates2d(
            Coordinates2d.IMAGE_PIXELS,
            convertFloats,
            Coordinates2d.VIEW,
            convertFloatsOut
        )
        return hitTestView(convertFloatsOut[0], convertFloatsOut[1], frame, plane)
   }

   private fun hitTestView(x: Float, y: Float, frame: Frame, plane: Plane): Pose? {
        val hits = frame.hitTest(x, y)
        val result = hits.firstOrNull { it.trackable.equals(plane) } ?: return null
        return result.hitPose
    }

I tried to implement of mathematical plane from detected plane and did rays from screen corners. class Quaternion equals as from sceneform library. class MathPlane as hidden class Plane from sceneform library

val planePose = plane.centerPose

 val quaternion = Quaternion(planePose.rotationQuaternion)
 val normal = Quaternion.rotateVector(quaternion, Vector3.right())

 val mathPlane =
                MathPlane(Vector3(planePose.tx(), planePose.ty(), planePose.tz()), normal)

// left-bottom corner
val ray = rayHitCalculation.screenPointToRay(0f, view.root.height.toFloat())
                val rayHit = RayHit()
                if (mathPlane.rayIntersection(ray, rayHit)) {
                    val p = rayHit.point
                    lbPose = Pose(floatArrayOf(p.x, p.y, p.z), planePose.rotationQuaternion)
                }
/// etc. for another corners

Upvotes: 0

Views: 161

Answers (1)

Michael Budash
Michael Budash

Reputation: 1

I find solution.

  val planePose = plane.centerPose
  val normal = FloatArray(3) { 0f }
  plane.centerPose.getTransformedAxis(1, 1.0f, normal, 0)

where normal calculates good

Upvotes: 0

Related Questions