Exemplo n.º 1
0
def test_point_in_tri():
    test_datasets = [
        {
            'triangle': np.array([[0., 0.], [1., 0.], [0., 1.]]),
            'points_inside': [np.array([0.1, 0.1]),
                              np.array([0.3, 0.3])],
            'points_outside': [np.array([5., 5.])],
        },
    ]

    for dataset in test_datasets:
        for point in dataset['points_inside']:
            assert point_in_tri(dataset['triangle'], point)
        for point in dataset['points_outside']:
            assert ~point_in_tri(dataset['triangle'], point)
Exemplo n.º 2
0
    def locate_faces(self,
                     points,
                     method='celltree',
                     _copy=False,
                     _memo=True,
                     _hash=None):
        """
        Returns the face indices, one per point.

        Points that are not in the mesh will have an index of -1

        If a single point is passed in, a single index will be returned
        If a sequence of points is passed in an array of indexes will be returned.

        :param points:  The points that you want to locate -- (lon, lat). If the shape of point
                        is 1D, function will return a scalar index. If it is 2D, it will return
                        a 1D array of indices
        :type point: array-like containing one or more points: shape (2,) for one point, shape (N, 2)
                     for more than one point.

        :param method='celltree': method to use. Options are 'celltree', 'simple'.
                                  for 'celltree' the celltree2d pacakge must be installed:
                                  https://github.com/NOAA-ORR-ERD/cell_tree2d/
                                  'simple' is very, very slow for large grids.
        :type simple: str

        This version utilizes the CellTree data structure.

        """
        points = np.asarray(points, dtype=np.float64)
        just_one = (points.ndim == 1)
        points.shape = (-1, 2)
        if not hasattr(self, '_ind_memo_dict'):
            self._ind_memo_dict = OrderedDict()

        if _memo:
            if _hash is None:
                _hash = self._hash_of_pts(points)
            result = self._get_memoed(points, self._ind_memo_dict, _copy,
                                      _hash)
            if result is not None:
                return result

        if method == 'celltree':
            try:
                import cell_tree2d
            except ImportError:
                raise ImportError(
                    "the cell_tree2d package must be installed to use the celltree search:\n"
                    "https://github.com/NOAA-ORR-ERD/cell_tree2d/")
            if self._tree is None:
                self.build_celltree()
            indices = self._tree.locate(points)
        elif method == 'simple':
            indices = np.zeros((points.shape[0]), dtype=IND_DT)
            for n, point in enumerate(points):
                for i, face in enumerate(self._faces):
                    f = self._nodes[face]
                    if point_in_tri(f, point):
                        indices[n] = i
                        break
                    else:
                        indices[n] = -1
        else:
            raise ValueError('"method" must be one of: "celltree", "simple"')

        if _memo:
            self._add_memo(points, indices, self._ind_memo_dict, _copy, _hash)

        if just_one:
            return indices[0]
        else:
            return indices