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

FXVec4f.h
1 /********************************************************************************
2 * *
3 * S i n g 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,2022 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 FXVEC4F_H
22 #define FXVEC4F_H
23 
24 namespace FX {
25 
26 
28 class FXAPI FXVec4f {
29 public:
30  FXfloat x;
31  FXfloat y;
32  FXfloat z;
33  FXfloat w;
34 public:
35 
37  FXVec4f(){}
38 
40  FXVec4f(const FXVec3f& v,FXfloat s=0.0f):x(v.x),y(v.y),z(v.z),w(s){}
41 
43  FXVec4f(const FXVec4f& v):x(v.x),y(v.y),z(v.z),w(v.w){}
44 
46  FXVec4f(const FXfloat v[]):x(v[0]),y(v[1]),z(v[2]),w(v[3]){}
47 
49  FXVec4f(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww):x(xx),y(yy),z(zz),w(ww){}
50 
52  FXfloat& operator[](FXint i){return (&x)[i];}
53 
55  const FXfloat& operator[](FXint i) const {return (&x)[i];}
56 
58  FXVec4f& operator=(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
59 
61  FXVec4f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
62 
64  FXVec4f& set(const FXVec4f& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
65 
67  FXVec4f& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
68 
70  FXVec4f& set(FXfloat xx,FXfloat yy,FXfloat zz,FXfloat ww){x=xx;y=yy;z=zz;w=ww;return *this;}
71 
73  FXVec4f& operator*=(FXfloat n){ return set(x*n,y*n,z*n,w*n); }
74  FXVec4f& operator/=(FXfloat n){ return set(x/n,y/n,z/n,w/n); }
75 
77  FXVec4f& operator+=(const FXVec4f& v){ return set(x+v.x,y+v.y,z+v.z,w+v.w); }
78  FXVec4f& operator-=(const FXVec4f& v){ return set(x-v.x,y-v.y,z-v.z,w-v.w); }
79  FXVec4f& operator%=(const FXVec4f& v){ return set(x*v.x,y*v.y,z*v.z,w*v.w); }
80  FXVec4f& operator/=(const FXVec4f& v){ return set(x/v.x,y/v.y,z/v.z,w/v.w); }
81 
83  operator FXfloat*(){return &x;}
84  operator const FXfloat*() const {return &x;}
85  operator FXVec3f&(){return *reinterpret_cast<FXVec3f*>(this);}
86  operator const FXVec3f&() const {return *reinterpret_cast<const FXVec3f*>(this);}
87 
89  FXbool operator!() const { return x==0.0f && y==0.0f && z==0.0f && w==0.0f; }
90 
92  FXVec4f operator+() const { return *this; }
93  FXVec4f operator-() const { return FXVec4f(-x,-y,-z,-w); }
94 
96  FXfloat length2() const { return w*w+z*z+y*y+x*x; }
97  FXfloat length() const { return Math::sqrt(length2()); }
98 
100  FXfloat distance(const FXVec3f& p) const;
101 
103  FXbool crosses(const FXVec3f& a,const FXVec3f& b) const;
104 
107  };
108 
109 
111 inline FXfloat operator*(const FXVec4f& a,const FXVec4f& b){ return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; }
112 
114 inline FXVec4f operator*(const FXVec4f& a,FXfloat n){return FXVec4f(a.x*n,a.y*n,a.z*n,a.w*n);}
115 inline FXVec4f operator*(FXfloat n,const FXVec4f& a){return FXVec4f(n*a.x,n*a.y,n*a.z,n*a.w);}
116 inline FXVec4f operator/(const FXVec4f& a,FXfloat n){return FXVec4f(a.x/n,a.y/n,a.z/n,a.w/n);}
117 inline FXVec4f operator/(FXfloat n,const FXVec4f& a){return FXVec4f(n/a.x,n/a.y,n/a.z,n/a.w);}
118 
120 inline FXVec4f operator+(const FXVec4f& a,const FXVec4f& b){ return FXVec4f(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w); }
121 inline FXVec4f operator-(const FXVec4f& a,const FXVec4f& b){ return FXVec4f(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w); }
122 
124 inline FXVec4f operator%(const FXVec4f& a,const FXVec4f& b){ return FXVec4f(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w); }
125 inline FXVec4f operator/(const FXVec4f& a,const FXVec4f& b){ return FXVec4f(a.x/b.x,a.y/b.y,a.z/b.z,a.w/b.w); }
126 
128 inline FXbool operator==(const FXVec4f& a,FXfloat n){return a.x==n && a.y==n && a.z==n && a.w==n;}
129 inline FXbool operator!=(const FXVec4f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
130 inline FXbool operator==(FXfloat n,const FXVec4f& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
131 inline FXbool operator!=(FXfloat n,const FXVec4f& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
132 
134 inline FXbool operator==(const FXVec4f& a,const FXVec4f& b){ return a.x==b.x && a.y==b.y && a.z==b.z && a.w==b.w; }
135 inline FXbool operator!=(const FXVec4f& a,const FXVec4f& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z || a.w!=b.w; }
136 
138 inline FXbool operator<(const FXVec4f& a,FXfloat n){return a.x<n && a.y<n && a.z<n && a.w<n;}
139 inline FXbool operator<=(const FXVec4f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
140 inline FXbool operator>(const FXVec4f& a,FXfloat n){return a.x>n && a.y>n && a.z>n && a.w>n;}
141 inline FXbool operator>=(const FXVec4f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
142 
144 inline FXbool operator<(FXfloat n,const FXVec4f& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
145 inline FXbool operator<=(FXfloat n,const FXVec4f& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
146 inline FXbool operator>(FXfloat n,const FXVec4f& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
147 inline FXbool operator>=(FXfloat n,const FXVec4f& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
148 
150 inline FXbool operator<(const FXVec4f& a,const FXVec4f& b){ return a.x<b.x && a.y<b.y && a.z<b.z && a.w<b.w; }
151 inline FXbool operator<=(const FXVec4f& a,const FXVec4f& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z && a.w<=b.w; }
152 inline FXbool operator>(const FXVec4f& a,const FXVec4f& b){ return a.x>b.x && a.y>b.y && a.z>b.z && a.w>b.w; }
153 inline FXbool operator>=(const FXVec4f& a,const FXVec4f& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z && a.w>=b.w; }
154 
156 inline FXVec4f lo(const FXVec4f& a,const FXVec4f& b){return FXVec4f(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 inline FXVec4f lo(const FXVec4f& a,FXfloat n){return FXVec4f(Math::fmin(a.x,n),Math::fmin(a.y,n),Math::fmin(a.z,n),Math::fmin(a.w,n));}
158 inline FXVec4f lo(FXfloat n,const FXVec4f& b){return FXVec4f(Math::fmin(n,b.x),Math::fmin(n,b.y),Math::fmin(n,b.z),Math::fmin(n,b.w));}
159 
161 inline FXVec4f hi(const FXVec4f& a,const FXVec4f& b){return FXVec4f(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 inline FXVec4f hi(const FXVec4f& a,FXfloat n){return FXVec4f(Math::fmax(a.x,n),Math::fmax(a.y,n),Math::fmax(a.z,n),Math::fmax(a.w,n));}
163 inline FXVec4f hi(FXfloat n,const FXVec4f& b){return FXVec4f(Math::fmax(n,b.x),Math::fmax(n,b.y),Math::fmax(n,b.z),Math::fmax(n,b.w));}
164 
166 inline FXVec4f clamp(FXfloat lower,const FXVec4f& x,FXfloat upper){return hi(lo(x,upper),lower);}
167 
169 inline FXVec4f clamp(const FXVec4f& lower,const FXVec4f& x,const FXVec4f& upper){return hi(lo(x,upper),lower);}
170 
172 inline FXVec4f abs(const FXVec4f& a){return FXVec4f(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z),Math::fabs(a.w));}
173 
175 inline FXfloat max(const FXVec4f& a){ return Math::fmax(Math::fmax(a.x,a.y),Math::fmax(a.z,a.w)); }
176 
178 inline FXfloat min(const FXVec4f& a){ return Math::fmin(Math::fmin(a.x,a.y),Math::fmin(a.z,a.w)); }
179 
181 inline FXVec4f lerp(const FXVec4f& u,const FXVec4f& v,FXfloat f){return (v-u)*f+u;}
182 
184 extern FXAPI FXVec4f plane(const FXVec4f& vec);
185 
187 extern FXAPI FXVec4f plane(const FXVec3f& vec,FXfloat dist);
188 
190 extern FXAPI FXVec4f plane(const FXVec3f& vec,const FXVec3f& p);
191 
193 extern FXAPI FXVec4f plane(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
194 
196 extern FXAPI FXColor colorFromVec4f(const FXVec4f& vec);
197 
199 extern FXAPI FXVec4f colorToVec4f(FXColor clr);
200 
202 extern FXAPI FXVec4f normalize(const FXVec4f& v);
203 
205 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4f& v);
206 
208 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4f& v);
209 
210 }
211 
212 #endif
FXVec4f(const FXVec3f &v, FXfloat s=0.0f)
Construct with 3-vector.
Definition: FXVec4f.h:40
FXVec4f()
Default constructor; value is not initialized.
Definition: FXVec4f.h:37
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec4f.h:55
FXVec4f & operator*=(FXfloat n)
Assigning operators.
Definition: FXVec4f.h:73
FXfloat length2() const
Length and square of length.
Definition: FXVec4f.h:96
FXbool operator!() const
Test if zero.
Definition: FXVec4f.h:89
~FXVec4f()
Destructor.
Definition: FXVec4f.h:106
Definition: FX4Splitter.h:28
FXVec4f operator+() const
Unary.
Definition: FXVec4f.h:92
FXVec4f & operator=(const FXVec4f &v)
Assignment.
Definition: FXVec4f.h:58
FXVec4f(const FXfloat v[])
Construct from array of floats.
Definition: FXVec4f.h:46
Single-precision 3-element vector.
Definition: FXVec3f.h:28
FXVec4f(const FXVec4f &v)
Initialize from another vector.
Definition: FXVec4f.h:43
FXVec4f & operator+=(const FXVec4f &v)
Element-wise assigning operators.
Definition: FXVec4f.h:77
FXVec4f & operator=(const FXfloat v[])
Assignment from array of floats.
Definition: FXVec4f.h:61
FXVec4f(FXfloat xx, FXfloat yy, FXfloat zz, FXfloat ww)
Construct from components.
Definition: FXVec4f.h:49
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec4f.h:52
Single-precision 4-element vector.
Definition: FXVec4f.h:28

Copyright © 1997-2022 Jeroen van der Zijp