Debian Codegraph @

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