Intersection 3D meshes python
Asked Answered
S

3

11

I just started to work with 3D meshes, oriented to be used for finite element analysis. I would like to model inclusions for materials (any shape, but mainly interested in spheres and ellipsoids) in a cube-like matrix. These inclusions shouldn't be coincident with each other.

So I was thinking to use some sort of package for python which can plot primitive objects, look for their intersection (if it exists) and export the geometry (or mesh it and export it). In case of spheres, I was coding my own solution, but I think it will be very difficult to expand it for any other shape than spheres.

After a couple of days looking for an appropriate library/module, I can't decide which one I should use.

I would like to use python as the main language since it is easy, open source and I already know a bit. Hence checked this link: Good geometry library in python?

But:

  • SymPy. It seems it is only for 2D objects.
  • pyeuclid. Looks discontinued in its google code page.
  • CGAL. It seems the most advanced, but I can't find if it does intersections.
  • geometry-simple. Also discontinued, and I believe it does not handle 3D objects.
  • pythonocc. The lack of documentation is an important issue here.
  • Open mesh. It seems a good alternative but I can't see if it does intersections.

I might be missing some features or libraries. Sorry about it.

If it is really necessary and useful, I could try to use C++ which it seems has more libraries for geometry as stated in here, here and here. Also, I think I should mention I work mainly on Windows 10, and my experience with Linux is little but I could challenge myself.

To summarize my question: Is there any good library in python (if possible) which can find intersections between 3D objects? Am I missing any feature or library I should know? I would appreciate a lot any suggestion in the right direction.

Thank very much in advance!

Specialism answered 23/7, 2018 at 10:32 Comment(2)
I like your well-crafted question. But I am afraid, as it is, it is off topic for SOCoz
Hi @Mr.T What a pity.... I will try to re-write it, maybe focusing more on a software. Maybe gmsh. Thank very much!Specialism
O
7

You might want to check out pygalmesh (out of my zoo). It interfaces CGAL and as such uses level-set functions for mesh construction. All domain combinations (intersections, unions, differences) are implemented.

enter image description here

Oosperm answered 12/11, 2019 at 15:52 Comment(1)
Thanks very much for your answer @Nico. It seems a very good solution to my problem! RegardsSpecialism
S
2

Is there any good library in python (if possible) which can find intersections between 3D objects?

A good option is to use open-source library MeshLib, written in C++ but also having a Python interface and package downloadable by pip install.

A code example for subtracting two spheres (similar to the other answer) using MeshLib:

import meshlib.mrmeshpy as mr

# create a mesh of default sphere with radius 1:
sparams = mr.SphereParams()
smallSphere = mr.makeSphere(sparams)

# create a large sphere with more points and triangles
sparams.numMeshVertices = 400
sparams.radius = 2
bigSphere = mr.makeSphere(sparams)

# shift smaller sphere from the origin on 2 units along Z axis:
transVector = mr.Vector3f()
transVector.z = 2
diffXf = mr.AffineXf3f.translation(transVector)
smallSphere.transform(diffXf)

# find the difference mesh between two spheres:
diff = mr.boolean( bigSphere, smallSphere, mr.BooleanOperation.DifferenceAB )

# save the result in file:
mr.saveMesh(diff.mesh, mr.Path("diffSpheres.stl"))

The result is something like this: enter image description here

Suttles answered 4/10, 2022 at 16:43 Comment(0)
C
0

I am posting this to help complete the list of solutions

A very pythonic library for this is pymadcad (which I am the author of).

It is meant to manipulate and generate triangular meshes to create 3D models of mechanical parts, including boolean operations. It may be suited for your application, at least the operations you are looking for should be in it, since your meshes are supposed to represent physical objects.

A simple example of mesh intersection:

from madcad import *

a = icosphere(vec3(0,0,0), 1)
b = icosphere(vec3(1,0,0), 0.8)
result = difference(a, b)

io.write(result, '/tmp/result.stl')
show([result])

You will get the following:

resulting mesh

Of course you can do the same with imported meshes. The only point for your application is that pymadcad has not function to generate triangulations dedicated to finite elements, meaning ith the madcad operations you will likely end up with meshes containing thin triangles which are not greate for finite elements.

C answered 13/10, 2023 at 19:17 Comment(1)
Hi, may I know is it possible to use your pymadcad to intersect a closed 3d mesh surface with a open mesh surface?Amando

© 2022 - 2024 — McMap. All rights reserved.