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,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (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 GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXVec3d.h 3297 2015-12-14 20:30:04Z arthurcnorman $ *
23 ********************************************************************************/
24 #ifndef FXVEC3D_H
25 #define FXVEC3D_H
26
27
28 namespace FX {
29
30
31 class FXMat3d;
32 class FXMat4d;
33
34
35 /// Double-precision 3-element vector
36 class FXAPI FXVec3d {
37 public:
38 FXdouble x;
39 FXdouble y;
40 FXdouble z;
41 public:
42
43 /// Default constructor
FXVec3d()44 FXVec3d(){}
45
46 /// Initialize from another vector
FXVec3d(const FXVec3d & v)47 FXVec3d(const FXVec3d& v){x=v.x;y=v.y;z=v.z;}
48
49 /// Initialize from array of doubles
FXVec3d(const FXdouble v[])50 FXVec3d(const FXdouble v[]){x=v[0];y=v[1];z=v[2];}
51
52 /// Initialize with components
53 FXVec3d(FXdouble xx,FXdouble yy,FXdouble zz=1.0){x=xx;y=yy;z=zz;}
54
55 /// Initialize with color
56 FXVec3d(FXColor color);
57
58 /// Return a non-const reference to the ith element
59 FXdouble& operator[](FXint i){return (&x)[i];}
60
61 /// Return a const reference to the ith element
62 const FXdouble& operator[](FXint i) const {return (&x)[i];}
63
64 /// Assign color
65 FXVec3d& operator=(FXColor color);
66
67 /// Assignment
68 FXVec3d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
69
70 /// Assignment from array of doubles
71 FXVec3d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
72
73 /// Set value from another vector
set(const FXVec3d & v)74 FXVec3d& set(const FXVec3d& v){x=v.x;y=v.y;z=v.z;return *this;}
75
76 /// Set value from array of floats
set(const FXdouble v[])77 FXVec3d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
78
79 /// Set value from components
set(FXdouble xx,FXdouble yy,FXdouble zz)80 FXVec3d& set(FXdouble xx,FXdouble yy,FXdouble zz){x=xx;y=yy;z=zz;return *this;}
81
82 /// Assigning operators
83 FXVec3d& operator*=(FXdouble n){x*=n;y*=n;z*=n;return *this;}
84 FXVec3d& operator/=(FXdouble n){x/=n;y/=n;z/=n;return *this;}
85 FXVec3d& operator+=(const FXVec3d& v){x+=v.x;y+=v.y;z+=v.z;return *this;}
86 FXVec3d& operator-=(const FXVec3d& v){x-=v.x;y-=v.y;z-=v.z;return *this;}
87
88 /// Conversions
89 operator FXdouble*(){return &x;}
90 operator const FXdouble*() const {return &x;}
91 operator FXVec2d&(){return *reinterpret_cast<FXVec2d*>(this);}
92 operator const FXVec2d&() const {return *reinterpret_cast<const FXVec2d*>(this);}
93
94 /// Convert to color
95 operator FXColor() const;
96
97 /// Unary
98 FXVec3d operator+() const { return *this; }
99 FXVec3d operator-() const { return FXVec3d(-x,-y,-z); }
100
101 /// Vector and vector
102 FXVec3d operator+(const FXVec3d& v) const { return FXVec3d(x+v.x,y+v.y,z+v.z); }
103 FXVec3d operator-(const FXVec3d& v) const { return FXVec3d(x-v.x,y-v.y,z-v.z); }
104
105 /// Vector and matrix
106 FXVec3d operator*(const FXMat3d& m) const;
107 FXVec3d operator*(const FXMat4d& m) const;
108
109 /// Scaling
110 friend inline FXVec3d operator*(const FXVec3d& a,FXdouble n);
111 friend inline FXVec3d operator*(FXdouble n,const FXVec3d& a);
112 friend inline FXVec3d operator/(const FXVec3d& a,FXdouble n);
113 friend inline FXVec3d operator/(FXdouble n,const FXVec3d& a);
114
115 /// Dot product
116 FXdouble operator*(const FXVec3d& v) const { return x*v.x+y*v.y+z*v.z; }
117
118 /// Cross product
119 FXVec3d operator^(const FXVec3d& v) const { return FXVec3d(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
120
121 /// Test if zero
122 bool operator!() const { return x==0.0 && y==0.0 && z==0.0; }
123
124 /// Equality tests
125 bool operator==(const FXVec3d& v) const { return x==v.x && y==v.y && z==v.z; }
126 bool operator!=(const FXVec3d& v) const { return x!=v.x || y!=v.y || z!=v.z; }
127
128 friend inline bool operator==(const FXVec3d& a,FXdouble n);
129 friend inline bool operator!=(const FXVec3d& a,FXdouble n);
130 friend inline bool operator==(FXdouble n,const FXVec3d& a);
131 friend inline bool operator!=(FXdouble n,const FXVec3d& a);
132
133 /// Inequality tests
134 bool operator<(const FXVec3d& v) const { return x<v.x && y<v.y && z<v.z; }
135 bool operator<=(const FXVec3d& v) const { return x<=v.x && y<=v.y && z<=v.z; }
136 bool operator>(const FXVec3d& v) const { return x>v.x && y>v.y && z>v.z; }
137 bool operator>=(const FXVec3d& v) const { return x>=v.x && y>=v.y && z>=v.z; }
138
139 friend inline bool operator<(const FXVec3d& a,FXdouble n);
140 friend inline bool operator<=(const FXVec3d& a,FXdouble n);
141 friend inline bool operator>(const FXVec3d& a,FXdouble n);
142 friend inline bool operator>=(const FXVec3d& a,FXdouble n);
143
144 friend inline bool operator<(FXdouble n,const FXVec3d& a);
145 friend inline bool operator<=(FXdouble n,const FXVec3d& a);
146 friend inline bool operator>(FXdouble n,const FXVec3d& a);
147 friend inline bool operator>=(FXdouble n,const FXVec3d& a);
148
149 /// Length and square of length
length2()150 FXdouble length2() const { return x*x+y*y+z*z; }
length()151 FXdouble length() const { return sqrt(length2()); }
152
153 /// Clamp values of vector between limits
clamp(FXdouble lo,FXdouble hi)154 FXVec3d& clamp(FXdouble lo,FXdouble hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);z=FXCLAMP(lo,z,hi);return *this;}
155
156 /// Lowest or highest components
157 friend inline FXVec3d lo(const FXVec3d& a,const FXVec3d& b);
158 friend inline FXVec3d hi(const FXVec3d& a,const FXVec3d& b);
159
160 /// Compute normal from three points a,b,c
161 friend FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
162
163 /// Compute approximate normal from four points a,b,c,d
164 friend FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d);
165
166 /// Normalize vector
167 friend FXAPI FXVec3d normalize(const FXVec3d& v);
168
169 /// Save vector to a stream
170 friend FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
171
172 /// Load vector from a stream
173 friend FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
174 };
175
176
177 inline FXVec3d operator*(const FXVec3d& a,FXdouble n){return FXVec3d(a.x*n,a.y*n,a.z*n);}
178 inline FXVec3d operator*(FXdouble n,const FXVec3d& a){return FXVec3d(n*a.x,n*a.y,n*a.z);}
179 inline FXVec3d operator/(const FXVec3d& a,FXdouble n){return FXVec3d(a.x/n,a.y/n,a.z/n);}
180 inline FXVec3d operator/(FXdouble n,const FXVec3d& a){return FXVec3d(n/a.x,n/a.y,n/a.z);}
181
182 inline bool operator==(const FXVec3d& a,FXdouble n){return a.x==n && a.y==n && a.z==n;}
183 inline bool operator!=(const FXVec3d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n;}
184 inline bool operator==(FXdouble n,const FXVec3d& a){return n==a.x && n==a.y && n==a.z;}
185 inline bool operator!=(FXdouble n,const FXVec3d& a){return n!=a.x || n!=a.y || n!=a.z;}
186
187 inline bool operator<(const FXVec3d& a,FXdouble n){return a.x<n && a.y<n && a.z<n;}
188 inline bool operator<=(const FXVec3d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n;}
189 inline bool operator>(const FXVec3d& a,FXdouble n){return a.x>n && a.y>n && a.z>n;}
190 inline bool operator>=(const FXVec3d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n;}
191
192 inline bool operator<(FXdouble n,const FXVec3d& a){return n<a.x && n<a.y && n<a.z;}
193 inline bool operator<=(FXdouble n,const FXVec3d& a){return n<=a.x && n<=a.y && n<=a.z;}
194 inline bool operator>(FXdouble n,const FXVec3d& a){return n>a.x && n>a.y && n>a.z;}
195 inline bool operator>=(FXdouble n,const FXVec3d& a){return n>=a.x && n>=a.y && n>=a.z;}
196
lo(const FXVec3d & a,const FXVec3d & b)197 inline FXVec3d lo(const FXVec3d& a,const FXVec3d& b){return FXVec3d(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z));}
hi(const FXVec3d & a,const FXVec3d & b)198 inline FXVec3d hi(const FXVec3d& a,const FXVec3d& b){return FXVec3d(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z));}
199
200 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
201 extern FXAPI FXVec3d normal(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c,const FXVec3d& d);
202
203 extern FXAPI FXVec3d normalize(const FXVec3d& v);
204
205 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec3d& v);
206 extern FXAPI FXStream& operator>>(FXStream& store,FXVec3d& v);
207
208 }
209
210 #endif
211