contrib/gel/gtrl/gtrl_triangulation.cxx
Go to the documentation of this file.
00001 // This is gel/gtrl/gtrl_triangulation.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author crossge@crd.ge.com
00008 
00009 #include "gtrl_triangulation.h"
00010 #include <vcl_compiler.h>
00011 
00012 // UNIX specific timer close
00013 #ifdef VCL_WIN32
00014 #define NO_TIMER
00015 #endif
00016 
00017 // triangulation library
00018 extern "C" {
00019 #ifdef SINGLE
00020 #define REAL float
00021 #else /* not SINGLE */
00022 #define REAL double
00023 #endif /* not SINGLE */
00024 
00025 #include <triangle.h> // from netlib, for triangulate()
00026 }
00027 
00028 
00029 gtrl_triangulation::gtrl_triangulation( gtrl_polygon poly)
00030   : poly_( poly)
00031 {
00032 }
00033 
00034 void gtrl_triangulation::run()
00035 {
00036   REAL *points= new REAL[poly_.size()*2];
00037   int *markers= new int[poly_.size()];
00038 
00039   // for some reason, the markers don't seem to
00040   //   like starting at 0
00041   const int offset = 100;
00042 
00043   vcl_vector<gtrl_vertex_sptr> pointlist;
00044 
00045   for (int i=0; i< poly_.size(); i++)
00046     {
00047       gtrl_vertex_sptr p= poly_[i];
00048       pointlist.push_back( p);
00049 
00050       points[i*2]  = p->x();
00051       points[i*2+1]= p->y();
00052       markers[i]= i+offset;
00053     }
00054 
00055   // input
00056   triangulateio in;
00057   in.numberofpoints= poly_.size();
00058   in.pointlist= points;
00059   in.pointmarkerlist= markers;
00060   in.numberofpointattributes= 0;
00061   in.numberofholes= 0;
00062   in.trianglelist= 0;
00063 
00064   // output
00065   triangulateio out;
00066   out.pointlist= 0;
00067   out.trianglelist= 0;
00068   out.pointmarkerlist= 0;
00069   out.numberofpointattributes= 0;
00070   out.numberofholes= 0;
00071   out.numberoftriangleattributes= 0;
00072 
00073   // do triangulation
00074   triangulate( "-z -i -q", &in, &out, 0);
00075 
00076   // create any new points that are necessary
00077   for (int i=0; i< out.numberofpoints; i++)
00078     {
00079       if (out.pointmarkerlist[i]< offset)
00080         {
00081           pointlist.push_back( new gtrl_vertex( out.pointlist[i*2],
00082                                                 out.pointlist[i*2+1]));
00083 
00084           out.pointmarkerlist[i]= pointlist.size()-1+ offset;
00085         }
00086     }
00087 
00088   // clean up from previous triangulation
00089   tris_.clear();
00090 
00091   // create the triangles
00092   for (int i=0; i< out.numberoftriangles; i++)
00093     {
00094       gtrl_triangle_sptr triangle= new gtrl_triangle( pointlist[ out.pointmarkerlist[ out.trianglelist[i*3]]- offset],
00095                                                      pointlist[ out.pointmarkerlist[ out.trianglelist[i*3+1]]- offset],
00096                                                      pointlist[ out.pointmarkerlist[ out.trianglelist[i*3+2]]- offset]);
00097 
00098       if (poly_.inside( triangle->mid_point()))
00099         tris_.push_back( triangle);
00100     }
00101 
00102   pts_= pointlist;
00103 
00104   // clean memory
00105   delete[] out.pointlist;
00106   delete[] out.pointmarkerlist;
00107   delete[] out.trianglelist;
00108   delete[] points;
00109   delete[] markers;
00110 }