def dstruc_loadin(self, dir): #output block put before edges first_block = '' #data structure to store the merged graph. key is the edge. #value is the recurrence from kjbuckets import kjDict graph_dict = kjDict() files = os.listdir(dir) sys.stderr.write("\tTotally, %d files to be processed.\n"%len(files)) for f in files: pathname = os.path.join(dir, f) sys.stderr.write("%d/%d:\t%s\n"%(files.index(f)+1,len(files),f)) file_no = files.index(f)+1 inf = open(pathname, 'r') for line in inf: if line[0] == 'e': #edge here, like 'e 3807 3859 0.804645' line_list = line[:-1].split() vertex1 = int(line_list[1]) vertex2 = int(line_list[2]) if vertex1 <= vertex2: edge = (vertex1, vertex2) else: edge = (vertex2, vertex1) if graph_dict.has_key(edge): graph_dict[edge] += 1 else: graph_dict[edge] = 1 elif file_no == 1: first_block += line inf.close() return (first_block, graph_dict)
def _write_faces(self, output, pov_file, identifier): """ Triangulates all faces and writes them to *output* (javascript) and *pov_file* (POVRay). """ BRepMesh_Mesh(self.topo_shape, self._precision) _points = kjDict() faces_iterator = Topo(self.topo_shape).faces() index = 0 for F in faces_iterator: face_location = TopLoc_Location() triangulation = BRep_Tool_Triangulation(F, face_location) if triangulation.IsNull() == False: facing = triangulation.GetObject() tab = facing.Nodes() tri = facing.Triangles() the_normal = TColgp_Array1OfDir(tab.Lower(), tab.Upper()) StdPrs_ToolShadedShape_Normal(F, Poly_Connect(facing.GetHandle()), the_normal) for i in range(1, facing.NbTriangles() + 1): trian = tri.Value(i) if F.Orientation() == TopAbs_REVERSED: index1, index3, index2 = trian.Get() else: index1, index2, index3 = trian.Get() P1 = tab.Value(index1).Transformed( face_location.Transformation()) P2 = tab.Value(index2).Transformed( face_location.Transformation()) P3 = tab.Value(index3).Transformed( face_location.Transformation()) p1_coord = P1.XYZ().Coord() p2_coord = P2.XYZ().Coord() p3_coord = P3.XYZ().Coord() if self._triangle_is_valid(P1, P2, P3): for point in (p1_coord, p2_coord, p3_coord): if not _points.has_key(point): _points.add(point, index) output.write( vertice_fmt % (identifier, point[0], point[1], point[2])) index += 1 n1 = the_normal(index1) n2 = the_normal(index2) n3 = the_normal(index2) output.write(face_fmt % ( identifier, _points.neighbors(p1_coord)[0], _points.neighbors(p2_coord)[0], _points.neighbors(p3_coord)[0], n1.X(), n1.Y(), n1.Z(), n2.X(), n2.Y(), n2.Z(), n3.X(), n3.Y(), n3.Z(), )) pov_file.write(triangle_fmt % ( p1_coord[0], p1_coord[1], p1_coord[2], n1.X(), n1.Y(), n1.Z(), p2_coord[0], p2_coord[1], p2_coord[2], n2.X(), n2.Y(), n2.Z(), p3_coord[0], p3_coord[1], p3_coord[2], n3.X(), n3.Y(), n3.Z(), )) self.triangle_count += 1
del d[i] d[ ` i `] = ` (i * 3 % 1000) ` for i in r2: del d[ ` i `] def dtest4(d): for i in range(10): dtest(d) dtest2(d) dtest3(d) if __name__ == "__main__": from kjbuckets import kjDict dtest4(kjDict()) # some profiling done on my ancient sun server # # example stats for Python dict #>>> D = {} #>>> profile.run("dtest4(D)") # 33 function calls in 83.033 CPU seconds # # Ordered by: standard name # # ncalls tottime percall cumtime percall filename:lineno(function) # 10 14.383 1.438 14.383 1.438 kjtest.py:11(dtest2) # 10 20.967 2.097 20.967 2.097 kjtest.py:17(dtest3) # 1 0.083 0.083 83.017 83.017 kjtest.py:28(dtest4) # 10 47.583 4.758 47.583 4.758 kjtest.py:7(dtest)
for i in r: temp = d[ (i*7) % 1000 ] for i in r2: del d[i] d[`i`] = `(i*3%1000)` for i in r2: del d[`i`] def dtest4(d): for i in range(10): dtest(d) dtest2(d) dtest3(d) if __name__=="__main__": from kjbuckets import kjDict dtest4(kjDict()) # some profiling done on my ancient sun server # # example stats for Python dict #>>> D = {} #>>> profile.run("dtest4(D)") # 33 function calls in 83.033 CPU seconds # # Ordered by: standard name # # ncalls tottime percall cumtime percall filename:lineno(function) # 10 14.383 1.438 14.383 1.438 kjtest.py:11(dtest2) # 10 20.967 2.097 20.967 2.097 kjtest.py:17(dtest3) # 1 0.083 0.083 83.017 83.017 kjtest.py:28(dtest4) # 10 47.583 4.758 47.583 4.758 kjtest.py:7(dtest)
def _write_faces(self, output, pov_file, identifier): """ Triangulates all faces and writes them to *output* (javascript) and *pov_file* (POVRay). """ BRepMesh_Mesh(self.topo_shape, self._precision) _points = kjDict() faces_iterator = Topo(self.topo_shape).faces() index=0 for F in faces_iterator: face_location = TopLoc_Location() triangulation = BRep_Tool_Triangulation(F,face_location) if triangulation.IsNull() == False: facing = triangulation.GetObject() tab = facing.Nodes() tri = facing.Triangles() the_normal = TColgp_Array1OfDir(tab.Lower(), tab.Upper()) StdPrs_ToolShadedShape_Normal(F, Poly_Connect(facing.GetHandle()), the_normal) for i in range(1, facing.NbTriangles()+1): trian = tri.Value(i) if F.Orientation() == TopAbs_REVERSED: index1, index3, index2 = trian.Get() else: index1, index2, index3 = trian.Get() P1 = tab.Value(index1).Transformed(face_location.Transformation()) P2 = tab.Value(index2).Transformed(face_location.Transformation()) P3 = tab.Value(index3).Transformed(face_location.Transformation()) p1_coord = P1.XYZ().Coord() p2_coord = P2.XYZ().Coord() p3_coord = P3.XYZ().Coord() if self._triangle_is_valid(P1, P2, P3): for point in (p1_coord, p2_coord, p3_coord): if not _points.has_key(point): _points.add(point, index) output.write(vertice_fmt % (identifier, point[0], point[1], point[2])) index+=1 n1 = the_normal(index1) n2 = the_normal(index2) n3 = the_normal(index2) output.write(face_fmt % (identifier, _points.neighbors(p1_coord)[0], _points.neighbors(p2_coord)[0], _points.neighbors(p3_coord)[0], n1.X(), n1.Y(), n1.Z(), n2.X(), n2.Y(), n2.Z(), n3.X(), n3.Y(), n3.Z(), )) pov_file.write(triangle_fmt % ( p1_coord[0], p1_coord[1], p1_coord[2], n1.X(), n1.Y(), n1.Z(), p2_coord[0], p2_coord[1], p2_coord[2], n2.X(), n2.Y(), n2.Z(), p3_coord[0], p3_coord[1], p3_coord[2], n3.X(), n3.Y(), n3.Z(), )) self.triangle_count += 1