00001 #ifndef GEOM_UTIL_H
00002 #define GEOM_UTIL_H
00003
00004 #include <iostream>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <math.h>
00008 #include <vector>
00009 #include <mapbasic.h>
00010
00011 #define EXIT_SUCCESS 0
00012 #define EXIT_FAILURE 1
00013
00014 using namespace std;
00015
00016 typedef MAP::Vector_3<double> Vec3D;
00017 typedef MAP::Vector_2<double> tPointd;
00018
00019
00020 typedef std::vector<tPointd> tPolygond;
00021 typedef enum { Pin, Qin, Unknown } tInFlag;
00022
00023
00024
00025
00026
00027 class Ray3D
00028 {
00029
00030 public:
00031
00032 Vec3D src;
00033 Vec3D delta;
00034
00035 Ray3D(){};
00036
00037
00038
00039 Ray3D(Vec3D & s, Vec3D & d)
00040 {
00041 src = s;
00042 delta = d-s;
00043 };
00044
00045 void set(double* s , double* d)
00046 {
00047 src[0] = s[0];
00048 src[1] = s[1];
00049 src[2] = s[2];
00050 delta[0] = d[0]-s[0];
00051 delta[1] = d[1]-s[1];
00052 delta[2] = d[2]-s[2];
00053 }
00054
00055 Vec3D get_point(double d)
00056 {
00057 return Vec3D(src[0] + (d*delta[0]),
00058 src[1] + (d*delta[1]),
00059 src[2] + (d*delta[2]));
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 };
00080
00081
00082
00083
00084
00085 class Plane3D
00086 {
00087
00088 public:
00089 Vec3D p;
00090 Vec3D n;
00091 Vec3D u;
00092 Vec3D v;
00093
00094 Plane3D(){};
00095
00096
00097 void from_tri(double tri[][3])
00098 {
00099 Vec3D t1,t2;
00100
00101 p[0] = tri[0][0];
00102 p[1] = tri[0][1];
00103 p[2] = tri[0][2];
00104 t1[0] = tri[1][0] - p[0];
00105 t1[1] = tri[1][1] - p[1];
00106 t1[2] = tri[1][2] - p[2];
00107 t2[0] = tri[2][0] - p[0];
00108 t2[1] = tri[2][1] - p[1];
00109 t2[2] = tri[2][2] - p[2];
00110 n = (Vec3D) MAP::Vector_3<double>::cross_product(t1,t2);
00111 n.normalize();
00112
00113 Vec3D u,v;
00114 u[0] = tri[1][0]-p[0];
00115 u[1] = tri[1][1]-p[1];
00116 u[2] = tri[1][2]-p[2];
00117 v = (Vec3D) MAP::Vector_3<double>::cross_product(n,u);
00118 set_local_frame(u,v);
00119 }
00120
00121 double intersect_ray(Ray3D & r)
00122 {
00123 double d,t;
00124 d = -(p*n);
00125 t = -((n*r.src) +d)/(n*r.delta);
00126 return t;
00127 }
00128
00129 void set_local_frame(Vec3D & ul, Vec3D & vl)
00130 {
00131 u = ul; u.normalize();
00132 v = vl; v.normalize();
00133 }
00134
00135
00136
00137
00138 void map(Vec3D & p3d, tPointd & p2d)
00139 {
00140 Vec3D tp3d = p3d-p;
00141 p2d[0] = tp3d*u;
00142 p2d[1] = tp3d*v;
00143 }
00144
00145 void unmap(Vec3D & p3d, tPointd & p2d)
00146 {
00147 p3d = p + p2d[0]*u + p2d[1]*v;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 };
00171
00172
00173
00174 template<typename T>
00175 void erase_consecutive_dups(vector<T> & v)
00176 {
00177 int d =0;
00178 typename vector<T>::iterator i = unique(v.begin(), v.end());
00179 v.erase(i,v.end());
00180 if (v[0] == v[v.size()-1]) v.erase(v.end()-1,v.end());
00181 }
00182
00183
00184
00185 bool point_in_convpoly3D(Vec3D & p, vector<Plane3D> & P);
00186 bool point_in_convpoly2D(tPointd & p, tPolygond & P);
00187 bool poly_in_convpoly2D(tPolygond & P1, tPolygond & P2);
00188 void radial_sort2D(tPolygond & P);
00189 double tri_prism_X(double tri[][3], double prism[][3], vector<Vec3D> & xpoly);
00190 int convex_poly_X( tPolygond & P, tPolygond & Q, tPolygond & I);
00191
00192 int area_sign( tPointd & a, tPointd & b, tPointd & c );
00193 char seg_seg_int( tPointd & a, tPointd & b, tPointd & c,
00194 tPointd & d, tPointd & p, tPointd & q );
00195 char parallel_int( tPointd & a, tPointd & b, tPointd & c,
00196 tPointd & d, tPointd & p, tPointd & q );
00197 bool between( tPointd & a, tPointd & b, tPointd & c );
00198 bool left_on( tPointd & a, tPointd & b, tPointd & c );
00199 bool left( tPointd & a, tPointd & b, tPointd & c );
00200 tInFlag in_out( tPointd & p, tInFlag inflag, int aHB, int bHA, tPolygond & I );
00201 int advance( int a, int *aa, int n, bool inside, tPointd & v , tPolygond & I);
00202 void shared_seg( tPointd & p, tPointd & q, tPolygond & I );
00203
00204
00205 #endif