def test_topomesh():
    t = Topomesh(3)

    def dstr (did) :
    	return "%d (%d)" % (did, t.degree(did) )

    dids = [t.add_wisp(d) for d in range(4)]

    print [dstr(did) for did in t.wisps()]
def graph_from_svg(filename):
    """
    """
    from vplants.plantgl.math import Vector2
    from openalea.container import Topomesh
    from openalea.svgdraw import open_svg, SVGSphere, SVGConnector
    from openalea.tissueshape import edge_loop_around

    mesh = Topomesh(2)
    f = open_svg(filename, 'r')
    sc = f.read()
    f.close()

    zone_pos = {}
    zone_svg_id = {}
    lay = sc.get_layer("zones")
    for elm in lay.elements():
        if isinstance(elm, SVGSphere):
            cid = mesh.add_wisp(2)
            zone_svg_id[elm.id()] = cid
            zone_pos[cid] = Vector2(*sc.natural_pos(
                *elm.scene_pos(elm.center())))

    vertex_pos = {}
    vertex_svg_id = {}
    lay = sc.get_layer("walls")
    for elm in lay.elements():
        if isinstance(elm, SVGSphere):
            pid = mesh.add_wisp(0)
            vertex_svg_id[elm.id()] = pid
            vertex_pos[pid] = Vector2(*sc.natural_pos(
                *elm.scene_pos(elm.center())))

    lay = sc.get_layer("walls")
    for elm in lay.elements():
        if isinstance(elm, SVGConnector):
            eid = mesh.add_wisp(1)
            mesh.link(1, eid, vertex_svg_id[elm.source()])
            mesh.link(1, eid, vertex_svg_id[elm.target()])
    for cid, ref_point in zone_pos.iteritems():
        for eid in edge_loop_around(mesh, vertex_pos, ref_point):
            mesh.link(2, cid, eid)

    position = dict([(v, np.array(p)) for v, p in vertex_pos.iteritems()])

    return graph_from_tissue(mesh, position)
def save_topomesh(filename, bldmesh):
    """Write the given blender mesh
	into a file
	"""

    mesh = Topomesh(2)
    X = Quantity({}, "pix", "float", "x coordinate of vertices")
    Y = Quantity({}, "pix", "float", "y coordinate of vertices")
    Z = Quantity({}, "pix", "float", "z coordinate of vertices")

    #vertices
    ptrans = {}
    for vtx in bldmesh.verts:
        pid = mesh.add_wisp(0)
        X[pid], Y[pid], Z[pid] = vtx.co
        ptrans[vtx.index] = pid

    #edges
    etrans = {}
    for edge in bldmesh.edges:
        pids = [ptrans[edge.v1.index], ptrans[edge.v2.index]]
        pids.sort()
        try:
            eid = etrans[tuple(pids)]
        except KeyError:
            eid = mesh.add_wisp(1)
            etrans[tuple(pids)] = eid

        for pid in pids:
            mesh.link(1, eid, pid)

    #faces
    for face in bldmesh.faces:
        fid = mesh.add_wisp(2)
        pids = [ptrans[vtx.index] for vtx in face.v]
        nb = len(pids)
        for i in xrange(nb):
            bids = [pids[i], pids[(i + 1) % nb]]
            bids.sort()
            mesh.link(2, fid, etrans[tuple(bids)])

    #write
    props = [[('X', X), ('Y', Y), ('Z', Z)], [], []]
    write_topomesh(filename, mesh, "from blender", props)
