PF_API 0.52
|
00001 00002 // Copyright (C) 2006-2011 Dylan Blair 00003 // 00004 // email: dblair@alumni.cs.utexas.edu 00005 // 00006 // This library is free software; you can redistribute it and/or 00007 // modify it under the terms of the GNU Lesser General Public 00008 // License as published by the Free Software Foundation; either 00009 // version 2.1 of the License, or (at your option) any later version. 00010 // 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // Lesser General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU Lesser General Public 00017 // License along with this library; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 #ifndef PF_WAYPOINT_COLLECTION_H 00022 #define PF_WAYPOINT_COLLECTION_H 00023 00024 #include "../Math_LIB/MPath.h" 00025 #include "../CD_LIB/CDGrid.h" 00026 00027 namespace OpenSkyNet { 00028 namespace PF { 00034 class WaypointCollection { 00035 public: 00037 struct lessThan { 00038 bool operator()(const Math::Point<int>& p1_, const Math::Point<int>& p2_) const { 00039 if (p1_.x() < p2_.x()) return true; 00040 else if (p1_.x() > p2_.x()) return false; 00041 else if (p1_.y() < p2_.y()) return true; 00042 else if (p1_.y() > p2_.y()) return false; 00043 else if (p1_.z() < p2_.z()) return true; 00044 else if (p1_.z() > p2_.z()) return false; 00045 return false; 00046 } 00047 }; 00049 struct WhoGoesWhereDef { 00050 std::set<Math::Point<int>, lessThan> _drivBins; 00051 std::set<Math::Point<int>, lessThan> _targBins; 00052 std::vector<Math::Point<> > _pathPoints; 00053 protected: 00054 bool findBin(const Math::Point<int>& bin_, const std::set<Math::Point<int>, lessThan>& bins_) const; 00055 public: 00056 inline bool findDrivBin(const Math::Point<int>& drivBin_) const { 00057 return findBin(drivBin_,_drivBins); 00058 } 00059 inline bool findTargBin(const Math::Point<int>& targBin_) const { 00060 return findBin(targBin_,_targBins); 00061 } 00062 }; 00063 std::vector<WhoGoesWhereDef*> _defs; 00064 private: 00065 Math::Point<> _initMaxCorner; 00066 Math::Point<> _maxCorner; 00067 Math::Point<> _size; 00068 Math::Point<Utils::uint> _axisDivisions; 00069 Math::Point<> _binSize; 00070 public: 00072 void loadWPC(const char* wPC_); 00073 00076 inline Math::Point<int> getWPCBin(const Math::Point<>& pos_) const { 00077 return Math::Point<int>(static_cast<int>(floor((_maxCorner.x() - pos_.x())/_binSize.x())), 00078 static_cast<int>(floor((_maxCorner.y() - pos_.y())/_binSize.y())), 00079 static_cast<int>(floor((_maxCorner.z() - pos_.z())/_binSize.z()))); 00080 } 00081 00083 inline bool getWPCBin(const Math::Point<>& pos_, Math::Point<int>& wPCBin_) const { 00084 wPCBin_ = getWPCBin(pos_); 00085 00086 if ((((((wPCBin_.x() < 0) || (wPCBin_.y() < 0)) || (wPCBin_.z() < 0)) || 00087 (wPCBin_.x() >= static_cast<int>(_axisDivisions.x()))) || 00088 (wPCBin_.y() >= static_cast<int>(_axisDivisions.y()))) || 00089 (wPCBin_.z() >= static_cast<int>(_axisDivisions.z()))) 00090 return false; 00091 return true; 00092 } 00093 00095 inline Math::Point<int> getNearestWPCBin(const Math::Point<>& pos_) const { 00096 Math::Point<int> wPCBin(getWPCBin(pos_)); 00097 wPCBin.x() = wPCBin.x() < 0 ? 0 : wPCBin.x(); 00098 wPCBin.y() = wPCBin.y() < 0 ? 0 : wPCBin.y(); 00099 wPCBin.z() = wPCBin.z() < 0 ? 0 : wPCBin.z(); 00100 00101 return Math::Point<int>(static_cast<Utils::uint>(wPCBin.x()) >= _axisDivisions.x() ? _axisDivisions.x()-1 : wPCBin.x(), 00102 static_cast<Utils::uint>(wPCBin.y()) >= _axisDivisions.y() ? _axisDivisions.y()-1 : wPCBin.y(), 00103 static_cast<Utils::uint>(wPCBin.z()) >= _axisDivisions.z() ? _axisDivisions.z()-1 : wPCBin.z()); 00104 } 00105 00108 inline Math::Point<int> getWPCBin(const Math::Point<int>& gridBin_, const CD::Grid* grid_) const { 00109 Math::Point<> gridBinCenter(grid_->getMaxCorner() - grid_->getBinSize()/2.0f); 00110 gridBinCenter -= grid_->getBinSize()*gridBin_; 00111 return getWPCBin(gridBinCenter); 00112 } 00113 00116 inline bool getWPCBin(const Math::Point<int>& gridBin_, const CD::Grid* grid_, Math::Point<int>& wPCBin_) const { 00117 wPCBin_ = getWPCBin(gridBin_,grid_); 00118 00119 if ((((((wPCBin_.x() < 0) || (wPCBin_.y() < 0)) || (wPCBin_.z() < 0)) || 00120 (wPCBin_.x() >= static_cast<int>(_axisDivisions.x()))) || 00121 (wPCBin_.y() >= static_cast<int>(_axisDivisions.y()))) || 00122 (wPCBin_.z() >= static_cast<int>(_axisDivisions.z()))) 00123 return false; 00124 return true; 00125 } 00126 00128 inline Math::Point<int> getNearestWPCBin(const Math::Point<int>& gridBin_, const CD::Grid* grid_) const { 00129 Math::Point<int> wPCBin(getWPCBin(gridBin_,grid_)); 00130 wPCBin.x() = wPCBin.x() < 0 ? 0 : wPCBin.x(); 00131 wPCBin.y() = wPCBin.y() < 0 ? 0 : wPCBin.y(); 00132 wPCBin.z() = wPCBin.z() < 0 ? 0 : wPCBin.z(); 00133 00134 return Math::Point<int>(static_cast<Utils::uint>(wPCBin.x()) >= _axisDivisions.x() ? _axisDivisions.x()-1 : wPCBin.x(), 00135 static_cast<Utils::uint>(wPCBin.y()) >= _axisDivisions.y() ? _axisDivisions.y()-1 : wPCBin.y(), 00136 static_cast<Utils::uint>(wPCBin.z()) >= _axisDivisions.z() ? _axisDivisions.z()-1 : wPCBin.z()); 00137 } 00138 00140 int getWGWDIndex(Math::Point<int>& drivBin_, Math::Point<int>& targBin_, const CD::Grid* grid_) const; 00141 00143 int getWGWDIndex(const Math::Point<>& drivPos_, const Math::Point<>& targPos_) const; 00144 00147 void getPath(int wGWDIndex_, const Math::Point<>& drivPos_, const Math::Point<>& targPos_, Math::Path& path_) const; 00148 00150 inline const Math::Point<>& getMaxCorner() const { return _maxCorner; } 00151 00153 inline void translate(const Math::Point<>& delta_) { _maxCorner += delta_; } 00154 00156 inline const Math::Point<>& getSize() const { return _size; } 00157 00159 inline const Math::Point<Utils::uint>& getAxisDivs() const { return _axisDivisions; } 00160 00163 inline const Math::Point<>& getBinSize() const { return _binSize; } 00164 }; 00165 } 00166 } 00167 00168 #endif //PF_WAYPOINT_COLLECTION_H