Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXVec4d.h
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,2024 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 
28 class FXAPI FXVec4d {
29 public:
30  FXdouble x;
31  FXdouble y;
32  FXdouble z;
33  FXdouble w;
34 public:
35 
37  FXVec4d(){}
38 
40  explicit FXVec4d(FXdouble d):x(d),y(d),z(d),w(d){}
41 
43  FXVec4d(const FXVec3d& v,FXdouble s=0.0):x(v.x),y(v.y),z(v.z),w(s){}
44 
46  FXVec4d(const FXVec4d& v):x(v.x),y(v.y),z(v.z),w(v.w){}
47 
49  FXVec4d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
50 
52  FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww):x(xx),y(yy),z(zz),w(ww){}
53 
55  FXdouble& operator[](FXint i){return (&x)[i];}
56 
58  const FXdouble& operator[](FXint i) const {return (&x)[i];}
59 
61  FXVec4d& operator=(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
62 
64  FXVec4d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
65 
67  FXVec4d& set(FXdouble d){x=d;y=d;z=d;w=d;return *this;}
68 
70  FXVec4d& set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww){x=xx;y=yy;z=zz;w=ww;return *this;}
71 
73  FXVec4d& set(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
74 
76  FXVec4d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
77 
79  FXVec4d& operator*=(FXdouble n){ return set(x*n,y*n,z*n,w*n); }
80  FXVec4d& operator/=(FXdouble n){ return set(x/n,y/n,z/n,w/n); }
81 
83  FXVec4d& operator+=(const FXVec4d& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
84  FXVec4d& operator-=(const FXVec4d& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
85  FXVec4d& operator%=(const FXVec4d& v){ return set(x*v.x,y*v.y,z*v.z,w*v.w); }
86  FXVec4d& operator/=(const FXVec4d& v){ return set(x/v.x,y/v.y,z/v.z,w/v.w); }
87 
89  operator FXdouble*(){return &x;}
90  operator const FXdouble*() const {return &x;}
91  operator FXVec3d&(){return *reinterpret_cast<FXVec3d*>(this);}
92  operator const FXVec3d&() const {return *reinterpret_cast<const FXVec3d*>(this);}
93 
95  FXbool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
96 
98  FXVec4d operator+() const { return *this; }
99  FXVec4d operator-() const { return FXVec4d(-x,-y,-z,-w); }
100 
102  FXdouble length2() const { return w*w+z*z+y*y+x*x; }
103  FXdouble length() const { return Math::sqrt(length2()); }
104 
106  FXdouble distance(const FXVec3d& p) const;
107 
109  FXbool crosses(const FXVec3d& a,const FXVec3d& b) const;
110 
113  };
114 
115 
117 static 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; }
118 
120 static inline FXVec4d operator*(const FXVec4d& a,FXdouble n){return FXVec4d(a.x*n,a.y*n,a.z*n,a.w*n);}
121 static inline FXVec4d operator*(FXdouble n,const FXVec4d& a){return FXVec4d(n*a.x,n*a.y,n*a.z,n*a.w);}
122 static inline FXVec4d operator/(const FXVec4d& a,FXdouble n){return FXVec4d(a.x/n,a.y/n,a.z/n,a.w/n);}
123 static inline FXVec4d operator/(FXdouble n,const FXVec4d& a){return FXVec4d(n/a.x,n/a.y,n/a.z,n/a.w);}
124 
126 static 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); }
127 static 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); }
128 
130 static 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); }
131 static 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); }
132 
134 static inline FXbool operator==(const FXVec4d& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
135 static inline FXbool operator!=(const FXVec4d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
136 static inline FXbool operator==(FXdouble n,const FXVec4d& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
137 static inline FXbool operator!=(FXdouble n,const FXVec4d& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
138 
140 static 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; }
141 static 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; }
142 
144 static inline FXbool operator<(const FXVec4d& a,FXdouble n){return a.x<n && a.y<n && a.z<n && a.w<n;}
145 static inline FXbool operator<=(const FXVec4d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
146 static inline FXbool operator>(const FXVec4d& a,FXdouble n){return a.x>n && a.y>n && a.z>n && a.w>n;}
147 static inline FXbool operator>=(const FXVec4d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
148 
150 static inline FXbool operator<(FXdouble n,const FXVec4d& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
151 static inline FXbool operator<=(FXdouble n,const FXVec4d& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
152 static inline FXbool operator>(FXdouble n,const FXVec4d& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
153 static inline FXbool operator>=(FXdouble n,const FXVec4d& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
154 
156 static 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; }
157 static 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; }
158 static 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; }
159 static 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; }
160 
162 static 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));}
163 static 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));}
164 static 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));}
165 
167 static 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));}
168 static 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));}
169 static 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));}
170 
172 static inline FXVec4d clamp(FXdouble lower,const FXVec4d& x,FXdouble upper){return hi(lo(x,upper),lower);}
173 
175 static inline FXVec4d clamp(const FXVec4d& x,FXdouble limit){return hi(lo(x,limit),-limit);}
176 
178 static inline FXVec4d clamp(const FXVec4d& lower,const FXVec4d& x,const FXVec4d& upper){return hi(lo(x,upper),lower);}
179 
181 static inline FXVec4d clamp(const FXVec4d& x,const FXVec4d& limit){return hi(lo(x,limit),-limit);}
182 
184 static inline FXVec4d abs(const FXVec4d& a){return FXVec4d(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z),Math::fabs(a.w));}
185 
187 static inline FXdouble max(const FXVec4d& a){ return Math::fmax(Math::fmax(a.x,a.y),Math::fmax(a.z,a.w)); }
188 
190 static inline FXdouble min(const FXVec4d& a){ return Math::fmin(Math::fmin(a.x,a.y),Math::fmin(a.z,a.w)); }
191 
193 static inline FXVec4d lerp(const FXVec4d& u,const FXVec4d& v,FXdouble f){return (v-u)*f+u;}
194 
196 extern FXAPI FXVec4d plane(const FXVec4d& vec);
197 
199 extern FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
200 
202 extern FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
203 
205 extern FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
206 
208 extern FXAPI FXColor colorFromVec4d(const FXVec4d& vec);
209 
211 extern FXAPI FXVec4d colorToVec4d(FXColor clr);
212 
214 extern FXAPI FXVec4d normalize(const FXVec4d& v);
215 
217 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
218 
220 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
221 
222 }
223 
224 #endif
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec4d.h:55
FXVec4d(const FXVec3d &v, FXdouble s=0.0)
Construct with 3-vector.
Definition: FXVec4d.h:43
FXVec4d & operator=(const FXVec4d &v)
Assignment.
Definition: FXVec4d.h:61
FXVec4d(FXdouble xx, FXdouble yy, FXdouble zz, FXdouble ww)
Initialize with components.
Definition: FXVec4d.h:52
Double-precision 3-element vector.
Definition: FXVec3d.h:28
FXVec4d(const FXVec4d &v)
Initialize from another vector.
Definition: FXVec4d.h:46
FXdouble length2() const
Length and square of length.
Definition: FXVec4d.h:102
FXVec4d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec4d.h:79
FXbool operator!() const
Test if zero.
Definition: FXVec4d.h:95
FXVec4d operator+() const
Unary.
Definition: FXVec4d.h:98
~FXVec4d()
Destructor.
Definition: FXVec4d.h:112
Definition: FX4Splitter.h:28
FXVec4d & operator+=(const FXVec4d &v)
Element-wise assigning operators.
Definition: FXVec4d.h:83
Double-precision 4-element vector.
Definition: FXVec4d.h:28
FXVec4d(FXdouble d)
Initialize all components the same.
Definition: FXVec4d.h:40
FXVec4d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec4d.h:64
FXVec4d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec4d.h:49
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec4d.h:58
FXVec4d()
Default constructor; value is not initialized.
Definition: FXVec4d.h:37

Copyright © 1997-2022 Jeroen van der Zijp