How to use the generic.np.random.random 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_points.py View on Github external
cloud = g.trimesh.points.PointCloud(points)

        initial_md5 = cloud.md5()

        assert cloud.convex_hull.volume > 0.0

        # check shapes of data
        assert cloud.vertices.shape == shape
        assert cloud.shape == shape
        assert cloud.extents.shape == (3,)
        assert cloud.bounds.shape == (2, 3)

        assert cloud.md5() == initial_md5

        # set some random colors
        cloud.colors = g.np.random.random((shape[0], 4))
        # remove the duplicates we created
        cloud.merge_vertices()

        # new shape post- merge
        new_shape = (shape[0] - 9, shape[1])

        # make sure vertices and colors are new shape
        assert cloud.vertices.shape == new_shape
        assert len(cloud.colors) == new_shape[0]
        assert cloud.md5() != initial_md5

        # AABB volume should be same as points
        assert g.np.isclose(cloud.bounding_box.volume,
                            g.np.product(points.ptp(axis=0)))

        # will populate all bounding primitives
github mikedh / trimesh / tests / test_color.py View on Github external
assert m.visual.kind == 'vertex'

        test = (g.np.random.random(4) * 255).astype(g.np.uint8)
        m.visual.face_colors = test
        assert bool((m.visual.vertex_colors == test).all())
        assert m.visual.kind == 'face'
        m.visual.vertex_colors[0] = (
            g.np.random.random(4) * 255).astype(g.np.uint8)
        assert m.visual.kind == 'vertex'

        test = (g.np.random.random(4) * 255).astype(g.np.uint8)
        m.visual.vertex_colors = test
        assert bool((m.visual.face_colors == test).all())
        assert m.visual.kind == 'vertex'
        m.visual.face_colors[0] = (
            g.np.random.random(4) * 255).astype(g.np.uint8)
        assert m.visual.kind == 'face'
github mikedh / trimesh / tests / test_points.py View on Github external
def test_vertex_only(self):
        """
        Test to make sure we can instantiate a mesh with just
        vertices and no faces for some unknowable reason
        """

        v = g.np.random.random((1000, 3))
        v[g.np.floor(g.np.random.random(90) * len(v)).astype(int)] = v[0]

        mesh = g.trimesh.Trimesh(v)

        assert len(mesh.vertices) < 950
        assert len(mesh.vertices) > 900
github mikedh / trimesh / tests / test_proximity.py View on Github external
def test_helper(self):
        # just make sure the plumbing returns something
        for mesh in g.get_meshes(2):
            points = (g.np.random.random((100, 3)) - .5) * 100

            a = mesh.nearest.on_surface(points)
            assert a is not None

            b = mesh.nearest.vertex(points)
            assert b is not None
github mikedh / trimesh / tests / test_arc.py View on Github external
def test_center_random(self):
        from trimesh.path.arc import arc_center
        # Test that arc centers work on well formed random points in 2D and 3D
        min_angle = g.np.radians(2)
        count = 1000

        center_3D = (g.np.random.random((count, 3)) - .5) * 50
        center_2D = center_3D[:, 0:2]
        radii = g.np.clip(g.np.random.random(count) * 100, min_angle, g.np.inf)

        angles = g.np.random.random((count, 2)) * \
            (g.np.pi - min_angle) + min_angle
        angles = g.np.column_stack((g.np.zeros(count),
                                    g.np.cumsum(angles, axis=1)))

        points_2D = g.np.column_stack((g.np.cos(angles[:, 0]),
                                       g.np.sin(angles[:, 0]),
                                       g.np.cos(angles[:, 1]),
                                       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)),
