1 /********************************************************************************
2 * *
3 * D o u b 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,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: FXVec4d.h 3297 2015-12-14 20:30:04Z arthurcnorman $ *
23 ********************************************************************************/
24 #ifndef FXVEC4D_H
25 #define FXVEC4D_H
26
27
28 namespace FX {
29
30
31 class FXMat4d;
32
33
34 /// Double-precision 4-element vector
35 class FXAPI FXVec4d {
36 public:
37 FXdouble x;
38 FXdouble y;
39 FXdouble z;
40 FXdouble w;
41 public:
42
43 /// Default constructor
FXVec4d()44 FXVec4d(){}
45
46 /// Initialize from another vector
FXVec4d(const FXVec4d & v)47 FXVec4d(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;}
48
49 /// Construct with 3-vector and optional scalar
50 FXVec4d(const FXVec3d& v,FXdouble ww=1.0){x=v.x;y=v.y;z=v.z;w=ww;}
51
52 /// Initialize from array of doubles
FXVec4d(const FXdouble v[])53 FXVec4d(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];}
54
55 /// Initialize with components
56 FXVec4d(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww=1.0){x=xx;y=yy;z=zz;w=ww;}
57
58 /// Initialize with color
59 FXVec4d(FXColor color);
60
61 /// Return a non-const reference to the ith element
62 FXdouble& operator[](FXint i){return (&x)[i];}
63
64 /// Return a const reference to the ith element
65 const FXdouble& operator[](FXint i) const {return (&x)[i];}
66
67 /// Assign color
68 FXVec4d& operator=(FXColor color);
69
70 /// Assignment
71 FXVec4d& operator=(const FXVec3d& v){x=v.x;y=v.y;z=v.z;w=1.0;return *this;}
72 FXVec4d& operator=(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
73
74 /// Assignment from array of doubles
75 FXVec4d& operator=(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
76
77 /// Set value from another vector
set(const FXVec4d & v)78 FXVec4d& set(const FXVec4d& v){x=v.x;y=v.y;z=v.z;w=v.w;return *this;}
79
80 /// Set value from array of floats
set(const FXdouble v[])81 FXVec4d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];w=v[3];return *this;}
82
83 /// Set value from components
set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww)84 FXVec4d& set(FXdouble xx,FXdouble yy,FXdouble zz,FXdouble ww){x=xx;y=yy;z=zz;w=ww;return *this;}
85
86 /// Assigning operators
87 FXVec4d& operator*=(FXdouble n){x*=n;y*=n;z*=n;w*=n;return *this;}
88 FXVec4d& operator/=(FXdouble n){x/=n;y/=n;z/=n;w/=n;return *this;}
89 FXVec4d& operator+=(const FXVec4d& v){x+=v.x;y+=v.y;z+=v.z;w+=v.w;return *this;}
90 FXVec4d& operator-=(const FXVec4d& v){x-=v.x;y-=v.y;z-=v.z;w-=v.w;return *this;}
91
92 /// Conversion
93 operator FXdouble*(){return &x;}
94 operator const FXdouble*() const {return &x;}
95 operator FXVec3d&(){return *reinterpret_cast<FXVec3d*>(this);}
96 operator const FXVec3d&() const {return *reinterpret_cast<const FXVec3d*>(this);}
97
98 /// Convert to color
99 operator FXColor() const;
100
101 /// Unary
102 FXVec4d operator+() const { return *this; }
103 FXVec4d operator-() const { return FXVec4d(-x,-y,-z,-w); }
104
105 /// Vector and vector
106 FXVec4d operator+(const FXVec4d& v) const { return FXVec4d(x+v.x,y+v.y,z+v.z,w+v.w); }
107 FXVec4d operator-(const FXVec4d& v) const { return FXVec4d(x-v.x,y-v.y,z-v.z,w-v.w); }
108
109 /// Vector and matrix
110 FXVec4d operator*(const FXMat4d& m) const;
111
112 /// Scaling
113 friend inline FXVec4d operator*(const FXVec4d& a,FXdouble n);
114 friend inline FXVec4d operator*(FXdouble n,const FXVec4d& a);
115 friend inline FXVec4d operator/(const FXVec4d& a,FXdouble n);
116 friend inline FXVec4d operator/(FXdouble n,const FXVec4d& a);
117
118 /// Dot product
119 FXdouble operator*(const FXVec4d& v) const { return x*v.x+y*v.y+z*v.z+w*v.w; }
120
121 /// Test if zero
122 bool operator!() const {return x==0.0 && y==0.0 && z==0.0 && w==0.0; }
123
124 /// Equality tests
125 bool operator==(const FXVec4d& v) const { return x==v.x && y==v.y && z==v.z && w==v.w; }
126 bool operator!=(const FXVec4d& v) const { return x!=v.x || y!=v.y || z!=v.z || w!=v.w; }
127
128 friend inline bool operator==(const FXVec4d& a,FXdouble n);
129 friend inline bool operator!=(const FXVec4d& a,FXdouble n);
130 friend inline bool operator==(FXdouble n,const FXVec4d& a);
131 friend inline bool operator!=(FXdouble n,const FXVec4d& a);
132
133 /// Inequality tests
134 bool operator<(const FXVec4d& v) const { return x<v.x && y<v.y && z<v.z && w<v.w; }
135 bool operator<=(const FXVec4d& v) const { return x<=v.x && y<=v.y && z<=v.z && w<=v.w; }
136 bool operator>(const FXVec4d& v) const { return x>v.x && y>v.y && z>v.z && w>v.w; }
137 bool operator>=(const FXVec4d& v) const { return x>=v.x && y>=v.y && z>=v.z && w>=v.w; }
138
139 friend inline bool operator<(const FXVec4d& a,FXdouble n);
140 friend inline bool operator<=(const FXVec4d& a,FXdouble n);
141 friend inline bool operator>(const FXVec4d& a,FXdouble n);
142 friend inline bool operator>=(const FXVec4d& a,FXdouble n);
143
144 friend inline bool operator<(FXdouble n,const FXVec4d& a);
145 friend inline bool operator<=(FXdouble n,const FXVec4d& a);
146 friend inline bool operator>(FXdouble n,const FXVec4d& a);
147 friend inline bool operator>=(FXdouble n,const FXVec4d& a);
148
149 /// Length and square of length
length2()150 FXdouble length2() const { return x*x+y*y+z*z+w*w; }
length()151 FXdouble length() const { return sqrt(length2()); }
152
153 /// Clamp values of vector between limits
clamp(FXdouble lo,FXdouble hi)154 FXVec4d& clamp(FXdouble lo,FXdouble hi){x=FXCLAMP(lo,x,hi);y=FXCLAMP(lo,y,hi);z=FXCLAMP(lo,z,hi);w=FXCLAMP(lo,w,hi);return *this;}
155
156 /// Lowest or highest components
157 friend inline FXVec4d lo(const FXVec4d& a,const FXVec4d& b);
158 friend inline FXVec4d hi(const FXVec4d& a,const FXVec4d& b);
159
160 /// Compute normalized plane equation ax+by+cz+d=0
161 friend FXAPI FXVec4d plane(const FXVec4d& vec);
162 friend FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
163 friend FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
164 friend FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
165
166 /// Signed distance normalized plane and point
167 FXdouble distance(const FXVec3d& p) const;
168
169 /// Return true if edge a-b crosses plane
170 bool crosses(const FXVec3d& a,const FXVec3d& b) const;
171
172 /// Normalize vector
173 friend FXAPI FXVec4d normalize(const FXVec4d& v);
174
175 /// Save to a stream
176 friend FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
177
178 /// Load from a stream
179 friend FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
180 };
181
182
183 inline FXVec4d operator*(const FXVec4d& a,FXdouble n){return FXVec4d(a.x*n,a.y*n,a.z*n,a.w*n);}
184 inline FXVec4d operator*(FXdouble n,const FXVec4d& a){return FXVec4d(n*a.x,n*a.y,n*a.z,n*a.w);}
185 inline FXVec4d operator/(const FXVec4d& a,FXdouble n){return FXVec4d(a.x/n,a.y/n,a.z/n,a.w/n);}
186 inline FXVec4d operator/(FXdouble n,const FXVec4d& a){return FXVec4d(n/a.x,n/a.y,n/a.z,n/a.w);}
187
188 inline bool operator==(const FXVec4d& a,FXdouble n){return a.x==n && a.y==n && a.z==n && a.w==n;}
189 inline bool operator!=(const FXVec4d& a,FXdouble n){return a.x!=n || a.y!=n || a.z!=n || a.w!=n;}
190 inline bool operator==(FXdouble n,const FXVec4d& a){return n==a.x && n==a.y && n==a.z && n==a.w;}
191 inline bool operator!=(FXdouble n,const FXVec4d& a){return n!=a.x || n!=a.y || n!=a.z || n!=a.w;}
192
193 inline bool operator<(const FXVec4d& a,FXdouble n){return a.x<n && a.y<n && a.z<n && a.w<n;}
194 inline bool operator<=(const FXVec4d& a,FXdouble n){return a.x<=n && a.y<=n && a.z<=n && a.w<=n;}
195 inline bool operator>(const FXVec4d& a,FXdouble n){return a.x>n && a.y>n && a.z>n && a.w>n;}
196 inline bool operator>=(const FXVec4d& a,FXdouble n){return a.x>=n && a.y>=n && a.z>=n && a.w>=n;}
197
198 inline bool operator<(FXdouble n,const FXVec4d& a){return n<a.x && n<a.y && n<a.z && n<a.w;}
199 inline bool operator<=(FXdouble n,const FXVec4d& a){return n<=a.x && n<=a.y && n<=a.z && n<=a.w;}
200 inline bool operator>(FXdouble n,const FXVec4d& a){return n>a.x && n>a.y && n>a.z && n>a.w;}
201 inline bool operator>=(FXdouble n,const FXVec4d& a){return n>=a.x && n>=a.y && n>=a.z && n>=a.w;}
202
lo(const FXVec4d & a,const FXVec4d & b)203 inline FXVec4d lo(const FXVec4d& a,const FXVec4d& b){return FXVec4d(FXMIN(a.x,b.x),FXMIN(a.y,b.y),FXMIN(a.z,b.z),FXMIN(a.w,b.w));}
hi(const FXVec4d & a,const FXVec4d & b)204 inline FXVec4d hi(const FXVec4d& a,const FXVec4d& b){return FXVec4d(FXMAX(a.x,b.x),FXMAX(a.y,b.y),FXMAX(a.z,b.z),FXMAX(a.w,b.w));}
205
206 extern FXAPI FXVec4d plane(const FXVec4d& vec);
207 extern FXAPI FXVec4d plane(const FXVec3d& vec,FXdouble dist);
208 extern FXAPI FXVec4d plane(const FXVec3d& vec,const FXVec3d& p);
209 extern FXAPI FXVec4d plane(const FXVec3d& a,const FXVec3d& b,const FXVec3d& c);
210
211 extern FXAPI FXVec4d normalize(const FXVec4d& v);
212
213 extern FXAPI FXStream& operator<<(FXStream& store,const FXVec4d& v);
214 extern FXAPI FXStream& operator>>(FXStream& store,FXVec4d& v);
215
216 }
217
218 #endif
219