How to use the generic.np.array 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_loaded.py View on Github external
# we're going to load faces in a basic text way
        # and compare the order from this method to the
        # trimesh loader, to see if we get the same thing
        faces = []
        verts = []
        with open(file_name, 'r') as f:
            for line in f:
                line = line.strip()
                if line[0] == 'f':
                    faces.append(line[1:].strip().split())
                if line[0] == 'v':
                    verts.append(line[1:].strip().split())

        # get faces as basic numpy array
        faces = g.np.array(faces, dtype=g.np.int64) - 1
        verts = g.np.array(verts, dtype=g.np.float64)

        # trimesh loader should return the same face order
        assert g.np.allclose(faces, m.faces)
        assert g.np.allclose(verts, m.vertices)
github mikedh / trimesh / tests / test_obj.py View on Github external
# we're going to load faces in a basic text way
        # and compare the order from this method to the
        # trimesh loader, to see if we get the same thing
        faces = []
        verts = []
        with open(file_name, 'r') as f:
            for line in f:
                line = line.strip()
                if line[0] == 'f':
                    faces.append(line[1:].strip().split())
                if line[0] == 'v':
                    verts.append(line[1:].strip().split())

        # get faces as basic numpy array
        faces = g.np.array(faces, dtype=g.np.int64) - 1
        verts = g.np.array(verts, dtype=g.np.float64)

        # trimesh loader should return the same face order
        assert g.np.allclose(faces, m.faces)
        assert g.np.allclose(verts, m.vertices)
github mikedh / trimesh / tests / test_identifier.py View on Github external
md5.append(permutated.identifier_md5)
                idf.append(permutated.identifier)

            result = g.np.array(md5)
            ok = (result[0] == result[1:]).all()

            if not ok:
                debug = []
                for a in idf:
                    as_int, exp = g.trimesh.util.sigfig_int(
                        a, g.trimesh.comparison.id_sigfig)

                    debug.append(as_int * (10**exp))
                g.log.error('Hashes on %s differ after transform! diffs:\n %s\n',
                            mesh.metadata['file_name'],
                            str(g.np.array(debug, dtype=g.np.int)))

                raise ValueError('values differ after transform!')

            if md5[-1] == permutated.permutate.noise(
                    mesh.scale / 100.0).identifier_md5:
                raise ValueError('Hashes on %s didn\'t change after noise!',
                                 mesh.metadata['file_name'])
github mikedh / trimesh / tests / test_voxel.py View on Github external
v = voxel.VoxelGrid(matrix).apply_scale(
            pitch).apply_translation(origin)

        boxes1 = v.as_boxes()
        boxes2 = ops.multibox(centers).apply_scale(pitch)
        colors = [g.trimesh.visual.DEFAULT_COLOR] * matrix.sum() * 12
        for boxes in [boxes1, boxes2]:
            g.np.testing.assert_allclose(
                boxes.visual.face_colors, colors, atol=0, rtol=0)

        # check assigning a single color
        color = [255, 0, 0, 255]
        boxes1 = v.as_boxes(colors=color)
        boxes2 = ops.multibox(
            centers=centers, colors=color).apply_scale(pitch)
        colors = g.np.array([color] * len(centers) * 12)
        for boxes in [boxes1, boxes2]:
            g.np.testing.assert_allclose(
                boxes.visual.face_colors, colors, atol=0, rtol=0)

        # check matrix colors
        colors = color * g.np.ones(g.np.append(v.shape, 4),
                                   dtype=g.np.uint8)
        boxes = v.as_boxes(colors=colors)
        assert g.np.allclose(
            boxes.visual.face_colors, color, atol=0, rtol=0)
github mikedh / trimesh / tests / test_color.py View on Github external
def test_interpolate(self):
        """
        Check our color interpolation
        """
        values = g.np.array([-1.0, 0.0, 1.0, 2.0])
        # should clamp
        colors = g.trimesh.visual.linear_color_map(values)

        assert g.np.allclose(colors[0], [255, 0, 0, 255])
        assert g.np.allclose(colors[1], [255, 0, 0, 255])
        assert g.np.allclose(colors[2], [0, 255, 0, 255])
        assert g.np.allclose(colors[3], [0, 255, 0, 255])

        # should scale to range
        colors = g.trimesh.visual.interpolate(values)
        assert g.np.allclose(colors[0], [255, 0, 0, 255])
        # scaled to range not clamped
        assert not g.np.allclose(colors[1], [255, 0, 0, 255])
        assert not g.np.allclose(colors[2], [0, 255, 0, 255])
        # end of range
        assert g.np.allclose(colors[3], [0, 255, 0, 255])