github mikedh / trimesh / tests / test_mutate.py View on Github external
# ray.intersects_id
        centre = mesh.vertices.mean(axis=0)
        origins = g.np.random.random((100, 3)) * 1000
        directions = g.np.copy(origins)
        directions[:50, :] -= centre
        directions[50:, :] += centre
        mesh.ray.intersects_id(origins, directions)

        # nearest.vertex
        points = g.np.random.random((500, 3)) * 100
        mesh.nearest.vertex(points)

        # section
        section_count = 20
        origins = g.np.random.random((section_count, 3)) * 100
        normals = g.np.random.random((section_count, 3)) * 100
        heights = g.np.random.random((10,)) * 100
        for o, n in zip(origins, normals):
            # try slicing at random origin and at center mass
            mesh.slice_plane(o, n)
            mesh.slice_plane(mesh.center_mass, n)

            # section at random origin and center mass
            mesh.section(o, n)
            mesh.section(mesh.center_mass, n)

            # same with multiplane
            mesh.section_multiplane(o, n, heights)
            mesh.section_multiplane(mesh.center_mass, n, heights)

        assert not mesh.vertices.flags['WRITEABLE']
        assert not mesh.faces.flags['WRITEABLE']
github mikedh / trimesh / tests / test_registration.py View on Github external
def test_procrustes(self):
        # create random points in space
        points_a = (g.np.random.random((1000, 3)) - .5) * 1000
        # create a random transform
        matrix = g.trimesh.transformations.random_rotation_matrix()
        # add a translation component to transform
        matrix[:3, 3] = g.np.random.random(3) * 100
        # apply transform to points A
        points_b = g.trimesh.transform_points(points_a, matrix)

        # run the solver
        (matrixN,
         transformed,
         cost) = g.trimesh.registration.procrustes(points_a, points_b)
        # the points should be identical
        assert(cost < 0.01)
        # it should have found the matrix we used
        assert g.np.allclose(matrixN, matrix)
github mikedh / trimesh / tests / test_ray.py View on Github external
def test_empty(self):
        """
        Test queries with no hits
        """
        for use_embree in [True, False]:
            dimension = (100, 3)
            sphere = g.get_mesh('unit_sphere.STL',
                                use_embree=use_embree)
            # should never hit the sphere
            ray_origins = g.np.random.random(dimension)
            ray_directions = g.np.tile([0, 1, 0], (dimension[0], 1))
            ray_origins[:, 2] = -5

            # make sure ray functions always return numpy arrays
            # these functions return multiple results all of which
            # should always be a numpy array
            assert all(len(i.shape) >= 0 for i in
                       sphere.ray.intersects_id(
                           ray_origins, ray_directions))
            assert all(len(i.shape) >= 0 for i in
                       sphere.ray.intersects_location(
                           ray_origins, ray_directions))
github mikedh / trimesh / tests / test_points.py View on Github external
def test_pointcloud(self):
        """
        Test PointCloud object
        """
        shape = (100, 3)
        # random points
        points = g.np.random.random(shape)
        # make sure randomness never gives duplicates by offsetting
        points += g.np.arange(shape[0]).reshape((-1, 1))

        # make some duplicate vertices
        points[:10] = points[0]

        # create a pointcloud object
        cloud = g.trimesh.points.PointCloud(points)

        initial_md5 = cloud.md5()

        assert cloud.convex_hull.volume > 0.0

        # check shapes of data
        assert cloud.vertices.shape == shape
        assert cloud.shape == shape
github mikedh / trimesh / tests / test_inertia.py View on Github external
for p in primitives:
            for i in range(100):
                # check to make sure the analytic inertia tensors are relatively
                # close to the meshed inertia tensor (order of magnitude and
                # sign)
                b = p.to_mesh()
                comparison = g.np.abs(
                    p.moment_inertia - b.moment_inertia)
                c_max = comparison.max() / g.np.abs(p.moment_inertia).max()
                assert c_max < .1

                if hasattr(p.primitive, 'transform'):
                    matrix = g.trimesh.transformations.random_rotation_matrix()
                    p.primitive.transform = matrix
                elif hasattr(p.primitive, 'center'):
                    p.primitive.center = g.np.random.random(3)