1 """
2 This page is in the table of contents.
3 The gts.py script is an import translator plugin to get a carving from an gts file.
4
5 An import plugin is a script in the interpret_plugins folder which has the function getCarving. It is meant to be run from the interpret tool. To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
6
7 The getCarving function takes the file name of an gts file and returns the carving.
8
9 The GNU Triangulated Surface (.gts) format is described at:
10 http://gts.sourceforge.net/reference/gts-surfaces.html#GTS-SURFACE-WRITE
11
12 Quoted from http://gts.sourceforge.net/reference/gts-surfaces.html#GTS-SURFACE-WRITE
13 "All the lines beginning with GTS_COMMENTS (#!) are ignored. The first line contains three unsigned integers separated by spaces. The first integer is the number of vertexes, nv, the second is the number of edges, ne and the third is the number of faces, nf.
14
15 Follows nv lines containing the x, y and z coordinates of the vertexes. Follows ne lines containing the two indices (starting from one) of the vertexes of each edge. Follows nf lines containing the three ordered indices (also starting from one) of the edges of each face.
16
17 The format described above is the least common denominator to all GTS files. Consistent with an object-oriented approach, the GTS file format is extensible. Each of the lines of the file can be extended with user-specific attributes accessible through the read() and write() virtual methods of each of the objects written (surface, vertexes, edges or faces). When read with different object classes, these extra attributes are just ignored."
18
19 """
20
21 from fabmetheus_utilities.geometry.geometry_tools import face
22 from fabmetheus_utilities.geometry.solids import triangle_mesh
23 from fabmetheus_utilities.vector3 import Vector3
24 from fabmetheus_utilities import archive
25
26 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
27 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
28 __date__ = '$Date: 2008/21/04 $'
29 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
30
31
32 -def getFromGNUTriangulatedSurfaceText( gnuTriangulatedSurfaceText, triangleMesh ):
33 "Initialize from a GNU Triangulated Surface Text."
34 if gnuTriangulatedSurfaceText == '':
35 return None
36 lines = archive.getTextLines( gnuTriangulatedSurfaceText )
37 linesWithoutComments = []
38 for line in lines:
39 if len(line) > 0:
40 firstCharacter = line[0]
41 if firstCharacter != '#' and firstCharacter != '!':
42 linesWithoutComments.append(line)
43 splitLine = linesWithoutComments[0].split()
44 numberOfVertexes = int( splitLine[0] )
45 numberOfEdges = int(splitLine[1])
46 numberOfFaces = int( splitLine[2] )
47 faceTriples = []
48 for vertexIndex in xrange( numberOfVertexes ):
49 line = linesWithoutComments[ vertexIndex + 1 ]
50 splitLine = line.split()
51 vertex = Vector3( float( splitLine[0] ), float(splitLine[1]), float( splitLine[2] ) )
52 triangleMesh.vertexes.append(vertex)
53 edgeStart = numberOfVertexes + 1
54 for edgeIndex in xrange( numberOfEdges ):
55 line = linesWithoutComments[ edgeIndex + edgeStart ]
56 splitLine = line.split()
57 vertexIndexes = []
58 for word in splitLine[ : 2 ]:
59 vertexIndexes.append( int(word) - 1 )
60 edge = face.Edge().getFromVertexIndexes( edgeIndex, vertexIndexes )
61 triangleMesh.edges.append( edge )
62 faceStart = edgeStart + numberOfEdges
63 for faceIndex in xrange( numberOfFaces ):
64 line = linesWithoutComments[ faceIndex + faceStart ]
65 splitLine = line.split()
66 edgeIndexes = []
67 for word in splitLine[ : 3 ]:
68 edgeIndexes.append( int(word) - 1 )
69 triangleMesh.faces.append( face.Face().getFromEdgeIndexes( edgeIndexes, triangleMesh.edges, faceIndex ) )
70 return triangleMesh
71
75