Author Topic: Usable code for TIN volume calculation  (Read 8670 times)

0 Members and 1 Guest are viewing this topic.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Usable code for TIN volume calculation
« on: January 29, 2013, 04:16:30 AM »
Does anyone have free to use code for calculating volumes from triangluated data, i.e. two collections of 3D Faces or two collections of 3D points?

After searching the internet for weeks I only found information about how to triangulate, so I can create a TIN of 3D faces, but could not find information about calculating the differences between two collections of 3D Faces.

Of course I can dust my old school books with the math stuff, and try to write functions myself, but that will take ages. Also I probably have to face challenges like partly overlapping tins.

So if there are examples available I would really appriciate to see them.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

wizman

  • Bull Frog
  • Posts: 290
Re: Usable code for TIN volume calculation
« Reply #1 on: January 29, 2013, 06:06:22 AM »
i don't know if this will help but here goes,

I remember a long time ago that there is a lisp to project each 3D faces to a datum say 0 thus transforming it to a solids.

After projecting each surface, using union  2 main solids can be generated, one base and one final.

Using AutoCAD's subtract command:

Base solid minus final solid produces a solid whose volume is the CUT.

final solid minus the base solid produces a solid whose volume is the FILL

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: Usable code for TIN volume calculation
« Reply #2 on: January 29, 2013, 07:14:35 AM »
Does anyone have free to use code for calculating volumes from triangluated data, i.e. two collections of 3D Faces or two collections of 3D points?
What about this code: http://through-the-interface.typepad.com/through_the_interface/2009/07/triangulating-an-autocad-3d-solid-from-a-set-of-points-using-net.html

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Usable code for TIN volume calculation
« Reply #3 on: January 29, 2013, 08:18:45 AM »
...
What about this code: http://through-the-interface.typepad.com/through_the_interface/2009/07/triangulating-an-autocad-3d-solid-from-a-set-of-points-using-net.html

I've seen it, but I'm not sure if this is the solution. I can't see a difference between two surfaces, just creating solids to a given level. Also the performance is really slow (only 5000 points in 2 till 4 minutes).
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Usable code for TIN volume calculation
« Reply #4 on: January 29, 2013, 08:19:45 AM »
i don't know if this will help but here goes,

I remember a long time ago that there is a lisp to project each 3D faces to a datum say 0 thus transforming it to a solids.

After projecting each surface, using union  2 main solids can be generated, one base and one final.

Using AutoCAD's subtract command:

Base solid minus final solid produces a solid whose volume is the CUT.

final solid minus the base solid produces a solid whose volume is the FILL

