Library of Assembled Shared Sources
line_segment_3d_ray_3d.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_RAY_3D_H
44#define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_RAY_3D_H
45
46#include "prim_common.h"
47#include "line_segment_3d.h"
48#include "ray_3d.h"
49
50namespace lass
51{
52namespace prim
53{
54
55
56template<typename T, class PP1, class NP2, class PP2>
57T distance(
58 const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray,
59 const T& tMin = T())
60{
61 return num::sqrt(squaredDistance(lineSegment, ray, tMin));
62}
63
64template<typename T, class PP1, class NP2, class PP2>
65T squaredDistance(
66 const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray,
67 const T& tMin = T())
68{
69 typedef typename LineSegment3D<T, PP1>::TValue TValue;
70 typedef Point3D<TValue> TPoint;
71 typedef Vector3D<TValue> TVector;
72
73 const TPoint S1 = ray.support();
74 const TVector D = ray.direction();
75 const TPoint R = lineSegment.tail();
76 const TVector E = lineSegment.vector();
77
78 TVector SR = R - S1;
79 const TVector N = cross(D,E);
80 if(N.squaredNorm() == 0)
81 return squaredDistance(ray.project(R), R);
82 const TPoint S = S1 + N.project(SR);
83 SR = R - S;
84
85 TValue tRay, tSeg;
86
87 // cramer
88 if(num::abs(N.z) > num::abs(N.x) &&
89 num::abs(N.z) > num::abs(N.y))
90 {
91 tRay = (SR.x * E.y - SR.y * E.x) / N.z;
92 tSeg = (SR.x * D.y - SR.y * D.x) / N.z;
93 }
94 else if(num::abs(N.x) > num::abs(N.y))
95 {
96 tRay = (SR.y * E.z - SR.z * E.y) / N.x;
97 tSeg = (SR.y * D.z - SR.z * D.y) / N.x;
98 }else
99 {
100 tRay = (SR.z * E.x - SR.x * E.z) / N.y;
101 tSeg = (SR.z * D.x - SR.x * D.z) / N.y;
102 }
103
104 if(tSeg > 1)
105 {
106 TValue tHead = ray.t(lineSegment.head());
107 if(tHead > tMin)
108 return squaredDistance(ray.point(tHead), lineSegment.head());
109 else
110 {
111 tSeg = lineSegment.t(S1);
112 if(tSeg < 0)
113 return squaredDistance(S1, lineSegment.tail());
114 if(tSeg > 1)
115 return squaredDistance(S1, lineSegment.head());
116 return squaredDistance(S1, lineSegment.point(tSeg));
117 }
118 }
119 if(tSeg < 0)
120 {
121 TValue tTail = ray.t(R);
122 if(tTail > tMin)
123 return squaredDistance(ray.point(tTail), R);
124 else
125 {
126 tSeg = lineSegment.t(S1);
127 if(tSeg < 0)
128 return squaredDistance(S1, lineSegment.tail());
129 if(tSeg > 1)
130 return squaredDistance(S1, lineSegment.head());
131 return squaredDistance(S1, lineSegment.point(tSeg));
132 }
133 }
134
135 if(tRay > tMin)
136 return squaredDistance(ray.point(tRay), lineSegment.point(tSeg));
137
138 return lineSegment.squaredDistance(S1);
139}
140
141template<typename T, class PP1, class NP2, class PP2>
142T closestsPoints(
143 const LineSegment3D<T, PP1>& lineSegment, const Ray3D<T, NP2, PP2>& ray,
144 T &tSeg, T &tRay, const T& tMin = T())
145{
146 typedef typename LineSegment3D<T, PP1>::TValue TValue;
147 typedef Point3D<TValue> TPoint;
148 typedef Vector3D<TValue> TVector;
149
150 const TPoint S1 = ray.support();
151 const TVector D = ray.direction();
152 const TPoint R = lineSegment.tail();
153 const TVector E = lineSegment.vector();
154
155 TVector SR = R - S1;
156 const TVector N = cross(D,E);
157 if(N.squaredNorm() == 0)
158 return squaredDistance(ray.project(R), R);
159 const TPoint S = S1 + N.project(SR);
160 SR = R - S;
161
162 // cramer
163 if(num::abs(N.z) > num::abs(N.x) &&
164 num::abs(N.z) > num::abs(N.y))
165 {
166 tRay = (SR.x * E.y - SR.y * E.x) / N.z;
167 tSeg = (SR.x * D.y - SR.y * D.x) / N.z;
168 }
169 else if(num::abs(N.x) > num::abs(N.y))
170 {
171 tRay = (SR.y * E.z - SR.z * E.y) / N.x;
172 tSeg = (SR.y * D.z - SR.z * D.y) / N.x;
173 }else
174 {
175 tRay = (SR.z * E.x - SR.x * E.z) / N.y;
176 tSeg = (SR.z * D.x - SR.x * D.z) / N.y;
177 }
178
179 if(tSeg > 1)
180 {
181 TValue tHead = ray.t(lineSegment.head());
182 if(tHead > tMin)
183 {
184 tRay = tHead;
185 tSeg = 1.0;
186 return squaredDistance(ray.point(tHead), lineSegment.head());
187 }
188 else
189 {
190 tSeg = lineSegment.t(S1);
191 tRay = 0.0;
192 if(tSeg < 0)
193 {
194 tSeg = 0.0;
195 return squaredDistance(S1, lineSegment.tail());
196 }
197 if(tSeg > 1)
198 {
199 tSeg = 1.0;
200 return squaredDistance(S1, lineSegment.head());
201 }
202 return squaredDistance(S1, lineSegment.point(tSeg));
203 }
204 }
205 if(tSeg < 0)
206 {
207 TValue tTail = ray.t(R);
208 if(tTail > tMin)
209 {
210 tRay = tTail;
211 return squaredDistance(ray.point(tTail), R);
212 }
213 else
214 {
215 tSeg = lineSegment.t(S1);
216 tRay = 0.0;
217 if(tSeg < 0)
218 {
219 tSeg = 0.0;
220 return squaredDistance(S1, lineSegment.tail());
221 }
222 if(tSeg > 1)
223 {
224 tSeg = 1.0;
225 return squaredDistance(S1, lineSegment.head());
226 }
227 return squaredDistance(S1, lineSegment.point(tSeg));
228 }
229 }
230
231 if(tRay > tMin)
232 return squaredDistance(ray.point(tRay), lineSegment.point(tSeg));
233
234 tRay = 0.0;
235 return lineSegment.closestsPoint(S1, tSeg);
236}
237
238}
239}
240
241#endif
242
243// EOF
T abs(const T &x)
if x < 0 return -x, else return x.
Definition basic_ops.h:145
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53