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  FXVec4d(const FXVec3d& v,FXdouble s=0.0):x(v.x),y(v.y),z(v.z),w(s){}
41 
43  FXVec4d(const FXVec4d& v):x(v.x),y(v.y),z(v.z),w(v.w){}
44 
46  FXVec4d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
47 
49  FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww):x(xx),y(yy),z(zz),w(ww){}
50 
52  FXdouble& operator[](FXint i){return (&x)[i];}
53 
55  const FXdouble& operator[](FXint i) const {return (&x)[i];}
56 
58  FXVec4d& operator=(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
59 
61  FXVec4d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
62 
64  FXVec4d& set(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
65 
67  FXVec4d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];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& 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 
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 
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 
89  FXbool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
90 
92  FXVec4d operator+() const { return *this; }
93  FXVec4d operator-() const { return FXVec4d(-x,-y,-z,-w); }
94 
96  FXdouble length2() const { return w*w+z*z+y*y+x*x; }
97  FXdouble length() const { return Math::sqrt(length2()); }
98 
100  FXdouble distance(const FXVec3d& p) const;
101 
103  FXbool crosses(const FXVec3d& a,const FXVec3d& b) const;
104 
107  };
108 
109 
111 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; }
112 
114 static inline FXVec4d operator*(const FXVec4d& a,FXdouble n){return FXVec4d(a.x*n,a.y*n,a.z*n,a.w*n);}
115 static inline FXVec4d operator*(FXdouble n,const FXVec4d& a){return FXVec4d(n*a.x,n*a.y,n*a.z,n*a.w);}
116 static inline FXVec4d operator/(const FXVec4d& a,FXdouble n){return FXVec4d(a.x/n,a.y/n,a.z/n,a.w/n);}
117 static inline FXVec4d operator/(FXdouble n,const FXVec4d& a){return FXVec4d(n/a.x,n/a.y,n/a.z,n/a.w);}
118 
120 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); }
121 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); }
122 
124 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); }
125 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); }
126 
128 static inline FXbool operator==(const FXVec4d& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
129 static inline FXbool operator!=(const FXVec4d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
130 static inline FXbool operator==(FXdouble n,const FXVec4d& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
131 static inline FXbool operator!=(FXdouble n,const FXVec4d& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
132 
134 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; }
135 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; }
136 
138 static inline FXbool operator<(const FXVec4d& a,FXdouble n){return a.x<n && a.y<n && a.z<n && a.w<n;}
139 static inline FXbool operator<=(const FXVec4d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
140 static inline FXbool operator>(const FXVec4d& a,FXdouble n){return a.x>n && a.y>n && a.z>n && a.w>n;}
141 static inline FXbool operator>=(const FXVec4d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
142 
144 static inline FXbool operator<(FXdouble n,const FXVec4d& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
145 static inline FXbool operator<=(FXdouble n,const FXVec4d& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
146 static inline FXbool operator>(FXdouble n,const FXVec4d& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
147 static inline FXbool operator>=(FXdouble n,const FXVec4d& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
148 
150 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; }
151 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; }
152 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; }
153 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; }
154 
156 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));}
157 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));}
158 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));}
159 
161 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));}
162 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));}
163 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));}
164 
166 static inline FXVec4d clamp(FXdouble lower,const FXVec4d& x,FXdouble upper){return hi(lo(x,upper),lower);}
167 
169 static inline FXVec4d clamp(const FXVec4d& x,FXdouble limit){return hi(lo(x,limit),-limit);}
170 
172 static inline FXVec4d clamp(const FXVec4d& lower,const FXVec4d& x,const FXVec4d& upper){return hi(lo(x,upper),lower);}
173 
175 static inline FXVec4d clamp(const FXVec4d& x,const FXVec4d& limit){return hi(lo(x,limit),-limit);}
176 
178 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));}
179 
181 static inline FXdouble max(const FXVec4d& a){ return Math::fmax(Math::fmax(a.x,a.y),Math::fmax(a.z,a.w)); }
182 
184 static inline FXdouble min(const FXVec4d& a){ return Math::fmin(Math::fmin(a.x,a.y),Math::fmin(a.z,a.w)); }
185 
187 static inline FXVec4d lerp(const FXVec4d& u,const FXVec4d& v,FXdouble f){return (v-u)*f+u;}
188 
190 extern FXAPI FXVec4d plane(const FXVec4d& vec);
191 
193 extern FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
194 
196 extern FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
197 
199 extern FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
200 
202 extern FXAPI FXColor colorFromVec4d(const FXVec4d& vec);
203 
205 extern FXAPI FXVec4d colorToVec4d(FXColor clr);
206 
208 extern FXAPI FXVec4d normalize(const FXVec4d& v);
209 
211 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
212 
214 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
215 
216 }
217 
218 #endif
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec4d.h:52
FXVec4d(const FXVec3d &v, FXdouble s=0.0)
Construct with 3-vector.
Definition: FXVec4d.h:40
FXVec4d & operator=(const FXVec4d &v)
Assignment.
Definition: FXVec4d.h:58
FXVec4d(FXdouble xx, FXdouble yy, FXdouble zz, FXdouble ww)
Initialize with components.
Definition: FXVec4d.h:49
Double-precision 3-element vector.
Definition: FXVec3d.h:28
FXVec4d(const FXVec4d &v)
Initialize from another vector.
Definition: FXVec4d.h:43
FXdouble length2() const
Length and square of length.
Definition: FXVec4d.h:96
FXVec4d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec4d.h:73
FXbool operator!() const
Test if zero.
Definition: FXVec4d.h:89
FXVec4d operator+() const
Unary.
Definition: FXVec4d.h:92
~FXVec4d()
Destructor.
Definition: FXVec4d.h:106
Definition: FX4Splitter.h:28
FXVec4d & operator+=(const FXVec4d &v)
Element-wise assigning operators.
Definition: FXVec4d.h:77
Double-precision 4-element vector.
Definition: FXVec4d.h:28
FXVec4d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec4d.h:61
FXVec4d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec4d.h:46
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec4d.h:55
FXVec4d()
Default constructor; value is not initialized.
Definition: FXVec4d.h:37

Copyright © 1997-2022 Jeroen van der Zijp