Library of Assembled Shared Sources
line_segment_3d.inl
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
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_LINE_SEGMENT_3D_INL
47
48#include "line_segment_3d.h"
49
50
51
52namespace lass
53{
54
55namespace prim
56{
57
58template <typename T, class PP>
59LineSegment3D<T, PP>::LineSegment3D():
60 tail_(),
61 head_()
62{
63 LASS_ASSERT(tail_.isZero());
64 LASS_ASSERT(head_.isZero());
65}
66
67
68
69template <typename T, class PP>
70LineSegment3D<T, PP>::LineSegment3D(const TPoint& iTail, const TPoint& iHead):
71 tail_(iTail),
72 head_(iHead)
73{
74}
75
76
77
78template <typename T, class PP>
79template <typename PP2>
80LineSegment3D<T, PP>::LineSegment3D(const LineSegment3D<T, PP2>& iOther):
81 tail_(iOther.tail()),
82 head_(iOther.head())
83{
84}
85
86
87
88template <typename T, class PP> inline
89const typename LineSegment3D<T, PP>::TPoint&
90LineSegment3D<T, PP>::tail() const
91{
92 return tail_;
93}
94
95
96
97template <typename T, class PP> inline
98typename LineSegment3D<T, PP>::TPoint&
99LineSegment3D<T, PP>::tail()
100{
101 return tail_;
102}
103
104
105
106template <typename T, class PP> inline
107const typename LineSegment3D<T, PP>::TPoint&
108LineSegment3D<T, PP>::head() const
110 return head_;
113
115template <typename T, class PP> inline
116typename LineSegment3D<T, PP>::TPoint&
117LineSegment3D<T, PP>::head()
118{
119 return head_;
120}
121
122
123
124/** Return point on ray by it's parameter.
125 * @return origin + t * direction
126 */
127template <typename T, class PP>
128const typename LineSegment3D<T, PP>::TPoint
130{
131 TParameterPolicy::enforceRange(iT, TNumTraits::zero, TNumTraits::one);
132 return tail_ + iT * vector();
133}
134
135
136
137/** Return parameter of @e projection of @a iPoint on line segment.
138 * @warning the result can be out of bound [0, 1] regardless the parameter policy used.
139 */
140template <typename T, class PP>
141const typename LineSegment3D<T, PP>::TValue
142LineSegment3D<T, PP>::t(const TPoint& iPoint) const
143{
144 const TVector v = vector();
145 const TValue t1 = dot(iPoint - tail_, v);
146 const TValue t2 = -dot(iPoint - head_, v);
147 const TValue t = std::max(t1,t2) / (t1 + t2);
148 return t1 > t2 ? t : TNumTraits::one - t;
149}
150
151/** Return vector from tail to head.
152 */
153template <typename T, class PP>
154const typename LineSegment3D<T, PP>::TVector
156{
157 return head_ - tail_;
158}
159
160
161
162/** Return length of line segment.
163 */
164template <typename T, class PP>
165const typename LineSegment3D<T, PP>::TValue
167{
168 const TVector v = vector();
169 return v.norm();
170}
171/** t is parameter of closests point and return squared distance of a point to the line segment.
172*/
173template <typename T, class PP>
174const typename LineSegment3D<T, PP>::TValue
175LineSegment3D<T, PP>::closestsPoint(const TPoint &iPoint, T &oT) const
176{
177 oT = this->t(iPoint);
178 if(oT < 0)
179 {
180 oT = 0.0;
181 return (iPoint - tail_).squaredNorm();
182 }
183 if(oT > 1)
184 {
185 oT = 1.0;
186 return (iPoint - head_).squaredNorm();
187 }
188 return (iPoint - this->point(oT)).squaredNorm();
189}
190
191/** Return squared distance of a point to line segment.
192 */
193template <typename T, class PP>
194const typename LineSegment3D<T, PP>::TValue
195LineSegment3D<T, PP>::squaredDistance(const TPoint& iPoint) const
196{
197 TParam t = this->t(iPoint);
198 if(t < 0)
199 return (iPoint - tail_).squaredNorm();
200 if(t > 1)
201 return (iPoint - head_).squaredNorm();
202 return (iPoint - this->point(t)).squaredNorm();
203}
204
205/** Return distance of point to line segment.
206 */
207template <typename T, class PP>
208const typename LineSegment3D<T, PP>::TValue
209LineSegment3D<T, PP>::distance(const TPoint& iPoint) const
210{
211 TValue squaredDistance = this->squaredDistance(iPoint);
212 return lass::num::sqrt(squaredDistance);
213}
214
215/** @relates lass::prim::LineSegment3D
216 */
217template <typename T, class PPa, class PPb> bool operator==(const LineSegment3D<T, PPa>& iA, const LineSegment3D<T, PPb>& iB)
218{
219 return iA.tail()==iB.tail() && iA.head()==iB.head();
220}
221
222/** @relates lass::prim::LineSegment3D
223 */
224template <typename T, class PPa, class PPb> bool operator!=(const LineSegment3D<T, PPa>& iA, const LineSegment3D<T, PPb>& iB)
225{
226 return !(iA==iB);
227}
228
229
230/** @relates lass::prim::LineSegment3D
231 */
232template<typename T, class PP>
233std::ostream& operator<<(std::ostream& ioOStream, const LineSegment3D<T, PP>& iLineSegment)
234{
235 LASS_ENFORCE_STREAM(ioOStream) << "{T=" << iLineSegment.tail() << ", H=" << iLineSegment.head() << "}";
236 return ioOStream;
237}
238
239
240
241/** @relates lass::prim::LineSegment3D
242 */
243template<typename T, class PP>
244io::XmlOStream& operator<<(io::XmlOStream& ioOStream, const LineSegment3D<T, PP>& iLineSegment)
245{
246 LASS_ENFORCE_STREAM(ioOStream)
247 << "<LineSegment3D>\n"
248 << "<tail>" << iLineSegment.tail() << "</tail>\n"
249 << "<head>" << iLineSegment.head() << "</head>\n"
250 << "</LineSegment3D>\n";
251
252 return ioOStream;
253}
254
255
256
257}
258
259}
260
261#endif
const TValue t(const TPoint &iPoint) const
Return parameter of projection of iPoint on line segment.
const TValue length() const
Return length of line segment.
const TVector vector() const
Return vector from tail to head.
const TValue distance(const TPoint &iPoint) const
Return distance of point to line segment.
const TPoint point(TParam iT) const
Return point on ray by it's parameter.
const TValue closestsPoint(const TPoint &iPoint, T &oT) const
t is parameter of closests point and return squared distance of a point to the line segment.
const TValue squaredDistance(const TPoint &iPoint) const
Return squared distance of a point to line segment.
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
const TValue norm() const
Return norm of vector.