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

FXVec3d.h
1 /********************************************************************************
2 * *
3 * D o u b 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,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 FXVEC3D_H
22 #define FXVEC3D_H
23 
24 namespace FX {
25 
26 
28 class FXAPI FXVec3d {
29 public:
30  FXdouble x;
31  FXdouble y;
32  FXdouble z;
33 public:
34 
36  FXVec3d(){}
37 
39  explicit FXVec3d(FXdouble d):x(d),y(d),z(d){}
40 
42  FXVec3d(const FXVec2d& v,FXdouble s=0.0):x(v.x),y(v.y),z(s){}
43 
45  FXVec3d(const FXVec3d& v):x(v.x),y(v.y),z(v.z){}
46 
48  FXVec3d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]){}
49 
51  FXVec3d(FXdouble xx,FXdouble yy,FXdouble zz):x(xx),y(yy),z(zz){}
52 
54  FXdouble& operator[](FXint i){return (&x)[i];}
55 
57  const FXdouble& operator[](FXint i) const {return (&x)[i];}
58 
60  FXVec3d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
61 
63  FXVec3d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
64 
66  FXVec3d& set(FXdouble d){x=d;y=d;z=d;return *this;}
67 
69  FXVec3d& set(FXdouble xx,FXdouble yy,FXdouble zz){x=xx;y=yy;z=zz;return *this;}
70 
72  FXVec3d& set(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
73 
75  FXVec3d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
76 
78  FXVec3d& operator*=(FXdouble n){ return set(x*n,y*n,z*n); }
79  FXVec3d& operator/=(FXdouble n){ return set(x/n,y/n,z/n); }
80 
82  FXVec3d& operator+=(const FXVec3d& v){ return set(x+v.x,y+v.y,z+v.z); }
83  FXVec3d& operator-=(const FXVec3d& v){ return set(x-v.x,y-v.y,z-v.z); }
84  FXVec3d& operator%=(const FXVec3d& v){ return set(x*v.x,y*v.y,z*v.z); }
85 
87  FXVec3d& operator^=(const FXVec3d& v){ return set(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x); }
88 
90  operator FXdouble*(){return &x;}
91  operator const FXdouble*() const {return &x;}
92  operator FXVec2d&(){return *reinterpret_cast<FXVec2d*>(this);}
93  operator const FXVec2d&() const {return *reinterpret_cast<const FXVec2d*>(this);}
94 
96  FXbool operator!() const { return x==0.0 && y==0.0 && z==0.0; }
97 
99  FXVec3d operator+() const { return *this; }
100  FXVec3d operator-() const { return FXVec3d(-x,-y,-z); }
101 
103  FXdouble length2() const { return z*z+y*y+x*x; }
104  FXdouble length() const { return Math::sqrt(length2()); }
105 
108  };
109 
110 
112 static inline FXdouble operator*(const FXVec3d& a,const FXVec3d& b){ return a.x*b.x+a.y*b.y+a.z*b.z; }
113 
115 static inline FXVec3d operator^(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x); }
116 
118 static inline FXVec3d operator*(const FXVec3d& a,FXdouble n){return FXVec3d(a.x*n,a.y*n,a.z*n);}
119 static inline FXVec3d operator*(FXdouble n,const FXVec3d& a){return FXVec3d(n*a.x,n*a.y,n*a.z);}
120 static inline FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);}
121 static inline FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);}
122 
124 static inline FXVec3d operator+(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x+b.x,a.y+b.y,a.z+b.z); }
125 static inline FXVec3d operator-(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x-b.x,a.y-b.y,a.z-b.z); }
126 
128 static inline FXVec3d operator%(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x*b.x,a.y*b.y,a.z*b.z); }
129 static inline FXVec3d operator/(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x/b.x,a.y/b.y,a.z/b.z); }
130 
132 static inline FXbool operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;}
133 static inline FXbool operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;}
134 static inline FXbool operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;}
135 static inline FXbool operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;}
136 
138 static inline FXbool operator==(const FXVec3d& a,const FXVec3d& b){ return a.x==b.x && a.y==b.y && a.z==b.z; }
139 static inline FXbool operator!=(const FXVec3d& a,const FXVec3d& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z; }
140 
142 static inline FXbool operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;}
143 static inline FXbool operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;}
144 static inline FXbool operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;}
145 static inline FXbool operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;}
146 
148 static inline FXbool operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;}
149 static inline FXbool operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;}
150 static inline FXbool operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;}
151 static inline FXbool operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;}
152 
154 static inline FXbool operator<(const FXVec3d& a,const FXVec3d& b){ return a.x<b.x && a.y<b.y && a.z<b.z; }
155 static inline FXbool operator<=(const FXVec3d& a,const FXVec3d& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z; }
156 static inline FXbool operator>(const FXVec3d& a,const FXVec3d& b){ return a.x>b.x && a.y>b.y && a.z>b.z; }
157 static inline FXbool operator>=(const FXVec3d& a,const FXVec3d& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z; }
158 
160 static inline FXVec3d lo(const FXVec3d& a,const FXVec3d& b){return FXVec3d(Math::fmin(a.x,b.x),Math::fmin(a.y,b.y),Math::fmin(a.z,b.z));}
161 static inline FXVec3d lo(const FXVec3d& a,FXdouble n){return FXVec3d(Math::fmin(a.x,n),Math::fmin(a.y,n),Math::fmin(a.z,n));}
162 static inline FXVec3d lo(FXdouble n,const FXVec3d& b){return FXVec3d(Math::fmin(n,b.x),Math::fmin(n,b.y),Math::fmin(n,b.z));}
163 
165 static inline FXVec3d hi(const FXVec3d& a,const FXVec3d& b){return FXVec3d(Math::fmax(a.x,b.x),Math::fmax(a.y,b.y),Math::fmax(a.z,b.z));}
166 static inline FXVec3d hi(const FXVec3d& a,FXdouble n){return FXVec3d(Math::fmax(a.x,n),Math::fmax(a.y,n),Math::fmax(a.z,n));}
167 static inline FXVec3d hi(FXdouble n,const FXVec3d& b){return FXVec3d(Math::fmax(n,b.x),Math::fmax(n,b.y),Math::fmax(n,b.z));}
168 
170 static inline FXVec3d clamp(FXdouble lower,const FXVec3d& x,FXdouble upper){return hi(lo(x,upper),lower);}
171 
173 static inline FXVec3d clamp(const FXVec3d& x,FXdouble limit){return hi(lo(x,limit),-limit);}
174 
176 static inline FXVec3d clamp(const FXVec3d& lower,const FXVec3d& x,const FXVec3d& upper){return hi(lo(x,upper),lower);}
177 
179 static inline FXVec3d clamp(const FXVec3d& x,const FXVec3d& limit){return hi(lo(x,limit),-limit);}
180 
182 static inline FXVec3d abs(const FXVec3d& a){return FXVec3d(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z));}
183 
185 static inline FXdouble max(const FXVec3d& a){ return Math::fmax(Math::fmax(a.x,a.y),a.z); }
186 
188 static inline FXdouble min(const FXVec3d& a){ return Math::fmin(Math::fmin(a.x,a.y),a.z); }
189 
191 static inline FXVec3d lerp(const FXVec3d& u,const FXVec3d& v,FXdouble f){return (v-u)*f+u;}
192 
194 extern FXAPI FXColor colorFromVec3d(const FXVec3d& vec);
195 
197 extern FXAPI FXVec3d colorToVec3d(FXColor clr);
198 
200 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
201 
203 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d);
204 
206 extern FXAPI FXVec3d normalize(const FXVec3d& v);
207 
209 extern FXAPI FXVec3d orthogonal(const FXVec3d& v);
210 
212 extern FXAPI FXVec3d rotate(const FXVec3d& vec,const FXVec3d& axis,FXdouble ca,FXdouble sa);
213 
215 extern FXAPI FXVec3d rotate(const FXVec3d& vec,const FXVec3d& axis,FXdouble ang);
216 
218 extern FXAPI FXdouble distFromRay(const FXVec3d& org,const FXVec3d& dir,const FXVec3d& pnt);
219 
221 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
222 
224 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
225 
226 }
227 
228 #endif
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec3d.h:54
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec3d.h:57
FXdouble length2() const
Length and square of length.
Definition: FXVec3d.h:103
FXVec3d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec3d.h:48
FXVec3d & operator=(const FXVec3d &v)
Assignment.
Definition: FXVec3d.h:60
FXVec3d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec3d.h:78
Double-precision 3-element vector.
Definition: FXVec3d.h:28
~FXVec3d()
Destructor.
Definition: FXVec3d.h:107
FXVec3d & operator^=(const FXVec3d &v)
Cross product assigning operator.
Definition: FXVec3d.h:87
FXbool operator!() const
Test if zero.
Definition: FXVec3d.h:96
FXVec3d()
Default constructor; value is not initialized.
Definition: FXVec3d.h:36
FXVec3d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec3d.h:63
FXVec3d(const FXVec3d &v)
Initialize from another vector.
Definition: FXVec3d.h:45
FXVec3d(FXdouble xx, FXdouble yy, FXdouble zz)
Initialize from components.
Definition: FXVec3d.h:51
Definition: FX4Splitter.h:28
Double-precision 2-element vector.
Definition: FXVec2d.h:28
FXVec3d & operator+=(const FXVec3d &v)
Element-wise assigning operators.
Definition: FXVec3d.h:82
FXVec3d(const FXVec2d &v, FXdouble s=0.0)
Initialize from 2-vector.
Definition: FXVec3d.h:42
FXVec3d(FXdouble d)
Initialize all components the same.
Definition: FXVec3d.h:39
FXVec3d operator+() const
Unary.
Definition: FXVec3d.h:99

Copyright © 1997-2022 Jeroen van der Zijp