def __divide_all(self, vertices, triangles): # Subdivide each triangle in the old approximation and normalize # the new points thus generated to lie on the surface of the unit # sphere. # Each input triangle with vertices labelled [0,1,2] as shown # below will be turned into four new triangles: # # Make new points # a = (0+2)/2 # b = (0+1)/2 # c = (1+2)/2 # 1 # /\ Normalize a, b, c # / \ # b/____\ c Construct new triangles # /\ /\ t1 [0,b,a] # / \ / \ t2 [b,1,c] # /____\/____\ t3 [a,b,c] # 0 a 2 t4 [a,c,2] v = [] for tri in triangles: v0 = vertices[tri[0]] v1 = vertices[tri[1]] v2 = vertices[tri[2]] a = vm.unit_vector((v0 + v2) * 0.5) b = vm.unit_vector((v0 + v1) * 0.5) c = vm.unit_vector((v1 + v2) * 0.5) v += [v0, b, a, b, v1, c, a, b, c, a, c, v2] return v, np.arange(len(v)).reshape((-1, 3))
def calculate_vertice_norms(self): self._vertices_norm = [] for v_id in range(len(self._vertices)): v_norm = np.array([0.,0.,0.]) for face in self._faces: if face.contains_vertex(v_id): v_norm += face._norm self._vertices_norm.append(VecMath.unit_vector(v_norm))
model.remove_face(face) model.merge(embedded_model) 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()
def from_points(cls, p0, p1, p2, norm_dir=1.): """ Create a plane from 3 points that span the plane """ return Plane(p0, vm.unit_vector(np.cross(p1 - p0, p2 - p0) * norm_dir))
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 if __name__ == "__main__": radius = 100. sphere = primitives.Sphere(radius) sphere_model = sphere.triangulate(recursion_level=2) for iter in range(0, 4): print(f'noise iteration({iter})') for idx, vertex in enumerate(sphere_model._vertices): l = min([ edge.length for edge in sphere_model.get_edges_with_vertex(idx) ]) * .1 rnd_d = get_random_norm() dir = vm.unit_vector(vertex) transf = MtxMath.conv_to_euclid( vm.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(sphere_model, f'./export/_sphere_noise.obj')
def calculate_norm(self, vertices): self._norm = np.cross( vertices[self._vertex_ids[1]] - vertices[self._vertex_ids[0]], vertices[self._vertex_ids[2]] - vertices[self._vertex_ids[0]]) self._norm = VecMath.unit_vector(self._norm)
def test_unit_vector(self): self.assertEqual( np.linalg.norm(VecMath.unit_vector(np.array([1., 2., 3.]))), 1.)