feat: add obj reader

This commit is contained in:
parker
2025-08-01 16:04:35 +01:00
parent ab6b5ac935
commit 92bfc8ff26
9 changed files with 176 additions and 19 deletions

View File

@@ -60,7 +60,6 @@ enzo::nt::NetworkManager& enzo::nt::NetworkManager::getInstance()
enzo::nt::GeometryOperator& enzo::nt::NetworkManager::getGeoOperator(nt::OpId opId) enzo::nt::GeometryOperator& enzo::nt::NetworkManager::getGeoOperator(nt::OpId opId)
{ {
std::cout << "gop size middle getter: " << gopStore_.size() <<"\n"; // <- size 0
auto it = gopStore_.find(opId); auto it = gopStore_.find(opId);
if(it == gopStore_.end()) if(it == gopStore_.end())
{ {

View File

@@ -1,4 +1,5 @@
#pragma once #pragma once
#include <oneapi/tbb/concurrent_vector.h>
#include <string> #include <string>
#include <optional> #include <optional>
#include <string_view> #include <string_view>
@@ -9,6 +10,8 @@
namespace enzo{ namespace enzo{
namespace ga{ namespace ga{
template <typename T>
using StoreContainer = std::vector<T>;
template <typename T> template <typename T>
class AttributeHandle; class AttributeHandle;
@@ -41,11 +44,10 @@ namespace enzo{
void* data_; void* data_;
// data stores // data stores
std::shared_ptr<std::vector<int>> intStore_; std::shared_ptr<StoreContainer<int>> intStore_;
std::shared_ptr<std::vector<float>> floatStore_; std::shared_ptr<StoreContainer<float>> floatStore_;
std::shared_ptr<std::vector<enzo::bt::Vector3>> vector3Store_; std::shared_ptr<StoreContainer<enzo::bt::Vector3>> vector3Store_;
}; };

View File

@@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include "Engine/Operator/Attribute.h" #include "Engine/Operator/Attribute.h"
#include "Engine/Types.h" #include "Engine/Types.h"
#include "tbb/concurrent_vector.h"
#include <iostream> #include <iostream>
@@ -54,9 +55,10 @@ public:
data_->push_back(value); data_->push_back(value);
} }
// TODO: replace with iterator
std::vector<T> getAllValues() const std::vector<T> getAllValues() const
{ {
return *data_; return {data_->begin(), data_->end()};
} }
size_t getSize() size_t getSize()
@@ -96,7 +98,7 @@ private:
std::string name_=""; std::string name_="";
std::shared_ptr<std::vector<T>> data_; std::shared_ptr<StoreContainer<T>> data_;
// int typeID_; // int typeID_;

View File

@@ -59,19 +59,19 @@ void GLMesh::setPosBuffer(enzo::geo::Geometry& geometry)
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
vertices.clear(); vertices.clear();
enzo::geo::HeMesh heMesh = geometry.computeHalfEdgeMesh(); // enzo::geo::HeMesh heMesh = geometry.computeHalfEdgeMesh();
// compute mesh normals // // compute mesh normals
auto vnormals = heMesh.add_property_map<enzo::geo::V_index, enzo::geo::Vector>("v:normals", CGAL::NULL_VECTOR).first; // auto vnormals = heMesh.add_property_map<enzo::geo::V_index, enzo::geo::Vector>("v:normals", CGAL::NULL_VECTOR).first;
auto fnormals = heMesh.add_property_map<enzo::geo::F_index, enzo::geo::Vector>("f:normals", CGAL::NULL_VECTOR).first; // auto fnormals = heMesh.add_property_map<enzo::geo::F_index, enzo::geo::Vector>("f:normals", CGAL::NULL_VECTOR).first;
namespace PMP = CGAL::Polygon_mesh_processing; // namespace PMP = CGAL::Polygon_mesh_processing;
PMP::compute_normals( // PMP::compute_normals(
heMesh, // heMesh,
vnormals, // vnormals,
fnormals, // fnormals,
PMP::parameters::vertex_point_map(heMesh.points()) // PMP::parameters::vertex_point_map(heMesh.points())
); // );
std::shared_ptr<enzo::ga::Attribute> PAttr = geometry.getAttribByName(enzo::ga::AttrOwner::POINT, "P"); std::shared_ptr<enzo::ga::Attribute> PAttr = geometry.getAttribByName(enzo::ga::AttrOwner::POINT, "P");
enzo::ga::AttributeHandleVector3 PAttrHandle = enzo::ga::AttributeHandleVector3(PAttr); enzo::ga::AttributeHandleVector3 PAttrHandle = enzo::ga::AttributeHandleVector3(PAttr);

View File

@@ -22,6 +22,7 @@ add_library(${libName} SHARED
GopTransform.cpp GopTransform.cpp
GopHouse.cpp GopHouse.cpp
GopTestCube.cpp GopTestCube.cpp
GopGeometryImport.cpp
) )
target_link_libraries(${libName} PRIVATE Qt6::Core Qt6::Widgets Qt6::SvgWidgets Qt6::OpenGLWidgets glm::glm Eigen3::Eigen TBB::tbb) target_link_libraries(${libName} PRIVATE Qt6::Core Qt6::Widgets Qt6::SvgWidgets Qt6::OpenGLWidgets glm::glm Eigen3::Eigen TBB::tbb)

View File

@@ -0,0 +1,123 @@
#include "OpDefs/GopGeometryImport.h"
#include "Engine/Operator/AttributeHandle.h"
#include "Engine/Types.h"
#include <cstdio>
#include <oneapi/tbb/parallel_for.h>
#include <fstream>
#include <string>
#include <boost/algorithm/string/split.hpp>
GopGeometryImport::GopGeometryImport(enzo::nt::NetworkManager* network, enzo::op::OpInfo opInfo)
: GeometryOpDef(network, opInfo)
{
}
void GopGeometryImport::cookOp(enzo::op::Context context)
{
using namespace enzo;
if(outputRequested(0))
{
std::string filePath = "/home/parker/Downloads/Rat_Placeholder_Polycount_12.obj";
geo::Geometry geo;
auto PAttr = geo.getAttribByName(ga::AttrOwner::POINT, "P");
ga::AttributeHandleVector3 PAttrHandle(PAttr);
auto pointAttr = geo.getAttribByName(ga::AttrOwner::VERTEX, "point");
ga::AttributeHandleInt pointAttrHandle(pointAttr);
auto vertexCountAttr = geo.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount");
ga::AttributeHandleInt vertexCountHandle(vertexCountAttr);
std::ifstream file(filePath);
if(!file.is_open())
{
std::cerr << "Failed to open file " << filePath << "\n";
return;
}
std::string line;
while(std::getline(file, line))
{
if(line.size()==0)
{
continue;
}
char firstChar = line[0];
if(firstChar=='v')
{
std::vector<std::string> result;
boost::split(result, line, isspace);
if(result.size()!=4)
{
continue;
}
const bt::Vector3 pointPos = {std::stod(result[1]), std::stod(result[2]), std::stod(result[3])};
PAttrHandle.addValue(pointPos);
std::cout << "adding vector: " << pointPos.x() << " " << pointPos.y() << " " << pointPos.z() << "\n";
}
else if(firstChar=='f')
{
std::vector<std::string> result;
boost::split(result, line, isspace);
// if(result.size()<3)
// {
// continue;
// }
// set vertex attributes
std::cout << "connecting:";
for(int i=1; i<result.size(); ++i)
{
const int primNum = std::stoi(result[i]);
pointAttrHandle.addValue(primNum-1);
std::cout << " " << primNum;
}
std::cout << "\n";
// set face attribute
std::cout << "face size: " << result.size()-1 << "\n";
vertexCountHandle.addValue(result.size()-1);
}
}
// scale
const float scale = context.evalFloatParm("size");
for(int i=0; i<PAttrHandle.getSize(); ++i)
{
enzo::bt::Vector3 pointPos = PAttrHandle.getValue(i);
pointPos*=scale;
PAttrHandle.setValue(i, pointPos);
std::cout << "adding point: " << pointPos.x() << " " << pointPos.y() << " " << pointPos.z() << "\n";
}
for(auto value : pointAttrHandle.getAllValues())
{
std::cout << "adding vector: " << value << "\n";
}
for(auto value : vertexCountHandle.getAllValues())
{
std::cout << "adding face: " << value << "\n";
}
// ----
// set output geometry
setOutputGeometry(0, geo);
}
}
enzo::prm::Template GopGeometryImport::parameterList[] =
{
enzo::prm::Template(enzo::prm::Type::FLOAT, "size", 1),
enzo::prm::Terminator
};

View File

@@ -0,0 +1,18 @@
#pragma once
#include "Engine/Operator/GeometryOpDef.h"
#include "Engine/Parameter/Template.h"
class GopGeometryImport
: public enzo::nt::GeometryOpDef
{
public:
GopGeometryImport(enzo::nt::NetworkManager* network, enzo::op::OpInfo opInfo);
virtual void cookOp(enzo::op::Context context);
static enzo::nt::GeometryOpDef* ctor(enzo::nt::NetworkManager* network, enzo::op::OpInfo opInfo)
{
return new GopGeometryImport(network, opInfo);
}
static BOOST_SYMBOL_EXPORT enzo::prm::Template parameterList[];
};

View File

@@ -16,7 +16,7 @@ void GOP_house::cookOp(enzo::op::Context context)
if(outputRequested(0)) if(outputRequested(0))
{ {
// copy input geometry // copy input geometry
geo::Geometry geo = context.cloneInputGeo(0); geo::Geometry geo;
// ---- // ----
// create geometry start // create geometry start

View File

@@ -1,5 +1,6 @@
#include "Engine/Operator/OpInfo.h" #include "Engine/Operator/OpInfo.h"
#include "Engine/Operator/OperatorTable.h" #include "Engine/Operator/OperatorTable.h"
#include "GopGeometryImport.h"
#include "GopHouse.h" #include "GopHouse.h"
#include "GopTestCube.h" #include "GopTestCube.h"
#include "OpDefs/GopTransform.hpp" #include "OpDefs/GopTransform.hpp"
@@ -43,6 +44,17 @@ extern "C"
1, 1,
} }
); );
addOperator(
enzo::op::OpInfo {
"geometryImport",
"Geometry Import",
&GopGeometryImport::ctor,
GopGeometryImport::parameterList,
0,
0,
1,
}
);
} }
} }