1 /*
2  * Copyright 2013 Daniel Warner <contact@danrw.com>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VECTOR_H_
18 #define VECTOR_H_
19 
20 #include <cmath>
21 #include <iomanip>
22 #include <sstream>
23 #include <string>
24 
25 /**
26  * @brief Generic vector
27  *
28  * Stores x, y, z, w
29  */
30 struct Vector {
31 public:
32     /**
33      * Default constructor
34      */
VectorVector35     Vector() : x(0.0), y(0.0), z(0.0), w(0.0)
36     {
37     }
38 
39     /**
40      * Constructor
41      * @param arg_x x value
42      * @param arg_y y value
43      * @param arg_z z value
44      */
VectorVector45     Vector(const double arg_x, const double arg_y, const double arg_z) : x(arg_x), y(arg_y), z(arg_z), w(0.0)
46     {
47     }
48 
49     /**
50      * Constructor
51      * @param arg_x x value
52      * @param arg_y y value
53      * @param arg_z z value
54      * @param arg_w w value
55      */
VectorVector56     Vector(const double arg_x, const double arg_y, const double arg_z, const double arg_w)
57         : x(arg_x), y(arg_y), z(arg_z), w(arg_w)
58     {
59     }
60 
61     /**
62      * Copy constructor
63      * @param v value to copy from
64      */
VectorVector65     Vector(const Vector &v)
66     {
67         x = v.x;
68         y = v.y;
69         z = v.z;
70         w = v.w;
71     }
72 
73     /**
74      * Destructor
75      */
~VectorVector76     virtual ~Vector()
77     {
78     }
79 
80     /**
81      * Assignment operator
82      * @param v value to copy from
83      */
84     Vector &operator=(const Vector &v)
85     {
86         if (this != &v) {
87             x = v.x;
88             y = v.y;
89             z = v.z;
90             w = v.w;
91         }
92         return *this;
93     }
94 
95     /**
96      * Subtract operator
97      * @param v value to suctract from
98      */
99     Vector operator-(const Vector &v)
100     {
101         return Vector(x - v.x, y - v.y, z - v.z, 0.0);
102     }
103 
104     /**
105      * Calculates the magnitude of the vector
106      * @returns magnitude of the vector
107      */
MagnitudeVector108     double Magnitude() const
109     {
110         return sqrt(x * x + y * y + z * z);
111     }
112 
113     /**
114      * Calculates the dot product
115      * @returns dot product
116      */
DotVector117     double Dot(const Vector &vec) const
118     {
119         return (x * vec.x) + (y * vec.y) + (z * vec.z);
120     }
121 
122     /**
123      * Converts this vector to a string
124      * @returns this vector as a string
125      */
ToStringVector126     std::string ToString() const
127     {
128         std::stringstream ss;
129         ss << std::right << std::fixed << std::setprecision(3);
130         ss << "X: " << std::setw(9) << x;
131         ss << ", Y: " << std::setw(9) << y;
132         ss << ", Z: " << std::setw(9) << z;
133         ss << ", W: " << std::setw(9) << w;
134         return ss.str();
135     }
136 
137     /** x value */
138     double x;
139     /** y value */
140     double y;
141     /** z value */
142     double z;
143     /** w value */
144     double w;
145 };
146 
147 inline std::ostream &operator<<(std::ostream &strm, const Vector &v)
148 {
149     return strm << v.ToString();
150 }
151 
152 #endif
153