]> git.mxchange.org Git - flightgear.git/blob - Triangulate/triangle.cxx
Added trisegs.[ch]xx tripoly.[ch]xx.
[flightgear.git] / Triangulate / triangle.cxx
1 // triangle.cxx -- "Triangle" interface class
2 //
3 // Written by Curtis Olson, started March 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #include "triangle.hxx"
26 #include "tripoly.hxx"
27
28
29 // Constructor
30 FGTriangle::FGTriangle( void ) {
31 }
32
33
34 // Destructor
35 FGTriangle::~FGTriangle( void ) {
36 }
37
38
39 // populate this class based on the specified gpc_polys list
40 int FGTriangle::build( const FGgpcPolyList& gpc_polys ) {
41     int index;
42     // traverse the gpc_polys and build a unified node list and a set
43     // of Triangle PSLG that reference the node list by index
44     // (starting at zero)
45
46     gpc_polygon *gpc_poly;
47     const_gpcpoly_iterator current, last;
48
49     // process polygons in priority order
50     cout << "prepairing node list and polygons" << endl;
51
52     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
53         cout << "area type = " << i << endl;
54         current = gpc_polys.polys[i].begin();
55         last = gpc_polys.polys[i].end();
56         for ( ; current != last; ++current ) {
57             gpc_poly = *current;
58             cout << "processing a polygon, contours = " 
59                  << gpc_poly->num_contours << endl;
60
61             FGTriPoly poly;
62
63             if (gpc_poly->num_contours <= 0 ) {
64                 cout << "FATAL ERROR! no contours in this polygon" << endl;
65                 exit(-1);
66             }
67
68             if (gpc_poly->num_contours > 1 ) {
69                 cout << "FATAL ERROR! no multi-contour support" << endl;
70                 sleep(5);
71                 // exit(-1);
72             }
73
74             for ( int j = 0; j < gpc_poly->num_contours; j++ ) {
75                 for ( int k = 0; k < gpc_poly->contour[j].num_vertices; k++ ) {
76                     Point3D p( gpc_poly->contour[j].vertex[k].x,
77                                gpc_poly->contour[j].vertex[k].y,
78                                0 );
79                     index = trinodes.unique_add( p );
80                     poly.add_node(index);
81                     // cout << index << endl;
82                 }
83                 poly.calc_point_inside( trinodes );
84
85                 polylist[i].push_back(poly);
86             }
87         }
88     }
89
90     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
91         if ( polylist[i].size() ) {
92             cout << get_area_name((AreaType)i) << " = " 
93                  << polylist[i].size() << endl;
94         }
95     }
96     return 0;
97 }
98
99
100 // do actual triangulation
101 int FGTriangle::do_triangulate( const FGTriPoly& poly ) {
102     trinode_list node_list;
103     struct triangulateio in, out;
104     int counter;
105
106     // define input points
107     node_list = trinodes.get_node_list();
108
109     in.numberofpoints = node_list.size();
110     in.numberofpointattributes = 0;
111     in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
112
113     trinode_list_iterator current, last;
114     current = node_list.begin();
115     last = node_list.end();
116     counter = 0;
117     for ( ; current != last; ++current ) {
118         in.pointlist[counter++] = current->x();
119         in.pointlist[counter++] = current->y();
120     }
121
122     return 0;
123 }
124
125
126 // triangulate each of the polygon areas
127 int FGTriangle::triangulate() {
128     FGTriPoly poly;
129     struct triangulateio in, out;
130
131     trinode_list node_list = trinodes.get_node_list();
132
133     // point list
134     in.numberofpoints = node_list.size();
135     in.numberofpointattributes = 1;
136     in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
137
138     trinode_list_iterator tn_current, tn_last;
139     tn_current = node_list.begin();
140     tn_last = node_list.end();
141     int counter = 0;
142     for ( ; tn_current != tn_last; ++tn_current ) {
143         in.pointlist[counter++] = tn_current->x();
144         in.pointlist[counter++] = tn_current->y();
145     }
146
147     in.pointattributelist = (REAL *) NULL;
148     in.pointmarkerlist = (int *) NULL;
149
150     // segment list
151     in.numberofsegments = 0;
152
153     tripoly_list_iterator tp_current, tp_last;
154     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
155         cout << "area type = " << i << endl;
156         tp_current = polylist[i].begin();
157         tp_last = polylist[i].end();
158         for ( ; tp_current != tp_last; ++tp_current ) {
159             poly = *tp_current;
160             in.numberofsegments += poly.size() + 1;
161         }
162     }
163
164     in.numberofsegments = 0;
165
166   in.numberofholes = 0;
167   in.numberofregions = 1;
168   in.regionlist = (REAL *) malloc(in.numberofregions * 4 * sizeof(REAL));
169   in.regionlist[0] = 0.5;
170   in.regionlist[1] = 5.0;
171   in.regionlist[2] = 7.0;            /* Regional attribute (for whole mesh). */
172   in.regionlist[3] = 0.1;          /* Area constraint that will not be used. */
173
174   /*
175     tripoly_list_iterator current, last;
176     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
177         cout << "area type = " << i << endl;
178         current = polylist[i].begin();
179         last = polylist[i].end();
180         for ( ; current != last; ++current ) {
181             poly = *current;
182             cout << "triangulating a polygon, size = " << poly.size() << endl;
183
184             do_triangulate( poly );
185         }
186     }
187     */
188     return 0;
189 }
190
191
192 // $Log$
193 // Revision 1.6  1999/03/20 13:22:11  curt
194 // Added trisegs.[ch]xx tripoly.[ch]xx.
195 //
196 // Revision 1.5  1999/03/20 02:21:52  curt
197 // Continue shaping the code towards triangulation bliss.  Added code to
198 // calculate some point guaranteed to be inside a polygon.
199 //
200 // Revision 1.4  1999/03/19 22:29:04  curt
201 // Working on preparationsn for triangulation.
202 //
203 // Revision 1.3  1999/03/19 00:27:10  curt
204 // Continued work on triangulation preparation.
205 //
206 // Revision 1.2  1999/03/18 04:31:11  curt
207 // Let's not pass copies of huge structures on the stack ... ye might see a
208 // segfault ... :-)
209 //
210 // Revision 1.1  1999/03/17 23:51:59  curt
211 // Initial revision.
212 //