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

FXVec3f.h
1 /********************************************************************************
2 * *
3 * S i n g l e - P r e c i s i o n 3 - 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 FXVEC3F_H
22 #define FXVEC3F_H
23 
24 namespace FX {
25 
26 
28 class FXAPI FXVec3f {
29 public:
30  FXfloat x;
31  FXfloat y;
32  FXfloat z;
33 public:
34 
36  FXVec3f(){}
37 
39  FXVec3f(const FXVec2f& v,FXfloat s=0.0f):x(v.x),y(v.y),z(s){}
40 
42  FXVec3f(const FXVec3f& v):x(v.x),y(v.y),z(v.z){}
43 
45  FXVec3f(const FXfloat v[]):x(v[0]),y(v[1]),z(v[2]){}
46 
48  FXVec3f(FXfloat xx,FXfloat yy,FXfloat zz):x(xx),y(yy),z(zz){}
49 
51  FXfloat& operator[](FXint i){return (&x)[i];}
52 
54  const FXfloat& operator[](FXint i) const {return (&x)[i];}
55 
57  FXVec3f& operator=(const FXVec3f& v){x=v.x;y=v.y;z=v.z;return *this;}
58 
60  FXVec3f& operator=(const FXfloat v[]){x=v[0];y=v[1];z=v[2];return *this;}
61 
63  FXVec3f& set(const FXVec3f& v){x=v.x;y=v.y;z=v.z;return *this;}
64 
66  FXVec3f& set(const FXfloat v[]){x=v[0];y=v[1];z=v[2];return *this;}
67 
69  FXVec3f& set(FXfloat xx,FXfloat yy,FXfloat zz){x=xx;y=yy;z=zz;return *this;}
70 
72  FXVec3f& operator*=(FXfloat n){ return set(x*n,y*n,z*n); }
73  FXVec3f& operator/=(FXfloat n){ return set(x/n,y/n,z/n); }
74 
76  FXVec3f& operator+=(const FXVec3f& v){ return set(x+v.x,y+v.y,z+v.z); }
77  FXVec3f& operator-=(const FXVec3f& v){ return set(x-v.x,y-v.y,z-v.z); }
78  FXVec3f& operator%=(const FXVec3f& v){ return set(x*v.x,y*v.y,z*v.z); }
79  FXVec3f& operator/=(const FXVec3f& v){ return set(x/v.x,y/v.y,z/v.z); }
80 
82  FXVec3f& operator^=(const FXVec3f& v){ return set(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x); }
83 
85  operator FXfloat*(){return &x;}
86  operator const FXfloat*() const {return &x;}
87  operator FXVec2f&(){return *reinterpret_cast<FXVec2f*>(this);}
88  operator const FXVec2f&() const {return *reinterpret_cast<const FXVec2f*>(this);}
89 
91  FXbool operator!() const { return x==0.0f && y==0.0f && z==0.0f; }
92 
94  FXVec3f operator+() const { return *this; }
95  FXVec3f operator-() const { return FXVec3f(-x,-y,-z); }
96 
98  FXfloat length2() const { return z*z+y*y+x*x; }
99  FXfloat length() const { return Math::sqrt(length2()); }
100 
103  };
104 
105 
107 inline FXfloat operator*(const FXVec3f& a,const FXVec3f& b){ return a.x*b.x+a.y*b.y+a.z*b.z; }
108 
110 inline FXVec3f operator^(const FXVec3f& a,const FXVec3f& b){ return FXVec3f(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); }
111 
113 inline FXVec3f operator*(const FXVec3f& a,FXfloat n){return FXVec3f(a.x*n,a.y*n,a.z*n);}
114 inline FXVec3f operator*(FXfloat n,const FXVec3f& a){return FXVec3f(n*a.x,n*a.y,n*a.z);}
115 inline FXVec3f operator/(const FXVec3f& a,FXfloat n){return FXVec3f(a.x/n,a.y/n,a.z/n);}
116 inline FXVec3f operator/(FXfloat n,const FXVec3f& a){return FXVec3f(n/a.x,n/a.y,n/a.z);}
117 
119 inline FXVec3f operator+(const FXVec3f& a,const FXVec3f& b){ return FXVec3f(a.x+b.x,a.y+b.y,a.z+b.z); }
120 inline FXVec3f operator-(const FXVec3f& a,const FXVec3f& b){ return FXVec3f(a.x-b.x,a.y-b.y,a.z-b.z); }
121 
123 inline FXVec3f operator%(const FXVec3f& a,const FXVec3f& b){ return FXVec3f(a.x*b.x,a.y*b.y,a.z*b.z); }
124 inline FXVec3f operator/(const FXVec3f& a,const FXVec3f& b){ return FXVec3f(a.x/b.x,a.y/b.y,a.z/b.z); }
125 
127 inline FXbool operator==(const FXVec3f& a,FXfloat n){return a.x==n && a.y==n && a.z==n;}
128 inline FXbool operator!=(const FXVec3f& a,FXfloat n){return a.x!=n || a.y!=n || a.z!=n;}
129 inline FXbool operator==(FXfloat n,const FXVec3f& a){return n==a.x && n==a.y && n==a.z;}
130 inline FXbool operator!=(FXfloat n,const FXVec3f& a){return n!=a.x || n!=a.y || n!=a.z;}
131 
133 inline FXbool operator==(const FXVec3f& a,const FXVec3f& b){ return a.x==b.x && a.y==b.y && a.z==b.z; }
134 inline FXbool operator!=(const FXVec3f& a,const FXVec3f& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z; }
135 
137 inline FXbool operator<(const FXVec3f& a,FXfloat n){return a.x<n && a.y<n && a.z<n;}
138 inline FXbool operator<=(const FXVec3f& a,FXfloat n){return a.x<=n && a.y<=n && a.z<=n;}
139 inline FXbool operator>(const FXVec3f& a,FXfloat n){return a.x>n && a.y>n && a.z>n;}
140 inline FXbool operator>=(const FXVec3f& a,FXfloat n){return a.x>=n && a.y>=n && a.z>=n;}
141 
143 inline FXbool operator<(FXfloat n,const FXVec3f& a){return n<a.x && n<a.y && n<a.z;}
144 inline FXbool operator<=(FXfloat n,const FXVec3f& a){return n<=a.x && n<=a.y && n<=a.z;}
145 inline FXbool operator>(FXfloat n,const FXVec3f& a){return n>a.x && n>a.y && n>a.z;}
146 inline FXbool operator>=(FXfloat n,const FXVec3f& a){return n>=a.x && n>=a.y && n>=a.z;}
147 
149 inline FXbool operator<(const FXVec3f& a,const FXVec3f& b){ return a.x<b.x && a.y<b.y && a.z<b.z; }
150 inline FXbool operator<=(const FXVec3f& a,const FXVec3f& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z; }
151 inline FXbool operator>(const FXVec3f& a,const FXVec3f& b){ return a.x>b.x && a.y>b.y && a.z>b.z; }
152 inline FXbool operator>=(const FXVec3f& a,const FXVec3f& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z; }
153 
155 inline FXVec3f lo(const FXVec3f& a,const FXVec3f& b){return FXVec3f(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y),Math::fmin(a.z,b.z));}
156 inline FXVec3f lo(const FXVec3f& a,FXfloat n){return FXVec3f(Math::fmin(a.x,n),Math::fmin(a.y,n),Math::fmin(a.z,n));}
157 inline FXVec3f lo(FXfloat n,const FXVec3f& b){return FXVec3f(Math::fmin(n,b.x),Math::fmin(n,b.y),Math::fmin(n,b.z));}
158 
160 inline FXVec3f hi(const FXVec3f& a,const FXVec3f& b){return FXVec3f(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y),Math::fmax(a.z,b.z));}
161 inline FXVec3f hi(const FXVec3f& a,FXfloat n){return FXVec3f(Math::fmax(a.x,n),Math::fmax(a.y,n),Math::fmax(a.z,n));}
162 inline FXVec3f hi(FXfloat n,const FXVec3f& b){return FXVec3f(Math::fmax(n,b.x),Math::fmax(n,b.y),Math::fmax(n,b.z));}
163 
165 inline FXVec3f clamp(FXfloat lower,const FXVec3f& x,FXfloat upper){return hi(lo(x,upper),lower);}
166 
168 inline FXVec3f clamp(const FXVec3f& lower,const FXVec3f& x,const FXVec3f& upper){return hi(lo(x,upper),lower);}
169 
171 inline FXVec3f abs(const FXVec3f& a){return FXVec3f(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z));}
172 
174 inline FXfloat max(const FXVec3f& a){ return Math::fmax(Math::fmax(a.x,a.y),a.z); }
175 
177 inline FXfloat min(const FXVec3f& a){ return Math::fmin(Math::fmin(a.x,a.y),a.z); }
178 
180 inline FXVec3f lerp(const FXVec3f& u,const FXVec3f& v,FXfloat f){return (v-u)*f+u;}
181 
183 extern FXAPI FXColor colorFromVec3f(const FXVec3f& vec);
184 
186 extern FXAPI FXVec3f colorToVec3f(FXColor clr);
187 
189 extern FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c);
190 
192 extern FXAPI FXVec3f normal(const FXVec3f& a,const FXVec3f& b,const FXVec3f& c,const FXVec3f& d);
193 
195 extern FXAPI FXVec3f normalize(const FXVec3f& v);
196 
198 extern FXAPI FXVec3f orthogonal(const FXVec3f& v);
199 
201 extern FXAPI FXVec3f rotate(const FXVec3f& vec,const FXVec3f& axis,FXfloat ca,FXfloat sa);
202 
204 extern FXAPI FXVec3f rotate(const FXVec3f& vector,const FXVec3f& axis,FXfloat ang);
205 
207 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3f& v);
208 
210 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3f& v);
211 
212 }
213 
214 #endif
FXfloat length2() const
Length and square of length.
Definition: FXVec3f.h:98
FXVec3f operator+() const
Unary.
Definition: FXVec3f.h:94
FXVec3f & operator+=(const FXVec3f &v)
Element-wise assigning operators.
Definition: FXVec3f.h:76
Single-precision 2-element vector.
Definition: FXVec2f.h:28
FXVec3f & operator=(const FXVec3f &v)
Assignment.
Definition: FXVec3f.h:57
FXVec3f(const FXfloat v[])
Initialize from array of floats.
Definition: FXVec3f.h:45
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec3f.h:54
FXVec3f & operator=(const FXfloat v[])
Assignment from array of floats.
Definition: FXVec3f.h:60
FXVec3f & operator^=(const FXVec3f &v)
Cross product assigning operator.
Definition: FXVec3f.h:82
FXVec3f(const FXVec2f &v, FXfloat s=0.0f)
Initialize from 2-vector.
Definition: FXVec3f.h:39
Definition: FX4Splitter.h:28
FXVec3f(FXfloat xx, FXfloat yy, FXfloat zz)
Initialize from components.
Definition: FXVec3f.h:48
Single-precision 3-element vector.
Definition: FXVec3f.h:28
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec3f.h:51
~FXVec3f()
Destructor.
Definition: FXVec3f.h:102
FXVec3f(const FXVec3f &v)
Initialize from another vector.
Definition: FXVec3f.h:42
FXVec3f()
Default constructor; value is not initialized.
Definition: FXVec3f.h:36
FXVec3f & operator*=(FXfloat n)
Assigning operators.
Definition: FXVec3f.h:72
FXbool operator!() const
Test if zero.
Definition: FXVec3f.h:91

Copyright © 1997-2022 Jeroen van der Zijp