2015-10-14 15:32:53 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-10-14 15:32:53 +11:00
|
|
|
*
|
2018-04-09 12:49:09 +10:00
|
|
|
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
|
2015-10-14 15:32:53 +11:00
|
|
|
*/
|
|
|
|
|
2018-04-09 12:49:09 +10:00
|
|
|
#ifndef CRUFT_GEOM_TRI_HPP
|
|
|
|
#define CRUFT_GEOM_TRI_HPP
|
2015-10-14 15:32:53 +11:00
|
|
|
|
2018-04-09 12:49:09 +10:00
|
|
|
#include "../point.hpp"
|
2018-11-26 14:08:50 +11:00
|
|
|
#include "sample/fwd.hpp"
|
2018-04-26 18:37:01 +10:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <random>
|
2018-04-09 12:49:09 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::geom {
|
2018-04-26 18:37:01 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
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)
|
|
|
|
{ ; }
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T> a, b, c;
|
2018-04-26 18:37:01 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t S, typename T>
|
|
|
|
tri (point<S,T>, point<S,T>, point<S,T>) -> tri<S,T>;
|
|
|
|
|
|
|
|
using tri3f = tri<3,float>;
|
|
|
|
|
|
|
|
|
2018-11-26 14:08:50 +11:00
|
|
|
namespace sample {
|
2018-04-26 18:37:01 +10:00
|
|
|
template <size_t S, typename T>
|
2018-11-26 14:08:50 +11:00
|
|
|
class surface<tri<S,T>> {
|
2018-05-01 16:01:17 +10:00
|
|
|
public:
|
2018-11-26 14:08:50 +11:00
|
|
|
using shape_type = tri<S,T>;
|
|
|
|
|
|
|
|
explicit surface (tri<S,T> _target):
|
2018-04-26 18:37:01 +10:00
|
|
|
base (_target.a),
|
|
|
|
v0 (_target.b - _target.a),
|
|
|
|
v1 (_target.c - _target.a)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
template <typename GeneratorT>
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T>
|
2018-04-26 18:37:01 +10:00
|
|
|
operator() (GeneratorT &&gen) const noexcept
|
|
|
|
{
|
|
|
|
std::uniform_real_distribution<float> dist (0, 1);
|
|
|
|
return base + dist (gen) * v0 + dist (gen) * v1;
|
|
|
|
}
|
|
|
|
|
2018-05-01 16:01:17 +10:00
|
|
|
private:
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::point<S,T> base;
|
2018-11-26 14:08:50 +11:00
|
|
|
cruft::vector<S,T> v0;
|
|
|
|
cruft::vector<S,T> v1;
|
2018-04-26 18:37:01 +10:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-04-09 12:49:09 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// n-dimensional triangle area
|
|
|
|
template <std::size_t DimensionV, typename ValueT>
|
|
|
|
ValueT
|
2018-04-26 18:37:01 +10:00
|
|
|
area (tri<DimensionV,ValueT> obj)
|
2018-04-09 12:49:09 +10:00
|
|
|
{
|
|
|
|
// heron's formula
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto ab = cruft::distance (obj.a, obj.b);
|
|
|
|
const auto bc = cruft::distance (obj.b, obj.c);
|
|
|
|
const auto ca = cruft::distance (obj.c, obj.a);
|
2018-04-09 12:49:09 +10:00
|
|
|
|
|
|
|
const auto s = (ab + bc + ca) / 2;
|
|
|
|
|
|
|
|
return std::sqrt (s * (s - ab) * (s - bc) * (s - ca));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// 2-dimension triangle area
|
|
|
|
template <typename T>
|
|
|
|
T
|
2018-04-26 18:37:01 +10:00
|
|
|
area (tri<2,T> obj)
|
2018-04-09 12:49:09 +10:00
|
|
|
{
|
|
|
|
// | x1 y1 1 |
|
|
|
|
// area = 0.5 det | x2 y2 1 |
|
|
|
|
// | x3 y3 1 |
|
|
|
|
|
|
|
|
return std::abs (
|
2018-04-26 18:37:01 +10:00
|
|
|
-obj.b.x * obj.a.y
|
|
|
|
+obj.c.x * obj.a.y
|
|
|
|
+obj.a.x * obj.b.y
|
|
|
|
-obj.c.x * obj.b.y
|
|
|
|
-obj.a.x * obj.c.y
|
|
|
|
+obj.b.x * obj.c.y
|
2018-04-09 12:49:09 +10:00
|
|
|
) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// 3-dimension triangle area
|
|
|
|
template <typename T>
|
|
|
|
T
|
2018-04-26 18:37:01 +10:00
|
|
|
area (tri<3,T> obj)
|
2018-04-09 12:49:09 +10:00
|
|
|
{
|
2018-04-26 18:37:01 +10:00
|
|
|
const auto ab = obj.a - obj.b;
|
|
|
|
const auto ac = obj.a - obj.c;
|
2018-04-09 12:49:09 +10:00
|
|
|
|
|
|
|
return norm (cross (ab, ac)) / 2;
|
|
|
|
}
|
2018-04-26 18:37:01 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// 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));
|
|
|
|
}
|
2018-04-09 12:49:09 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|