I wrote the code below using the Programable Filter to extract the volume/area of the object. Not sure if this is a optimised way or not? Please share if there is a better way.
import numpy as np
import vtk
input0 = inputs[0]
numTriangles = input0.GetNumberOfCells()
volumeArray = np.empty(numTriangles, dtype=np.float64)
areaArray = np.empty(numTriangles, dtype=np.float64)
for i in range(numTriangles):
cell = input0.GetCell(i)
p1 = np.array(input0.GetPoint(cell.GetPointId(0)))
p2 = np.array(input0.GetPoint(cell.GetPointId(1)))
p3 = np.array(input0.GetPoint(cell.GetPointId(2)))
# Compute the area of the triangle
side1 = np.linalg.norm(p2 - p1)
side2 = np.linalg.norm(p3 - p1)
side3 = np.linalg.norm(p3 - p2)
s = 0.5 * (side1 + side2 + side3)
areaArray[i] = np.sqrt(s * (s - side1) * (s - side2) * (s - side3))
# Assuming the volume is computed for a tetrahedron with a dummy point p4
p4 = np.array([0,0,0]) # Use barycenter of surface for better results.
volumeArray[i] = vtk.vtkTetra.ComputeVolume(p1, p2, p4, p3)
output.CellData.append(volumeArray, "Volume")
output.CellData.append(areaArray, "Area")
After that, I added Python Annotation filter to show the Volume/Area of this object
"Volume in cubic centimeters: %f\nArea in square centimeters: %f" % (1000000 * sum(Volume), 10000 * sum(Area))
Either of them would be good, my goal to extract the area, volume, and dimension into the csv file. I’ve already successfully extracted the dimension using Python Shell (refer to this Bounding Ruler filter: adding an option to show X, Y, Z delta - #8 by NinhGhoster). Hopefully, I can extract these the area and volume into the same csv file by using code since I don’t know a way how to integrate GUI and code together.
Each time step is a unique object. I intend to run the code automated to get these data from all steps. Please let me know if this is not a good idea.
Just wanted to share a potential way to simplify. Instead of writing the algorithm from scratch, you may consider using the vtkMassProperties class in your python script to compute the object properties.
Thanks. As you can see in the beginning post, I used vtk to get area and volume of the object. Because it’s 3D object, it’s quite difficult to get dimension. I extracted the bounding box dimension instead.
@sherin.sugathan@mwestphal I made a short video to explain what I want to achieve, it would be appreciated if you could take look and give feedback for me, thank you
The reason I am trying to automate it is that I have hundreds of objects that need to be scanned