1999-04-28 22:18:19 +00:00
|
|
|
// polygon.hxx -- polygon (with holes) management class
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started March 1999.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _POLYGON_HXX
|
|
|
|
#define _POLYGON_HXX
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __cplusplus
|
|
|
|
# error This library requires C++
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include <Include/compiler.h>
|
|
|
|
|
1999-06-07 03:40:33 +00:00
|
|
|
#include <string>
|
1999-04-28 22:18:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <Main/construct_types.hxx>
|
|
|
|
|
|
|
|
#include "trinodes.hxx"
|
|
|
|
|
1999-06-07 03:40:33 +00:00
|
|
|
FG_USING_STD(string);
|
1999-04-28 22:18:19 +00:00
|
|
|
FG_USING_STD(vector);
|
|
|
|
|
|
|
|
|
1999-06-05 12:45:40 +00:00
|
|
|
#define FG_MAX_VERTICES 100000
|
|
|
|
|
|
|
|
|
1999-06-06 02:31:34 +00:00
|
|
|
typedef vector < point_list > polytype;
|
1999-04-28 22:18:19 +00:00
|
|
|
typedef polytype::iterator polytype_iterator;
|
|
|
|
typedef polytype::const_iterator const_polytype_iterator;
|
|
|
|
|
|
|
|
|
|
|
|
class FGPolygon {
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
polytype poly; // polygons
|
|
|
|
point_list inside_list; // point inside list
|
|
|
|
int_list hole_list; // hole flag list
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// Constructor and destructor
|
|
|
|
FGPolygon( void );
|
|
|
|
~FGPolygon( void );
|
|
|
|
|
1999-06-07 03:40:33 +00:00
|
|
|
// Add a contour
|
|
|
|
inline void add_contour( const point_list contour, const int hole_flag ) {
|
|
|
|
poly.push_back( contour );
|
|
|
|
hole_list.push_back( hole_flag );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a contour
|
|
|
|
inline point_list get_contour( const int i ) const {
|
|
|
|
return poly[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a contour
|
|
|
|
inline void delete_contour( const int i ) {
|
|
|
|
polytype_iterator start_poly = poly.begin();
|
|
|
|
poly.erase( start_poly + i );
|
|
|
|
|
|
|
|
int_list_iterator start_hole = hole_list.begin();
|
|
|
|
hole_list.erase( start_hole + i );
|
|
|
|
}
|
|
|
|
|
1999-04-28 22:18:19 +00:00
|
|
|
// Add the specified node (index) to the polygon
|
1999-06-06 02:31:34 +00:00
|
|
|
inline void add_node( int contour, Point3D p ) {
|
1999-04-28 22:18:19 +00:00
|
|
|
if ( contour >= (int)poly.size() ) {
|
|
|
|
// extend polygon
|
1999-06-06 02:31:34 +00:00
|
|
|
point_list empty_contour;
|
1999-04-28 22:18:19 +00:00
|
|
|
empty_contour.clear();
|
|
|
|
for ( int i = 0; i < contour - (int)poly.size() + 1; ++i ) {
|
|
|
|
poly.push_back( empty_contour );
|
|
|
|
inside_list.push_back( Point3D(0.0) );
|
|
|
|
hole_list.push_back( 0 );
|
|
|
|
}
|
|
|
|
}
|
1999-06-06 02:31:34 +00:00
|
|
|
poly[contour].push_back( p );
|
1999-04-28 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return size
|
|
|
|
inline int contours() const { return poly.size(); }
|
|
|
|
inline int contour_size( int contour ) const {
|
|
|
|
return poly[contour].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the ith polygon point index from the specified contour
|
1999-06-06 02:31:34 +00:00
|
|
|
inline Point3D get_pt( int contour, int i ) const {
|
1999-04-28 22:18:19 +00:00
|
|
|
return poly[contour][i];
|
|
|
|
}
|
|
|
|
|
1999-04-29 21:39:27 +00:00
|
|
|
// calculate some "arbitrary" point inside the specified contour for
|
1999-04-28 22:18:19 +00:00
|
|
|
// assigning attribute areas
|
|
|
|
void calc_point_inside( const int contour, const FGTriNodes& trinodes );
|
|
|
|
inline Point3D get_point_inside( const int contour ) const {
|
|
|
|
return inside_list[contour];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get and set hole flag
|
|
|
|
inline int get_hole_flag( const int contour ) const {
|
|
|
|
return hole_list[contour];
|
|
|
|
}
|
|
|
|
inline void set_hole_flag( const int contour, const int flag ) {
|
|
|
|
hole_list[contour] = flag;
|
|
|
|
}
|
|
|
|
|
1999-06-07 03:40:33 +00:00
|
|
|
// erase
|
1999-04-28 22:18:19 +00:00
|
|
|
inline void erase() { poly.clear(); }
|
1999-06-05 12:45:40 +00:00
|
|
|
|
1999-06-07 03:40:33 +00:00
|
|
|
// informational
|
|
|
|
|
|
|
|
// return the area of a contour (assumes simple polygons,
|
|
|
|
// i.e. non-self intersecting.)
|
|
|
|
double area_contour( const int contour );
|
|
|
|
|
|
|
|
// return the smallest interior angle of the polygon
|
|
|
|
double minangle_contour( const int contour );
|
|
|
|
|
|
|
|
// output
|
|
|
|
void write( const string& file );
|
1999-04-28 22:18:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef vector < FGPolygon > poly_list;
|
|
|
|
typedef poly_list::iterator poly_list_iterator;
|
|
|
|
typedef poly_list::const_iterator const_poly_list_iterator;
|
|
|
|
|
|
|
|
|
1999-06-05 12:45:40 +00:00
|
|
|
// wrapper functions for gpc polygon clip routines
|
|
|
|
|
|
|
|
// Difference
|
|
|
|
FGPolygon polygon_diff( const FGPolygon& subject, const FGPolygon& clip );
|
|
|
|
|
|
|
|
// Intersection
|
|
|
|
FGPolygon polygon_int( const FGPolygon& subject, const FGPolygon& clip );
|
|
|
|
|
|
|
|
// Exclusive or
|
|
|
|
FGPolygon polygon_xor( const FGPolygon& subject, const FGPolygon& clip );
|
|
|
|
|
|
|
|
// Union
|
|
|
|
FGPolygon polygon_union( const FGPolygon& subject, const FGPolygon& clip );
|
|
|
|
|
|
|
|
|
1999-04-28 22:18:19 +00:00
|
|
|
#endif // _POLYGON_HXX
|
|
|
|
|
|
|
|
|