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

FXRangef.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * S i n g l e - P r e c i s i o n R a n g e C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2004,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: FXRangef.h,v 1.16 2006/01/22 17:58:08 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXRANGEF_H
25 #define FXRANGEF_H
26 
27 
28 namespace FX {
29 
30 
31 class FXSpheref;
32 
33 
34 /// Bounds
35 class FXAPI FXRangef {
36 public:
39 public:
40 
41  /// Default constructor
43 
44  /// Copy constructor
45  FXRangef(const FXRangef& bounds):lower(bounds.lower),upper(bounds.upper){}
46 
47  /// Initialize from two vectors
48  FXRangef(const FXVec3f& lo,const FXVec3f& hi):lower(lo),upper(hi){}
49 
50  /// Initialize from six numbers
51  FXRangef(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi):lower(xlo,ylo,zlo),upper(xhi,yhi,zhi){}
52 
53  /// Initialize box to fully contain the given bounding sphere
54  FXRangef(const FXSpheref& sphere);
55 
56  /// Assignment
57  FXRangef& operator=(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
58 
59  /// Set value from another range
60  FXRangef& set(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
61 
62  /// Set value from two vectors
63  FXRangef& set(const FXVec3f& lo,const FXVec3f& hi){ lower=lo; upper=hi; return *this; }
64 
65  /// Set value from six numbers
66  FXRangef& set(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi){ lower.set(xlo,ylo,zlo); upper.set(xhi,yhi,zhi); return *this; }
67 
68  /// Indexing with 0..1
69  FXVec3f& operator[](FXint i){ return (&lower)[i]; }
70 
71  /// Indexing with 0..1
72  const FXVec3f& operator[](FXint i) const { return (&lower)[i]; }
73 
74  /// Comparison
75  bool operator==(const FXRangef& r) const { return lower==r.lower && upper==r.upper; }
76  bool operator!=(const FXRangef& r) const { return lower!=r.lower || upper!=r.upper; }
77 
78  /// Width of box
79  FXfloat width() const { return upper.x-lower.x; }
80 
81  /// Height of box
82  FXfloat height() const { return upper.y-lower.y; }
83 
84  /// Depth of box
85  FXfloat depth() const { return upper.z-lower.z; }
86 
87  /// Longest side
88  FXfloat longest() const;
89 
90  /// shortest side
91  FXfloat shortest() const;
92 
93  /// Length of diagonal
94  FXfloat diameter() const;
95 
96  /// Get radius of box
97  FXfloat radius() const;
98 
99  /// Compute diagonal
100  FXVec3f diagonal() const;
101 
102  /// Get center of box
103  FXVec3f center() const;
104 
105  /// Test if empty
106  bool empty() const;
107 
108  /// Test if box contains point x,y,z
109  bool contains(FXfloat x,FXfloat y,FXfloat z) const;
110 
111  /// Test if box contains point p
112  bool contains(const FXVec3f& p) const;
113 
114  /// Test if box properly contains another box
115  bool contains(const FXRangef& bounds) const;
116 
117  /// Test if box properly contains sphere
118  bool contains(const FXSpheref& sphere) const;
119 
120  /// Include point
121  FXRangef& include(FXfloat x,FXfloat y,FXfloat z);
122 
123  /// Include point
124  FXRangef& include(const FXVec3f& v);
125 
126  /// Include given range into box
127  FXRangef& include(const FXRangef& box);
128 
129  /// Include given sphere into this box
130  FXRangef& include(const FXSpheref& sphere);
131 
132  /// Intersect box with normalized plane ax+by+cz+w; returns -1,0,+1
133  FXint intersect(const FXVec4f& plane) const;
134 
135  /// Intersect box with ray u-v
136  bool intersect(const FXVec3f& u,const FXVec3f& v);
137 
138  /// Test if boxes a and b overlap
139  friend FXAPI bool overlap(const FXRangef& a,const FXRangef& b);
140 
141  /// Get corner number 0..7
142  FXVec3f corner(FXint c) const { return FXVec3f((&lower)[c&1].x,(&lower)[(c>>1)&1].y,(&lower)[c>>2].z); }
143 
144  /// Union of two boxes
145  friend FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
146 
147  /// Intersection of two boxes
148  friend FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
149 
150  /// Save object to a stream
151  friend FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
152 
153  /// Load object from a stream
154  friend FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
155  };
156 
157 
158 extern FXAPI bool overlap(const FXRangef& a,const FXRangef& b);
159 
160 extern FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
161 extern FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
162 
163 extern FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
164 extern FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
165 
166 }
167 
168 #endif
169 
FXVec2d lo(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:174
FXStream & operator>>(FXStream &store, FXDate &d)
FXfloat z
Definition: FXVec3f.h:40
FXRangef & set(FXfloat xlo, FXfloat xhi, FXfloat ylo, FXfloat yhi, FXfloat zlo, FXfloat zhi)
Set value from six numbers.
Definition: FXRangef.h:66
FXVec3f lower
Definition: FXRangef.h:37
FXfloat width() const
Width of box.
Definition: FXRangef.h:79
FXExtentd intersect(const FXExtentd &a, const FXExtentd &b)
const FXVec3f & operator[](FXint i) const
Indexing with 0..1.
Definition: FXRangef.h:72
FXRangef & set(const FXVec3f &lo, const FXVec3f &hi)
Set value from two vectors.
Definition: FXRangef.h:63
FXfloat depth() const
Depth of box.
Definition: FXRangef.h:85
#define FXAPI
Definition: fxdefs.h:122
FXRangef(const FXRangef &bounds)
Copy constructor.
Definition: FXRangef.h:45
FXExtentd unite(const FXExtentd &a, const FXExtentd &b)
FXRangef(const FXVec3f &lo, const FXVec3f &hi)
Initialize from two vectors.
Definition: FXRangef.h:48
A stream is a way to serialize data and objects into a byte stream.
Definition: FXStream.h:99
FXfloat x
Definition: FXVec3f.h:38
Definition: FX4Splitter.h:31
int FXint
Definition: fxdefs.h:397
FXVec3f & operator[](FXint i)
Indexing with 0..1.
Definition: FXRangef.h:69
FXfloat y
Definition: FXVec3f.h:39
bool overlap(const FXExtentd &a, const FXExtentd &b)
FXVec2d hi(const FXVec2d &a, const FXVec2d &b)
Definition: FXVec2d.h:175
Single-precision 3-element vector.
Definition: FXVec3f.h:36
Spherical bounds.
Definition: FXSpheref.h:35
Bounds.
Definition: FXRangef.h:35
bool operator==(const FXRangef &r) const
Comparison.
Definition: FXRangef.h:75
float FXfloat
Definition: fxdefs.h:398
FXRangef()
Default constructor.
Definition: FXRangef.h:42
FXRangef(FXfloat xlo, FXfloat xhi, FXfloat ylo, FXfloat yhi, FXfloat zlo, FXfloat zhi)
Initialize from six numbers.
Definition: FXRangef.h:51
FXVec3f upper
Definition: FXRangef.h:38
FXVec4d plane(const FXVec4d &vec)
FXStream & operator<<(FXStream &store, const FXDate &d)
FXRangef & set(const FXRangef &bounds)
Set value from another range.
Definition: FXRangef.h:60
FXVec3f & set(const FXVec3f &v)
Set value from another vector.
Definition: FXVec3f.h:74
FXfloat height() const
Height of box.
Definition: FXRangef.h:82
FXRangef & operator=(const FXRangef &bounds)
Assignment.
Definition: FXRangef.h:57
bool operator!=(const FXRangef &r) const
Definition: FXRangef.h:76
FXVec3f corner(FXint c) const
Get corner number 0..7.
Definition: FXRangef.h:142
Single-precision 4-element vector.
Definition: FXVec4f.h:35

Copyright © 1997-2005 Jeroen van der Zijp