From b222c21715384efc2eaa53d3ba295fb34da5b599 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 9 Apr 2023 12:24:55 +0100 Subject: Start to factor out geometric place from face controller split --- assetFactory/faceController.cpp | 16 ++++++++-------- assetFactory/faceController.h | 4 ++-- lib/geometricPlane.cpp | 9 +++++++++ lib/geometricPlane.h | 12 ++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 lib/geometricPlane.cpp create mode 100644 lib/geometricPlane.h diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index 7992fba..37aaa4c 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -76,7 +76,6 @@ FaceController::extrude(ModelFactoryMesh & mesh, const std::string & faceName, O return newFaces; } -enum class PlaneRelation { Above, Below, On }; Shape::CreatedFaces FaceController::split( ModelFactoryMesh & mesh, const std::string & name, OpenMesh::FaceHandle & fh, const Split & split) const @@ -84,15 +83,16 @@ FaceController::split( // Map face vertex handles to their relationship to the split plane const auto vertices = materializeRange(mesh.fv_range(fh)); auto vertexRelations = vertices * [&split, &mesh](OpenMesh::VertexHandle vh) { - const auto d = glm::dot(split.normal, mesh.point(vh) - split.origin); - return std::make_pair(vh, d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On); + return std::make_pair(vh, split.getRelation(mesh.point(vh))); }; // Insert new vertices where half edges intersect the split plane for (size_t curIdx = 0; curIdx < vertexRelations.size(); ++curIdx) { const size_t nextIdx = (curIdx + 1) % vertexRelations.size(); const auto ¤t = vertexRelations[curIdx], next = vertexRelations[nextIdx]; - if ((current.second == PlaneRelation::Above && next.second == PlaneRelation::Below) - || (current.second == PlaneRelation::Below && next.second == PlaneRelation::Above)) { + if ((current.second == GeometricPlane::PlaneRelation::Above + && next.second == GeometricPlane::PlaneRelation::Below) + || (current.second == GeometricPlane::PlaneRelation::Below + && next.second == GeometricPlane::PlaneRelation::Above)) { const auto origin = mesh.point(current.first), dir = glm::normalize(mesh.point(next.first) - origin); float dist {}; @@ -101,7 +101,7 @@ FaceController::split( auto where = vertexRelations.begin(); ++curIdx; std::advance(where, curIdx); - vertexRelations.emplace(where, newv, PlaneRelation::On); + vertexRelations.emplace(where, newv, GeometricPlane::PlaneRelation::On); } } // Create vertex vectors @@ -113,8 +113,8 @@ FaceController::split( } } }; - filterVertices(out.front(), PlaneRelation::Above); - filterVertices(out.back(), PlaneRelation::Below); + filterVertices(out.front(), GeometricPlane::PlaneRelation::Above); + filterVertices(out.back(), GeometricPlane::PlaneRelation::Below); if (out.back().size() > 2) { Shape::CreatedFaces newFaces; diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 8d30dc6..0376241 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -1,5 +1,6 @@ #pragma once +#include "geometricPlane.h" #include "modelFactoryMesh_fwd.h" #include "mutation.h" #include "persistence.h" @@ -10,10 +11,9 @@ class FaceController : public Mutation, public Style, public Persistence::Persistable { public: - class Split : public Persistable { + class Split : public Persistable, public GeometricPlane { public: std::string id; - glm::vec3 origin, normal; private: friend Persistence::SelectionPtrBase>; diff --git a/lib/geometricPlane.cpp b/lib/geometricPlane.cpp new file mode 100644 index 0000000..cb52997 --- /dev/null +++ b/lib/geometricPlane.cpp @@ -0,0 +1,9 @@ +#include "geometricPlane.h" +#include + +GeometricPlane::PlaneRelation +GeometricPlane::getRelation(glm::vec3 p) const +{ + const auto d = glm::dot(normal, p - origin); + return d < 0.f ? PlaneRelation::Below : d > 0.f ? PlaneRelation::Above : PlaneRelation::On; +} diff --git a/lib/geometricPlane.h b/lib/geometricPlane.h new file mode 100644 index 0000000..96816f2 --- /dev/null +++ b/lib/geometricPlane.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +class GeometricPlane { +public: + enum class PlaneRelation { Above, Below, On }; + + glm::vec3 origin, normal; + + PlaneRelation getRelation(glm::vec3 point) const; +}; -- cgit v1.2.3