libcruft-util/point.hpp

87 lines
2.3 KiB
C++
Raw Normal View History

2011-06-23 22:04:51 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-06-23 22:04:51 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2011-06-23 22:04:51 +10:00
*
2015-01-15 14:04:51 +11:00
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
2011-06-23 22:04:51 +10:00
*/
#ifndef __UTIL_POINT_HPP
#define __UTIL_POINT_HPP
#include "extent.hpp"
#include "vector.hpp"
#include "coord.hpp"
#include <array>
#include <initializer_list>
#include <iostream>
2014-12-19 14:59:16 +11:00
#include <type_traits>
2011-08-15 20:11:20 +10:00
namespace util {
/// An n-dimensional position in space.
template <size_t S, typename T>
struct point : public coord::base<S,T,point,coord::xyzw>
{
using coord::base<S,T,util::point,coord::xyzw>::base;
2011-06-23 22:04:51 +10:00
// point operators
template <typename U> typename std::common_type<T,U>::type distance (point<S,U>) const;
template <typename U> typename std::common_type<T,U>::type distance2 (point<S,U>) const;
template <typename U> typename std::common_type<T,U>::type manhattan (point<S,U>) const;
2011-09-07 21:51:13 +10:00
vector<S,T> to (point) const;
vector<S,T> from (point) const;
2015-02-19 13:19:27 +11:00
template <size_t D> point<D,T> homog (void) const;
2015-01-05 18:08:20 +11:00
static const point<S,T> ORIGIN;
void sanity (void) const;
2011-08-15 20:11:20 +10:00
};
// iostream operators
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, point<S,T>);
2012-05-26 18:02:11 +10:00
// Convenience typedefs
typedef point<2,float> point2f;
typedef point<3,float> point3f;
2015-01-13 18:39:43 +11:00
typedef point<4,float> point4f;
typedef point<2,double> point2d;
typedef point<3,double> point3d;
typedef point<2,size_t> point2u;
typedef point<2,intmax_t> point2i;
2011-08-15 20:11:20 +10:00
}
2011-06-23 22:04:51 +10:00
#include <functional>
namespace std {
template <size_t S, typename T>
struct hash<util::point<S,T>> {
size_t operator() (util::point<S,T> p) const {
std::hash<T> h;
size_t k = 0;
for (size_t i = 0; i < S; ++i)
k = h (p.data[i] ^ k);
return k;
}
};
}
2014-02-18 15:28:28 +11:00
#include "point.ipp"
2011-06-23 22:04:51 +10:00
#endif // __UTIL_POINT_HPP