diff --git a/src/Engine/Operator/Attribute.cpp b/src/Engine/Operator/Attribute.cpp index 048651f..637aa72 100644 --- a/src/Engine/Operator/Attribute.cpp +++ b/src/Engine/Operator/Attribute.cpp @@ -14,16 +14,19 @@ ga::Attribute::Attribute(std::string name, ga::AttributeType type) switch(type_) { case(AttrType::intT): - intStore_=std::make_shared>(); + intStore_=std::make_shared>(); break; case(AttrType::floatT): - floatStore_=std::make_shared>(); + floatStore_=std::make_shared>(); break; case(AttrType::vectorT): vector3Store_=std::make_shared>(); break; + case(AttrType::boolT): + boolStore_=std::make_shared>(); + break; default: - throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for"); + throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for in Attribute constructor"); } @@ -40,16 +43,19 @@ ga::Attribute::Attribute(const Attribute& other) switch(type_) { case(AttrType::intT): - intStore_=std::make_shared>(*other.intStore_); + intStore_=std::make_shared>(*other.intStore_); break; case(AttrType::floatT): - floatStore_=std::make_shared>(*other.floatStore_); + floatStore_=std::make_shared>(*other.floatStore_); break; case(AttrType::vectorT): vector3Store_=std::make_shared>(*other.vector3Store_); break; + case(AttrType::boolT): + boolStore_=std::make_shared>(*other.boolStore_); + break; default: - throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for"); + throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for in Attribute copy constructor"); } } diff --git a/src/Engine/Operator/Attribute.h b/src/Engine/Operator/Attribute.h index 365687e..8f4c6ad 100644 --- a/src/Engine/Operator/Attribute.h +++ b/src/Engine/Operator/Attribute.h @@ -46,9 +46,10 @@ namespace enzo{ // void* data_; // data stores - std::shared_ptr> intStore_; - std::shared_ptr> floatStore_; + std::shared_ptr> intStore_; + std::shared_ptr> floatStore_; std::shared_ptr> vector3Store_; + std::shared_ptr> boolStore_; }; diff --git a/src/Engine/Operator/AttributeHandle.h b/src/Engine/Operator/AttributeHandle.h index e3e0a49..0178409 100644 --- a/src/Engine/Operator/AttributeHandle.h +++ b/src/Engine/Operator/AttributeHandle.h @@ -27,13 +27,13 @@ public: // TODO: add the other types // int - if constexpr (std::is_same::value) + if constexpr (std::is_same::value) { data_=attribute->intStore_; } // float - else if constexpr (std::is_same::value) + else if constexpr (std::is_same::value) { data_=attribute->floatStore_; } @@ -43,9 +43,13 @@ public: { data_=attribute->vector3Store_; } + else if constexpr (std::is_same::value) + { + data_=attribute->boolStore_; + } else { - throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for"); + throw std::runtime_error("Type " + std::to_string(static_cast(type_)) + " was not properly accounted for in AttributeHandle constructor"); } } @@ -109,8 +113,9 @@ private: }; -using AttributeHandleInt = AttributeHandle; -using AttributeHandleFloat = AttributeHandle; +using AttributeHandleInt = AttributeHandle; +using AttributeHandleFloat = AttributeHandle; using AttributeHandleVector3 = AttributeHandle; +using AttributeHandleBool = AttributeHandle; } diff --git a/src/Engine/Operator/Geometry.cpp b/src/Engine/Operator/Geometry.cpp index 069fb3e..8f2a72d 100644 --- a/src/Engine/Operator/Geometry.cpp +++ b/src/Engine/Operator/Geometry.cpp @@ -12,6 +12,7 @@ using namespace enzo; geo::Geometry::Geometry() : vertexCountHandlePrim_{addIntAttribute(ga::AttrOwner::PRIMITIVE, "vertexCount")}, + closedHandlePrim_{addBoolAttribute(ga::AttrOwner::PRIMITIVE, "closed")}, pointOffsetHandleVert_{addIntAttribute(ga::AttrOwner::VERTEX, "point")}, posHandlePoint_{addVector3Attribute(ga::AttrOwner::POINT, "P")} { @@ -19,27 +20,39 @@ geo::Geometry::Geometry() : } geo::Geometry::Geometry(const Geometry& other): + // attributes pointAttributes_{deepCopyAttributes(other.pointAttributes_)}, vertexAttributes_{deepCopyAttributes(other.vertexAttributes_)}, primitiveAttributes_{deepCopyAttributes(other.primitiveAttributes_)}, globalAttributes_{deepCopyAttributes(other.globalAttributes_)}, + + // handles vertexCountHandlePrim_{enzo::ga::AttributeHandleInt(getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount"))}, + closedHandlePrim_{enzo::ga::AttributeHandleBool(getAttribByName(ga::AttrOwner::PRIMITIVE, "closed"))}, pointOffsetHandleVert_{enzo::ga::AttributeHandleInt(getAttribByName(ga::AttrOwner::VERTEX, "point"))}, posHandlePoint_{enzo::ga::AttributeHandleVector3(getAttribByName(ga::AttrOwner::POINT, "P"))} { } -void geo::Geometry::addFace(std::initializer_list pointOffsets) +void geo::Geometry::addFace(std::vector pointOffsets, bool closed) { for(ga::Offset pointOffset : pointOffsets) { pointOffsetHandleVert_.addValue(pointOffset); } vertexCountHandlePrim_.addValue(pointOffsets.size()); + closedHandlePrim_.addValue(closed); } +void geo::Geometry::addPoint(const bt::Vector3& pos) +{ + posHandlePoint_.addValue(pos); + +} + + bt::Vector3 geo::Geometry::getPosFromVert(ga::Offset vertexOffset) const { // get point offset @@ -53,6 +66,12 @@ bt::Vector3 geo::Geometry::getPointPos(ga::Offset pointOffset) const return posHandlePoint_.getValue(pointOffset); } +void geo::Geometry::setPointPos(const ga::Offset offset, const bt::Vector3& pos) +{ + posHandlePoint_.setValue(offset, pos); +} + + unsigned int geo::Geometry::getPrimVertCount(ga::Offset primOffset) const { return vertexCountHandlePrim_.getValue(primOffset); @@ -68,6 +87,11 @@ ga::Offset geo::Geometry::getNumVerts() const return pointOffsetHandleVert_.getSize(); } +ga::Offset geo::Geometry::getNumPoints() const +{ + return posHandlePoint_.getSize(); +} + @@ -191,13 +215,26 @@ void geo::Geometry::computePrimStartVertices() -ga::AttributeHandle geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name) +ga::AttributeHandleInt geo::Geometry::addIntAttribute(ga::AttributeOwner owner, std::string name) { auto newAttribute = std::make_shared(name, ga::AttrType::intT); getAttributeStore(owner).push_back(newAttribute); - return ga::AttributeHandle(newAttribute); + return ga::AttributeHandleInt(newAttribute); } +ga::AttributeHandleBool geo::Geometry::addBoolAttribute(ga::AttributeOwner owner, std::string name) +{ + auto newAttribute = std::make_shared(name, ga::AttrType::boolT); + getAttributeStore(owner).push_back(newAttribute); + return ga::AttributeHandleBool(newAttribute); +} + +bt::boolT geo::Geometry::isClosed(ga::Offset primOffset) const +{ + return closedHandlePrim_.getValue(primOffset); +} + + ga::AttributeHandle geo::Geometry::addVector3Attribute(ga::AttributeOwner owner, std::string name) { auto newAttribute = std::make_shared(name, ga::AttrType::vectorT); diff --git a/src/Engine/Operator/Geometry.h b/src/Engine/Operator/Geometry.h index 833c97e..e865149 100644 --- a/src/Engine/Operator/Geometry.h +++ b/src/Engine/Operator/Geometry.h @@ -24,21 +24,28 @@ class Geometry public: Geometry(); Geometry(const Geometry& other); - ga::AttributeHandle addIntAttribute(ga::AttributeOwner owner, std::string name); + ga::AttributeHandle addIntAttribute(ga::AttributeOwner owner, std::string name); + ga::AttributeHandleBool addBoolAttribute(ga::AttributeOwner owner, std::string name); ga::AttributeHandle addVector3Attribute(ga::AttributeOwner owner, std::string name); // TODO: return weak ptr std::shared_ptr getAttribByName(ga::AttributeOwner owner, std::string name); std::vector derivePointNormals(); HeMesh computeHalfEdgeMesh(); - // returns the first vertex of the primitive - void addFace(std::initializer_list pointOffsets); + void addFace(std::vector pointOffsets, bool closed=true); + void addPoint(const bt::Vector3& pos); - ga::Offset getPrimStartVertex(ga::Offset primOffset) const; + void setPointPos(const ga::Offset offset, const bt::Vector3& pos); + + ga::Offset getPrimStartVertex(ga::Offset primOffset) const; // returns the first vertex of the primitive bt::Vector3 getPosFromVert(ga::Offset vertexOffset) const; bt::Vector3 getPointPos(ga::Offset pointOffset) const; unsigned int getPrimVertCount(ga::Offset primOffset) const; ga::Offset getNumPrims() const; ga::Offset getNumVerts() const; + ga::Offset getNumPoints() const; + + bt::boolT isClosed(ga::Offset primOffset) const; + void computePrimStartVertices(); private: using attribVector = std::vector>; @@ -55,6 +62,7 @@ private: // handles enzo::ga::AttributeHandleInt vertexCountHandlePrim_; + enzo::ga::AttributeHandleBool closedHandlePrim_; enzo::ga::AttributeHandleInt pointOffsetHandleVert_; enzo::ga::AttributeHandleVector3 posHandlePoint_; }; diff --git a/src/Engine/Types.h b/src/Engine/Types.h index d1b84dc..4ced7d5 100644 --- a/src/Engine/Types.h +++ b/src/Engine/Types.h @@ -19,6 +19,7 @@ namespace enzo floatT, listT, vectorT, + boolT, }; using AttrType = AttributeType; using AttrOwner = AttributeOwner; @@ -29,6 +30,7 @@ namespace enzo { using floatT = double; using intT = int64_t; + using boolT = bool; using Vector3 = Eigen::Vector3d; using Vector4 = Eigen::Vector4d; using String = std::string; diff --git a/src/Gui/Viewport/GLMesh.cpp b/src/Gui/Viewport/GLMesh.cpp index b4d147f..15d0a98 100644 --- a/src/Gui/Viewport/GLMesh.cpp +++ b/src/Gui/Viewport/GLMesh.cpp @@ -117,21 +117,29 @@ void GLMesh::setPosBuffer(enzo::geo::Geometry& geometry) unbind(); } -void GLMesh::setIndexBuffer(std::vector pointIndices, std::vector primVertexCounts) +void GLMesh::setIndexBuffer(enzo::geo::Geometry& geometry) { bind(); faceIndexData.clear(); lineIndexData.clear(); - unsigned int startVert = 0; // create triangle fan from potentially ngon inputs - for(size_t primNum=0; primNum=3) + if(!closed && primVertexCount>=2) + { + for(size_t i=0; i=3) { for(size_t i=1; i pointIndices, std::vector prim } } - if(primVertexCount==2) - { - IC(startVert, startVert+1); - lineIndexData.push_back(startVert); - lineIndexData.push_back(startVert+1); - } - startVert += primVertexCount; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, faceIndexBuffer); diff --git a/src/Gui/Viewport/GLMesh.h b/src/Gui/Viewport/GLMesh.h index ce773b9..6083ca7 100644 --- a/src/Gui/Viewport/GLMesh.h +++ b/src/Gui/Viewport/GLMesh.h @@ -29,7 +29,7 @@ public: void init(); void initBuffers(); void setPosBuffer(enzo::geo::Geometry& geometry); - void setIndexBuffer(std::vector pointIndices, std::vector primVertexCounts); + void setIndexBuffer(enzo::geo::Geometry& geometry); void bind(); void unbind(); void draw(); diff --git a/src/Gui/Viewport/ViewportGLWidget.cpp b/src/Gui/Viewport/ViewportGLWidget.cpp index f2cbe58..14c8ff4 100644 --- a/src/Gui/Viewport/ViewportGLWidget.cpp +++ b/src/Gui/Viewport/ViewportGLWidget.cpp @@ -184,14 +184,7 @@ std::unique_ptr ViewportGLWidget::meshFromGeo(enzo::geo::Geometry& geome mesh->setPosBuffer(geometry); - - std::shared_ptr pointAttr = geometry.getAttribByName(ga::AttrOwner::VERTEX, "point"); - ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr); - - std::shared_ptr vertexCountAttr = geometry.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount"); - ga::AttributeHandleInt vertexCountHandle = ga::AttributeHandleInt(vertexCountAttr); - - mesh->setIndexBuffer(pointAttrHandle.getAllValues(), vertexCountHandle.getAllValues()); + mesh->setIndexBuffer(geometry); @@ -205,12 +198,5 @@ void ViewportGLWidget::geometryChanged(enzo::geo::Geometry& geometry) ga::AttributeHandleVector3 PAttrHandle = ga::AttributeHandleVector3(PAttr); triangleMesh_->setPosBuffer(geometry); - - std::shared_ptr pointAttr = geometry.getAttribByName(ga::AttrOwner::VERTEX, "point"); - ga::AttributeHandleInt pointAttrHandle = ga::AttributeHandleInt(pointAttr); - - std::shared_ptr vertexCountAttr = geometry.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount"); - ga::AttributeHandleInt vertexCountHandle = ga::AttributeHandleInt(vertexCountAttr); - - triangleMesh_->setIndexBuffer(pointAttrHandle.getAllValues(), vertexCountHandle.getAllValues()); + triangleMesh_->setIndexBuffer(geometry); } diff --git a/src/OpDefs/GopGeometryImport.cpp b/src/OpDefs/GopGeometryImport.cpp index fadf756..29eba6c 100644 --- a/src/OpDefs/GopGeometryImport.cpp +++ b/src/OpDefs/GopGeometryImport.cpp @@ -40,14 +40,14 @@ void GopGeometryImport::cookOp(enzo::op::Context context) } - auto PAttr = geo.getAttribByName(ga::AttrOwner::POINT, "P"); - ga::AttributeHandleVector3 PAttrHandle(PAttr); + // 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 pointAttr = geo.getAttribByName(ga::AttrOwner::VERTEX, "point"); + // ga::AttributeHandleInt pointAttrHandle(pointAttr); - auto vertexCountAttr = geo.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount"); - ga::AttributeHandleInt vertexCountHandle(vertexCountAttr); + // auto vertexCountAttr = geo.getAttribByName(ga::AttrOwner::PRIMITIVE, "vertexCount"); + // ga::AttributeHandleInt vertexCountHandle(vertexCountAttr); std::ifstream file(filePath); if(!file.is_open()) @@ -76,10 +76,12 @@ void GopGeometryImport::cookOp(enzo::op::Context context) } const bt::Vector3 pointPos = {std::stod(result[1]), std::stod(result[2]), std::stod(result[3])}; - PAttrHandle.addValue(pointPos); + geo.addPoint(pointPos); } else if(firstChar=='f' || firstChar=='l') { + bool closedFace = firstChar=='f'; + std::vector result; boost::split(result, line, isspace); if(result.size()<3) @@ -88,16 +90,18 @@ void GopGeometryImport::cookOp(enzo::op::Context context) } + ga::Offset numVerts = result.size()-1; + + std::vector verts; + verts.reserve(numVerts); // set vertex attributes - for(int i=1; i myHandle = geo.addIntAttribute(ga::AttrOwner::POINT, "index"); + ga::AttributeHandleInt myHandle = geo.addIntAttribute(ga::AttrOwner::POINT, "index"); myHandle.addValue(5); myHandle.addValue(6); REQUIRE(myHandle.getValue(0)==5);