def labels2meshes_ski(
    labelimage,
    labels,
    spacing=[0, 0, 0],
    nvoxthr=0
):  # deprecated and underdeveloped: use vtk implementation instead
    """"""
    for label in labels:
        labelmask = labelimage == label
        if np.count_nonzero(labelmask) > nvoxthr:
            labelmask = binary_closing(labelmask)
            verts, faces = marching_cubes(labelmask, 0, spacing=spacing)
            faces = correct_mesh_orientation(labelmask,
                                             verts,
                                             faces,
                                             spacing=spacing,
                                             gradient_direction='descent')
            # Fancy indexing to define two vector arrays from triangle vertices
            actual_verts = verts[faces]
            a = actual_verts[:, 0, :] - actual_verts[:, 1, :]
            b = actual_verts[:, 0, :] - actual_verts[:, 2, :]
            # Find normal vectors for each face via cross product
            crosses = np.cross(a, b)
            normals = crosses / (np.sum(crosses**2, axis=1)**(0.5))[:,
                                                                    np.newaxis]
            ob = stl.Solid(name=label)
            for ii, _ in enumerate(faces):
                ob.add_facet(normals[ii], actual_verts[ii])
            with open(str(label) + ".stl",
                      'w') as f:  #with open("allobjects.stl", 'a') as f:
                ob.write_ascii(f)  #ob.write_binary(f)
                f.write("\n")
Example #2
0
def stl_from_triangles(triangles):
    facets = []
    for triangle in triangles:
        normal = normal_from_triangle(triangle)
        facet = stl.types.Facet(normal.as_list(), [
            triangle[0].as_list(), triangle[1].as_list(),
            triangle[2].as_list()
        ])
        facets.append(facet)
    print("{} facets".format(len(facets)))
    return stl.Solid("thing", facets)
Example #3
0
    def test_solid_output_for_simple_graph(self):
        def simple(x, y):
            if x > 0.5 and y > 0.5:
                return 2
            else:
                return 1

        graph = ZGraph((0, 1), (0, 1), simple, 1)

        top_left_front = stl.Vector3d(0, 0, 1)
        top_right_front = stl.Vector3d(1, 0, 1)
        top_left_back = stl.Vector3d(0, 1, 1)
        top_right_back = stl.Vector3d(1, 1, 2)
        bottom_left_front = stl.Vector3d(0, 0, 0)
        bottom_right_front = stl.Vector3d(1, 0, 0)
        bottom_left_back = stl.Vector3d(0, 1, 0)
        bottom_right_back = stl.Vector3d(1, 1, 0)
        top = [
            stl.Facet(stl.Vector3d(0, -1 / sqrt(2), 1 / sqrt(2)),
                      [top_left_front, top_right_front, top_right_back]),
            stl.Facet(stl.Vector3d(-1 / sqrt(2), 0, 1 / sqrt(2)),
                      [top_left_front, top_right_back, top_left_back]),
        ]
        bottom = ZGraph.triangulate(stl.Vector3d(0, 0, -1),
                                    (bottom_left_front, bottom_left_back,
                                     bottom_right_back, bottom_right_front))
        front = ZGraph.triangulate(stl.Vector3d(0, -1, 0),
                                   (bottom_left_front, bottom_right_front,
                                    top_right_front, top_left_front))
        right = ZGraph.triangulate(stl.Vector3d(1, 0, 0),
                                   (bottom_right_front, bottom_right_back,
                                    top_right_back, top_right_front))
        back = ZGraph.triangulate(stl.Vector3d(0, 1, 0),
                                  (bottom_left_back, top_left_back,
                                   top_right_back, bottom_right_back))
        left = ZGraph.triangulate(stl.Vector3d(-1, 0, 0),
                                  (bottom_left_front, top_left_front,
                                   top_left_back, bottom_left_back))
        facets = top + bottom + front + right + back + left
        expected = stl.Solid("ZGraph", facets)

        self.assertEqual(graph.solid_output(), expected)
