From 715d4879fdd096ac82367984fdb22117d48737a4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 15 Feb 2023 02:26:06 +0000 Subject: First cut of the model factory and the hardcoded Brush 47 model Requires temporary change to the fragment shader to hardcode some visible colour to the model --- assetFactory/assetFactoryConfig.h | 28 ++++++++++++++++++ assetFactory/assetFactoryConfig_fwd.h | 8 ++++++ assetFactory/cuboid.cpp | 29 +++++++++++++++++++ assetFactory/cuboid.h | 8 ++++++ assetFactory/cylinder.cpp | 43 ++++++++++++++++++++++++++++ assetFactory/cylinder.h | 8 ++++++ assetFactory/faceController.cpp | 53 +++++++++++++++++++++++++++++++++++ assetFactory/faceController.h | 20 +++++++++++++ assetFactory/factoryMesh.cpp | 27 ++++++++++++++++++ assetFactory/factoryMesh.h | 14 +++++++++ assetFactory/modelFactory.cpp | 14 +++++++++ assetFactory/modelFactory.h | 12 ++++++++ assetFactory/mutation.cpp | 10 +++++++ assetFactory/mutation.h | 14 +++++++++ assetFactory/object.cpp | 17 +++++++++++ assetFactory/object.h | 15 ++++++++++ assetFactory/plane.cpp | 15 ++++++++++ assetFactory/plane.h | 8 ++++++ assetFactory/shape.cpp | 17 +++++++++++ assetFactory/shape.h | 25 +++++++++++++++++ assetFactory/use.cpp | 11 ++++++++ assetFactory/use.h | 17 +++++++++++ 22 files changed, 413 insertions(+) create mode 100644 assetFactory/assetFactoryConfig.h create mode 100644 assetFactory/assetFactoryConfig_fwd.h create mode 100644 assetFactory/cuboid.cpp create mode 100644 assetFactory/cuboid.h create mode 100644 assetFactory/cylinder.cpp create mode 100644 assetFactory/cylinder.h create mode 100644 assetFactory/faceController.cpp create mode 100644 assetFactory/faceController.h create mode 100644 assetFactory/factoryMesh.cpp create mode 100644 assetFactory/factoryMesh.h create mode 100644 assetFactory/modelFactory.cpp create mode 100644 assetFactory/modelFactory.h create mode 100644 assetFactory/mutation.cpp create mode 100644 assetFactory/mutation.h create mode 100644 assetFactory/object.cpp create mode 100644 assetFactory/object.h create mode 100644 assetFactory/plane.cpp create mode 100644 assetFactory/plane.h create mode 100644 assetFactory/shape.cpp create mode 100644 assetFactory/shape.h create mode 100644 assetFactory/use.cpp create mode 100644 assetFactory/use.h (limited to 'assetFactory') diff --git a/assetFactory/assetFactoryConfig.h b/assetFactory/assetFactoryConfig.h new file mode 100644 index 0000000..171b81a --- /dev/null +++ b/assetFactory/assetFactoryConfig.h @@ -0,0 +1,28 @@ +#pragma once + +#include "assetFactoryConfig_fwd.h" +#include +#include +#include +#include + +namespace OpenMesh { + template struct glmvec : public VectorT { + using VectorT::VectorT; + glmvec(const VectorT & v) : VectorT {v} { } + operator glm::vec() const + { + glm::vec out; + std::copy(this->begin(), this->end(), &out[0]); + return out; + } + }; +} + +struct ModelFactoryTraits : public OpenMesh::DefaultTraits { + FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + EdgeAttributes(OpenMesh::Attributes::Status); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + using Point = OpenMesh::glmvec; + using Normal = OpenMesh::glmvec; +}; diff --git a/assetFactory/assetFactoryConfig_fwd.h b/assetFactory/assetFactoryConfig_fwd.h new file mode 100644 index 0000000..28489ff --- /dev/null +++ b/assetFactory/assetFactoryConfig_fwd.h @@ -0,0 +1,8 @@ +#pragma once + +namespace OpenMesh { + template class PolyMesh_ArrayKernelT; +} +struct ModelFactoryTraits; + +using ModelFactoryMesh = OpenMesh::PolyMesh_ArrayKernelT; diff --git a/assetFactory/cuboid.cpp b/assetFactory/cuboid.cpp new file mode 100644 index 0000000..90b2108 --- /dev/null +++ b/assetFactory/cuboid.cpp @@ -0,0 +1,29 @@ +#include "cuboid.h" +#include "assetFactoryConfig.h" + +Cuboid::CreatedFaces +Cuboid::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const +{ + static constexpr std::array VERTICES {{ + // bottom + {n, n, z}, + {n, y, z}, + {y, y, z}, + {y, n, z}, + // top + {y, n, o}, + {y, y, o}, + {n, y, o}, + {n, n, o}, + }}; + + const auto vhs = addMutatedToMesh(mesh, VERTICES, mutation); + return { + {"top", mesh.add_face({vhs[4], vhs[5], vhs[6], vhs[7]})}, + {"bottom", mesh.add_face({vhs[0], vhs[1], vhs[2], vhs[3]})}, + {"left", mesh.add_face({vhs[0], vhs[7], vhs[6], vhs[1]})}, + {"right", mesh.add_face({vhs[2], vhs[5], vhs[4], vhs[3]})}, + {"front", mesh.add_face({vhs[0], vhs[3], vhs[4], vhs[7]})}, + {"back", mesh.add_face({vhs[2], vhs[1], vhs[6], vhs[5]})}, + }; +} diff --git a/assetFactory/cuboid.h b/assetFactory/cuboid.h new file mode 100644 index 0000000..5a4072a --- /dev/null +++ b/assetFactory/cuboid.h @@ -0,0 +1,8 @@ +#pragma once + +#include "shape.h" + +class Cuboid : public Shape { +public: + CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; +}; diff --git a/assetFactory/cylinder.cpp b/assetFactory/cylinder.cpp new file mode 100644 index 0000000..66d456d --- /dev/null +++ b/assetFactory/cylinder.cpp @@ -0,0 +1,43 @@ +#include "cylinder.h" +#include "assetFactoryConfig.h" +#include "maths.h" + +Cylinder::CreatedFaces +Cylinder::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const +{ + const glm::vec2 scale {std::accumulate(&mutation[0][0], &mutation[0][3], 0.f), + std::accumulate(&mutation[1][0], &mutation[1][3], 0.f)}; + const unsigned int P = static_cast(std::round(15.F * std::sqrt(glm::length(scale)))); + std::vector bottom(P), top(P); + std::generate_n(bottom.begin(), P, [a = 0.f, step = two_pi / static_cast(P), &mesh, &mutation]() mutable { + const auto xy = sincosf(a += step) * .5F; + const auto xyz = (xy ^ 0) % mutation; + return mesh.add_vertex({xyz.x, xyz.y, xyz.z}); + }); + std::generate_n(top.begin(), P, [a = 0.f, step = two_pi / static_cast(P), &mesh, &mutation]() mutable { + const auto xy = sincosf(a -= step) * .5F; + const auto xyz = (xy ^ 1) % mutation; + return mesh.add_vertex({xyz.x, xyz.y, xyz.z}); + }); + CreatedFaces surface; + std::generate_n(std::inserter(surface, surface.end()), P, + [a = 0.f, step = two_pi / static_cast(P), &mesh, &mutation]() mutable { + const auto xy1 = sincosf(a) * .5F; + const auto xy2 = sincosf(a -= step) * .5F; + const auto xyz1b = (xy1 ^ 0) % mutation; + const auto xyz2b = (xy2 ^ 0) % mutation; + const auto xyz1t = (xy1 ^ 1) % mutation; + const auto xyz2t = (xy2 ^ 1) % mutation; + return std::make_pair(std::string {"edge"}, + mesh.add_face({ + mesh.add_vertex({xyz1b.x, xyz1b.y, xyz1b.z}), + mesh.add_vertex({xyz2b.x, xyz2b.y, xyz2b.z}), + mesh.add_vertex({xyz2t.x, xyz2t.y, xyz2t.z}), + mesh.add_vertex({xyz1t.x, xyz1t.y, xyz1t.z}), + })); + }); + surface.emplace("bottom", mesh.add_face(bottom)); + surface.emplace("top", mesh.add_face(top)); + + return surface; +} diff --git a/assetFactory/cylinder.h b/assetFactory/cylinder.h new file mode 100644 index 0000000..65ca5e5 --- /dev/null +++ b/assetFactory/cylinder.h @@ -0,0 +1,8 @@ +#pragma once + +#include "shape.h" + +class Cylinder : public Shape { +public: + CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; +}; diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp new file mode 100644 index 0000000..bdef74b --- /dev/null +++ b/assetFactory/faceController.cpp @@ -0,0 +1,53 @@ +#include "faceController.h" +#include "assetFactoryConfig.h" +#include "maths.h" + +void +FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const +{ + if (!type.empty()) { + const auto mutation = getMatrix(); + const auto controlledFacesRange = faces.equal_range(name); + const std::vector controlledFaces(controlledFacesRange.first, controlledFacesRange.second); + faces.erase(name); + for (const auto & cf : controlledFaces) { + // get face vertices + const auto faceVertexRange = mesh.fv_range(cf.second); + // get points + const std::vector baseVertices(faceVertexRange.begin(), faceVertexRange.end()); + std::vector points; + std::transform( + faceVertexRange.begin(), faceVertexRange.end(), std::back_inserter(points), [&mesh](auto && v) { + return mesh.point(v); + }); + const auto vertexCount = points.size(); + const auto centre + = std::accumulate(points.begin(), points.end(), glm::vec3 {}) / static_cast(vertexCount); + if (type == "extrude") { + Shape::CreatedFaces newFaces; + // mutate points + std::for_each(points.begin(), points.end(), [&mutation, ¢re](auto && p) { + p = centre + ((p - centre) % mutation); + }); + // create new vertices + std::vector vertices; + std::transform(points.begin(), points.end(), std::back_inserter(vertices), [&mesh](auto && p) { + return mesh.add_vertex({p.x, p.y, p.z}); + }); + // create new faces + + mesh.delete_face(cf.second); + for (size_t idx {}; idx < vertexCount; ++idx) { + const auto next = (idx + 1) % vertexCount; + newFaces.emplace("extrusion", + mesh.add_face({baseVertices[idx], baseVertices[next], vertices[next], vertices[idx]})); + } + newFaces.emplace(name, mesh.add_face(vertices)); + for (const auto & [name, faceController] : faceControllers) { + faceController.apply(mesh, name, newFaces); + } + faces.merge(std::move(newFaces)); + } + } + } +} diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h new file mode 100644 index 0000000..64c33f3 --- /dev/null +++ b/assetFactory/faceController.h @@ -0,0 +1,20 @@ +#pragma once + +#include "assetFactoryConfig_fwd.h" +#include "mutation.h" +#include "shape.h" +#include +#include + +class FaceController : public Mutation { +public: + using FaceControllers = std::map; + + void apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const; + + std::string id; + std::string colour; + std::string type; + bool smooth {false}; + FaceControllers faceControllers; +}; diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp new file mode 100644 index 0000000..1b89d14 --- /dev/null +++ b/assetFactory/factoryMesh.cpp @@ -0,0 +1,27 @@ +#include "factoryMesh.h" +#include "assetFactoryConfig.h" +#include "collections.hpp" +#include "gfx/models/vertex.hpp" +#include + +Mesh::Ptr +FactoryMesh::createMesh() const +{ + constexpr glm::vec2 NullUV {}; + + ModelFactoryMesh mesh; + for (const auto & use : uses) { + use->createMesh(mesh, glm::identity()); + } + mesh.garbage_collection(); + + mesh.triangulate(); + mesh.update_face_normals(); + std::vector vertices; + for (const auto & face : mesh.faces()) { + for (const auto & vertex : mesh.fv_range(face)) { + vertices.emplace_back(mesh.point(vertex), NullUV, mesh.property(mesh.face_normals_pph(), face)); + } + } + return std::make_shared(vertices, vectorOfN(vertices.size())); +} diff --git a/assetFactory/factoryMesh.h b/assetFactory/factoryMesh.h new file mode 100644 index 0000000..f850893 --- /dev/null +++ b/assetFactory/factoryMesh.h @@ -0,0 +1,14 @@ +#pragma once + +#include "gfx/models/mesh.h" +#include "stdTypeDefs.hpp" +#include "use.h" + +class FactoryMesh : public StdTypeDefs { +public: + Mesh::Ptr createMesh() const; + + std::string id; + glm::vec3 size; + Use::Collection uses; +}; diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp new file mode 100644 index 0000000..3a89157 --- /dev/null +++ b/assetFactory/modelFactory.cpp @@ -0,0 +1,14 @@ +#include "modelFactory.h" +#include "assetFactoryConfig.h" +#include "cuboid.h" +#include "cylinder.h" +#include "plane.h" + +ModelFactory::ModelFactory() : + shapes { + {"plane", std::make_shared()}, + {"cuboid", std::make_shared()}, + {"cylinder", std::make_shared()}, + } +{ +} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h new file mode 100644 index 0000000..f2b1b48 --- /dev/null +++ b/assetFactory/modelFactory.h @@ -0,0 +1,12 @@ +#pragma once + +#include "shape.h" + +class ModelFactory { +public: + using Shapes = std::map>; + + ModelFactory(); + + Shapes shapes; +}; diff --git a/assetFactory/mutation.cpp b/assetFactory/mutation.cpp new file mode 100644 index 0000000..21d2a24 --- /dev/null +++ b/assetFactory/mutation.cpp @@ -0,0 +1,10 @@ +#include "mutation.h" +#include +#include + +Mutation::Matrix +Mutation::getMatrix() const +{ + return glm::translate(glm::identity(), position) * rotate_ypr(rotation) + * glm::scale(glm::identity(), scale); +} diff --git a/assetFactory/mutation.h b/assetFactory/mutation.h new file mode 100644 index 0000000..440fab0 --- /dev/null +++ b/assetFactory/mutation.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +struct Mutation { + using Matrix = glm::mat4; + + Matrix getMatrix() const; + + glm::vec3 position {}; + glm::vec3 rotation {}; + glm::vec3 scale {1}; +}; diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp new file mode 100644 index 0000000..8b70676 --- /dev/null +++ b/assetFactory/object.cpp @@ -0,0 +1,17 @@ +#include "object.h" +#include + +Object::Object(std::string i) : id {std::move(i)} { } + +Object::CreatedFaces +Object::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const +{ + CreatedFaces faces; + for (const auto & use : uses) { + auto useFaces = use->createMesh(mesh, mutation); + std::transform(useFaces.begin(), useFaces.end(), std::inserter(faces, faces.end()), [this](auto && face) { + return std::make_pair(id + ":" + face.first, std::move(face.second)); + }); + } + return faces; +} diff --git a/assetFactory/object.h b/assetFactory/object.h new file mode 100644 index 0000000..f48b78a --- /dev/null +++ b/assetFactory/object.h @@ -0,0 +1,15 @@ +#pragma once + +#include "shape.h" +#include "stdTypeDefs.hpp" +#include "use.h" + +class Object : public StdTypeDefs, public Shape { +public: + Object(std::string i); + + CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; + + Use::Collection uses; + std::string id; +}; diff --git a/assetFactory/plane.cpp b/assetFactory/plane.cpp new file mode 100644 index 0000000..1e522ad --- /dev/null +++ b/assetFactory/plane.cpp @@ -0,0 +1,15 @@ +#include "plane.h" +#include "assetFactoryConfig.h" + +Plane::CreatedFaces +Plane::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const +{ + static constexpr std::array VERTICES {{ + {n, n, z}, + {y, n, z}, + {y, y, z}, + {n, y, z}, + }}; + + return {{"plane", mesh.add_face(addMutatedToMesh(mesh, VERTICES, mutation))}}; +} diff --git a/assetFactory/plane.h b/assetFactory/plane.h new file mode 100644 index 0000000..5e93ee4 --- /dev/null +++ b/assetFactory/plane.h @@ -0,0 +1,8 @@ +#pragma once + +#include "shape.h" + +class Plane : public Shape { +public: + CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; +}; diff --git a/assetFactory/shape.cpp b/assetFactory/shape.cpp new file mode 100644 index 0000000..0994898 --- /dev/null +++ b/assetFactory/shape.cpp @@ -0,0 +1,17 @@ +#include "shape.h" +#include "assetFactoryConfig.h" +#include "gfx/models/vertex.hpp" +#include "maths.h" +#include "shape.h" + +std::vector +Shape::addMutatedToMesh( + ModelFactoryMesh & mesh, const std::span vertices, const Mutation::Matrix & mutation) +{ + std::vector vhs; + std::transform(vertices.begin(), vertices.end(), std::back_inserter(vhs), [&mesh, &mutation](const auto & v) { + const auto p = v % mutation; + return mesh.add_vertex({p.x, p.y, p.z}); + }); + return vhs; +} diff --git a/assetFactory/shape.h b/assetFactory/shape.h new file mode 100644 index 0000000..9510538 --- /dev/null +++ b/assetFactory/shape.h @@ -0,0 +1,25 @@ +#pragma once + +#include "assetFactoryConfig_fwd.h" +#include "mutation.h" +#include "stdTypeDefs.hpp" +#include +#include +#include +#include + +class Vertex; + +class Shape : public StdTypeDefs { +public: + using CreatedFaces = std::multimap; + + static constexpr float z {}, y {.5}, n {-y}, o {1}; + + virtual ~Shape() = default; + + virtual CreatedFaces createMesh(ModelFactoryMesh &, const Mutation::Matrix & mutation) const = 0; + + static std::vector addMutatedToMesh( + ModelFactoryMesh & mesh, const std::span vertices, const Mutation::Matrix & mutation); +}; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp new file mode 100644 index 0000000..d191329 --- /dev/null +++ b/assetFactory/use.cpp @@ -0,0 +1,11 @@ +#include "use.h" + +Shape::CreatedFaces +Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const +{ + auto faces = type->createMesh(mesh, mutation * getMatrix()); + for (const auto & [name, faceController] : faceControllers) { + faceController.apply(mesh, name, faces); + } + return faces; +} diff --git a/assetFactory/use.h b/assetFactory/use.h new file mode 100644 index 0000000..4bfce97 --- /dev/null +++ b/assetFactory/use.h @@ -0,0 +1,17 @@ +#pragma once + +#include "assetFactoryConfig_fwd.h" +#include "faceController.h" +#include "shape.h" +#include "stdTypeDefs.hpp" + +class Use : public StdTypeDefs, public Mutation { +public: + using FaceControllers = std::map; + + Shape::CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; + + Shape::CPtr type; + std::string colour; + FaceControllers faceControllers; +}; -- cgit v1.2.3 From c22c676dfee583bd48e1f962378f580f8310838b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 16 Feb 2023 23:54:22 +0000 Subject: Refactor so ModelFactoryMesh can define the smooth property on faces --- assetFactory/assetFactoryConfig.h | 28 ---------------------------- assetFactory/assetFactoryConfig_fwd.h | 8 -------- assetFactory/cuboid.cpp | 2 +- assetFactory/cylinder.cpp | 2 +- assetFactory/faceController.cpp | 2 +- assetFactory/faceController.h | 2 +- assetFactory/factoryMesh.cpp | 2 +- assetFactory/modelFactory.cpp | 2 +- assetFactory/modelFactoryMesh.cpp | 6 ++++++ assetFactory/modelFactoryMesh.h | 34 ++++++++++++++++++++++++++++++++++ assetFactory/modelFactoryMesh_fwd.h | 3 +++ assetFactory/plane.cpp | 2 +- assetFactory/shape.cpp | 2 +- assetFactory/shape.h | 2 +- assetFactory/use.h | 2 +- 15 files changed, 53 insertions(+), 46 deletions(-) delete mode 100644 assetFactory/assetFactoryConfig.h delete mode 100644 assetFactory/assetFactoryConfig_fwd.h create mode 100644 assetFactory/modelFactoryMesh.cpp create mode 100644 assetFactory/modelFactoryMesh.h create mode 100644 assetFactory/modelFactoryMesh_fwd.h (limited to 'assetFactory') diff --git a/assetFactory/assetFactoryConfig.h b/assetFactory/assetFactoryConfig.h deleted file mode 100644 index 171b81a..0000000 --- a/assetFactory/assetFactoryConfig.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "assetFactoryConfig_fwd.h" -#include -#include -#include -#include - -namespace OpenMesh { - template struct glmvec : public VectorT { - using VectorT::VectorT; - glmvec(const VectorT & v) : VectorT {v} { } - operator glm::vec() const - { - glm::vec out; - std::copy(this->begin(), this->end(), &out[0]); - return out; - } - }; -} - -struct ModelFactoryTraits : public OpenMesh::DefaultTraits { - FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - EdgeAttributes(OpenMesh::Attributes::Status); - VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); - using Point = OpenMesh::glmvec; - using Normal = OpenMesh::glmvec; -}; diff --git a/assetFactory/assetFactoryConfig_fwd.h b/assetFactory/assetFactoryConfig_fwd.h deleted file mode 100644 index 28489ff..0000000 --- a/assetFactory/assetFactoryConfig_fwd.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -namespace OpenMesh { - template class PolyMesh_ArrayKernelT; -} -struct ModelFactoryTraits; - -using ModelFactoryMesh = OpenMesh::PolyMesh_ArrayKernelT; diff --git a/assetFactory/cuboid.cpp b/assetFactory/cuboid.cpp index 90b2108..86114e5 100644 --- a/assetFactory/cuboid.cpp +++ b/assetFactory/cuboid.cpp @@ -1,5 +1,5 @@ #include "cuboid.h" -#include "assetFactoryConfig.h" +#include "modelFactoryMesh.h" Cuboid::CreatedFaces Cuboid::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const diff --git a/assetFactory/cylinder.cpp b/assetFactory/cylinder.cpp index 66d456d..83cf035 100644 --- a/assetFactory/cylinder.cpp +++ b/assetFactory/cylinder.cpp @@ -1,6 +1,6 @@ #include "cylinder.h" -#include "assetFactoryConfig.h" #include "maths.h" +#include "modelFactoryMesh.h" Cylinder::CreatedFaces Cylinder::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index bdef74b..c04a656 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -1,6 +1,6 @@ #include "faceController.h" -#include "assetFactoryConfig.h" #include "maths.h" +#include "modelFactoryMesh.h" void FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 64c33f3..9974caf 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -1,6 +1,6 @@ #pragma once -#include "assetFactoryConfig_fwd.h" +#include "modelFactoryMesh_fwd.h" #include "mutation.h" #include "shape.h" #include diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 1b89d14..9d30ac9 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -1,7 +1,7 @@ #include "factoryMesh.h" -#include "assetFactoryConfig.h" #include "collections.hpp" #include "gfx/models/vertex.hpp" +#include "modelFactoryMesh.h" #include Mesh::Ptr diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp index 3a89157..4c25e48 100644 --- a/assetFactory/modelFactory.cpp +++ b/assetFactory/modelFactory.cpp @@ -1,7 +1,7 @@ #include "modelFactory.h" -#include "assetFactoryConfig.h" #include "cuboid.h" #include "cylinder.h" +#include "modelFactoryMesh_fwd.h" #include "plane.h" ModelFactory::ModelFactory() : diff --git a/assetFactory/modelFactoryMesh.cpp b/assetFactory/modelFactoryMesh.cpp new file mode 100644 index 0000000..6c772ad --- /dev/null +++ b/assetFactory/modelFactoryMesh.cpp @@ -0,0 +1,6 @@ +#include "modelFactoryMesh.h" + +ModelFactoryMesh::ModelFactoryMesh() +{ + add_property(smoothFaceProperty); +} diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h new file mode 100644 index 0000000..d0ffc26 --- /dev/null +++ b/assetFactory/modelFactoryMesh.h @@ -0,0 +1,34 @@ +#pragma once + +#include "modelFactoryMesh_fwd.h" +#include +#include +#include +#include + +namespace OpenMesh { + template struct glmvec : public VectorT { + using VectorT::VectorT; + glmvec(const VectorT & v) : VectorT {v} { } + operator glm::vec() const + { + glm::vec out; + std::copy(this->begin(), this->end(), &out[0]); + return out; + } + }; +} + +struct ModelFactoryTraits : public OpenMesh::DefaultTraits { + FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + EdgeAttributes(OpenMesh::Attributes::Status); + VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + using Point = OpenMesh::glmvec; + using Normal = OpenMesh::glmvec; +}; + +struct ModelFactoryMesh : public OpenMesh::PolyMesh_ArrayKernelT { + ModelFactoryMesh(); + + OpenMesh::FPropHandleT smoothFaceProperty; +}; diff --git a/assetFactory/modelFactoryMesh_fwd.h b/assetFactory/modelFactoryMesh_fwd.h new file mode 100644 index 0000000..ac10f2d --- /dev/null +++ b/assetFactory/modelFactoryMesh_fwd.h @@ -0,0 +1,3 @@ +#pragma once + +struct ModelFactoryMesh; diff --git a/assetFactory/plane.cpp b/assetFactory/plane.cpp index 1e522ad..37c8194 100644 --- a/assetFactory/plane.cpp +++ b/assetFactory/plane.cpp @@ -1,5 +1,5 @@ #include "plane.h" -#include "assetFactoryConfig.h" +#include "modelFactoryMesh.h" Plane::CreatedFaces Plane::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const diff --git a/assetFactory/shape.cpp b/assetFactory/shape.cpp index 0994898..f6e55e8 100644 --- a/assetFactory/shape.cpp +++ b/assetFactory/shape.cpp @@ -1,7 +1,7 @@ #include "shape.h" -#include "assetFactoryConfig.h" #include "gfx/models/vertex.hpp" #include "maths.h" +#include "modelFactoryMesh.h" #include "shape.h" std::vector diff --git a/assetFactory/shape.h b/assetFactory/shape.h index 9510538..5a2b59c 100644 --- a/assetFactory/shape.h +++ b/assetFactory/shape.h @@ -1,6 +1,6 @@ #pragma once -#include "assetFactoryConfig_fwd.h" +#include "modelFactoryMesh_fwd.h" #include "mutation.h" #include "stdTypeDefs.hpp" #include diff --git a/assetFactory/use.h b/assetFactory/use.h index 4bfce97..28f5459 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -1,7 +1,7 @@ #pragma once -#include "assetFactoryConfig_fwd.h" #include "faceController.h" +#include "modelFactoryMesh_fwd.h" #include "shape.h" #include "stdTypeDefs.hpp" -- cgit v1.2.3 From 88d5b474f3ab1c021612b0730f1b8d327a7a32db Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 16 Feb 2023 23:56:10 +0000 Subject: Add support for smooth faces --- assetFactory/cylinder.cpp | 3 +++ assetFactory/faceController.cpp | 8 ++++++++ assetFactory/factoryMesh.cpp | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'assetFactory') diff --git a/assetFactory/cylinder.cpp b/assetFactory/cylinder.cpp index 83cf035..d51dc37 100644 --- a/assetFactory/cylinder.cpp +++ b/assetFactory/cylinder.cpp @@ -36,6 +36,9 @@ Cylinder::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) mesh.add_vertex({xyz1t.x, xyz1t.y, xyz1t.z}), })); }); + for (const auto & [name, face] : surface) { + mesh.property(mesh.smoothFaceProperty, face) = true; + } surface.emplace("bottom", mesh.add_face(bottom)); surface.emplace("top", mesh.add_face(top)); diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index c04a656..7ec7820 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -23,6 +23,9 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: const auto vertexCount = points.size(); const auto centre = std::accumulate(points.begin(), points.end(), glm::vec3 {}) / static_cast(vertexCount); + if (smooth) { + mesh.property(mesh.smoothFaceProperty, cf.second) = true; + } if (type == "extrude") { Shape::CreatedFaces newFaces; // mutate points @@ -43,6 +46,11 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: mesh.add_face({baseVertices[idx], baseVertices[next], vertices[next], vertices[idx]})); } newFaces.emplace(name, mesh.add_face(vertices)); + if (smooth) { + for (const auto & [name, face] : newFaces) { + mesh.property(mesh.smoothFaceProperty, face) = true; + } + } for (const auto & [name, faceController] : faceControllers) { faceController.apply(mesh, name, newFaces); } diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 9d30ac9..baa031d 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -17,10 +17,14 @@ FactoryMesh::createMesh() const mesh.triangulate(); mesh.update_face_normals(); + mesh.update_vertex_normals(); std::vector vertices; for (const auto & face : mesh.faces()) { + const auto smooth = mesh.property(mesh.smoothFaceProperty, face); for (const auto & vertex : mesh.fv_range(face)) { - vertices.emplace_back(mesh.point(vertex), NullUV, mesh.property(mesh.face_normals_pph(), face)); + vertices.emplace_back(mesh.point(vertex), NullUV, + smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) + : mesh.property(mesh.face_normals_pph(), face)); } } return std::make_shared(vertices, vectorOfN(vertices.size())); -- cgit v1.2.3 From 6772443b0ef78ce7cbdd815103d7653f2b243e36 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 01:05:38 +0000 Subject: Add missing override --- assetFactory/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assetFactory') diff --git a/assetFactory/object.h b/assetFactory/object.h index f48b78a..da28c1f 100644 --- a/assetFactory/object.h +++ b/assetFactory/object.h @@ -8,7 +8,7 @@ class Object : public StdTypeDefs, public Shape { public: Object(std::string i); - CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; + CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; Use::Collection uses; std::string id; -- cgit v1.2.3 From ed7c2a6a73f24f97a0f04c2e6be6862ffe54b585 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 01:25:34 +0000 Subject: Support for loading objects, uses and model factories from an XML resource --- assetFactory/modelFactory.cpp | 17 +++++++++++++++++ assetFactory/modelFactory.h | 9 ++++++++- assetFactory/object.cpp | 22 ++++++++++++++++++++++ assetFactory/object.h | 13 ++++++++++++- assetFactory/use.cpp | 20 ++++++++++++++++++++ assetFactory/use.h | 12 +++++++++++- 6 files changed, 90 insertions(+), 3 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp index 4c25e48..2642900 100644 --- a/assetFactory/modelFactory.cpp +++ b/assetFactory/modelFactory.cpp @@ -2,7 +2,10 @@ #include "cuboid.h" #include "cylinder.h" #include "modelFactoryMesh_fwd.h" +#include "object.h" #include "plane.h" +#include "saxParse-persistence.h" +#include ModelFactory::ModelFactory() : shapes { @@ -12,3 +15,17 @@ ModelFactory::ModelFactory() : } { } + +std::shared_ptr +ModelFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +ModelFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h index f2b1b48..94db055 100644 --- a/assetFactory/modelFactory.h +++ b/assetFactory/modelFactory.h @@ -1,12 +1,19 @@ #pragma once +#include "persistence.h" #include "shape.h" +#include -class ModelFactory { +class ModelFactory : public Persistence::Persistable { public: using Shapes = std::map>; ModelFactory(); + [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp index 8b70676..faa9a17 100644 --- a/assetFactory/object.cpp +++ b/assetFactory/object.cpp @@ -15,3 +15,25 @@ Object::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) c } return faces; } + +template struct Appender : public Persistence::SelectionT> { + Appender(Container & c) : Persistence::SelectionT> {s}, container {c} { } + using Persistence::SelectionT>::SelectionT; + void + endObject(Persistence::Stack & stk) override + { + container.emplace_back(s); + stk.pop(); + } + +private: + std::shared_ptr s; + Container & container; +}; + +bool +Object::persist(Persistence::PersistenceStore & store) +{ + using UseAppend = Appender; + return STORE_TYPE && STORE_MEMBER(id) && STORE_NAME_HELPER("use", uses, UseAppend); +} diff --git a/assetFactory/object.h b/assetFactory/object.h index da28c1f..1069f66 100644 --- a/assetFactory/object.h +++ b/assetFactory/object.h @@ -1,15 +1,26 @@ #pragma once +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" #include "use.h" -class Object : public StdTypeDefs, public Shape { +class Object : public StdTypeDefs, public Shape, public Persistence::Persistable { public: + Object() = default; Object(std::string i); CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const override; Use::Collection uses; std::string id; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return id; + }; }; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index d191329..21e26f3 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -1,4 +1,5 @@ #include "use.h" +#include "modelFactory.h" Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const @@ -9,3 +10,22 @@ Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) cons } return faces; } + +struct Lookup : public Persistence::SelectionV { + using Persistence::SelectionV::SelectionV; + using Persistence::SelectionV::setValue; + void + setValue(std::string && str) override + { + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("modelFactory"))) { + v = mf->shapes.at(str); + } + } +}; + +bool +Use::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) + && STORE_MEMBER(rotation); +} diff --git a/assetFactory/use.h b/assetFactory/use.h index 28f5459..96f07f6 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -2,10 +2,11 @@ #include "faceController.h" #include "modelFactoryMesh_fwd.h" +#include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" -class Use : public StdTypeDefs, public Mutation { +class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { public: using FaceControllers = std::map; @@ -14,4 +15,13 @@ public: Shape::CPtr type; std::string colour; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; -- cgit v1.2.3 From 5e561d390dc82b08c20532de0952f428e7b14283 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 19:34:16 +0000 Subject: Rename ModelFactory to AssetFactory --- assetFactory/assetFactory.cpp | 31 +++++++++++++++++++++++++++++++ assetFactory/assetFactory.h | 19 +++++++++++++++++++ assetFactory/modelFactory.cpp | 31 ------------------------------- assetFactory/modelFactory.h | 19 ------------------- assetFactory/use.cpp | 4 ++-- 5 files changed, 52 insertions(+), 52 deletions(-) create mode 100644 assetFactory/assetFactory.cpp create mode 100644 assetFactory/assetFactory.h delete mode 100644 assetFactory/modelFactory.cpp delete mode 100644 assetFactory/modelFactory.h (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp new file mode 100644 index 0000000..0ee1f94 --- /dev/null +++ b/assetFactory/assetFactory.cpp @@ -0,0 +1,31 @@ +#include "assetFactory.h" +#include "cuboid.h" +#include "cylinder.h" +#include "modelFactoryMesh_fwd.h" +#include "object.h" +#include "plane.h" +#include "saxParse-persistence.h" +#include + +AssetFactory::AssetFactory() : + shapes { + {"plane", std::make_shared()}, + {"cuboid", std::make_shared()}, + {"cylinder", std::make_shared()}, + } +{ +} + +std::shared_ptr +AssetFactory::loadXML(const std::filesystem::path & filename) +{ + filesystem::FileStar file {filename.c_str(), "r"}; + return Persistence::SAXParsePersistence {}.loadState>(file); +} + +bool +AssetFactory::persist(Persistence::PersistenceStore & store) +{ + using MapObjects = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); +} diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h new file mode 100644 index 0000000..5cf90dd --- /dev/null +++ b/assetFactory/assetFactory.h @@ -0,0 +1,19 @@ +#pragma once + +#include "persistence.h" +#include "shape.h" +#include + +class AssetFactory : public Persistence::Persistable { +public: + using Shapes = std::map>; + + AssetFactory(); + [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); + + Shapes shapes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; +}; diff --git a/assetFactory/modelFactory.cpp b/assetFactory/modelFactory.cpp deleted file mode 100644 index 2642900..0000000 --- a/assetFactory/modelFactory.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "modelFactory.h" -#include "cuboid.h" -#include "cylinder.h" -#include "modelFactoryMesh_fwd.h" -#include "object.h" -#include "plane.h" -#include "saxParse-persistence.h" -#include - -ModelFactory::ModelFactory() : - shapes { - {"plane", std::make_shared()}, - {"cuboid", std::make_shared()}, - {"cylinder", std::make_shared()}, - } -{ -} - -std::shared_ptr -ModelFactory::loadXML(const std::filesystem::path & filename) -{ - filesystem::FileStar file {filename.c_str(), "r"}; - return Persistence::SAXParsePersistence {}.loadState>(file); -} - -bool -ModelFactory::persist(Persistence::PersistenceStore & store) -{ - using MapObjects = Persistence::MapByMember; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); -} diff --git a/assetFactory/modelFactory.h b/assetFactory/modelFactory.h deleted file mode 100644 index 94db055..0000000 --- a/assetFactory/modelFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "persistence.h" -#include "shape.h" -#include - -class ModelFactory : public Persistence::Persistable { -public: - using Shapes = std::map>; - - ModelFactory(); - [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); - - Shapes shapes; - -private: - friend Persistence::SelectionPtrBase, true>; - bool persist(Persistence::PersistenceStore & store) override; -}; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 21e26f3..3b574c3 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -1,5 +1,5 @@ #include "use.h" -#include "modelFactory.h" +#include "assetFactory.h" Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const @@ -17,7 +17,7 @@ struct Lookup : public Persistence::SelectionV { void setValue(std::string && str) override { - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("modelFactory"))) { + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { v = mf->shapes.at(str); } } -- cgit v1.2.3 From f91e2ea8d9e30b1e62c4f8784fddfd4eb9578d2d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 21 Feb 2023 20:09:41 +0000 Subject: Move Appender in Persistence NS and simplify types --- assetFactory/object.cpp | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/object.cpp b/assetFactory/object.cpp index faa9a17..ae5a301 100644 --- a/assetFactory/object.cpp +++ b/assetFactory/object.cpp @@ -16,24 +16,8 @@ Object::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) c return faces; } -template struct Appender : public Persistence::SelectionT> { - Appender(Container & c) : Persistence::SelectionT> {s}, container {c} { } - using Persistence::SelectionT>::SelectionT; - void - endObject(Persistence::Stack & stk) override - { - container.emplace_back(s); - stk.pop(); - } - -private: - std::shared_ptr s; - Container & container; -}; - bool Object::persist(Persistence::PersistenceStore & store) { - using UseAppend = Appender; - return STORE_TYPE && STORE_MEMBER(id) && STORE_NAME_HELPER("use", uses, UseAppend); + return STORE_TYPE && STORE_MEMBER(id) && STORE_NAME_HELPER("use", uses, Persistence::Appender); } -- cgit v1.2.3 From 320e5cc574f0c8f83def034e36f6d0c57b1f75ac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Feb 2023 20:54:39 +0000 Subject: Fixup MapByMember to work with shared or unique ptr --- assetFactory/assetFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 0ee1f94..470eacf 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -26,6 +26,6 @@ AssetFactory::loadXML(const std::filesystem::path & filename) bool AssetFactory::persist(Persistence::PersistenceStore & store) { - using MapObjects = Persistence::MapByMember; + using MapObjects = Persistence::MapByMember>; return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); } -- cgit v1.2.3 From df2a078c51cee464905c6fb1d1c7c4aa7873f6a1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Feb 2023 23:40:46 +0000 Subject: Implement loading asset, mesh and face definitions --- assetFactory/assetFactory.cpp | 11 ++++++++++- assetFactory/assetFactory.h | 18 +++++++++++++++++- assetFactory/faceController.cpp | 10 +++++++++- assetFactory/faceController.h | 14 ++++++++++++-- assetFactory/factoryMesh.cpp | 7 +++++++ assetFactory/factoryMesh.h | 6 +++++- assetFactory/use.cpp | 5 +++-- assetFactory/use.h | 2 +- 8 files changed, 64 insertions(+), 9 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 470eacf..564ea28 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -27,5 +27,14 @@ bool AssetFactory::persist(Persistence::PersistenceStore & store) { using MapObjects = Persistence::MapByMember>; - return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects); + using MapAssets = Persistence::MapByMember; + return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) + && STORE_NAME_HELPER("asset", assets, MapAssets); +} + +bool +Asset::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) + && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); } diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 5cf90dd..9d79827 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -1,17 +1,33 @@ #pragma once +#include "factoryMesh.h" #include "persistence.h" #include "shape.h" #include +#include + +class Asset : public Persistence::Persistable, public StdTypeDefs { +public: + std::string id; + std::string name; + + FactoryMesh::Collection meshes; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; +}; class AssetFactory : public Persistence::Persistable { public: - using Shapes = std::map>; + using Shapes = std::map>; + using Assets = std::map>; AssetFactory(); [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; + Assets assets; private: friend Persistence::SelectionPtrBase, true>; diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index 7ec7820..499f7e4 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -52,10 +52,18 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: } } for (const auto & [name, faceController] : faceControllers) { - faceController.apply(mesh, name, newFaces); + faceController->apply(mesh, name, newFaces); } faces.merge(std::move(newFaces)); } } } } + +bool +FaceController::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(colour) && STORE_MEMBER(type) && STORE_MEMBER(smooth) + && STORE_MEMBER(scale) && STORE_MEMBER(position) && STORE_MEMBER(rotation) + && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); +} diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 9974caf..296210d 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -2,13 +2,14 @@ #include "modelFactoryMesh_fwd.h" #include "mutation.h" +#include "persistence.h" #include "shape.h" #include #include -class FaceController : public Mutation { +class FaceController : public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map; + using FaceControllers = std::map>; void apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const; @@ -17,4 +18,13 @@ public: std::string type; bool smooth {false}; FaceControllers faceControllers; + +private: + friend Persistence::SelectionPtrBase, false>; + bool persist(Persistence::PersistenceStore & store) override; + std::string + getId() const override + { + return {}; + }; }; diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index baa031d..0fb72ad 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -29,3 +29,10 @@ FactoryMesh::createMesh() const } return std::make_shared(vertices, vectorOfN(vertices.size())); } + +bool +FactoryMesh::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(size) + && STORE_NAME_HELPER("use", uses, Persistence::Appender); +} diff --git a/assetFactory/factoryMesh.h b/assetFactory/factoryMesh.h index f850893..4b6d3e5 100644 --- a/assetFactory/factoryMesh.h +++ b/assetFactory/factoryMesh.h @@ -4,11 +4,15 @@ #include "stdTypeDefs.hpp" #include "use.h" -class FactoryMesh : public StdTypeDefs { +class FactoryMesh : public Persistence::Persistable, public StdTypeDefs { public: Mesh::Ptr createMesh() const; std::string id; glm::vec3 size; Use::Collection uses; + +private: + friend Persistence::SelectionPtrBase, true>; + bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 3b574c3..1f28332 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -6,7 +6,7 @@ Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) cons { auto faces = type->createMesh(mesh, mutation * getMatrix()); for (const auto & [name, faceController] : faceControllers) { - faceController.apply(mesh, name, faces); + faceController->apply(mesh, name, faces); } return faces; } @@ -27,5 +27,6 @@ bool Use::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) - && STORE_MEMBER(rotation); + && STORE_MEMBER(rotation) && STORE_MEMBER(colour) + && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); } diff --git a/assetFactory/use.h b/assetFactory/use.h index 96f07f6..853af23 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -8,7 +8,7 @@ class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { public: - using FaceControllers = std::map; + using FaceControllers = std::map>; Shape::CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; -- cgit v1.2.3 From 7d0decccaac3aa564b549d91a36279e7aca0814e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 24 Feb 2023 19:30:30 +0000 Subject: Support for recursive colouring of asset factory faces Updates colours in sample model. --- assetFactory/faceController.cpp | 29 +++++++++++++------- assetFactory/faceController.h | 7 ++--- assetFactory/factoryMesh.cpp | 4 ++- assetFactory/modelFactoryMesh.h | 3 ++- assetFactory/style.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ assetFactory/style.h | 22 +++++++++++++++ assetFactory/use.cpp | 5 ++-- assetFactory/use.h | 4 +-- 8 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 assetFactory/style.cpp create mode 100644 assetFactory/style.h (limited to 'assetFactory') diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index 499f7e4..9f057da 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -1,14 +1,19 @@ #include "faceController.h" +#include "collections.hpp" #include "maths.h" #include "modelFactoryMesh.h" void -FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const +FaceController::apply(ModelFactoryMesh & mesh, const StyleStack & parents, const std::string & name, + Shape::CreatedFaces & faces) const { + const auto controlledFacesRange = faces.equal_range(name); + const std::vector controlledFaces(controlledFacesRange.first, controlledFacesRange.second); + if (controlledFaces.empty()) { + throw std::runtime_error("Named face(s) do not exist: " + name); + } if (!type.empty()) { const auto mutation = getMatrix(); - const auto controlledFacesRange = faces.equal_range(name); - const std::vector controlledFaces(controlledFacesRange.first, controlledFacesRange.second); faces.erase(name); for (const auto & cf : controlledFaces) { // get face vertices @@ -23,9 +28,6 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: const auto vertexCount = points.size(); const auto centre = std::accumulate(points.begin(), points.end(), glm::vec3 {}) / static_cast(vertexCount); - if (smooth) { - mesh.property(mesh.smoothFaceProperty, cf.second) = true; - } if (type == "extrude") { Shape::CreatedFaces newFaces; // mutate points @@ -38,7 +40,6 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: return mesh.add_vertex({p.x, p.y, p.z}); }); // create new faces - mesh.delete_face(cf.second); for (size_t idx {}; idx < vertexCount; ++idx) { const auto next = (idx + 1) % vertexCount; @@ -51,11 +52,21 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: mesh.property(mesh.smoothFaceProperty, face) = true; } } + applyStyle(mesh, parents + this, newFaces); for (const auto & [name, faceController] : faceControllers) { - faceController->apply(mesh, name, newFaces); + faceController->apply(mesh, parents + this, name, newFaces); } faces.merge(std::move(newFaces)); } + else { + mesh.property(mesh.smoothFaceProperty, cf.second) = smooth; + applyStyle(mesh, parents + this, cf.second); + } + } + } + else { + for (const auto & cf : controlledFaces) { + applyStyle(mesh, parents + this, cf.second); } } } @@ -63,7 +74,7 @@ FaceController::apply(ModelFactoryMesh & mesh, const std::string & name, Shape:: bool FaceController::persist(Persistence::PersistenceStore & store) { - return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(colour) && STORE_MEMBER(type) && STORE_MEMBER(smooth) + return STORE_TYPE && STORE_MEMBER(id) && Style::persist(store) && STORE_MEMBER(type) && STORE_MEMBER(smooth) && STORE_MEMBER(scale) && STORE_MEMBER(position) && STORE_MEMBER(rotation) && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); } diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 296210d..0618388 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -4,17 +4,18 @@ #include "mutation.h" #include "persistence.h" #include "shape.h" +#include "style.h" #include #include -class FaceController : public Mutation, public Persistence::Persistable { +class FaceController : public Mutation, public Style, public Persistence::Persistable { public: using FaceControllers = std::map>; - void apply(ModelFactoryMesh & mesh, const std::string & name, Shape::CreatedFaces & faces) const; + void apply(ModelFactoryMesh & mesh, const Style::StyleStack & parents, const std::string & name, + Shape::CreatedFaces & faces) const; std::string id; - std::string colour; std::string type; bool smooth {false}; FaceControllers faceControllers; diff --git a/assetFactory/factoryMesh.cpp b/assetFactory/factoryMesh.cpp index 0fb72ad..0cfed85 100644 --- a/assetFactory/factoryMesh.cpp +++ b/assetFactory/factoryMesh.cpp @@ -21,10 +21,12 @@ FactoryMesh::createMesh() const std::vector vertices; for (const auto & face : mesh.faces()) { const auto smooth = mesh.property(mesh.smoothFaceProperty, face); + const auto colour = mesh.color(face); for (const auto & vertex : mesh.fv_range(face)) { vertices.emplace_back(mesh.point(vertex), NullUV, smooth ? mesh.property(mesh.vertex_normals_pph(), vertex) - : mesh.property(mesh.face_normals_pph(), face)); + : mesh.property(mesh.face_normals_pph(), face), + colour); } } return std::make_shared(vertices, vectorOfN(vertices.size())); diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h index d0ffc26..7d222f5 100644 --- a/assetFactory/modelFactoryMesh.h +++ b/assetFactory/modelFactoryMesh.h @@ -20,11 +20,12 @@ namespace OpenMesh { } struct ModelFactoryTraits : public OpenMesh::DefaultTraits { - FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); + FaceAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status | OpenMesh::Attributes::Color); EdgeAttributes(OpenMesh::Attributes::Status); VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); using Point = OpenMesh::glmvec; using Normal = OpenMesh::glmvec; + using Color = OpenMesh::glmvec; }; struct ModelFactoryMesh : public OpenMesh::PolyMesh_ArrayKernelT { diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp new file mode 100644 index 0000000..099d81a --- /dev/null +++ b/assetFactory/style.cpp @@ -0,0 +1,59 @@ +#include "style.h" + +ModelFactoryMesh::Color +Style::parseColour(const std::string_view & in) +{ + if (in.empty()) { + return {}; + } + if (in[0] == '#') { + ModelFactoryMesh::Color out {0, 0, 0, 1}; + std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { + uint8_t channel; + std::from_chars(in, in + 2, channel, 16); + in += 2; + return static_cast(channel) / 256.F; + }); + return out; + } + return {}; +} + +void +Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Shape::CreatedFaces & faces) const +{ + if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { + const auto parsedColour = parseColour(effectiveColour); + for (const auto & face : faces) { + mesh.set_color(face.second, parsedColour); + } + } +} + +void +Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const ModelFactoryMesh::FaceHandle & face) const +{ + if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { + const auto parsedColour = parseColour(effectiveColour); + mesh.set_color(face, parsedColour); + } +} + +std::string_view +Style::getProperty(const StyleStack & parents, std::string Style::*member) +{ + if (const auto itr = std::find_if(parents.rbegin(), parents.rend(), + [&member](auto && s) { + return !(s->*member).empty(); + }); + itr != parents.rend()) { + return (*itr)->*member; + } + return {}; +} + +bool +Style::persist(Persistence::PersistenceStore & store) +{ + return STORE_MEMBER(colour); +} diff --git a/assetFactory/style.h b/assetFactory/style.h new file mode 100644 index 0000000..0c7ad5a --- /dev/null +++ b/assetFactory/style.h @@ -0,0 +1,22 @@ +#pragma once + +#include "modelFactoryMesh.h" +#include "persistence.h" +#include "shape.h" +#include + +class Style { +public: + using StyleStack = std::vector; + + static ModelFactoryMesh::Color parseColour(const std::string_view &); + void applyStyle(ModelFactoryMesh &, const StyleStack & parents, const Shape::CreatedFaces &) const; + void applyStyle(ModelFactoryMesh &, const StyleStack & parents, const ModelFactoryMesh::FaceHandle &) const; + + static std::string_view getProperty(const StyleStack & parents, std::string Style::*member); + + std::string colour; + +protected: + bool persist(Persistence::PersistenceStore & store); +}; diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 1f28332..a6fac5c 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -5,8 +5,9 @@ Shape::CreatedFaces Use::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const { auto faces = type->createMesh(mesh, mutation * getMatrix()); + applyStyle(mesh, {this}, faces); for (const auto & [name, faceController] : faceControllers) { - faceController->apply(mesh, name, faces); + faceController->apply(mesh, {this}, name, faces); } return faces; } @@ -27,6 +28,6 @@ bool Use::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_HELPER(type, Lookup) && STORE_MEMBER(position) && STORE_MEMBER(scale) - && STORE_MEMBER(rotation) && STORE_MEMBER(colour) + && STORE_MEMBER(rotation) && Style::persist(store) && STORE_NAME_HELPER("face", faceControllers, Persistence::MapByMember); } diff --git a/assetFactory/use.h b/assetFactory/use.h index 853af23..5b61eca 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -5,15 +5,15 @@ #include "persistence.h" #include "shape.h" #include "stdTypeDefs.hpp" +#include "style.h" -class Use : public StdTypeDefs, public Mutation, public Persistence::Persistable { +class Use : public StdTypeDefs, public Mutation, public Style, public Persistence::Persistable { public: using FaceControllers = std::map>; Shape::CreatedFaces createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const; Shape::CPtr type; - std::string colour; FaceControllers faceControllers; private: -- cgit v1.2.3 From b76d161a968ff4a2817e9cfe5c02d3538ef6029f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Feb 2023 03:05:24 +0000 Subject: Add face name property and wrapper to add a named face --- assetFactory/modelFactoryMesh.cpp | 9 +++++++++ assetFactory/modelFactoryMesh.h | 3 +++ 2 files changed, 12 insertions(+) (limited to 'assetFactory') diff --git a/assetFactory/modelFactoryMesh.cpp b/assetFactory/modelFactoryMesh.cpp index 6c772ad..f9ee6a0 100644 --- a/assetFactory/modelFactoryMesh.cpp +++ b/assetFactory/modelFactoryMesh.cpp @@ -3,4 +3,13 @@ ModelFactoryMesh::ModelFactoryMesh() { add_property(smoothFaceProperty); + add_property(nameFaceProperty); +} + +std::pair +ModelFactoryMesh::add_namedFace(std::string name, std::vector p) +{ + const auto handle = add_face(std::move(p)); + property(nameFaceProperty, handle) = name; + return std::make_pair(name, handle); } diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h index 7d222f5..b4f5254 100644 --- a/assetFactory/modelFactoryMesh.h +++ b/assetFactory/modelFactoryMesh.h @@ -32,4 +32,7 @@ struct ModelFactoryMesh : public OpenMesh::PolyMesh_ArrayKernelT smoothFaceProperty; + OpenMesh::FPropHandleT nameFaceProperty; + + std::pair add_namedFace(std::string name, std::vector p); }; -- cgit v1.2.3 From f6f085d3744990a6e80927aa17f4c5d77456d4f9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Feb 2023 03:10:25 +0000 Subject: Primitives add named faces --- assetFactory/cuboid.cpp | 12 ++++++------ assetFactory/cylinder.cpp | 10 +++++----- assetFactory/plane.cpp | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/cuboid.cpp b/assetFactory/cuboid.cpp index 86114e5..24fe4a4 100644 --- a/assetFactory/cuboid.cpp +++ b/assetFactory/cuboid.cpp @@ -19,11 +19,11 @@ Cuboid::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) c const auto vhs = addMutatedToMesh(mesh, VERTICES, mutation); return { - {"top", mesh.add_face({vhs[4], vhs[5], vhs[6], vhs[7]})}, - {"bottom", mesh.add_face({vhs[0], vhs[1], vhs[2], vhs[3]})}, - {"left", mesh.add_face({vhs[0], vhs[7], vhs[6], vhs[1]})}, - {"right", mesh.add_face({vhs[2], vhs[5], vhs[4], vhs[3]})}, - {"front", mesh.add_face({vhs[0], vhs[3], vhs[4], vhs[7]})}, - {"back", mesh.add_face({vhs[2], vhs[1], vhs[6], vhs[5]})}, + mesh.add_namedFace("top", {vhs[4], vhs[5], vhs[6], vhs[7]}), + mesh.add_namedFace("bottom", {vhs[0], vhs[1], vhs[2], vhs[3]}), + mesh.add_namedFace("left", {vhs[0], vhs[7], vhs[6], vhs[1]}), + mesh.add_namedFace("right", {vhs[2], vhs[5], vhs[4], vhs[3]}), + mesh.add_namedFace("front", {vhs[0], vhs[3], vhs[4], vhs[7]}), + mesh.add_namedFace("back", {vhs[2], vhs[1], vhs[6], vhs[5]}), }; } diff --git a/assetFactory/cylinder.cpp b/assetFactory/cylinder.cpp index d51dc37..cf0dbfb 100644 --- a/assetFactory/cylinder.cpp +++ b/assetFactory/cylinder.cpp @@ -28,19 +28,19 @@ Cylinder::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) const auto xyz2b = (xy2 ^ 0) % mutation; const auto xyz1t = (xy1 ^ 1) % mutation; const auto xyz2t = (xy2 ^ 1) % mutation; - return std::make_pair(std::string {"edge"}, - mesh.add_face({ + return mesh.add_namedFace("edge", + { mesh.add_vertex({xyz1b.x, xyz1b.y, xyz1b.z}), mesh.add_vertex({xyz2b.x, xyz2b.y, xyz2b.z}), mesh.add_vertex({xyz2t.x, xyz2t.y, xyz2t.z}), mesh.add_vertex({xyz1t.x, xyz1t.y, xyz1t.z}), - })); + }); }); for (const auto & [name, face] : surface) { mesh.property(mesh.smoothFaceProperty, face) = true; } - surface.emplace("bottom", mesh.add_face(bottom)); - surface.emplace("top", mesh.add_face(top)); + surface.insert(mesh.add_namedFace("bottom", bottom)); + surface.insert(mesh.add_namedFace("top", top)); return surface; } diff --git a/assetFactory/plane.cpp b/assetFactory/plane.cpp index 37c8194..563c4e9 100644 --- a/assetFactory/plane.cpp +++ b/assetFactory/plane.cpp @@ -11,5 +11,5 @@ Plane::createMesh(ModelFactoryMesh & mesh, const Mutation::Matrix & mutation) co {n, y, z}, }}; - return {{"plane", mesh.add_face(addMutatedToMesh(mesh, VERTICES, mutation))}}; + return {mesh.add_namedFace("plane", addMutatedToMesh(mesh, VERTICES, mutation))}; } -- cgit v1.2.3 From 43856fbd3b4a23e9ce41a85a641479a11413602d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Feb 2023 03:31:12 +0000 Subject: Name new faces based on adjacent faces when extruding --- assetFactory/faceController.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/faceController.cpp b/assetFactory/faceController.cpp index 9f057da..b485c80 100644 --- a/assetFactory/faceController.cpp +++ b/assetFactory/faceController.cpp @@ -7,23 +7,30 @@ void FaceController::apply(ModelFactoryMesh & mesh, const StyleStack & parents, const std::string & name, Shape::CreatedFaces & faces) const { - const auto controlledFacesRange = faces.equal_range(name); - const std::vector controlledFaces(controlledFacesRange.first, controlledFacesRange.second); + const auto getAdjacentFaceName = [&mesh](const auto & ofrange, OpenMesh::FaceHandle nf) -> std::string { + const auto nfrange = mesh.ff_range(nf); + if (const auto target = std::find_first_of(ofrange.begin(), ofrange.end(), nfrange.begin(), nfrange.end()); + target != ofrange.end()) { + return mesh.property(mesh.nameFaceProperty, *target); + }; + return {}; + }; + + const auto controlledFaces {materializeRange(faces.equal_range(name))}; if (controlledFaces.empty()) { throw std::runtime_error("Named face(s) do not exist: " + name); } + if (!type.empty()) { const auto mutation = getMatrix(); faces.erase(name); for (const auto & cf : controlledFaces) { - // get face vertices - const auto faceVertexRange = mesh.fv_range(cf.second); // get points - const std::vector baseVertices(faceVertexRange.begin(), faceVertexRange.end()); - std::vector points; - std::transform( - faceVertexRange.begin(), faceVertexRange.end(), std::back_inserter(points), [&mesh](auto && v) { - return mesh.point(v); + const auto baseVertices {materializeRange(mesh.fv_range(cf.second))}; + auto points = std::accumulate(baseVertices.begin(), baseVertices.end(), std::vector {}, + [&mesh](auto && out, auto && v) { + out.push_back(mesh.point(v)); + return std::move(out); }); const auto vertexCount = points.size(); const auto centre @@ -40,11 +47,15 @@ FaceController::apply(ModelFactoryMesh & mesh, const StyleStack & parents, const return mesh.add_vertex({p.x, p.y, p.z}); }); // create new faces + const auto ofrange = materializeRange(mesh.ff_range(cf.second)); mesh.delete_face(cf.second); for (size_t idx {}; idx < vertexCount; ++idx) { const auto next = (idx + 1) % vertexCount; - newFaces.emplace("extrusion", - mesh.add_face({baseVertices[idx], baseVertices[next], vertices[next], vertices[idx]})); + const auto newFace + = mesh.add_face({baseVertices[idx], baseVertices[next], vertices[next], vertices[idx]}); + auto & name = mesh.property(mesh.nameFaceProperty, newFace); + name = getAdjacentFaceName(ofrange, newFace); + newFaces.emplace(name, newFace); } newFaces.emplace(name, mesh.add_face(vertices)); if (smooth) { -- cgit v1.2.3 From b7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 19:38:14 +0000 Subject: Load the X11 RGB colour definitions into a map --- assetFactory/assetFactory.cpp | 28 +++++++++++++++++++++++++++- assetFactory/assetFactory.h | 6 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 564ea28..016f29e 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -1,4 +1,5 @@ #include "assetFactory.h" +#include "collections.hpp" #include "cuboid.h" #include "cylinder.h" #include "modelFactoryMesh_fwd.h" @@ -12,7 +13,8 @@ AssetFactory::AssetFactory() : {"plane", std::make_shared()}, {"cuboid", std::make_shared()}, {"cylinder", std::make_shared()}, - } + }, + colours {parseX11RGB("/usr/share/X11/rgb.txt")} { } @@ -23,6 +25,30 @@ AssetFactory::loadXML(const std::filesystem::path & filename) return Persistence::SAXParsePersistence {}.loadState>(file); } +AssetFactory::Colours +AssetFactory::parseX11RGB(const char * path) +{ + filesystem::FileStar rgb {path, "r"}; + Colours out; + glm::u8vec3 colour; + char inname[BUFSIZ]; + while (fscanf(rgb, "%hhu %hhu %hhu %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { + std::string name {inname}; + normalizeColourName(name); + out.emplace(std::move(name), colour); + } + return out; +} + +void +AssetFactory::normalizeColourName(std::string & name) +{ + std::erase_if(name, ::isblank); + name *= [l = std::locale {}](auto & ch) { + ch = std::tolower(ch, l); + }; +} + bool AssetFactory::persist(Persistence::PersistenceStore & store) { diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 9d79827..a68b460 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -22,12 +22,18 @@ class AssetFactory : public Persistence::Persistable { public: using Shapes = std::map>; using Assets = std::map>; + using Colour = glm::u8vec3; + using Colours = std::map>; AssetFactory(); [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); Shapes shapes; Assets assets; + Colours colours; + + static Colours parseX11RGB(const char * rgbtxtpath); + static void normalizeColourName(std::string &); private: friend Persistence::SelectionPtrBase, true>; -- cgit v1.2.3 From 5c703a1549b88339ca48ac3c48f67ab7503d223a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 23:56:03 +0000 Subject: Support for named colours in assets Fixes up some error handling in colour parser. --- assetFactory/style.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index 099d81a..d2977a7 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -1,12 +1,16 @@ #include "style.h" +#include "assetFactory.h" ModelFactoryMesh::Color Style::parseColour(const std::string_view & in) { if (in.empty()) { - return {}; + throw std::runtime_error("Empty colour specification"); } if (in[0] == '#') { + if (in.length() > 9 || in.length() % 2 == 0) { + throw std::runtime_error("Invalid hex colour specification"); + } ModelFactoryMesh::Color out {0, 0, 0, 1}; std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { uint8_t channel; @@ -16,7 +20,13 @@ Style::parseColour(const std::string_view & in) }); return out; } - return {}; + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { + const auto out = glm::vec3 {colour->second} / 256.F; + return {out.r, out.g, out.b, 1.f}; + } + } + throw std::runtime_error("No such asset factory colour"); } void -- cgit v1.2.3 From 7a70e75656d31428c9bbbd51fbf1ca920e577ed1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 28 Feb 2023 01:24:18 +0000 Subject: Remove to specify if the Selection pointer type is shared or not Inferred based on whether the pointer is copyable or not. --- assetFactory/assetFactory.h | 4 ++-- assetFactory/faceController.h | 2 +- assetFactory/factoryMesh.h | 2 +- assetFactory/object.h | 2 +- assetFactory/use.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index a68b460..42082eb 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -14,7 +14,7 @@ public: FactoryMesh::Collection meshes; private: - friend Persistence::SelectionPtrBase, true>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; @@ -36,6 +36,6 @@ public: static void normalizeColourName(std::string &); private: - friend Persistence::SelectionPtrBase, true>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/faceController.h b/assetFactory/faceController.h index 0618388..10a226a 100644 --- a/assetFactory/faceController.h +++ b/assetFactory/faceController.h @@ -21,7 +21,7 @@ public: FaceControllers faceControllers; private: - friend Persistence::SelectionPtrBase, false>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; std::string getId() const override diff --git a/assetFactory/factoryMesh.h b/assetFactory/factoryMesh.h index 4b6d3e5..bbeb870 100644 --- a/assetFactory/factoryMesh.h +++ b/assetFactory/factoryMesh.h @@ -13,6 +13,6 @@ public: Use::Collection uses; private: - friend Persistence::SelectionPtrBase, true>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; diff --git a/assetFactory/object.h b/assetFactory/object.h index 1069f66..f3726c7 100644 --- a/assetFactory/object.h +++ b/assetFactory/object.h @@ -16,7 +16,7 @@ public: std::string id; private: - friend Persistence::SelectionPtrBase, true>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; std::string getId() const override diff --git a/assetFactory/use.h b/assetFactory/use.h index 5b61eca..5e4c35f 100644 --- a/assetFactory/use.h +++ b/assetFactory/use.h @@ -17,7 +17,7 @@ public: FaceControllers faceControllers; private: - friend Persistence::SelectionPtrBase, true>; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; std::string getId() const override -- cgit v1.2.3 From 98df33e0a52e086b68df2a62ce2f41cc6b67db63 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:18:38 +0000 Subject: Parse colour values as they're read --- assetFactory/assetFactory.cpp | 32 ++++++++++++++++-- assetFactory/assetFactory.h | 4 ++- assetFactory/modelFactoryMesh.h | 3 +- assetFactory/style.cpp | 72 ++++++++++++++--------------------------- assetFactory/style.h | 18 +++++++++-- assetFactory/use.cpp | 3 +- 6 files changed, 76 insertions(+), 56 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 016f29e..70f5337 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -30,12 +30,12 @@ AssetFactory::parseX11RGB(const char * path) { filesystem::FileStar rgb {path, "r"}; Colours out; - glm::u8vec3 colour; + Colour colour; char inname[BUFSIZ]; - while (fscanf(rgb, "%hhu %hhu %hhu %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { + while (fscanf(rgb, "%f %f %f %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) { std::string name {inname}; normalizeColourName(name); - out.emplace(std::move(name), colour); + out.emplace(std::move(name), colour / 255.f); } return out; } @@ -49,6 +49,32 @@ AssetFactory::normalizeColourName(std::string & name) }; } +AssetFactory::ColourAlpha +AssetFactory::parseColour(std::string_view in) const +{ + if (in.empty()) { + throw std::runtime_error("Empty colour specification"); + } + if (in[0] == '#') { + if (in.length() > 9 || in.length() % 2 == 0) { + throw std::runtime_error("Invalid hex colour specification"); + } + ColourAlpha out {0, 0, 0, 1}; + std::generate_n(&out.r, (in.length() - 1) / 2, [in = in.data() + 1]() mutable { + uint8_t channel; + std::from_chars(in, in + 2, channel, 16); + in += 2; + return static_cast(channel) / 255.f; + }); + return out; + } + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { + return {colour->second, 1}; + } + } + throw std::runtime_error("No such asset factory colour"); +} bool AssetFactory::persist(Persistence::PersistenceStore & store) { diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 42082eb..2bd576b 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -22,11 +22,13 @@ class AssetFactory : public Persistence::Persistable { public: using Shapes = std::map>; using Assets = std::map>; - using Colour = glm::u8vec3; + using Colour = glm::vec3; + using ColourAlpha = glm::vec4; using Colours = std::map>; AssetFactory(); [[nodiscard]] static std::shared_ptr loadXML(const std::filesystem::path &); + [[nodiscard]] ColourAlpha parseColour(std::string_view) const; Shapes shapes; Assets assets; diff --git a/assetFactory/modelFactoryMesh.h b/assetFactory/modelFactoryMesh.h index b4f5254..258913b 100644 --- a/assetFactory/modelFactoryMesh.h +++ b/assetFactory/modelFactoryMesh.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace OpenMesh { template struct glmvec : public VectorT { @@ -25,7 +26,7 @@ struct ModelFactoryTraits : public OpenMesh::DefaultTraits { VertexAttributes(OpenMesh::Attributes::Normal | OpenMesh::Attributes::Status); using Point = OpenMesh::glmvec; using Normal = OpenMesh::glmvec; - using Color = OpenMesh::glmvec; + using Color = glm::vec4; }; struct ModelFactoryMesh : public OpenMesh::PolyMesh_ArrayKernelT { diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index d2977a7..b2a2cf7 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -1,41 +1,16 @@ #include "style.h" #include "assetFactory.h" -ModelFactoryMesh::Color -Style::parseColour(const std::string_view & in) -{ - if (in.empty()) { - throw std::runtime_error("Empty colour specification"); - } - if (in[0] == '#') { - if (in.length() > 9 || in.length() % 2 == 0) { - throw std::runtime_error("Invalid hex colour specification"); - } - ModelFactoryMesh::Color out {0, 0, 0, 1}; - std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { - uint8_t channel; - std::from_chars(in, in + 2, channel, 16); - in += 2; - return static_cast(channel) / 256.F; - }); - return out; - } - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { - if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { - const auto out = glm::vec3 {colour->second} / 256.F; - return {out.r, out.g, out.b, 1.f}; - } - } - throw std::runtime_error("No such asset factory colour"); -} - void Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Shape::CreatedFaces & faces) const { - if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { - const auto parsedColour = parseColour(effectiveColour); + if (const auto effectiveColour = getProperty(parents, &Style::colour, + [](auto && style) { + return style->colour.a > 0; + }); + effectiveColour.has_value()) { for (const auto & face : faces) { - mesh.set_color(face.second, parsedColour); + mesh.set_color(face.second, effectiveColour->get()); } } } @@ -43,27 +18,30 @@ Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Sha void Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const ModelFactoryMesh::FaceHandle & face) const { - if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { - const auto parsedColour = parseColour(effectiveColour); - mesh.set_color(face, parsedColour); - } -} - -std::string_view -Style::getProperty(const StyleStack & parents, std::string Style::*member) -{ - if (const auto itr = std::find_if(parents.rbegin(), parents.rend(), - [&member](auto && s) { - return !(s->*member).empty(); + if (const auto effectiveColour = getProperty(parents, &Style::colour, + [](auto && style) { + return style->colour.a > 0; }); - itr != parents.rend()) { - return (*itr)->*member; + effectiveColour.has_value()) { + mesh.set_color(face, effectiveColour->get()); } - return {}; } bool Style::persist(Persistence::PersistenceStore & store) { - return STORE_MEMBER(colour); + struct ColourParser : public Persistence::SelectionV { + using Persistence::SelectionV::SelectionV; + using Persistence::SelectionV::setValue; + void + setValue(std::string && str) override + { + if (auto mf + = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + v = mf->parseColour(str); + } + } + }; + + return STORE_HELPER(colour, ColourParser); } diff --git a/assetFactory/style.h b/assetFactory/style.h index 0c7ad5a..e8fd012 100644 --- a/assetFactory/style.h +++ b/assetFactory/style.h @@ -3,19 +3,31 @@ #include "modelFactoryMesh.h" #include "persistence.h" #include "shape.h" +#include #include +#include class Style { public: using StyleStack = std::vector; + using Colour = glm::vec3; + using ColourAlpha = glm::vec4; - static ModelFactoryMesh::Color parseColour(const std::string_view &); void applyStyle(ModelFactoryMesh &, const StyleStack & parents, const Shape::CreatedFaces &) const; void applyStyle(ModelFactoryMesh &, const StyleStack & parents, const ModelFactoryMesh::FaceHandle &) const; - static std::string_view getProperty(const StyleStack & parents, std::string Style::*member); + template + static std::optional> + getProperty(const StyleStack & parents, T Style::*member, auto && test) + { + if (const auto itr = std::find_if(parents.rbegin(), parents.rend(), std::forward(test)); + itr != parents.rend()) { + return (*itr)->*member; + } + return {}; + } - std::string colour; + ColourAlpha colour {}; protected: bool persist(Persistence::PersistenceStore & store); diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index a6fac5c..53fc9b9 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -18,7 +18,8 @@ struct Lookup : public Persistence::SelectionV { void setValue(std::string && str) override { - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = std::dynamic_pointer_cast( + Persistence::sharedObjects.at("assetFactory"))) { v = mf->shapes.at(str); } } -- cgit v1.2.3 From 17ac090dd1dd245cf1e24b62b7333ba9be571bde Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:36:34 +0000 Subject: Add ParseBase Acts as a base class for persistence parser, encompasses the parse stack and manages shared objects --- assetFactory/assetFactory.cpp | 2 +- assetFactory/style.cpp | 3 +-- assetFactory/use.cpp | 3 +-- 3 files changed, 3 insertions(+), 5 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index 70f5337..b9724c9 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -68,7 +68,7 @@ AssetFactory::parseColour(std::string_view in) const }); return out; } - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { return {colour->second, 1}; } diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index b2a2cf7..fc5c34e 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -36,8 +36,7 @@ Style::persist(Persistence::PersistenceStore & store) void setValue(std::string && str) override { - if (auto mf - = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { v = mf->parseColour(str); } } diff --git a/assetFactory/use.cpp b/assetFactory/use.cpp index 53fc9b9..708e310 100644 --- a/assetFactory/use.cpp +++ b/assetFactory/use.cpp @@ -18,8 +18,7 @@ struct Lookup : public Persistence::SelectionV { void setValue(std::string && str) override { - if (auto mf = std::dynamic_pointer_cast( - Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { v = mf->shapes.at(str); } } -- cgit v1.2.3 From 5b7f5f2de723d30d3461f653e90fa4ae95941786 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 3 Mar 2023 19:52:55 +0000 Subject: Split Asset into its own file --- assetFactory/asset.cpp | 8 ++++++++ assetFactory/asset.h | 17 +++++++++++++++++ assetFactory/assetFactory.cpp | 7 ------- assetFactory/assetFactory.h | 15 +-------------- 4 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 assetFactory/asset.cpp create mode 100644 assetFactory/asset.h (limited to 'assetFactory') diff --git a/assetFactory/asset.cpp b/assetFactory/asset.cpp new file mode 100644 index 0000000..fa30a78 --- /dev/null +++ b/assetFactory/asset.cpp @@ -0,0 +1,8 @@ +#include "asset.h" + +bool +Asset::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) + && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); +} diff --git a/assetFactory/asset.h b/assetFactory/asset.h new file mode 100644 index 0000000..d86b804 --- /dev/null +++ b/assetFactory/asset.h @@ -0,0 +1,17 @@ +#pragma once + +#include "factoryMesh.h" +#include "persistence.h" +#include + +class Asset : public Persistence::Persistable, public StdTypeDefs { +public: + std::string id; + std::string name; + + FactoryMesh::Collection meshes; + +protected: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; +}; diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp index b9724c9..f5fc2b3 100644 --- a/assetFactory/assetFactory.cpp +++ b/assetFactory/assetFactory.cpp @@ -83,10 +83,3 @@ AssetFactory::persist(Persistence::PersistenceStore & store) return STORE_TYPE && STORE_NAME_HELPER("object", shapes, MapObjects) && STORE_NAME_HELPER("asset", assets, MapAssets); } - -bool -Asset::persist(Persistence::PersistenceStore & store) -{ - return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) - && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); -} diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h index 2bd576b..b47d408 100644 --- a/assetFactory/assetFactory.h +++ b/assetFactory/assetFactory.h @@ -1,22 +1,9 @@ #pragma once -#include "factoryMesh.h" +#include "asset.h" #include "persistence.h" #include "shape.h" #include -#include - -class Asset : public Persistence::Persistable, public StdTypeDefs { -public: - std::string id; - std::string name; - - FactoryMesh::Collection meshes; - -private: - friend Persistence::SelectionPtrBase>; - bool persist(Persistence::PersistenceStore & store) override; -}; class AssetFactory : public Persistence::Persistable { public: -- cgit v1.2.3 From 29ce7770c3e8373d2efea0b47fe50a13a81e75a2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Mar 2023 14:17:00 +0000 Subject: Add Asset helpers to construct OpenGL ready meshes from Asset Factory meshes during load --- assetFactory/asset.cpp | 29 ++++++++++++++++++++++++++++- assetFactory/asset.h | 22 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'assetFactory') diff --git a/assetFactory/asset.cpp b/assetFactory/asset.cpp index fa30a78..659a950 100644 --- a/assetFactory/asset.cpp +++ b/assetFactory/asset.cpp @@ -3,6 +3,33 @@ bool Asset::persist(Persistence::PersistenceStore & store) { - return STORE_TYPE && STORE_MEMBER(id) && STORE_MEMBER(name) + return STORE_MEMBER(id) && STORE_MEMBER(name) && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); } + +Asset::MeshConstruct::MeshConstruct(Mesh::Ptr & m) : + Persistence::SelectionPtrBase {fmesh}, out {m} { } + +void +Asset::MeshConstruct::endObject(Persistence::Stack & stk) +{ + out = fmesh->createMesh(); + Persistence::SelectionPtrBase::endObject(stk); +} + +Asset::MeshArrayConstruct::MeshArrayConstruct(std::span m) : + Persistence::SelectionPtrBase {fmesh}, out {m} +{ +} + +void +Asset::MeshArrayConstruct::endObject(Persistence::Stack & stk) +{ + for (auto & outMesh : out) { + if (!outMesh) { + outMesh = fmesh->createMesh(); + break; + } + } + Persistence::SelectionPtrBase::endObject(stk); +} diff --git a/assetFactory/asset.h b/assetFactory/asset.h index d86b804..8dd1ecb 100644 --- a/assetFactory/asset.h +++ b/assetFactory/asset.h @@ -12,6 +12,28 @@ public: FactoryMesh::Collection meshes; protected: + struct MeshConstruct : public Persistence::SelectionPtrBase { + using Persistence::SelectionPtrBase::setValue; + + MeshConstruct(Mesh::Ptr & m); + + void endObject(Persistence::Stack & stk) override; + + FactoryMesh::Ptr fmesh; + Mesh::Ptr & out; + }; + + struct MeshArrayConstruct : public Persistence::SelectionPtrBase { + using Persistence::SelectionPtrBase::setValue; + + MeshArrayConstruct(std::span m); + + void endObject(Persistence::Stack & stk) override; + + FactoryMesh::Ptr fmesh; + std::span out; + }; + friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; -- cgit v1.2.3 From 54ddcd3da0916b6a46f5fcf3f33ff0fbffe375ce Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 4 Mar 2023 14:27:23 +0000 Subject: Support and load factory asset directly into a RailVehicleClass instance --- assetFactory/asset.cpp | 3 +-- assetFactory/asset.h | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'assetFactory') diff --git a/assetFactory/asset.cpp b/assetFactory/asset.cpp index 659a950..3ab2f1c 100644 --- a/assetFactory/asset.cpp +++ b/assetFactory/asset.cpp @@ -3,8 +3,7 @@ bool Asset::persist(Persistence::PersistenceStore & store) { - return STORE_MEMBER(id) && STORE_MEMBER(name) - && STORE_NAME_HELPER("mesh", meshes, Persistence::Appender); + return STORE_MEMBER(id) && STORE_MEMBER(name); } Asset::MeshConstruct::MeshConstruct(Mesh::Ptr & m) : diff --git a/assetFactory/asset.h b/assetFactory/asset.h index 8dd1ecb..e3318e4 100644 --- a/assetFactory/asset.h +++ b/assetFactory/asset.h @@ -9,8 +9,6 @@ public: std::string id; std::string name; - FactoryMesh::Collection meshes; - protected: struct MeshConstruct : public Persistence::SelectionPtrBase { using Persistence::SelectionPtrBase::setValue; -- cgit v1.2.3