Volume Calculation on PointCloud

55 Views Asked by At

I am currently working on pointcloud where i will select the particular area for that particular area i need volume where my code giving me huge values than expected ,please help me resolve the issue

calculateVolume(points) {
            // Ensure uniqueness of points based on x, y, z coordinates
            const uniquePoints = Array.from(new Set(points.map(p => JSON.stringify(p)))).map(p => JSON.parse(p));
            
            if (uniquePoints.length < 4) {
                console.error("Not enough points to calculate volume.");
                return 0;
            }
        
            // Find the minimum z-value among all points
            const minZ = Math.min(...uniquePoints.map(point => point.z));
            const maxz = Math.max(...uniquePoints.map(point => point.z));
            console.log("minZ value is ",minZ);
            console.log("maxZ value is ",maxz);
            const boundedPoints = uniquePoints.map(point => ({ x: point.x, y: point.y, z: (point.z > minZ) && (point.z < maxz) }));
            // console.log(boundedPoints)
            const delaunay = Delaunator.from(boundedPoints.map(point => [point.x, point.y, point.z]));
        
            let volume = 0;
            const triangles = delaunay.triangles;
        
            for (let i = 0; i < triangles.length; i += 3) {
                const p1 = boundedPoints[triangles[i]];
                const p2 = boundedPoints[triangles[i + 1]];
                const p3 = boundedPoints[triangles[i + 2]];
                console.log("points p1 is", p1);
                console.log("points p2 is", p2);
                console.log("points p3 is", p3);

        
                volume += (
                    p1.x * (p2.y * p3.z - p3.y * p2.z) -
                    p1.y * (p2.x * p3.z - p2.z * p3.x) +
                    p1.z * (p2.x * p3.y - p2.y * p3.x)
                );
                console.log("volume is", volume);
            }
        
            // Calculate the volume using the minimum z-value
            volume = Math.abs(volume) / 6.0;
        
            return volume;
        }

where i want the volume of the selected region within the points in the above code i am using the delaunay triangulation method

1

There are 1 best solutions below

1
On

In the special case of a triangle, the volume of a triangular column raised over the plane is the average height of the three vertices times the area of the triangle. So you could try something like the following formula from Wikipedia

area = ( (p1.x-p3.x)*(p2.y-p1.y)-(p1.x-p2.x)*(p3.y-p1.y) )/2.0;
zMean =  (p1.z+p2.z+p3.z)/3.0;
volume += area*zMean; 

Kevin Brown gives a good explanation and alternate formulas for the computation at Volume Under a Triangle

I've got a web article talking about computing the volume of a lake at Using the Delaunay to Compute Lake Volume