MeshLib icon indicating copy to clipboard operation
MeshLib copied to clipboard

Minkowski Sum in python

Open tobias-scheepers opened this issue 1 year ago • 2 comments

I'm looking to perform a minkowski sum of two meshes. It seems that in C++ there is a function to do so for a mesh and a curve. Would it be possible to add these to the exposed python functions. I'd like to compute the minkowski sum for every contour coming from slicing the original shape. As a whole I'm looking for the functions to perform the following operations:

  1. Offset a shape (shape_a) to the inside
  2. Slice the offsetted shape using a plane to create contours at different heights
  3. Per contour perform a minkowski sum using a tool mesh (shape_b)
  4. Intersect the shape_b and shape_a to find the collision areas
  5. Computed the closest point for all vertices in shape_a with respect to the shape_b to find all the areas that could not be reached by the tool.

tobias-scheepers avatar Feb 26 '24 13:02 tobias-scheepers

Hello!

I think function makeMovementBuildBody can be helpful to you (if tool mesh has axial symmetry) we will expose it to python:

from meshlib import mrmeshpy as mm
inputMesh = mm.makeCube() # shape_a mm.loadMesh("shape_a,stl")
box = inputMesh.computeBoundingBox()
step = (box.max.z - box.min.z)/11
start = box.min.z + step/2

# create tool profile
bodyContours = mm.Contours3f()
bodyContours.resize(1)
bodyContours[0].resize(5)
bodyContours[0][0] = mm.Vector3f(-0.01,-0.01,0)
bodyContours[0][1] = mm.Vector3f(0.01,-0.01,0)
bodyContours[0][2] = mm.Vector3f(0.01,0.01,0)
bodyContours[0][3] = mm.Vector3f(-0.01,0.01,0)
bodyContours[0][4] = bodyContours[0][0] # make sure that profile is closed

sumMesh = mm.Mesh()

while start < box.max.z:
	plane = mm.Plane3f()
	plane.n = mm.Vector3f(0,0,1)
	plane.d = start
	plSections = mm.extractPlaneSections(inputMesh, plane)
	contours = mm.Contours3f()
	contours.resize( plSections.size() )
	for i in range(len(plSections)):
		contours[i].resize(plSections[i].size())
		for j in range(plSections[i].size()):
			contours[i][j] = inputMesh.edgePoint( plSections[i][j] )
	moveMesh = mm.makeMovementBuildBody(bodyContours,contours)
	sumMesh.addPartByMask(moveMesh,moveMesh.topology.getValidFaces())
	start = start + step;
	pass

mm.saveMesh(sumMesh,"sumMesh.stl")
# offset here to fix possible self-intersections and internal surface parts
sumMesh = mm.offsetMesh(sumMesh,0)
mm.saveMesh(sumMesh,"sumMeshOff.stl")

image

Will it work for you?

Grantim avatar Feb 26 '24 16:02 Grantim

That looks perfect! Thanks for the amazing quick response. I'll test and confirm back here if I run into anything further. So far I'm duly impressed by the speed of the library.

tobias-scheepers avatar Feb 27 '24 12:02 tobias-scheepers