Skip to content

Commit

Permalink
Share paths between multiple shapes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed Feb 25, 2012
1 parent 0433616 commit 5c5f2f0
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions depixel/depixeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,22 +534,35 @@ def isolate_outlines(self):
for node in nx.isolates(self.outlines_graph):
self.outlines_graph.remove_node(node)

def make_path(self, graph):
path = Path(graph)
key = path.key()
if key not in self.paths:
self.paths[key] = path
path.make_spline()
return self.paths[key]

def add_shape_outlines(self):
self.paths = {}

for shape in self.shapes:
sg = self.outlines_graph.subgraph(shape.corners)
for graph in nx.connected_component_subgraphs(sg):
path = self.make_path(graph)
if (min(graph.nodes()) == min(sg.nodes())):
shape.add_outline(graph, True)
shape.add_outline(path, True)
else:
shape.add_outline(graph)
shape.add_outline(path)

def smooth_splines(self):
for shape in self.shapes:
shape.smooth_splines = [
self.smooth_spline(s.copy()) for s in shape.splines]

def smooth_spline(self, spline):
return bspline.smooth_spline(spline)
print "Smoothing splines..."
for i, path in enumerate(self.paths.values()):
print " * %s/%s (%s, %s)..." % (
i + 1, len(self.paths), len(path.shapes), len(path.path))
if len(path.shapes) == 1:
path.smooth = path.spline.copy()
continue
path.smooth_spline()


class Shape(object):
Expand All @@ -576,18 +589,27 @@ def splines(self):
paths.extend(path.spline for path in self._inside_paths)
return paths

def add_outline(self, graph, outside=False):
path = Path(graph)
@property
def smooth_splines(self):
paths = [self._outside_path.smooth.reversed()]
paths.extend(path.smooth for path in self._inside_paths)
return paths

def add_outline(self, path, outside=False):
if outside:
self._outside_path = path
else:
self._inside_paths.append(path)
path.shapes.add(self)


class Path(object):
def __init__(self, shape_graph):
self.path = self._make_path(shape_graph)
self.spline = self.make_spline(self.path)
self.shapes = set()

def key(self):
return tuple(self.path)

def _make_path(self, shape_graph):
# Find initial nodes.
Expand All @@ -607,5 +629,8 @@ def _make_path(self, shape_graph):
break
return path

def make_spline(self, path):
return bspline.polyline_to_closed_bspline(path)
def make_spline(self):
self.spline = bspline.polyline_to_closed_bspline(self.path)

def smooth_spline(self):
self.smooth = bspline.smooth_spline(self.spline)

0 comments on commit 5c5f2f0

Please sign in to comment.