Package fabmetheus_utilities :: Package geometry :: Package geometry_tools :: Module face
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.geometry.geometry_tools.face

  1  """ 
  2  Face of a triangle mesh. 
  3   
  4  """ 
  5   
  6  from fabmetheus_utilities.geometry.geometry_utilities import evaluate 
  7  from fabmetheus_utilities.vector3 import Vector3 
  8  from fabmetheus_utilities import euclidean 
  9  #from fabmetheus_utilities import intercircle 
 10  from fabmetheus_utilities import xml_simple_reader 
 11  from fabmetheus_utilities import xml_simple_writer 
 12  import cmath 
 13  import StringIO 
 14  import math 
 15   
 16   
 17  __author__ = 'Enrique Perez (perez_enrique@yahoo.com)' 
 18  __credits__ = 'Art of Illusion <http://www.artofillusion.org/>' 
 19  __date__ = '$Date: 2008/02/05 $' 
 20  __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' 
 21   
 22   
23 -def addFaces(geometryOutput, faces):
24 'Add the faces.' 25 if geometryOutput.__class__ == list: 26 for element in geometryOutput: 27 addFaces(element, faces) 28 return 29 if geometryOutput.__class__ != dict: 30 return 31 for geometryOutputKey in geometryOutput.keys(): 32 geometryOutputValue = geometryOutput[geometryOutputKey] 33 if geometryOutputKey == 'face': 34 for face in geometryOutputValue: 35 faces.append(face) 36 else: 37 addFaces(geometryOutputValue, faces)
38
39 -def addGeometryList( faces, xmlElement ):
40 "Add vertex elements to an xml element." 41 for face in faces: 42 faceElement = xml_simple_reader.XMLElement() 43 face.addToAttributeDictionary( faceElement.attributeDictionary ) 44 faceElement.localName = 'face' 45 faceElement.parentNode = xmlElement 46 xmlElement.childNodes.append( faceElement )
47
48 -def getCommonVertexIndex( edgeFirst, edgeSecond ):
49 "Get the vertex index that both edges have in common." 50 for edgeFirstVertexIndex in edgeFirst.vertexIndexes: 51 if edgeFirstVertexIndex == edgeSecond.vertexIndexes[0] or edgeFirstVertexIndex == edgeSecond.vertexIndexes[1]: 52 return edgeFirstVertexIndex 53 print( "Inconsistent GNU Triangulated Surface" ) 54 print( edgeFirst ) 55 print( edgeSecond ) 56 return 0
57
58 -def getFaces(geometryOutput):
59 'Get the faces.' 60 faces = [] 61 addFaces(geometryOutput, faces) 62 return faces
63
64 -def processXMLElement(xmlElement):
65 "Process the xml element." 66 face = Face() 67 face.index = len(xmlElement.parentNode.xmlObject.faces) 68 for vertexIndexIndex in xrange(3): 69 face.vertexIndexes.append(evaluate.getEvaluatedInt(None, 'vertex' + str(vertexIndexIndex), xmlElement)) 70 xmlElement.parentNode.xmlObject.faces.append(face)
71 72
73 -class Edge:
74 "An edge of a triangle mesh."
75 - def __init__(self):
76 "Set the face indexes to None." 77 self.faceIndexes = [] 78 self.vertexIndexes = [] 79 self.zMaximum = None 80 self.zMinimum = None
81
82 - def __repr__(self):
83 "Get the string representation of this Edge." 84 return str( self.index ) + ' ' + str( self.faceIndexes ) + ' ' + str(self.vertexIndexes)
85
86 - def addFaceIndex( self, faceIndex ):
87 "Add first None face index to input face index." 88 self.faceIndexes.append( faceIndex )
89
90 - def getFromVertexIndexes( self, edgeIndex, vertexIndexes ):
91 "Initialize from two vertex indices." 92 self.index = edgeIndex 93 self.vertexIndexes = vertexIndexes[:] 94 self.vertexIndexes.sort() 95 return self
96 97
98 -class Face:
99 "A face of a triangle mesh."
100 - def __init__(self):
101 "Initialize." 102 self.edgeIndexes = [] 103 self.index = None 104 self.vertexIndexes = []
105
106 - def __repr__(self):
107 "Get the string representation of this object info." 108 output = StringIO.StringIO() 109 self.addXML( 2, output ) 110 return output.getvalue()
111
112 - def addToAttributeDictionary(self, attributeDictionary):
113 "Add to the attribute dictionary." 114 for vertexIndexIndex in xrange(len(self.vertexIndexes)): 115 vertexIndex = self.vertexIndexes[vertexIndexIndex] 116 attributeDictionary['vertex' + str(vertexIndexIndex)] = str(vertexIndex)
117
118 - def addXML(self, depth, output):
119 "Add the xml for this object." 120 attributeDictionary = {} 121 self.addToAttributeDictionary(attributeDictionary) 122 xml_simple_writer.addClosedXMLTag( attributeDictionary, depth, 'face', output )
123
124 - def copy(self):
125 'Get the copy of this face.' 126 faceCopy = Face() 127 faceCopy.edgeIndexes = self.edgeIndexes[:] 128 faceCopy.index = self.index 129 faceCopy.vertexIndexes = self.vertexIndexes[:] 130 return faceCopy
131
132 - def getFromEdgeIndexes( self, edgeIndexes, edges, faceIndex ):
133 "Initialize from edge indices." 134 if len(self.vertexIndexes) > 0: 135 return 136 self.index = faceIndex 137 self.edgeIndexes = edgeIndexes 138 for edgeIndex in edgeIndexes: 139 edges[ edgeIndex ].addFaceIndex( faceIndex ) 140 for triangleIndex in xrange(3): 141 indexFirst = ( 3 - triangleIndex ) % 3 142 indexSecond = ( 4 - triangleIndex ) % 3 143 self.vertexIndexes.append( getCommonVertexIndex( edges[ edgeIndexes[ indexFirst ] ], edges[ edgeIndexes[ indexSecond ] ] ) ) 144 return self
145
146 - def setEdgeIndexesToVertexIndexes( self, edges, edgeTable ):
147 "Set the edge indexes to the vertex indexes." 148 if len(self.edgeIndexes) > 0: 149 return 150 for triangleIndex in xrange(3): 151 indexFirst = ( 3 - triangleIndex ) % 3 152 indexSecond = ( 4 - triangleIndex ) % 3 153 vertexIndexFirst = self.vertexIndexes[ indexFirst ] 154 vertexIndexSecond = self.vertexIndexes[ indexSecond ] 155 vertexIndexPair = [ vertexIndexFirst, vertexIndexSecond ] 156 vertexIndexPair.sort() 157 edgeIndex = len( edges ) 158 if str( vertexIndexPair ) in edgeTable: 159 edgeIndex = edgeTable[ str( vertexIndexPair ) ] 160 else: 161 edgeTable[ str( vertexIndexPair ) ] = edgeIndex 162 edge = Edge().getFromVertexIndexes( edgeIndex, vertexIndexPair ) 163 edges.append( edge ) 164 edges[ edgeIndex ].addFaceIndex( self.index ) 165 self.edgeIndexes.append( edgeIndex )
166