def build_scene(self, leaf_material=None, stem_material=None, soil_material=None): if not leaf_material: leaf_material = pgl.Material(pgl.Color3(0, 180, 0)) if not stem_material: stem_material = pgl.Material(pgl.Color3(0, 130, 0)) if not soil_material: soil_material = pgl.Material(pgl.Color3(170, 85, 0)) scene = pgl.Scene() for id, plant in self.plants.items(): leaves = plant["leaves"] stems = plant["stems"] for lid, leaf in leaves.items(): shape = pgl.Shape(leaf, leaf_material) shape.name = str(lid) scene.add(shape) if len(stems.pointList) > 0: shape = pgl.Shape(stems, stem_material) shape.name = str(id) scene.add(shape) if "soil" in self.soil: shape = pgl.Shape(self.soil["soil"], soil_material) scene.add(shape) self.scene = scene
def stand_box(domain): ''' domain: 3D bounding box of the stand ''' # list of points z_base = domain[0][2] z_top = domain[1][2] sides_points = [ domain[0], # coordinates of bottom right corner (domain[1][0], domain[0][1], z_base), (domain[1][0], domain[1][1], z_base), # coordinates of bottom left corner (domain[0][0], domain[1][1], z_base), (domain[0][0], domain[0][1], z_top), # coordinates of top right corner (domain[1][0], domain[0][1], z_top), (domain[1][0], domain[1][1], z_top), # coordinates of top left corner (domain[0][0], domain[1][1], z_top) ] bottom_points = [ domain[0], # coordinates of bottom right corner (domain[1][0], domain[0][1], z_base), (domain[1][0], domain[1][1], z_base), # coordinates of bottom left corner (domain[0][0], domain[1][1], z_base) ] # list of indices to make the quads of the sides from the points side_indices = [ (0, 1, 5, 4), # (1, 2, 6, 5), # indices for (2, 3, 7, 6), # side faces (3, 0, 4, 7) ] # # list of indices to make the quads of the bottom from the points bottom_indices = [(0, 1, 2, 3)] # indices for bottom face # list of colors side_color = pgl.Color3(0, 0, 0) bottom_color = pgl.Color3(255, 255, 255) # construction of the geometry for the sides side_box = pgl.QuadSet(sides_points, side_indices) # construction of the geometry for the bottom bottom_box = pgl.QuadSet(bottom_points, bottom_indices) # create 2 shapes: 1 with side_color, 1 with bottom_color sides_shape = pgl.Shape(side_box, pgl.Material(side_color)) bottom_shape = pgl.Shape(bottom_box, pgl.Material(bottom_color)) scene = pgl.Scene() scene.add(sides_shape) scene.add(bottom_shape) return scene
def color( r, g, b , trans=False, name='material', diffu = 1): c=pgl.Color3( r,g,b ) diffuse=diffu specular=pgl.Color3(40,40,40) emission=pgl.Color3(0,0,0) shininess=0 if trans: transparency=0.5 else: transparency=0 return pgl.Material( name, c, diffuse, specular, emission, shininess, transparency )
def color(self, elements, **kwds): rgb = elements[0] assert rgb.tag == 'rgb' color = pgl.Color3(*(int(float(x) * 255) for x in rgb.text.strip().split())) self._current_turtle.color = color return color
def mesh2shapes(scene): """ Convert all the meshes containing colors into independent shapes. """ if isinstance(scene, pgl.Scene): scene = scene else: # Case caribu scene scene = scene.scene new_scene = pgl.Scene() for shape in scene: g = shape.geometry if isinstance(g, pgl.TriangleSet) and len(g.colorList) > 0: # convert the mesh into shapes with different colors pts = g.pointList indx = g.indexList colors = g.colorList for i, ind in enumerate(indx): new_geom = pgl.TriangleSet([], []) new_geom.indexList.append(pgl.Index3(0, 1, 2)) for k in ind: new_geom.pointList.append(pts[k]) _shape = pgl.Shape(new_geom, pgl.Material(pgl.Color3(colors[i]))) new_scene.add(_shape) else: new_scene.add(shape) return new_scene,
def display(vertices, faces, color=None, view=True): """3D display of a polyhedron with PlantGL Args: vertices (list of tuples): list of 3D coordinates of polyhedron vertices faces (list of tuple): list of vertex indices defining the faces color: a (r,g,b) tuple defining color. If None (default), default PlantGL material is used. view (bool): should the shape be displayed ? Returns: a pgl shape """ global display_enable if display_enable: if color is None: shape = pgl.Shape(pgl.FaceSet(pointList=vertices, indexList=faces)) else: m = pgl.Material(pgl.Color3(*color)) shape = pgl.Shape(pgl.FaceSet(pointList=vertices, indexList=faces), m) if view: pgl.Viewer.display(shape) else: warnings.warn('PlantGL not installed: display is not enable!') shape = None return shape
def display_leaves(g): green = pgl.Material(pgl.Color3(0, 150, 0)) scene = pgl.Scene() for vid, geom in g.property('geometry').items(): sh = pgl.Shape(geom, green) sh.id = vid scene.add(sh) pgl.Viewer.display(scene) return scene
def simple_tree(height=2, crown_radius=1, n_leaves=500, leaf_area=0.1, inner=True): """return a simple tree scene""" leaves = random_leaves(n_leaves=n_leaves, leaf_area=leaf_area, crown_radius=crown_radius, crown_center=(0, 0, height), inner=inner) trunk = pgl.Cylinder(0.1, height) green = pgl.Material(pgl.Color3(0, 150, 0)) brown = pgl.Material(pgl.Color3(100, 60, 10)) scene = pgl.Scene() scene.add(pgl.Shape(trunk, brown)) for leaf in leaves: scene.add(pgl.Shape(leaf, green)) return scene
def add_sun(scene, sun, distance=5, radius=0.1): elevation, azimuth, luminance = sun colors = jet_colors(luminance) pgl_colors = [pgl.Material(pgl.Color3(r, g, b)) for r, g, b in colors] sph = pgl.Sphere(radius) #red = pgl.Material(pgl.Color3(150, 0, 0)) pos = cartesian(elevation, azimuth, distance) pgl_pos = [pgl.Vector3(*p) for p in pos] for i, vect in enumerate(pgl_pos): shape = pgl.Translated(vect, sph) scene.add(pgl.Shape(shape, pgl_colors[i])) return scene
def frame(matrix, scene, color=1): """ Add a frame to the scene. The frame is represented by the matrix. :param color: allow to distinguish between to identical frames. """ if color == 1: r = pgl.Material(pgl.Color3(*(255, 0, 0))) g = pgl.Material(pgl.Color3(*(0, 255, 0))) b = pgl.Material(pgl.Color3(*(0, 0, 255))) else: r = pgl.Material(pgl.Color3(*(255, 0, 255))) g = pgl.Material(pgl.Color3(*(255, 255, 0))) b = pgl.Material(pgl.Color3(*(0, 0, 0))) cylinder = pgl.Cylinder(radius=0.005, height=1) #x = pgl.AxisRotated(Vector3(0,1,0), radians(90), cylinder) #y = pgl.AxisRotated(Vector3(1,0,0), radians(-90), cylinder) z = cylinder #geom_x = transform4(matrix, x) #scene.add(pgl.Shape(geom_x, r)) #geom_y = transform4(matrix, y) #scene.add(pgl.Shape(geom_y, g)) geom_z = transform4(matrix, z) scene.add(pgl.Shape(geom_z, b))
def show_sources(sun, north=0, scene=None, distance=5, radius=0.1): sc = pgl.Scene() if scene is not None: sc.add(scene) elevation, azimuth, luminance = [numpy.array(x) for x in sun] az = -(azimuth + north) colors = jet_colors(luminance) pgl_colors = [pgl.Material(pgl.Color3(r, g, b)) for r, g, b in colors] sph = pgl.Sphere(radius) pos = cartesian(elevation, az, distance) pgl_pos = [pgl.Vector3(*p) for p in pos] for i, vect in enumerate(pgl_pos): shape = pgl.Translated(vect, sph) sc.add(pgl.Shape(shape, pgl_colors[i])) pgl.Viewer.display(sc) return sc
def add_sky(scene, sky, distance=4, radius=0.05): sph = pgl.Sphere(radius) #blue = pgl.Material(pgl.Color3(0, 0, 150)) elevation, azimuth, luminance = sky colors = jet_colors(luminance) pgl_colors = [pgl.Material(pgl.Color3(r, g, b)) for r, g, b in colors] # elevations46 = [9.23] * 10 + [10.81] * 5 + [26.57] * 5 + [31.08] * 10 + [ # 47.41] * 5 + [52.62] * 5 + [69.16] * 5 + [90] # azimuths46 = [12.23, 59.77, 84.23, 131.77, 156.23, 203.77, 228.23, 275.77, # 300.23, 347.77, 36, 108, 180, 252, 324, 0, 72, 144, 216, 288, # 23.27, 48.73, 95.27, 120.73, 167.27, 192.73, 239.27, 264.73, # 311.27, 336.73, 0, 72, 144, 216, 288, 36, 108, 180, 252, 324, # 0, 72, 144, 216, 288, 180] pos = cartesian(numpy.radians(elevation), numpy.radians(azimuth), distance) pgl_pos = [pgl.Vector3(*p) for p in pos] for i, vect in enumerate(pgl_pos): shape = pgl.Translated(vect, sph) scene.add(pgl.Shape(shape, pgl_colors[i])) return scene
def rgb_color(index): """ Returns an RGB color from an index. """ LUT_color = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170), (170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170), (85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255), (255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255), ] color = pgl.Color3(LUT_color[int(index)]) return color
#!/usr/bin/env python """Contains basic material definitions. :version: pon mar 19 16:32:21 CET 2007 :author: szymon stoma """ __docformat__ = "restructuredtext en" import openalea.plantgl.all as pgl white = pgl.Material(ambient=pgl.Color3(255, 255, 255), name="White", diffuse=1, specular=pgl.Color3(40, 40, 40), emission=pgl.Color3(0, 0, 0), shininess=1, transparency=0) red = pgl.Material(ambient=pgl.Color3(255, 0, 0), name="Red", diffuse=1, specular=pgl.Color3(40, 40, 40), emission=pgl.Color3(0, 0, 0), shininess=1, transparency=0) green = pgl.Material(ambient=pgl.Color3(0, 255, 0), name="Green", diffuse=1, specular=pgl.Color3(40, 40, 40), emission=pgl.Color3(0, 0, 0),
import openalea.plantgl.all as pgl from PyQt4.QtCore import * from PyQt4.QtGui import * from nose import with_setup import warnings if not QCoreApplication.instance() is None: warnings.warn("A QApplication is already running") else: app = QApplication([]) pgl.Viewer.start() red = pgl.Material(ambient=pgl.Color3(60, 10, 30), diffuse=3) green = pgl.Material(ambient=pgl.Color3(30, 60, 10), diffuse=3) blue = pgl.Material(ambient=pgl.Color3(10, 30, 60), diffuse=3) def setup_func(): sc = pgl.Scene() sc += pgl.Shape(pgl.Box(1, 1, 1), red, id=1) sc += pgl.Shape(pgl.Translated(4, 4, 6, pgl.Sphere(1)), blue, id=2) sc += pgl.Shape(pgl.Translated(2, 3, -3, pgl.Cone(1, 2)), green, id=3) sc += pgl.Shape(pgl.Translated(-8, 3, -2, pgl.Box(4, 2, 0.5)), blue, id=4) sc += pgl.Shape(pgl.Translated( -4, -2, 5, pgl.EulerRotated(3.14, 2.7, 0, pgl.Cone(2, 5))), green, id=5) sc += pgl.Shape(pgl.Translated(4, -3, -3, pgl.Sphere(2)), red, id=6) return sc
def stand_material(r=20, g=100, b=60, spec_name="houppier_mat"): return stand_mat.setdefault( spec_name, pgl.Material(spec_name, pgl.Color3(r, g, b), 0.5))
- plantgl - random - math - pgl_utils Module for generating stands of tree according to mesure. """ __docformat__ = "restructuredtext en" import openalea.plantgl.all as pgl from openalea.plantgl.ext.pgl_utils import sphere, arrow, color, createSwung from openalea.core import * houppier_mat = pgl.Material("houppier_mat", pgl.Color3(20, 100, 60), 0.5) trunk_mat = pgl.Material("trunk_mat", pgl.Color3(50, 28, 6), 2) stand_mat = {"houppier_mat": houppier_mat, "trunk_mat": trunk_mat} def stand_material(r=20, g=100, b=60, spec_name="houppier_mat"): return stand_mat.setdefault( spec_name, pgl.Material(spec_name, pgl.Color3(r, g, b), 0.5)) from math import pi, cos, sin, radians def makeScene(trees, midCr=0.5, wood=True, random=False): #treeList = treesFromFile(file)
def visu(g, plot_prop=None, min_value=None, max_value=None, cmap='jet', fmt='%6.0f', elmnt_labels=None, elmnt_color_dict=None, def_elmnt_color_dict=False, use_mtg_color=False, snap_shot_path=None, scene=None, view_result=True): """Displays 3D moke-up using `plantgl` package. Args: g (openalea.mtg.mtg.MTG): an MTG object plot_prop (str): name of an MTG property to plot min_value (float): minimum value for the color scale max_value (float): maximum value for the color scale cmap (str): the colormap to use; only active when `plot_prop` is given or `use_mtg_color` is True (default='jet) fmt (str): format of matplotlib colorbar ticklabels (default='%6.0f') elmnt_labels (list): the desired MTG elemnts to be displayed elmnt_color_dict (dict): RGB color tuples for the given MTG labels def_elmnt_color_dict (bool): if True, uses DEFAULT_COLORS` to set a dictionary of RGB color tuples with `elmnt_labels` as keys (default=False) use_mtg_color (bool): if True, the 'color' property from MTG nodes is used (default=False) snap_shot_path (str or None): path to save scene snapshot (default=None) scene (Scene): if given, adds scene shapes to it (default=None) view_result (bool): if True, the scene is displayed (default=False) Returns: (Scene) """ my_scene = pgl.Scene() if scene is None else scene if plot_prop is not None: prop = [ g.node(vid).properties()[plot_prop] for vid in g.property(plot_prop) if not g.node(vid).label.startswith('soil') ] if min_value is None: min_value = min(prop) if max_value is None: max_value = max(prop) g, cb = pglcolor.colorbar(g, property_name=plot_prop, cmap=cmap, lognorm=False, N=6, fmt=fmt, min_value=min_value, max_value=max_value) # cb.patch.set_facecolor((0.2, 0.2, 0.2, 1.0)) g = pglcolor.colormap(g, property_name=plot_prop, cmap=cmap, lognorm=False, min_value=min_value, max_value=max_value) for vid in g.property(plot_prop).keys(): try: n = g.node(vid) mesh = n.geometry scene_shape = pgl.Shape(mesh, pgl.Material(pgl.Color3(n.color))) my_scene.add(scene_shape) except: pass else: if elmnt_labels is None: elmnt_labels = DEFAULT_ELEMENTS if elmnt_color_dict is None: if def_elmnt_color_dict: def_color_dict = DEFAULT_COLORS elmnt_color_dict = { key_: def_color_dict[key_] for key_ in elmnt_labels } elif not use_mtg_color: raise Exception( "Element colors are missing. You may set elmnt_color_dict=True to use default colors" "provied by DEFAULT_COLORS.") for vid in g.property('geometry'): n = g.node(vid) if n.label.startswith(tuple(elmnt_labels)): mesh = n.geometry label = n.label if use_mtg_color: color = n.color else: for i in tuple(elmnt_labels): if i in label: color = elmnt_color_dict[i] # color=(50,50,50) if vid != tt else (255,255,0) scene_shape = pgl.Shape(mesh, pgl.Material(pgl.Color3(color))) my_scene.add(scene_shape) if view_result: pgl.Viewer.display(my_scene) if snap_shot_path: pgl.Viewer.saveSnapshot(snap_shot_path) return my_scene
def visu(g, plot_prop=None, min_value=None, max_value=None, tt=1001, cmap='jet', fmt='%6.0f', elmnt_labels=None, elmnt_color_dict=None, def_elmnt_color_dict=False, use_mtg_color=False, snap_shot_path=None, scene=None, view_result=True): """ Displays 3D moke-up using `plantgl` package. :Parameters: - **g**: an MTG object - **plot_prop**: string, an MTG property to plot - **min_value**: float, minimum property value for the color scale - **max_value**: float, maximum property value for the color scale - **cmap**: string, def('jet'), the colormap to use; only active when `plot_prop` is given or `use_mtg_color` is True - **fmt**: string, a legal text format, def('%6.0f'), the string format of matplotlib colorbar - **elmnt_labels**: a list of strings refering to the desired mtg elemnts to be displayed - **elmnt_colors_dict**: a dictionary of RGB color tuples for given mtg labels - **def_elmnt_color_dict**: logical, if `True`, uses :func:`default_color_dict()` to set a dictionary of RGB color tuples with `elmnt_labels` as keys - **use_mtg_color**: logical, if `True`, . If `False` uses the 'color' property from mtg nodes - **snap_shot_path**: string, if given, saves scene snapshot to the defined path and file name - **scene**: def None,if given, adds scene shapes to it """ MyScene = pgl.Scene() if scene == None else scene if plot_prop is not None: prop = [g.node(vid).properties()[plot_prop] for vid in g.property(plot_prop) \ if not g.node(vid).label.startswith('soil')] if min_value is None: min_value = min(prop) if max_value is None: max_value = max(prop) g, cb = pglcolor.colorbar(g, property_name=plot_prop, cmap=cmap, lognorm=False, N=6, fmt=fmt, min_value=min_value, max_value=max_value) # cb.patch.set_facecolor((0.2, 0.2, 0.2, 1.0)) g = pglcolor.colormap(g, property_name=plot_prop, cmap=cmap,\ lognorm=False,min_value=min_value,\ max_value=max_value) for vid in g.property(plot_prop).keys(): try: n = g.node(vid) mesh = n.geometry Scene_shape = pgl.Shape(mesh, pgl.Material(pgl.Color3(n.color))) MyScene.add(Scene_shape) except: pass else: if elmnt_labels is None: elmnt_labels = default_element_list() if elmnt_color_dict is None: if def_elmnt_color_dict: def_color_dict = default_color_dict() elmnt_color_dict = { key_: def_color_dict[key_] for key_ in elmnt_labels } elif use_mtg_color == False: raise StandardError( "Elements colors are missing. You may set elmnt_color_dict=True to use default colors provied by default_color_dict()." ) for vid in g.property('geometry'): n = g.node(vid) if n.label.startswith(tuple(elmnt_labels)): mesh = n.geometry label = n.label if use_mtg_color: color = n.color else: for i in tuple(elmnt_labels): if i in label: color = elmnt_color_dict[i] # color=(50,50,50) if vid != tt else (255,255,0) Scene_shape = pgl.Shape(mesh, pgl.Material(pgl.Color3(color))) MyScene.add(Scene_shape) if view_result: pgl.Viewer.display(MyScene) if snap_shot_path: pgl.Viewer.saveSnapshot(snap_shot_path) return MyScene
def parse(cmds): import openalea.plantgl.all as pgl objmap = {'Black' : pgl.Color3.BLACK, 'White' : pgl.Color3.WHITE, 'Red' : pgl.Color3.RED, 'Green' : pgl.Color3.GREEN, 'Blue' : pgl.Color3.BLUE, 'Cyan' : pgl.Color3.CYAN, 'Magenta' : pgl.Color3(255,0,255), 'Yellow' : pgl.Color3.YELLOW } shapes = [] currentid = 0 while currentid < len(cmds): currentid, obj = parse_object(cmds, currentid, objmap) if type(obj) == pgl.Shape: shapes.append(obj) if len(shapes) > 0 : return pgl.Scene(shapes) else : return pgl.Scene([pgl.Shape(obj) for obj in objmap if isinstance(obj, pgl.Geometry)])
import PyQGLViewer as qgl import openalea.plantgl.all as pgl toVec = lambda v: qgl.Vec(v.x, v.y, v.z) toV3 = lambda v: pgl.Vector3(v.x, v.y, v.z) toC3 = lambda v: pgl.Color3(v.red(), v.green(), v.blue()) toC4 = lambda v: pgl.Color4(v.red(), v.green(), v.blue(), v.alpha()) toQC = lambda v: pgl.QColor(v.red, v.green, v.blue) bbx2qgl = lambda bbx: (toVec(bbx.lowerLeftCorner), toVec(bbx.upperRightCorner))