Polygon2D.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  Polygon2D.cpp
00003  *
00004  *  (C) 2008 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Additional information:
00008  *  $Id: Polygon2D.cpp 44313 2011-04-06 22:46:28Z agas $
00009  *******************************************************************************/
00010 
00011 #include <stdlib.h>
00012 #include "Polygon2D.h"
00013 #include "Point2D.h"
00014 #include "Line2D.h"
00015 
00016 
00017 #define THIS Polygon2D
00018 
00019 using namespace std;
00020 
00021 vector<Line2D> THIS::getLines() const
00022 {
00023   vector<Line2D> lines;
00024   vector<Point2D>::const_iterator pointIt = m_Points.begin() +1;
00025   while ( pointIt != m_Points.end() )
00026   {
00027     lines.push_back ( Line2D ( * ( pointIt-1 ), *pointIt ) );
00028     pointIt++;
00029   }
00030   lines.push_back ( Line2D ( * ( m_Points.end()-1 ), *m_Points.begin() ) );
00031 
00032   return lines;
00033 }
00034 
00035 void THIS::clipLines ( std::vector<Line2D>& linesToClip ) const
00036 {
00037   // for each line to clip
00038   vector<Line2D>::iterator linesIt = linesToClip.begin();
00039   while ( linesIt != linesToClip.end() )
00040   {
00041     if ( !clipLine ( *linesIt ) )
00042     {
00043       // line lies completely outside of the polygon
00044       linesToClip.erase ( linesIt );
00045       continue;
00046     }
00047     linesIt++;
00048   }
00049 }
00050 
00051 bool THIS::clipLine ( Line2D& lineToClip ) const
00052 {
00053   vector<Line2D> polygonLines = getLines();
00054   CVec2 v = lineToClip.vec();
00055 
00056   float tIn = 0.0;
00057   float tOut = 1.0;
00058 
00059   // for each line of the polygon
00060   vector<Line2D>::const_iterator polygonIt = polygonLines.begin();
00061   while ( polygonIt != polygonLines.end() )
00062   {
00063     CVec2 n = ( *polygonIt ).getNormal();
00064 //     cout << "polLine "<< ( *polygonIt ).toString() << endl;
00065 //     cout << "n "<< n.toString() << endl;
00066 
00067     CVec2 w0 = lineToClip.start()- ( *polygonIt ).start();
00068     CVec2 w1 = lineToClip.end()- ( *polygonIt ).start();
00069 
00070 //     cout << "w0 " <<w0.toString() << endl;
00071 //     cout << "w1 " <<w1.toString() << endl;
00072 
00073     float c0 = w0.dot ( n );
00074     float c1 = w1.dot ( n );
00075 
00076     // test if line lies completely inside
00077     if ( c0 < 0 && c1 < 0 )
00078     {
00079       // do nothing;
00080       polygonIt++;
00081       continue;
00082     }
00083     // test if line lies completely outside
00084     else if ( c0 >= 0 && c1 >= 0 )
00085     {
00086 /*      cout << "return 1" << endl;*/
00087       return false;
00088     }
00089     // clip lines
00090     else
00091     {
00092       float denominator = v.dot ( n );
00093 
00094       if ( denominator == 0 )
00095       {
00096         // impossible to happen
00097         polygonIt++;
00098         continue;
00099       }
00100 
00101       float t = -c0/denominator;
00102 
00103       // test for "in" or "out" point
00104       if ( denominator < 0 && t > tIn )
00105       {
00106         // intersection point is an "in point"
00107         tIn = t;
00108       }
00109       else if ( denominator > 0 && t < tOut )
00110       {
00111         // intersection point is an "out point"
00112         tOut = t;
00113       }
00114     }
00115     polygonIt++;
00116   }
00117 
00118   if ( tIn > tOut ) {
00119 /*    cout << "return 2" << endl;*/
00120     return false;
00121   }
00122 
00123   lineToClip.setEnd ( lineToClip.start() + tOut * v );
00124   lineToClip.setStart ( lineToClip.start() + tIn * v );
00125 
00126 //   cout << "return true" << endl;
00127 
00128   return true;
00129 }
00130 
00131 #undef THIS


robbie_architecture
Author(s): Viktor Seib
autogenerated on Mon Oct 6 2014 02:53:09