1 /*
2  * CRRCsim - the Charles River Radio Control Club Flight Simulator Project
3  *   Copyright (C) 2005, 2008 - Jens Wilhelm Wulf (original author)
4  *   Copyright (C) 2008 - Jan Reucker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  */
21 #ifndef VECTOR3_H
22 # define VECTOR3_H
23 
24 # include <math.h>
25 # include <string>
26 
27 namespace CRRCMath
28 {
29   /**
30    * A vector with one column and three rows of double values.
31    *
32    * @author Jens Wilhelm Wulf
33    */
34   class Vector3
35   {
36     public:
37      /**
38       * Rows 0, 1 and 2.
39       */
40      double r[3];
41 
42     public:
43 
Vector3()44      Vector3()
45      {
46        r[0] = r[1] = r[2] = 0;
47      }
48 
Vector3(const Vector3 & v)49      Vector3(const Vector3& v)
50      {
51        r[0] = v.r[0];
52        r[1] = v.r[1];
53        r[2] = v.r[2];
54      }
55 
Vector3(double i0,double i1,double i2)56      Vector3(double i0, double i1, double i2)
57      {
58        r[0] = i0;
59        r[1] = i1;
60        r[2] = i2;
61      };
62 
63      /**
64       * Operator: Zuweisung
65       * \todo operator= not first checking for assignment to this
66       */
67      Vector3& operator=(const Vector3& b)
68      {
69        r[0] = b.r[0];
70        r[1] = b.r[1];
71        r[2] = b.r[2];
72        return *this;
73      }
74 
75      /**
76       * Operator: Scalarprodukt
77       * Wichtig: n*Vector3 geht nicht, es muss Vector3*n benutzt werden!
78       */
79      Vector3 operator*(const double scalar) const
80      {
81        return Vector3(scalar*r[0], scalar*r[1], scalar*r[2]);
82      }
83 
84      /**
85       * Operator: Kreuzprodukt
86       */
87      Vector3 operator*(const Vector3& V) const
88      {
89        return Vector3( r[1] * V.r[2] - r[2] * V.r[1],
90                        r[2] * V.r[0] - r[0] * V.r[2],
91                        r[0] * V.r[1] - r[1] * V.r[0] );
92      }
93 
94      /**
95       * multiply element-wise
96       */
mul(const Vector3 & V)97      Vector3 mul(const Vector3& V) const
98      {
99        return Vector3(r[0] * V.r[0],
100                       r[1] * V.r[1],
101                       r[2] * V.r[2]);
102      }
103 
104 
105      /**
106       * Operator: Summe
107       */
108      Vector3 operator+(const Vector3& V) const
109      {
110        return Vector3( r[0] + V.r[0],
111                        r[1] + V.r[1],
112                        r[2] + V.r[2] );
113      }
114 
115      /**
116       * Operator: Differenz
117       */
118      Vector3 operator-(const Vector3& V) const
119      {
120        return Vector3( r[0] - V.r[0],
121                        r[1] - V.r[1],
122                        r[2] - V.r[2] );
123      }
124 
125      /**
126       * Operator: Addition
127       */
128      Vector3 operator+=(const Vector3& V)
129      {
130        r[0] += V.r[0];
131        r[1] += V.r[1];
132        r[2] += V.r[2];
133 
134        return *this;
135      }
136 
137      /**
138       * Operator: Multiplication
139       */
140      Vector3 operator*=(const double scalar)
141      {
142        r[0] *= scalar;
143        r[1] *= scalar;
144        r[2] *= scalar;
145 
146        return *this;
147      }
148 
149     /** \brief Calculate the scalar product / inner product.
150      *
151      *  This operator calculates the scalar product of two vectors.
152      *
153      *  \return A value representing the area of the parallelogram
154      *          defined by the two vectors.
155      */
inner(Vector3 const & rhs)156       double inner(Vector3 const& rhs) const
157       {
158         return (r[0] * rhs.r[0] + r[1] * rhs.r[1] + r[2] * rhs.r[2]);
159       }
160 
161      /**
162       * phi is the angle between both of the vectors and
163       * this method returns ( cos(phi) )^2
164       */
angle_cos_sqr(Vector3 const & rhs)165       double angle_cos_sqr(Vector3 const& rhs)
166       {
167         double cos_phi_sqr = (this->inner(rhs))*(this->inner(rhs))
168                               / ( (this->inner(*this)) * (rhs.inner(rhs)) );
169         return(cos_phi_sqr);
170       }
171 
172 
173      /**
174       * returns length of vector
175       */
176      double length() const;
177 
178      /**
179       * normalize vector to unit length
180       *
181       * The vector is divided by its length to transform it into
182       * a vector of unit length that points into the same direction.
183       *
184       * \return const reference to the normalized vector
185       */
186       Vector3 const& normalize();
187 
188      void print(std::string pre, std::string post) const;
189   };
190 }
191 #endif
192