Performance is a problem with solid shaping.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Usable code for TIN volume calculation
« Reply #5 on: January 29, 2013, 10:20:08 AM »
I haven't found something specific, either.  *But* if you don't need hyper-accuracy, consider my findings.  What you are looking for is the volume of a triangular prism, ie. area x length.  The top, bottom, or both may be sloped ends which means the length is only easy to find if you truncate the prism at the the point where the slope begins.  That just leaves a small portion of the volume to be determined at the top, or bottom, or both.  If you treated the remaining solid as clay or other malleable substance, you could start slicing parts off the very top to gradually fill in the "missing space" to form a regular triangular prism.  Evenutually you would end up with a triangular prism without a sloped surface.  If you took the average elevation of the three points, or used the center (can't remember the mathematical term) of the triangle, or other logical point as this length you should get quite close to your requirements.  Especially considering the equipment to be used and the material to be worked.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Usable code for TIN volume calculation
« Reply #6 on: January 31, 2013, 02:57:54 AM »
It is not exactly what I was looking for, but thanks anyway for your effort. Have to look further :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Usable code for TIN volume calculation
« Reply #7 on: January 31, 2013, 10:35:09 AM »
There are a few forumlas for volumes of truncated pyramids, but they require data that isn't readily available.  Anything more complex than what I've considered is usually best left to Civil3D.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Usable code for TIN volume calculation
« Reply #8 on: January 31, 2013, 11:04:57 AM »
Can you share an example of the point clouds/TIN you are dealing with?  I've got some idea's but they would have limitations depending on your situation.  I'm particularly curious about how you expect to see overlapping TINs.

The simplest case would be a regular shape like a soccer ball, we could pick an arbitrary point in the middle and calculate the volume of the many triangular prisms enclosed


The top, bottom, or both may be sloped ends which means the length is only easy to find if you truncate the prism at the the point where the slope begins.  That just leaves a small portion of the volume to be determined at the top, or bottom, or both.
Consider the following:


If we know the base triangle from our TIN then we can create the line b and get the point where h intersects b using b.GetPointClosestTo(OppositeCorner), we can then determine where H intersects h using h.GetPointClosestTo(TopVertex) and the volume easily follows as |b|*|h|*|H|/6 where |b| is the length of b, etc.

I can't imagine your situation will be this simple, but an example showing how complex would be helpful.

Cheers

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Usable code for TIN volume calculation
« Reply #9 on: January 31, 2013, 11:23:25 AM »
The problem I ran into is the bottom face isn't necessarily level - the three points could be at different elevations.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Usable code for TIN volume calculation
« Reply #10 on: January 31, 2013, 11:52:42 AM »
This method will accommodate that scenario, the worst case we would run into is where you need to extend the lines because the shape is so skewed.  Post an example and I'll do my best to guide you through it.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Usable code for TIN volume calculation
« Reply #11 on: January 31, 2013, 12:05:28 PM »
Correction:

Sorry, just realized that looking at the simplified example I forgot that we use the closest point on a line to get h, but to get H we will need to create a plane from the base triangle and H is the distance from the top vertex to the closest point on the plane

Had to brush the dust off my math skills...

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Usable code for TIN volume calculation
« Reply #12 on: January 31, 2013, 12:37:23 PM »
I tested this on a very basic case, a triangle base (0,0,0),(0,1,0),(1,0,0) and a top vertex (1,1,1) so that the top vertex was not normal to any point of the triangle face. If you can please find a way to break it for me:

Code - C#: [Select]
  1. [CommandMethod("testPyramidalVolume")]
  2.         public void TestVolume()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;
  7.             Point3d bP1 = new Point3d();
  8.             Point3d bP2 = new Point3d();
  9.             Point3d bP3 = new Point3d();
  10.             Point3d tP = new Point3d();
  11.             for (int i = 0; i < 4; i++)
  12.             {
  13.                 PromptPointResult ppr = ed.GetPoint("\nSelect point");
  14.                 if (ppr.Status != PromptStatus.OK)
  15.                 {
  16.                     ed.WriteMessage("\nPrompt point failed!");
  17.                     return;
  18.                 }
  19.                 switch (i)
  20.                 {
  21.                     case 0:
  22.                         bP1 = ppr.Value;
  23.                         break;
  24.                     case 1:
  25.                         bP2 = ppr.Value;
  26.                         break;
  27.                     case 2:
  28.                         bP3 = ppr.Value;
  29.                         break;
  30.                     case 3:
  31.                         tP = ppr.Value;
  32.                         break;
  33.                     default:
  34.                         break;
  35.                 }
  36.             }
  37.             ed.WriteMessage("\nVolume calculated: {0}", TriangularPyramidalVolume(bP1, bP2, bP3, tP));
  38.         }
  39.  
  40.    
  41.  
  42.         public double TriangularPyramidalVolume(Point3d bP1, Point3d bP2, Point3d bP3, Point3d tP)
  43.         {
  44.             Line b = new Line(bP1, bP2);
  45.             Point3d i = b.GetClosestPointTo(bP3, true);
  46.             Vector3d v12 = new Vector3d(bP1.X - bP2.X, bP1.Y - bP2.Y, bP1.Z - bP2.Z);
  47.             Vector3d vi3 = new Vector3d(i.X - bP3.X, i.Y - bP3.Y, i.Z - bP3.Z);
  48.             Vector3d normal = v12.CrossProduct(vi3);
  49.             Plane p = new Plane(bP1, normal);
  50.             PointOnSurface Hs = p.GetClosestPointTo(tP);
  51.             Point3d pH = Hs.GetPoint();
  52.             Vector3d vptP = new Vector3d(tP.X - pH.X, tP.Y - pH.Y, tP.Z - pH.Z);
  53.             return v12.Length * vi3.Length * vptP.Length / 6;
  54.         }
  55.  

Cheers

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Usable code for TIN volume calculation
« Reply #13 on: February 01, 2013, 02:02:10 AM »
The reason of my question is that a lot of surveyors need a tool for calcultating accurate volumes, but don't have budget for a Civil3D licence. Which is also only used as a very expensive AutoCAD. So I thought about creating a tool that does the math in standard AutoCAD or BricsCAD (or even stand alone, since it is just math).

I found a lot of useful information here: http://paulbourke.net/papers/triangulate/

I think the best way to keep the surfaces together is creating 3D Faces, using the Delaunay method, and put the 3D Faces in a block. This way you can keep the surfaces seperated inside a drawing.

To create volume surfaces, two surfaces (basically two collections of 3D faces), need to be compared. The way Civil3D it does, is very practical, it creates new 3D faces between the crossing lines and the corners of each face get the difference in distance. The final calculation is always towards a zero base. Besides that, it is easy to show the cut and fill locations because all the faces below zero are cut volumes.

The last part of this, creating new triangles between overlapping 3D faces, is the most complicated I think. Of course I can start looping all faces of one surface to compare them with all the faces in a second surface, to see if they overlap. But it will not be an efficient method. Probably there are better ways.
Then you need to find the apparent intersection of the lines of the faces, to be able to create new triangles.

Then, with simple math, it is possible to find the volumes of every triangle in the volume surface.

In the images below you see two overlapping surfaces and a new created volume surface (red). That is how I would like to gain.

The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8764
  • AKA Daniel
Re: Usable code for TIN volume calculation
« Reply #14 on: February 01, 2013, 07:30:59 AM »