def triangulate(self, sides): cylinder_model = model.Model() angle_f = 2. * math.pi / float(sides) base_verts = [ euclid.Vector3( math.cos(angle_f * i) * self._radius, math.sin(angle_f * i) * self._radius, 0.) + self._center for i in range(0, sides) ] top_verts = [ euclid.Vector3(0, 0, self._height) + v for v in base_verts ] if self._height > 0: cylinder_model.add_face(top_verts, tags=['top']) cylinder_model.add_face(list(reversed(base_verts)), tags=['bottom']) for i in range(0, sides): cylinder_model.add_face([ base_verts[i], base_verts[(i + 1) % sides], top_verts[(i + 1) % sides], top_verts[i] ], tags=['side']) else: cylinder_model.add_face(base_verts, tags=['top']) cylinder_model.add_face(list(reversed(top_verts)), tags=['bottom']) for i in range(0, sides): cylinder_model.add_face([ top_verts[i], top_verts[(i + 1) % sides], base_verts[(i + 1) % sides], base_verts[i] ], tags=['side']) return cylinder_model
def get_random_norm(): d = euclid.Vector3(random.uniform(0, 1), random.uniform(0, 1), 0.).normalize() # d = euclid.Vector3(0., 0., 0.) # d.x = random.uniform(-1, 1) # d.y = random.uniform(-1, 1) # d.z = random.uniform(-.1, .1) # d.z = 1. return d
def get_random_norm(): d = euclid.Vector3(random.uniform(0, 1), random.uniform(0, 1), 0.).normalize() return d
return embedded_model if __name__ == "__main__": radius = 100. sphere = primitives.Sphere(radius) obj_model = sphere.triangulate(recursion_level=2) for iter in range(0,4): print(f'noise iteration({iter})') for idx, vertex in enumerate(obj_model._vertices): l = min([edge.length for edge in obj_model.get_edges_with_vertex(idx)]) * .1 rnd_d = get_random_norm() dir = VecMath.unit_vector(vertex) transf = MtxMath.conv_to_euclid(VecMath.rotate_fromto_matrix(np.array([0., 0., 1.]), dir)) tv = transf * (rnd_d*euclid.Vector3(l,l,10)) vertex += tv # sphere_model.triangulate() ObjExporter.write(obj_model, f'./export/_sphere_noise.obj') print(f'Faces: {len(obj_model._faces)}') print(f'Vertices: {len(obj_model._vertices)}') bbox_size = obj_model._size print(f'Boundingbox: [{bbox_size[0]}, {bbox_size[1]}, {bbox_size[2]}]') target_lid_size = 100. #mm^2 faceted_model = Model() facets = [] for face_id, face in enumerate(obj_model._faces):
def transform(self, mtx): for i, v in enumerate(self._vertices): vt = mtx * euclid.Vector3(v[0], v[1], v[2]) self._vertices[i] = np.array([vt[0], vt[1], vt[2]]) self._update()
def rotate(self, angle, x, y, z): self._transforms.rotate_axis(math.pi*angle/180.0, euclid.Vector3(x, y, z))
def __init__(self, radius, center=euclid.Vector3(0., 0., 0.)): self._center = euclid.Vector3(center[0], center[1], center[2]) self._radius = radius
import primitives import svg import PathUtility import ObjExporter from euclid import euclid if __name__ == "__main__": filename = 'logo_tamedia_black' logo_path = svg.Path.read(f'./example/svg/{filename}.svg', flip_y=True) logo_bbox = PathUtility.get_bbox_for_path(logo_path) cylinder = primitives.Cylinder(logo_bbox._radius, -15., center=euclid.Vector3( logo_bbox._center[0], logo_bbox._center[1], 0.)) cylinder_model = cylinder.triangulate(64) top_faces = cylinder_model.get_faces_by_tag('top') path, transform = PathUtility.get_path_from_faces(top_faces) embedded_model = path.embed([logo_path[0].triangulate()], group_name='svg') embedded_model.transform(transform.inverse()) embedded_model.extrude(15., faces=embedded_model.get_group('svg')._faces) # replace top face with logo cylinder_model.remove_face(top_faces) cylinder_model.merge(embedded_model) # cylinder_model.get_group('svg')._material._diffuse = [1., 0., 0.] cylinder_model.triangulate()