Comments and answers for "Finding volume of two 3D meshes intersection."
http://answers.unity.com/questions/1178637/finding-volume-of-two-3d-meshes-intersection.html
The latest comments and answers for the question "Finding volume of two 3D meshes intersection."Comment by romajiMA on romajiMA's comment
http://answers.unity.com/comments/1192334/view.html
Also, note, I am working with a special case where my intersector is a box, and I only care about it being filled from the bottom.Wed, 25 May 2016 14:53:19 GMTromajiMAComment by Whipexx on Whipexx's answer
http://answers.unity.com/comments/1192245/view.html
This is basically what I concluded myself, the issue here resides in the Boolean operation, for which I found no working built in solution. And I'm pretty sure that implementing Boolean Operations to solve the issue might be overkill.
That being said, if no other alternative is proposed I'll accept this as the answer in a while; but I still would love to find a working solution, not for me (since I evaded the problem) but for future generations.Wed, 25 May 2016 11:28:44 GMTWhipexxComment by romajiMA on romajiMA's comment
http://answers.unity.com/comments/1192005/view.html
@Bunny83 those are not vertices.
They're numbers. Each triple is a vector
And actually, I'm splitting into those and triangular prisomsWed, 25 May 2016 02:09:11 GMTromajiMAComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1191965/view.html
A four sided pyramid? That's a very special shape with a lot restrictions. If you have an arbitrary mesh, how would you split that into such pyramids? Your pyramid would need a quad as base. All four corners of that quad need to be coplanar. What if that base quad isn't a rectangle i.e. some non-regular polygon? Working with tetrahedrons is way simpler as it doesn't matter how it looks like. A single four sided pyramid can always be split into two tetrahedrons. Basically any convex closed triangle mesh can be splited into tetrahedrons. In addition any concave mesh can be split into several convex meshes. See [this post for an example][1].
However holes inside a mesh (like a torus / donut) makes it quite difficult to split. There are many special cases to consider. However if you know how your geometry looks like and what "features" it can have any algorithm can be simplified / optimised.
ps: a four sided pyramid still only has 5 vertices (A,B,C,D,E), so what's "F"?
[1]: http://gamedev.stackexchange.com/questions/53142/decomposing-a-concave-mesh-into-a-set-of-convex-meshes/67901Tue, 24 May 2016 23:46:25 GMTBunny83Comment by romajiMA on romajiMA's comment
http://answers.unity.com/comments/1191675/view.html
I meant a pyramid with a one 4 sided part.
And, I am currently working on a solution to cutting up into those shapes in python, then to convert it into C# for use in unity.
I will post the python solution when I have itTue, 24 May 2016 13:47:25 GMTromajiMAComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1191673/view.html
I'm not sure what "E" and "F" should be in your formula as a tetrahedron has only 4 vertices 6 edges and 4 faces.
The tripple product does exactly what you said in your comment. The magnitude of the cross product between two vectors is equal to the area of the parallelogram that those vectors define. Half of that area is the area of the base triangle. The height of the tetrahedron can't be calculated that easy from the length of the edges without some trig or "Pythagoras".
However since the vector that is returned from the cross product is perpendicular to the base, we just have to project one of the edges to the top vertex onto that normal vector to get the height. Since the length of the "normal vector" is already equal to the area of the base, the result is the volume of the "parallelepiped" defined by those 3 vectors. The volume of the tetrahedron with the same 3 vectors has 1/6 the volume as the "parallelepiped".
However as i said the main problem is not to calculate the volume of those simple shapes, but the process to break it down into those.Tue, 24 May 2016 13:43:50 GMTBunny83Comment by romajiMA on romajiMA's answer
http://answers.unity.com/comments/1191170/view.html
Can't you calculate the volume through the area of one of the sides and the average height of the ones above?
So, to calculate the volume of a pyramid with points (<0,0,0>,,,,) you could use ((E+F)|AD-BC|)/12 ?
I think I'm coming up with a solution right now, that seems simpler.Mon, 23 May 2016 16:29:23 GMTromajiMAAnswer by Bunny83
http://answers.unity.com/answers/1191028/view.html
Yes, this is a quite difficult problem to solve. I doubt there are any reliable approximation methods which always work. The usual straight forward approach would involve some advanced algorithms:
- First you need to clip your mesh so the resulting mesh only contains the "wanted" volume. This first step is the most complicated. You basically need a [CSG intersect algorithm][1] which can create a new mesh out of the intersection of two (boolean operation). You should clip the more complex mesh on the simple mesh. So all triangles of the complex mesh outside the simple mesh are ignored. The triangles which intersect the simple mesh need to be clipped. So new vertices need to be calculated and new triangle faces created.
- Once you have your final mesh You might want to test if the mesh is a convex or concave mesh. If it's concave you need to split it into several convex meshes.
- Once you have only convex volume meshes you can simply pick the center of each sub mesh and treat each triangle of the mesh as a tetrahedron. This does only work with convex meshes. The sum of those tje volumes of all those tetrahedrons is your wanted volume.
- To calculate the volume of a tetrahedron you just need to use [the tripple product][2] and divide by 6. The link that Eno Khaon posted above also uses the tripple product, but calculates it "manually".
The tripple procuct in Unity would look like this, given the 4 corner points of our tetrahedron:
public static float CalcVolumeTetrahedron(Vector3 p0, Vector3 p1,Vector3 p2,Vector3 p3)
{
Vector3 a = p1-p0;
Vector3 b = p2-p0;
Vector3 c = p3-p0;
return Mathf.Abs(Vector3.Dot(Vector3.Cross(a,b),c) / 6f);
}
The `Mathf.Abs` is needed, otherwise the volume could be negative depending on the order of the vertices.
I don't think there's a simpler way to do this.
One very rough approximation would be to create a voxel grid around your mesh and do a "contains check". This however can be a bit tricky for concave meshes so you might want to split the complex mesh into several convex meshes. So you would have to check every point inside the simple mesh if it's inside of any of the convex meshes. Finally just count those which are inside and divide by the total number of voxels inside the simple mesh. The smaller your voxel grid is the better the approximation but the computational complexity increases by the power of 3.
A point is inside a convex mesh if the point is behind **all** planes that each triangle create. Unity's [Plane struct][3] might be helpful here.
[1]: https://en.wikipedia.org/wiki/Constructive_solid_geometry
[2]: https://en.wikipedia.org/wiki/Triple_product
[3]: http://docs.unity3d.com/ScriptReference/Plane.htmlMon, 23 May 2016 11:15:57 GMTBunny83Comment by Whipexx on Whipexx's comment
http://answers.unity.com/comments/1190981/view.html
romajiMA:
Not sure I understood your question but you can do that either using a unity built in method (http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html), by manually taking into account the transforms or even by changing this objects parent to the one you wan't to know the relative position. It all depends on the purpose.Mon, 23 May 2016 09:05:06 GMTWhipexxComment by Eno-Khaon on Eno-Khaon's comment
http://answers.unity.com/comments/1188779/view.html
Volume of a (closed) mesh can be calculated by cycling through each triangle on the mesh and adding/subtracting their relative "volume" together.
http://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up
In order to get a precise intersection, you would need to perform a boolean operation and determine the properties of the new "region" created by their intersection.
I don't think there's any good means of making that cheap, though.
Instead, if there's likely to always be a primitive involved in the collision, you can typically assume their center will be right in the middle of the object. Based on that, you can automatically assume that any intersection between two convex objects will result in a greater percentage of the primitive's volume being on the side its center is at.Wed, 18 May 2016 19:16:14 GMTEno-KhaonComment by romajiMA on romajiMA's comment
http://answers.unity.com/comments/1188769/view.html
Sorry for the late reply.
Is there a way of getting vertexes in the local space of another object? If so, then getting the mesh points should be easyWed, 18 May 2016 18:52:10 GMTromajiMAComment by Whipexx on Whipexx's comment
http://answers.unity.com/comments/1188087/view.html
Knowing whether a mesh point is inside or not of the cube might be tricky. Can you reasonably go through all the candidate vertexes? (if your mesh is either simple or subdivided into a bunch of simpler meshes this could be feasible), given some points finding wether they are or not inside the cube could be solved with a raycast for each of them.Tue, 17 May 2016 13:37:43 GMTWhipexxComment by romajiMA on romajiMA's comment
http://answers.unity.com/comments/1187239/view.html
I'm making a PC game, where an operation like this will run probably 2 times a frame.
What I was looking for is a way to see if this box is over half way into the ground, and then pushing the player up enough to be half way in. I don't care about the other sides of the box being filled, just what's underneath
How would you get the points of the mesh inside the box? That, along with 4 raytraces for each corner, seems like it would be enough.Sun, 15 May 2016 21:40:26 GMTromajiMAComment by Whipexx
http://answers.unity.com/comments/1187199/view.html
Hi, I'm afraid I found no plugin that solved my issue. I couldn't come up with an efficient way of solving this either.
If you are not so concerned as me for efficiency you could try finding the mesh vertexes inside the area (that's easy) and then preserve the already existing triangles in it and create new ones, a triangle strip like algorithm probably works just fine; with this it only remains to invert normals and generate a new mesh that combines both previous one, a simple built in mesh.combinemeshes *might* work. This, however, seems too much work for a task this simple.
What I did and thus can say definetely works is to simplify the problem, I realized that I didn't need a precise value for my volume, an aproximation that never underestimates will suffice. To get this I did use boxcast to find the lowest point of the mesh below my cube. With that and knowing the relative position of the cube to the mesh it comes down to a simple cross-multiplication to find out the ceiling of the volume. Im sure that taking this route and doing a couple more boxcasts (or raycasts) you can approximate a value good enough for most cases.Sun, 15 May 2016 19:53:22 GMTWhipexxComment by romajiMA
http://answers.unity.com/comments/1187175/view.html
I'm interested in this question too.
What plugins did you find?Sun, 15 May 2016 19:09:44 GMTromajiMA