vec4.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2014 Damien Rohmer
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 
22 #ifndef VEC4_HPP
23 #define VEC4_HPP
24 
25 #include <string>
26 #include <iostream>
27 
28 
29 
30 namespace cpe
31 {
32 class vec3;
33 class vec2;
34 
36 class vec4
37 {
38 public:
39 
40  // ********************************************* //
41  // ********************************************* //
42  // CONSTRUCTORS
43  // ********************************************* //
44  // ********************************************* //
45 
47  vec4();
49  vec4(float x,float y,float z,float w);
50 
51  // ********************************************* //
52  // ********************************************* //
53  // Get/set
54  // ********************************************* //
55  // ********************************************* //
56 
58  float x() const;
60  float& x();
62  float y() const;
64  float& y();
66  float z() const;
68  float& z();
70  float w() const;
72  float& w();
73 
75  float operator[](const size_t& k) const;
77  float& operator[](const size_t& k);
79  float operator()(const size_t& k) const;
81  float& operator()(const size_t& k);
82 
84  void set_zero();
85 
88  const float *pointer() const;
89 
90  // ********************************************* //
91  // ********************************************* //
92  // Convert to lower dimension
93  // ********************************************* //
94  // ********************************************* //
95 
97  vec3 to_vec3() const;
99  vec2 to_vec2() const;
100 
101 
102 
103  // ********************************************* //
104  // ********************************************* //
105  // Math operation
106  // ********************************************* //
107  // ********************************************* //
108 
110  float dot(const vec4& p) const;
111 
113  float norm() const;
115  float norm2() const;
117  vec4 normalized() const;
118 
119 
120 
121 
122 
123 
124  // ********************************************* //
125  // ********************************************* //
126  // Operator +-*/
127  // ********************************************* //
128  // ********************************************* //
129 
131  vec4 operator+(const vec4& p2) const;
133  vec4 operator+(float s) const;
134 
136  vec4 operator-(const vec4& p2) const;
138  vec4 operator-(float s) const;
139 
141  vec4 operator*(float s) const;
143  vec4 operator/(float s) const;
144 
146  vec4& operator+=(const vec4& p);
148  vec4& operator+=(float s);
150  vec4& operator-=(const vec4& p);
152  vec4& operator-=(float s);
154  vec4& operator*=(float s);
156  vec4& operator/=(float s);
157 
159  vec4 operator-() const;
160 
164  vec4 product_compontentwise(const vec4& p) const;
171  void scale(float sx,float sy,float sz,float sw);
172 
173 
174  // ********************************************* //
175  // ********************************************* //
176  // Output
177  // ********************************************* //
178  // ********************************************* //
179 
181  std::string to_string() const;
182 
183 
184 
185 private:
186 
187 
188  // ********************************************* //
189  // ********************************************* //
190  // Internal parameters
191  // ********************************************* //
192  // ********************************************* //
193 
195  float internal_x;
197  float internal_y;
199  float internal_z;
201  float internal_w;
202 
203 
204  // ********************************************* //
205  // ********************************************* //
206  // Helper
207  // ********************************************* //
208  // ********************************************* //
209 
211  void assert_size(const size_t& k) const;
212 
213 };
214 
216 vec4 operator+(float s,const vec4& p);
218 vec4 operator-(float s,const vec4& p);
220 vec4 operator*(float s,const vec4& p);
221 
222 // ********************************************* //
223 // ********************************************* //
224 // Output
225 // ********************************************* //
226 // ********************************************* //
227 
229 std::ostream& operator<<(std::ostream& stream,const vec4& p);
230 
231 
232 
233 }
234 
235 
236 
237 #endif
vec4 operator/(float s) const
divide by a scalar operator
Definition: vec4.cpp:192
float w() const
get w coordinate
Definition: vec4.cpp:66
vec4()
empty constructor
Definition: vec4.cpp:35
vec4 & product_compontentwise_internal(const vec4 &p)
does componentwise mutliplication
Definition: vec4.cpp:269
float x() const
get x coordinate
Definition: vec4.cpp:37
void scale(float sx, float sy, float sz, float sw)
internal scaling (similar to componentwise)
Definition: vec4.cpp:307
float internal_z
z coordinate
Definition: vec4.hpp:199
float internal_x
x coordinate
Definition: vec4.hpp:195
vec4 & operator-=(const vec4 &p)
internal -
Definition: vec4.cpp:218
vec3 to_vec3() const
convert to v3 (x,y,z)
Definition: vec4.cpp:315
matrix1x4 operator-(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:82
float norm() const
get the norm of the vector
Definition: vec4.cpp:137
matrix1x4 operator+(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:61
float operator()(const size_t &k) const
Access to the k_th entry (k in [0,3])
Definition: vec4.cpp:114
float y() const
get y coordinate
Definition: vec4.cpp:47
const float * pointer() const
fast pointer access
Definition: vec4.cpp:318
vec4 & operator/=(float s)
internal /
Definition: vec4.cpp:246
vec4 operator+(const vec4 &p2) const
operator
Definition: vec4.cpp:148
vec4 product_compontentwise(const vec4 &p) const
does componentwise mutliplication
Definition: vec4.cpp:264
float internal_y
y coordinate
Definition: vec4.hpp:197
float operator[](const size_t &k) const
Access to the k_th entry (k in [0,3])
Definition: vec4.cpp:76
vec2 to_vec2() const
convert to v2 (x,y)
Definition: vec4.cpp:316
matrix1x4 operator*(float s, const matrix1x4 &m)
multiply by a scalar operator
Definition: matrix1x4.cpp:93
void assert_size(const size_t &k) const
assert that a size_t belongs to [[0,2]]
Definition: vec4.cpp:289
float internal_w
w coordinate
Definition: vec4.hpp:201
std::ostream & operator<<(std::ostream &stream, const matrix1x4 &_m)
output the vector in ostream
Definition: matrix1x4.cpp:182
Vectors/Points 2D.
Definition: vec2.hpp:34
vec4 normalized() const
normalize the vector to unit length
Definition: vec4.cpp:298
vec4 & operator+=(const vec4 &p)
internal +
Definition: vec4.cpp:200
Vectors/Points 3D.
Definition: vec3.hpp:36
std::string to_string() const
export the value as string cout<<v4(2,3,6,7) => 2 3 6 7
Definition: vec4.cpp:277
void set_zero()
set every entry to 0
Definition: vec4.cpp:124
float z() const
get z coordinate
Definition: vec4.cpp:57
vec4 operator-() const
unary negation
Definition: vec4.cpp:259
float norm2() const
get the square norm of the vector
Definition: vec4.cpp:142
Vectors/Points 4D.
Definition: vec4.hpp:36
float dot(const vec4 &p) const
perform dot product between two v4
Definition: vec4.cpp:132
vec4 operator*(float s) const
multiply by a scalar operator
Definition: vec4.cpp:185
vec4 & operator*=(float s)
internal *
Definition: vec4.cpp:237