geom/tri: add tri sampler

This commit is contained in:
Danny Robson 2018-04-26 18:37:01 +10:00
parent 4bb66d2c05
commit 5011c670c6

View File

@ -17,23 +17,63 @@
#ifndef CRUFT_GEOM_TRI_HPP #ifndef CRUFT_GEOM_TRI_HPP
#define CRUFT_GEOM_TRI_HPP #define CRUFT_GEOM_TRI_HPP
#include <cstddef>
#include "../point.hpp" #include "../point.hpp"
#include "sample.hpp"
#include <cstddef>
#include <random>
namespace util::geom { namespace util::geom {
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
struct tri {
tri () = default;
tri (point<S,T> _a, point<S,T> _b, point<S,T> _c):
a (_a), b (_b), c (_c)
{ ; }
util::point<S,T> a, b, c;
};
template <size_t S, typename T>
tri (point<S,T>, point<S,T>, point<S,T>) -> tri<S,T>;
using tri3f = tri<3,float>;
namespace surface {
template <size_t S, typename T>
struct sampler<tri<S,T>> {
sampler (tri<S,T> _target):
base (_target.a),
v0 (_target.b - _target.a),
v1 (_target.c - _target.a)
{ ; }
template <typename GeneratorT>
util::point<S,T>
operator() (GeneratorT &&gen) const noexcept
{
std::uniform_real_distribution<float> dist (0, 1);
return base + dist (gen) * v0 + dist (gen) * v1;
}
util::point<S,T> base;
util::vector<S,T> v0, v1;
};
};
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// n-dimensional triangle area // n-dimensional triangle area
template <std::size_t DimensionV, typename ValueT> template <std::size_t DimensionV, typename ValueT>
ValueT ValueT
area (util::point<DimensionV,ValueT> a, area (tri<DimensionV,ValueT> obj)
util::point<DimensionV,ValueT> b,
util::point<DimensionV,ValueT> c)
{ {
// heron's formula // heron's formula
const auto ab = util::distance (a, b); const auto ab = util::distance (obj.a, obj.b);
const auto bc = util::distance (b, c); const auto bc = util::distance (obj.b, obj.c);
const auto ca = util::distance (c, a); const auto ca = util::distance (obj.c, obj.a);
const auto s = (ab + bc + ca) / 2; const auto s = (ab + bc + ca) / 2;
@ -45,19 +85,19 @@ namespace util::geom {
// 2-dimension triangle area // 2-dimension triangle area
template <typename T> template <typename T>
T T
area (util::point<2,T> a, util::point<2,T> b, util::point<2,T> c) area (tri<2,T> obj)
{ {
// | x1 y1 1 | // | x1 y1 1 |
// area = 0.5 det | x2 y2 1 | // area = 0.5 det | x2 y2 1 |
// | x3 y3 1 | // | x3 y3 1 |
return std::abs ( return std::abs (
-b.x * a.y -obj.b.x * obj.a.y
+c.x * a.y +obj.c.x * obj.a.y
+a.x * b.y +obj.a.x * obj.b.y
-c.x * b.y -obj.c.x * obj.b.y
-a.x * c.y -obj.a.x * obj.c.y
+b.x * c.y +obj.b.x * obj.c.y
) / 2; ) / 2;
} }
@ -66,13 +106,23 @@ namespace util::geom {
// 3-dimension triangle area // 3-dimension triangle area
template <typename T> template <typename T>
T T
area (util::point<3,T> a, util::point<3,T> b, util::point<3,T> c) area (tri<3,T> obj)
{ {
const auto ab = a - b; const auto ab = obj.a - obj.b;
const auto ac = a - c; const auto ac = obj.a - obj.c;
return norm (cross (ab, ac)) / 2; return norm (cross (ab, ac)) / 2;
} }
//-------------------------------------------------------------------------
// convenience forwarder
template <size_t S, typename T>
T
area (point<S,T> a, point<S,T> b, point<S,T> c)
{
return area (tri (a, b, c));
}
}; };
#endif #endif