def save_topomesh (filename, bldmesh):
	"""Write the given blender mesh
	into a file
	"""
	
	mesh = Topomesh(2)
	X = Quantity({},"pix","float","x coordinate of vertices")
	Y = Quantity({},"pix","float","y coordinate of vertices")
	Z = Quantity({},"pix","float","z coordinate of vertices")
	
	#vertices
	ptrans = {}
	for vtx in bldmesh.verts :
		pid = mesh.add_wisp(0)
		X[pid],Y[pid],Z[pid] = vtx.co
		ptrans[vtx.index] = pid
	
	#edges
	etrans = {}
	for edge in bldmesh.edges :
		pids = [ptrans[edge.v1.index],
		        ptrans[edge.v2.index] ]
		pids.sort()
		try :
			eid = etrans[tuple(pids)]
		except KeyError :
			eid = mesh.add_wisp(1)
			etrans[tuple(pids)] = eid
		
		for pid in pids :
			mesh.link(1,eid,pid)
	
	#faces
	for face in bldmesh.faces :
		fid = mesh.add_wisp(2)
		pids = [ptrans[vtx.index] for vtx in face.v]
		nb = len(pids)
		for i in xrange(nb) :
			bids = [pids[i],pids[(i + 1) % nb]]
			bids.sort()
			mesh.link(2,fid,etrans[tuple(bids)])
	
	#write
	props = [[('X',X),('Y',Y),('Z',Z)],
	         [],
	         [] ]
	write_topomesh(filename,mesh,"from blender",props)
def graph_from_svg(filename):
    """
    """
    from vplants.plantgl.math import Vector2
    from openalea.container import Topomesh
    from openalea.svgdraw import open_svg, SVGSphere, SVGConnector
    from openalea.tissueshape import edge_loop_around

    mesh = Topomesh(2)
    f = open_svg(filename, "r")
    sc = f.read()
    f.close()

    zone_pos = {}
    zone_svg_id = {}
    lay = sc.get_layer("zones")
    for elm in lay.elements():
        if isinstance(elm, SVGSphere):
            cid = mesh.add_wisp(2)
            zone_svg_id[elm.id()] = cid
            zone_pos[cid] = Vector2(*sc.natural_pos(*elm.scene_pos(elm.center())))

    vertex_pos = {}
    vertex_svg_id = {}
    lay = sc.get_layer("walls")
    for elm in lay.elements():
        if isinstance(elm, SVGSphere):
            pid = mesh.add_wisp(0)
            vertex_svg_id[elm.id()] = pid
            vertex_pos[pid] = Vector2(*sc.natural_pos(*elm.scene_pos(elm.center())))

    lay = sc.get_layer("walls")
    for elm in lay.elements():
        if isinstance(elm, SVGConnector):
            eid = mesh.add_wisp(1)
            mesh.link(1, eid, vertex_svg_id[elm.source()])
            mesh.link(1, eid, vertex_svg_id[elm.target()])
    for cid, ref_point in zone_pos.iteritems():
        for eid in edge_loop_around(mesh, vertex_pos, ref_point):
            mesh.link(2, cid, eid)

    position = dict([(v, np.array(p)) for v, p in vertex_pos.iteritems()])

    return graph_from_tissue(mesh, position)
from openalea.container import Topomesh

m=Topomesh(2)

pts=[m.add_wisp(0) for i in xrange(6)]
edges=[m.add_wisp(1) for i in xrange(7)]
faces=[m.add_wisp(2) for i in xrange(2)]

for i,(pid1,pid2) in enumerate([(0,1),(1,2),(2,3),(3,4),(4,5),(5,1),(1,4)]) :
    m.link(1,edges[i],pts[pid1])
    m.link(1,edges[i],pts[pid2])

for i,eids in enumerate([(0,4,5,6),(1,2,3,6)]) :
    for eid in eids :
        m.link(2,faces[i],edges[eid])

for eid in edges :
    print eid
    print "B",list(m.borders(1,eid)),m.nb_borders(1,eid)
    print "R",list(m.regions(1,eid)),m.nb_regions(1,eid)

for fid in faces :
    print list(m.borders(2,fid)),m.nb_borders(2,fid)
from pdb import pm
from openalea.container import Topomesh,\
                                is_flip_topo_allowed,flip_edge,\
                                is_collapse_topo_allowed,collapse_edge

m = Topomesh(3)
protected = [1]
##########################
#
#       single triangle
#
##########################
for i in xrange(3):
    m.add_wisp(0, i)
for i in xrange(3):
    m.add_wisp(1, i)
    m.link(1, i, i)
    m.link(1, i, (i + 1) % 3)
m.add_wisp(2, 3)
for i in xrange(3):
    m.link(2, 3, i)
m.add_wisp(3, 0)
m.link(3, 0, 3)

#flip
for eid in m.wisps(1):
    assert not is_flip_topo_allowed(m, eid)

