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

FXVec3d.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 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,v 1.22 2006/01/22 17:58:12 fox Exp $ *
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:
41 public:
42 
43  /// Default constructor
44  FXVec3d(){}
45 
46  /// Initialize from another vector
47  FXVec3d(const FXVec3d& v){x=v.x;y=v.y;z=v.z;}
48 
49  /// Initialize from array of doubles
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
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
77  FXVec3d& set(const FXdouble v[]){x=v[0];y=v[1];z=v[2];return *this;}
78 
79  /// Set value from components
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
150  FXdouble length2() const { return x*x+y*y+z*z; }
151  FXdouble length() const { return sqrt(length2()); }
152 
153  /// Clamp values of vector between limits
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 
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));}
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
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: FXVec3d.h:59
FXStream & operator>>(FXStream &store, FXDate &d)
FXVec3d operator-() const
Definition: FXVec3d.h:99
FXdouble operator*(const FXVec3d &v) const
Dot product.
Definition: FXVec3d.h:116
FXVec3d(const FXdouble v[])
Initialize from array of doubles.
Definition: FXVec3d.h:50
FXVec3d & operator=(const FXVec3d &v)
Assignment.
Definition: FXVec3d.h:68
#define FXCLAMP(lo, x, hi)
Clamp value x to range [lo..hi].
Definition: fxdefs.h:509
FXVec3d & operator*=(FXdouble n)
Assigning operators.
Definition: FXVec3d.h:83
FXVec3d & clamp(FXdouble lo, FXdouble hi)
Clamp values of vector between limits.
Definition: FXVec3d.h:154
Double-precision 3-element vector.
Definition: FXVec3d.h:36
Double-precision 4x4 matrix.
Definition: FXMat4d.h:32
bool operator==(const FXVec3d &v) const
Equality tests.
Definition: FXVec3d.h:125
FXVec3d & set(const FXVec3d &v)
Set value from another vector.
Definition: FXVec3d.h:74
#define FXAPI
Definition: fxdefs.h:122
FXVec3d operator-(const FXVec3d &v) const
Definition: FXVec3d.h:103
FXVec3d & set(const FXdouble v[])
Set value from array of floats.
Definition: FXVec3d.h:77
FXdouble length() const
Definition: FXVec3d.h:151
bool operator<=(const FXString &s1, const FXString &s2)
FXVec3d operator+() const
Unary.
Definition: FXVec3d.h:98
FXVec3d normal(const FXVec3d &a, const FXVec3d &b, const FXVec3d &c)
FXuint FXColor
Definition: fxdefs.h:454
bool operator>=(const FXString &s1, const FXString &s2)
FXVec3d()
Default constructor.
Definition: FXVec3d.h:44
FXVec3d & operator=(const FXdouble v[])
Assignment from array of doubles.
Definition: FXVec3d.h:71
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
FXString normalize(const FXString &s)
FXVec3d(const FXVec3d &v)
Initialize from another vector.
Definition: FXVec3d.h:47
FXdouble y
Definition: FXVec3d.h:39
double FXdouble
Definition: fxdefs.h:399
FXVec3d operator+(const FXVec3d &v) const
Vector and vector.
Definition: FXVec3d.h:102
FXdouble x
Definition: FXVec3d.h:38
#define FXMAX(a, b)
Return the maximum of a or b.
Definition: fxdefs.h:488
bool operator>(const FXVec3d &v) const
Definition: FXVec3d.h:136
int FXint
Definition: fxdefs.h:397
bool operator<(const FXVec3d &v) const
Inequality tests.
Definition: FXVec3d.h:134
FXdouble z
Definition: FXVec3d.h:40
FXVec2d hi(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:175
bool operator>=(const FXVec3d &v) const
Definition: FXVec3d.h:137
#define FXMIN(a, b)
Return the minimum of a or b.
Definition: fxdefs.h:491
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXVec3d.h:62
bool operator<=(const FXVec3d &v) const
Definition: FXVec3d.h:135
Double-precision 2-element vector.
Definition: FXVec2d.h:35
FXdouble length2() const
Length and square of length.
Definition: FXVec3d.h:150
bool operator<(const FXString &s1, const FXString &s2)
FXVec3d & operator-=(const FXVec3d &v)
Definition: FXVec3d.h:86
bool operator>(const FXString &s1, const FXString &s2)
bool operator!() const
Test if zero.
Definition: FXVec3d.h:122
FXVec3d & operator+=(const FXVec3d &v)
Definition: FXVec3d.h:85
FXVec3d(FXdouble xx, FXdouble yy, FXdouble zz=1.0)
Initialize with components.
Definition: FXVec3d.h:53
Double-precision 3x3 matrix.
Definition: FXMat3d.h:35
FXStream & operator<<(FXStream &store, const FXDate &d)
FXVec3d & set(FXdouble xx, FXdouble yy, FXdouble zz)
Set value from components.
Definition: FXVec3d.h:80
bool operator!=(const FXString &s1, const FXString &s2)
FXVec3d operator^(const FXVec3d &v) const
Cross product.
Definition: FXVec3d.h:119
bool operator==(const FXString &s1, const FXString &s2)
FXMat3d operator*(FXdouble x, const FXMat3d &a)
FXVec3d & operator/=(FXdouble n)
Definition: FXVec3d.h:84
bool operator!=(const FXVec3d &v) const
Definition: FXVec3d.h:126

Copyright © 1997-2005 Jeroen van der Zijp