1. Measuring Distance
Distance is the most fundamental AR measurement. It is calculated as the Euclidean distance between two 3D coordinates (Anchors) placed in the AR scene.
The Math:
Given two points P_1(x_1, y_1, z_1) and P_2(x_2, y_2, z_2) from AR Core Pose objects:
Distance = ((x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2)^(1/2)
2. Measuring AreaÂ
To measure area, the user must place at least three anchors to form a polygon. Most AR area measurements assume the surface is a flat plane (2D surface in 3D space).
The Algorithm:
The most common approach is the Shoelace Formula (Surveyor's Formula). Since the points are in 3D, you should first project them onto a 2D plane (using the plane's normal) or ensure all points lie on the same detected AR Core Plane.
Simplified Shoelace Formula:
Area = (1/2) *| sum_{i=1}^{n-1} (x_i y_{i+1} - x_{i+1} y_i) |
3. Measuring Volume
Volume is significantly more complex because it requires depth. There are two primary ways to approach this:
- Box Method (Extrusion):Measure the floor area (as described above) and then measure a vertical distance (height).
Formula: Volume = Area*Height
- Bounding Box Method: Use two diagonal corner points to define a 3D cube.
Formula: V = |x_2 - x_1| * |y_2 - y_1| * |z_2 – z_1|
- Depth API (Advanced): Use AR Core Raw Depth API to create a point cloud of an object and calculate volume via a convex hull or voxelization. This is much more accurate for irregular objects but requires a device with a Time-of-Flight (ToF) sensor or high-quality depth support.
Key AR Core Components for Success
Component
Purpose
Hit Result
Used to determine where the user tapped on a real-world surface to place an anchor.
Anchor Â
Fixes a point in the real world so the coordinates stay consistent as the phone moves.
Depth
API(Optional) Allows for "occlusion" and much more accurate surface distance measurements without needing plane detection..
Config.PlaneFindingMode
Should be set to HORIZONTAL or VERTICAL to give the user a surface to measure on.
Since you're diving into the implementation, the real challenge in ARCore isn't just the math—it's the UX and precision. Measuring a 3D space on a 2D screen can be finicky for users.
Here is a breakdown of how to structure your Java logic for these features:
To get a clean measurement, you should use Session.hitTest() to place Anchors. Using anchors ensures that even if the camera drifts, the points stay "locked" to the physical world coordinates.
For area, users usually want to measure a floor or a wall. You should restrict your points to a single Plane. If the points are on the same horizontal plane (y values are nearly identical), you can ignore the y-axis and treat it as a 2D polygon calculation.
Implementation Tip: Store your points in a List<Pose>. As the user adds a new point, dynamically update a "current area" preview. The Shoelace Formula is the standard here because it works for any non-self-intersecting polygon (irregular shapes).
Calculating the volume of an arbitrary 3D object is very difficult without a LiDAR sensor. For a standard Java/ARCore app, the most reliable way is the Extruded Polygon method:
Define the Base: User clicks points to define the floor area (A).
Define the Height: User clicks a point on the floor and drags a line upward, or clicks a point on the ceiling (h).
Calculate: V = A * h.
The Depth API: If the device supports it, enable the Raw Depth API. It allows you to perform hit-tests against "non-planar" objects (like a couch or a ball) which standard plane-finding might miss.
Smoothing: AR coordinates can jitter. Use a simple Moving Average Filter on the camera pose or the hit-test results to prevent the measurement numbers from jumping around rapidly.
Measurement Manager: A singleton or helper class to store the list of active anchors.
Renderer: Handles drawing the lines and text labels in the AR overlay.
Unit Converter: A utility to toggle between Metric (meters/cm) and Imperial (feet/inches).
The application has, along with the specified functions, additional functions such as localization: English, Spanish, French, Bulgarian and German. Calculated distance, area and volume are in meters, which can be converted to centimeters, inches, feet.