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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/g4tools/include/tools/lina/vec4 (Version 11.3.0) and /externals/g4tools/include/tools/lina/vec4 (Version 11.0.p2)


  1 // Copyright (C) 2010, Guy Barrand. All rights      1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.            2 // See the file tools.license for terms.
  3                                                     3 
  4 #ifndef tools_vec4                                  4 #ifndef tools_vec4
  5 #define tools_vec4                                  5 #define tools_vec4
  6                                                     6 
  7 #include <cstddef> //size_t                         7 #include <cstddef> //size_t
  8                                                     8 
  9 #ifdef TOOLS_MEM                                    9 #ifdef TOOLS_MEM
 10 #include "../mem"                                  10 #include "../mem"
 11 #endif                                             11 #endif
 12                                                    12 
 13 namespace tools {                                  13 namespace tools {
 14                                                    14 
 15 template <class T>                                 15 template <class T>
 16 class vec4 {                                       16 class vec4 {
 17 #ifdef TOOLS_MEM                                   17 #ifdef TOOLS_MEM
 18 public:                                            18 public:
 19   static const std::string& s_class() {            19   static const std::string& s_class() {
 20     static const std::string s_v("tools::vec4"     20     static const std::string s_v("tools::vec4");
 21     return s_v;                                    21     return s_v;
 22   }                                                22   }
 23 #endif                                             23 #endif
 24 protected:                                         24 protected:
 25   static T zero() {return T();}                    25   static T zero() {return T();}
 26   static T minus_one() {return T(-1);}             26   static T minus_one() {return T(-1);}
 27 public:                                            27 public:
 28   typedef T elem_t;                                28   typedef T elem_t;
 29   unsigned int dimension() const {return 4;}       29   unsigned int dimension() const {return 4;}
 30 public:                                            30 public:
 31   vec4(){                                          31   vec4(){
 32 #ifdef TOOLS_MEM                                   32 #ifdef TOOLS_MEM
 33     mem::increment(s_class().c_str());             33     mem::increment(s_class().c_str());
 34 #endif                                             34 #endif
 35     m_data[0] = T();                               35     m_data[0] = T();
 36     m_data[1] = T();                               36     m_data[1] = T();
 37     m_data[2] = T();                               37     m_data[2] = T();
 38     m_data[3] = T();                               38     m_data[3] = T();
 39   }                                                39   }
 40   vec4(const T a_vec[4]) {                         40   vec4(const T a_vec[4]) {
 41 #ifdef TOOLS_MEM                                   41 #ifdef TOOLS_MEM
 42     mem::increment(s_class().c_str());             42     mem::increment(s_class().c_str());
 43 #endif                                             43 #endif
 44     m_data[0] = a_vec[0];                          44     m_data[0] = a_vec[0];
 45     m_data[1] = a_vec[1];                          45     m_data[1] = a_vec[1];
 46     m_data[2] = a_vec[2];                          46     m_data[2] = a_vec[2];
 47     m_data[3] = a_vec[3];                          47     m_data[3] = a_vec[3];
 48   }                                                48   }
 49   vec4(const T& a0,const T& a1,const T& a2,con     49   vec4(const T& a0,const T& a1,const T& a2,const T& a3
 50 #ifdef TOOLS_MEM                                   50 #ifdef TOOLS_MEM
 51        ,bool a_inc = true                          51        ,bool a_inc = true
 52 #endif                                             52 #endif
 53        ) {                                         53        ) {
 54 #ifdef TOOLS_MEM                                   54 #ifdef TOOLS_MEM
 55     if(a_inc) mem::increment(s_class().c_str()     55     if(a_inc) mem::increment(s_class().c_str());
 56 #endif                                             56 #endif
 57     m_data[0] = a0;                                57     m_data[0] = a0;
 58     m_data[1] = a1;                                58     m_data[1] = a1;
 59     m_data[2] = a2;                                59     m_data[2] = a2;
 60     m_data[3] = a3;                                60     m_data[3] = a3;
 61   }                                                61   }
 62   virtual ~vec4() {                                62   virtual ~vec4() {
 63 #ifdef TOOLS_MEM                                   63 #ifdef TOOLS_MEM
 64     mem::decrement(s_class().c_str());             64     mem::decrement(s_class().c_str());
 65 #endif                                             65 #endif
 66   }                                                66   }
 67 public:                                            67 public:
 68   vec4(const vec4& a_from){                        68   vec4(const vec4& a_from){
 69 #ifdef TOOLS_MEM                                   69 #ifdef TOOLS_MEM
 70     mem::increment(s_class().c_str());             70     mem::increment(s_class().c_str());
 71 #endif                                             71 #endif
 72     m_data[0] = a_from.m_data[0];                  72     m_data[0] = a_from.m_data[0];
 73     m_data[1] = a_from.m_data[1];                  73     m_data[1] = a_from.m_data[1];
 74     m_data[2] = a_from.m_data[2];                  74     m_data[2] = a_from.m_data[2];
 75     m_data[3] = a_from.m_data[3];                  75     m_data[3] = a_from.m_data[3];
 76   }                                                76   }
 77   vec4& operator=(const vec4& a_from) {            77   vec4& operator=(const vec4& a_from) {
 78     m_data[0] = a_from.m_data[0];                  78     m_data[0] = a_from.m_data[0];
 79     m_data[1] = a_from.m_data[1];                  79     m_data[1] = a_from.m_data[1];
 80     m_data[2] = a_from.m_data[2];                  80     m_data[2] = a_from.m_data[2];
 81     m_data[3] = a_from.m_data[3];                  81     m_data[3] = a_from.m_data[3];
 82     return *this;                                  82     return *this;
 83   }                                                83   }
 84 public:                                            84 public:
 85   const T& v0() const { return m_data[0];}         85   const T& v0() const { return m_data[0];}
 86   const T& v1() const { return m_data[1];}         86   const T& v1() const { return m_data[1];}
 87   const T& v2() const { return m_data[2];}         87   const T& v2() const { return m_data[2];}
 88   const T& v3() const { return m_data[3];}         88   const T& v3() const { return m_data[3];}
 89                                                    89 
 90   void v0(const T& a_value) { m_data[0] = a_va     90   void v0(const T& a_value) { m_data[0] = a_value;}
 91   void v1(const T& a_value) { m_data[1] = a_va     91   void v1(const T& a_value) { m_data[1] = a_value;}
 92   void v2(const T& a_value) { m_data[2] = a_va     92   void v2(const T& a_value) { m_data[2] = a_value;}
 93   void v3(const T& a_value) { m_data[3] = a_va     93   void v3(const T& a_value) { m_data[3] = a_value;}
 94                                                    94 
 95   const T& x() const { return m_data[0];}          95   const T& x() const { return m_data[0];}
 96   const T& y() const { return m_data[1];}          96   const T& y() const { return m_data[1];}
 97   const T& z() const { return m_data[2];}          97   const T& z() const { return m_data[2];}
 98   const T& t() const { return m_data[3];}          98   const T& t() const { return m_data[3];}
 99                                                    99 
100   void set_value(const T& a0,const T& a1,const    100   void set_value(const T& a0,const T& a1,const T& a2,const T& a3) {
101     m_data[0] = a0;                               101     m_data[0] = a0;
102     m_data[1] = a1;                               102     m_data[1] = a1;
103     m_data[2] = a2;                               103     m_data[2] = a2;
104     m_data[3] = a3;                               104     m_data[3] = a3;
105   }                                               105   }
106   void set_value(const T aV[4]) {                 106   void set_value(const T aV[4]) {
107     m_data[0] = aV[0];                            107     m_data[0] = aV[0];
108     m_data[1] = aV[1];                            108     m_data[1] = aV[1];
109     m_data[2] = aV[2];                            109     m_data[2] = aV[2];
110     m_data[3] = aV[3];                            110     m_data[3] = aV[3];
111   }                                               111   }
112   void value(T& a0,T& a1,T& a2,T& a3) const {     112   void value(T& a0,T& a1,T& a2,T& a3) const {
113     a0 = m_data[0];                               113     a0 = m_data[0];
114     a1 = m_data[1];                               114     a1 = m_data[1];
115     a2 = m_data[2];                               115     a2 = m_data[2];
116     a3 = m_data[3];                               116     a3 = m_data[3];
117   }                                               117   }
118                                                   118 
119   bool set_value(unsigned int a_index,const T&    119   bool set_value(unsigned int a_index,const T& a_value) {
120     if(a_index>=4) return false;                  120     if(a_index>=4) return false;
121     m_data[a_index] = a_value;                    121     m_data[a_index] = a_value;
122     return true;                                  122     return true;
123   }                                               123   }
124                                                   124 
125   T length(T(*a_sqrt)(T)) const {                 125   T length(T(*a_sqrt)(T)) const {
126     return a_sqrt(m_data[0]*m_data[0]+m_data[1    126     return a_sqrt(m_data[0]*m_data[0]+m_data[1]*m_data[1]+m_data[2]*m_data[2]+m_data[3]*m_data[3]);
127   }                                               127   }
128                                                   128 
129   T normalize(T(*a_sqrt)(T)) {                    129   T normalize(T(*a_sqrt)(T)) {
130     T norme = length(a_sqrt);                     130     T norme = length(a_sqrt);
131     if(norme==T()) return T();                    131     if(norme==T()) return T();
132     divide(norme);                                132     divide(norme);
133     return norme;                                 133     return norme;
134   }                                               134   }
135                                                   135 
136   bool equal(const vec4& a_vec) const {           136   bool equal(const vec4& a_vec) const {
137     if(m_data[0]!=a_vec.m_data[0]) return fals    137     if(m_data[0]!=a_vec.m_data[0]) return false;
138     if(m_data[1]!=a_vec.m_data[1]) return fals    138     if(m_data[1]!=a_vec.m_data[1]) return false;
139     if(m_data[2]!=a_vec.m_data[2]) return fals    139     if(m_data[2]!=a_vec.m_data[2]) return false;
140     if(m_data[3]!=a_vec.m_data[3]) return fals    140     if(m_data[3]!=a_vec.m_data[3]) return false;
141     return true;                                  141     return true;
142   }                                               142   }
143   bool equal(const vec4& a_vec,const T& a_epsi    143   bool equal(const vec4& a_vec,const T& a_epsil) const {
144     T* tp = (T*)m_data;                           144     T* tp = (T*)m_data;
145     T* ap = (T*)a_vec.m_data;                     145     T* ap = (T*)a_vec.m_data;
146     for(unsigned int i=0;i<4;i++,tp++,ap++) {     146     for(unsigned int i=0;i<4;i++,tp++,ap++) {
147       T diff = (*tp) - (*ap);                     147       T diff = (*tp) - (*ap);
148       if(diff<zero()) diff *= minus_one();        148       if(diff<zero()) diff *= minus_one();
149       if(diff>=a_epsil) return false;             149       if(diff>=a_epsil) return false;
150     }                                             150     }
151     return true;                                  151     return true;
152   }                                               152   }
153                                                   153 
154   bool is_proportional(const vec4& a_vec,T& a_    154   bool is_proportional(const vec4& a_vec,T& a_factor) const {
155     // If true, then : a_vec = a_factor * this    155     // If true, then : a_vec = a_factor * this.
156     a_factor = zero();                            156     a_factor = zero();
157     bool first = true;                            157     bool first = true;
158     T* tp = (T*)m_data;                           158     T* tp = (T*)m_data;
159     T* ap = (T*)a_vec.m_data;                     159     T* ap = (T*)a_vec.m_data;
160     for(unsigned int i=0;i<4;i++,tp++,ap++) {     160     for(unsigned int i=0;i<4;i++,tp++,ap++) {
161              if( ((*tp)==zero()) && ((*ap)==ze    161              if( ((*tp)==zero()) && ((*ap)==zero())) {
162         continue;                                 162         continue;
163       } else if( ((*tp)!=zero()) && ((*ap)==ze    163       } else if( ((*tp)!=zero()) && ((*ap)==zero())) {
164         return false;                             164         return false;
165       } else if( ((*tp)==zero()) && ((*ap)!=ze    165       } else if( ((*tp)==zero()) && ((*ap)!=zero())) {
166         return false;                             166         return false;
167       } else {                                    167       } else {
168         if(first) {                               168         if(first) {
169           a_factor = (*ap)/(*tp);                 169           a_factor = (*ap)/(*tp);
170           first = false;                          170           first = false;
171         } else {                                  171         } else {
172           if((*ap)!=(*tp)*a_factor) return fal    172           if((*ap)!=(*tp)*a_factor) return false;
173         }                                         173         }
174       }                                           174       }
175     }                                             175     }
176     return true;                                  176     return true;
177   }                                               177   }
178                                                   178 
179   void multiply(const T& a_T) {                   179   void multiply(const T& a_T) {
180     m_data[0] *= a_T;                             180     m_data[0] *= a_T;
181     m_data[1] *= a_T;                             181     m_data[1] *= a_T;
182     m_data[2] *= a_T;                             182     m_data[2] *= a_T;
183     m_data[3] *= a_T;                             183     m_data[3] *= a_T;
184   }                                               184   }
185                                                   185 
186   bool divide(const T& a_T) {                     186   bool divide(const T& a_T) {
187     if(a_T==T()) return false;                    187     if(a_T==T()) return false;
188     m_data[0] /= a_T;                             188     m_data[0] /= a_T;
189     m_data[1] /= a_T;                             189     m_data[1] /= a_T;
190     m_data[2] /= a_T;                             190     m_data[2] /= a_T;
191     m_data[3] /= a_T;                             191     m_data[3] /= a_T;
192     return true;                                  192     return true;
193   }                                               193   }
194                                                   194 
195   void add(const vec4& a_v) {                     195   void add(const vec4& a_v) {
196     m_data[0] += a_v.m_data[0];                   196     m_data[0] += a_v.m_data[0];
197     m_data[1] += a_v.m_data[1];                   197     m_data[1] += a_v.m_data[1];
198     m_data[2] += a_v.m_data[2];                   198     m_data[2] += a_v.m_data[2];
199     m_data[3] += a_v.m_data[3];                   199     m_data[3] += a_v.m_data[3];
200   }                                               200   }
201                                                   201 
202   void add(const T& a0,const T& a1,const T& a2    202   void add(const T& a0,const T& a1,const T& a2,const T& a3) {
203     m_data[0] += a0;                              203     m_data[0] += a0;
204     m_data[1] += a1;                              204     m_data[1] += a1;
205     m_data[2] += a2;                              205     m_data[2] += a2;
206     m_data[3] += a3;                              206     m_data[3] += a3;
207   }                                               207   }
208                                                   208 
209   void subtract(const vec4& a_v) {                209   void subtract(const vec4& a_v) {
210     m_data[0] -= a_v.m_data[0];                   210     m_data[0] -= a_v.m_data[0];
211     m_data[1] -= a_v.m_data[1];                   211     m_data[1] -= a_v.m_data[1];
212     m_data[2] -= a_v.m_data[2];                   212     m_data[2] -= a_v.m_data[2];
213     m_data[3] -= a_v.m_data[3];                   213     m_data[3] -= a_v.m_data[3];
214   }                                               214   }
215                                                   215 
216   void subtract(const T& a0,const T& a1,const     216   void subtract(const T& a0,const T& a1,const T& a2,const T& a3) {
217     m_data[0] -= a0;                              217     m_data[0] -= a0;
218     m_data[1] -= a1;                              218     m_data[1] -= a1;
219     m_data[2] -= a2;                              219     m_data[2] -= a2;
220     m_data[3] -= a3;                              220     m_data[3] -= a3;
221   }                                               221   }
222                                                   222 
223 public: //operators                               223 public: //operators
224   T& operator[](size_t a_index) {                 224   T& operator[](size_t a_index) {
225     //WARNING : no check on a_index.              225     //WARNING : no check on a_index.
226     return m_data[a_index];                       226     return m_data[a_index];
227   }                                               227   }
228   const T& operator[](size_t a_index) const {     228   const T& operator[](size_t a_index) const {
229     //WARNING : no check on a_index.              229     //WARNING : no check on a_index.
230     return m_data[a_index];                       230     return m_data[a_index];
231   }                                               231   }
232                                                   232 
233   vec4 operator+(const vec4& a_v) const {         233   vec4 operator+(const vec4& a_v) const {
234     return vec4(m_data[0]+a_v.m_data[0],          234     return vec4(m_data[0]+a_v.m_data[0],
235                 m_data[1]+a_v.m_data[1],          235                 m_data[1]+a_v.m_data[1],
236                 m_data[2]+a_v.m_data[2],          236                 m_data[2]+a_v.m_data[2],
237                 m_data[3]+a_v.m_data[3]);         237                 m_data[3]+a_v.m_data[3]);
238   }                                               238   }
239                                                   239 
240   vec4 operator-(const vec4& a_v) const {         240   vec4 operator-(const vec4& a_v) const {
241     return vec4(m_data[0]-a_v.m_data[0],          241     return vec4(m_data[0]-a_v.m_data[0],
242                 m_data[1]-a_v.m_data[1],          242                 m_data[1]-a_v.m_data[1],
243                 m_data[2]-a_v.m_data[2],          243                 m_data[2]-a_v.m_data[2],
244                 m_data[3]-a_v.m_data[3]);         244                 m_data[3]-a_v.m_data[3]);
245   }                                               245   }
246                                                   246 
247   vec4 operator*(const T& a_v) const {            247   vec4 operator*(const T& a_v) const {
248     return vec4(m_data[0]*a_v,                    248     return vec4(m_data[0]*a_v,
249                 m_data[1]*a_v,                    249                 m_data[1]*a_v,
250                 m_data[2]*a_v,                    250                 m_data[2]*a_v,
251                 m_data[3]*a_v);                   251                 m_data[3]*a_v);
252   }                                               252   }
253                                                   253 
254   bool operator==(const vec4& a_v) const {retu    254   bool operator==(const vec4& a_v) const {return equal(a_v);}
255   bool operator!=(const vec4& a_v) const {retu    255   bool operator!=(const vec4& a_v) const {return !operator==(a_v);}
256                                                   256 
257 public: //for tools/sg/sf_vec                  << 257 public: //for inlib/sg/sf_vec
258   typedef unsigned int size_type;                 258   typedef unsigned int size_type;
259   size_type size() const {return 4;}              259   size_type size() const {return 4;}
260   const T* data() const {return m_data;}          260   const T* data() const {return m_data;}
261 protected:                                        261 protected:
262   T m_data[4];                                    262   T m_data[4];
263 private:static void check_instantiation() {vec    263 private:static void check_instantiation() {vec4<float> v;}
264 };                                                264 };
265                                                   265 
266 //for sf, mf :                                    266 //for sf, mf :
267 template <class T>                                267 template <class T>
268 inline const T* get_data(const vec4<T>& a_v) {    268 inline const T* get_data(const vec4<T>& a_v) {return a_v.data();}
269                                                   269 
270 }                                                 270 }
271                                                   271 
272 #include <ostream>                                272 #include <ostream>
273                                                   273 
274 namespace tools {                                 274 namespace tools {
275                                                   275 
276 // for sf_vec::dump().                            276 // for sf_vec::dump().
277 template <class T>                                277 template <class T>
278 inline std::ostream& operator<<(std::ostream&     278 inline std::ostream& operator<<(std::ostream& a_out,const vec4<T>& a_this){
279   a_out << "x = " << a_this.v0()                  279   a_out << "x = " << a_this.v0()
280         << ",y = " << a_this.v1()                 280         << ",y = " << a_this.v1()
281         << ",z = " << a_this.v2()                 281         << ",z = " << a_this.v2()
282         << ",t = " << a_this.v3();                282         << ",t = " << a_this.v3();
283   return a_out;                                   283   return a_out;
284 }                                                 284 }
285                                                   285 
286 }                                                 286 }
287                                                   287 
288 #endif                                            288 #endif