Helper_ConvexHull.h File Reference


Detailed Description

Author:
Daniel Krajzewicz
Date:
Fri, 29.04.2005
Version:
Id
Helper_ConvexHull.h 8236 2010-02-10 11:16:41Z behrisch

Definition in file Helper_ConvexHull.h.

#include <config.h>
#include "Position2D.h"
#include "Position2DVector.h"
#include <vector>

Go to the source code of this file.

Functions

SUMOReal isLeft (const Position2D &P0, const Position2D &P1, const Position2D &P2)
Position2DVector simpleHull_2D (const Position2DVector &V)


Function Documentation

SUMOReal isLeft ( const Position2D P0,
const Position2D P1,
const Position2D P2 
) [inline]

Definition at line 60 of file Helper_ConvexHull.h.

References Position2D::x(), and Position2D::y().

Referenced by simpleHull_2D().

00062                              {
00063     return (P1.x() - P0.x())*(P2.y() - P0.y()) - (P2.x() - P0.x())*(P1.y() - P0.y());
00064 }

Position2DVector simpleHull_2D ( const Position2DVector V  ) 

Definition at line 49 of file Helper_ConvexHull.cpp.

References isLeft(), Position2DVector::push_back_noDoublePos(), and Position2DVector::size().

Referenced by Position2DVector::convexHull().

00049                                          {
00050     if (V.size()<3) {
00051         throw ProcessError();
00052     }
00053     // initialize a deque D[] from bottom to top so that the
00054     // 1st three vertices of V[] are a counterclockwise triangle
00055     int n = (int) V.size();
00056     std::vector<Position2D> D(2*n+1);
00057     int bot = n-2, top = bot+3;   // initial bottom and top deque indices
00058     D[bot] = D[top] = V[2];       // 3rd vertex is at both bot and top
00059     if (isLeft(V[0], V[1], V[2]) > 0) {
00060         D[bot+1] = V[0];
00061         D[bot+2] = V[1];          // ccw vertices are: 2,0,1,2
00062     } else {
00063         D[bot+1] = V[1];
00064         D[bot+2] = V[0];          // ccw vertices are: 2,1,0,2
00065     }
00066 
00067     // compute the hull on the deque D[]
00068     for (int i=3; i < n; i++) {   // process the rest of vertices
00069         // test if next vertex is inside the deque hull
00070         if (bot>=(int) D.size()||top-1>=(int) D.size()||i>=(int) V.size()) {
00071             throw ProcessError();
00072         }
00073         if ((isLeft(D[bot], D[bot+1], V[i]) > 0) &&
00074                 (isLeft(D[top-1], D[top], V[i]) > 0))
00075             continue;         // skip an interior vertex
00076 
00077         // incrementally add an exterior vertex to the deque hull
00078         // get the rightmost tangent at the deque bot
00079         while (isLeft(D[bot], D[bot+1], V[i]) <= 0) {
00080             ++bot;                // remove bot of deque
00081             if (bot>=(int) D.size()) {
00082                 throw ProcessError();
00083             }
00084         }
00085         if (bot==0) {
00086             throw ProcessError();
00087         }
00088         D[--bot] = V[i];          // insert V[i] at bot of deque
00089 
00090         if (top==0||top>=(int) D.size()) {
00091             throw ProcessError();
00092         }
00093         // get the leftmost tangent at the deque top
00094         while (isLeft(D[top-1], D[top], V[i]) <= 0) {
00095             --top;                // pop top of deque
00096             if (top==0||top>=(int) D.size()) {
00097                 throw ProcessError();
00098             }
00099         }
00100 
00101         if (top+1>=(int) D.size()) {
00102             throw ProcessError();
00103         }
00104         D[++top] = V[i];          // push V[i] onto top of deque
00105     }
00106 
00107     // transcribe deque D[] to the output hull array H[]
00108     int h;        // hull vertex counter
00109     Position2DVector H;
00110     for (h=0; h <= (top-bot); h++) {
00111         if (bot + h>=(int) D.size()) {
00112             throw ProcessError();
00113         }
00114         H.push_back_noDoublePos(D[bot + h]);
00115     }
00116     return H;
00117 }


Generated on Wed May 5 00:06:38 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6