### Convex Hull and Volume Computation

Max raised an interesting question in a comment on the discussion on the calculation of 2D polygon areas:

Question: If I have an array of 3d points, how can I do to get volume information?

Answer: The answer is maybe not quite as easy as you expected. To calculate that volume, you have to solve two tasks:

Both of these steps are non-trivial. A number of different convex hull algorithms exist both for the two-dimensional and for higher dimensional cases. Several open source libraries for solid modelling or computational geometry implement these. One of the best known and most reliable tools which is specifically targeted at these two issues is Qhull. Here is the blurb from its home page:

Qhull computes the convex hull, Delaunay triangulation, Voronoi diagram, halfspace intersection about a point, furthest-site Delaunay triangulation, and furthest-site Voronoi diagram. The source code runs in 2-d, 3-d, 4-d, and higher dimensions. Qhull implements the Quickhull algorithm for computing the convex hull. It handles roundoff errors from floating point arithmetic. It computes volumes, surface areas, and approximations to the convex hull.

I downloaded the current version of Qhull to explore exactly what you might be able to use for your purposes. The Qhull distribution includes a list of sample programs. One of these is qconvex, and one of its output options is FA to compute total area and volume of the input points, which is pretty exactly what you want.

Now how can you make use of this inside the Revit API?

Qhull is implemented in standard C, the Revit API is a .NET environment. The easiest way to make use of C source code from .NET is to compile a DLL and call it from .NET. In this case you would need to analyse the source code for the qconvex program, which is contained in the file qconvex.c, and package the required functionality in a DLL that you make accessible from .NET.

The relevant lines of code from qconvex.c are:

```  qh_checkflags (qh qhull_command, hidden_options);
qh_initflags (qh qhull_command);
qh_init_B (points, numpoints, dim, ismalloc);
qh_qhull();
qh_check_output();
qh_produce_output();
```

This program reads its input from a file or the console standard input stream, performs its calculations, and produces its output into a file or the standard output stream. You would need to adapt this to pass the information from and to the .NET calling application.

Another alternative, possible much simpler, but obviously less efficient, would be to set up the .NET application to write and read files in the expected Qhull input and output formats and then execute the unchanged qconvex executable provided by the Qhull package.

Reply from Max: Very good this library, but we need a wrapper for .NET, and how can I encapsulate this library in a C# project?