1 module deimos.tesselator;
2 
3 //import graphite.utils.constants : TargetPlatform;
4 
5 /*
6 ** SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) 
7 ** Copyright (C) [dates of first publication] Silicon Graphics, Inc.
8 ** All Rights Reserved.
9 **
10 ** Permission is hereby granted, free of charge, to any person obtaining a copy
11 ** of this software and associated documentation files (the "Software"), to deal
12 ** in the Software without restriction, including without limitation the rights
13 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 ** of the Software, and to permit persons to whom the Software is furnished to do so,
15 ** subject to the following conditions:
16 ** 
17 ** The above copyright notice including the dates of first publication and either this
18 ** permission notice or a reference to http://oss.sgi.com/projects/FreeB/ shall be
19 ** included in all copies or substantial portions of the Software. 
20 **
21 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
22 ** INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 ** PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL SILICON GRAPHICS, INC.
24 ** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
26 ** OR OTHER DEALINGS IN THE SOFTWARE.
27 ** 
28 ** Except as contained in this notice, the name of Silicon Graphics, Inc. shall not
29 ** be used in advertising or otherwise to promote the sale, use or other dealings in
30 ** this Software without prior written authorization from Silicon Graphics, Inc.
31 */
32 /*
33 ** Author: Mikko Mononen, July 2009.
34 */
35 
36 extern(C):
37 nothrow:
38 
39 enum TessWindingRule
40 {
41     TESS_WINDING_ODD,
42     TESS_WINDING_NONZERO,
43     TESS_WINDING_POSITIVE,
44     TESS_WINDING_NEGATIVE,
45     TESS_WINDING_ABS_GEQ_TWO,
46 }
47 
48 
49 // Tesselation result element types:
50 // TESS_TRIANGLES
51 //   Each element is defined as array of 3 TESSindex.
52 //   The 3 values in the array defines indices to each vertex of a triangle.
53 // TESS_CONNECTED_TRIANGLES
54 //   Each element is defined as array of 6 TESSindex.
55 //   The first 3 values in the array defines indices to each vertex of a triangle.
56 //   The second 3 values in the array defines indices to each of neighbour element or -1 if there is no neighbour.
57 // TESS_BOUNDARY_CONTOURS
58 //   Each element is defined as array of 2 TESSindex.
59 //   The first value is index to first vertex in contour and the second value is number of vertices in the contour.
60 enum TessElementType
61 {
62     TESS_POLYGONS,
63     TESS_CONNECTED_POLYGONS,
64     TESS_BOUNDARY_CONTOURS,
65 }
66 
67 alias float TESSreal;
68 //note this shouldn't be defined(TARGET_OS_IPHONE) as its always defined either 0 or 1
69 
70 //#if TARGET_OS_IPHONE || ANDROID || __ARMEL__
71 /// OS_IPHONE, __ARMEL__のD言語でのバージョン識別子は無いので適当
72 //static if(isVersion!"iOS" || isVersion!"iPhone" || isVersion!"Android" || isVersion!"ARM")
73 //static if(TargetPlatform.isOpenGLES)
74     //alias ushort TESSindex;
75 //else
76     alias uint TESSindex;
77 
78 
79 struct TESStesselator;
80 
81 immutable TESSindex TESS_UNDEF = ~cast(TESSindex)0;
82 
83 // Custom memory allocator interface.
84 // The internal memory allocator allocates mesh edges, vertices and faces
85 // as well as dictionary nodes and active regions in buckets and uses simple
86 // freelist to speed up the allocation. The bucket size should roughly match your
87 // expected input data. For example if you process only hundreds of vertices,
88 // a bucket size of 128 might be ok, where as when processing thousands of vertices
89 // bucket size of 1024 might be approproate. The bucket size is a compromise between
90 // how often to allocate memory from the system versus how much extra space the system
91 // should allocate. Reasonable defaults are show in commects below, they will be used if
92 // the bucket sizes are zero.
93 // 
94 // The use may left the memrealloc to be null. In that case, the tesselator will not try to
95 // dynamically grow int's internal arrays. The tesselator only needs the reallocation when it
96 // has found intersecting segments and needs to add new vertex. This defency can be cured by
97 // allocating some extra vertices beforehand. The 'extraVertices' variable allows to specify
98 // number of expected extra vertices.  
99 struct TESSalloc
100 {
101     void* function(void* userData, uint size ) memalloc;
102     void* function(void* userData, void* ptr, uint size ) memrealloc;
103     void function(void *userData, void *ptr ) memfree;
104     void* userData;             // User data passed to the allocator functions.
105     int meshEdgeBucketSize;     // 512
106     int meshVertexBucketSize;   // 512
107     int meshFaceBucketSize;     // 256
108     int dictNodeBucketSize;     // 512
109     int regionBucketSize;       // 256
110     int extraVertices;          // Number of extra vertices allocated for the priority queue.
111 }
112     
113 // tessNewTess() - Creates a new tesselator.
114 // Use tessDeleteTess() to delete the tesselator.
115 // Returns:
116 //   new tesselator object.
117 TESStesselator* tessNewTess( TESSalloc* alloc );
118 
119 // tessDeleteTess() - Deletes a tesselator.
120 // Parameters:
121 //   tess - pointer to tesselator object to be deleted.
122 void tessDeleteTess( TESStesselator* tess );
123 
124 // tessAddContour() - Adds a contour to be tesselated.
125 // The type of the vertex coordinates is assumed to be TESSreal.
126 // Parameters:
127 //   tess - pointer to tesselator object.
128 //   size - number of coordinates per vertex. Must be 2 or 3.
129 //   pointer - pointer to the first coordinate of the first vertex in the array.
130 //   stride - defines offset in bytes between consecutive vertices.
131 //   count - number of vertices in contour.
132 void tessAddContour( TESStesselator* tess, int size, const(void*) pointer, int stride, int count );
133 
134 // tessTesselate() - tesselate contours.
135 // Parameters:
136 //   tess - pointer to tesselator object.
137 //   windingRule - winding rules used for tesselation, must be one of TessWindingRule.
138 //   elementType - defines the tesselation result element type, must be one of TessElementType.
139 //   polySize - defines maximum vertices per polygons if output is polygons.
140 //   vertexSize - defines the number of coordinates in tesselation result vertex, must be 2 or 3.
141 //   normal - defines the normal of the input contours, of null the normal is calculated automatically.
142 // Returns:
143 //   1 if succeed, 0 if failed.
144 int tessTesselate( TESStesselator* tess, int windingRule, int elementType, int polySize, int vertexSize, const TESSreal* normal );
145 
146 // tessGetVertexCount() - Returns number of vertices in the tesselated output.
147 int tessGetVertexCount( TESStesselator* tess );
148 
149 // tessGetVertices() - Returns pointer to first coordinate of first vertex.
150 const(TESSreal)* tessGetVertices( TESStesselator* tess );
151 
152 // tessGetVertexIndices() - Returns pointer to first vertex index.
153 // Vertex indices can be used to map the generated vertices to the original vertices.
154 // Every point added using tessAddContour() will get a new index starting at 0.
155 // New vertices generated at the intersections of segments are assigned value TESS_UNDEF.
156 const(TESSindex)* tessGetVertexIndices( TESStesselator* tess );
157     
158 // tessGetElementCount() - Returns number of elements in the the tesselated output.
159 int tessGetElementCount( TESStesselator* tess );
160 
161 // tessGetElements() - Returns pointer to the first element.
162 const(TESSindex)* tessGetElements( TESStesselator* tess );