diff --git a/CMakeLists.txt b/CMakeLists.txt index 662b66b..be89900 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/Engine/Operator/Attribute.cpp b/src/Engine/Operator/Attribute.cpp index 5217b16..a074f56 100644 --- a/src/Engine/Operator/Attribute.cpp +++ b/src/Engine/Operator/Attribute.cpp @@ -20,7 +20,7 @@ ga::Attribute::Attribute(std::string name, ga::AttributeType type) floatStore_=std::make_shared>(); break; default: - throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not accounted for"); + throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for"); } @@ -31,4 +31,8 @@ ga::AttributeType ga::Attribute::getType() return type_; } +std::string ga::Attribute::getName() +{ + return name_; +} diff --git a/src/Engine/Operator/Attribute.h b/src/Engine/Operator/Attribute.h index 0601a2e..2820bc1 100644 --- a/src/Engine/Operator/Attribute.h +++ b/src/Engine/Operator/Attribute.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include "Engine/Types.h" #include @@ -17,6 +18,7 @@ namespace enzo{ public: Attribute(std::string name, ga::AttributeType type); AttributeType getType(); + std::string getName(); diff --git a/src/Engine/Operator/AttributeHandle.h b/src/Engine/Operator/AttributeHandle.h index f62932f..257bdfd 100644 --- a/src/Engine/Operator/AttributeHandle.h +++ b/src/Engine/Operator/AttributeHandle.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -16,40 +17,24 @@ class AttributeHandle public: ga::AttributeType type_; - AttributeHandle(Attribute& attribute) + AttributeHandle(std::shared_ptr 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::value) { - data_=attribute.intStore_; + data_=attribute->intStore_; } else if constexpr (std::is_same::value) { - data_=attribute.floatStore_; + data_=attribute->floatStore_; } else { - throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not accounted for"); + throw std::runtime_error("Type " + std::to_string(static_cast(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(type_)) + " was not accounted for"); - - // } } void addValue(T value) diff --git a/src/Engine/Operator/Geometry.cpp b/src/Engine/Operator/Geometry.cpp new file mode 100644 index 0000000..6739fd1 --- /dev/null +++ b/src/Engine/Operator/Geometry.cpp @@ -0,0 +1,55 @@ +#include "Engine/Operator/Geometry.h" +#include "Engine/Operator/Attribute.h" +#include "Engine/Operator/AttributeHandle.h" +#include "Engine/Types.h" +#include +#include + +using namespace enzo; +geo::Geometry::Geometry() +{ + +} + +ga::AttributeHandle geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name) +{ + auto newAttribute = std::make_shared(name, ga::AttrType::intT); + getOwnerVector(owner).push_back(newAttribute); + return ga::AttributeHandle(newAttribute); +} + +std::vector>& 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 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; +} + diff --git a/src/Engine/Operator/Geometry.h b/src/Engine/Operator/Geometry.h new file mode 100644 index 0000000..0c979a2 --- /dev/null +++ b/src/Engine/Operator/Geometry.h @@ -0,0 +1,22 @@ +#pragma once +#include "Engine/Operator/Attribute.h" +#include "Engine/Types.h" +#include + +namespace enzo::geo +{ +class Geometry +{ +public: + Geometry(); + ga::AttributeHandle addIntAttribute(ga::AttributeOwner owner, std::string name); + std::shared_ptr getAttribByName(ga::AttributeOwner owner, std::string name); +private: + using attribVector = std::vector>; + attribVector& getOwnerVector(ga::AttributeOwner& owner); + attribVector pointAttributes_; + attribVector vertexAttributes_; + attribVector primitiveAttributes_; + attribVector globalAttributes_; +}; +} diff --git a/src/Engine/Operator/Primitive.cpp b/src/Engine/Operator/Primitive.cpp deleted file mode 100644 index c67b1d3..0000000 --- a/src/Engine/Operator/Primitive.cpp +++ /dev/null @@ -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; -} diff --git a/src/Engine/Operator/Primitive.h b/src/Engine/Operator/Primitive.h deleted file mode 100644 index 2c04a1f..0000000 --- a/src/Engine/Operator/Primitive.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Engine/Operator/Attribute.h" -#include "Engine/Types.h" -#include - -namespace enzo -{ -class Primitive -{ -public: - Primitive(); - bool addIntAttrib(AttributeOwner owner, AttributeInt attribute); - AttributeInt findAttribByName(AttributeOwner owner, std::string name); -private: - std::vector> pointAttribs_; -}; -} diff --git a/tests/OperatorTests.cpp b/tests/OperatorTests.cpp index 3193f32..170cf25 100644 --- a/tests/OperatorTests.cpp +++ b/tests/OperatorTests.cpp @@ -1,7 +1,9 @@ #include +#include #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("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 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 myAttribute = geo.getAttribByName(ga::AttrOwner::POINT, "index"); + ga::AttributeHandle myHandle2(myAttribute); + REQUIRE(myHandle2.getValue(0)==5); + REQUIRE(myHandle2.getValue(1)==6); + + // check failed lookup + std::shared_ptr 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 myAttrib = std::make_shared("test", ga::AttrType::floatT); ga::AttributeHandleFloat myHandle(myAttrib); myHandle.addValue(5.3f); myHandle.addValue(6.9f);