BWAPI
SPAR/AIModule/SparAIModule/PerceptualState/Locations/Static/Arbitrary/RectangularLocation.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "../StaticLocation.h"
00003 #include <boost/numeric/conversion/bounds.hpp>
00004 
00005 class RectangularLocation : public Location
00006 {
00007 public:
00008   RectangularLocation(BWAPI::Position topLeftCorner, BWAPI::Position bottomRightCorner)
00009     : Location()
00010     , m_topLeftCorner(topLeftCorner)
00011     , m_bottomRightCorner(bottomRightCorner)
00012   {
00013     assert(m_topLeftCorner.x() <= m_bottomRightCorner.x());
00014     assert(m_topLeftCorner.y() <= m_bottomRightCorner.y());
00015   }
00016 
00017   virtual bool isInLocation(BWAPI::Position position) const
00018   {
00019     return is_between<int, true>(m_topLeftCorner.x(), m_bottomRightCorner.x())(position.x())
00020         && is_between<int, true>(m_topLeftCorner.y(), m_bottomRightCorner.y())(position.y());
00021   }
00022   
00023   RectangularLocation expand(int horizontalExpansion, int verticalExpansion) const
00024   {
00025     // Is it necessary to keep the position inside bounds?
00026     return RectangularLocation(BWAPI::Position(m_topLeftCorner.x() - horizontalExpansion, m_topLeftCorner.y() - verticalExpansion),
00027                                BWAPI::Position(m_bottomRightCorner.x() + horizontalExpansion, m_bottomRightCorner.y() + verticalExpansion));
00028   }
00029 
00030   virtual BWAPI::Position getCurrentCenter() const
00031   {
00032     int x = (m_bottomRightCorner.x() - m_topLeftCorner.x())/2;
00033     int y = (m_bottomRightCorner.y() - m_topLeftCorner.y())/2;
00034     return BWAPI::Position(x,y);
00035   }
00036 
00037   template <class UnitGroup>
00038   static RectangularLocation getBoundingBox(const UnitGroup* group)
00039   {
00040     int xMin = boost::numeric::bounds<int>::highest();
00041     int xMax = boost::numeric::bounds<int>::lowest();
00042     int yMin = boost::numeric::bounds<int>::highest();
00043     int yMax = boost::numeric::bounds<int>::lowest();
00044 
00045     for (UnitGroup::const_iterator it = group->begin();
00046          it != group->end();
00047          ++it)
00048     {
00049       xMin = std::min(xMin, (*it)->getPosition().x());
00050       xMax = std::max(xMax, (*it)->getPosition().x());
00051       yMin = std::min(yMin, (*it)->getPosition().y());
00052       yMax = std::max(yMax, (*it)->getPosition().y());
00053     }
00054     return RectangularLocation(BWAPI::Position(xMin, yMin),
00055                                BWAPI::Position(xMax, yMax));
00056   }
00057 
00058   BWAPI::Position getTopLeftCorner() const
00059   {
00060     return m_topLeftCorner;
00061   }
00062 
00063   BWAPI::Position getBottomRightCorner() const
00064   {
00065     return m_bottomRightCorner;
00066   }
00067 
00068 protected:
00069   virtual void output(std::ostream& out) const
00070   {
00071     out << "Rectangle(" << m_topLeftCorner << ", " << m_bottomRightCorner << ")";
00072   }
00073 
00074   const BWAPI::Position m_topLeftCorner;
00075   const BWAPI::Position m_bottomRightCorner;
00076 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines