feat(engine): add geometry class
This commit is contained in:
@@ -50,7 +50,7 @@ add_executable(${TestExec}
|
|||||||
tests/main-tests.cpp
|
tests/main-tests.cpp
|
||||||
tests/OperatorTests.cpp
|
tests/OperatorTests.cpp
|
||||||
src/Engine/Operator/Attribute.cpp
|
src/Engine/Operator/Attribute.cpp
|
||||||
# src/Engine/Operator/Primitive.cpp
|
src/Engine/Operator/Geometry.cpp
|
||||||
)
|
)
|
||||||
find_package(Catch2 3 REQUIRED)
|
find_package(Catch2 3 REQUIRED)
|
||||||
target_link_libraries(${TestExec} PRIVATE Catch2::Catch2WithMain)
|
target_link_libraries(${TestExec} PRIVATE Catch2::Catch2WithMain)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ ga::Attribute::Attribute(std::string name, ga::AttributeType type)
|
|||||||
floatStore_=std::make_shared<std::vector<float>>();
|
floatStore_=std::make_shared<std::vector<float>>();
|
||||||
break;
|
break;
|
||||||
default:
|
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_;
|
return type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string ga::Attribute::getName()
|
||||||
|
{
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Engine/Types.h"
|
#include "Engine/Types.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -17,6 +18,7 @@ namespace enzo{
|
|||||||
public:
|
public:
|
||||||
Attribute(std::string name, ga::AttributeType type);
|
Attribute(std::string name, ga::AttributeType type);
|
||||||
AttributeType getType();
|
AttributeType getType();
|
||||||
|
std::string getName();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -16,40 +17,24 @@ class AttributeHandle
|
|||||||
public:
|
public:
|
||||||
ga::AttributeType type_;
|
ga::AttributeType type_;
|
||||||
|
|
||||||
AttributeHandle(Attribute& attribute)
|
AttributeHandle(std::shared_ptr<Attribute> attribute)
|
||||||
{
|
{
|
||||||
type_ = attribute.getType();
|
type_ = attribute->getType();
|
||||||
// get attribute data pointer
|
// get attribute data pointer
|
||||||
// TODO: check types match
|
// TODO: check types match
|
||||||
// TODO: add the other types
|
// TODO: add the other types
|
||||||
if constexpr (std::is_same<int, T>::value)
|
if constexpr (std::is_same<int, T>::value)
|
||||||
{
|
{
|
||||||
data_=attribute.intStore_;
|
data_=attribute->intStore_;
|
||||||
}
|
}
|
||||||
else if constexpr (std::is_same<float, T>::value)
|
else if constexpr (std::is_same<float, T>::value)
|
||||||
{
|
{
|
||||||
data_=attribute.floatStore_;
|
data_=attribute->floatStore_;
|
||||||
}
|
}
|
||||||
else
|
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)
|
void addValue(T value)
|
||||||
|
|||||||
55
src/Engine/Operator/Geometry.cpp
Normal file
55
src/Engine/Operator/Geometry.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
22
src/Engine/Operator/Geometry.h
Normal file
22
src/Engine/Operator/Geometry.h
Normal 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_;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <memory>
|
||||||
#include "Engine/Operator/Attribute.h"
|
#include "Engine/Operator/Attribute.h"
|
||||||
#include "Engine/Operator/AttributeHandle.h"
|
#include "Engine/Operator/AttributeHandle.h"
|
||||||
#include "Engine/Types.h"
|
#include "Engine/Types.h"
|
||||||
|
#include "Engine/Operator/Geometry.h"
|
||||||
// #include "Engine/Operator/Primitive.h"
|
// #include "Engine/Operator/Primitive.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -9,7 +11,7 @@ TEST_CASE("attrHandleInt")
|
|||||||
{
|
{
|
||||||
using namespace enzo;
|
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);
|
ga::AttributeHandleInt myHandle(myAttrib);
|
||||||
myHandle.addValue(5);
|
myHandle.addValue(5);
|
||||||
myHandle.addValue(6);
|
myHandle.addValue(6);
|
||||||
@@ -17,11 +19,34 @@ TEST_CASE("attrHandleInt")
|
|||||||
REQUIRE(myHandle.getValue(1)==6);
|
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")
|
TEST_CASE("attrHandleFloat")
|
||||||
{
|
{
|
||||||
using namespace enzo;
|
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);
|
ga::AttributeHandleFloat myHandle(myAttrib);
|
||||||
myHandle.addValue(5.3f);
|
myHandle.addValue(5.3f);
|
||||||
myHandle.addValue(6.9f);
|
myHandle.addValue(6.9f);
|
||||||
|
|||||||
Reference in New Issue
Block a user