00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #ifndef util_bounds_BoundingCylinder_hpp 00023 #define util_bounds_BoundingCylinder_hpp 00024 00025 #include "../type/util_type.hpp" 00026 #include "../vecmath/Point3.hpp" 00027 #include "util_bounds.hpp" 00028 #include "util/error/Log.hpp" 00029 00030 00031 namespace se_core { 00032 class _SeCoreExport BoundingCylinder { 00033 public: 00034 BoundingCylinder() : centerX_(0), centerZ_(0), minY_(0), maxY_(0), radius_(0) {} 00035 00036 BoundingCylinder(const Point3& p, coor_t radius) 00037 : centerX_(p.x_), centerZ_(p.z_) 00038 , minY_(p.y_ - radius), maxY_(p.y_ + radius) 00039 , radius_(radius) { 00040 } 00041 00042 BoundingCylinder(const Point3& p, coor_t radius, coor_t height) 00043 : centerX_(p.x_), centerZ_(p.z_) 00044 , minY_(p.y_), maxY_(p.y_ + height) 00045 , radius_(radius) { 00046 } 00047 00048 BoundingCylinder(const Point3& p, const BoundingCylinder& b) 00049 : centerX_(p.x_ + b.centerX_), centerZ_(p.z_ + b.centerZ_) 00050 , minY_(p.y_ + b.minY_), maxY_(p.y_ + b.maxY_) 00051 , radius_(b.radius_) { 00052 } 00053 00054 BoundingCylinder(const Point3& p, const BoundingBox& b); 00055 00056 void reset() { 00057 centerX_ = centerZ_ = 0; 00058 minY_ = maxY_ = 0; 00059 radius_ = 0; 00060 } 00061 00062 bool isNull() const { 00063 return (radius_ == 0); 00064 } 00065 00066 00067 bool hasInside(coor_t x, coor_t y, coor_t z) const { 00068 if(y < minY_ || y > maxY_) 00069 return false; 00070 00071 coor_t xDist = x - centerX_; 00072 coor_t zDist = z - centerZ_; 00073 00074 if(xDist * xDist + zDist * zDist > radius_ * radius_) 00075 return false; 00076 00077 return true; 00078 } 00079 00080 00081 bool hasInside(const Point3& p) const { 00082 if(p.y_ <= minY_ || p.y_ >= maxY_) 00083 return false; 00084 00085 coor_t xDist = p.x_ - centerX_; 00086 coor_t zDist = p.z_ - centerZ_; 00087 00088 if(xDist * xDist + zDist * zDist >= radius_ * radius_) 00089 return false; 00090 00091 return true; 00092 } 00093 00094 bool isTouching(const Point3& p) const { 00095 if(p.y_ < minY_ || p.y_ > maxY_) 00096 return false; 00097 00098 coor_t xDist = p.x_ - centerX_; 00099 coor_t zDist = p.z_ - centerZ_; 00100 00101 if(xDist * xDist + zDist * zDist > radius_ * radius_) 00102 return false; 00103 00104 return true; 00105 } 00106 00107 00108 bool hasInside(const Point3& offset, const Point3& p) const { 00109 if(offset.y_ + p.y_ <= minY_ || offset.y_ + p.y_ >= maxY_) 00110 return false; 00111 00112 coor_t xDist = offset.x_ + p.x_ - centerX_; 00113 coor_t zDist = offset.z_ + p.z_ - centerZ_; 00114 00115 if(xDist * xDist + zDist * zDist >= radius_ * radius_) 00116 return false; 00117 00118 return true; 00119 00120 } 00121 00122 00123 void center(Point3& out) const { 00124 out.x_ = centerX_; 00125 out.y_ = CoorT::half(minY_ + maxY_); 00126 out.z_ = centerZ_; 00127 } 00128 00129 00130 bool isTouching(const BoundingCylinder& b) const { 00131 if(b.maxY_ < minY_ 00132 || b.minY_ > maxY_ 00133 || maxY_ < b.minY_ 00134 || minY_ > b.maxY_) 00135 return false; 00136 00137 coor_t radSum = radius_ + b.radius_; 00138 coor_t distX = centerX_ - b.centerX_; 00139 coor_t distZ = centerZ_ - b.centerZ_; 00140 00141 if(radSum * radSum < distX * distX + distZ + distZ) 00142 return false; 00143 00144 return true; 00145 } 00146 00147 bool isTouching(const BoundingBox& b) const; 00148 00149 coor_t radius() const { 00150 return radius_; 00151 } 00152 00153 coor_t centerX_, centerZ_; 00154 coor_t minY_, maxY_; 00155 coor_t radius_; 00156 }; 00157 00158 se_err::Log& operator<< (se_err::Log& log, const BoundingCylinder& b); 00159 00160 } 00161 00162 #endif
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.