libcruft-util/extent.cpp

164 lines
4.3 KiB
C++
Raw Normal View History

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/>.
*
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
2011-10-26 21:43:38 +11:00
*/
#include "extent.hpp"
#include "debug.hpp"
#include "maths.hpp"
#include <cmath>
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2011-10-26 21:43:38 +11:00
template <typename T>
2015-02-13 16:29:07 +11:00
util::extent<T>::extent (const T _width, const T _height):
w (_width),
h (_height)
{ ; }
2011-10-26 21:43:38 +11:00
2015-02-12 17:40:36 +11:00
//-----------------------------------------------------------------------------
template <typename T>
2015-02-13 16:29:07 +11:00
util::extent<T>::extent (const util::extent<T> &rhs):
w (rhs.w),
h (rhs.h)
{ ; }
2015-02-12 17:40:36 +11:00
//-----------------------------------------------------------------------------
template <typename T>
2015-02-13 16:29:07 +11:00
util::extent<T>&
2015-02-13 16:30:22 +11:00
util::extent<T>::operator= (const util::extent<T> &rhs)
{
w = rhs.w;
h = rhs.h;
return *this;
}
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2011-10-26 21:43:38 +11:00
template <typename T>
T
2015-02-13 16:30:22 +11:00
util::extent<T>::diameter (void) const
{
return static_cast<T> (sqrt (w * w + h * h));
2011-10-26 21:43:38 +11:00
}
2015-02-12 17:40:36 +11:00
//-----------------------------------------------------------------------------
2011-10-26 21:43:38 +11:00
template <typename T>
T
2015-02-13 16:29:07 +11:00
util::extent<T>::area (void) const
2015-02-13 16:30:22 +11:00
{
return w * h;
}
2011-10-26 21:43:38 +11:00
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2015-01-16 14:42:04 +11:00
template <typename T>
2015-02-13 16:29:07 +11:00
util::extent<T>
util::extent<T>::expanded (util::vector<2,T> size) const
2015-01-16 14:42:04 +11:00
{
return {
w + size.x,
h + size.y
};
}
//-----------------------------------------------------------------------------
2012-06-04 14:51:25 +10:00
template <typename T>
2015-02-13 16:29:07 +11:00
util::extent<T>
util::extent<T>::expanded (T t) const
2015-02-12 17:40:47 +11:00
{
return expanded (util::vector<2,T> {t});
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
2014-12-19 18:29:12 +11:00
float
2015-02-13 16:29:07 +11:00
util::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
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2011-10-26 21:43:38 +11:00
template <typename T>
bool
2015-02-13 16:29:07 +11:00
util::extent<T>::empty (void) const
2015-02-13 16:30:22 +11:00
{
return almost_equal (area(), 0);
}
2011-10-26 21:43:38 +11:00
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2011-10-26 21:43:38 +11:00
template <typename T>
bool
2015-02-13 16:30:22 +11:00
util::extent<T>::operator ==(const extent& rhs) const
{
return almost_equal (w, rhs.w) &&
almost_equal (h, rhs.h);
2011-10-26 21:43:38 +11:00
}
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2015-02-13 17:32:08 +11:00
namespace debug {
template <typename T>
struct validator<util::extent,T> {
static bool is_valid (const util::extent<T> &e)
{
return e.w >= 0 && e.h >= 0;
}
};
}
2011-10-26 21:43:38 +11:00
2015-02-13 17:32:08 +11:00
template bool debug::valid (const util::extent<float>&);
template bool debug::valid (const util::extent<double>&);
template bool debug::valid (const util::extent<uint16_t>&);
template bool debug::valid (const util::extent<uint32_t>&);
template bool debug::valid (const util::extent<uint64_t>&);
2011-10-26 21:43:38 +11:00
2015-02-12 17:40:36 +11:00
///////////////////////////////////////////////////////////////////////////////
2014-07-02 15:43:59 +10:00
template <typename T>
std::ostream&
2015-02-13 16:30:22 +11:00
util::operator<< (std::ostream &os, util::extent<T> e)
{
2014-07-02 15:43:59 +10:00
os << "[" << e.w << ", " << e.h << "]";
return os;
}
template std::ostream& util::operator<< (std::ostream&, util::extent<uint16_t>);
template std::ostream& util::operator<< (std::ostream&, util::extent<uint32_t>);
template std::ostream& util::operator<< (std::ostream&, util::extent<uint64_t>);
template std::ostream& util::operator<< (std::ostream&, util::extent<float>);
template std::ostream& util::operator<< (std::ostream&, util::extent<double>);
2011-10-26 21:43:38 +11:00
2015-02-12 17:40:36 +11:00
//-----------------------------------------------------------------------------
namespace util {
template struct extent<uint32_t>;
template struct extent<uint64_t>;
template struct extent<float>;
template struct extent<double>;
}