def GetMoreTerminalPoints(shape, pts, winRad, maxGridVal, targetNumber=5): """ adds a set of new terminal points using a max-min algorithm """ shapeGrid = shape.grid shapeVect = shapeGrid.GetOccupancyVect() nGridPts = len(shapeVect) # loop, taking the grid point with the maximum minimum distance, until # we have enough points while len(pts) < targetNumber: maxMin = -1 for i in range(nGridPts): if shapeVect[i] < maxGridVal: continue minVal = 1e8 posI = shapeGrid.GetGridPointLoc(i) for currPt in pts: dst = posI.Distance(currPt.location) if dst < minVal: minVal = dst if minVal > maxMin: maxMin = minVal bestPt = posI count, centroid = Geometry.ComputeGridCentroid(shapeGrid, bestPt, winRad) pts.append(SubshapeObjects.SkeletonPoint(location=centroid))
def FindGridPointBetweenPoints(pt1,pt2,shapeGrid,winRad): center = pt1+pt2 center /= 2.0 d=1e8 while d>shapeGrid.GetSpacing(): count,centroid=Geometry.ComputeGridCentroid(shapeGrid,center,winRad) d = center.Distance(centroid) center = centroid return center
def FindFarthestGridPoint(shape, loc, winRad, maxGridVal): """ find the grid point with max occupancy that is furthest from a given location """ shapeGrid = shape.grid shapeVect = shapeGrid.GetOccupancyVect() nGridPts = len(shapeVect) dMax = -1 for i in range(nGridPts): if shapeVect[i] < maxGridVal: continue posI = shapeGrid.GetGridPointLoc(i) dst = posI.Distance(loc) if dst > dMax: dMax = dst res = posI count, centroid = Geometry.ComputeGridCentroid(shapeGrid, res, winRad) res = centroid return res
def AppendSkeletonPoints(shapeGrid, termPts, winRad, stepDist, maxGridVal=3, maxDistC=15.0, distTol=1.5, symFactor=1.5, verbose=False): nTermPts = len(termPts) skelPts = [] shapeVect = shapeGrid.GetOccupancyVect() nGridPts = len(shapeVect) # find all possible skeleton points if verbose: print('generate all possible') for i in range(nGridPts): if shapeVect[i] < maxGridVal: continue posI = shapeGrid.GetGridPointLoc(i) ok = True for pt in termPts: dst = posI.Distance(pt.location) if dst < stepDist: ok = False break if ok: skelPts.append(SubshapeObjects.SkeletonPoint(location=posI)) # now start removing them if verbose: print('Compute centroids:', len(skelPts)) gridBoxVolume = shapeGrid.GetSpacing()**3 maxVol = 4.0 * math.pi / 3.0 * winRad**3 * maxGridVal / gridBoxVolume i = 0 while i < len(skelPts): pt = skelPts[i] count, centroid = Geometry.ComputeGridCentroid(shapeGrid, pt.location, winRad) #count,centroid=ComputeShapeGridCentroid(pt.location,shapeGrid,winRad) centroidPtDist = centroid.Distance(pt.location) if centroidPtDist > maxDistC: del skelPts[i] else: pt.fracVol = float(count) / maxVol pt.location.x = centroid.x pt.location.y = centroid.y pt.location.z = centroid.z i += 1 if verbose: print('remove points:', len(skelPts)) res = termPts + skelPts i = 0 while i < len(res): p = -1 mFrac = 0.0 ptI = res[i] startJ = max(i + 1, nTermPts) for j in range(startJ, len(res)): ptJ = res[j] distC = ptI.location.Distance(ptJ.location) if distC < symFactor * stepDist: if ptJ.fracVol > mFrac: p = j mFrac = ptJ.fracVol #print i,len(res),p,mFrac if p > -1: ptP = res.pop(p) j = startJ while j < len(res): ptJ = res[j] distC = ptI.location.Distance(ptJ.location) if distC < symFactor * stepDist: del res[j] else: j += 1 res.append(ptP) #print '% 3d'%i,'% 5.2f % 5.2f % 5.2f'%tuple(list(ptI.location)),' - ','% 5.2f % 5.2f % 5.2f'%tuple(list(ptJ.location)) i += 1 return res