Package pyffi :: Package utils :: Module trianglemesh :: Class Mesh
[hide private]
[frames] | no frames]

Class Mesh

source code

A mesh of interconnected faces.
Instance Methods [hide private]
 
__init__(self, faces=None, lock=True)
Initialize a mesh, and optionally assign its faces and lock.
source code
 
__repr__(self)
String representation.
source code
 
_add_edge(self, face, pv0, pv1)
Create new edge for mesh for given face, or return existing edge.
source code
 
add_face(self, v0, v1, v2)
Create new face for mesh, or return existing face.
source code
 
lock(self)
Lock the mesh.
source code
 
discard_face(self, face)
Remove the face from the mesh.
source code
Instance Variables [hide private]
  _faces
Dictionary of all faces.
  _edges
Dictionary of all edges.
list of Face faces
List of faces of the mesh.
Method Details [hide private]

__init__(self, faces=None, lock=True)
(Constructor)

source code 
Initialize a mesh, and optionally assign its faces and lock.
Parameters:
  • faces (Iterable or type(None)) - None, or an iterator over faces to assign to the mesh.
  • lock (bool) - Whether to lock the mesh or not (ignored when faces are not specified).

__repr__(self)
(Representation operator)

source code 

String representation. Examples:

>>> m = Mesh()
>>> m
Mesh()
>>> tmp = m.add_face(1, 2, 3)
>>> tmp = m.add_face(3, 2, 4)
>>> m
Mesh(faces=[(1, 2, 3), (2, 4, 3)], lock=False)
>>> m.lock()
>>> m
Mesh(faces=[(1, 2, 3), (2, 4, 3)])
>>> Mesh(faces=[(1, 2, 3),(3, 2, 4)])
Mesh(faces=[(1, 2, 3), (2, 4, 3)])

_add_edge(self, face, pv0, pv1)

source code 
Create new edge for mesh for given face, or return existing edge. Lists of faces of the new/existing edge is also updated, as well as lists of adjacent faces. For internal use only, called on each edge of the face in add_face.

add_face(self, v0, v1, v2)

source code 

Create new face for mesh, or return existing face. List of adjacent faces is also updated.

>>> m = Mesh()
>>> f0 = m.add_face(0, 1, 2)
>>> [list(faces) for faces in f0.adjacent_faces]
[[], [], []]
>>> m = Mesh()
>>> f0 = m.add_face(0, 1, 2)
>>> f1 = m.add_face(2, 1, 3)
>>> f2 = m.add_face(2, 3, 4)
>>> len(m._faces)
3
>>> len(m._edges)
9
>>> f3 = m.add_face(2, 3, 4)
>>> f3 is f2
True
>>> f4 = m.add_face(10, 11, 12)
>>> f5 = m.add_face(12, 10, 11)
>>> f6 = m.add_face(11, 12, 10)
>>> f4 is f5
True
>>> f4 is f6
True
>>> len(m._faces)
4
>>> len(m._edges)
12

Another mesh:

0->-1
 \ / \
  2-<-3
  2->-3
   \ /
    4
>>> m = Mesh()
>>> f0 = m.add_face(0, 1, 2)
>>> f1 = m.add_face(1, 3, 2)
>>> f2 = m.add_face(2, 3, 4)
>>> list(f0.get_adjacent_faces(0))
[Face(1, 3, 2)]
>>> list(f0.get_adjacent_faces(1))
[]
>>> list(f0.get_adjacent_faces(2))
[]
>>> list(f1.get_adjacent_faces(1))
[Face(2, 3, 4)]
>>> list(f1.get_adjacent_faces(3))
[Face(0, 1, 2)]
>>> list(f1.get_adjacent_faces(2))
[]
>>> list(f2.get_adjacent_faces(2))
[]
>>> list(f2.get_adjacent_faces(3))
[]
>>> list(f2.get_adjacent_faces(4))
[Face(1, 3, 2)]
>>> # add an extra face, and check changes
>>> f3 = m.add_face(2, 3, 5)
>>> list(f0.get_adjacent_faces(0))
[Face(1, 3, 2)]
>>> list(f0.get_adjacent_faces(1))
[]
>>> list(f0.get_adjacent_faces(2))
[]
>>> list(f1.get_adjacent_faces(1)) # extra face here!
[Face(2, 3, 4), Face(2, 3, 5)]
>>> list(f1.get_adjacent_faces(3))
[Face(0, 1, 2)]
>>> list(f1.get_adjacent_faces(2))
[]
>>> list(f2.get_adjacent_faces(2))
[]
>>> list(f2.get_adjacent_faces(3))
[]
>>> list(f2.get_adjacent_faces(4))
[Face(1, 3, 2)]

lock(self)

source code 

Lock the mesh. Frees memory by clearing the structures which are only used to update the face adjacency lists. Sets the faces attribute to the sorted list of all faces (sorting helps with ensuring that the strips in faces are close together).

>>> m = Mesh()
>>> f0 = m.add_face(3, 1, 2)
>>> f1 = m.add_face(0, 1, 2)
>>> f2 = m.add_face(5, 6, 2)
>>> m.faces # doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
AttributeError: ...
>>> m.lock()
>>> m.faces # should be sorted
[Face(0, 1, 2), Face(1, 2, 3), Face(2, 5, 6)]
>>> m.faces[0].index
0
>>> m.faces[1].index
1
>>> m.faces[2].index
2
>>> m._faces # doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
AttributeError: ...
>>> m._edges # doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
AttributeError: ...
>>> m.add_face(1, 2, 3) # doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
AttributeError: ...