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 
21 #ifndef VEC3_HPP
22 #define VEC3_HPP
23 
24 #include <string>
25 #include <iostream>
26 
27 #include <exception_cpe.hpp>
28 
29 namespace cpe
30 {
31 class vec2;
32 
34 class vec3
35 {
36 public:
37 
38  // ********************************************* //
39  // ********************************************* //
40  // CONSTRUCTORS
41  // ********************************************* //
42  // ********************************************* //
43 
45  vec3();
47  vec3(const double& x,const double& y,const double& z);
48 
49  // ********************************************* //
50  // ********************************************* //
51  // Get/set
52  // ********************************************* //
53  // ********************************************* //
54 
56  const double& x() const;
58  double& x();
60  const double& y() const;
62  double& y();
64  const double& z() const;
66  double& z();
67 
69  const double& operator[](const size_t& k) const;
71  double& operator[](const size_t& k);
73  const double& operator()(const size_t& k) const;
75  double& operator()(const size_t& k);
76 
78  void set_zero();
79 
82  const double *pointer() const;
83 
84  // ********************************************* //
85  // ********************************************* //
86  // Convert to lower dimension
87  // ********************************************* //
88  // ********************************************* //
89 
91  vec2 to_vec2() const;
92 
93 
94  // ********************************************* //
95  // ********************************************* //
96  // Math operation
97  // ********************************************* //
98  // ********************************************* //
99 
101  double dot(const vec3& p) const;
102 
104  double norm() const;
106  double norm2() const;
108  vec3 normalized() const;
109 
111  vec3 cross(const vec3& p) const;
112 
113 
114 
115 
116  // ********************************************* //
117  // ********************************************* //
118  // Operator +-*/
119  // ********************************************* //
120  // ********************************************* //
121 
123  vec3 operator+(const vec3& p2) const;
125  vec3 operator+(const double& s) const;
126 
128  vec3 operator-(const vec3& p2) const;
130  vec3 operator-(const double& s) const;
131 
132 
134  vec3 operator*(const double& s) const;
136  vec3 operator/(const double& s) const;
137 
139  vec3& operator+=(const vec3& p);
141  vec3& operator+=(const double& s);
143  vec3& operator-=(const vec3& p);
145  vec3& operator-=(const double& s);
147  vec3& operator*=(const double& s);
149  vec3& operator/=(const double& s);
150 
152  vec3 operator-() const;
153 
157  vec3 product_compontentwise(const vec3& p) const;
164  void scale(const double& sx,const double& sy,const double& sz);
165 
166 
167  // ********************************************* //
168  // ********************************************* //
169  // Output
170  // ********************************************* //
171  // ********************************************* //
172 
174  std::string to_string() const;
175 
176 
177 
178 private:
179 
180 
181  // ********************************************* //
182  // ********************************************* //
183  // Internal parameters
184  // ********************************************* //
185  // ********************************************* //
186 
188  double internal_x;
190  double internal_y;
192  double internal_z;
193 
194 
195  // ********************************************* //
196  // ********************************************* //
197  // Helper
198  // ********************************************* //
199  // ********************************************* //
200 
202  void assert_size(const size_t& k) const;
203 
204 };
205 
207 vec3 operator+(const double& s,const vec3& p);
209 vec3 operator-(const double& s,const vec3& p);
211 vec3 operator*(const double& s,const vec3& p);
212 
213 // ********************************************* //
214 // ********************************************* //
215 // Output
216 // ********************************************* //
217 // ********************************************* //
218 
220 std::ostream& operator<<(std::ostream& stream,const vec3& p);
221 
224 {
225 public:
226 
230  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){}
231 };
232 
233 }
234 
235 
236 
237 #endif