コード例 #1
0
    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 = points_in_polygon(trial, vertices)
        w_result = 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 = points_in_polygon(trial, vertices)
        w_result = 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")
コード例 #2
0
ファイル: lasso_selection.py プロジェクト: enthought/chaco
    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
コード例 #3
0
    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
コード例 #4
0
    def test_empty_points_in_polygon(self):
        polygon = array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)))
        points = zeros((0, 2))

        result = points_in_polygon(points, polygon)
        self.assertTrue(len(result) == len(points))

        polygon = array([])
        points = array(((-1.0, -1.0), (5.0, 5.0), (15.0, 15.0)))

        result = points_in_polygon(points, polygon)
        self.assertTrue(allclose(array([0, 0, 0]), result))
コード例 #5
0
    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 = points_in_polygon(points, polygon)
        self.assertTrue(allclose(array([1, 1, 1]), result))

        return
コード例 #6
0
    def test_transposed_points_in_polygon(self):

        polygon = array(((0.0, 10.0, 10.0, 0.0), (0.0, 0.0, 10.0, 10.0)))
        points = array(((-1.0, 5.0, 15.0), (-1.0, 5.0, 15.0)))

        result = points_in_polygon(points, polygon)
        self.assertTrue(allclose(array([0, 1, 0]), result))

        return
コード例 #7
0
    def test_rectangle(self):
        vertices = array(((0, 0), (0, 10), (10, 10), (10, 0)))

        # Try the lower left.
        trial = array(((0, 0),))
        oe_result = points_in_polygon(trial, vertices)
        w_result = 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 = points_in_polygon(trial, vertices)
        w_result = 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 = points_in_polygon(trial, vertices)
        w_result = 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")
コード例 #8
0
    def test_discontiguous_inputs(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), (7.0, 7.0)))

        # Create a discontiguous polygon array
        poly3 = zeros((polygon.shape[0], 3))
        poly3[:, :2] = polygon
        polygon = poly3[:, :2]

        # Create a discontiguous points array.
        points = points[1::2]

        result = points_in_polygon(points, polygon)
        self.assertTrue(allclose(array([1, 1]), result))
コード例 #9
0
ファイル: polygon.py プロジェクト: pingleewu/ShareRoot
    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]
コード例 #10
0
ファイル: polygon.py プロジェクト: enthought/enable
    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]
コード例 #11
0
ファイル: polygon_plot.py プロジェクト: enthought/chaco
    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.column_stack((index, value))
        if points_in_polygon([data_pt], poly)[0] == 1:
            return True
        else:
            return False
コード例 #12
0
ファイル: polygon_plot.py プロジェクト: skailasa/chaco
    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.column_stack((index, value))
        if points_in_polygon([data_pt], poly)[0] == 1:
            return True
        else:
            return False
コード例 #13
0
    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 = points_in_polygon(points, polygon)
        self.assertTrue(allclose(array([0, 1, 0]), result))
コード例 #14
0
ファイル: polygon_hit_test.py プロジェクト: alexlib/enable
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from time import perf_counter

import numpy

from kiva.api import points_in_polygon

poly = numpy.array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)))

print(points_in_polygon((-1, -1), poly))
print(points_in_polygon((0.0, 0.0), poly))
print(points_in_polygon((5, 5), poly))
print(points_in_polygon((10, 10), poly))
print(points_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),
))
コード例 #15
0
import numpy

from kiva.api import points_in_polygon

poly = numpy.array(((0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 10.0)))

print(point_in_polygon(-1, -1, poly))
print(point_in_polygon(0.0, 0.0, poly))
print(point_in_polygon(5, 5, poly))
print(point_in_polygon(10, 10, poly))
print(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 = points_in_polygon(pts, poly)

print(results)

pts = numpy.random.random_sample((20000, 2)) * 12.5 - 2.5

import time
t1 = time.clock()
results = 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(
コード例 #16
0
ファイル: polygon_hit_test.py プロジェクト: enthought/enable
print(point_in_polygon(-1,-1,poly))
print(point_in_polygon(0.0,0.0,poly))
print(point_in_polygon(5,5,poly))
print(point_in_polygon(10,10,poly))
print(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 = points_in_polygon(pts, poly)

print(results)

pts = numpy.random.random_sample((20000, 2))*12.5-2.5

import time
t1 = time.clock()
results = 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),