Ejemplo n.º 1
0
    def test_path_extrude(self):
        try:
            import meshpy
        except ImportError:
            g.log.error("no meshpy: skipping test")
            return

        # Create base polygon
        vec = g.np.array([0, 1]) * 0.2
        n_comps = 100
        angle = g.np.pi * 2.0 / n_comps
        rotmat = g.np.array([
            [g.np.cos(angle), -g.np.sin(angle)],
            [g.np.sin(angle), g.np.cos(angle)]])
        perim = []
        for i in range(n_comps):
            perim.append(vec)
            vec = g.np.dot(rotmat, vec)
        poly = g.Polygon(perim)

        # Create 3D path
        angles = g.np.linspace(0, 8 * g.np.pi, 1000)
        x = angles / 10.0
        y = g.np.cos(angles)
        z = g.np.sin(angles)
        path = g.np.c_[x, y, z]

        # Extrude
        mesh = g.trimesh.creation.sweep_polygon(poly, path)
        self.assertTrue(mesh.is_volume)
Ejemplo n.º 2
0
    def test_path_sweep(self):
        if len(self.engines) == 0:
            return

        # Create base polygon
        vec = g.np.array([0, 1]) * 0.2
        n_comps = 100
        angle = g.np.pi * 2.0 / n_comps
        rotmat = g.np.array([[g.np.cos(angle), -g.np.sin(angle)],
                             [g.np.sin(angle),
                              g.np.cos(angle)]])
        perim = []
        for i in range(n_comps):
            perim.append(vec)
            vec = g.np.dot(rotmat, vec)
        poly = g.Polygon(perim)

        # Create 3D path
        angles = g.np.linspace(0, 8 * g.np.pi, 1000)
        x = angles / 10.0
        y = g.np.cos(angles)
        z = g.np.sin(angles)
        path = g.np.c_[x, y, z]

        # Extrude
        mesh = g.trimesh.creation.sweep_polygon(poly, path)
        assert mesh.is_volume
Ejemplo n.º 3
0
    def check_nearest_point_function(self, fun):
        def plot_tri(tri, color='g'):
            plottable = g.np.vstack((tri, tri[0]))
            plt.plot(plottable[:, 0], plottable[:, 1], color=color)

        def points_on_circle(count):
            theta = g.np.linspace(0, g.np.pi * 2, count + 1)[:count]
            s = g.np.column_stack((theta, [g.np.pi / 2] * count))
            t = g.trimesh.util.spherical_to_vector(s)
            return t

        # inscribed triangle on a circle on the XY plane
        triangle = points_on_circle(3)

        # a circle of points surrounding the triangle
        query = points_on_circle(63) * 2
        # set the points up in space
        query[:, 2] = 10
        # a circle of points inside the triangle
        query = g.np.vstack((query, query * .1))

        mesh = g.Trimesh(**g.trimesh.triangles.to_kwargs([triangle]))

        result, result_distance, result_tid = fun(mesh, query)

        polygon = g.Polygon(triangle[:, 0:2])
        polygon_buffer = polygon.buffer(1e-5)

        # all of the points returned should be on the triangle we're querying
        broken = g.np.array([not polygon_buffer.intersects(
            g.Point(i)) for i in result[:, 0:2]])

        # see what distance shapely thinks the nearest point is for the 2D triangle
        # and the query points
        distance_shapely = g.np.array(
            [polygon.distance(g.Point(i)) for i in query[:, 0:2]])

        # see what distance our function returned for the nearest point
        distance_ours = ((query[:, 0:2] - result[:, 0:2])
                         ** 2).sum(axis=1) ** .5

        # how far was our distance from the one shapely gave
        distance_test = g.np.abs(distance_shapely - distance_ours)

        '''
        # plot test to debug failures 
        import matplotlib.pyplot as plt
        plottable = g.np.column_stack((result[:,0:2], query[:,0:2])).reshape((-1,2,2))
        plot_tri(triangle, color='g')
        for i in plottable:
            plt.plot(*i.T, color='b')
        plt.scatter(*result[:,0:2].T, color='k')
        plt.scatter(*result[broken][:,0:2].T, color='r')
        plt.show()
        '''

        self.assertFalse(broken.any())
        self.assertTrue(distance_test.max() < g.trimesh.constants.tol.merge)

        return result, result_distance
Ejemplo n.º 4
0
    def check_nearest_point_function(self, fun):
        # def plot_tri(tri, color='g'):
        #     plottable = g.np.vstack((tri, tri[0]))
        #     plt.plot(plottable[:, 0], plottable[:, 1], color=color)

        def points_on_circle(count):
            theta = g.np.linspace(0, g.np.pi * 2, count + 1)[:count]
            s = g.np.column_stack((theta, [g.np.pi / 2] * count))
            t = g.trimesh.util.spherical_to_vector(s)
            return t

        # generate some pseudorandom triangles
        # use our random to avoid spurious failures
        triangles = g.random((100, 3, 3)) - 0.5
        # put them on- plane
        triangles[:, :, 2] = 0.0

        # make one of the triangles equilaterial
        triangles[-1] = points_on_circle(3)

        # a circle of points surrounding the triangle
        query = points_on_circle(63) * 2
        # set the points up in space
        query[:, 2] = 10
        # a circle of points inside-ish the triangle
        query = g.np.vstack((query, query * .1))

        # loop through each triangle
        for triangle in triangles:
            # create a mesh with one triangle
            mesh = g.Trimesh(**g.trimesh.triangles.to_kwargs([triangle]))

            result, result_distance, result_tid = fun(mesh, query)

            polygon = g.Polygon(triangle[:, 0:2])
            polygon_buffer = polygon.buffer(1e-5)

            # all of the points returned should be on the triangle we're
            # querying
            assert all(polygon_buffer.intersects(
                g.Point(i)) for i in result[:, 0:2])

            # see what distance shapely thinks the nearest point
            # is for the 2D triangle and the query points
            distance_shapely = g.np.array([polygon.distance(g.Point(i))
                                           for i in query[:, :2]])

            # see what distance our function returned for the nearest point
            distance_ours = ((query[:, :2] - result[:, :2])
                             ** 2).sum(axis=1) ** .5

            # how far was our distance from the one shapely gave
            distance_test = g.np.abs(distance_shapely - distance_ours)  # NOQA

            # we should have calculated the same distance as shapely
            assert g.np.allclose(distance_ours, distance_shapely)

        # now check to make sure closest point doesn't depend on
        # the frame, IE the results should be the same after
        # any rigid transform
        # chop query off to same length as triangles
        assert len(query) > len(triangles)
        query = query[:len(triangles)]
        # run the closest point query as a corresponding query
        close = g.trimesh.triangles.closest_point(triangles=triangles,
                                                  points=query)
        # distance between closest point and query point
        # this should stay the same regardless of frame
        distance = g.np.linalg.norm(close - query, axis=1)
        for T in g.transforms:
            # transform the query points
            points = g.trimesh.transform_points(query, T)
            # transform the triangles we're checking
            tri = g.trimesh.transform_points(
                triangles.reshape((-1, 3)), T).reshape((-1, 3, 3))
            # run the closest point check
            check = g.trimesh.triangles.closest_point(triangles=tri,
                                                      points=points)
            check_distance = g.np.linalg.norm(check - points, axis=1)
            # should be the same in any frame
            assert g.np.allclose(check_distance, distance)

        return result, result_distance