Blender V2.61 - r43446

util_boundbox.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #ifndef __UTIL_BOUNDBOX_H__
00020 #define __UTIL_BOUNDBOX_H__
00021 
00022 #include <math.h>
00023 #include <float.h>
00024 
00025 #include "util_math.h"
00026 #include "util_transform.h"
00027 #include "util_types.h"
00028 
00029 using namespace std;
00030 
00031 CCL_NAMESPACE_BEGIN
00032 
00033 class BoundBox
00034 {
00035 public:
00036     float3 min, max;
00037 
00038     BoundBox(void)
00039     {
00040         min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX);
00041         max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
00042     }
00043 
00044     BoundBox(const float3& min_, const float3& max_)
00045     : min(min_), max(max_)
00046     {
00047     }
00048 
00049     void grow(const float3& pt)  
00050     {
00051         min = ccl::min(min, pt);
00052         max = ccl::max(max, pt);
00053     }
00054 
00055     void grow(const BoundBox& bbox)
00056     {
00057         grow(bbox.min);
00058         grow(bbox.max);
00059     }
00060 
00061     void intersect(const BoundBox& bbox) 
00062     {
00063         min = ccl::max(min, bbox.min);
00064         max = ccl::min(max, bbox.max);
00065     }
00066 
00067     float area(void) const
00068     {
00069         if(!valid())
00070             return 0.0f;
00071 
00072         float3 d = max - min;
00073         return dot(d, d)*2.0f;
00074     }
00075 
00076     bool valid(void) const
00077     {
00078         return (min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z) &&
00079                !(isnan(min.x) || isnan(min.y) || isnan(min.z)) &&
00080                !(isnan(max.x) || isnan(max.y) || isnan(max.z));
00081     }
00082 
00083     BoundBox transformed(const Transform *tfm)
00084     {
00085         BoundBox result;
00086 
00087         for(int i = 0; i < 8; i++) {
00088             float3 p;
00089 
00090             p.x = (i & 1)? min.x: max.x;
00091             p.y = (i & 2)? min.y: max.y;
00092             p.z = (i & 4)? min.z: max.z;
00093 
00094             result.grow(transform(tfm, p));
00095         }
00096 
00097         return result;
00098     }
00099 };
00100 
00101 CCL_NAMESPACE_END
00102 
00103 #endif /* __UTIL_BOUNDBOX_H__ */
00104