1 /********************************************************************************
2 *                                                                               *
3 *       D o u b l e - P r e c i s i o n   4 - E l e m e n t   V e c t o r       *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1994,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (at your option) any later version.                                           *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXVEC4D_H
22 #define FXVEC4D_H
23 
24 namespace FX {
25 
26 
27 /// Double-precision 4-element vector
28 class FXAPI FXVec4d {
29 public:
30   FXdouble x;
31   FXdouble y;
32   FXdouble z;
33   FXdouble w;
34 public:
35 
36   /// Default constructor; value is not initialized
FXVec4d()37   FXVec4d(){}
38 
39   /// Construct with 3-vector
40   FXVec4d(const FXVec3d& v,FXdouble s=0.0):x(v.x),y(v.y),z(v.z),w(s){}
41 
42   /// Initialize from another vector
FXVec4d(const FXVec4d & v)43   FXVec4d(const FXVec4d& v):x(v.x),y(v.y),z(v.z),w(v.w){}
44 
45   /// Initialize from array of doubles
FXVec4d(const FXdouble v[])46   FXVec4d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
47 
48   /// Initialize with components
FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww)49   FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww):x(xx),y(yy),z(zz),w(ww){}
50 
51   /// Return a non-const reference to the ith element
52   FXdouble& operator[](FXint i){return (&x)[i];}
53 
54   /// Return a const reference to the ith element
55   const FXdouble& operator[](FXint i) const {return (&x)[i];}
56 
57   /// Assignment
58   FXVec4d& operator=(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
59 
60   /// Assignment from array of doubles
61   FXVec4d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
62 
63   /// Set value from another vector
set(const FXVec4d & v)64   FXVec4d& set(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
65 
66   /// Set value from array of doubles
set(const FXdouble v[])67   FXVec4d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
68 
69   /// Set value from components
set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww)70   FXVec4d& set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww){x=xx;y=yy;z=zz;w=ww;return *this;}
71 
72   /// Assigning operators
73   FXVec4d& operator*=(FXdouble n){ return set(x*n,y*n,z*n,w*n); }
74   FXVec4d& operator/=(FXdouble n){ return set(x/n,y/n,z/n,w/n); }
75 
76   /// Element-wise assigning operators
77   FXVec4d& operator+=(const FXVec4d& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
78   FXVec4d& operator-=(const FXVec4d& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
79   FXVec4d& operator%=(const FXVec4d& v){ return set(x*v.x,y*v.y,z*v.z,w*v.w); }
80   FXVec4d& operator/=(const FXVec4d& v){ return set(x/v.x,y/v.y,z/v.z,w/v.w); }
81 
82   /// Conversion
83   operator FXdouble*(){return &x;}
84   operator const FXdouble*() const {return &x;}
85   operator FXVec3d&(){return *reinterpret_cast<FXVec3d*>(this);}
86   operator const FXVec3d&() const {return *reinterpret_cast<const FXVec3d*>(this);}
87 
88   /// Test if zero
89   FXbool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
90 
91   /// Unary
92   FXVec4d operator+() const { return *this; }
93   FXVec4d operator-() const { return FXVec4d(-x,-y,-z,-w); }
94 
95   /// Length and square of length
length2()96   FXdouble length2() const { return x*x+y*y+z*z+w*w; }
length()97   FXdouble length() const { return Math::sqrt(length2()); }
98 
99   /// Signed distance normalized plane and point
100   FXdouble distance(const FXVec3d& p) const;
101 
102   /// Return true if edge a-b crosses plane
103   FXbool crosses(const FXVec3d& a,const FXVec3d& b) const;
104 
105   /// Destructor
~FXVec4d()106  ~FXVec4d(){}
107   };
108 
109 
110 /// Dot product
111 inline FXdouble operator*(const FXVec4d& a,const FXVec4d& b){ return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; }
112 
113 /// Scaling
114 inline FXVec4d operator*(const FXVec4d& a,FXdouble n){return FXVec4d(a.x*n,a.y*n,a.z*n,a.w*n);}
115 inline FXVec4d operator*(FXdouble n,const FXVec4d& a){return FXVec4d(n*a.x,n*a.y,n*a.z,n*a.w);}
116 inline FXVec4d operator/(const FXVec4d& a,FXdouble n){return FXVec4d(a.x/n,a.y/n,a.z/n,a.w/n);}
117 inline FXVec4d operator/(FXdouble n,const FXVec4d& a){return FXVec4d(n/a.x,n/a.y,n/a.z,n/a.w);}
118 
119 /// Vector and vector addition
120 inline FXVec4d operator+(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w); }
121 inline FXVec4d operator-(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w); }
122 
123 /// Element-wise multiply and divide
124 inline FXVec4d operator%(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w); }
125 inline FXVec4d operator/(const FXVec4d& a,const FXVec4d& b){ return FXVec4d(a.x/b.x,a.y/b.y,a.z/b.z,a.w/b.w); }
126 
127 /// Equality tests
128 inline FXbool operator==(const FXVec4d& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
129 inline FXbool operator!=(const FXVec4d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
130 inline FXbool operator==(FXdouble n,const FXVec4d& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
131 inline FXbool operator!=(FXdouble n,const FXVec4d& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
132 
133 /// Equality tests
134 inline FXbool operator==(const FXVec4d& a,const FXVec4d& b){ return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
135 inline FXbool operator!=(const FXVec4d& a,const FXVec4d& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w; }
136 
137 /// Inequality tests
138 inline FXbool operator<(const FXVec4d& a,FXdouble n){return a.x<n && a.y<n && a.z<n && a.w<n;}
139 inline FXbool operator<=(const FXVec4d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
140 inline FXbool operator>(const FXVec4d& a,FXdouble n){return a.x>n && a.y>n && a.z>n && a.w>n;}
141 inline FXbool operator>=(const FXVec4d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
142 
143 /// Inequality tests
144 inline FXbool operator<(FXdouble n,const FXVec4d& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
145 inline FXbool operator<=(FXdouble n,const FXVec4d& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
146 inline FXbool operator>(FXdouble n,const FXVec4d& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
147 inline FXbool operator>=(FXdouble n,const FXVec4d& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
148 
149 /// Inequality tests
150 inline FXbool operator<(const FXVec4d& a,const FXVec4d& b){ return a.x<b.x && a.y<b.y && a.z<b.z && a.w<b.w; }
151 inline FXbool operator<=(const FXVec4d& a,const FXVec4d& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z && a.w<=b.w; }
152 inline FXbool operator>(const FXVec4d& a,const FXVec4d& b){ return a.x>b.x && a.y>b.y && a.z>b.z && a.w>b.w; }
153 inline FXbool operator>=(const FXVec4d& a,const FXVec4d& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z && a.w>=b.w; }
154 
155 /// Lowest components
lo(const FXVec4d & a,const FXVec4d & b)156 inline FXVec4d lo(const FXVec4d& a,const FXVec4d& b){return FXVec4d(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y),Math::fmin(a.z,b.z),Math::fmin(a.w,b.w));}
lo(const FXVec4d & a,FXdouble n)157 inline FXVec4d lo(const FXVec4d& a,FXdouble n){return FXVec4d(Math::fmin(a.x,n),Math::fmin(a.y,n),Math::fmin(a.z,n),Math::fmin(a.w,n));}
lo(FXdouble n,const FXVec4d & b)158 inline FXVec4d lo(FXdouble n,const FXVec4d& b){return FXVec4d(Math::fmin(n,b.x),Math::fmin(n,b.y),Math::fmin(n,b.z),Math::fmin(n,b.w));}
159 
160 /// Highest components
hi(const FXVec4d & a,const FXVec4d & b)161 inline FXVec4d hi(const FXVec4d& a,const FXVec4d& b){return FXVec4d(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y),Math::fmax(a.z,b.z),Math::fmax(a.w,b.w));}
hi(const FXVec4d & a,FXdouble n)162 inline FXVec4d hi(const FXVec4d& a,FXdouble n){return FXVec4d(Math::fmax(a.x,n),Math::fmax(a.y,n),Math::fmax(a.z,n),Math::fmax(a.w,n));}
hi(FXdouble n,const FXVec4d & b)163 inline FXVec4d hi(FXdouble n,const FXVec4d& b){return FXVec4d(Math::fmax(n,b.x),Math::fmax(n,b.y),Math::fmax(n,b.z),Math::fmax(n,b.w));}
164 
165 /// Clamp components of vector between lower and upper limits
clamp(FXdouble lower,const FXVec4d & x,FXdouble upper)166 inline FXVec4d clamp(FXdouble lower,const FXVec4d& x,FXdouble upper){return hi(lo(x,upper),lower);}
167 
168 /// Clamp components of vector between lower corner and upper corner
clamp(const FXVec4d & lower,const FXVec4d & x,const FXVec4d & upper)169 inline FXVec4d clamp(const FXVec4d& lower,const FXVec4d& x,const FXVec4d& upper){return hi(lo(x,upper),lower);}
170 
171 /// Return vector of absolute value of each element
abs(const FXVec4d & a)172 inline FXVec4d abs(const FXVec4d& a){return FXVec4d(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z),Math::fabs(a.w));}
173 
174 /// Return maximum component of vector
max(const FXVec4d & a)175 inline FXdouble max(const FXVec4d& a){ return Math::fmax(Math::fmax(a.x,a.y),Math::fmax(a.z,a.w)); }
176 
177 /// Return minimum component of vector
min(const FXVec4d & a)178 inline FXdouble min(const FXVec4d& a){ return Math::fmin(Math::fmin(a.x,a.y),Math::fmin(a.z,a.w)); }
179 
180 /// Linearly interpolate
lerp(const FXVec4d & u,const FXVec4d & v,FXdouble f)181 inline FXVec4d lerp(const FXVec4d& u,const FXVec4d& v,FXdouble f){return (v-u)*f+u;}
182 
183 /// Compute normalized plane equation ax+by+cz+d=0
184 extern FXAPI FXVec4d plane(const FXVec4d& vec);
185 
186 /// Compute plane equation from vector and distance
187 extern FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
188 
189 /// Compute plane equation from vector and point on plane
190 extern FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
191 
192 /// Compute plane equation from 3 points a,b,c
193 extern FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
194 
195 /// Convert vector to color
196 extern FXAPI FXColor colorFromVec4d(const FXVec4d& vec);
197 
198 /// Convert color to vector
199 extern FXAPI FXVec4d colorToVec4d(FXColor clr);
200 
201 /// Normalize vector
202 extern FXAPI FXVec4d normalize(const FXVec4d& v);
203 
204 /// Save vector to a stream
205 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
206 
207 /// Load vector from a stream
208 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
209 
210 }
211 
212 #endif
213