Menu

Calculate volume of a 3d object

Help
2016-12-19
2016-12-20
  • Ørjan Nilsen

    Ørjan Nilsen - 2016-12-19

    Is there function in the GLScene library to calculate the volume of a 3d object?

     
  • Pavel Vassiliev

    Pavel Vassiliev - 2016-12-20

    No, in general case you need to subdivide an object on pyramids or tetrahedra and then calculate the total volume. See e.g. http://wwwf.imperial.ac.uk/~rn/centroid.pdf and https://www.wikiwand.com/en/Polyhedron#/Volume. Thus in GLScene it's necessary to add some common functions to calculate areas and volumes in GLVectorgeometry unit and then to implement methods in GLGomObjects and GLPolyhedron units. Then the work will be done.
    PW

     
  • Ørjan Nilsen

    Ørjan Nilsen - 2017-01-30

    What if the 3d object was loaded into GLScene from a STL file? If I understand correctly the objects in STL files are already subdivided on triangles. Is this lost after import to GLScene, or do I still have access to all the triangles for volume calculation?

     
  • Roman

    Roman - 2017-01-30

    Hi.
    Use MeshObjects.ExtractTriangles

    procedure STL_Triangles(FF:TGLFreeForm); //FF <- LoadFromFile('1.STL');
    var i:integer;
    dataFace:TSTLFace;
    list:TaffineVectorlist;
    begin
      list:=FF.MeshObjects.ExtractTriangles;
      if list.Count>0 then
      try
        i:=0;
        with dataFace do
        while i<list.Count do
        begin
          normal:=CalcPlaneNormal(list[i], list[i+1], list[i+2]);
    
          //Fix Matrix (Position, Direction etc.)
          v1:=VectorTransform(list[i],TGLFREeform(FF).AbsoluteMatrix);
          v2:=VectorTransform(list[i+1],TGLFREeform(FF).AbsoluteMatrix);
          v3:=VectorTransform(list[i+2],TGLFREeform(FF).AbsoluteMatrix);
          {
          //Normals:
          normal.X, normal.Y, normal.Z
          //Vertex:
          v1.X, v1.Y, v1.Z
          v2.X, v2.Y, v2.Z
          v3.X, v3.Y, v3.Z
          }
          Inc(i, 3);
        end;
      finally
        list.Free;
      end;
    end;
    
     

    Last edit: Roman 2017-01-30
  • Ørjan Nilsen

    Ørjan Nilsen - 2017-02-03

    Thank you for your help. By adding som lines to your procedure I get the volume of a STL file:

    function TMainForm.STL_Triangles(FF:TGLFreeForm): Single;
    var
      i:integer;
      dataFace:TSTLFace;
      list:TaffineVectorlist;
    begin
      Result:=0;
      list:=FF.MeshObjects.ExtractTriangles;
      if list.Count>0 then
      try
        i:=0;
        with dataFace do
        while i<list.Count do
        begin
          normal:=CalcPlaneNormal(list[i], list[i+1], list[i+2]);
    
          //Fix Matrix (Position, Direction etc.)
          v1:=VectorTransform(list[i],TGLFREeform(FF).AbsoluteMatrix);
          v2:=VectorTransform(list[i+1],TGLFREeform(FF).AbsoluteMatrix);
          v3:=VectorTransform(list[i+2],TGLFREeform(FF).AbsoluteMatrix);
          {
          //Normals:
          normal.X, normal.Y, normal.Z
          //Vertex:
          v1.X, v1.Y, v1.Z
          v2.X, v2.Y, v2.Z
          v3.X, v3.Y, v3.Z
          }
          Inc(i, 3);
          Result:= Result + VectorDotProduct(v1, VectorCrossProduct(v2, v3));
        end;
        Result:= Result / 6;
      finally
        list.Free;
      end;
    end;
    

    Just wondering, why wouldn’t a method for calculating the volume of a TmeshObject be part of GLScene? Is it beyond the scope of this library, or is it that nobody has bothered to add it yet?

     

    Last edit: Ørjan Nilsen 2017-02-03
  • Pavel Vassiliev

    Pavel Vassiliev - 2017-02-03

    Hi Swein,
    Well, also In GLVectorGeometry unit there are function TriangleArea(const p1, p2, p3: TAffineVector): Single; function TriangleSignedArea(const p1, p2, p3: TAffineVector): Single; function PolygonArea(const p: PAffineVectorArray; nSides: Integer): Single; function PolygonSignedArea(const p: PAffineVectorArray;
    that help to calculate features of other GLObjects and not only meshes. But I agree that it’s would be useful to have more methods like Length, Area and Volume functions in TMeshObject class. The volume makes sense only for closed meshes. So give your variants to be included in GLScene after testing it on stl, 3ds etc. formats and e.g. for polyhedrons in torque demo.
    PW

     
  • Pavel Vassiliev

    Pavel Vassiliev - 2017-02-09

    Hi Swein,
    I’ve added function Volume in TGLMeshObjectList of GLVectorFileObjects unit in current SVN, so try to calculate it for your stl mesh file (e.g. ffObject.MeshObjects.Volume).
    PW

     
  • Ørjan Nilsen

    Ørjan Nilsen - 2017-02-10

    Hi Pavel. Thank you for adding the volume function. I also noticed that you added the volume to the statusbar of GLSViewer. Now I can use GLScene for getting this information without adding anything to the librarary. But I think you have made a "copy and paste" mistake to the function. You use the same index (List[i]) for all three vectors:

          Tri.V1 := VectorTransform(List[i], TGLBaseSceneObject(Owner).AbsoluteMatrix);
          Tri.V2 := VectorTransform(List[i], TGLBaseSceneObject(Owner).AbsoluteMatrix);
          Tri.V3 := VectorTransform(List[i], TGLBaseSceneObject(Owner).AbsoluteMatrix);
    
     

    Last edit: Ørjan Nilsen 2017-02-10
  • Pavel Vassiliev

    Pavel Vassiliev - 2017-02-10

    Swein,
    Repaired, check it with your dataset
    PW

     
  • Ørjan Nilsen

    Ørjan Nilsen - 2017-02-13

    I think I get the right volume for my STL files now. GLScene gives me the exact same volume for my files as I get from these two online services:
    http://www.viewstl.com/
    http://www.planfab.eu/price-calculator/

     

    Last edit: Ørjan Nilsen 2017-02-13

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.