Debian Codegraph @

vcg/vcg/space/line2.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
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
27 ****************************************************************************/
28
29
30
31 #ifndef __VCGLIB_LINE2
32 #define __VCGLIB_LINE2
33
34 #include <vcg/space/point2.h>
35
36 namespace vcg {
37
38 /** \addtogroup space */
39 /*@{*/
40 /**
41 Templated class for 2D lines.
42 This is the class for infinite lines in 3D space. A Line is stored just as two Point2:
43 an origin and a direction (not necessarily normalized).
44 @param LineScalarType (template parameter) Specifies the type of scalar used to represent coords.
45 @param NORM: if on, the direction is always Normalized
46 */
47 template <class LineScalarType, bool NORM=false>
48 class Line2
49 {
50 public:
51
52 /// The scalar type
53 typedef LineScalarType ScalarType;
54
55 /// The point type
56 typedef Point2<LineScalarType> PointType;
57
58 /// The line type
59 typedef Line2<LineScalarType,NORM> LineType;
60
61 private:
62
63 /// Origin
64 PointType _ori;
65
66 /// Direction (not necessarily normalized, unless so specified by NORM)
67 PointType _dir;
68
69 public:
70
71 //@{
72 /** @name Members to access the origin or direction
73 Direction() cannot be assigned directly.
74 Use SetDirection() or Set() instead.
75 **/
76 ///
77 inline const PointType &Origin() const { return _ori; }
78 inline PointType &Origin() { return _ori; }
79 inline const PointType &Direction() const { return _dir; }
80 /// sets the origin
81 inline void SetOrigin( const PointType & ori )
82 { _ori=ori; }
83 /// sets the direction
84 inline void SetDirection( const PointType & dir)
85 { _dir=dir; if (NORM) _dir.Normalize(); }
86 /// sets origin and direction.
87 inline void Set( const PointType & ori, const PointType & dir )
88 { SetOrigin(ori); SetDirection(dir); }
89 //@}
90
91 //@{
92 /** @name Constructors
93 **/
94 /// The empty constructor
95 Line2() {};
96 /// The (origin, direction) constructor
97 Line2(const PointType &ori, const PointType &dir) {SetOrigin(ori); SetDirection(dir);};
98 //@}
99
100 /// Operator to compare two lines
101 inline bool operator == ( LineType const & p ) const
102 { return _ori==p._ori && _dir==p._dir; }
103 /// Operator to dispare two lines
104 inline bool operator != ( LineType const & p ) const
105 { return _ori!=p._ori || _dir!=p._dir; }
106 /// Projects a point on the line
107 inline ScalarType Projection( const PointType &p ) const
108 { if (NORM) return ScalarType((p-_ori)*_dir);
109 else return ScalarType((p-_ori)*_dir/_dir.SquaredNorm());
110 }
111 /// returns wheter this type is normalized or not
112 static bool IsNormalized() {return NORM;};
113 /// calculates the point of parameter t on the line.
114 inline PointType P( const ScalarType t ) const
115 { return _ori + _dir * t; }
116 /// normalizes direction field (returns a Normalized Line)
117 inline Line2<ScalarType,true> &Normalize()
118 { if (!NORM) _dir.Normalize(); return *((Line2<ScalarType,true>*)this);}
119 /// normalizes direction field (returns a Normalized Line) - static version
120 static Line2<ScalarType,true> &Normalize(LineType &p)
121 { p.Normalize(); return *((Line2<ScalarType,true>*)(&p));}
122 /// importer for different line types (with any scalar type or normalization beaviour)
123 template <class Q, bool K>
124 inline void Import( const Line2<Q,K> & b )
125 { _ori.Import( b.Origin() ); _dir.Import( b.Direction() );
126 if ((NORM) && (!K)) _dir.Normalize();
127 //printf("(=)%c->%c ",(!NORM)?'N':'n', NORM?'N':'n');
128 }
129 /// constructs a new line importing it from an existing one
130 template <class Q, bool K>
131 static LineType Construct( const Line2<Q,K> & b )
132 { LineType res; res.Import(b); return res;
133 }
134 PointType ClosestPoint(const PointType & p) const{
135 return P(Projection(p));
136 }
137 /// flips the line
138 inline void Flip(){
139 _dir=-_dir;
140 };
141
142 //@{
143 /** @name Linearity for 3d lines
144 (operators +, -, *, /) so a line can be set as a linear combination
145 of several lines. Note that the result of any operation returns
146 a non-normalized line; however, the command r0 = r1*a + r2*b is licit
147 even if r0,r1,r2 are normalized lines, as the normalization will
148 take place within the final assignement operation.
149 **/
150 inline Line2<ScalarType,false> operator + ( LineType const & p) const
151 {return Line2<ScalarType,false> ( _ori+p.Origin(), _dir+p.Direction() );}
152 inline Line2<ScalarType,false> operator - ( LineType const & p) const
153 {return Line2<ScalarType,false> ( _ori-p.Origin(), _dir-p.Direction() );}
154 inline Line2<ScalarType,false> operator * ( const ScalarType s ) const
155 {return Line2<ScalarType,false> ( _ori*s, _dir*s );}
156 inline Line2<ScalarType,false> operator / ( const ScalarType s ) const
157 {ScalarType s0=((ScalarType)1.0)/s; return LineType( _ori*s0, _dir*s0 );}
158 //@}
159
160
161 //@{
162 /** @name Automatic normalized to non-normalized
163 "Line3dN r0 = r1" is equivalent to
164 "Line3dN r0 = r1.Normalize()" if r1 is a Line3d
165 **/
166 /// copy constructor that takes opposite beaviour
167 Line2 (const Line2<ScalarType,!NORM > &r)
168 { Import(r); };
169 /// assignment
170 inline LineType & operator = ( Line2<ScalarType,!NORM> const &r)
171 { Import(r); return *this; };
172 //@}
173
174 }; // end class definition
175
176 typedef Line2<short> Line2s;
177 typedef Line2<int> Line2i;
178 typedef Line2<float> Line2f;
179 typedef Line2<double> Line2d;
180
181 typedef Line2<short ,true> Line2sN;
182 typedef Line2<int ,true> Line2iN;
183 typedef Line2<float ,true> Line2fN;
184 typedef Line2<double,true> Line2dN;
185
186 /// returns closest point
187 template <class ScalarType, bool NORM>
188 Point2<ScalarType> ClosestPoint( Line2<ScalarType,NORM> l, const Point2<ScalarType> & p)
189 {
190 return l.P(l.Projection(p));
191 }
192
193 template <class ScalarType, bool NORM>
194 ScalarType Distance(const Line2<ScalarType, NORM> &l,
195 const Point2<ScalarType> &p) {
196 Point2<ScalarType> o = l.ClosestPoint(p);
197 return (o - p).Norm();
198 }
199
200 template <class ScalarType, bool NORM>
201 vcg::Point2<ScalarType> Mirror(const vcg::Line2<ScalarType, NORM> &l,
202 const vcg::Point2<ScalarType> &p)
203 {
204 vcg::Point2<ScalarType> nearest=vcg::ClosestPoint<ScalarType,NORM>(l,p);
205 vcg::Point2<ScalarType> dir=(nearest - p);
206 nearest+=dir;
207 return nearest;
208 }
209
210 /*@}*/
211
212 } // end namespace
213 #endif