Helper_ConvexHull.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034
00035 #include "Helper_ConvexHull.h"
00036
00037
00038 #include <utils/common/UtilExceptions.h>
00039 #include <iostream>
00040
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044
00045
00046
00047
00048 Position2DVector
00049 simpleHull_2D(const Position2DVector &V) {
00050 if (V.size()<3) {
00051 throw ProcessError();
00052 }
00053
00054
00055 int n = (int) V.size();
00056 std::vector<Position2D> D(2*n+1);
00057 int bot = n-2, top = bot+3;
00058 D[bot] = D[top] = V[2];
00059 if (isLeft(V[0], V[1], V[2]) > 0) {
00060 D[bot+1] = V[0];
00061 D[bot+2] = V[1];
00062 } else {
00063 D[bot+1] = V[1];
00064 D[bot+2] = V[0];
00065 }
00066
00067
00068 for (int i=3; i < n; i++) {
00069
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;
00076
00077
00078
00079 while (isLeft(D[bot], D[bot+1], V[i]) <= 0) {
00080 ++bot;
00081 if (bot>=(int) D.size()) {
00082 throw ProcessError();
00083 }
00084 }
00085 if (bot==0) {
00086 throw ProcessError();
00087 }
00088 D[--bot] = V[i];
00089
00090 if (top==0||top>=(int) D.size()) {
00091 throw ProcessError();
00092 }
00093
00094 while (isLeft(D[top-1], D[top], V[i]) <= 0) {
00095 --top;
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];
00105 }
00106
00107
00108 int h;
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 }
00118
00119
00120
00121
00122