]> git.mxchange.org Git - flightgear.git/blob - Triangulate/triangle.cxx
Working on preparationsn for triangulation.
[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
27
28 // Constructor
29 FGTriangle::FGTriangle( void ) {
30 }
31
32
33 // Destructor
34 FGTriangle::~FGTriangle( void ) {
35 }
36
37
38 // populate this class based on the specified gpc_polys list
39 int FGTriangle::build( const FGgpcPolyList& gpc_polys ) {
40     int index;
41     tripoly poly;
42
43     // traverse the gpc_polys and build a unified node list and a set
44     // of Triangle PSLG that reference the node list by index
45     // (starting at zero)
46
47     gpc_polygon *gpc_poly;
48     const_gpcpoly_iterator current, last;
49
50     // process polygons in priority order
51     cout << "prepairing node list and polygons" << endl;
52
53     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
54         cout << "area type = " << i << endl;
55         current = gpc_polys.polys[i].begin();
56         last = gpc_polys.polys[i].end();
57         for ( ; current != last; ++current ) {
58             gpc_poly = *current;
59             cout << "processing a polygon, contours = " 
60                  << gpc_poly->num_contours << endl;
61
62             poly.erase(poly.begin(), poly.end());
63
64             if (gpc_poly->num_contours <= 0 ) {
65                 cout << "FATAL ERROR! no contours in this polygon" << endl;
66                 exit(-1);
67             }
68
69             if (gpc_poly->num_contours > 1 ) {
70                 cout << "FATAL ERROR! no multi-contour support" << endl;
71                 sleep(5);
72                 // exit(-1);
73             }
74
75             for ( int j = 0; j < gpc_poly->num_contours; j++ ) {
76                 for ( int k = 0; k < gpc_poly->contour[j].num_vertices; k++ ) {
77                     Point3D p( gpc_poly->contour[j].vertex[k].x,
78                                gpc_poly->contour[j].vertex[k].y,
79                                0 );
80                     index = trinodes.unique_add( p );
81                     poly.push_back(index);
82                     // cout << index << endl;
83                 }
84                 polylist[i].push_back(poly);
85             }
86         }
87     }
88
89     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
90         if ( polylist[i].size() ) {
91             cout << get_area_name((AreaType)i) << " = " 
92                  << polylist[i].size() << endl;
93         }
94     }
95     return 0;
96 }
97
98
99 // do actual triangulation
100 int FGTriangle::do_triangulate( const tripoly& poly ) {
101     point_container node_list;
102     struct triangulateio in, mid, out, vorout;
103     int counter;
104
105     // define input points
106     node_list = trinodes.get_node_list();
107
108     in.numberofpoints = node_list.size();
109     in.pointlist = (REAL *) malloc(in.numberofpoints * 2 * sizeof(REAL));
110
111     point_iterator current, last;
112     current = node_list.begin();
113     last = node_list.end();
114     counter = 0;
115     for ( ; current != last; ++current ) {
116         in.pointlist[counter++] = current->x();
117         in.pointlist[counter++] = current->y();
118     }
119 }
120
121
122 // triangulate each of the polygon areas
123 int FGTriangle::triangulate() {
124     tripoly poly;
125
126     tripoly_list_iterator current, last;
127
128     for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
129         cout << "area type = " << i << endl;
130         current = polylist[i].begin();
131         last = polylist[i].end();
132         for ( ; current != last; ++current ) {
133             poly = *current;
134             cout << "triangulating a polygon, size = " << poly.size() << endl;
135
136             do_triangulate( poly );
137         }
138     }
139
140     return 0;
141 }
142
143
144 // $Log$
145 // Revision 1.4  1999/03/19 22:29:04  curt
146 // Working on preparationsn for triangulation.
147 //
148 // Revision 1.3  1999/03/19 00:27:10  curt
149 // Continued work on triangulation preparation.
150 //
151 // Revision 1.2  1999/03/18 04:31:11  curt
152 // Let's not pass copies of huge structures on the stack ... ye might see a
153 // segfault ... :-)
154 //
155 // Revision 1.1  1999/03/17 23:51:59  curt
156 // Initial revision.
157 //