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