Example #4
0
    def test_solid_output_for_const_graph(self):
        def const_2(x, y):
            return 2

        graph = ZGraph((0, 1), (0, 1), const_2, 1)

        top_left_front = stl.Vector3d(0, 0, 2)
        top_right_front = stl.Vector3d(1, 0, 2)
        top_left_back = stl.Vector3d(0, 1, 2)
        top_right_back = stl.Vector3d(1, 1, 2)
        bottom_left_front = stl.Vector3d(0, 0, 0)
        bottom_right_front = stl.Vector3d(1, 0, 0)
        bottom_left_back = stl.Vector3d(0, 1, 0)
        bottom_right_back = stl.Vector3d(1, 1, 0)
        top = ZGraph.triangulate(
            stl.Vector3d(0, 0, 1),
            (top_left_front, top_right_front, top_right_back, top_left_back))
        bottom = ZGraph.triangulate(stl.Vector3d(0, 0, -1),
                                    (bottom_left_front, bottom_left_back,
                                     bottom_right_back, bottom_right_front))
        front = ZGraph.triangulate(stl.Vector3d(0, -1, 0),
                                   (bottom_left_front, bottom_right_front,
                                    top_right_front, top_left_front))
        right = ZGraph.triangulate(stl.Vector3d(1, 0, 0),
                                   (bottom_right_front, bottom_right_back,
                                    top_right_back, top_right_front))
        back = ZGraph.triangulate(stl.Vector3d(0, 1, 0),
                                  (bottom_left_back, top_left_back,
                                   top_right_back, bottom_right_back))
        left = ZGraph.triangulate(stl.Vector3d(-1, 0, 0),
                                  (bottom_left_front, top_left_front,
                                   top_left_back, bottom_left_back))
        facets = top + bottom + front + right + back + left
        expected = stl.Solid("ZGraph", facets)

        self.assertEqual(graph.solid_output(), expected)
Example #5
0
spacing = (0.03, 0.006, 0.006)
xyzOffset = (1100, 8000, 5000)

for label in labels:
    labelmask = label_im == label
    verts, faces = measure.marching_cubes(labelmask, 0, spacing=spacing)
    faces = measure.correct_mesh_orientation(labelmask, verts, faces, spacing=spacing, gradient_direction='descent')
    # from scikit_image correct_mesh_orientation
    # Fancy indexing to define two vector arrays from triangle vertices
    actual_verts = verts[faces]
    a = actual_verts[:, 0, :] - actual_verts[:, 1, :]
    b = actual_verts[:, 0, :] - actual_verts[:, 2, :]
    # Find normal vectors for each face via cross product
    crosses = np.cross(a, b)
    normals = crosses / (np.sum(crosses ** 2, axis=1) ** (0.5))[:, np.newaxis]
    ob = stl.Solid(label)
    for ii,face in enumerate(faces):
        ob.add_facet(normals[ii], actual_verts[ii])
    with open(str(label) + ".stl", 'w') as f:  #with open("allobjects.stl", 'a') as f:
        ob.write_binary(f)
        f.write("\n");








dims = label_im.shape
Example #6
0
 def solid_output(self):
     return stl.Solid(name="ZGraph",
                      facets=self.__top + self.__bottom + self.__front +
                      self.__right + self.__back + self.__left)
Example #7
0
    normal = tuple(np.dot(m, f.normal))
    vertices = []
    for i in f.vertices:
        vertices.append(np.dot(m, i))
    facet = stl.Facet(normal, vertices)
    return facet


if __name__ == '__main__':

    import stl
    import os

    i = 0
    while i < 2 * math.pi:

        i += 0.1
        frame = '%02f' % (i,)

        door = stl.read_binary_file(open('/home/mburr/tmp/pov/door.stl.ORIG', 'rb'))
        facets = []
        for facet in door.facets:
            facet = fix_facet(facet)
            facets.append(facet)
        new_door = stl.Solid('new_door_01', facets)
        stl.binary.write(new_door, open('door.%s.stl' % frame, 'wb'))
        os.system('stl2pov door.%s.stl' % frame)
        content = open('door.pov.template').read() % {'name': 'foo'}
        open('door.%s.pov' % frame, 'wb').write(content)
        break
### Fault Points as python lists
fstCurve = [
    bezier(p1, p2, p3, p4, t) for t in np.linspace(0, 1 - 1 / Cres, Cres)
]
scdCurve = [bezier(p4, p5, p6, p1, t) for t in np.linspace(0, 1, Cres)]

# Assemble fault points and extrude in Z-direction
points = fstCurve + scdCurve
points.reverse()
[normals, triangles] = sTri(points, depth)

## Create STL facets with normals inwarding
faces = [stl.Facet(normals[i], triangles[i]) for i in range(len(normals))]

## Create fault solid
fault = stl.Solid(fault, faces)
fs = open(filename, mode='w+')
fault.write_ascii(fs)

## Create Outer Boundary patches
# Top patch
tp = np.array([ [bBox[0][0], bBox[1][1], bBox[0][2]],\
     [bBox[1][0],bBox[1][1],bBox[0][2]] ])
[tn, tt] = sTri(tp, depth)
tf = [stl.Facet(tn[i], tt[i]) for i in range(len(tt))]
top = stl.Solid(top, tf)
top.write_ascii(fs)
# Right patch
tp = np.array([ [bBox[1][0], bBox[1][1], bBox[0][2]],\
     [bBox[1][0],bBox[0][1],bBox[0][2]] ])
[tn, tt] = sTri(tp, depth)