github mikedh / trimesh / tests / test_align.py View on Github external
def test_rigid(self):
        # check issues with near-reversed vectors not returning rigid
        align = g.trimesh.geometry.align_vectors
        T = align([0, 0, -1], [-1e-17, 1e-17, 1])
        assert g.np.isclose(g.np.linalg.det(T), 1.0)

        T = align([0, 0, -1], [-1e-4, 1e-4, 1])
        assert g.np.isclose(g.np.linalg.det(T), 1.0)

        vector_1 = g.np.array([7.12106798e-07, -7.43194705e-08, 1.00000000e+00])
        vector_2 = g.np.array([0, 0, -1])
        T, angle = align(vector_1, vector_2, return_angle=True)
        assert g.np.isclose(g.np.linalg.det(T), 1.0)
github mikedh / trimesh / tests / test_paths.py View on Github external
group = g.trimesh.grouping.group_rows(edges, require_count=1)

        # run the polygon conversion
        polygon = g.trimesh.path.polygons.edges_to_polygons(
            edges=edges[group],
            vertices=vertices)

        assert len(polygon) == 1
        assert g.np.isclose(polygon[0].area,
                            m.facets_area[index])

        # try transforming the polygon around
        M = g.np.eye(3)
        M[0][2] = 10.0
        P2 = g.trimesh.path.polygons.transform_polygon(polygon[0], M)
        distance = g.np.array(P2.centroid) - g.np.array(polygon[0].centroid)
        assert g.np.allclose(distance, [10.0, 0])
github mikedh / trimesh / tests / test_grouping.py View on Github external
r = blocks(**kwargs)
        assert len(r) == 2
        assert len(r[0]) == 1
        assert len(r[1]) == 3
        check_roll_wrap(**kwargs)

        # one block and one eligible but non-block point
        data = g.np.array([1, 0, 0, 0, 1, 1, 1, 1])
        r = blocks(
            data,
            min_len=2, wrap=True, only_nonzero=True)
        assert len(r) == 1
        assert g.np.allclose(data[r[0]], 1)

        # CASE: neither are in a block but together they are eligible
        data = g.np.array([1, 0, 0, 0, 1])
        kwargs = {'data': data,
                  'min_len': 3,
                  'wrap': True,
                  'only_nonzero': True}
        r = blocks(**kwargs)
        assert len(r) == 0
        check_roll_wrap(**kwargs)
        kwargs['only_nonzero'] = False
        r = blocks(**kwargs)
        assert len(r) == 1
        assert g.np.allclose(data[r[0]], 0)
        check_roll_wrap(**kwargs)

        kwargs['data'] = g.np.abs(data - 1)
        # should be the same even inverted
        rn = blocks(**kwargs)
github mikedh / trimesh / tests / test_raster.py View on Github external
# rasterize just the outline
        outline = p.rasterize(origin=origin,
                              pitch=pitch,
                              resolution=resolution,
                              fill=False,
                              width=2.0)

        # rasterize both
        both = p.rasterize(origin=origin,
                           pitch=pitch,
                           resolution=resolution,
                           fill=True,
                           width=2.0)

        # count the number of filled pixels
        fill_cnt = g.np.array(filled).sum()
        both_cnt = g.np.array(both).sum()
        outl_cnt = g.np.array(outline).sum()

        # filled should have more than an outline
        assert fill_cnt > outl_cnt
        # filled+outline should have more than outline
        assert both_cnt > outl_cnt
        # filled+outline should have more than filled
        assert both_cnt > fill_cnt
github mikedh / trimesh / tests / test_triangles.py View on Github external
def test_zero_angle(self):
        # a zero- area triangle
        tris = g.np.array(
            [[[0, 0, 0], [1, 0, 0], [1, 0, 0]]], dtype=g.np.float64)
        angles = g.trimesh.triangles.angles(tris)
        # degenerate angles should be zero, not NaN
        assert g.np.allclose(angles, 0.0)