def _update_selection(self): """ Sets the selection datasource's 'selection' metadata element to a mask of all the points selected """ if self.selection_datasource is None: return selected_mask = zeros(self.selection_datasource._data.shape, dtype=numpy.bool) data = self._get_data() # Compose the selection mask from the cached selections first, then # the active selection, taking into account the selection mode only # for the active selection for selection in self._previous_selections: selected_mask |= (points_in_polygon(data, selection, False)) if self.selection_mode == 'exclude': selected_mask |= (points_in_polygon(data, self._active_selection, False)) selected_mask = 1 - selected_mask elif self.selection_mode == 'invert': selected_mask = -1 * (selected_mask - points_in_polygon( data, self._active_selection, False)) else: selected_mask |= (points_in_polygon(data, self._active_selection, False)) if sometrue(selected_mask != self.selection_datasource.metadata['selection']): self.selection_datasource.metadata['selection'] = selected_mask self.selection_changed = True return
def _update_selection(self): """ Sets the selection datasource's 'selection' metadata element to a mask of all the points selected """ if self.selection_datasource is None: return selected_mask = zeros(self.selection_datasource._data.shape, dtype=numpy.int32) data = self._get_data() # Compose the selection mask from the cached selections first, then # the active selection, taking into account the selection mode only # for the active selection for selection in self._previous_selections: selected_mask |= (points_in_polygon(data, selection, False)) if self.selection_mode == 'exclude': selected_mask |= (points_in_polygon(data, self._active_selection, False)) selected_mask = 1 - selected_mask elif self.selection_mode == 'invert': selected_mask = -1 * (selected_mask -points_in_polygon(data, self._active_selection, False)) else: selected_mask |= (points_in_polygon(data, self._active_selection, False)) if sometrue(selected_mask != self.selection_datasource.metadata['selection']): self.selection_datasource.metadata['selection'] = selected_mask self.selection_changed = True return
def test_center_removed(self): # Tests a polygon which resembles the following: # # 9------8 # | | # | 3---+---------2 # | | | | # | 4---+----5 | # | | | | # | 7----6 | # | | # 0----------------1 # # Using the winding rule, the inner square containing the edge (3,4) # is inside the polygon, while using the odd-even rule, it is outside. # The inner square with containing the edge (5,6) is outside in both # cases. vertices = array(((0, 0), (10, 0), (10, 8), (2, 8), (2, 6), (8, 6), (8, 2), (5, 2), (5, 10), (0, 10))) trial = array(((3, 7), )) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Interior polygon inside: odd-even") self.assertEqual(1, w_result[0], "Interior polygon outside: winding") trial = array(((6, 5), )) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Interior polygon inside: odd-even") self.assertEqual(0, w_result[0], "Interior polygon inside: winding")
def _update_selection(self): """ Sets the selection datasource's metadata to a mask of all the points selected """ if self.selection_datasource is None: return selected_mask = zeros(self.selection_datasource._data.shape, dtype=numpy.bool) data = self._get_data() # Compose the selection mask from the cached selections first, then # the active selection, taking into account the selection mode only # for the active selection for selection in self._previous_selections: selected_mask |= points_in_polygon( data, selection, False).astype(bool, copy=False) active_selection = points_in_polygon( data, self._active_selection, False).astype(bool, copy=False) if self.selection_mode == 'exclude': # XXX I think this should be "set difference"? - CJW selected_mask |= active_selection selected_mask = ~selected_mask elif self.selection_mode == 'invert': selected_mask ^= active_selection else: selected_mask |= active_selection if sometrue(selected_mask != self.selection_datasource.metadata[self.metadata_name]): self.selection_datasource.metadata[self.metadata_name] = selected_mask self.selection_changed = True
def test_simple_points_in_polygon(self): polygon = array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0))) points = array(((-1.0, -1.0), (5.0, 5.0), (15.0, 15.0))) result = agg.points_in_polygon(points, polygon) self.assertTrue(allclose(array([0,1,0]), result)) return
def test_asymmetric_points_in_polygon(self): polygon = array(((0.0, 0.0), (20.0, 0.0), (20.0, 10.0), (0.0, 10.0))) points = array(((5.0, 5.0), (10.0, 5.0), (15.0, 5.0))) result = agg.points_in_polygon(points, polygon) self.assertTrue(allclose(array([1,1,1]), result)) return
def test_simple_points_in_polygon(self): polygon = array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0))) points = array(((-1.0, -1.0), (5.0, 5.0), (15.0, 15.0))) result = agg.points_in_polygon(points, polygon) self.assertTrue(allclose(array([0, 1, 0]), result)) return
def test_asymmetric_points_in_polygon(self): polygon = array(((0.0, 0.0), (20.0, 0.0), (20.0, 10.0), (0.0, 10.0))) points = array(((5.0, 5.0), (10.0, 5.0), (15.0, 5.0))) result = agg.points_in_polygon(points, polygon) self.assertTrue(allclose(array([1, 1, 1]), result)) return
def test_center_removed(self): # Tests a polygon which resembles the following: # # 9------8 # | | # | 3---+---------2 # | | | | # | 4---+----5 | # | | | | # | 7----6 | # | | # 0----------------1 # # Using the winding rule, the inner square containing the edge (3,4) # is inside the polygon, while using the odd-even rule, it is outside. # The inner square with containing the edge (5,6) is outside in both # cases. vertices = array(((0,0), (10, 0), (10, 8), (2, 8), (2, 6), (8, 6), (8, 2), (5, 2), (5, 10), (0, 10))) trial = array(((3,7),)) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Interior polygon inside: odd-even") self.assertEqual(1, w_result[0], "Interior polygon outside: winding") trial = array(((6,5),)) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Interior polygon inside: odd-even") self.assertEqual(0, w_result[0], "Interior polygon inside: winding")
def test_rectangle(self): vertices = array(((0, 0), (0, 10), (10, 10), (10, 0))) # Try the lower left. trial = array(((0, 0), )) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Lower left corner not in polygon. OEF") self.assertEqual(1, w_result[0], "Lower left corner not in polygon. Winding") # Try the center. trial = array(((5, 5), )) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(1, oe_result[0], "Center not in polygon. OEF") self.assertEqual(1, w_result[0], "Center not in polygon. Winding") # Try the center. trial = array(((10, 10), )) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(1, oe_result[0], "Top-right in polygon. OEF") self.assertEqual(0, w_result[0], "Top-right in polygon. Winding") return
def test_rectangle(self): vertices = array(((0,0), (0,10), (10,10), (10,0))) # Try the lower left. trial = array(((0,0),)) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(0, oe_result[0], "Lower left corner not in polygon. OEF") self.assertEqual(1, w_result[0], "Lower left corner not in polygon. Winding") # Try the center. trial = array(((5,5),)) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(1, oe_result[0], "Center not in polygon. OEF") self.assertEqual(1, w_result[0], "Center not in polygon. Winding") # Try the center. trial = array(((10,10),)) oe_result = agg.points_in_polygon(trial, vertices) w_result = agg.points_in_polygon(trial, vertices, True) self.assertEqual(1, oe_result[0], "Top-right in polygon. OEF") self.assertEqual(0, w_result[0], "Top-right in polygon. Winding") return
def _update_selection(self): """ Sets the selection datasource's metadata to a mask of all the points selected """ if self.selection_datasource is None: return selected_mask = zeros(self.selection_datasource._data.shape, dtype=numpy.bool) data = self._get_data() # Compose the selection mask from the cached selections first, then # the active selection, taking into account the selection mode only # for the active selection for selection in self._previous_selections: selected_mask |= points_in_polygon(data, selection, False).astype(bool, copy=False) active_selection = points_in_polygon(data, self._active_selection, False).astype(bool, copy=False) if self.selection_mode == 'exclude': # XXX I think this should be "set difference"? - CJW selected_mask |= active_selection selected_mask = ~selected_mask elif self.selection_mode == 'invert': selected_mask ^= active_selection else: selected_mask |= active_selection if sometrue(selected_mask != self.selection_datasource.metadata[ self.metadata_name]): self.selection_datasource.metadata[ self.metadata_name] = selected_mask self.selection_changed = True
def _is_in(self, point): """ Test if the point (an x, y tuple) is within this polygonal region. To perform the test, we use the winding number inclusion algorithm, referenced in the comp.graphics.algorithms FAQ (http://www.faqs.org/faqs/graphics/algorithms-faq/) and described in detail here: http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm """ point_array = array((point,)) vertices = array(self.model.points) winding = self.inside_rule == "winding" result = points_in_polygon(point_array, vertices, winding) return result[0]
def _is_in(self, point): """ Test if the point (an x, y tuple) is within this polygonal region. To perform the test, we use the winding number inclusion algorithm, referenced in the comp.graphics.algorithms FAQ (http://www.faqs.org/faqs/graphics/algorithms-faq/) and described in detail here: http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm """ point_array = array((point, )) vertices = array(self.model.points) winding = self.inside_rule == 'winding' result = points_in_polygon(point_array, vertices, winding) return result[0]
def _eval ( self, model ): """ Evaluates the result of the filter for the specified model. """ if len( self.points ) == 0: return None points = array( zip( getattr( model, self.x_value ), getattr( model, self.y_value ) ) ) result = None for items in self.points: pip = points_in_polygon( points, array( items ) ) if result is None: result = pip else: result = result | pip return result
def hittest(self, screen_pt, threshold=7.0, return_distance=False): """ Performs point-in-polygon testing or point/line proximity testing. If self.hittest_type is "line" or "point", then behaves like the parent class BaseXYPlot.hittest(). If self.hittest_type is "poly", then returns True if the given point is inside the polygon, and False otherwise. """ if self.hittest_type in ("line", "point"): return BaseXYPlot.hittest(self, screen_pt, threshold, return_distance) 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
import numpy from kiva import agg poly = numpy.array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0))) print(agg.point_in_polygon(-1, -1, poly)) print(agg.point_in_polygon(0.0, 0.0, poly)) print(agg.point_in_polygon(5, 5, poly)) print(agg.point_in_polygon(10, 10, poly)) print(agg.point_in_polygon(15, 15, poly)) pts = numpy.array(((-1.0, -1.0), (0.1, 0.0), (0.0, 0.1), (0.0, 0.0), (5.0, 5.0), (10.0, 10.0), (15.0, 15.0))) results = agg.points_in_polygon(pts, poly) print(results) pts = numpy.random.random_sample((20000, 2)) * 12.5 - 2.5 import time t1 = time.clock() results = agg.points_in_polygon(pts, poly) t2 = time.clock() print('points_in_polygon() for %d pts in %d point polygon (sec): %f' % \ (len(pts), len(poly), t2-t1)) print(pts[:5]) print(results[:5])
print(agg.point_in_polygon(-1,-1,poly)) print(agg.point_in_polygon(0.0,0.0,poly)) print(agg.point_in_polygon(5,5,poly)) print(agg.point_in_polygon(10,10,poly)) print(agg.point_in_polygon(15,15,poly)) pts = numpy.array(((-1.0, -1.0), ( 0.1, 0.0), ( 0.0, 0.1), ( 0.0, 0.0), ( 5.0, 5.0), ( 10.0, 10.0), ( 15.0, 15.0))) results = agg.points_in_polygon(pts, poly) print(results) pts = numpy.random.random_sample((20000, 2))*12.5-2.5 import time t1 = time.clock() results = agg.points_in_polygon(pts, poly) t2 = time.clock() print('points_in_polygon() for %d pts in %d point polygon (sec): %f' % \ (len(pts), len(poly), t2-t1)) print(pts[:5]) print(results[:5]) poly = numpy.array((( 0.0, 0.0),