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