#collapse
for eid in m.wisps(1):
    assert not is_collapse_topo_allowed(m, eid, protected)
예제 #8
0
def test_algos():
    m = Topomesh(3)
    protected = [1]
    ##########################
    #
    #       single triangle
    #
    ##########################
    for i in xrange(3):
        m.add_wisp(0, i)
    for i in xrange(3):
        m.add_wisp(1, i)
        m.link(1, i, i)
        m.link(1, i, (i + 1) % 3)
    m.add_wisp(2, 3)
    for i in xrange(3):
        m.link(2, 3, i)
    m.add_wisp(3, 0)
    m.link(3, 0, 3)

    # flip
    for eid in m.wisps(1):
        assert not is_flip_topo_allowed(m, eid)
예제 #9
0
from openalea.container import Topomesh,topo_divide_face,topo_divide_cell

#############################################
#
#		divide face
#
#############################################

#	0----0----1----1----2
#	|                   |
#	5        fid        2
#	|                   |
#	5----4----4----3----3

m = Topomesh(2)

for i in xrange(6) :
	m.add_wisp(0,i)

for i in xrange(6) :
	m.add_wisp(1,i)
	m.link(1,i,i)
	m.link(1,i,(i + 1) % 6)

fid = m.add_wisp(2)
for i in xrange(6) :
	m.link(2,fid,i)

fid1,fid2,eid = topo_divide_face(m,fid,1,4)

#	0----0----1----1----2
예제 #10
0
def test_topomesh():
    t = Topomesh(3)

    def dstr(did):
        return "%d (%d)" % (did, t.degree(did))

    dids = [t.add_wisp(d) for d in range(4)]

    print[dstr(did) for did in t.wisps()]
    print[dstr(did) for did in t.wisps(1)]

    for i in range(3):
        t.link(dids[i], dids[i + 1])

    did = dids[1]
    print "regions", did, [dstr(rid) for rid in t.regions(did)]
    print "borders", did, [dstr(bid) for bid in t.borders(did)]

    did = dids[2]
    print "regions2", did, [dstr(rid) for rid in t.regions(did, 2)]
    print "borders2", did, [dstr(bid) for bid in t.borders(did, 2)]

    t.remove_wisp(did)
    print[dstr(did) for did in t.wisps()]

    did = dids[1]
    print "regions", did, [dstr(rid) for rid in t.regions(did)]
    print "borders", did, [dstr(bid) for bid in t.borders(did)]
예제 #11
0
from openalea.container import Topomesh

t = Topomesh()

def dstr (did) :
	return "%d (%d)" % (did, t.degree(did) )

dids = [t.add_dart(d) for d in range(4)]

print [dstr(did) for did in t.darts()]
print [dstr(did) for did in t.darts(1)]

for i in range(3) :
	t.link(dids[i], dids[i + 1])

did = dids[1]
print "regions", did, [dstr(rid) for rid in t.regions(did)]
print "borders", did, [dstr(bid) for bid in t.borders(did)]


did = dids[2]
print "regions2", did, [dstr(rid) for rid in t.regions(did, 2)]
print "borders2", did, [dstr(bid) for bid in t.borders(did, 2)]


t.remove_dart(did)
print [dstr(did) for did in t.darts()]

