1 /*
2  This file is part of the VRender library.
3  Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
4  Version 1.0.0, released on June 27, 2005.
5 
6  http://artis.imag.fr/Members/Cyril.Soler/VRender
7 
8  VRender is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  VRender is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with VRender; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
23 /****************************************************************************
24 
25  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
26 
27  This file is part of the QGLViewer library version 2.7.2.
28 
29  http://www.libqglviewer.com - contact@libqglviewer.com
30 
31  This file may be used under the terms of the GNU General Public License
32  versions 2.0 or 3.0 as published by the Free Software Foundation and
33  appearing in the LICENSE file included in the packaging of this file.
34  In addition, as a special exception, Gilles Debunne gives you certain
35  additional rights, described in the file GPL_EXCEPTION in this package.
36 
37  libQGLViewer uses dual licensing. Commercial/proprietary software must
38  purchase a libQGLViewer Commercial License.
39 
40  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
41  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42 
43 *****************************************************************************/
44 
45 #ifndef _VRENDER_VECTOR3_H
46 #define _VRENDER_VECTOR3_H
47 
48 #include <stdexcept>
49 
50 #ifndef FLT_MAX
51 # define FLT_MAX 9.99E20f
52 #endif
53 
54 namespace vrender
55 {
56   class NVector3;
57 
58 	class Vector3
59 	{
60 		public:
61 			// ---------------------------------------------------------------------------
62 			//! @name Constant
63 			//@{
64 			static const Vector3 inf;
65 			//@}
66 
67 			// ---------------------------------------------------------------------------
68 			//! @name Constructor(s) and destructor
69 			//@{
70 			Vector3 ();
71 			~Vector3 ();
72 			Vector3 (const Vector3&);
73 			Vector3 (const NVector3&);
74 			Vector3 (double, double, double);
75 
76 			//@}
77 
78 			// ---------------------------------------------------------------------------
79 			//! @name Access methods
80 			//@{
x()81 			inline double  x() const { return _xyz[0]; }
y()82 			inline double  y() const { return _xyz[1]; }
z()83 			inline double  z() const { return _xyz[2]; }
setX(double r)84 			inline void  setX(double r) { _xyz[0] = r; }
setY(double r)85 			inline void  setY(double r) { _xyz[1] = r; }
setZ(double r)86 			inline void  setZ(double r) { _xyz[2] = r; }
setXYZ(double x,double y,double z)87 			inline void  setXYZ (double x,double y,double z) { _xyz[0] = x; _xyz[1] = y; _xyz[2] = z; }
88 			//@}
89 
90 			// ---------------------------------------------------------------------------
91 			//! @name Assignment
92 			//@{
93 			inline Vector3& operator= (const Vector3& u)  { _xyz[0] = u._xyz[0]; _xyz[1] = u._xyz[1]; _xyz[2] = u._xyz[2]; return *this; }
94 			Vector3& operator= (const NVector3& u);
95 			//@}
96 
97 			// ---------------------------------------------------------------------------
98 			//! @name Comparisons
99 			//@{
100 			friend bool operator== (const Vector3&,const Vector3&);
101 			friend bool operator!= (const Vector3&,const Vector3&);
102 			//@}
103 
104 			// ---------------------------------------------------------------------------
105 			//! @name Algebraic operations
106 			//@{
107 			inline Vector3& operator+= (const Vector3& v)
108 			{
109 				_xyz[0] += v._xyz[0];
110 				_xyz[1] += v._xyz[1];
111 				_xyz[2] += v._xyz[2];
112 				return *this;
113 			}
114 
115 			inline Vector3& operator-= (const Vector3& v)
116 			{
117 				_xyz[0] -= v._xyz[0];
118 				_xyz[1] -= v._xyz[1];
119 				_xyz[2] -= v._xyz[2];
120 				return *this;
121 			}
122 
123 			inline Vector3& operator*= (double f) { _xyz[0] *= f; _xyz[1] *= f; _xyz[2] *= f; return *this;}
124 			inline Vector3& operator/= (double f) { _xyz[0] /= f; _xyz[1] /= f; _xyz[2] /= f; return *this;}
125 
126 			static Vector3 mini(const Vector3&,const Vector3&) ;
127 			static Vector3 maxi(const Vector3&,const Vector3&) ;
128 
129 			Vector3& operator-= (const NVector3&);
130 			Vector3& operator+= (const NVector3&);
131 
132 			friend Vector3 operator- (const Vector3& u) { return Vector3(-u[0], -u[1], -u[2]); }
133 
134 			inline Vector3 operator+(const Vector3& u) const
135 			{
136 				return Vector3(_xyz[0]+u._xyz[0],_xyz[1]+u._xyz[1],_xyz[2]+u._xyz[2]);
137 			}
138 			inline Vector3 operator-(const Vector3& u) const
139 			{
140 				return Vector3(_xyz[0]-u._xyz[0],_xyz[1]-u._xyz[1],_xyz[2]-u._xyz[2]);
141 			}
142 
143 			inline double    operator*(const Vector3& u) const
144 			{
145 				return _xyz[0]*u._xyz[0] + _xyz[1]*u._xyz[1] + _xyz[2]*u._xyz[2];
146 			}
147 
148 			inline Vector3 operator^(const Vector3& v) const
149 			{
150 				return Vector3(	_xyz[1]*v._xyz[2] - _xyz[2]*v._xyz[1],
151 											_xyz[2]*v._xyz[0] - _xyz[0]*v._xyz[2],
152 											_xyz[0]*v._xyz[1] - _xyz[1]*v._xyz[0]);
153 			}
154 
155 			Vector3 operator/ (double v) { return Vector3(_xyz[0]/v,_xyz[1]/v,_xyz[2]/v); }
156 			Vector3 operator* (double v) { return Vector3(_xyz[0]*v,_xyz[1]*v,_xyz[2]*v); }
157 
158 			friend Vector3 operator* (double,const Vector3&);
159 			//@}
160 
161 			// ---------------------------------------------------------------------------
162 			//! @name Metrics
163 			//@{
164 			double norm       () const;
165 			double squareNorm () const;
166 			double infNorm    () const; /// Should be used for most comparisons, for efficiency reasons.
167 			//@}
168 			// ---------------------------------------------------------------------------
169 			//! @name Stream overrides
170 			//@{
171 			friend std::ostream& operator<< (std::ostream&,const Vector3&);
172 			//@}
173 
174 			double  operator[] (int i) const
175 			{
176 				if((i < 0)||(i > 2))
177 					throw std::runtime_error("Out of bounds in Vector3::operator[]") ;
178 
179 				return _xyz[i];
180 			}
181 
182 			double& operator[] (int i)
183 			{
184 				if((i < 0)||(i > 2))
185 					throw std::runtime_error("Out of bounds in Vector3::operator[]") ;
186 
187 				return _xyz[i];
188 			}
189 
190 		private:
191 			double _xyz[3];  //!< The 3 vector components
192 
193 	}; // interface of Vector3
194 }
195 #endif // _VECTOR3_H
196