geom/tri: add inclusive test for points

This commit is contained in:
Danny Robson 2024-10-29 14:03:40 +10:00
parent c3f79d1e69
commit b1e99acaab
2 changed files with 42 additions and 6 deletions

View File

@ -7,3 +7,29 @@
*/ */
#include "tri.hpp" #include "tri.hpp"
///////////////////////////////////////////////////////////////////////////////
template <typename T>
bool
cruft::geom::inclusive (tri2<T> const &obj, point2<T> const &p)
{
// Assumes ccw winding
CHECK (cross (obj.b - obj.a, obj.c - obj.a) > 0);
auto const sign = [] (
cruft::point2f const p_,
cruft::point2f const a_,
cruft::point2f const b_)
{
return cross (p_ - a_, b_ - a_);
};
return sign (p, obj.a, obj.b) <= 0
and sign (p, obj.b, obj.c) <= 0
and sign (p, obj.c, obj.a) <= 0;
}
//-----------------------------------------------------------------------------
template bool cruft::geom::inclusive (tri2<f32> const&, point2<f32> const&);

View File

@ -6,8 +6,7 @@
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net> * Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_GEOM_TRI_HPP #pragma once
#define CRUFT_GEOM_TRI_HPP
#include "../point.hpp" #include "../point.hpp"
#include "sample/fwd.hpp" #include "sample/fwd.hpp"
@ -15,13 +14,16 @@
#include <cstddef> #include <cstddef>
#include <random> #include <random>
namespace cruft::geom { namespace cruft::geom {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
struct tri { struct tri {
tri () = default; tri () = default;
tri (point<S,T> _a, point<S,T> _b, point<S,T> _c): tri (point<S,T> _a, point<S,T> _b, point<S,T> _c)
a (_a), b (_b), c (_c) : a (_a)
, b (_b)
, c (_c)
{ ; } { ; }
cruft::point<S,T> a, b, c; cruft::point<S,T> a, b, c;
@ -30,6 +32,10 @@ namespace cruft::geom {
template <size_t S, typename T> template <size_t S, typename T>
tri (point<S,T>, point<S,T>, point<S,T>) -> tri<S,T>; tri (point<S,T>, point<S,T>, point<S,T>) -> tri<S,T>;
template <typename T> using tri2 = tri<2, T>;
template <typename T> using tri3 = tri<3, T>;
using tri2f = tri<2,float>;
using tri3f = tri<3,float>; using tri3f = tri<3,float>;
@ -120,6 +126,10 @@ namespace cruft::geom {
{ {
return area (tri (a, b, c)); return area (tri (a, b, c));
} }
};
#endif
//-------------------------------------------------------------------------
template <typename T>
bool
inclusive (tri2<T> const &obj, point2<T> const &p);
};