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 build_geometry(elements): plants = {} soil = {} indexes = [] print('number of elements', len(elements)) for i, (label, triangle) in enumerate(elements): pid = plant_id(label) if pid not in plants: plants[pid] = {"leaves": {}, "stems": pgl.TriangleSet([], [])} plant = plants[pid] if is_leaf(label): lid = leaf_id(label) leaves = plant['leaves'] if lid not in leaves: leaves[lid] = pgl.TriangleSet([], []) shape = leaves[lid] elif is_stem(label): shape = plant['stems'] else: assert is_soil(label) if "soil" not in soil: soil["soil"] = pgl.TriangleSet([], []) shape = soil["soil"] count = len(shape.pointList) shape.pointList.append(triangle[0]) shape.pointList.append(triangle[1]) shape.pointList.append(triangle[2]) shape.indexList.append(pgl.Index3(count, count + 1, count + 2)) indexes.append((label, count)) return CanestraScene(plants, soil, indexes)
def tgl(): return pgl.TriangleSet( pgl.Point3Array([ pgl.Vector3(1, 0, 0), pgl.Vector3(-0.2, 1, 0), pgl.Vector3(-0.2, -1, 0) ]), pgl.Index3Array([pgl.Index3(0, 1, 2)]))
def _common_init(self, **keys): """ """ if len(keys["points"]) != 3: raise Exception( "ATriangle: triangle must be described by 3 points..") self._indexes = [] self._points = pgl.Point3Array(keys["points"]) for i in xrange(1, len(keys["points"])): self._indexes.append(pgl.Index3(0, 1, 2)) self._indexes = pgl.Index3Array(self._indexes)
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