Page d'accueil Description du projet
#ifndef POLYGON_H
#define POLYGON_H
/*********************************************
 *
 * Cedric Pradalier
 * DEA 2000/2001 
 * INRIA Rhones Alpes
 * http://cedric.pradalier.free.fr/index.html
 * mail : http://cedric.pradalier.free.fr/mail.html
 *
 * *******************************************/


#include <stdlib.h>
extern "C" {
    #include "gpc.h"
}
#include "Object.h"
#include "Vector.h"
#include "VtkPoly.h"


// Classe implémentant un wrapper autour des polygones
// de gpc.h
class Polygon:public Object
{
    protected : 
        gpc_polygon * poly;
    public :
        Polygon() {poly = new gpc_polygon;gpc_init_polygon(poly);}
        
        // Approximation polygonale d'un arc de cercle fermé 
        // (en part de tarte). Plus num_edges est grand, plus l'approximation
        // est valide
        Polygon(double x_center,double y_center,
                double radius,double theta_min,
                double theta_max,int num_edges) 
        {
            poly = new gpc_polygon;
            gpc_init_polygon(poly);
            gpc_pie_polygon(x_center,y_center,
                    radius,theta_min,theta_max,
                    num_edges,poly);
        }

        // Polygone rectangulaire
        Polygon (double x,double y,double w,double h)
        {
            poly = new gpc_polygon;
            gpc_init_polygon(poly);
            gpc_create_rectangle(x,y,w,h,poly);
        }

        // Construction a partir d'une liste de point
        // [x1,y1,x2,y2...xn,yn]. size == n
        Polygon(double * points,int size)
        {
            poly = new gpc_polygon;
            gpc_init_polygon(poly);
            gpc_create_polygon(points,size,poly);
        }
            

        // Construction d'un polygone a partir d'un gpc_polygon*.
        // le pointeur est affecté directement, sans copie
        Polygon(gpc_polygon *p) {poly = p;}
        
        // Constructeur de copie
        Polygon(Polygon *p) 
        {
            poly = new gpc_polygon;
            gpc_init_polygon(poly);
            gpc_copy_polygon(p->poly,poly);
        }
        
        // Constructeur de copie
        Polygon(const Polygon &p) 
        {
            poly = new gpc_polygon;
            gpc_init_polygon(poly);
            gpc_copy_polygon(p.poly,poly);
        }

        virtual ~Polygon(); 

        // Afectation
        Polygon & operator=(const Polygon & P) 
        {
                copyPolygon(P);
                return (*this);
        }

        // copie explicite
        void copyPolygon(const Polygon & P) 
        {
                delete poly;
                poly = new gpc_polygon;
                gpc_init_polygon(poly);
                gpc_copy_polygon(P.poly,poly);
        }

        
        virtual void Print(FILE*fp=stdout) 
        {
            gpc_write_polygon(fp,true,poly);
        }

        void PrintAsTriStrip(FILE*fp=stdout)
        {
            gpc_write_tristrip(fp,poly);
        }
            
        gpc_polygon* getGpcPolygon() {return poly;}

        // Lecture dans fp. la syntaxe doit etre correcte.
        void Read(FILE * fp); 
        
        bool isEmpty() {return (poly->num_contours == 0);}

        // Décomposition du polygone en partie connexe
        void getConnexList(Vector * list);

        // Operateurs ensemblistes
        Polygon * Union(Polygon * p);
        Polygon * Intersection(Polygon * p);
        Polygon * Minus(Polygon * p);
        Polygon * Xor(Polygon * p);

        
        int getNbContours() {return poly->num_contours;}
        
        // teste si (x,y) est un sommet du poly
        bool isAVertex(double x,double y);
        
        // barycentre
        void getCenter(double * x, double * y);

        // cercle circonscrit
        void getBoundingCircle(double * x, double * y,double * r);

        // surface
        double getArea();

        // liste des segments formant les cotes
        void getEdges(Vector * list);

        // fusions des points trop pres l'un de l'autre
        void filter() {gpc_filter_polygon(poly);}
        
        // decoupage au points d'étranglement :
        //
        // +-----------+
        // |           |                                 
        // |           |                                 
        // |           | point d'etranglement            
        // |           |/                                
        // +-----------+----------+                      
        //             |          |                      
        //             |          |                      
        //             |          |                      
        //             +----------+                      
        //                                                   
        void cut();        
            
        // Nombre de parties connexes
        int numPartConnex();

        // Calcule l'ensemble des points visibles à partir de (x,y).
        // Algorithme TRES TRES inefficace. TODO : utiliser un meilleur algo
        Polygon * computeVisibility(double x,double y);

        // Collect les points du polygone pour les exporter
        // en PolyData pour Vtk (Visualization Tool Kit)
        void collectVtkPoints(VtkPoints & points,VtkPolygons & polygons);
};

#endif // POLYGON_H