00001
00002 // ** THIS IS A CODE SNIPPET WHICH WILL EFFICIEINTLY TRIANGULATE ANY
00003 // ** POLYGON/CONTOUR (without holes) AS A STATIC CLASS. THIS SNIPPET
00004 // ** IS COMPRISED OF 3 FILES, TRIANGULATE.H, THE HEADER FILE FOR THE
00005 // ** TRIANGULATE BASE CLASS, TRIANGULATE.CPP, THE IMPLEMENTATION OF
00006 // ** THE TRIANGULATE BASE CLASS, AND TEST.CPP, A SMALL TEST PROGRAM
00007 // ** DEMONSTRATING THE USAGE OF THE TRIANGULATOR. THE TRIANGULATE
00008 // ** BASE CLASS ALSO PROVIDES TWO USEFUL HELPER METHODS, ONE WHICH
00009 // ** COMPUTES THE AREA OF A POLYGON, AND ANOTHER WHICH DOES AN EFFICENT
00010 // ** POINT IN A TRIANGLE TEST.
00011 // ** SUBMITTED BY JOHN W. RATCLIFF (jratcliff@verant.com) July 22, 2000
00012
00013 #ifndef TRIANGULATE_H
00014 #define TRIANGULATE_H
00015
00016 /****************************************************************/
00017 /* Static class to triangulate any contour/polygon efficiently **/
00018 /* You should replace Vector2d with whatever your own Vector **/
00019 /* class might be. Does not support polygons with holes. **/
00020 /* Uses STL vectors to represent a dynamic array of vertices. **/
00021 /* This code snippet was submitted to FlipCode.com by **/
00022 /* John W. Ratcliff (jratcliff@verant.com) on July 22, 2000 **/
00023 /* I did not write the original code/algorithm for this **/
00024 /* this triangulator, in fact, I can't even remember where I **/
00025 /* found it in the first place. However, I did rework it into **/
00026 /* the following black-box static class so you can make easy **/
00027 /* use of it in your own code. Simply replace Vector2d with **/
00028 /* whatever your own Vector implementation might be. **/
00029 /****************************************************************/
00030
00031
00032 #include "MathTypes.h"
00033
00034 class Triangulate_f
00035 {
00036 public:
00037
00038 // triangulate a contour/polygon, places results in STL vector
00039 // as series of triangles.
00040 static bool Process(const FLine2 &contour, FLine2 &result);
00041
00042 // compute area of a contour/polygon
00043 static float Area(const FLine2 &contour);
00044
00045 // decide if point Px/Py is inside triangle defined by
00046 // (Ax,Ay) (Bx,By) (Cx,Cy)
00047 static bool InsideTriangle(float Ax, float Ay,
00048 float Bx, float By,
00049 float Cx, float Cy,
00050 float Px, float Py);
00051
00052 private:
00053 static bool Snip(const FLine2 &contour,int u,int v,int w,int n,int *V);
00054 };
00055
00056 class Triangulate_d
00057 {
00058 public:
00059
00060 // triangulate a contour/polygon, places results in STL vector
00061 // as series of triangles.
00062 static bool Process(const DLine2 &contour, DLine2 &result);
00063
00064 // compute area of a contour/polygon
00065 static double Area(const DLine2 &contour);
00066
00067 // decide if point Px/Py is inside triangle defined by
00068 // (Ax,Ay) (Bx,By) (Cx,Cy)
00069 static bool InsideTriangle(double Ax, double Ay,
00070 double Bx, double By,
00071 double Cx, double Cy,
00072 double Px, double Py);
00073
00074 private:
00075 static bool Snip(const DLine2 &contour,int u,int v,int w,int n,int *V);
00076 };
00077
00078 #endif
1.2.4 written by Dimitri van Heesch,
© 1997-2000