/* * 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/. * * Copyright 2016-2018 Danny Robson */ #ifndef CRUFT_GEOM_TRI_HPP #define CRUFT_GEOM_TRI_HPP #include "../point.hpp" #include "sample.hpp" #include #include namespace util::geom { /////////////////////////////////////////////////////////////////////////// template struct tri { tri () = default; tri (point _a, point _b, point _c): a (_a), b (_b), c (_c) { ; } util::point a, b, c; }; template tri (point, point, point) -> tri; using tri3f = tri<3,float>; namespace surface { template class sampler> { public: sampler (tri _target): base (_target.a), v0 (_target.b - _target.a), v1 (_target.c - _target.a) { ; } template util::point operator() (GeneratorT &&gen) const noexcept { std::uniform_real_distribution dist (0, 1); return base + dist (gen) * v0 + dist (gen) * v1; } private: util::point base; util::vector v0, v1; }; }; /////////////////////////////////////////////////////////////////////////// // n-dimensional triangle area template ValueT area (tri obj) { // heron's formula const auto ab = util::distance (obj.a, obj.b); const auto bc = util::distance (obj.b, obj.c); const auto ca = util::distance (obj.c, obj.a); const auto s = (ab + bc + ca) / 2; return std::sqrt (s * (s - ab) * (s - bc) * (s - ca)); } //------------------------------------------------------------------------- // 2-dimension triangle area template T area (tri<2,T> obj) { // | x1 y1 1 | // area = 0.5 det | x2 y2 1 | // | x3 y3 1 | return std::abs ( -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 ) / 2; } //------------------------------------------------------------------------- // 3-dimension triangle area template T area (tri<3,T> obj) { const auto ab = obj.a - obj.b; const auto ac = obj.a - obj.c; return norm (cross (ab, ac)) / 2; } //------------------------------------------------------------------------- // convenience forwarder template T area (point a, point b, point c) { return area (tri (a, b, c)); } }; #endif