Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a 'list of polygons' capability #207

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions chaco/polygon_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class PolygonPlot(BaseXYPlot):

# Override the hittest_type trait inherited from BaseXYPlot
hittest_type = Enum("poly", "point", "line")

# The RGBA tuple for rendering edges. It is always a tuple of length 4.
# It has the same RGB values as edge_color_, and its alpha value is the
# alpha value of self.edge_color multiplied by self.alpha.
# alpha value of self.edge_color multiplied by self.alpha.
effective_edge_color = Property(Tuple, depends_on=['edge_color', 'alpha'])

# The RGBA tuple for rendering the face. It is always a tuple of length 4.
# It has the same RGB values as face_color_, and its alpha value is the
# alpha value of self.face_color multiplied by self.alpha.
# alpha value of self.face_color multiplied by self.alpha.
effective_face_color = Property(Tuple, depends_on=['face_color', 'alpha'])

#----------------------------------------------------------------------
Expand Down Expand Up @@ -100,9 +100,19 @@ def _render(self, gc, points):
gc.set_line_dash(self.edge_style_)
gc.set_fill_color(self.effective_face_color)

gc.lines(points)
gc.close_path()
gc.draw_path()
# bnds is True where polygons are separated + ensures last is True
# indx contains the list of indices that separates the polygons
# lines contains a list of lines that represents all polygons
bnds = [np.isnan(point[0]) for point in points]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely to be slow: see https://github.com/enthought/chaco/blob/master/chaco/lineplot.py#L266 for comparison to get an idea of how the line plot finds the runs of non-nan values.

Also this code, and the equivalent code below in hittest should probably go into the gather_points function so that it can be cached and we can avoid re-computation of the polygons on every draw.

if not bnds[-1]:
bnds.append(True)
indx = [-1] + [n for n, b in enumerate(bnds) if b]
lines = [points[i+1:j] for i, j in zip(indx[:-1],indx[1:])]

for line in lines:
gc.lines(line)
gc.close_path()
gc.draw_path()


def _render_icon(self, gc, x, y, width, height):
Expand Down Expand Up @@ -134,11 +144,23 @@ def hittest(self, screen_pt, threshold=7.0, return_distance=False):
data_pt = self.map_data(screen_pt, all_values=True)
index = self.index.get_data()
value = self.value.get_data()
poly = np.vstack((index,value)).T
if points_in_polygon([data_pt], poly)[0] == 1:
return True
else:
return False
points = zip(index,value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using zip with numpy arrays (there are, unfortunately too many places where this already happens in the Chaco code base), and instead use things like np.vstack or np.column_stack which are vectorized and return numpy arrays.


# bnds is True where polygons are separated + ensures last is True
# indx contains the list of indices that separates the polygons
# lines contains a list of lines that represents all polygons
bnds = [np.isnan(point[0]) for point in points]
if not bnds[-1]:
bnds.append(True)
indx = [-1] + [n for n, b in enumerate(bnds) if b]
lines = [points[i+1:j] for i, j in zip(indx[:-1],indx[1:])]

for line in lines:
poly = np.vstack(line).T
if points_in_polygon([data_pt], poly)[0] == 1:
return True

return False

#------------------------------------------------------------------------
# Event handlers
Expand Down