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,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: FXRangef.h,v 1.9.2.1 2006/03/21 07:08:29 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:
37   FXVec3f lower;
38   FXVec3f upper;
39 public:
40 
41   /// Default constructor
FXRangef()42   FXRangef(){}
43 
44   /// Copy constructor
FXRangef(const FXRangef & bounds)45   FXRangef(const FXRangef& bounds):lower(bounds.lower),upper(bounds.upper){}
46 
47   /// Initialize from two vectors
FXRangef(const FXVec3f & lo,const FXVec3f & hi)48   FXRangef(const FXVec3f& lo,const FXVec3f& hi):lower(lo),upper(hi){}
49 
50   /// Initialize from six numbers
FXRangef(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi)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   /// Indexing with 0..1
60   FXVec3f& operator[](FXint i){ return (&lower)[i]; }
61 
62   /// Indexing with 0..1
63   const FXVec3f& operator[](FXint i) const { return (&lower)[i]; }
64 
65   /// Width of box
width()66   FXfloat width() const { return upper.x-lower.x; }
67 
68   /// Height of box
height()69   FXfloat height() const { return upper.y-lower.y; }
70 
71   /// Depth of box
depth()72   FXfloat depth() const { return upper.z-lower.z; }
73 
74   /// Longest side
75   FXfloat longest() const;
76 
77   /// shortest side
78   FXfloat shortest() const;
79 
80   /// Length of diagonal
81   FXfloat diameter() const;
82 
83   /// Compute diagonal
84   FXVec3f diagonal() const;
85 
86   /// Get center of box
87   FXVec3f center() const;
88 
89   /// Test if empty
90   FXbool empty() const;
91 
92   /// Test if box contains point x,y,z
93   FXbool contains(FXfloat x,FXfloat y,FXfloat z) const;
94 
95   /// Test if box contains point p
96   FXbool contains(const FXVec3f& p) const;
97 
98   /// Test if box properly contains another box
99   FXbool contains(const FXRangef& bounds) const;
100 
101   /// Test if box properly contains sphere
102   FXbool contains(const FXSpheref& sphere) const;
103 
104   /// Include point
105   FXRangef& include(FXfloat x,FXfloat y,FXfloat z);
106 
107   /// Include point
108   FXRangef& include(const FXVec3f& v);
109 
110   /// Include given range into box
111   FXRangef& include(const FXRangef& box);
112 
113   /// Include given sphere into this box
114   FXRangef& include(const FXSpheref& sphere);
115 
116   /// Intersect box with normalized plane ax+by+cz+w; returns -1,0,+1
117   FXint intersect(const FXVec4f& plane) const;
118 
119   /// Intersect box with ray u-v
120   FXbool intersect(const FXVec3f& u,const FXVec3f& v);
121 
122   /// Test if boxes a and b overlap
123   friend FXAPI FXbool overlap(const FXRangef& a,const FXRangef& b);
124 
125   /// Get corner number 0..7
corner(FXint c)126   FXVec3f corner(FXint c) const { return FXVec3f((&lower)[c&1].x,(&lower)[(c>>1)&1].y,(&lower)[c>>2].z); }
127 
128   /// Union of two boxes
129   friend FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
130 
131   /// Intersection of two boxes
132   friend FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
133 
134   /// Save object to a stream
135   friend FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
136 
137   /// Load object from a stream
138   friend FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
139   };
140 
141 
142 extern FXAPI FXbool overlap(const FXRangef& a,const FXRangef& b);
143 extern FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
144 extern FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
145 extern FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
146 extern FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
147 
148 }
149 
150 #endif
151 
152