How to use the generic.np.allclose function in generic

To help you get started, we’ve selected a few generic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikedh / trimesh / tests / test_thickness.py View on Github external
mesh=m,
            points=samples,
            exterior=False,
            normals=m.face_normals[faces],
            method='ray')

        assert (thickness > -g.trimesh.tol.merge).all()

        # check thickness at a specific point
        point = g.np.array([[0.5, 0., 0.]])
        point_thickness = g.trimesh.proximity.thickness(
            mesh=m,
            points=point,
            exterior=False,
            method='ray')
        assert g.np.allclose(point_thickness, 1.0)
github mikedh / trimesh / tests / test_simplify.py View on Github external
# loading the polygon will make all arcs discrete
        path = g.trimesh.load_path(polygon)

        # save the md5 before doing operations
        md5_pre = g.deepcopy(path.md5())

        # this should return a copy of the path
        simplified = path.simplify()

        # make sure the simplify call didn't alter our original mesh
        assert path.md5() == md5_pre

        for garbage in range(2):
            # the simplified version shouldn't have lost area
            assert g.np.allclose(path.area,
                                 simplified.area,
                                 rtol=1e-3)

            # see if we fit as many arcs as existed in the original drawing
            new_count = sum(int(type(i).__name__ == 'Arc')
                            for i in simplified.entities)
            if new_count != arc_count:
                print(new_count, arc_count)

            if arc_count > 1:
                g.log.info('originally were {} arcs, simplify found {}'.format(
                    arc_count,
                    new_count))
                assert new_count > 0
                assert new_count <= arc_count
github mikedh / trimesh / tests / test_bounds.py View on Github external
def test_random_cylinder(self):
        """
        Check exact cylinders with the bounding cylinder function.
        """
        for i in range(20):
            # create a random cylinder
            c = g.trimesh.creation.cylinder(
                radius=1.0, height=10).permutate.transform()
            # bounding primitive should have same height and radius
            assert g.np.isclose(
                c.bounding_cylinder.primitive.height, 10, rtol=1e-6)
            assert g.np.isclose(
                c.bounding_cylinder.primitive.radius, 1, rtol=1e-6)
            # mesh is a cylinder, so center mass of bounding cylinder
            # should be exactly the same as the mesh center mass
            assert g.np.allclose(
                c.center_mass,
                c.bounding_cylinder.center_mass,
                rtol=1e-6)
github mikedh / trimesh / tests / test_inertia.py View on Github external
# the direction (long axis) of the cylinder should correspond to
        # the smallest principal component of inertia, AKA rotation along
        # the axis, rather than the other two which are perpendicular
        components, vectors = g.trimesh.inertia.principal_axis(
            c.moment_inertia)
        # inferred cylinder axis
        inferred = vectors[components.argmin()]

        # inferred cylinder axis should be close to actual cylinder axis
        axis_test = g.np.allclose(g.np.abs(inferred),
                                  g.np.abs(c.direction))
        assert axis_test

        # make sure Trimesh attribute is plumbed correctly
        assert g.np.allclose(c.principal_inertia_components, components)
        assert g.np.allclose(c.principal_inertia_vectors, vectors)

        # the other two axis of the cylinder should be identical
        assert g.np.abs(g.np.diff(g.np.sort(components)[-2:])).max() < 1e-8

        m = g.get_mesh('featuretype.STL')
        i0 = m.moment_inertia.copy()
        # rotate the moment of inertia
        i1 = g.trimesh.inertia.transform_inertia(
            transform=t0, inertia_tensor=i0)

        # rotate the mesh
        m.apply_transform(t0)
        # check to see if the rotated mesh + recomputed moment of inertia
        # is close to the rotated moment of inertia
        tf_test = g.np.abs((m.moment_inertia / i1) - 1)
        assert tf_test.max() < 1e-6
