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

FXRangef.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *           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           *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2004,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXRangef.h,v 1.16 2006/01/22 17:58:08 fox Exp $                          *
00023 ********************************************************************************/
00024 #ifndef FXRANGEF_H
00025 #define FXRANGEF_H
00026 
00027 
00028 namespace FX {
00029 
00030 
00031 class FXSpheref;
00032 
00033 
00034 /// Bounds
00035 class FXAPI FXRangef {
00036 public:
00037   FXVec3f lower;
00038   FXVec3f upper;
00039 public:
00040 
00041   /// Default constructor
00042   FXRangef(){}
00043 
00044   /// Copy constructor
00045   FXRangef(const FXRangef& bounds):lower(bounds.lower),upper(bounds.upper){}
00046 
00047   /// Initialize from two vectors
00048   FXRangef(const FXVec3f& lo,const FXVec3f& hi):lower(lo),upper(hi){}
00049 
00050   /// Initialize from six numbers
00051   FXRangef(FXfloat xlo,FXfloat xhi,FXfloat ylo,FXfloat yhi,FXfloat zlo,FXfloat zhi):lower(xlo,ylo,zlo),upper(xhi,yhi,zhi){}
00052 
00053   /// Initialize box to fully contain the given bounding sphere
00054   FXRangef(const FXSpheref& sphere);
00055 
00056   /// Assignment
00057   FXRangef& operator=(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
00058 
00059   /// Set value from another range
00060   FXRangef& set(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
00061 
00062   /// Set value from two vectors
00063   FXRangef& set(const FXVec3f& lo,const FXVec3f& hi){ lower=lo; upper=hi; return *this; }
00064 
00065   /// Set value from six numbers
00066   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; }
00067 
00068   /// Indexing with 0..1
00069   FXVec3f& operator[](FXint i){ return (&lower)[i]; }
00070 
00071   /// Indexing with 0..1
00072   const FXVec3f& operator[](FXint i) const { return (&lower)[i]; }
00073 
00074   /// Comparison
00075   bool operator==(const FXRangef& r) const { return lower==r.lower && upper==r.upper; }
00076   bool operator!=(const FXRangef& r) const { return lower!=r.lower || upper!=r.upper; }
00077 
00078   /// Width of box
00079   FXfloat width() const { return upper.x-lower.x; }
00080 
00081   /// Height of box
00082   FXfloat height() const { return upper.y-lower.y; }
00083 
00084   /// Depth of box
00085   FXfloat depth() const { return upper.z-lower.z; }
00086 
00087   /// Longest side
00088   FXfloat longest() const;
00089 
00090   /// shortest side
00091   FXfloat shortest() const;
00092 
00093   /// Length of diagonal
00094   FXfloat diameter() const;
00095 
00096   /// Get radius of box
00097   FXfloat radius() const;
00098 
00099   /// Compute diagonal
00100   FXVec3f diagonal() const;
00101 
00102   /// Get center of box
00103   FXVec3f center() const;
00104 
00105   /// Test if empty
00106   bool empty() const;
00107 
00108   /// Test if box contains point x,y,z
00109   bool contains(FXfloat x,FXfloat y,FXfloat z) const;
00110 
00111   /// Test if box contains point p
00112   bool contains(const FXVec3f& p) const;
00113 
00114   /// Test if box properly contains another box
00115   bool contains(const FXRangef& bounds) const;
00116 
00117   /// Test if box properly contains sphere
00118   bool contains(const FXSpheref& sphere) const;
00119 
00120   /// Include point
00121   FXRangef& include(FXfloat x,FXfloat y,FXfloat z);
00122 
00123   /// Include point
00124   FXRangef& include(const FXVec3f& v);
00125 
00126   /// Include given range into box
00127   FXRangef& include(const FXRangef& box);
00128 
00129   /// Include given sphere into this box
00130   FXRangef& include(const FXSpheref& sphere);
00131 
00132   /// Intersect box with normalized plane ax+by+cz+w; returns -1,0,+1
00133   FXint intersect(const FXVec4f& plane) const;
00134 
00135   /// Intersect box with ray u-v
00136   bool intersect(const FXVec3f& u,const FXVec3f& v);
00137 
00138   /// Test if boxes a and b overlap
00139   friend FXAPI bool overlap(const FXRangef& a,const FXRangef& b);
00140 
00141   /// Get corner number 0..7
00142   FXVec3f corner(FXint c) const { return FXVec3f((&lower)[c&1].x,(&lower)[(c>>1)&1].y,(&lower)[c>>2].z); }
00143 
00144   /// Union of two boxes
00145   friend FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
00146 
00147   /// Intersection of two boxes
00148   friend FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
00149 
00150   /// Save object to a stream
00151   friend FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
00152 
00153   /// Load object from a stream
00154   friend FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
00155   };
00156 
00157 
00158 extern FXAPI bool overlap(const FXRangef& a,const FXRangef& b);
00159 
00160 extern FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
00161 extern FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
00162 
00163 extern FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
00164 extern FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
00165 
00166 }
00167 
00168 #endif
00169 

Copyright © 1997-2005 Jeroen van der Zijp