00001
00016 #include <dlrGeometry/utilities2D.h>
00017 #include <dlrNumeric/utilities.h>
00018
00019 namespace num = dlr::numeric;
00020
00021
00022 namespace {
00023
00024 bool
00025 solve2By2LinearSystem(double a00, double a01, double a10, double a11,
00026 double b0, double b1,
00027 double& x0, double& x1) {
00028
00029 double determinant = a00 * a11 - a01 * a10;
00030 if(determinant == 0.0) {
00031 return false;
00032 }
00033 x0 = (a11 * b0 - a01 * b1) / determinant;
00034 x1 = (a00 * b1 - a10 * b0) / determinant;
00035 return true;
00036 }
00037
00038 }
00039
00040
00041 namespace dlr {
00042
00043 namespace geometry {
00044
00045 bool
00046 checkIntersect(const LineSegment2D& lineSegment0,
00047 const LineSegment2D& lineSegment1)
00048 {
00049 Vector2D dummy;
00050 return checkIntersect(lineSegment0, lineSegment1, dummy);
00051 }
00052
00053
00054 bool
00055 checkIntersect(const LineSegment2D& lineSegment0,
00056 const LineSegment2D& lineSegment1,
00057 numeric::Vector2D& intersect)
00058 {
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 Vector2D lineDirection0 =
00084 lineSegment0.getVertex0() - lineSegment0.getVertex1();
00085 Vector2D lineDirection1 =
00086 lineSegment1.getVertex0() - lineSegment1.getVertex1();
00087 double const& A_00 = lineDirection0.x();
00088 double const& A_01 = -lineDirection1.x();
00089 double const& A_10 = lineDirection0.y();
00090 double const& A_11 = -lineDirection1.y();
00091
00092
00093 Vector2D bb = lineSegment0.getVertex0() - lineSegment1.getVertex0();
00094
00095
00096 double alpha;
00097 double beta;
00098 if(!solve2By2LinearSystem(A_00, A_01, A_10, A_11, bb.x(), bb.y(),
00099 alpha, beta)) {
00100 return false;
00101 }
00102
00103
00104
00105 if((alpha < 0.0) || (alpha >= 1.0) || (beta < 0.0) || (beta >= 1.0)) {
00106 return false;
00107 }
00108
00109
00110 intersect = lineSegment0.getVertex0() - alpha * lineDirection0;
00111 return true;
00112 }
00113
00114
00115 bool
00116 checkIntersect(const Ray2D& ray, const LineSegment2D& lineSegment)
00117 {
00118 double dummy0;
00119 Vector2D dummy1;
00120 return checkIntersect(ray, lineSegment, dummy1, dummy0);
00121 }
00122
00123
00124 bool
00125 checkIntersect(const Ray2D& ray, const LineSegment2D& lineSegment,
00126 numeric::Vector2D& intersect)
00127 {
00128 double dummy;
00129 return checkIntersect(ray, lineSegment, intersect, dummy);
00130 }
00131
00132
00133 bool
00134 checkIntersect(const Ray2D& ray, const LineSegment2D& lineSegment,
00135 double& lambda)
00136 {
00137 Vector2D dummy;
00138 return checkIntersect(ray, lineSegment, dummy, lambda);
00139 }
00140
00141
00142 bool
00143 checkIntersect(const Ray2D& ray, const LineSegment2D& lineSegment,
00144 numeric::Vector2D& intersect, double& lambda)
00145 {
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 Vector2D lineDirection =
00169 lineSegment.getVertex0() - lineSegment.getVertex1();
00170 Vector2D const& rayDirection = ray.getDirectionVector();
00171 double const& A_00 = lineDirection.x();
00172 double const& A_01 = rayDirection.x();
00173 double const& A_10 = lineDirection.y();
00174 double const& A_11 = rayDirection.y();
00175
00176
00177 Vector2D bb = lineSegment.getVertex0() - ray.getOrigin();
00178
00179
00180 double alpha;
00181 double beta;
00182 if(!solve2By2LinearSystem(A_00, A_01, A_10, A_11, bb.x(), bb.y(),
00183 alpha, beta)) {
00184 return false;
00185 }
00186
00187
00188
00189 if((alpha < 0.0) || (alpha >= 1.0)) {
00190 return false;
00191 }
00192
00193
00194
00195
00196 if(beta < 0.0) {
00197 return false;
00198 }
00199
00200
00201 lambda = beta;
00202 intersect = ray.getOrigin() + beta * ray.getDirectionVector();
00203 return true;
00204 }
00205
00206
00207 numeric::Vector2D
00208 findClosestPoint(numeric::Vector2D const& point,
00209 Ray2D const& ray)
00210 {
00211 num::Vector2D v1 = point - ray.getOrigin();
00212 double kk = dot(v1, ray.getDirectionVector());
00213 return ray.getOrigin() + kk * ray.getDirectionVector();
00214 }
00215
00216
00217 LineSegment2D
00218 operator*(const numeric::Transform2D& transform,
00219 const LineSegment2D& inputSegment)
00220 {
00221 Vector2D newVertex0 = transform * inputSegment.getVertex0();
00222 Vector2D newVertex1 = transform * inputSegment.getVertex1();
00223 return LineSegment2D(newVertex0, newVertex1);
00224 }
00225
00226
00227 Ray2D
00228 operator*(const numeric::Transform2D& transform,
00229 const Ray2D& inputRay)
00230 {
00231 Vector2D newOrigin = transform * inputRay.getOrigin();
00232 Vector2D newEndpoint =
00233 transform * (inputRay.getOrigin() + inputRay.getDirectionVector());
00234 return Ray2D(newOrigin, newEndpoint - newOrigin, false);
00235 }
00236
00237
00238 }
00239
00240 }