github mikedh / trimesh / tests / test_transformations.py View on Github external
[0, 0, 0, 1]),
                             [1, -1, 0, 1])

        angle = (g.np.random.random() - 0.5) * (2 * g.np.pi)
        direc = g.np.random.random(3) - 0.5
        point = g.np.random.random(3) - 0.5
        R0 = rotation_matrix(angle, direc, point)
        R1 = rotation_matrix(angle - 2 * g.np.pi, direc, point)
        assert g.trimesh.transformations.is_same_transform(R0, R1)

        R0 = rotation_matrix(angle, direc, point)
        R1 = rotation_matrix(-angle, -direc, point)
        assert g.trimesh.transformations.is_same_transform(R0, R1)

        I = g.np.identity(4, g.np.float64)  # NOQA
        assert g.np.allclose(I, rotation_matrix(g.np.pi * 2, direc))

        assert g.np.allclose(
            2,
            g.np.trace(rotation_matrix(g.np.pi / 2,
                                       direc, point)))

        # test symbolic
        angle = g.sp.Symbol('angle')
        Rs = rotation_matrix(angle, [0, 0, 1], [1, 0, 0])

        R = g.np.array(Rs.subs(
            angle,
            g.np.pi / 2.0).evalf()).astype(g.np.float64)

        assert g.np.allclose(g.np.dot(R,
                                      [0, 0, 0, 1]),
github mikedh / trimesh / tests / test_transformations.py View on Github external
assert not tr.is_rigid(g.np.ones((4, 4)))

        planar = tr.planar_matrix(offset=[10, -10], theta=0.0)
        assert g.np.allclose(planar[:2, 2], [10, -10])

        planar = tr.planar_matrix(offset=[0, -0], theta=g.np.pi)
        assert g.np.allclose(planar[:2, 2], [0, 0])

        planar = tr.planar_matrix(offset=[0, 0], theta=0.0)
        assert g.np.allclose(planar, g.np.eye(3))

        as_3D = tr.planar_matrix_to_3D(g.np.eye(3))
        assert g.np.allclose(as_3D, g.np.eye(4))

        spherical = tr.spherical_matrix(theta=0.0, phi=0.0)
        assert g.np.allclose(spherical, g.np.eye(4))

        points = g.np.arange(60, dtype=g.np.float64).reshape((-1, 3))
        assert g.np.allclose(tr.transform_points(points, g.np.eye(4)), points)

        points = g.np.arange(60, dtype=g.np.float64).reshape((-1, 2))
        assert g.np.allclose(tr.transform_points(points, g.np.eye(3)), points)
github mikedh / trimesh / tests / test_obj.py View on Github external
def test_obj_compressed(self):
        mesh = g.get_mesh('cube_compressed.obj', process=False)

        assert g.np.allclose(g.np.abs(mesh.vertex_normals).sum(axis=1),
                             1.0)
github mikedh / trimesh / tests / test_arc.py View on Github external
g.np.sin(angles[:, 1]),
                                       g.np.cos(angles[:, 2]),
                                       g.np.sin(angles[:, 2]))).reshape((-1, 6))
        points_2D *= radii.reshape((-1, 1))
        points_2D += g.np.tile(center_2D, (1, 3))
        points_2D = points_2D.reshape((-1, 3, 2))
        points_3D = g.np.column_stack((points_2D.reshape((-1, 2)),
                                       g.np.tile(center_3D[:, 2].reshape((-1, 1)),
                                                 (1, 3)).reshape(-1))).reshape((-1, 3, 3))

        for center, radius, three in zip(center_2D,
                                         radii,
                                         points_2D):
            info = arc_center(three)

            assert g.np.allclose(center, info['center'])
            assert g.np.allclose(radius, info['radius'])

        for center, radius, three in zip(center_3D,
                                         radii,
                                         points_3D):
            transform = g.trimesh.transformations.random_rotation_matrix()
            center = g.trimesh.transformations.transform_points([center], transform)[
                0]
            three = g.trimesh.transformations.transform_points(
                three, transform)

            info = arc_center(three)

            assert g.np.allclose(center, info['center'])
            assert g.np.allclose(radius, info['radius'])
github mikedh / trimesh / tests / test_bounds.py View on Github external
for i in range(100):
            # transform box randomly in rotation and translation
            mat = g.trimesh.transformations.random_rotation_matrix()
            # translate in box -100 : +100
            mat[:3, 3] = (g.np.random.random(3) - .5) * 200

            # source mesh to check
            b = g.trimesh.creation.box(extents=extents,
                                       transform=mat)

            # calculated OBB primitive
            obb = b.bounding_box_oriented

            # make sure extents returned were ordered
            assert g.np.allclose(obb.primitive.extents,
                                 extents_ordered)

            # make sure mesh isn't reversing windings
            assert g.np.isclose(obb.to_mesh().volume,
                                g.np.product(extents))

            # make sure OBB has the same bounds as the source mesh
            # since it is a box the AABB of the OBB should be
            # the same as the AABB of the source mesh (lol)
            assert g.np.allclose(obb.bounds,
                                 b.bounds)

            # unordered extents and transforms
            transform, extents = g.trimesh.bounds.oriented_bounds(b, ordered=False)
            assert g.np.allclose(g.np.sort(extents), extents_ordered)
            # create a box from the unordered OBB information
github mikedh / trimesh / tests / test_primitives.py View on Github external
assert as_mesh.is_winding_consistent
            assert as_mesh.is_watertight

            # check that overload of dir worked
            assert len([i
                        for i in dir(primitive.primitive) if '_' not in i]) > 0

            if hasattr(primitive, 'direction'):
                assert primitive.direction.shape == (3,)

            centroid = primitive.centroid.copy()
            translation = [0, 0, 5]
            primitive.apply_translation(translation)

            # centroid should have translated correctly
            assert g.np.allclose(primitive.centroid - centroid,
                                 translation)