Skip to content

Commit

Permalink
Merge pull request #139 from joemarshall/medial_fixes
Browse files Browse the repository at this point in the history
Medial fixes
  • Loading branch information
pppalain authored Feb 6, 2024
2 parents 8dd6de6 + e1303c5 commit c347ed7
Show file tree
Hide file tree
Showing 11 changed files with 23,770 additions and 27 deletions.
65 changes: 46 additions & 19 deletions scripts/addons/cam/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ def __init__(self,inpoints=None, startpoints=None, endpoints=None, rotations=Non
if inpoints is None:
inpoints=[]
self.points=inpoints
self.startpoints=startpoints
self.endpoints=endpoints
self.rotations=rotations
self.startpoints=startpoints or []
self.endpoints=endpoints or []
self.rotations=rotations or []
self.depth = None

def to_chunk(self):
chunk = camPathChunk(self.points,self.startpoints,self.endpoints,self.rotations)
if len(self.points)>2 and np.array_equal(self.points[0],self.points[-1]):
chunk.closed = True
if self.depth is not None:
chunk.depth = self.depth

return chunk

# an actual chunk - stores points as numpy arrays
Expand Down Expand Up @@ -113,7 +117,7 @@ def __init__(self, inpoints, startpoints=None, endpoints=None, rotations=None):

def update_poly(self):
if len(self._points) > 2:
self.poly = sgeometry.Polygon(self._points)
self.poly = sgeometry.Polygon(self._points[:,0:2])
else:
self.poly = sgeometry.Polygon()

Expand Down Expand Up @@ -291,6 +295,18 @@ def pop(self, index):
self.endpoints.pop(index)
self.rotations.pop(index)

def dedupePoints(self):
if len(self._points)>1:
keep_points=np.empty(self._points.shape[0],dtype=bool)
keep_points[0]=True
diff_points=np.sum((self._points[1:]-self._points[:1])**2,axis=1)
keep_points[1:]=diff_points>0.000000001
self._points=self._points[keep_points,:]


def insert(self,at_index,point,startpoint=None, endpoint=None, rotation=None):
self.append(point,startpoint=startpoint,endpoint=endpoint,rotation=rotation,at_index=at_index)

def append(self, point, startpoint=None, endpoint=None, rotation=None,at_index=None):
if at_index is None:
self._points=np.concatenate((self._points,np.array([point])))
Expand Down Expand Up @@ -857,19 +873,22 @@ def parentChild(parents, children, o):

def chunksToShapely(chunks): # this does more cleve chunks to Poly with hierarchies... ;)
# print ('analyzing paths')

for ch in chunks: # first convert chunk to poly
if len(ch._points) > 2:
# pchunk=[]
ch.poly = sgeometry.Polygon(ch._points)
ch.poly = sgeometry.Polygon(ch._points[:,0:2])
if not ch.poly.is_valid:
ch.poly = sgeometry.Polygon()
else:
ch.poly = sgeometry.Polygon()

for ppart in chunks: # then add hierarchy relations
for ptest in chunks:

if ppart != ptest:
if ptest.poly.contains(ppart.poly):
# hierarchy works like this: - children get milled first.
ppart.parents.append(ptest)
if not ppart.poly.is_empty and not ptest.poly.is_empty:
if ptest.poly.contains(ppart.poly):
# hierarchy works like this: - children get milled first.
ppart.parents.append(ptest)

for ch in chunks: # now make only simple polygons with holes, not more polys inside others
found = False
Expand Down Expand Up @@ -897,14 +916,14 @@ def chunksToShapely(chunks): # this does more cleve chunks to Poly with hierarc

print('chunksToShapely oops!')

lastPt = False
lastPt = None
tolerance = 0.0000003
newPoints = []

for pt in ch._points:
toleranceXok = True
toleranceYok = True
if lastPt:
if lastPt is not None:
if abs(pt[0] - lastPt[0]) < tolerance:
toleranceXok = False
if abs(pt[1] - lastPt[1]) < tolerance:
Expand Down Expand Up @@ -936,7 +955,7 @@ def chunksToShapely(chunks): # this does more cleve chunks to Poly with hierarc

# print('chunksToShapely double oops!')

lastPt = False
lastPt = None
tolerance = 0.0000003
newPoints = []

Expand All @@ -945,7 +964,7 @@ def chunksToShapely(chunks): # this does more cleve chunks to Poly with hierarc
toleranceYok = True
# print( '{0:.9f}, {0:.9f}, {0:.9f}'.format(pt[0], pt[1], pt[2]) )
# print(pt)
if lastPt:
if lastPt is not None:
if abs(pt[0] - lastPt[0]) < tolerance:
toleranceXok = False
if abs(pt[1] - lastPt[1]) < tolerance:
Expand Down Expand Up @@ -979,8 +998,9 @@ def chunksToShapely(chunks): # this does more cleve chunks to Poly with hierarc

for polyi in range(0, len(chunks)): # export only the booleaned polygons
ch = chunks[polyi]
if len(ch.parents) == 0:
returnpolys.append(ch.poly)
if not ch.poly.is_empty:
if len(ch.parents) == 0:
returnpolys.append(ch.poly)
from shapely.geometry import MultiPolygon
polys = MultiPolygon(returnpolys)
return polys
Expand Down Expand Up @@ -1017,7 +1037,11 @@ def meshFromCurveToChunk(object):
chunk.points.append((mesh.vertices[lastvi].co + object.location).to_tuple())
# add first point to end#originally the z was mesh.vertices[lastvi].co.z+z
lastvi = vi + 1
chunks.append(chunk.to_chunk())
chunk=chunk.to_chunk()
chunk.dedupePoints()
if chunk.count()>=1:
# dump single point chunks
chunks.append(chunk)
chunk = camPathChunkBuilder()

progress('processing curve - FINISHED')
Expand All @@ -1028,10 +1052,13 @@ def meshFromCurveToChunk(object):
chunk.closed = True
chunk.points.append(
(mesh.vertices[lastvi].co.x + x, mesh.vertices[lastvi].co.y + y, mesh.vertices[lastvi].co.z + z))
chunks.append(chunk.to_chunk())
chunk=chunk.to_chunk()
chunk.dedupePoints()
if chunk.count()>=1:
# dump single point chunks
chunks.append(chunk)
return chunks


def makeVisible(o):
storage = [True, []]

Expand Down
6 changes: 4 additions & 2 deletions scripts/addons/cam/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,9 @@ def getPathPattern4axis(operation):
rot[a1] = b * anglestep
chunk.rotations.append(rot)

chunk = chunk.to_chunk()
chunk.depth = radiusend - radius
pathchunks.append(chunk.to_chunk())
pathchunks.append(chunk)

if (reverse and o.movement.type == 'MEANDER') or (
o.movement.type == 'CONVENTIONAL' and o.movement.spindle_rotation == 'CW') or (
Expand Down Expand Up @@ -540,9 +541,10 @@ def getPathPattern4axis(operation):
cutterstart.rotate(e)
cutterend.rotate(e)

chunk=chunk.to_chunk()
chunk.depth = radiusend - radius

pathchunks.append(chunk.to_chunk())
pathchunks.append(chunk)
# print(chunk.startpoints)
# print(pathchunks)
# sprint(len(pathchunks))
Expand Down
14 changes: 12 additions & 2 deletions scripts/addons/cam/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,20 @@ async def medial_axis(o):
ob.data.resolution_u = 64

polys = utils.getOperationSilhouete(o)
mpoly = sgeometry.shape(polys)
if isinstance(polys, list):
if len(polys)==1 and isinstance(polys[0],shapely.MultiPolygon):
mpoly=polys[0]
else:
mpoly=sgeometry.MultiPolygon(polys)
elif isinstance(polys,shapely.MultiPolygon):
# just a multipolygon
mpoly=polys
else:
raise CamException("Failed getting object silhouette. Is input curve closed?")

mpoly_boundary = mpoly.boundary
ipol = 0
for poly in polys.geoms:
for poly in mpoly.geoms:
ipol = ipol + 1
schunks = shapelyToChunks(poly, -1)
schunks = chunksRefineThreshold(schunks, o.medial_axis_subdivision,
Expand Down
Binary file not shown.
Loading

0 comments on commit c347ed7

Please sign in to comment.