From b1e99acaab3f14dd9397badc5d0049db1d44b7e4 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 29 Oct 2024 14:03:40 +1000 Subject: [PATCH] geom/tri: add `inclusive` test for points --- cruft/util/geom/tri.cpp | 26 ++++++++++++++++++++++++++ cruft/util/geom/tri.hpp | 22 ++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/cruft/util/geom/tri.cpp b/cruft/util/geom/tri.cpp index 7c4cdea1..3e379839 100644 --- a/cruft/util/geom/tri.cpp +++ b/cruft/util/geom/tri.cpp @@ -7,3 +7,29 @@ */ #include "tri.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +template +bool +cruft::geom::inclusive (tri2 const &obj, point2 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 const&, point2 const&); diff --git a/cruft/util/geom/tri.hpp b/cruft/util/geom/tri.hpp index 19a1d9a7..970d3b20 100644 --- a/cruft/util/geom/tri.hpp +++ b/cruft/util/geom/tri.hpp @@ -6,8 +6,7 @@ * Copyright 2016-2018 Danny Robson */ -#ifndef CRUFT_GEOM_TRI_HPP -#define CRUFT_GEOM_TRI_HPP +#pragma once #include "../point.hpp" #include "sample/fwd.hpp" @@ -15,13 +14,16 @@ #include #include + namespace cruft::geom { /////////////////////////////////////////////////////////////////////////// template struct tri { tri () = default; - tri (point _a, point _b, point _c): - a (_a), b (_b), c (_c) + tri (point _a, point _b, point _c) + : a (_a) + , b (_b) + , c (_c) { ; } cruft::point a, b, c; @@ -30,6 +32,10 @@ namespace cruft::geom { template tri (point, point, point) -> tri; + template using tri2 = tri<2, T>; + template using tri3 = tri<3, T>; + + using tri2f = tri<2,float>; using tri3f = tri<3,float>; @@ -120,6 +126,10 @@ namespace cruft::geom { { return area (tri (a, b, c)); } -}; -#endif + + //------------------------------------------------------------------------- + template + bool + inclusive (tri2 const &obj, point2 const &p); +};