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 view_kdtree(kdtree, bbox=[[-1., 1.], [-1., 1.], [-1., 1.]], radius=0.05): import numpy as np scene = pgl.Scene() sphere = pgl.Sphere(radius, slices=16, stacks=16) silver = pgl.Material(ambient=(49, 49, 49), diffuse=3., specular=(129, 129, 129), shininess=0.4) gold = pgl.Material(ambient=(63, 50, 18), diffuse=3., specular=(160, 141, 93), shininess=0.4) if isinstance(kdtree, KDNode): dim = kdtree.axis plane_bbox = [b for i, b in enumerate(bbox) if i != dim] plane_points = [] plane_points += [ np.insert([plane_bbox[0][0], plane_bbox[1][0]], dim, kdtree.pivot[dim]) ] plane_points += [ np.insert([plane_bbox[0][0], plane_bbox[1][1]], dim, kdtree.pivot[dim]) ] plane_points += [ np.insert([plane_bbox[0][1], plane_bbox[1][1]], dim, kdtree.pivot[dim]) ] plane_points += [ np.insert([plane_bbox[0][1], plane_bbox[1][0]], dim, kdtree.pivot[dim]) ] left_bbox = np.copy(bbox).astype(float) right_bbox = np.copy(bbox).astype(float) left_bbox[dim, 1] = kdtree.pivot[dim] right_bbox[dim, 0] = kdtree.pivot[dim] scene += pgl.Shape(pgl.Translated(kdtree.pivot, sphere), gold) scene += view_kdtree(kdtree.left_child, bbox=left_bbox, radius=radius) scene += view_kdtree(kdtree.right_child, bbox=right_bbox, radius=radius) # scene += pgl.Shape(pgl.Polyline(plane_points+[plane_points[0]],width=2),pgl.Material(ambient=(0,0,0))) scene += pgl.Shape( pgl.Polyline(plane_points + [plane_points[0]], width=2), pgl.Material(ambient=tuple(list(np.insert([0, 0], dim, 255))))) scene += pgl.Shape(pgl.FaceSet(plane_points, [range(4)]), pgl.Material(ambient=(0, 0, 0), transparency=0.6)) else: assert (type(kdtree) == list) or (isinstance(kdtree, np.ndarray)) for p in kdtree: scene += pgl.Shape(pgl.Translated(p, sphere), silver) return scene
def test_class_interface(): scene = pgl.Scene() scene.add(pgl.Sphere(radius=0.1)) pov = PovRay() pov.render(scene) reader = cv2.imread im = pov.get_image(reader) return im
def plotSkyTurtle(): pgl.Viewer.start() sc = pgl.Scene() for i in range(len(elevations)): pos = pgl.Vector3(pgl.Vector3.Spherical(30,radians(azimuths[i]),radians(90-elevations[i]))) sc += pgl.Shape(pgl.Translated(pos,pgl.Sphere(0.5)),pgl.Material(),i+1) pgl.Viewer.display(sc) return sc
def test_scene(): ds = pgl.Sphere() t = pgl.Tapered(1., 1., ds) t2 = pgl.Translated(0., 0., 1., ds) sc = pgl.Scene([ pgl.Shape(t, pgl.Material((255, 0, 0))), pgl.Shape(t2, pgl.Material((0, 255, 0))) ]) jsonconversion(sc)
def test_scene(): s = pgl.Scene() cs = scene_to_cscene(s) assert cs == {} s.add(pgl.Sphere()) cs = scene_to_cscene(s) assert len(cs) == 1 css = cs.values()[0] assert len(css[0]) == 3 assert len(css[0][0]) == 3 s.add(pgl.Shape(pgl.Sphere())) cs = scene_to_cscene(s) assert len(cs) == 2 css = cs.values()[1] assert len(css[0]) == 3 assert len(css[0][0]) == 3 return cs
def test_bbox(): s = pgl.Scene() s.add(pgl.Sphere()) cs = CaribuScene(scene=s) bbox = cs.bbox() assert bbox == ((-0.5, -0.5, -0.5), (0.5, 0.5, 0.5)) points = [(0, 0, 0), (1, 0, 0), (0, 1, 0)] triangles = [points] pyscene = {'t1': triangles} cs = CaribuScene(scene=pyscene) bbox = cs.bbox() assert bbox == ((0, 0, 0), (1, 1, 0))
def Bud(g, vid, turtle): """ bt: Terminal Bud. sphere """ t = turtle #turtle.setColor(1) nid = g.node(vid) order = nid.order t.setColor(2+order) turtle.F(0.05) sphere = pgl.Sphere(radius=0.02) turtle.customGeometry(sphere)
def plotDirect( latitude, longitude, jourJul, startH, stopH, step=30, decalSun = 1, decalGMT = 0): # Plots Sun trajectory in the sky during a given Julian day, at given latitudes and longitudes # and at hours of the day defined in hours (typically between startH = 0.0 and stopH = 23.5) # decalSun is the time zone (fuseau horaire), e.g. +1 for Paris sunPos = getDirectLight( latitude, longitude, jourJul, startH, stopH, step, decalSun, decalGMT ) sc = pgl.Scene() for i in range(len(sunPos)): #print sunPos[i][0],sunPos[i][1], sunPos[i][2] pos = pgl.Vector3(pgl.Vector3.Spherical(30,radians( sunPos[i][0] ), radians( 90 - sunPos[i][1] ) )) sc += pgl.Shape(pgl.Translated(pos,pgl.Sphere(1)),pgl.Material(),i+1) pgl.Viewer.display(sc) return sc
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 test_autoscreen(): s = pgl.Scene() s.add(pgl.Sphere()) cs = CaribuScene(scene=s) npix = cs.auto_screen(0.01) assert npix == 173 points = [(0, 0, 0), (1, 0, 0), (0, 1, 0)] triangles = [points] pyscene = {'t1': triangles} cs = CaribuScene(scene=pyscene) npix = cs.auto_screen(0.01) assert npix == 141
def sphere(x, y, z, radius=0.4, color=color(80, 200, 100), slice=5, stack=5, id=0): v = pgl.Vector3(x, y, z) s = pgl.Sphere(radius, slice, stack) tr = pgl.Translated(v, s) return pgl.Shape(tr, color, id)
def dart_display(self, radius=0.1, coef=0.8, add=False): import openalea.plantgl.all as pgl sphere = pgl.Sphere(radius,slices=16,stacks=16) coal = pgl.Material(ambient=(8,10,13),diffuse=3.,specular=(89,89,89),shininess=0.3) purple = pgl.Material(ambient=(72,28,72),diffuse=2.,specular=(89,89,89),shininess=0.3) green = pgl.Material(ambient=(0,88,9),diffuse=2.,specular=(89,89,89),shininess=0.3) blue = pgl.Material(ambient=(9,0,88),diffuse=2.,specular=(89,89,89),shininess=0.3) font = pgl.Font(size=10) s = pgl.Scene() dart_points = {} for dart in self.darts(): dart_point = self.get_position(dart) dart_face_center = self.element_center(dart,2) dart_edge_center = self.element_center(dart,1) dart_face_point = dart_face_center + coef*(dart_point-dart_face_center) dart_face_edge_center = dart_face_center + coef*(dart_edge_center-dart_face_center) dart_edge_point = dart_face_edge_center + coef*(dart_face_point-dart_face_edge_center) dart_middle_edge_point = dart_face_edge_center + 0.33*(dart_edge_point-dart_face_edge_center) dart_points[dart] = [dart_edge_point,dart_middle_edge_point] s += pgl.Shape(pgl.Translated(dart_points[dart][0],sphere),coal) # s += pgl.Shape(pgl.Translated(np.mean(dart_points[dart],axis=0), pgl.Text(str(dart),fontstyle=font)), coal, id=dart) s += pgl.Shape(pgl.Polyline(dart_points[dart],width=2),coal) for dart in self.darts(): alpha_0_points = [] alpha_0_points += [dart_points[dart][1]] alpha_0_points += [dart_points[self.alpha(0,dart)][1]] s += pgl.Shape(pgl.Polyline(alpha_0_points,width=5),purple) alpha_1_points = [] alpha_1_points += [0.66*dart_points[dart][0] + 0.33*dart_points[dart][1]] alpha_1_points += [0.66*dart_points[self.alpha(1,dart)][0] + 0.33*dart_points[self.alpha(1,dart)][1]] s += pgl.Shape(pgl.Polyline(alpha_1_points,width=5),green) alpha_2_points = [] alpha_2_points += [0.33*dart_points[dart][0] + 0.66*dart_points[dart][1]] alpha_2_points += [0.33*dart_points[self.alpha(2,dart)][0] + 0.66*dart_points[self.alpha(2,dart)][1]] s += pgl.Shape(pgl.Polyline(alpha_2_points,width=5),blue) if add : pgl.Viewer.add(s) else : pgl.Viewer.display(s)
def bud2d(g, vid, turtle): """Return a bud shape :return: the bud shape (a sphere) :rtype: Sphere """ t = colors_turtle(turtle) #turtle.setColor(1) nid = g.node(vid) order = nid.order t.setColor(order) sphere = pgl.Sphere(0.08) turtle.customGeometry(sphere)
def plotDirect(latitude, longitude, jourJul, startH, stopH, step=30, decalSun=1, decalGMT=0): sunPos = getDirectLight(latitude, longitude, jourJul, startH, stopH, step, decalSun, decalGMT) sc = pgl.Scene() for i in range(len(sunPos)): # print sunPos[i][0],sunPos[i][1], sunPos[i][2] pos = pgl.Vector3(pgl.Vector3.Spherical(30, radians(sunPos[i][0]), radians(90 - sunPos[i][1]))) sc += pgl.Shape(pgl.Translated(pos, pgl.Sphere(1)), pgl.Material(), i + 1) pgl.Viewer.display(sc) return sc
def test_instantiation_from_python(): s = pgl.Scene() s.add(pgl.Sphere()) points = [(0, 0, 0), (1, 0, 0), (0, 1, 0)] triangles = [points] pyscene = {'t1': triangles} sky = [(0, (0, 0, -1)), (1, (0, 0, -1))] pattern = (0, 0, 20, 20) pattern_old_style = ((0, 0), (20, 20)) materials = {'par': {'t1': (0.3, )}} simplified_materials = {'par': (0.3, )} soil_reflectance = {'par': 0} cs = CaribuScene(scene=s, light=sky, pattern=pattern) assert len(cs.scene) == 1 assert len(list(cs.scene.values())[0]) == 112 assert len(cs.light) == 2 assert len(cs.pattern) == 4 assert list( cs.material[cs.default_band].values())[0] == cs.default_material assert cs.soil_reflectance[ cs.default_band] == cs.default_soil_reflectance cs = CaribuScene(scene=pyscene, opt=materials, pattern=pattern_old_style) assert len(cs.scene) == 1 assert len(cs.scene['t1']) == 1 assert cs.material == materials assert cs.soil_reflectance['par'] == cs.default_soil_reflectance assert len(cs.pattern) == 4 cs = CaribuScene(scene=pyscene, soil_reflectance=soil_reflectance) assert 'par' in cs.material assert cs.soil_reflectance == soil_reflectance cs = CaribuScene(scene=pyscene, opt=materials, soil_reflectance=soil_reflectance) assert 'par' in cs.material assert cs.soil_reflectance == soil_reflectance cs = CaribuScene(scene=pyscene, opt=simplified_materials, soil_reflectance=soil_reflectance) assert 'par' in cs.material assert cs.soil_reflectance == soil_reflectance return cs
def test_mtg(): g = MTG() cs = mtg_to_cscene(g) assert cs == {} g.add_property('geometry') cs = mtg_to_cscene(g) assert cs == {} geom = g.property('geometry') geom[0] = pgl.Sphere() cs = mtg_to_cscene(g) assert len(cs) == 1 assert len(cs.values()[0][0]) == 3 assert len(cs.values()[0][0][0]) == 3 return cs
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 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 = pgl.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 = pgl.Shape(pgl.TriangleSet(pointList, indexList), appearance=pgl.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 = pgl.Shape(geometry=pgl.Polyline(pts), appearance=pgl.Material((125, 12, 12))) scene.add(crv) for pt in pts: scene.add( pgl.Shape( pgl.Translated(pgl.Vector3(pt), pgl.Sphere(radius=0.1)))) for i in range(m): pts = [ctrl_net[j][i] for j in range(n)] crv = pgl.Shape(geometry=pgl.Polyline(pts), appearance=pgl.Material((12, 12, 125))) scene.add(crv) pgl.Viewer.display(scene)
def bud(g, vid, turtle): """Generates the buds in the turtle :param g: MTG :type g: MTG :param vid: vid selected :type vid: int :param turtle: Turtle :type turtle: Turtle """ t = colors_turtle(turtle) #turtle.setColor(1) nid = g.node(vid) order = nid.order t.setColor(2+order) turtle.F(0.05) sphere = pgl.Sphere(radius=0.02) turtle.customGeometry(sphere)
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 test_ifs(): for i in xrange(5): ifs = pgl.IFS( randint(1, 3), [pgl.Transform4(randtransform()) for i in xrange(1, 4)], pgl.Sphere(radius=uniform(0.1, 1), slices=randint(4, 255), stacks=randint(4, 255))) res = eval_code(ifs, True) for j in xrange(len(ifs.transfoList)): for k in xrange(4): if pgl.norm( ifs.transfoList[j].getMatrix().getColumn(k) - res.transfoList[j].getMatrix().getColumn(k)) > 1e-3: print ifs.transfoList[j].getMatrix() print res.transfoList[j].getMatrix() print k, pgl.norm( ifs.transfoList[j].getMatrix().getColumn(k) - res.transfoList[j].getMatrix().getColumn(k)) warnings.warn( "Transformation retrieval from matrix4 failed.")
def unit_sphere_scene(): return pgl.Scene([pgl.Sphere()])
__docformat__ = "restructuredtext en" ASHAPE3D_STANDARD_AXIS = pgl.Vector3.OZ ASHAPE3D_STANDARD_ROLL = 0. ASHAPE3D_STANDARD_POS = pgl.Vector3.ORIGIN ASHAPE3D_STANDARD_MATERIAL = material.white ASHAPE3D_STANDARD_SCALE = pgl.Vector3(1, 1, 1) # save memory with primitive types related ACYLINDER_PRIMITIVE = pgl.Cylinder() ACYLINDER_PRIMITIVE.name = "ACylinderPrimitive" ACYLINDER_STANDARD_RADIUS = 1. ACYLINDER_STANDARD_HEIGHT = 1. ACYLINDER_STANDARD_AXIS = ASHAPE3D_STANDARD_AXIS ASPHERE_PRIMITIVE = pgl.Sphere() ASPHERE_PRIMITIVE.name = "ASpherePrimitive" ASPHERE_STANDARD_RADIUS = 1. AARROW_STANDARD_AXIS = ASHAPE3D_STANDARD_AXIS AARROW_STANDARD_RADIUS = 1. AARROW_STANDARD_HEIGHT = 1. AARROW_SHAFT_PROPORTION = 0.7 AARROW_HEAD_PROPORTION = 1.5 ACONE_PRIMITIVE = pgl.Cone() ACONE_PRIMITIVE.name = "AConePrimitive" class AShape3D(object): """Basic shape used to wrap all <2 axis objects.
def leafB( x, y, z, color=color( 30, 210, 40 ) ): v = pgl.Vector3( x, y, z ) s = pgl.Sphere( 0.4, 5, 5 ) ts = pgl.Tapered( 1.0, 0.3, s ) tr = pgl.Translated( v, ts ) return pgl.Shape( tr, color )
def scene(): s = pgl.Scene() #s.add(pgl.Sphere(slices=64,stacks=64)) s.add(pgl.Sphere()) return s
def spheres(obj, **kwds): X_attr = kwds.get('X_attr', 'X') Y_attr = kwds.get('Y_attr', 'Y') pos_dist = kwds.get('dist', 'Distance') pos_az = kwds.get('az', 'Azimuth') circ_attr = kwds.get('circ_attr', 'Circonference') height_attr = kwds.get('height_attr', 'Haut') botHoup_attr = kwds.get('botHoup', 'BaseHoup') radiusHoup_prefix = kwds.get('radiusHoup_prefix', 'r_houp') radiusHoup = kwds.get('radiusHoup', 'Mean') wood = kwds.get('wood', True) rgb = kwds.get('rgb', None) spec_name = kwds.get('spec_name', 'houppier_mat') if not obj.__dict__.has_key("posX") and not obj.__dict__.has_key("posY"): try: r = obj.__dict__[pos_dist] a = obj.__dict__[pos_az] obj.posX = r * cos(radians(forest2geomAZ(a))) obj.posY = r * sin(radians(forest2geomAZ(a))) except KeyError: obj.posX = obj.__dict__[X_attr] obj.posY = obj.__dict__[Y_attr] ht = 100 * (obj.__dict__[height_attr] - obj.__dict__[botHoup_attr]) rd_kz = [k for k in obj.__dict__.keys() if radiusHoup_prefix in k] radii = [obj.__dict__[k] for k in rd_kz] if radiusHoup: if radiusHoup == 'Max': sph_radius = 1.0 * max(radii) else: sph_radius = obj.__dict__[radiusHoup] else: sph_radius = 1.0 * sum(radii) / len(radii) #sph_radius = ht/2. h = pgl.Translated( pgl.Vector3(obj.posX, obj.posY, obj.__dict__[botHoup_attr] * 100 + sph_radius), pgl.Sphere(sph_radius, 10, 5)) tr = pgl.Translated( pgl.Vector3(obj.posX, obj.posY, 0), pgl.Cylinder(obj.__dict__[circ_attr] / (2 * pi), obj.__dict__[botHoup_attr] * 100 + ht * 0.1)) if rgb == None: s_h = pgl.Shape(h, stand_material(spec_name=spec_name)) else: r, g, b = rgb s_h = pgl.Shape(h, stand_material(r, g, b, spec_name)) s_tr = pgl.Shape( tr, stand_material(spec_name="trunk_mat"), ) # s_h.id*10 ) if wood: return (s_h, s_tr) else: return (s_h, )
def Sphere(self, radius=1., **kwds): print "#### get in Sphere func" return pgl.Sphere(radius=float(radius)), None
def test(): pgl.Viewer.display(pgl.Sphere())
def Sphere(self, radius=1., **kwds): return pgl.Sphere(radius=float(radius)), None