feat(engine): add geometry class

This commit is contained in:
parker
2025-06-29 20:49:30 +01:00
parent deb237e481
commit 7db66aedeb
9 changed files with 118 additions and 69 deletions

View File

@@ -50,7 +50,7 @@ add_executable(${TestExec}
tests/main-tests.cpp
tests/OperatorTests.cpp
src/Engine/Operator/Attribute.cpp
# src/Engine/Operator/Primitive.cpp
src/Engine/Operator/Geometry.cpp
)
find_package(Catch2 3 REQUIRED)
target_link_libraries(${TestExec} PRIVATE Catch2::Catch2WithMain)

View File

@@ -20,7 +20,7 @@ ga::Attribute::Attribute(std::string name, ga::AttributeType type)
floatStore_=std::make_shared<std::vector<float>>();
break;
default:
throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not accounted for");
throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for");
}
@@ -31,4 +31,8 @@ ga::AttributeType ga::Attribute::getType()
return type_;
}
std::string ga::Attribute::getName()
{
return name_;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <optional>
#include <string_view>
#include <vector>
#include "Engine/Types.h"
#include <memory>
@@ -17,6 +18,7 @@ namespace enzo{
public:
Attribute(std::string name, ga::AttributeType type);
AttributeType getType();
std::string getName();

View File

@@ -1,4 +1,5 @@
#pragma once
#include <memory>
#include <stdexcept>
#include <string>
#include <optional>
@@ -16,40 +17,24 @@ class AttributeHandle
public:
ga::AttributeType type_;
AttributeHandle(Attribute& attribute)
AttributeHandle(std::shared_ptr<Attribute> attribute)
{
type_ = attribute.getType();
type_ = attribute->getType();
// get attribute data pointer
// TODO: check types match
// TODO: add the other types
if constexpr (std::is_same<int, T>::value)
{
data_=attribute.intStore_;
data_=attribute->intStore_;
}
else if constexpr (std::is_same<float, T>::value)
{
data_=attribute.floatStore_;
data_=attribute->floatStore_;
}
else
{
throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not accounted for");
throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not properly accounted for");
}
// switch(type_)
// {
// case(AttrType::intT):
// {
// data_=attribute.intStore_;
// break;
// }
// case(AttrType::floatT):
// {
// data_=attribute.floatStore_;
// break;
// }
// default:
// throw std::runtime_error("Type " + std::to_string(static_cast<int>(type_)) + " was not accounted for");
// }
}
void addValue(T value)

View File

@@ -0,0 +1,55 @@
#include "Engine/Operator/Geometry.h"
#include "Engine/Operator/Attribute.h"
#include "Engine/Operator/AttributeHandle.h"
#include "Engine/Types.h"
#include <memory>
#include <stdexcept>
using namespace enzo;
geo::Geometry::Geometry()
{
}
ga::AttributeHandle<int> geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name)
{
auto newAttribute = std::make_shared<ga::Attribute>(name, ga::AttrType::intT);
getOwnerVector(owner).push_back(newAttribute);
return ga::AttributeHandle<int>(newAttribute);
}
std::vector<std::shared_ptr<ga::Attribute>>& geo::Geometry::getOwnerVector(ga::AttributeOwner& owner)
{
switch(owner)
{
case ga::AttributeOwner::POINT:
return pointAttributes_;
break;
case ga::AttributeOwner::VERTEX:
return vertexAttributes_;
break;
case ga::AttributeOwner::PRIMITIVE:
return primitiveAttributes_;
break;
case ga::AttributeOwner::GLOBAL:
return globalAttributes_;
break;
default:
throw std::runtime_error("Unexpected, owner could not be found");
}
}
std::shared_ptr<ga::Attribute> geo::Geometry::getAttribByName(ga::AttributeOwner owner, std::string name)
{
auto& vector = getOwnerVector(owner);
for(auto it=vector.begin(); it!=vector.end(); ++it)
{
if((*it)->getName()==name)
{
return (*it);
}
}
return nullptr;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "Engine/Operator/Attribute.h"
#include "Engine/Types.h"
#include <variant>
namespace enzo::geo
{
class Geometry
{
public:
Geometry();
ga::AttributeHandle<int> addIntAttribute(ga::AttributeOwner owner, std::string name);
std::shared_ptr<ga::Attribute> getAttribByName(ga::AttributeOwner owner, std::string name);
private:
using attribVector = std::vector<std::shared_ptr<ga::Attribute>>;
attribVector& getOwnerVector(ga::AttributeOwner& owner);
attribVector pointAttributes_;
attribVector vertexAttributes_;
attribVector primitiveAttributes_;
attribVector globalAttributes_;
};
}

View File

@@ -1,24 +0,0 @@
#include "Engine/Operator/Primitive.h"
#include "Engine/Operator/Attribute.h"
#include "Engine/Types.h"
enzo::Primitive::Primitive()
{
}
bool enzo::Primitive::addIntAttrib(AttributeOwner owner, AttributeInt attribute)
{
bool status = true;
switch(owner)
{
case enzo::AttributeOwner::POINT:
pointAttribs_.push_back(attribute);
break;
}
pointAttribs_.push_back(attribute);
return status;
}

View File

@@ -1,20 +0,0 @@
#pragma once
#include "Engine/Operator/Attribute.h"
#include "Engine/Types.h"
#include <variant>
namespace enzo
{
class Primitive
{
public:
Primitive();
bool addIntAttrib(AttributeOwner owner, AttributeInt attribute);
AttributeInt findAttribByName(AttributeOwner owner, std::string name);
private:
std::vector<std::variant<
AttributeInt,
AttributeFloat
>> pointAttribs_;
};
}

View File

@@ -1,7 +1,9 @@
#include <catch2/catch_test_macros.hpp>
#include <memory>
#include "Engine/Operator/Attribute.h"
#include "Engine/Operator/AttributeHandle.h"
#include "Engine/Types.h"
#include "Engine/Operator/Geometry.h"
// #include "Engine/Operator/Primitive.h"
@@ -9,7 +11,7 @@ TEST_CASE("attrHandleInt")
{
using namespace enzo;
ga::Attribute myAttrib("test", ga::AttrType::intT);
auto myAttrib = std::make_shared<ga::Attribute>("test", ga::AttrType::intT);
ga::AttributeHandleInt myHandle(myAttrib);
myHandle.addValue(5);
myHandle.addValue(6);
@@ -17,11 +19,34 @@ TEST_CASE("attrHandleInt")
REQUIRE(myHandle.getValue(1)==6);
}
TEST_CASE("geometry")
{
using namespace enzo;
geo::Geometry geo;
// check add function
ga::AttributeHandle<int> myHandle = geo.addIntAttribute(ga::AttrOwner::POINT, "index");
myHandle.addValue(5);
myHandle.addValue(6);
REQUIRE(myHandle.getValue(0)==5);
REQUIRE(myHandle.getValue(1)==6);
// check getter
std::shared_ptr<ga::Attribute> myAttribute = geo.getAttribByName(ga::AttrOwner::POINT, "index");
ga::AttributeHandle<int> myHandle2(myAttribute);
REQUIRE(myHandle2.getValue(0)==5);
REQUIRE(myHandle2.getValue(1)==6);
// check failed lookup
std::shared_ptr<ga::Attribute> myAttribute2 = geo.getAttribByName(ga::AttrOwner::POINT, "nonExistant");
REQUIRE(myAttribute2==nullptr);
}
TEST_CASE("attrHandleFloat")
{
using namespace enzo;
ga::Attribute myAttrib("test", ga::AttrType::floatT);
std::shared_ptr<ga::Attribute> myAttrib = std::make_shared<ga::Attribute>("test", ga::AttrType::floatT);
ga::AttributeHandleFloat myHandle(myAttrib);
myHandle.addValue(5.3f);
myHandle.addValue(6.9f);