def mesh_area(mesh):
    if mesh:
        sc = pgl.SurfComputer(pgl.Discretizer())
        mesh.apply(sc)
        return sc.surface
    else:
        return None
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def scene_transform(self):
        """ Transform scene for RATP input
        
            return entity, x,y,z,surface, nitrogen, sh_id lists
        """

        def _surf(mesh, iface):
            A,B,C = [mesh.pointList[i] for i in mesh.indexAt(iface)]
            return pgl.norm(pgl.cross(B-A, C-A)) / 2.0

        def _normal(mesh, iface):
            A,B,C = [mesh.pointList[i] for i in mesh.indexAt(iface)]
            n = pgl.cross(B-A, C-A)
            return n.normed()

        def _process(shape, discretizer):
            discretizer.process(shape)
            mesh = discretizer.result
            ifaces = range(mesh.indexListSize())
            s = [_surf(mesh,i) * self.convert**2 for i in ifaces]
            a = float(sum(s)) / self.convert**2
            phi = [pgl.Vector3.Spherical(_normal(mesh,i)).phi for i in ifaces]
            sh_id  = [shape.id] * len(s)
            n = [self.nitrogen[shape.id]] * len(s)
            entity = [self.entity[shape.id] - 1] * len(s) # entity 1 is encoded 0 in fill grid
            scale_area = [float(self.area.get(shape.id, a)) / a] * len(s)
            centers = [mesh.faceCenter(i) for i in ifaces]
            x, y, z = zip(*map(lambda x: (x[0] * self.convert, x[1] * self.convert, x[2] * self.convert), centers))
            s = (numpy.array(s) * numpy.array(scale_area)).tolist()

            return entity, x, y, z, s, n, sh_id, phi
          
        d = pgl.Discretizer()
        transform = map(lambda x: _process(x,d), self.scene)
        return map(lambda what: reduce(lambda x,y : x+y, what), zip(*transform))
Ejemplo n.º 4
0
def pointsByShape(scene): #aka ptdiscretize
  """renvoie un dictionnaire Shape_id:[liste de points]"""
  d = pgl.Discretizer()
  pbs = {}
  for i in scene:
    if i.apply(d) == True:
      pts = d.getDiscretization().pointList #yield a pgl.Point3Array
      if pts:
          pbs[i.id] = pts
  return pbs
Ejemplo n.º 5
0
def as_scene_mesh(pgl_scene):
    """ Transform a PlantGL scene / PlantGL shape dict to a scene_mesh"""
    discretizer = pgl.Discretizer()

    if isinstance(pgl_scene, pgl.Scene):
        return {sh.id: shape_mesh(sh, discretizer) for sh in pgl_scene}
    elif isinstance(pgl_scene, dict):
        return {
            sh_id: shape_mesh(sh, discretizer)
            for sh_id, sh in pgl_scene.iteritems()
        }
    else:
        return pgl_scene
Ejemplo n.º 6
0
def leaf_shape(leaf, nb_triangles, length_max, length, radius_max):
    if length <= 0:
        return
    x, y, s, r = leaf
    leaf_new, leaf_surface = fit2(x, y, s, r)

    pts, ind = mesh2(leaf_new, length_max, length, radius_max)
    if len(pts) <= 1.5 * nb_triangles:
        pts2, ind2 = pts, ind
    else:
        pts2, ind2 = qslim(nb_triangles, pts, ind)
    # pts2, ind2 = pts, ind
    mesh = plantgl_shape(pts2, ind2)

    sc = pgl.SurfComputer(pgl.Discretizer())
    mesh.apply(sc)
    scale_radius = leaf_surface * length_max * radius_max / (sc.surface)
    mesh_final = mesh.transform(pgl.Scaling((1, scale_radius, 1)))
    mesh_final = mesh
    return mesh_final
Ejemplo n.º 7
0
def shape_mesh(pgl_shape, discretiser=None):
    if discretiser is None:
        discretiser = pgl.Discretizer()
    discretiser.process(pgl_shape)
    tset = discretiser.result
    return numpy.array(tset.pointList), numpy.array(tset.indexList)