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,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 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  FXVec3d(const FXVec2d& v,FXdouble s=0.0):x(v.x),y(v.y),z(s){}
40 
42  FXVec3d(const FXVec3d& v):x(v.x),y(v.y),z(v.z){}
43 
45  FXVec3d(const FXdouble v[]):x(v[0]),y(v[1]),z(v[2]){}
46 
48  FXVec3d(FXdouble xx,FXdouble yy,FXdouble zz):x(xx),y(yy),z(zz){}
49 
51  FXdouble& operator[](FXint i){return (&x)[i];}
52 
54  const FXdouble& operator[](FXint i) const {return (&x)[i];}
55 
57  FXVec3d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
58 
60  FXVec3d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
61 
63  FXVec3d& set(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
64 
66  FXVec3d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
67 
69  FXVec3d& set(FXdouble xx,FXdouble yy,FXdouble zz){x=xx;y=yy;z=zz;return *this;}
70 
72  FXVec3d& operator*=(FXdouble n){ return set(x*n,y*n,z*n); }
73  FXVec3d& operator/=(FXdouble n){ return set(x/n,y/n,z/n); }
74 
76  FXVec3d& operator+=(const FXVec3d& v){ return set(x+v.x,y+v.y,z+v.z); }
77  FXVec3d& operator-=(const FXVec3d& v){ return set(x-v.x,y-v.y,z-v.z); }
78  FXVec3d& operator%=(const FXVec3d& v){ return set(x*v.x,y*v.y,z*v.z); }
79 
81  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); }
82 
84  operator FXdouble*(){return &x;}
85  operator const FXdouble*() const {return &x;}
86  operator FXVec2d&(){return *reinterpret_cast<FXVec2d*>(this);}
87  operator const FXVec2d&() const {return *reinterpret_cast<const FXVec2d*>(this);}
88 
90  FXbool operator!() const { return x==0.0 && y==0.0 && z==0.0; }
91 
93  FXVec3d operator+() const { return *this; }
94  FXVec3d operator-() const { return FXVec3d(-x,-y,-z); }
95 
97  FXdouble length2() const { return x*x+y*y+z*z; }
98  FXdouble length() const { return Math::sqrt(length2()); }
99 
102  };
103 
104 
106 inline FXdouble operator*(const FXVec3d& a,const FXVec3d& b){ return a.x*b.x+a.y*b.y+a.z*b.z; }
107 
109 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); }
110 
112 inline FXVec3d operator*(const FXVec3d& a,FXdouble n){return FXVec3d(a.x*n,a.y*n,a.z*n);}
113 inline FXVec3d operator*(FXdouble n,const FXVec3d& a){return FXVec3d(n*a.x,n*a.y,n*a.z);}
114 inline FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);}
115 inline FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);}
116 
118 inline FXVec3d operator+(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x+b.x,a.y+b.y,a.z+b.z); }
119 inline FXVec3d operator-(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x-b.x,a.y-b.y,a.z-b.z); }
120 
122 inline FXVec3d operator%(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x*b.x,a.y*b.y,a.z*b.z); }
123 inline FXVec3d operator/(const FXVec3d& a,const FXVec3d& b){ return FXVec3d(a.x/b.x,a.y/b.y,a.z/b.z); }
124 
126 inline FXbool operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;}
127 inline FXbool operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;}
128 inline FXbool operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;}
129 inline FXbool operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;}
130 
132 inline FXbool operator==(const FXVec3d& a,const FXVec3d& b){ return a.x==b.x && a.y==b.y && a.z==b.z; }
133 inline FXbool operator!=(const FXVec3d& a,const FXVec3d& b){ return a.x!=b.x || a.y!=b.y || a.z!=b.z; }
134 
136 inline FXbool operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;}
137 inline FXbool operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;}
138 inline FXbool operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;}
139 inline FXbool operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;}
140 
142 inline FXbool operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;}
143 inline FXbool operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;}
144 inline FXbool operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;}
145 inline FXbool operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;}
146 
148 inline FXbool operator<(const FXVec3d& a,const FXVec3d& b){ return a.x<b.x && a.y<b.y && a.z<b.z; }
149 inline FXbool operator<=(const FXVec3d& a,const FXVec3d& b){ return a.x<=b.x && a.y<=b.y && a.z<=b.z; }
150 inline FXbool operator>(const FXVec3d& a,const FXVec3d& b){ return a.x>b.x && a.y>b.y && a.z>b.z; }
151 inline FXbool operator>=(const FXVec3d& a,const FXVec3d& b){ return a.x>=b.x && a.y>=b.y && a.z>=b.z; }
152 
154 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));}
155 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));}
156 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));}
157 
159 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));}
160 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));}
161 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));}
162 
164 inline FXVec3d clamp(FXdouble lower,const FXVec3d& x,FXdouble upper){return hi(lo(x,upper),lower);}
165 
167 inline FXVec3d clamp(const FXVec3d& lower,const FXVec3d& x,const FXVec3d& upper){return hi(lo(x,upper),lower);}
168 
170 inline FXVec3d abs(const FXVec3d& a){return FXVec3d(Math::fabs(a.x),Math::fabs(a.y),Math::fabs(a.z));}
171 
173 inline FXdouble max(const FXVec3d& a){ return Math::fmax(Math::fmax(a.x,a.y),a.z); }
174 
176 inline FXdouble min(const FXVec3d& a){ return Math::fmin(Math::fmin(a.x,a.y),a.z); }
177 
179 inline FXVec3d lerp(const FXVec3d& u,const FXVec3d& v,FXdouble f){return (v-u)*f+u;}
180 
182 extern FXAPI FXColor colorFromVec3d(const FXVec3d& vec);
183 
185 extern FXAPI FXVec3d colorToVec3d(FXColor clr);
186 
188 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
189 
191 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d);
192 
194 extern FXAPI FXVec3d normalize(const FXVec3d& v);
195 
197 extern FXAPI FXVec3d rotate(const FXVec3d& vec,const FXVec3d& axis,FXdouble ca,FXdouble sa);
198 
200 extern FXAPI FXVec3d rotate(const FXVec3d& vec,const FXVec3d& axis,FXdouble ang);
201 
203 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
204 
206 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
207 
208 }
209 
210 #endif
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec3d.h:51
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec3d.h:54
FXdouble length2() const
Length and square of length.
Definition: FXVec3d.h:97
FXVec3d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec3d.h:45
FXVec3d & operator=(const FXVec3d &v)
Assignment.
Definition: FXVec3d.h:57
FXVec3d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec3d.h:72
Double-precision 3-element vector.
Definition: FXVec3d.h:28
~FXVec3d()
Destructor.
Definition: FXVec3d.h:101
FXVec3d & operator^=(const FXVec3d &v)
Cross product assigning operator.
Definition: FXVec3d.h:81
FXbool operator!() const
Test if zero.
Definition: FXVec3d.h:90
FXVec3d()
Default constructor; value is not initialized.
Definition: FXVec3d.h:36
FXVec3d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec3d.h:60
FXVec3d(const FXVec3d &v)
Initialize from another vector.
Definition: FXVec3d.h:42
FXVec3d(FXdouble xx, FXdouble yy, FXdouble zz)
Initialize from components.
Definition: FXVec3d.h:48
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:76
FXVec3d(const FXVec2d &v, FXdouble s=0.0)
Initialize from 2-vector.
Definition: FXVec3d.h:39
FXVec3d operator+() const
Unary.
Definition: FXVec3d.h:93

Copyright © 1997-2022 Jeroen van der Zijp