如果已知顶点和边,如何在Android ArCore中以平方米为单位查找面积?

时间:2018-11-29 06:58:01

标签: android arcore

下面是Google Hello AR示例中的代码,我必须对其进行修改才能找到所选非相交区域的区域,但是区域却出错了

int size = anchors.size();
                arrayOfVertex = new float[size + 1][2];
                Pose firstPose = getPose(anchors.get(0));
                arrayOfVertex[0][0] = firstPose.tx();
                arrayOfVertex[0][1] = firstPose.ty();
                Pose pose0 = getPose(anchors.get(0));
                for (int i = 1; i < anchors.size(); i++) {
                    pose1 = getPose(anchors.get(i));                    
                    float meter = (getDistance(pose0, pose1));
                    total += meter;
                    sb.append(" + ").append(String.format("%.2f", meter));

                    pose0 = pose1;
                    arrayOfVertex[i][0] = pose0.tx();
                    arrayOfVertex[i][1] = pose0.ty();
                }

根据从姿势获得的顶点计算面积

area = sqrt(shoelaceFormulaToFindArea(arrayOfVertex)); final String result = "Area(m sq): " + area;

我用来查找区域的公式

 public double shoelaceFormulaToFindArea(float[][] arr)

{

    int n = arr.length;

    /** copy initial point to last row **/

    arr[n - 1][0] = arr[0][0];

    arr[n - 1][1] = arr[0][1];


    double det = 0.0;

    /** add product of x coordinate of ith point with y coordinate of (i + 1)th point **/

    for (int i = 0; i < n - 1; i++)

        det += (double) (arr[i][0] * arr[i + 1][1]);

    /** subtract product of y coordinate of ith point with x coordinate of (i + 1)th point **/

    for (int i = 0; i < n - 1; i++)

        det -= (double) (arr[i][1] * arr[i + 1][0]);


    /** find absolute value and divide by 2 **/

    det = Math.abs(det);

    det /= 2;

    return det;

}

获取错误的区域,有时为0.0。

1 个答案:

答案 0 :(得分:0)

正在犯一个简单的错误。我从3d平面传递了ty(),而不是tz()。当我通过tx和tz()进入鞋带公式后,进入该区域。