def build_scene_with_colors(self, colors): scene = pgl.Scene() n = len(colors) for i, (label, count) in enumerate(self.indexes): pid = plant_id(label) plant = self.plants[pid] if is_leaf(label): lid = leaf_id(label) geom = plant["leaves"][lid] elif is_stem(label): geom = plant["stems"] else: geom = self.soil["soil"] if count == 0: geom.colorList = [] geom.colorPerVertex = False assert 3 * len(geom.colorList) == count if i >= n or type(colors[i]) == float: geom.colorList.append(pgl.Color4(10, 10, 10, 0)) else: r, g, b = colors[i] geom.colorList.append(pgl.Color4(r, g, b, 0)) for plant in list(self.plants.values()): leaves = plant["leaves"] stems = plant["stems"] for leaf in list(leaves.values()): scene += leaf if len(stems.pointList) > 0: scene += stems if "soil" in self.soil: scene += self.soil["soil"] self.scene = scene
def plot(self, dfvox, dfmap, minval=None, maxval=None): output = pandas.merge(dfmap, dfvox) output = output.sort_values( 'primitive_index' ) # sort is needed to ensure matching with triangulation indices par = output['PAR'] if minval is None: minval = min(par) if maxval is None: maxval = max(par) cmap = ColorMap() scene = pgl.Scene() discretizer = pgl.Discretizer() for sh in self.scene: discretizer.process(sh) mesh = discretizer.result mesh.colorList = [] mesh.colorPerVertex = False colors = map(lambda x: cmap(x, minval, maxval, 250., 20.), par[output['shape_id'] == sh.id]) for color in colors: r, g, b = color mesh.colorList.append(pgl.Color4(r, g, b, 0)) scene.add(mesh) pgl.Viewer.display(scene) return scene
def to_geom_args(self, conversion=1.0, name=None, _as_obj=False): # pragma: lpy r"""Get arguments for creating a PlantGL geometry. Args: conversion (float, optional): Conversion factor that should be applied to the vertices. Defaults to 1.0. name (str, optional): Name that should be given to the created PlantGL symbol. Defaults to None and is ignored. Returns: tuple: Class, arguments and keyword arguments for PlantGL geometry. """ import openalea.plantgl.all as pgl kwargs = dict() # Add vertices obj_points = [] obj_colors = [] for v in self['vertices']: xarr = conversion * np.array([v[k] for k in ['x', 'y', 'z']]) obj_points.append( pgl.Vector3(np.float64(xarr[0]), np.float64(xarr[1]), np.float64(xarr[2]))) c = [v.get(k, None) for k in ['red', 'green', 'blue']] if None not in c: cast_type = int obj_colors.append( pgl.Color4(cast_type(c[0]), cast_type(c[1]), cast_type(c[2]), cast_type(1))) points = pgl.Point3Array(obj_points) if obj_colors: colors = pgl.Color4Array(obj_colors) kwargs['colorList'] = colors kwargs['colorPerVertex'] = True # Add indices obj_indices = [] index_class = pgl.Index array_class = pgl.IndexArray smb_class = pgl.FaceSet # index_class = pgl.Index3 # array_class = pgl.Index3Array # smb_class = pgl.TriangleSet for f in self['faces']: if _as_obj: f_int = [int(_f['vertex_index']) for _f in f] else: f_int = [int(_f) for _f in f['vertex_index']] obj_indices.append(index_class(*f_int)) indices = array_class(obj_indices) args = (points, indices) return smb_class, args, kwargs
def from_scene_mesh(scene_mesh, colors=None): plant_color = (0, 180, 0) if colors is None: colors = {k: plant_color for k in scene_mesh} scene = pgl.Scene() for sh_id, mesh in scene_mesh.iteritems(): vertices, faces = mesh if isinstance(colors[sh_id], tuple): r, g, b = colors[sh_id] color_list = [pgl.Color4(r, g, b, 0)] * len(faces) else: color_list = [pgl.Color4(r, g, b, 0) for r, g, b in colors[sh_id]] shape = pgl.TriangleSet(vertices, faces) shape.colorList = color_list shape.colorPerVertex = False shape.id = sh_id scene += shape return scene
def to_geom_args(self, conversion=1.0, name=None): r"""Get arguments for creating a PlantGL geometry. Args: conversion (float, optional): Conversion factor that should be applied to the vertices. Defaults to 1.0. name (str, optional): Name that should be given to the created PlantGL symbol. Defaults to None and is ignored. Returns: tuple: Class, arguments and keyword arguments for PlantGL geometry. """ import openalea.plantgl.all as pgl kwargs = dict() # Add vertices obj_points = [] for v in self['vertices']: xarr = conversion * np.array(v) obj_points.append(pgl.Vector3(xarr[0], xarr[1], xarr[2])) points = pgl.Point3Array(obj_points) # Add indices obj_indices = [] nind = None index_class = pgl.Index3 array_class = pgl.Index3Array smb_class = pgl.TriangleSet for f in self['faces']: if nind is None: nind = len(f) if nind == 3: pass else: raise ValueError( "No PlantGL class for faces with %d vertices." % nind) else: if len(f) != nind: raise ValueError("Faces do not all contain %d vertices." % nind) f_int = [int(_f) for _f in f] obj_indices.append(index_class(*f_int)) indices = array_class(obj_indices) # Add colors if self['vertex_colors']: obj_colors = [] for c in self['vertex_colors']: assert (len(c) == 3) obj_colors.append(pgl.Color4(c[0], c[1], c[2], 1)) colors = pgl.Color4Array(obj_colors) kwargs['colorList'] = colors kwargs['colorPerVertex'] = True args = (points, indices) return smb_class, args, kwargs
def generate_scene(triangle_scene, colors=None, soil=None, soil_colors=None): """ Build a colored PlantGL scene Args: triangle_scene: (dict of list of list of tuples) a {primitive_id: [triangles, ]} dict, each triangle being defined by an ordered triplet of 3-tuple points coordinates. colors: (dict of list of tuples) : a {primitive_id: [colors,]} dict defining colors of primitives in the scene. A color is a (r, g, b) tuple. soil: (list of triangles) : a list of triangles of the soil soil_colors : a list of (r, g, b) tuples defining the colors of the soil triangles Returns: A plantGL scene of colored shapes """ plant_color = (0, 180, 0) soil_color = (170, 85, 0) missing_color = (0, 0, 0) scene = pgl.Scene() if colors is None: colors = { k: [plant_color] * len(triangle_scene[k]) for k in triangle_scene } else: colors = { k: colors.get(k, [missing_color] * len(triangle_scene[k])) for k in triangle_scene } for k, triangles in triangle_scene.items(): shape = pgl.TriangleSet([], []) shape.colorList = [] shape.colorPerVertex = False shape.id = k for i, triangle in enumerate(triangles): shape.pointList.append(pgl.Vector3(triangle[0])) shape.pointList.append(pgl.Vector3(triangle[1])) shape.pointList.append(pgl.Vector3(triangle[2])) shape.indexList.append(pgl.Index3(3 * i, 3 * i + 1, 3 * i + 2)) r, g, b = colors[k][i] shape.colorList.append(pgl.Color4(r, g, b, 0)) scene += shape if soil is not None: if soil_colors is None: soil_colors = [soil_color] * len(soil) sid = max([sh.id for sh in scene]) shape = pgl.TriangleSet([], []) shape.colorList = [] shape.colorPerVertex = False shape.id = sid for i, triangle in enumerate(soil): shape.pointList.append(pgl.Vector3(triangle[0])) shape.pointList.append(pgl.Vector3(triangle[1])) shape.pointList.append(pgl.Vector3(triangle[2])) shape.indexList.append(pgl.Index3(3 * i, 3 * i + 1, 3 * i + 2)) r, g, b = soil_colors[i] shape.colorList.append(pgl.Color4(r, g, b, 0)) scene += shape return scene
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))