did = dids[1]
print "regions", did, [dstr(rid) for rid in t.regions(did)]
print "borders", did, [dstr(bid) for bid in t.borders(did)]
예제 #12
0
def grid2D (shape) :
	"""
	:param `shape`: shape of the grid tuple (nb_rows,nb_columns)
	"""
	t=Topomesh(2)
	positions={}
	NBI,NBJ=shape
	NBCELLS=NBI*NBJ
	xincr=1./NBI
	yincr=1./NBJ
	cell_grid=Grid( (NBI,NBJ) )
	for ind in cell_grid :
		cid=t.add_wisp(0,ind)
	point_grid=Grid( (NBI+1,NBJ+1) )
	for ind in point_grid :
		i,j=point_grid.coordinates(ind)
		eid=t.add_wisp(2,ind)
		positions[eid]=Vector3(xincr*i,yincr*j,0)
	#murs verticaux
	for j in xrange(NBJ) :
		for i in xrange(NBI+1) :
			wid=t.add_wisp(1)
			t.link(1,wid,point_grid.index( (i,j) ))
			t.link(1,wid,point_grid.index( (i,j+1) ))
			if i<NBI :
				t.link(0,cell_grid.index( (i,j) ),wid)
			if i>0 :
				t.link(0,cell_grid.index( (i-1,j) ),wid)
	#murs horizontaux
	for i in xrange(NBI) :
		for j in xrange(NBJ+1) :
			wid=t.add_wisp(1)
			t.link(1,wid,point_grid.index( (i,j) ))
			t.link(1,wid,point_grid.index( (i+1,j) ))
			if j<NBJ :
				t.link(0,cell_grid.index( (i,j) ),wid)
			if j>0 :
				t.link(0,cell_grid.index( (i,j-1) ),wid)
	#et voila
	return t,positions
예제 #13
0
def test_algos():
    m = Topomesh(3)
    protected = [1]
    ##########################
    #
    #       single triangle
    #
    ##########################
    for i in xrange(3):
        m.add_wisp(0, i)
    for i in xrange(3):
        m.add_wisp(1, i)
        m.link(1, i, i)
        m.link(1, i, (i + 1) % 3)
    m.add_wisp(2, 3)
    for i in xrange(3):
        m.link(2, 3, i)
    m.add_wisp(3, 0)
    m.link(3, 0, 3)

    # flip
    for eid in m.wisps(1):
        assert not is_flip_topo_allowed(m, eid)
예제 #14
0
def test_divide_face():
    #	0----0----1----1----2
    #	|                   |
    #	5        fid        2
    #	|                   |
    #	5----4----4----3----3

    m = Topomesh(2)

    for i in xrange(6):
        m.add_wisp(0, i)

    for i in xrange(6):
        m.add_wisp(1, i)
        m.link(1, i, i)
        m.link(1, i, (i + 1) % 6)

    fid = m.add_wisp(2)
    for i in xrange(6):
        m.link(2, fid, i)

    fid1, fid2, eid = topo_divide_face(m, fid, 1, 4)

    #	0----0----1----1----2
    #	|         |         |
    #	5  fid1  eid  fid2  2
    #	|         |         |
    #	5----4----4----3----3

    assert set(m.borders(1, eid)) == set([1, 4])
    assert set(m.regions(1, eid)) == set([fid1, fid2])
    fid1, = m.regions(1, 5)
    fid2, = m.regions(1, 2)
    assert set(m.borders(2, fid1)) == set([0, eid, 4, 5])
    assert set(m.borders(2, fid2)) == set([1, 2, 3, eid])
