1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25
26 $Log: not supported by cvs2svn $
27 Revision 1.8 2004/05/14 03:14:04 ponchio
28 Added Distance
29
30 Revision 1.7 2004/05/10 10:58:35 ganovelli
31 name of the constructor changed from LineType to Line3
32
33 Revision 1.6 2004/03/11 11:47:20 tarini
34 minor updates, corrections, added documentations, etc.
35
36 Revision 1.5 2004/03/10 15:27:48 tarini
37 added Normalized flag
38
39
40 Revision 1.1 2004/03/08 16:15:48 tarini
41 first version (tarini)
42
43 ****************************************************************************/
44
45
46
47 #ifndef __VCGLIB_LINE3
48 #define __VCGLIB_LINE3
49
50 #include <vcg/space/point3.h>
51
52 namespace vcg {
53
54 /** \addtogroup space */
55 /*@{*/
56 /**
57 Templated class for 3D lines.
58 This is the class for infinite lines in 3D space. A Line is stored just as two Point3:
59 an origin and a direction (not necessarily normalized).
60 @param LineScalarType (template parameter) Specifies the type of scalar used to represent coords.
61 @param NORM: if on, the direction is always Normalized
62 */
63 template <class LineScalarType, bool NORM=false>
64 class Line3
65 {
66 public:
67
68 /// The scalar type
69 typedef LineScalarType ScalarType;
70
71 /// The point type
72 typedef Point3<LineScalarType> PointType;
73
74 /// The line type
75 typedef Line3<LineScalarType,NORM> LineType;
76
77 private:
78
79 /// Origin
80 PointType _ori;
81
82 /// Direction (not necessarily normalized, unless so specified by NORM)
83 PointType _dir;
84
85 public:
86
87 //@{
88 /** @name Members to access the origin or direction
89 Direction() cannot be assigned directly.
90 Use SetDirection() or Set() instead.
91 **/
92 ///
Origin()93 inline const PointType &Origin() const { return _ori; }
Origin()94 inline PointType &Origin() { return _ori; }
Direction()95 inline const PointType &Direction() const { return _dir; }
96 /// sets the origin
SetOrigin(const PointType & ori)97 inline void SetOrigin( const PointType & ori )
98 { _ori=ori; }
99 /// sets the direction
SetDirection(const PointType & dir)100 inline void SetDirection( const PointType & dir)
101 { _dir=dir; if (NORM) _dir.Normalize(); }
102 /// sets origin and direction.
Set(const PointType & ori,const PointType & dir)103 inline void Set( const PointType & ori, const PointType & dir )
104 { SetOrigin(ori); SetDirection(dir); }
105 //@}
106
107 //@{
108 /** @name Constructors
109 **/
110 /// The empty constructor
Line3()111 Line3() {};
112 /// The (origin, direction) constructor
Line3(const PointType & ori,const PointType & dir)113 Line3(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
114 //@}
115
116 /// Operator to compare two lines
117 inline bool operator == ( LineType const & p ) const
118 { return _ori==p._ori && _dir==p._dir; }
119 /// Operator to dispare two lines
120 inline bool operator != ( LineType const & p ) const
121 { return _ori!=p._ori || _dir!=p._dir; }
122 /// Projects a point on the line
Projection(const PointType & p)123 inline ScalarType Projection( const PointType &p ) const
124 { if (NORM) return ScalarType((p-_ori).dot(_dir));
125 else return ScalarType((p-_ori).dot(_dir)/_dir.SquaredNorm());
126 }
127 /// returns wheter this type is normalized or not
IsNormalized()128 static bool IsNormalized() {return NORM;};
129 /// calculates the point of parameter t on the line.
P(const ScalarType t)130 inline PointType P( const ScalarType t ) const
131 { return _ori + _dir * t; }
132 /// normalizes direction field (returns a Normalized Line)
Normalize()133 inline Line3<ScalarType,true> &Normalize()
134 { if (!NORM) _dir.Normalize(); return *((Line3<ScalarType,true>*)this);}
135 /// normalizes direction field (returns a Normalized Line) - static version
Normalize(LineType & p)136 static Line3<ScalarType,true> &Normalize(LineType &p)
137 { p.Normalize(); return *((Line3<ScalarType,true>*)(&p));}
138 /// importer for different line types (with any scalar type or normalization beaviour)
139 template <class Q, bool K>
Import(const Line3<Q,K> & b)140 inline void Import( const Line3<Q,K> & b )
141 { _ori.Import( b.Origin() ); _dir.Import( b.Direction() );
142 if ((NORM) && (!K)) _dir.Normalize();
143 //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n');
144 }
145 /// constructs a new line importing it from an existing one
146 template <class Q, bool K>
Construct(const Line3<Q,K> & b)147 static LineType Construct( const Line3<Q,K> & b )
148 { LineType res; res.Import(b); return res;
149 }
ClosestPoint(const PointType & p)150 PointType ClosestPoint(const PointType & p) const{
151 return P(Projection(p));
152 }
153 /// flips the line
Flip()154 inline void Flip(){
155 _dir=-_dir;
156 };
157
158 //@{
159 /** @name Linearity for 3d lines
160 (operators +, -, *, /) so a line can be set as a linear combination
161 of several lines. Note that the result of any operation returns
162 a non-normalized line; however, the command r0 = r1*a + r2*b is licit
163 even if r0,r1,r2 are normalized lines, as the normalization will
164 take place within the final assignement operation.
165 **/
166 inline Line3<ScalarType,false> operator + ( LineType const & p) const
167 {return Line3<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
168 inline Line3<ScalarType,false> operator - ( LineType const & p) const
169 {return Line3<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
170 inline Line3<ScalarType,false> operator * ( const ScalarType s ) const
171 {return Line3<ScalarType,false> ( _ori*s, _dir*s );}
172 inline Line3<ScalarType,false> operator / ( const ScalarType s ) const
173 {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );}
174 //@}
175
176
177 //@{
178 /** @name Automatic normalized to non-normalized
179 "Line3dN r0 = r1" is equivalent to
180 "Line3dN r0 = r1.Normalize()" if r1 is a Line3d
181 **/
182 /// copy constructor that takes opposite beaviour
Line3(const Line3<ScalarType,!NORM> & r)183 Line3 (const Line3<ScalarType,!NORM > &r)
184 { Import(r); };
185 /// assignment
186 inline LineType & operator = ( Line3<ScalarType,!NORM> const &r)
187 { Import(r); return *this; };
188 //@}
189
190 }; // end class definition
191
192 typedef Line3<short> Line3s;
193 typedef Line3<int> Line3i;
194 typedef Line3<float> Line3f;
195 typedef Line3<double> Line3d;
196
197 typedef Line3<short ,true> Line3sN;
198 typedef Line3<int ,true> Line3iN;
199 typedef Line3<float ,true> Line3fN;
200 typedef Line3<double,true> Line3dN;
201
202 /// returns closest point
203 template <class ScalarType, bool NORM>
ClosestPoint(Line3<ScalarType,NORM> l,const Point3<ScalarType> & p)204 Point3<ScalarType> ClosestPoint( Line3<ScalarType,NORM> l, const Point3<ScalarType> & p)
205 {
206 return l.P(l.Projection(p));
207 }
208
209 template <class ScalarType, bool NORM>
Distance(const Line3<ScalarType,NORM> & l,const Point3<ScalarType> & p)210 ScalarType Distance(const Line3<ScalarType, NORM> &l,
211 const Point3<ScalarType> &p) {
212 Point3<ScalarType> o = l.ClosestPoint(p);
213 return (o - p).Norm();
214 }
215
216 /*@}*/
217
218 } // end namespace
219 #endif
220