1 /********************************************************************************
2 *                                                                               *
3 *           D o u b l e - P r e c i s i o n    S p h e r 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: FXSphered.h,v 1.17 2006/01/22 17:58:09 fox Exp $                         *
23 ********************************************************************************/
24 #ifndef FXSPHERED_H
25 #define FXSPHERED_H
26 
27 
28 namespace FX {
29 
30 
31 class FXRanged;
32 
33 
34 /// Spherical bounds
35 class FXAPI FXSphered {
36 public:
37   FXVec3d  center;
38   FXdouble radius;
39 public:
40 
41   /// Default constructor
FXSphered()42   FXSphered(){}
43 
44   /// Copy constructor
FXSphered(const FXSphered & sphere)45   FXSphered(const FXSphered& sphere):center(sphere.center),radius(sphere.radius){}
46 
47   /// Initialize from center and radius
center(cen)48   FXSphered(const FXVec3d& cen,FXdouble rad=0.0):center(cen),radius(rad){}
49 
50   /// Initialize from center and radius
center(x,y,z)51   FXSphered(FXdouble x,FXdouble y,FXdouble z,FXdouble rad=0.0):center(x,y,z),radius(rad){}
52 
53   /// Initialize sphere to fully contain the given bounding box
54   FXSphered(const FXRanged& bounds);
55 
56   /// Assignment
57   FXSphered& operator=(const FXSphered& sphere){ center=sphere.center; radius=sphere.radius; return *this; }
58 
59   /// Set value from another sphere
set(const FXSphered & sphere)60   FXSphered& set(const FXSphered& sphere){ center=sphere.center; radius=sphere.radius; return *this; }
61 
62   /// Set value from center and radius
set(const FXVec3d & cen,FXdouble rad)63   FXSphered& set(const FXVec3d& cen,FXdouble rad){ center=cen; radius=rad; return *this; }
64 
65   /// Set value from center and radius
set(FXdouble x,FXdouble y,FXdouble z,FXdouble rad)66   FXSphered& set(FXdouble x,FXdouble y,FXdouble z,FXdouble rad){ center.set(x,y,z); radius=rad; return *this; }
67 
68   /// Comparison
69   bool operator==(const FXSphered& s) const { return center==s.center && radius==s.radius;}
70   bool operator!=(const FXSphered& s) const { return center!=s.center || radius!=s.radius;}
71 
72   /// Diameter of sphere
diameter()73   FXdouble diameter() const { return radius*2.0; }
74 
75   /// Test if empty
empty()76   bool empty() const { return radius<0.0; }
77 
78   /// Test if sphere contains point x,y,z
79   bool contains(FXdouble x,FXdouble y,FXdouble z) const;
80 
81   /// Test if sphere contains point p
82   bool contains(const FXVec3d& p) const;
83 
84   /// Test if sphere contains another box
85   bool contains(const FXRanged& box) const;
86 
87   /// Test if sphere contains another sphere
88   bool contains(const FXSphered& sphere) const;
89 
90   /// Include point
91   FXSphered& include(FXdouble x,FXdouble y,FXdouble z);
92 
93   /// Include point
94   FXSphered& include(const FXVec3d& p);
95 
96   /// Expand radius to include point
97   FXSphered& includeInRadius(FXdouble x,FXdouble y,FXdouble z);
98 
99   /// Expand radius to include point
100   FXSphered& includeInRadius(const FXVec3d& p);
101 
102   /// Include given range into this one
103   FXSphered& include(const FXRanged& box);
104 
105   /// Expand radius to include box
106   FXSphered& includeInRadius(const FXRanged& box);
107 
108   /// Include given sphere into this one
109   FXSphered& include(const FXSphered& sphere);
110 
111   /// Expand radius to include sphere
112   FXSphered& includeInRadius(const FXSphered& sphere);
113 
114   /// Intersect sphere with normalized plane ax+by+cz+w; returns -1,0,+1
115   FXint intersect(const FXVec4d& plane) const;
116 
117   /// Intersect sphere with ray u-v
118   bool intersect(const FXVec3d& u,const FXVec3d& v) const;
119 
120   /// Test if box overlaps with sphere
121   friend FXAPI bool overlap(const FXRanged& a,const FXSphered& b);
122 
123   /// Test if sphere overlaps with box
124   friend FXAPI bool overlap(const FXSphered& a,const FXRanged& b);
125 
126   /// Test if spheres overlap
127   friend FXAPI bool overlap(const FXSphered& a,const FXSphered& b);
128 
129   /// Save object to a stream
130   friend FXAPI FXStream& operator<<(FXStream& store,const FXSphered& sphere);
131 
132   /// Load object from a stream
133   friend FXAPI FXStream& operator>>(FXStream& store,FXSphered& sphere);
134   };
135 
136 
137 extern FXAPI bool overlap(const FXRanged& a,const FXSphered& b);
138 extern FXAPI bool overlap(const FXSphered& a,const FXRanged& b);
139 extern FXAPI bool overlap(const FXSphered& a,const FXSphered& b);
140 
141 extern FXAPI FXStream& operator<<(FXStream& store,const FXSphered& sphere);
142 extern FXAPI FXStream& operator>>(FXStream& store,FXSphered& sphere);
143 
144 }
145 
146 #endif
147