예제 #15
0
def test_divide_cell():
    # top
    #	0----12---4----16---8
    #	|         |         |
    #	0    0    4    4    8
    #	|         |         |
    #	1----13---5----17---9

    # front
    #	1----13---5----17---9
    #	|         |         |
    #	1    1    5    5    9
    #	|         |         |
    #	2----14---6----18---10

    # bottom
    #	3----15---7----19---11
    #	|         |         |
    #	2    2    6    6    10
    #	|         |         |
    #	2----14---6----18---10

    # back
    #	0----12---4----16---8
    #	|         |         |
    #	3    3    7    7    11
    #	|         |         |
    #	3----15---7----19---11

    # left
    #	0----0----1
    #	|         |
    #	3    8    1
    #	|         |
    #	3----2----2

    # middle (no face)
    #	4----4----5
    #	|         |
    #	7         5
    #	|         |
    #	7----6----6

    # right
    #	8----8----9
    #	|         |
    #	11   9    9
    #	|         |
    #	11---10---10

    m = Topomesh(3)

    # points
    for i in xrange(12):
        m.add_wisp(0, i)

    # edges
    for i in xrange(20):
        m.add_wisp(1, i)

    for i in xrange(4):
        m.link(1, i, i)
        m.link(1, i, (i + 1) % 4)

        m.link(1, 4 + i, 4 + i)
        m.link(1, 4 + i, 4 + (i + 1) % 4)

        m.link(1, 8 + i, 8 + i)
        m.link(1, 8 + i, 8 + (i + 1) % 4)

        m.link(1, 12 + i, i)
        m.link(1, 12 + i, 4 + i)

        m.link(1, 16 + i, 4 + i)
        m.link(1, 16 + i, 8 + i)

    # faces
    for i in xrange(10):
        m.add_wisp(2, i)

    for i in xrange(4):
        m.link(2, 8, i)
        m.link(2, 9, 8 + i)

    for eid in (0, 4, 12, 13):
        for i in xrange(3):
            m.link(2, i, i + eid)
            m.link(2, 4 + i, 4 + i + eid)

    for eid in (3, 12, 7, 15):
        m.link(2, 3, eid)
        m.link(2, 7, 4 + eid)

    # cell
    cid = m.add_wisp(3)
    for i in xrange(10):
        m.link(3, cid, i)

    # divide
    cid1, cid2, fid = topo_divide_cell(m, cid, (4, 5, 6, 7))

    # middle (no face)
    #	4----4----5
    #	|         |
    #	7   fid   5
    #	|         |
    #	7----6----6

    assert set(m.borders(2, fid)) == set([4, 5, 6, 7])
    assert set(m.regions(2, fid)) == set([cid1, cid2])
    cid1, = m.regions(2, 8)
    cid2, = m.regions(2, 9)
    assert set(m.borders(3, cid1)) == set([0, 1, 2, 3, 8, fid])
    assert set(m.borders(3, cid2)) == set([4, 5, 6, 7, 9, fid])
from pdb import pm
from openalea.container import Topomesh, is_flip_topo_allowed, flip_edge, is_collapse_topo_allowed, collapse_edge

m = Topomesh(3)
protected = [1]
##########################
#
#       single triangle
#
##########################
for i in xrange(3):
    m.add_wisp(0, i)
for i in xrange(3):
    m.add_wisp(1, i)
    m.link(1, i, i)
    m.link(1, i, (i + 1) % 3)
m.add_wisp(2, 3)
for i in xrange(3):
    m.link(2, 3, i)
m.add_wisp(3, 0)
m.link(3, 0, 3)

# flip
for eid in m.wisps(1):
    assert not is_flip_topo_allowed(m, eid)

# collapse
for eid in m.wisps(1):
    assert not is_collapse_topo_allowed(m, eid, protected)

##########################
예제 #17
0
def grid2D(shape):
    """
	:param `shape`: shape of the grid tuple (nb_rows,nb_columns)
	"""
    t = Topomesh(2)
    positions = {}
    NBI, NBJ = shape
    NBCELLS = NBI * NBJ
    xincr = 1. / NBI
    yincr = 1. / NBJ
    cell_grid = Grid((NBI, NBJ))
    for ind in cell_grid:
        cid = t.add_wisp(0, ind)
    point_grid = Grid((NBI + 1, NBJ + 1))
    for ind in point_grid:
        i, j = point_grid.coordinates(ind)
        eid = t.add_wisp(2, ind)
        positions[eid] = Vector3(xincr * i, yincr * j, 0)
    #murs verticaux
    for j in xrange(NBJ):
        for i in xrange(NBI + 1):
            wid = t.add_wisp(1)
            t.link(1, wid, point_grid.index((i, j)))
            t.link(1, wid, point_grid.index((i, j + 1)))
            if i < NBI:
                t.link(0, cell_grid.index((i, j)), wid)
            if i > 0:
                t.link(0, cell_grid.index((i - 1, j)), wid)
    #murs horizontaux
    for i in xrange(NBI):
        for j in xrange(NBJ + 1):
            wid = t.add_wisp(1)
            t.link(1, wid, point_grid.index((i, j)))
            t.link(1, wid, point_grid.index((i + 1, j)))
            if j < NBJ:
                t.link(0, cell_grid.index((i, j)), wid)
            if j > 0:
                t.link(0, cell_grid.index((i, j - 1)), wid)
    #et voila
    return t, positions
예제 #18
0
def test_topomesh():
    t = Topomesh(3)

    assert t.nb_wisps(0) == 0
    assert t.nb_wisps(1) == 0
    assert t.nb_wisps(2) == 0