ArCore frame.hittest 实现或如何检测 arcore Plane 上的所有屏幕角点

问题描述 投票:0回答:1

我正在尝试在已识别的平面上绘制照片拍摄区域的框架。有时 frame.hitTest 返回 null。 在代码bounds是射击区域的点。

 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
    }

我尝试从检测到的平面实现数学平面,并从屏幕角落发出光线。 四元数类等于来自 sceneform 库。 将 MathPlane 类作为来自 sceneform 库的隐藏类 Plane

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
augmented-reality arcore hittest
1个回答
0
投票

我找到了解决方案。

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

正常计算好

© www.soinside.com 2019 - 2024. All rights reserved.