def push(self): # Save Blender's current state editmode = Blender.Window.EditMode() if editmode: Blender.Window.EditMode(0) if not self.mesh: # Create a new mesh self.mesh = bpy.data.meshes.new(self.name) # Link mesh to current scene scn = bpy.data.scenes.active ob = scn.objects.new(self.mesh, 'myObj') # Clear out the mesh self.mesh.faces.delete([i for i,face in enumerate(self.mesh.faces)]) self.mesh.verts.delete([i for i,face in enumerate(self.mesh.verts)]) # Put new vertices and faces into the mesh vertices,faces = gts.get_coords_and_face_indices(self) self.mesh.verts.extend(coords) self.mesh.faces.extend(faces) # Return Blender to original state if editmode: Blender.Window.EditMode(1)
def _New_Initialization(self): """Initialization(self) Run important initialization steps important for the class to work. self.vertices contains x,y,z coordinates of vertices. shape = self.n_vertices,3 self.faces contains indices of vertices forming faces. shape = self.n_faces,3 self.assoc contains indices of faces associated to a vertice. shape = self.n_vertices,6 Note: when only 5 faces associated, 6th value is equal to -99 """ print("Generating the geodesic surface using PyGTS") try: import gts except: print( "You likely don't have the PyGTS package installed on your computer." ) print( "It is impossible to create the surface vertices from scratch." ) print("Will trying reading them from the restart file instead.") self._Read_geodesic() return # Generate the geodesic primitives s = gts.sphere(self.ndiv) x, y, z, t = gts.get_coords_and_face_indices(s, True) self.vertices = np.c_[x, y, z] self.faces = np.array(t) self.n_vertices = self.vertices.shape[0] self.n_faces = self.faces.shape[0] print("Calculatating the associations") self.assoc = Utils.Tessellation.Match_assoc(self.faces, self.n_vertices) # We will pre-calculate the surface areas. They will need to be multiplied by rc^2. # The calculation is simply the Pythagorean sum of the areas of the respective projections on the x,y,z planes. print("meshing the surface") mesh = self.vertices[self.faces] print("calculating the area") self.pre_area = 0.5 * np.sqrt( ((mesh[:, 0, 0] * mesh[:, 1, 1] + mesh[:, 1, 0] * mesh[:, 2, 1] + mesh[:, 2, 0] * mesh[:, 0, 1]) - (mesh[:, 0, 1] * mesh[:, 1, 0] + mesh[:, 1, 1] * mesh[:, 2, 0] + mesh[:, 2, 1] * mesh[:, 0, 0]))**2 + ((mesh[:, 0, 1] * mesh[:, 1, 2] + mesh[:, 1, 1] * mesh[:, 2, 2] + mesh[:, 2, 1] * mesh[:, 0, 2]) - (mesh[:, 0, 2] * mesh[:, 1, 1] + mesh[:, 1, 2] * mesh[:, 2, 1] + mesh[:, 2, 2] * mesh[:, 0, 1]))**2 + ((mesh[:, 0, 2] * mesh[:, 1, 0] + mesh[:, 1, 2] * mesh[:, 2, 0] + mesh[:, 2, 2] * mesh[:, 0, 0]) - (mesh[:, 0, 0] * mesh[:, 1, 2] + mesh[:, 1, 0] * mesh[:, 2, 2] + mesh[:, 2, 0] * mesh[:, 0, 2]))**2) # The cosine of x,y,z for the center of the faces. shape = n_faces, 3 print("calculating the angles") self.cosx, self.cosy, self.cosz = mesh.mean(axis=1).T return
def plot_surface(s): x, y, z, t = gts.get_coords_and_face_indices(s, True) mlab.triangular_mesh(x, y, z, t, color=(0.8, 0.8, 0.8)) mlab.triangular_mesh(x, y, z, t, color=(0, 0, 1), representation='fancymesh', tube_radius=.001, scale_factor=0.001)
def build(cls, m, plot=True, region_color_map=None): import gts surface_sections = cls.buildsurface_sectiondict(m.to_tree()) meshes = [] for (sect, sect_surface) in surface_sections.iteritems(): print sect assert sect.region is not None # Look up the region color: if not sect.region.name in region_color_map: for (rgn, color) in region_color_map.iteritems(): print rgn.name, rgn, color print 'Looking for:', sect.region.name, sect.region assert False, "Can't find region in color map!" sect_color = region_color_map[sect.region.name] print sect_color vertex_objs = sect_surface.vertices() N = len(vertex_objs) dShape = (N, 3) v = np.array([(v.x, v.y, v.z) for v in vertex_objs]).reshape(dShape) color = np.array((sect_color.r, sect_color.g, sect_color.b)) colors = np.repeat(color, len(vertex_objs)).reshape(dShape, order='F') triangles = sect_surface.face_indices(vertex_objs) tm = TriangleMesh(vertices=v, triangles=triangles, vertex_colors=colors) meshes.append(tm) m = TriangleMesh.merge(meshes=meshes) if plot: from mayavi import mlab mlab.figure(size=(1024, 768)) for surface in surface_sections: (x, y, z, t) = gts.get_coords_and_face_indices(surface, True) mlab.triangular_mesh(x, y, z, t, color=(0.9, 0.9, 0.9)) mlab.show() return m
def _New_Initialization(self): """Initialization(self) Run important initialization steps important for the class to work. self.vertices contains x,y,z coordinates of vertices. shape = self.n_vertices,3 self.faces contains indices of vertices forming faces. shape = self.n_faces,3 self.assoc contains indices of faces associated to a vertice. shape = self.n_vertices,6 Note: when only 5 faces associated, 6th value is equal to -99 """ print( "Generating the geodesic surface using PyGTS" ) try: import gts except: print( "You likely don't have the PyGTS package installed on your computer." ) print( "It is impossible to create the surface vertices from scratch." ) print( "Will trying reading them from the restart file instead." ) self._Read_geodesic() return # Generate the geodesic primitives s = gts.sphere(self.ndiv) x,y,z,t = gts.get_coords_and_face_indices(s,True) self.vertices = np.c_[x,y,z] self.faces = np.array(t) self.n_vertices = self.vertices.shape[0] self.n_faces = self.faces.shape[0] print( "Calculatating the associations" ) self.assoc = Utils.Tessellation.Match_assoc(self.faces, self.n_vertices) # We will pre-calculate the surface areas. They will need to be multiplied by rc^2. # The calculation is simply the Pythagorean sum of the areas of the respective projections on the x,y,z planes. print( "meshing the surface" ) mesh = self.vertices[self.faces] print( "calculating the area" ) self.pre_area = 0.5 *np.sqrt( ((mesh[:,0,0]*mesh[:,1,1]+mesh[:,1,0]*mesh[:,2,1]+mesh[:,2,0]*mesh[:,0,1]) - (mesh[:,0,1]*mesh[:,1,0]+mesh[:,1,1]*mesh[:,2,0]+mesh[:,2,1]*mesh[:,0,0]))**2 + ((mesh[:,0,1]*mesh[:,1,2]+mesh[:,1,1]*mesh[:,2,2]+mesh[:,2,1]*mesh[:,0,2]) - (mesh[:,0,2]*mesh[:,1,1]+mesh[:,1,2]*mesh[:,2,1]+mesh[:,2,2]*mesh[:,0,1]))**2 + ((mesh[:,0,2]*mesh[:,1,0]+mesh[:,1,2]*mesh[:,2,0]+mesh[:,2,2]*mesh[:,0,0]) - (mesh[:,0,0]*mesh[:,1,2]+mesh[:,1,0]*mesh[:,2,2]+mesh[:,2,0]*mesh[:,0,2]))**2 ) # The cosine of x,y,z for the center of the faces. shape = n_faces, 3 print( "calculating the angles" ) self.cosx, self.cosy, self.cosz = mesh.mean(axis=1).T return