Skip to content

Commit

Permalink
Triangle Sets writer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderoster authored and gangatp committed Dec 17, 2024
1 parent 361db91 commit afc3bb3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 3 deletions.
1 change: 1 addition & 0 deletions Include/API/lib3mf_meshobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CMeshObject : public virtual IMeshObject, public virtual CObject {

NMR::CMeshInformation_Properties* getMeshInformationProperties();


protected:

/**
Expand Down
2 changes: 2 additions & 0 deletions Include/Common/Mesh/NMR_Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace NMR {
MESHFACES m_Faces;
CBeamLattice m_BeamLattice;



PMeshInformationHandler m_pMeshInformationHandler;
public:
CMesh();
Expand Down
6 changes: 6 additions & 0 deletions Include/Model/Classes/NMR_ModelConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ These are given by the 3MF Standard
#define XML_3MF_NAMESPACE_MATERIALSPEC "http://schemas.microsoft.com/3dmanufacturing/material/2015/02"
#define XML_3MF_NAMESPACE_PRODUCTIONSPEC "http://schemas.microsoft.com/3dmanufacturing/production/2015/06"
#define XML_3MF_NAMESPACE_BEAMLATTICESPEC "http://schemas.microsoft.com/3dmanufacturing/beamlattice/2017/02"
#define XML_3MF_NAMESPACE_TRIANGLESETS "http://schemas.microsoft.com/3dmanufacturing/trianglesets/2021/07"
#define XML_3MF_NAMESPACE_SLICESPEC "http://schemas.microsoft.com/3dmanufacturing/slice/2015/07"
#define XML_3MF_NAMESPACE_SECURECONTENTSPEC "http://schemas.microsoft.com/3dmanufacturing/securecontent/2019/04"
#define XML_3MF_NAMESPACE_DIGITALSIGNATURESPEC "http://www.w3.org/2000/09/xmldsig#"
Expand All @@ -94,6 +95,7 @@ These are given by the 3MF Standard
#define XML_3MF_NAMESPACEPREFIX_SECURECONTENT "sc"
#define XML_3MF_NAMESPACEPREFIX_VOLUMETRIC "v"
#define XML_3MF_NAMESPACEPREFIX_IMPLICIT "i"
#define XML_3MF_NAMESPACEPREFIX_TRIANGLESETS "t"

#define XML_3MF_ATTRIBUTE_XMLNS "xmlns"
#define XML_3MF_ATTRIBUTE_PREFIX_XML "xml"
Expand Down Expand Up @@ -149,6 +151,8 @@ These are given by the 3MF Standard
#define XML_3MF_ELEMENT_BEAMSET "beamset"
#define XML_3MF_ELEMENT_REF "ref"
#define XML_3MF_ELEMENT_BALLREF "ballref"
#define XML_3MF_ELEMENT_TRIANGLESETS "trianglesets"
#define XML_3MF_ELEMENT_TRIANGLESET "triangleset"

// Triangle element.
#define XML_3MF_ELEMENT_TRIANGLE "triangle"
Expand Down Expand Up @@ -188,6 +192,8 @@ These are given by the 3MF Standard
// deprecated (has been used in draft version of the specification):
#define XML_3MF_ATTRIBUTE_BEAMLATTICE_ACCURACY "accuracy"
#define XML_3MF_ATTRIBUTE_BEAMLATTICE_PRECISION "precision"
#define XML_3MF_ATTRIBUTE_TRIANGLESET_IDENTIFIER "identifier"
#define XML_3MF_ATTRIBUTE_TRIANGLESET_NAME "name"

// Vertex element.
#define XML_3MF_ELEMENT_VERTEX "vertex"
Expand Down
2 changes: 2 additions & 0 deletions Include/Model/Classes/NMR_ModelTriangleSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace NMR {
PModelTriangleSet duplicate(const std::string & sIdentifier);
void merge (PModelTriangleSet pTriangleSet);

std::set<uint32_t> & getTriangles ();

};

}
Expand Down
11 changes: 10 additions & 1 deletion Include/Model/Writer/v100/NMR_ModelWriterNode100_Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ This is the class for exporting the 3mf mesh node.
#define MODELWRITERMESH100_BEAMLATTICE_REFSTARTLENGTH 14
#define MODELWRITERMESH100_BEAMLATTICE_BALLREFLINESTART "<b:ballref index=\""
#define MODELWRITERMESH100_BEAMLATTICE_BALLREFSTARTLENGTH 18
#define MODELWRITERMESH100_TRIANGLESETS_REFLINESTART "<t:ref index=\""
#define MODELWRITERMESH100_TRIANGLESETS_REFSTARTLENGTH 14


namespace NMR {
Expand All @@ -69,6 +71,7 @@ namespace NMR {
nfBool m_bWriteMaterialExtension;
nfBool m_bWriteBeamLatticeExtension;
nfBool m_bWriteVolumetricExtension;
nfBool m_bWriteTriangleSetExtension;

// Internal functions for an efficient and buffered output of raw XML data
std::array<nfChar, MODELWRITERMESH100_LINEBUFFERSIZE> m_VertexLine;
Expand All @@ -77,12 +80,14 @@ namespace NMR {
std::array<nfChar, MODELWRITERMESH100_LINEBUFFERSIZE> m_BallLine;
std::array<nfChar, MODELWRITERMESH100_LINEBUFFERSIZE> m_BeamRefLine;
std::array<nfChar, MODELWRITERMESH100_LINEBUFFERSIZE> m_BallRefLine;
std::array<nfChar, MODELWRITERMESH100_LINEBUFFERSIZE> m_TriangleSetRefLine;
nfUint32 m_nVertexBufferPos;
nfUint32 m_nTriangleBufferPos;
nfUint32 m_nBeamBufferPos;
nfUint32 m_nBallBufferPos;
nfUint32 m_nBeamRefBufferPos;
nfUint32 m_nBallRefBufferPos;
nfUint32 m_nTriangleSetRefBufferPos;
private:
const int m_nPosAfterDecPoint;
const nfInt64 m_nPutDoubleFactor;
Expand Down Expand Up @@ -110,6 +115,9 @@ namespace NMR {
__NMR_INLINE void putBallRefString(_In_ const nfChar * pszString);
__NMR_INLINE void putBallRefUInt32(_In_ const nfUint32 nValue);

__NMR_INLINE void putTriangleSetRefString(_In_ const nfChar* pszString);
__NMR_INLINE void putTriangleSetRefUInt32(_In_ const nfUint32 nValue);

__NMR_INLINE void writeVertexData(_In_ MESHNODE * pNode);
__NMR_INLINE void writeFaceData_Plain(_In_ MESHFACE * pFace, _In_opt_ const nfChar * pszAdditionalString);
__NMR_INLINE void writeFaceData_OneProperty(_In_ MESHFACE * pFace, _In_ const ModelResourceID nPropertyID, _In_ const ModelResourceIndex nPropertyIndex, _In_opt_ const nfChar * pszAdditionalString);
Expand All @@ -118,11 +126,12 @@ namespace NMR {
__NMR_INLINE void writeBallData(_In_ MESHBALL * pBall, _In_ eModelBeamLatticeBallMode eBallMode, _In_ nfDouble dRadius);
__NMR_INLINE void writeRefData(_In_ INT nRefID);
__NMR_INLINE void writeBallRefData(_In_ INT nRefID);
__NMR_INLINE void writeTriangleSetRefData(_In_ INT nRefID);
public:
CModelWriterNode100_Mesh() = delete;
CModelWriterNode100_Mesh(_In_ CModelMeshObject * pModelMeshObject, _In_ CXmlWriter * pXMLWriter, _In_ PProgressMonitor pProgressMonitor,
_In_ PMeshInformation_PropertyIndexMapping pPropertyIndexMapping, _In_ int nPosAfterDecPoint, _In_ nfBool bWriteMaterialExtension, _In_ nfBool m_bWriteBeamLatticeExtension,
_In_ nfBool bWriteVolumetricExtension);
_In_ nfBool bWriteVolumetricExtension, _In_ nfBool bWriteTriangleSetExtension);
virtual void writeToXML();
};

Expand Down
1 change: 1 addition & 0 deletions Include/Model/Writer/v100/NMR_ModelWriterNode100_Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace NMR {
nfBool m_bWriteMaterialExtension;
nfBool m_bWriteProductionExtension;
nfBool m_bWriteBeamLatticeExtension;
nfBool m_bWriteTriangleSetExtension;
nfBool m_bWriteNurbsExtension;
nfBool m_bWriteSliceExtension;
nfBool m_bWriteSecureContentExtension;
Expand Down
4 changes: 4 additions & 0 deletions Source/Model/Classes/NMR_ModelTriangleSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,9 @@ namespace NMR {

}

std::set<uint32_t>& CModelTriangleSet::getTriangles()
{
return m_sTriangles;

}
}
67 changes: 66 additions & 1 deletion Source/Model/Writer/v100/NMR_ModelWriterNode100_Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This is the class for exporting the 3mf mesh node.
namespace NMR {

CModelWriterNode100_Mesh::CModelWriterNode100_Mesh(_In_ CModelMeshObject * pModelMeshObject, _In_ CXmlWriter * pXMLWriter, _In_ PProgressMonitor pProgressMonitor,
_In_ PMeshInformation_PropertyIndexMapping pPropertyIndexMapping, _In_ int nPosAfterDecPoint, _In_ nfBool bWriteMaterialExtension, _In_ nfBool bWriteBeamLatticeExtension, _In_ nfBool bWriteVolumetricExtension)
_In_ PMeshInformation_PropertyIndexMapping pPropertyIndexMapping, _In_ int nPosAfterDecPoint, _In_ nfBool bWriteMaterialExtension, _In_ nfBool bWriteBeamLatticeExtension, _In_ nfBool bWriteVolumetricExtension, _In_ nfBool bWriteTriangleSetExtension)
:CModelWriterNode_ModelBase(pModelMeshObject->getModel(), pXMLWriter, pProgressMonitor), m_nPosAfterDecPoint(nPosAfterDecPoint), m_nPutDoubleFactor((nfInt64)(pow(10, CModelWriterNode100_Mesh::m_nPosAfterDecPoint)))
{
__NMRASSERT(pModelMeshObject != nullptr);
Expand All @@ -61,6 +61,7 @@ namespace NMR {
m_bWriteMaterialExtension = bWriteMaterialExtension;
m_bWriteBeamLatticeExtension = bWriteBeamLatticeExtension;
m_bWriteVolumetricExtension = bWriteVolumetricExtension;
m_bWriteTriangleSetExtension = bWriteTriangleSetExtension;

m_pModelMeshObject = pModelMeshObject;
m_pPropertyIndexMapping = pPropertyIndexMapping;
Expand All @@ -72,12 +73,14 @@ namespace NMR {
m_nBallBufferPos = 0;
m_nBeamRefBufferPos = 0;
m_nBallRefBufferPos = 0;
m_nTriangleSetRefBufferPos = 0;
putVertexString(MODELWRITERMESH100_VERTEXLINESTART);
putTriangleString(MODELWRITERMESH100_TRIANGLELINESTART);
putBeamString(MODELWRITERMESH100_BEAMLATTICE_BEAMLINESTART);
putBallString(MODELWRITERMESH100_BEAMLATTICE_BALLLINESTART);
putBeamRefString(MODELWRITERMESH100_BEAMLATTICE_REFLINESTART);
putBallRefString(MODELWRITERMESH100_BEAMLATTICE_BALLREFLINESTART);
putTriangleSetRefString(MODELWRITERMESH100_TRIANGLESETS_REFLINESTART);
}

bool stringRepresentationsDiffer(double a, double b, nfInt64 putFactor) {
Expand Down Expand Up @@ -262,6 +265,32 @@ namespace NMR {
}
writeFullEndElement();


if (m_bWriteTriangleSetExtension) {
uint32_t nTriangleSetCount = m_pModelMeshObject->getTriangleSetCount();

if (nTriangleSetCount > 0) {
writeStartElementWithPrefix(XML_3MF_ELEMENT_TRIANGLESETS, XML_3MF_NAMESPACEPREFIX_TRIANGLESETS);

for (uint32_t nTriangleSetIndex = 0; nTriangleSetIndex < nTriangleSetCount; nTriangleSetIndex++) {
auto pTriangleSet = m_pModelMeshObject->getTriangleSet(nTriangleSetIndex);
writeStartElementWithPrefix(XML_3MF_ELEMENT_TRIANGLESET, XML_3MF_NAMESPACEPREFIX_TRIANGLESETS);
writeStringAttribute(XML_3MF_ATTRIBUTE_TRIANGLESET_IDENTIFIER, pTriangleSet->getIdentifier ());
writeStringAttribute(XML_3MF_ATTRIBUTE_TRIANGLESET_NAME, pTriangleSet->getName());

auto triangleRefs = pTriangleSet->getTriangles();
for (auto triangleRef : triangleRefs) {
writeTriangleSetRefData (triangleRef);
}

writeFullEndElement();
}

writeFullEndElement();
}
}


if (bMeshHasAProperty && !(nObjectLevelPropertyID != 0)) {
throw CNMRException(NMR_ERROR_MISSINGOBJECTLEVELPID);
}
Expand Down Expand Up @@ -610,6 +639,34 @@ namespace NMR {
m_nBeamRefBufferPos += nCount;
}

void CModelWriterNode100_Mesh::putTriangleSetRefString(_In_ const nfChar* pszString)
{
__NMRASSERT(pszString);
const nfChar* pChar = pszString;
nfChar* pTarget = &m_TriangleSetRefLine[m_nTriangleSetRefBufferPos];

while (*pChar != 0) {
*pTarget = *pChar;
pTarget++;
pChar++;
m_nTriangleSetRefBufferPos++;
}
}

void CModelWriterNode100_Mesh::putTriangleSetRefUInt32(_In_ const nfUint32 nValue)
{
#ifdef __GNUC__
int nCount = sprintf(&m_TriangleSetRefLine[m_nTriangleSetRefBufferPos], "%d", nValue);
#else
int nCount = sprintf_s(&m_TriangleSetRefLine[m_nTriangleSetRefBufferPos], MODELWRITERMESH100_LINEBUFFERSIZE - m_nTriangleSetRefBufferPos, "%d", nValue);
#endif // __GNUC__

if (nCount < 1)
throw CNMRException(NMR_ERROR_COULDNOTCONVERTNUMBER);
m_nTriangleSetRefBufferPos += nCount;
}


void CModelWriterNode100_Mesh::putBallRefString(_In_ const nfChar* pszString)
{
__NMRASSERT(pszString);
Expand Down Expand Up @@ -792,4 +849,12 @@ namespace NMR {
m_pXMLWriter->WriteRawLine(&m_BallRefLine[0], m_nBallRefBufferPos);
}

__NMR_INLINE void CModelWriterNode100_Mesh::writeTriangleSetRefData(_In_ INT nRefID)
{
m_nTriangleSetRefBufferPos = MODELWRITERMESH100_TRIANGLESETS_REFSTARTLENGTH;
putTriangleSetRefUInt32(nRefID);
putTriangleSetRefString("\"/>");
m_pXMLWriter->WriteRawLine(&m_TriangleSetRefLine[0], m_nTriangleSetRefBufferPos);
}

}
12 changes: 11 additions & 1 deletion Source/Model/Writer/v100/NMR_ModelWriterNode100_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace NMR {
m_bWriteBeamLatticeExtension = true;
m_bWriteSliceExtension = true;
m_bWriteSecureContentExtension = true;
m_bWriteTriangleSetExtension = true;
m_bWriteBaseMaterials = true;
m_bWriteObjects = true;
m_bWriteVolumetricExtension = true;
Expand Down Expand Up @@ -166,6 +167,15 @@ namespace NMR {
}
}

if (m_bWriteTriangleSetExtension) {
writeConstPrefixedStringAttribute(XML_3MF_ATTRIBUTE_XMLNS, XML_3MF_NAMESPACEPREFIX_SLICE, XML_3MF_NAMESPACE_TRIANGLESETS);
if (m_pModel->RequireExtension(XML_3MF_NAMESPACE_TRIANGLESETS)) {
if (sRequiredExtensions.size() > 0)
sRequiredExtensions = sRequiredExtensions + " ";
sRequiredExtensions = sRequiredExtensions + XML_3MF_NAMESPACEPREFIX_TRIANGLESETS;
}
}

if (m_bWriteSecureContentExtension) {
writeConstPrefixedStringAttribute(XML_3MF_ATTRIBUTE_XMLNS, XML_3MF_NAMESPACEPREFIX_SECURECONTENT, XML_3MF_NAMESPACE_SECURECONTENTSPEC);
if (m_pModel->RequireExtension(XML_3MF_NAMESPACE_SECURECONTENTSPEC)) {
Expand Down Expand Up @@ -603,7 +613,7 @@ namespace NMR {
pMeshObject, m_pXMLWriter, m_pProgressMonitor,
m_pPropertyIndexMapping, m_nDecimalPrecision,
m_bWriteMaterialExtension, m_bWriteBeamLatticeExtension,
m_bWriteVolumetricExtension);
m_bWriteVolumetricExtension, m_bWriteTriangleSetExtension);

ModelWriter_Mesh.writeToXML();
}
Expand Down

0 comments on commit afc3bb3

Please sign in to comment.