contrib/gel/gst/gst_polygon_2d_operators.cxx
Go to the documentation of this file.
00001 // This is gel/gst/gst_polygon_2d_operators.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author crossge@crd.ge.com
00008 
00009 #include "gst_polygon_2d_operators.h"
00010 
00011 vcl_vector<gst_polygon_2d_sptr> gst_make_polygons_2d( const vcl_vector<gst_edge_2d_sptr> edges)
00012 {
00013   // flags showing edges already used
00014   vcl_vector<int> used( edges.size(), 0);
00015 
00016   // repository of polygons as they are created
00017   vcl_vector<gst_polygon_2d_sptr> polygons;
00018 
00019   // start a polygon with each edge, and look for a closed cycle
00020   //  hopefully using a NEW edge
00021   for (unsigned int i=0; i< edges.size(); i++)
00022     {
00023       bool newface= false;
00024       bool closed= false;
00025       gst_polygon_2d_sptr thispoly= new gst_polygon_2d;
00026 
00027       // flags showing edges already used in this polygon
00028       vcl_vector<int> pused( edges.size(), 0);
00029 
00030       thispoly->add( edges[i]);
00031 
00032       if (!used[i])
00033           newface= true;
00034 
00035       used[i]= 1;
00036       pused[i]= 1;
00037 
00038       gst_vertex_2d_sptr start= edges[i]->get_start();
00039       gst_vertex_2d_sptr end  = edges[i]->get_end();
00040 
00041       // repeatedly look for the next edge in the cycle
00042       //  until we do a complete pass without finding any further
00043       //  edges
00044       bool added= true;
00045 
00046       while (added && !closed)
00047         {
00048           added= false;
00049 
00050           for (unsigned int j=0; j< edges.size() && !closed; j++)
00051             {
00052               if (edges[j]->get_start().ptr()== end.ptr() && !pused[j])
00053                 {
00054                   thispoly->add( edges[j]);
00055                   added= true;
00056 
00057                   end= edges[j]->get_end();
00058 
00059                   if (!used[j]) newface= true;
00060 
00061                   used[j]= 1;
00062                   pused[j]= 1;
00063 
00064                   if (end.ptr()== start.ptr())
00065                     closed= true;
00066                 }
00067             }
00068         }
00069 
00070       if (newface && closed)
00071         polygons.push_back( thispoly);
00072     }
00073 
00074   return polygons;
00075 }
00076 
00077 
00078 vcl_vector<gst_polygon_2d_sptr> gst_make_polygons_2d_unoriented( const vcl_vector<gst_edge_2d_sptr> edges)
00079 {
00080   // flags showing edges already used
00081   vcl_vector<int> used( edges.size(), 0);
00082 
00083   // repository of polygons as they are created
00084   vcl_vector<gst_polygon_2d_sptr> polygons;
00085 
00086   // start a polygon with each edge, and look for a closed cycle
00087   //  hopefully using a NEW edge
00088   for (unsigned int i=0; i< edges.size(); i++)
00089     {
00090       bool newface= false;
00091       bool closed= false;
00092       gst_polygon_2d_sptr thispoly= new gst_polygon_2d;
00093 #if 0
00094       vcl_cerr << "Starting face by adding edge\n"
00095                << *edges[i] << vcl_endl;
00096 #endif
00097       thispoly->add( edges[i]);
00098 
00099       if (!used[i])
00100         newface= true;
00101 
00102       used[i]= 1;
00103 
00104       gst_vertex_2d_sptr start= edges[i]->get_start();
00105       gst_vertex_2d_sptr end  = edges[i]->get_end();
00106 
00107       // repeatedly look for the next edge in the cycle
00108       //  until we do a complete pass without finding any further
00109       //  edges
00110       bool added= true;
00111 
00112       while (added && !closed)
00113         {
00114           added= false;
00115 
00116           for (unsigned int j=0; j< edges.size() && !closed; j++)
00117             {
00118               if (edges[j]->get_start().ptr()== end.ptr() && !used[j])
00119                 {
00120 #if 0
00121                   vcl_cerr << "Found unflip-necessary edge...\n";
00122                   vcl_cerr << *edges[j] << vcl_endl;
00123 #endif
00124                   thispoly->add( edges[j]);
00125                   added= true;
00126 
00127                   end= edges[j]->get_end();
00128 
00129                   if (!used[j]) newface= true;
00130 
00131                   used[j]= 1;
00132 
00133                   if (end.ptr()== start.ptr())
00134                     {
00135                       closed= true;
00136                     }
00137                 }
00138               else if (edges[j]->get_end().ptr()== end.ptr() && !used[j])
00139                 {
00140 #if 0
00141                   vcl_cerr << "Found flip-necessary edge...\n";
00142                   vcl_cerr << *edges[j] << " -- ";
00143 #endif
00144                   edges[j]->flip();
00145 #if 0
00146                   vcl_cerr << *edges[j] << vcl_endl;
00147 #endif
00148                   thispoly->add( edges[j]);
00149                   added= true;
00150 
00151                   end= edges[j]->get_end();
00152 
00153                   if (!used[j]) newface= true;
00154 
00155                   used[j]= 1;
00156 
00157                   if (end.ptr()== start.ptr())
00158                     closed= true;
00159                 }
00160             }
00161         }
00162 
00163       if (newface && closed)
00164         polygons.push_back( thispoly);
00165     }
00166 
00167 #if 0
00168    for (unsigned int i=0; i< polygons.size(); i++)
00169    {
00170      gst_polygon_2d *p= polygons[i].ptr();
00171      vcl_cerr << "Polygon " << i << vcl_endl;
00172      vcl_cerr << *p << vcl_endl;
00173    }
00174 #endif
00175   return polygons;
00176 }