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

FXVec4d.h
Go to the documentation of this file.
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,v 1.27 2006/01/22 17:58:12 fox Exp $ *
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:
41 public:
42 
43  /// Default constructor
44  FXVec4d(){}
45 
46  /// Initialize from another vector
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
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
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
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
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
150  FXdouble length2() const { return x*x+y*y+z*z+w*w; }
151  FXdouble length() const { return sqrt(length2()); }
152 
153  /// Clamp values of vector between limits
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 
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));}
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
FXdouble w
Definition: FXVec4d.h:40
FXVec2d lo(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:174
FXMat3d operator/(const FXMat3d &a, FXdouble x)
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXVec4d.h:62
FXStream & operator>>(FXStream &store, FXDate &d)
FXVec4d(FXdouble xx, FXdouble yy, FXdouble zz, FXdouble ww=1.0)
Initialize with components.
Definition: FXVec4d.h:56
FXVec4d & operator=(const FXVec3d &v)
Assignment.
Definition: FXVec4d.h:71
bool operator==(const FXVec4d &v) const
Equality tests.
Definition: FXVec4d.h:125
#define FXCLAMP(lo, x, hi)
Clamp value x to range [lo..hi].
Definition: fxdefs.h:502
FXdouble x
Definition: FXVec4d.h:37
FXVec4d & operator=(const FXVec4d &v)
Definition: FXVec4d.h:72
FXVec4d & clamp(FXdouble lo, FXdouble hi)
Clamp values of vector between limits.
Definition: FXVec4d.h:154
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec4d.h:65
Double-precision 3-element vector.
Definition: FXVec3d.h:36
Double-precision 4x4 matrix.
Definition: FXMat4d.h:32
bool operator<(const FXVec4d &v) const
Inequality tests.
Definition: FXVec4d.h:134
FXVec4d operator+(const FXVec4d &v) const
Vector and vector.
Definition: FXVec4d.h:106
#define FXAPI
Definition: fxdefs.h:122
FXVec4d(const FXVec4d &v)
Initialize from another vector.
Definition: FXVec4d.h:47
FXVec4d & set(FXdouble xx, FXdouble yy, FXdouble zz, FXdouble ww)
Set value from components.
Definition: FXVec4d.h:84
FXVec4d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec4d.h:87
bool operator!=(const FXVec4d &v) const
Definition: FXVec4d.h:126
FXVec4d & set(const FXdouble v[])
Set value from array of floats.
Definition: FXVec4d.h:81
bool operator<=(const FXString &s1, const FXString &s2)
FXuint FXColor
Definition: fxdefs.h:447
FXdouble operator*(const FXVec4d &v) const
Dot product.
Definition: FXVec4d.h:119
FXdouble z
Definition: FXVec4d.h:39
bool operator>=(const FXString &s1, const FXString &s2)
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
FXString normalize(const FXString &s)
bool operator>(const FXVec4d &v) const
Definition: FXVec4d.h:136
FXVec4d & set(const FXVec4d &v)
Set value from another vector.
Definition: FXVec4d.h:78
FXdouble y
Definition: FXVec3d.h:39
double FXdouble
Definition: fxdefs.h:392
FXdouble x
Definition: FXVec3d.h:38
bool operator!() const
Test if zero.
Definition: FXVec4d.h:122
#define FXMAX(a, b)
Return the maximum of a or b.
Definition: fxdefs.h:481
FXVec4d & operator-=(const FXVec4d &v)
Definition: FXVec4d.h:90
Definition: FX4Splitter.h:31
int FXint
Definition: fxdefs.h:390
FXVec4d & operator+=(const FXVec4d &v)
Definition: FXVec4d.h:89
Double-precision 4-element vector.
Definition: FXVec4d.h:35
FXdouble y
Definition: FXVec4d.h:38
FXdouble z
Definition: FXVec3d.h:40
bool operator<=(const FXVec4d &v) const
Definition: FXVec4d.h:135
FXVec2d hi(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:175
#define FXMIN(a, b)
Return the minimum of a or b.
Definition: fxdefs.h:484
FXdouble length() const
Definition: FXVec4d.h:151
FXVec4d operator-(const FXVec4d &v) const
Definition: FXVec4d.h:107
FXVec4d operator-() const
Definition: FXVec4d.h:103
FXVec4d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec4d.h:75
bool operator<(const FXString &s1, const FXString &s2)
FXdouble length2() const
Length and square of length.
Definition: FXVec4d.h:150
bool operator>(const FXString &s1, const FXString &s2)
FXVec4d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec4d.h:53
bool operator>=(const FXVec4d &v) const
Definition: FXVec4d.h:137
FXVec4d operator+() const
Unary.
Definition: FXVec4d.h:102
FXVec4d(const FXVec3d &v, FXdouble ww=1.0)
Construct with 3-vector and optional scalar.
Definition: FXVec4d.h:50
FXVec4d & operator/=(FXdouble n)
Definition: FXVec4d.h:88
FXVec4d plane(const FXVec4d &vec)
FXStream & operator<<(FXStream &store, const FXDate &d)
bool operator!=(const FXString &s1, const FXString &s2)
bool operator==(const FXString &s1, const FXString &s2)
FXMat3d operator*(FXdouble x, const FXMat3d &a)
FXVec4d()
Default constructor.
Definition: FXVec4d.h:44

Copyright © 1997-2005 Jeroen van der Zijp