2011-10-26 21:43:38 +11:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-08-10 17:36:56 +10:00
|
|
|
* Copyright 2010-2012 Danny Robson <danny@nerdcruft.net>
|
2011-10-26 21:43:38 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "extent.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "maths.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace util;
|
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:38 +11:00
|
|
|
template <typename T>
|
|
|
|
extent<T>::extent (const T _width, const T _height):
|
2012-08-08 14:39:24 +10:00
|
|
|
w (_width),
|
|
|
|
h (_height)
|
2012-06-05 22:50:52 +10:00
|
|
|
{ ; }
|
2011-10-26 21:43:38 +11:00
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
template <typename T>
|
|
|
|
extent<T>::extent (const extent<T> &rhs):
|
2012-08-08 14:39:24 +10:00
|
|
|
w (rhs.w),
|
|
|
|
h (rhs.h)
|
2012-06-05 22:50:52 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
extent<T>&
|
|
|
|
extent<T>::operator= (const extent<T> &rhs) {
|
2012-08-08 14:39:24 +10:00
|
|
|
w = rhs.w;
|
|
|
|
h = rhs.h;
|
2012-06-05 22:50:52 +10:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:38 +11:00
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
extent<T>::diameter (void) const {
|
2014-07-02 15:42:25 +10:00
|
|
|
return static_cast<T> (sqrt (w * w + h * h));
|
2011-10-26 21:43:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
extent<T>::area (void) const
|
2012-08-08 14:39:24 +10:00
|
|
|
{ return w * h; }
|
2011-10-26 21:43:38 +11:00
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-04 14:51:25 +10:00
|
|
|
template <typename T>
|
2014-12-19 18:29:12 +11:00
|
|
|
float
|
2012-06-04 14:51:25 +10:00
|
|
|
extent<T>::aspect (void) const
|
2014-12-19 18:29:12 +11:00
|
|
|
{
|
|
|
|
return static_cast<float> (w) / static_cast<float> (h);
|
|
|
|
}
|
2012-06-04 14:51:25 +10:00
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:38 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
extent<T>::empty (void) const
|
|
|
|
{ return almost_equal (area(), 0); }
|
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:38 +11:00
|
|
|
template <typename T>
|
|
|
|
bool
|
|
|
|
extent<T>::operator ==(const extent& rhs) const {
|
2012-08-08 14:39:24 +10:00
|
|
|
return almost_equal (w, rhs.w) &&
|
|
|
|
almost_equal (h, rhs.h);
|
2011-10-26 21:43:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-10-26 21:43:38 +11:00
|
|
|
template <typename T>
|
|
|
|
void
|
|
|
|
extent<T>::sanity (void) const
|
2012-08-08 14:39:24 +10:00
|
|
|
{ CHECK (w >= 0 && h >= 0); }
|
2011-10-26 21:43:38 +11:00
|
|
|
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
template <>
|
|
|
|
void
|
|
|
|
extent<unsigned int>::sanity (void) const
|
|
|
|
{ return; }
|
2012-02-09 14:55:07 +11:00
|
|
|
|
|
|
|
template <>
|
|
|
|
void
|
|
|
|
extent<unsigned long>::sanity (void) const
|
|
|
|
{ return; }
|
2011-10-26 21:43:38 +11:00
|
|
|
}
|
|
|
|
|
2014-07-02 15:43:59 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream &os, util::extent<T> e) {
|
|
|
|
os << "[" << e.w << ", " << e.h << "]";
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template std::ostream& operator<< (std::ostream&, util::extent<uint16_t>);
|
|
|
|
template std::ostream& operator<< (std::ostream&, util::extent<uint32_t>);
|
|
|
|
template std::ostream& operator<< (std::ostream&, util::extent<uint64_t>);
|
2011-10-26 21:43:38 +11:00
|
|
|
|
2012-06-05 22:50:52 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-26 18:01:04 +10:00
|
|
|
namespace util {
|
2014-07-02 15:43:24 +10:00
|
|
|
template struct extent<uint32_t>;
|
|
|
|
template struct extent<uint64_t>;
|
|
|
|
template struct extent<float>;
|
2012-05-26 18:01:04 +10:00
|
|
|
template struct extent<double>;
|
|
|
|
}
|