1 #ifndef COIN_SBBOX3F_H
2 #define COIN_SBBOX3F_H
3 
4 /**************************************************************************\
5  * Copyright (c) Kongsberg Oil & Gas Technologies AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 \**************************************************************************/
35 
36 #include <cstdio>
37 
38 #include <Inventor/SbVec3f.h>
39 
40 class SbBox3d;
41 class SbBox3i32;
42 class SbBox3s;
43 
44 class SbMatrix;
45 
46 class COIN_DLL_API SbBox3f {
47 public:
SbBox3f(void)48   SbBox3f(void) { makeEmpty(); }
SbBox3f(float xmin,float ymin,float zmin,float xmax,float ymax,float zmax)49   SbBox3f(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
50     : minpt(xmin, ymin, zmin), maxpt(xmax, ymax, zmax) { }
SbBox3f(const SbVec3f & minpoint,const SbVec3f & maxpoint)51   SbBox3f(const SbVec3f & minpoint, const SbVec3f & maxpoint)
52     : minpt(minpoint), maxpt(maxpoint) { }
SbBox3f(const SbBox3d & box)53   explicit SbBox3f(const SbBox3d & box) { setBounds(box); }
SbBox3f(const SbBox3s & box)54   explicit SbBox3f(const SbBox3s & box) { setBounds(box); }
SbBox3f(const SbBox3i32 & box)55   explicit SbBox3f(const SbBox3i32 & box) { setBounds(box); }
56 
setBounds(float xmin,float ymin,float zmin,float xmax,float ymax,float zmax)57   SbBox3f & setBounds(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax)
58     { minpt.setValue(xmin, ymin, zmin); maxpt.setValue(xmax, ymax, zmax); return *this; }
setBounds(const SbVec3f & minpoint,const SbVec3f & maxpoint)59   SbBox3f & setBounds(const SbVec3f & minpoint, const SbVec3f & maxpoint)
60     { minpt = minpoint; maxpt = maxpoint; return *this; }
61   SbBox3f & setBounds(const SbBox3d & box);
62   SbBox3f & setBounds(const SbBox3s & box);
63   SbBox3f & setBounds(const SbBox3i32 & box);
64 
getBounds(float & xmin,float & ymin,float & zmin,float & xmax,float & ymax,float & zmax)65   void getBounds(float & xmin, float & ymin, float & zmin, float & xmax, float & ymax, float & zmax) const
66     { minpt.getValue(xmin, ymin, zmin); maxpt.getValue(xmax, ymax, zmax); }
getBounds(SbVec3f & minpoint,SbVec3f & maxpoint)67   void getBounds(SbVec3f & minpoint, SbVec3f & maxpoint) const
68     { minpoint = minpt; maxpoint = maxpt; }
69 
getMin(void)70   const SbVec3f & getMin(void) const { return minpt; }
getMin(void)71   SbVec3f & getMin(void) { return minpt; }
getMax(void)72   const SbVec3f & getMax(void) const { return maxpt; }
getMax(void)73   SbVec3f & getMax(void) { return maxpt; }
74 
75   void extendBy(const SbVec3f & pt);
76   void extendBy(const SbBox3f & box);
77   void transform(const SbMatrix & matrix);
78   void makeEmpty(void);
isEmpty(void)79   SbBool isEmpty(void) const { return maxpt[0] < minpt[0]; }
hasVolume(void)80   SbBool hasVolume(void) const
81     { return ((maxpt[0] > minpt[0]) && (maxpt[1] > minpt[1]) && (maxpt[2] > minpt[2])); }
getVolume(void)82   float getVolume(void) const
83     { float dx = 0.0f, dy = 0.0f, dz = 0.0f; getSize(dx, dy, dz); return (dx * dy * dz); }
84 
85   SbBool intersect(const SbVec3f & pt) const;
86   SbBool intersect(const SbBox3f & box) const;
87   SbVec3f getClosestPoint(const SbVec3f & point) const;
88   SbBool outside(const SbMatrix & mvp, int & cullbits) const;
89 
getCenter(void)90   SbVec3f getCenter(void) const { return (minpt + maxpt) * 0.5f; }
getOrigin(float & originX,float & originY,float & originZ)91   void getOrigin(float & originX, float & originY, float & originZ) const
92     { minpt.getValue(originX, originY, originZ); }
getSize(float & sizeX,float & sizeY,float & sizeZ)93   void getSize(float & sizeX, float & sizeY, float & sizeZ) const
94     { if (isEmpty()) { sizeX = sizeY = sizeZ = 0; }
95       else { sizeX = maxpt[0] - minpt[0]; sizeY = maxpt[1] - minpt[1]; sizeZ = maxpt[2] - minpt[2]; } }
96 
getSize(void)97   SbVec3f getSize(void) const {
98     SbVec3f v;
99     this->getSize(v[0], v[1], v[2]);
100     return v;
101   }
102   void getSpan(const SbVec3f & dir, float & dmin, float & dmax) const;
103 
104   void print(FILE * file) const;
105 
106 private:
107   SbVec3f minpt, maxpt;
108 
109 }; // SbBox3f
110 
111 COIN_DLL_API inline int operator == (const SbBox3f & b1, const SbBox3f & b2) {
112   return ((b1.getMin() == b2.getMin()) && (b1.getMax() == b2.getMax()));
113 }
114 
115 COIN_DLL_API inline int operator != (const SbBox3f & b1, const SbBox3f & b2) {
116   return !(b1 == b2);
117 }
118 
119 #endif // !COIN_SBBOX3F_H
120