vec3.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 
23 #ifndef VEC3_HPP
24 #define VEC3_HPP
25 
26 #include <string>
27 #include <iostream>
28 
29 
30 
31 namespace cpe
32 {
33 class vec2;
34 
36 class vec3
37 {
38 public:
39 
40  // ********************************************* //
41  // ********************************************* //
42  // CONSTRUCTORS
43  // ********************************************* //
44  // ********************************************* //
45 
47  vec3();
49  vec3(float x,float y,float z);
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();
69 
71  float operator[](const size_t& k) const;
73  float& operator[](const size_t& k);
75  float operator()(const size_t& k) const;
77  float& operator()(const size_t& k);
78 
80  void set_zero();
81 
84  const float *pointer() const;
85 
86  // ********************************************* //
87  // ********************************************* //
88  // Convert to lower dimension
89  // ********************************************* //
90  // ********************************************* //
91 
93  vec2 to_vec2() const;
94 
95 
96  // ********************************************* //
97  // ********************************************* //
98  // Math operation
99  // ********************************************* //
100  // ********************************************* //
101 
103  float dot(const vec3& p) const;
104 
106  float norm() const;
108  float norm2() const;
110  vec3 normalized() const;
111 
113  vec3 cross(const vec3& p) const;
114 
115 
116 
117 
118  // ********************************************* //
119  // ********************************************* //
120  // Operator +-*/
121  // ********************************************* //
122  // ********************************************* //
123 
125  vec3 operator+(const vec3& p2) const;
127  vec3 operator+(float s) const;
128 
130  vec3 operator-(const vec3& p2) const;
132  vec3 operator-(float s) const;
133 
134 
136  vec3 operator*(float s) const;
138  vec3 operator/(float s) const;
139 
141  vec3& operator+=(const vec3& p);
143  vec3& operator+=(float s);
145  vec3& operator-=(const vec3& p);
147  vec3& operator-=(float s);
149  vec3& operator*=(float s);
151  vec3& operator/=(float s);
152 
154  vec3 operator-() const;
155 
159  vec3 product_compontentwise(const vec3& p) const;
166  void scale(float sx,float sy,float sz);
167 
168 
169  // ********************************************* //
170  // ********************************************* //
171  // Output
172  // ********************************************* //
173  // ********************************************* //
174 
176  std::string to_string() const;
177 
178 
179 
180 private:
181 
182 
183  // ********************************************* //
184  // ********************************************* //
185  // Internal parameters
186  // ********************************************* //
187  // ********************************************* //
188 
190  float internal_x;
192  float internal_y;
194  float internal_z;
195 
196 
197  // ********************************************* //
198  // ********************************************* //
199  // Helper
200  // ********************************************* //
201  // ********************************************* //
202 
204  void assert_size(const size_t& k) const;
205 
206 };
207 
209 vec3 operator+(float s,const vec3& p);
211 vec3 operator-(float s,const vec3& p);
213 vec3 operator*(float s,const vec3& p);
214 
215 // ********************************************* //
216 // ********************************************* //
217 // Output
218 // ********************************************* //
219 // ********************************************* //
220 
222 std::ostream& operator<<(std::ostream& stream,const vec3& p);
223 
224 
225 }
226 
227 
228 
229 #endif
void scale(float sx, float sy, float sz)
internal scaling (similar to componentwise)
Definition: vec3.cpp:290
float internal_x
x coordinate
Definition: vec3.hpp:190
float dot(const vec3 &p) const
perform dot product between two v3
Definition: vec3.cpp:118
vec3 operator+(const vec3 &p2) const
operator
Definition: vec3.cpp:140
float x() const
get x coordinate
Definition: vec3.cpp:38
vec3 operator*(float s) const
multiply by a scalar operator
Definition: vec3.cpp:179
vec2 to_vec2() const
convert to v2 (x,y)
Definition: vec3.cpp:297
float z() const
get z coordinate
Definition: vec3.cpp:58
vec3 operator/(float s) const
divide by a scalar operator
Definition: vec3.cpp:186
vec3 operator-() const
unary negation
Definition: vec3.cpp:243
vec3 product_compontentwise(const vec3 &p) const
does componentwise mutliplication
Definition: vec3.cpp:248
vec3 & operator+=(const vec3 &p)
internal +
Definition: vec3.cpp:195
matrix1x4 operator-(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:82
matrix1x4 operator+(float s, const matrix1x4 &m)
operator
Definition: matrix1x4.cpp:61
float norm2() const
get the square norm of the vector
Definition: vec3.cpp:128
float internal_z
z coordinate
Definition: vec3.hpp:194
vec3 & operator-=(const vec3 &p)
internal -
Definition: vec3.cpp:211
void set_zero()
set every entry to 0
Definition: vec3.cpp:111
vec3 & product_compontentwise_internal(const vec3 &p)
does componentwise mutliplication
Definition: vec3.cpp:253
float internal_y
y coordinate
Definition: vec3.hpp:192
float operator[](const size_t &k) const
Access to the k_th entry (k in [0,2])
Definition: vec3.cpp:68
std::string to_string() const
export the value as string cout<<v3(2,3,6) => 2 3 6
Definition: vec3.cpp:260
float y() const
get y coordinate
Definition: vec3.cpp:48
matrix1x4 operator*(float s, const matrix1x4 &m)
multiply by a scalar operator
Definition: matrix1x4.cpp:93
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
float norm() const
get the norm of the vector
Definition: vec3.cpp:123
vec3()
empty constructor
Definition: vec3.cpp:36
vec3 & operator/=(float s)
internal /
Definition: vec3.cpp:235
Vectors/Points 3D.
Definition: vec3.hpp:36
vec3 & operator*=(float s)
internal *
Definition: vec3.cpp:227
vec3 cross(const vec3 &p) const
compute cross product with an other vector
Definition: vec3.cpp:133
float operator()(const size_t &k) const
Access to the k_th entry (k in [0,2])
Definition: vec3.cpp:102
vec3 normalized() const
normalize the vector to unit length
Definition: vec3.cpp:281
const float * pointer() const
fast pointer access
Definition: vec3.cpp:299
void assert_size(const size_t &k) const
assert that a size_t belongs to [[0,2]]
Definition: vec3.cpp:272