示例#1
0
    def test_validate_inversion(self):
        """Make sure inverted meshes are fixed by `validate=True`"""
        orig_mesh = g.get_mesh("unit_cube.STL")
        orig_verts = orig_mesh.vertices.copy()
        orig_faces = orig_mesh.faces.copy()

        orig_face_set = {tuple(row) for row in orig_faces}

        inv_faces = orig_faces[:, ::-1]
        inv_mesh = g.Trimesh(orig_verts, inv_faces, validate=False)
        assert {tuple(row) for row in inv_mesh.faces} != orig_face_set

        fixed_mesh = g.Trimesh(orig_verts, inv_faces, validate=True)
        assert {tuple(row) for row in fixed_mesh.faces} == orig_face_set
示例#2
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
示例#3
0
    def test_inversion(self):
        """Make sure fix_inversion switches all reversed faces back"""
        orig_mesh = g.get_mesh("unit_cube.STL")
        orig_verts = orig_mesh.vertices.copy()
        orig_faces = orig_mesh.faces.copy()

        mesh = g.Trimesh(orig_verts, orig_faces[:, ::-1])
        inv_faces = mesh.faces.copy()
        # check not fixed on the way in
        assert not g.np.allclose(inv_faces, orig_faces)

        g.trimesh.repair.fix_inversion(mesh)
        assert not g.np.allclose(mesh.faces, inv_faces)
        assert g.np.allclose(mesh.faces, orig_faces)
示例#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