vec3.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2013 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 
20 #ifndef VEC3_HPP
21 #define VEC3_HPP
22 
23 #include <string>
24 #include <iostream>
25 
26 #include <exception_cpe.hpp>
27 
28 namespace cpe
29 {
30 class vec2;
31 
33 class vec3
34 {
35 public:
36 
37  // ********************************************* //
38  // ********************************************* //
39  // CONSTRUCTORS
40  // ********************************************* //
41  // ********************************************* //
42 
44  vec3();
46  vec3(float x,float y,float z);
47 
48  // ********************************************* //
49  // ********************************************* //
50  // Get/set
51  // ********************************************* //
52  // ********************************************* //
53 
55  float x() const;
57  float& x();
59  float y() const;
61  float& y();
63  float z() const;
65  float& z();
66 
68  float operator[](const size_t& k) const;
70  float& operator[](const size_t& k);
72  float operator()(const size_t& k) const;
74  float& operator()(const size_t& k);
75 
77  void set_zero();
78 
81  const float *pointer() const;
82 
83  // ********************************************* //
84  // ********************************************* //
85  // Convert to lower dimension
86  // ********************************************* //
87  // ********************************************* //
88 
90  vec2 to_vec2() const;
91 
92 
93  // ********************************************* //
94  // ********************************************* //
95  // Math operation
96  // ********************************************* //
97  // ********************************************* //
98 
100  float dot(const vec3& p) const;
101 
103  float norm() const;
105  float norm2() const;
107  vec3 normalized() const;
108 
110  vec3 cross(const vec3& p) const;
111 
112 
113 
114 
115  // ********************************************* //
116  // ********************************************* //
117  // Operator +-*/
118  // ********************************************* //
119  // ********************************************* //
120 
122  vec3 operator+(const vec3& p2) const;
124  vec3 operator+(float s) const;
125 
127  vec3 operator-(const vec3& p2) const;
129  vec3 operator-(float s) const;
130 
131 
133  vec3 operator*(float s) const;
135  vec3 operator/(float s) const;
136 
138  vec3& operator+=(const vec3& p);
140  vec3& operator+=(float s);
142  vec3& operator-=(const vec3& p);
144  vec3& operator-=(float s);
146  vec3& operator*=(float s);
148  vec3& operator/=(float s);
149 
151  vec3 operator-() const;
152 
156  vec3 product_compontentwise(const vec3& p) const;
163  void scale(float sx,float sy,float sz);
164 
165 
166  // ********************************************* //
167  // ********************************************* //
168  // Output
169  // ********************************************* //
170  // ********************************************* //
171 
173  std::string to_string() const;
174 
175 
176 
177 private:
178 
179 
180  // ********************************************* //
181  // ********************************************* //
182  // Internal parameters
183  // ********************************************* //
184  // ********************************************* //
185 
187  float internal_x;
189  float internal_y;
191  float internal_z;
192 
193 
194  // ********************************************* //
195  // ********************************************* //
196  // Helper
197  // ********************************************* //
198  // ********************************************* //
199 
201  void assert_size(const size_t& k) const;
202 
203 };
204 
206 vec3 operator+(float s,const vec3& p);
208 vec3 operator-(float s,const vec3& p);
210 vec3 operator*(float s,const vec3& p);
211 
212 // ********************************************* //
213 // ********************************************* //
214 // Output
215 // ********************************************* //
216 // ********************************************* //
217 
219 std::ostream& operator<<(std::ostream& stream,const vec3& p);
220 
223 {
224 public:
225 
229  exception_vec3(const std::string& msg_param,const std::string& file_param,const std::string& caller_param,const int& line_param):exception_cpe(msg_param,file_param,caller_param,line_param){}
230 };
231 
232 }
233 
234 
235 
236 #endif