Search code examples
androidarcore

How to find area in square meters in android ArCore if vertices and sides are known?


Below is code from Google Hello AR sample which I have to modifed to find the area of selected non intersecting region, But getting wrong Area

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();
                }

Calculating AREA from vertices obtained from Pose

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

Formula which I am using to find 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;

}

Getting Wrong Area, and sometimes 0.0.


Solution

  • Was doing a simple mistake. I was passing ty() from the 3d plane instead of tz(). Got the area once i passed tx & tz() to shoelace formula.