extract programable filter/python annotation values to csv file

Hi all,

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))

I wonder is there a way to extract these values to csv file using code instead of option "export spreadsheet?

Two other possibilities:

  • File → SaveData
  • CSV extractor

Could you please give me the document about CSV extractor? I’ve never known this way before.
And is there a way to use code to do this?

https://docs.paraview.org/en/latest/UsersGuide/savingResults.html#extractors

1 Like

In which kind of code ? pvpython ? programmable filter ? that would be completely different.

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.

Use vtkCSVWriter

pvpython

Use SaveData

Please let me know if this is not a good idea.

Hard to say without knowing more

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.

https://vtk.org/doc/nightly/html/classvtkMassProperties.html

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