Search code examples
rvolumergl

Using rgl package in R to measure the volume of an irregular object


I have vertices (x,y,z coordinates) of an irregular object (all normals point outward) and I would like to calculate its volume. With small objects with just a handful of vertices, I can find the volume by calculating the volume of tetrahedrons for each triangle on the surface with respect to a reference point. I have a script that will do that for small number of faces and identifying the faces and order of the points is not too difficult. However, for a large number of points, identifying each group of 3 points is challenging and time consuming. I think my first problem is that I need to convert my vertices into a mesh but I don't know how to do that. I have tried several commands in the rgl package but I'm not getting anywhere and I'm not understanding this step.

Below is a sample of my data. For each set of points, I slice my object and identify 6 vertices. plot3d(x,y,z) works nicely.

index   x   y   z   point
1   -225.64 0.3 427.91  1
2   -225.64 0.3 233.32  2
3   -39.83  0.3 44.62   3
4   45.7    0.3 41.67   4
5   228.56  0.3 236.26  5
6   228.56  0.3 427.91  6
7   -227.47 75  427.52  1
8   -224.52 75  244.72  2
9   -77.05  75  82.56   3
10  73.36   75  82.56   4
11  229.68  75  232.92  5
12  229.68  75  427.52  6
13  -226.37 85  425.78  1
14  -224.75 85  236.55  2
15  -84.08  85  92.6    3
16  84.08   85  92.06   4
17  227.98  85  238.16  5
18  226.37  85  427.4   6
19  -226.37 95  426.39  1
20  -226.37 95  235.54  2
21  -95.4   95  102.91  3
22  93.78   95  102.91  4
23  226.37  95  235.54  5
24  236.37  95  426.39  6
25  -229.6  105 428.21  1
26  -224.75 105 240.59  2
27  -105.1  105 112.81  3
28  106.72  105 112.81  4
29  226.37  105 237.35  5
30  226.37  105 426.59  6
31  -226.37 115 426.59  1
32  -226.37 115 237.35  2
33  -114.8  115 122.52  3
34  114.8   115 124.14  4
35  227.98  115 237.35  5
36  226.37  115 426.59  6
37  -224.75 125 426.59  1
38  -226.37 125 235.74  2
39  -124.5  125 133.84  3
40  122.89  125 135.46  4
41  226.37  125 235.74  5
42  226.37  125 426.59  6
43  -226.37 135 428.21  1
44  -226.37 135 237.35  2
45  -134.2  135 143.54  3
46  135.82  135 143.54  4
47  226.37  135 237.35  5
48  226.37  135 426.59  6
49  -226.37 145 428.21  1
50  -226.37 145 237.35  2
51  -145.54 145 153.25  3
52  142.29  145 154.87  4
53  226.37  145 237.35  5
54  226.37  145 426.59  6
55  -226.37 155 428.21  1
56  -226.37 155 237.35  2
57  -174.63 155 185.4   3
58  174.63  155 185.4   4
59  226.37  155 237.35  5
60  226.37  155 426.59  6

Solution

  • I don't think rgl has a function that you need, but if the object is convex, geometry::convhulln can do what you want. Let's assume that obj contains the data you showed, then this calculates the volume of the convex hull:

    xyz <- obj[c("x", "y", "z")]
    library(geometry)
    hull <- convhulln(xyz, options = "FA")
    hull$vol
    

    This gives 20389437. The volume of the rectangular prism containing all of your points is 27863952, so that looks about right.

    If your object is not convex, then maybe the alphashape3d package has something that would help you.