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,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXRANGEF_H
00022 #define FXRANGEF_H
00023 
00024 
00025 namespace FX {
00026 
00027 
00028 class FXSpheref;
00029 class FXMat4f;
00030 
00031 
00032 /// Bounds
00033 class FXAPI FXRangef {
00034 public:
00035   FXVec3f lower;
00036   FXVec3f upper;
00037 public:
00038 
00039   /// Default constructor; value is not initialized
00040   FXRangef(){}
00041 
00042   /// Initialize with another range
00043   FXRangef(const FXRangef& bounds):lower(bounds.lower),upper(bounds.upper){}
00044 
00045   /// Initialize with a single point
00046   FXRangef(const FXVec3f& p):lower(p),upper(p){}
00047 
00048   /// Initialize with corner points
00049   FXRangef(const FXVec3f& l,const FXVec3f& h):lower(l),upper(h){}
00050 
00051   /// Initialize with a single point
00052   FXRangef(FXfloat x,FXfloat y,FXfloat z):lower(x,y,z),upper(x,y,z){}
00053 
00054   /// Initialize with explicit values
00055   FXRangef(FXfloat xl,FXfloat xh,FXfloat yl,FXfloat yh,FXfloat zl,FXfloat zh):lower(xl,yl,zl),upper(xh,yh,zh){}
00056 
00057   /// Initialize box to fully contain the given bounding sphere
00058   FXRangef(const FXSpheref& sphere);
00059 
00060   /// Assignment
00061   FXRangef& operator=(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
00062 
00063   /// Set value from another range
00064   FXRangef& set(const FXRangef& bounds){ lower=bounds.lower; upper=bounds.upper; return *this; }
00065 
00066   /// Set value from single point
00067   FXRangef& set(const FXVec3f& p){ lower=upper=p; return *this; }
00068 
00069   /// Set value from corner points
00070   FXRangef& set(const FXVec3f& l,const FXVec3f& h){ lower=l; upper=h; return *this; }
00071 
00072   /// Set value from single point
00073   FXRangef& set(FXfloat x,FXfloat y,FXfloat z){ lower.x=upper.x=x; lower.y=upper.y=y; lower.z=upper.z=z; return *this; }
00074 
00075   /// Set value from explicit values
00076   FXRangef& set(FXfloat xl,FXfloat xh,FXfloat yl,FXfloat yh,FXfloat zl,FXfloat zh){ lower.set(xl,yl,zl); upper.set(xh,yh,zh); return *this; }
00077 
00078   /// Indexing with 0..1
00079   FXVec3f& operator[](FXint i){ return (&lower)[i]; }
00080 
00081   /// Indexing with 0..1
00082   const FXVec3f& operator[](FXint i) const { return (&lower)[i]; }
00083 
00084   /// Comparison
00085   FXbool operator==(const FXRangef& r) const { return lower==r.lower && upper==r.upper; }
00086   FXbool operator!=(const FXRangef& r) const { return lower!=r.lower || upper!=r.upper; }
00087 
00088   /// Width of box
00089   FXfloat width() const { return upper.x-lower.x; }
00090 
00091   /// Height of box
00092   FXfloat height() const { return upper.y-lower.y; }
00093 
00094   /// Depth of box
00095   FXfloat depth() const { return upper.z-lower.z; }
00096 
00097   /// Longest side
00098   FXfloat longest() const;
00099 
00100   /// shortest side
00101   FXfloat shortest() const;
00102 
00103   /// Length of diagonal
00104   FXfloat diameter() const;
00105 
00106   /// Get radius of box
00107   FXfloat radius() const;
00108 
00109   /// Compute diagonal
00110   FXVec3f diagonal() const;
00111 
00112   /// Get center of box
00113   FXVec3f center() const;
00114 
00115   /// Test if empty
00116   FXbool empty() const;
00117 
00118   /// Test if box contains point x,y,z
00119   FXbool contains(FXfloat x,FXfloat y,FXfloat z) const;
00120 
00121   /// Test if box contains point p
00122   FXbool contains(const FXVec3f& p) const;
00123 
00124   /// Test if box properly contains another box
00125   FXbool contains(const FXRangef& bounds) const;
00126 
00127   /// Test if box properly contains sphere
00128   FXbool contains(const FXSpheref& sphere) const;
00129 
00130   /// Include point
00131   FXRangef& include(FXfloat x,FXfloat y,FXfloat z);
00132 
00133   /// Include point
00134   FXRangef& include(const FXVec3f& v);
00135 
00136   /// Include given range into box
00137   FXRangef& include(const FXRangef& box);
00138 
00139   /// Include given sphere into this box
00140   FXRangef& include(const FXSpheref& sphere);
00141 
00142   /// Intersect box with normalized plane ax+by+cz+w; returns -1,0,+1
00143   FXint intersect(const FXVec4f& plane) const;
00144 
00145   /// Intersect box with ray u-v
00146   FXbool intersect(const FXVec3f& u,const FXVec3f& v);
00147 
00148   /// Get corner number 0..7
00149   FXVec3f corner(FXint c) const { return FXVec3f((&lower)[c&1].x,(&lower)[(c>>1)&1].y,(&lower)[c>>2].z); }
00150 
00151   /// Transform range by 4x4 matrix
00152   FXRangef transform(const FXMat4f& mat) const;
00153   };
00154 
00155 
00156 /// Test if boxes a and b overlap
00157 extern FXAPI FXbool overlap(const FXRangef& a,const FXRangef& b);
00158 
00159 /// Union of two boxes
00160 extern FXAPI FXRangef unite(const FXRangef& a,const FXRangef& b);
00161 
00162 /// Intersection of two boxes
00163 extern FXAPI FXRangef intersect(const FXRangef& a,const FXRangef& b);
00164 
00165 /// Save object to a stream
00166 extern FXAPI FXStream& operator<<(FXStream& store,const FXRangef& bounds);
00167 
00168 /// Load object from a stream
00169 extern FXAPI FXStream& operator>>(FXStream& store,FXRangef& bounds);
00170 
00171 }
00172 
00173 #endif
00174 

Copyright © 1997-2010 Jeroen van der Zijp