def display(self, color=(190, 205, 205), add=False): """ Display the 2-cells of a 2-G-Map using the ordered orbit of its darts in PlantGL. For each face element, retrieve the position of its ordered face darts and add a FaceSet PlantGL object to the scene. Example : s += pgl.Shape(pgl.FaceSet( [[0,0,0],[1,0,0],[1,1,0],[0,1,0]], [[0,1,2,3]]) , pgl.Material((0,100,0))) # for a green square """ from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Viewer from random import randint s = Scene() for facedart in self.elements(2): lastfart = facedart positions = [] for dart in self.oderedorbit(facedart, [0, 1]): if self.alpha(0, dart) != lastfart: positions.append(self.get_position(dart)) lastfart = dart if color is None: mat = Material( (randint(0, 255), randint(0, 255), randint(0, 255))) else: mat = Material(tuple(color), diffuse=0.25) s.add( Shape(FaceSet(positions, [range(len(positions))]), mat, facedart)) if add: Viewer.add(s) else: Viewer.display(s)
def display(self, color=[190, 205, 205], add=False, transparency=0): from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Viewer from random import randint s = Scene() for facedart in self.elements(2): lastdart = facedart positions = [] for dart in self.orderedorbit(facedart, [0, 1]): if self.alpha(0, dart) != lastdart: positions.append(self.get_position(dart)) lastdart = dart if color is None: mat = Material( (randint(0, 255), randint(0, 255), randint(0, 255)), transparency=transparency) else: mat = Material(tuple(color), diffuse=0.25, transparency=transparency) s.add( Shape(FaceSet(positions, [range(len(positions))]), mat, facedart)) if add: Viewer.add(s) else: Viewer.display(s)
def darts2pglscene(self, textsize = None): from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Polyline, PointSet, Translated, Text, Font from numpy import array s = Scene() th = 0.1 matdart = Material((0,0,0)) matalphai = [Material((100,100,100)),Material((0,200,0)),Material((0,0,200)),Material((200,0,0))] matlabel = Material((0,0,0)) alphaipos = [{} for i in xrange(3)] vertex = [] font = Font(size=textsize if textsize else 10) def process_edge(dart, facecenter = None, fshift = [0.,0.,0.]): try: edgecenter = self.cell_center(dart,1) except: print 'Cannot display dart', dart,': no coordinates.' return eshift = array(fshift) if not facecenter is None : eshift += (facecenter-edgecenter)*2*th for cdart in [dart, self.alpha(0,dart)]: pi = self.get_position(cdart) pdir = (edgecenter-pi) pi = pi + (pdir*(th*3) + eshift) vertex.append(pi) dartlengthratio = 0.6 s.add(Shape(Polyline([pi,pi+pdir*dartlengthratio]) , matdart, id = cdart)) s.add(Shape(Translated(pi+pdir*0.5, Text(str(cdart),fontstyle=font)), matlabel, id=cdart)) for i in xrange(0,3): oppidart = self.alpha( i,cdart) if oppidart != cdart: if i == 0 : alphaidartpos = pi+pdir* dartlengthratio else: alphaidartpos = pi+pdir*(0.1*i) if alphaipos[i].has_key(oppidart): s.add(Shape(Polyline([alphaipos[i][oppidart],alphaidartpos],width=5) , matalphai[i])) else: alphaipos[i][cdart] = alphaidartpos def process_face(dart, cellcenter = None, cshift = [0.,0.,0.]): try: facecenter = self.cell_center(fid,2) except: facecenter = None fshift = cshift if not cellcenter is None and not facecenter is None: fshift = (cellcenter-facecenter)*th for dart in self.incident_cells(fid,2,1): process_edge(dart, facecenter, fshift) for fid in self.elements(2): process_face(fid) s.add(Shape(PointSet(vertex, width=5) ,Material((0,0,0)) )) return s
def save(self, obj, filename): """ Store obj into filename """ filename = Path(filename) try: from openalea.plantgl.all import Scene sc = Scene() sc.add(obj) return sc.save(str(filename), "BGEOM") except ImportError: warnings.warn("You must install PlantGL if you want to load a BGEOM object.") except Exception, e: print e warnings.warn("Impossible to save the scene for object %s into %s" % (obj, filename))
def scene(self, view="axis", scene=None): """ Compute a scene from a tree squeleton. view='axis', 'node', 'polyline' or 'point' view specify if we compute a shape by axis, a shape by node or just a polyline. """ if not scene: scene = Scene() server = self.server max_order = server.max_order for order in range(max_order): self.axes(order, view) if server.has_leaf(): if view not in ['point']: self.leaves() else: self.axes(max_order, view) map(lambda x: scene.add(x), self._shapes) return scene
def display(self, color = [205,205,205], add = False): from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Viewer from random import randint s = Scene() for facedart in self.elements(2): lastdart = facedart positions = [] for dart in self.orderedorbit(facedart,[0,1]): if self.alpha(0, dart) != lastdart: positions.append(self.get_position(dart)) lastdart = dart if color is None: mat = Material((randint(0,255),randint(0,255),randint(0,255))) else: mat = Material(tuple(color),diffuse=0.25) s.add(Shape(FaceSet(positions, [range(len(positions))]) , mat )) if add : Viewer.add(s) else : Viewer.display(s)
def save(self, obj, filename): """ Store obj into filename """ filename = Path(filename) try: from openalea.plantgl.all import Scene sc = Scene() sc.add(obj) return sc.save(str(filename), "BGEOM") except ImportError: warnings.warn( "You must install PlantGL if you want to load a BGEOM object.") except Exception, e: print e warnings.warn( "Impossible to save the scene for object %s into %s" % (obj, filename))
def plot_spline_crv(ctrls, pts): """ Parameters ========== - ctrl: control points - pts : evaluated points on the curve """ from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Viewer, Polyline from random import randint scene = Scene() crv = Shape(geometry=Polyline(pts), appearance=Material((12, 12, 125))) scene.add(crv) ctrl = Shape(geometry=Polyline(ctrls), appearance=Material((12, 125, 12))) scene.add(ctrl) # To complete: Draw the control points and the line between each ones. Viewer.display(scene)
def view(patch): import itertools patchView = Scene() opoints = patch.points for i0,i1 in [(0,1),(1,2),(0,2)]: for index in itertools.product(*[list(range(d)) for d in [opoints.shape[i0],opoints.shape[i1]]]): i = [slice(None,None),slice(None,None),slice(None,None)] i[i0] = index[0] i[i1] = index[1] points = opoints[tuple(i)] pid = ((i[2]*10000 if not isinstance(i[2],slice) else 0)+ (i[1]*100 if not isinstance(i[1],slice) else 0)+ (i[0] if not isinstance(i[0],slice) else 0)) patchView.add(Polyline([p.project() for p in points])) return patchView
def display(self, degree = None, add = False, randomcolor = True): from openalea.plantgl.all import Scene, Shape, Material, FaceSet, Polyline, PointSet, Viewer from random import randint s = Scene() m = Material() if degree is None: degree = self.degree if degree >= 2: try: ccw = self.orientation() except ValueError: ccw = set() for fid in self.iterate_over_each_i_cell(2): if fid in ccw: ofid = self.alpha(0,fid) else: ofid = fid positions = [] for dart, deg in self.orderedorbit_iter(ofid,[0,1]): if deg != 1: positions.append(self.get_position(dart)) s.add(Shape(FaceSet(positions, [range(len(positions))]) , Material((randint(0,255),randint(0,255),randint(0,255))) if randomcolor else m)) elif degree == 1: for eid in self.iterate_over_each_i_cell(1): s.add(Shape(Polyline([self.get_position(eid),self.get_position(self.alpha(0,eid))]) ,Material((randint(0,255),randint(0,255),randint(0,255))) )) elif degree == 0: s.add(Shape(PointSet([self.get_position(pid) for pid in self.iterate_over_each_i_cell(0)]) ,Material((randint(0,255),randint(0,255),randint(0,255))) )) if add : Viewer.add(s) else : Viewer.display(s)
def plot_spline_surface(ctrl_net, points): """ Parameters ========== - ctrl_net : the net of control points (list of list) - points : a set of evaluated points (list of list) """ scene = Scene() n = len(points) m = len(points[0]) # Compute a mesh (i.e. TriangleSet) for the set of points pointList = [pt for rank in points for pt in rank] indexList = [] for i in range(n - 1): for j in range(m - 1): ii = i * m + j i1 = (ii, ii + 1, ii + m) i2 = (ii + 1, ii + m + 1, ii + m) indexList.append(i1) indexList.append(i2) surf = Shape(TriangleSet(pointList, indexList), appearance=Material((12, 125, 12))) scene.add(surf) # plot the control net n = len(ctrl_net) m = len(ctrl_net[0]) for pts in ctrl_net: crv = Shape(geometry=Polyline(pts), appearance=Material((125, 12, 12))) scene.add(crv) for pt in pts: scene.add(Shape(Translated(Vector3(pt), Sphere(radius=0.1)))) for i in range(m): pts = [ctrl_net[j][i] for j in range(n)] crv = Shape(geometry=Polyline(pts), appearance=Material((12, 12, 125))) scene.add(crv) Viewer.display(scene)