def embed_label(model, face, side, label, glyph): # transform face to shape (into which we will embed the label) transform = MtxMath.conv_to_euclid(VecMath.rotate_fromto_matrix(face._norm, np.array([0., 0., 1.]))) vertices = [transform * euclid.Point3(v[0], v[1], v[2]) for v in face._vertices] target_path = svg.Path.from_shape(svg.Shape([np.array([v[0], v[1]]) for v in vertices])) # render label into path label_path = svg.Path.combine([(glyph[int(c)], np.array([1., 0.])) for c in label]) # move/scale label to fit into target triangle p, s, r = fit_rect_in_triangle(vertices, side, label_path._bbox) label_path.scale(s[0], s[1]) pivot = label_path._bbox._center+np.array([0, label_path._bbox._size[1]*-.5]) label_path.rotate(r, anchor=pivot) label_path.translate(p-pivot) # embed the label embedded_model = target_path.embed([label_path.triangulate(z=vertices[0].z)], group_name='svg', z=vertices[0].z) embedded_model.transform(transform.inverse()) # (optional) extrude the label embedded_model.get_group('svg')._material._diffuse = [1., 0., 0.] embedded_model.extrude(-.5, faces=embedded_model.get_group('svg')._faces) # replace initial face with new 'labeled face' model.remove_face(face) model.merge(embedded_model) return embedded_model
def write_obj_normals(file, model, transform): """Write vertex normals to obj file""" file.write(f'\n') for face in model._faces: n = transform * euclid.Point3(face._norm[0], face._norm[1], face._norm[2]).normalized() file.write('vn %.4f %.4f %.4f\n' % tuple(n[:3]))
def get_path_from_face(face): transform = MtxMath.conv_to_euclid( VecMath.rotate_fromto_matrix(face._norm, np.array([0., 0., 1.]))) vertices = [ transform * euclid.Point3(v[0], v[1], v[2]) for v in face._vertices ] path = svg.Path.from_shape( svg.Shape([np.array([v[0], v[1]]) for v in vertices])) return path, transform
def add_model(self, model): center = model._center self.translate(-center[0], -center[1], -center[2]) for group in model._groups: vertices = [] normals = [] for face in group._faces: for tv in face._vertices: v = self._transforms * euclid.Point3(tv[0], tv[1], tv[2]) vertices.extend(v[:]) n = self._transforms * euclid.Point3(face._norm[0], face._norm[1], face._norm[2]).normalized() normals.extend(n[:]) material = Material(group._material._name) material.diffuse = group._material._diffuse self._batch.add(len(vertices) // 3, GL_TRIANGLES, material, ('v3f/static', tuple(vertices)), ('n3f/static', tuple(normals)), )
def write(model, filename, orientation_vec=None): """Rotate and center object to lay flat on the heat-bead""" model._update() if orientation_vec is None: transform = euclid.Matrix4.new_translate(-model._center[0], -model._center[1], -model._center[2]) else: transform = MtxMath.conv_to_euclid( VecMath.rotate_fromto_matrix(orientation_vec, np.array([0., 0., -1.]))) n = transform * euclid.Point3(-model._center[0], -model._center[1], -model._center[2]) transform = euclid.Matrix4.new_translate(n.x, n.y, n.z) * transform write_obj(model, filename, transform=transform)
b = height * 0.5 n = euclid.Vector2(side.v[1], side.v[0]).normalized() position = side.p + side.v * .5 - 2 * n rotation = rotation_angle(n, euclid.Vector2(0., rectangle._size[1])) scaling = euclid.Point2(0.1, 0.1) return position, scaling, rotation if __name__ == "__main__": label = '726' model = Model() vertices = [ euclid.Point3(128.36, -3.32, -13.75), euclid.Point3(129.77, 91.40, -13.75), euclid.Point3(103.81, 61.64, -13.75) ] target_path = svg.Path.from_shape( svg.Shape([np.array([v[0], v[1]]) for v in vertices])) # render label into path glyph = svg.Path.read(f'./example/svg/0123.svg', flip_y=True) label_path = svg.Path.combine([(glyph[int(c)], np.array([1., 0.])) for c in label]) p, s, r = fit_rect_in_triangle(vertices, 1, label_path._bbox) label_path.scale(s[0], s[1]) pivot = label_path._bbox._center + np.array( [0, label_path._bbox._size[1] * -.5])
def write_obj_vertices(file, model, transform): """Write vertices to obj file""" file.write(f'\n') for v in model._vertices: vertex = transform * euclid.Point3(v[0], v[1], v[2]) file.write('v %.4f %.4f %.4f\n' % tuple(vertex[:3]))