Skip to content

Commit

Permalink
fixed surface face index bug for older files
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancrews committed Nov 22, 2023
1 parent 38d3441 commit d41c96c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 43 deletions.
4 changes: 2 additions & 2 deletions LandXML2glTF/LXML2glTF/LXML2glTF.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>..\..\..\..\LandXML\* /s /b</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>"D:\GitHub\LandXML2glTF\LandXML\*" /s</LocalDebuggerCommandArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommandArguments>..\..\..\..\LandXML\subdivision-2.0\* /b</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>"D:\GitHub\LandXML2glTF\LandXML\*"</LocalDebuggerCommandArguments>
</PropertyGroup>
</Project>
6 changes: 4 additions & 2 deletions LandXML2glTF/LandXML2glTFDll/Inc/Models/LandXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "framework.h"
#include "GLTF.h"
#include <map>

#ifdef _MSC_VER
# pragma warning(push)
Expand Down Expand Up @@ -107,7 +108,8 @@ struct LANDXML2GLTFDLLAPI LandXMLSurface
LandXMLPoint3D m_minSurfPoint;
LandXMLPoint3D m_maxSurfPoint;

std::vector<LandXMLPoint3D> m_surfacePoints;
unsigned int m_surfacePointCount = 0;
std::map<unsigned int, LandXMLPoint3D> m_surfacePoints;
std::list<LandXMLTextureBoundary> m_textureBoundaries;
};

Expand Down Expand Up @@ -136,7 +138,7 @@ struct LANDXML2GLTFDLLAPI LandXMLModel
std::vector<LandXMLPolyline> m_landxmlPlanFeatures;
std::vector<LandXMLAlignment> m_landxmlAlignments;
std::vector<LandXMLParcel> m_landxmlParcels;
std::vector<LandXMLPoint3D> m_landxmlPoints;
std::map<std::string, LandXMLPoint3D> m_landxmlPoints;
};


Expand Down
18 changes: 13 additions & 5 deletions LandXML2glTF/LandXML2glTFDll/source/LandXMLModel2glTF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ bool LandXMLModel2glTF::Convert2glTFModel(const std::string& InLandXMLFilename,

glTFDoc.asset.generator = "LandXML to glTF 2.0 Converter, version 1.0";
glTFDoc.asset.copyright = "Nathan Crews";

std::cout << "Building glTF model...\n";
retval = CreateGLTFModel(landXMLModel, glTFDoc, gltfModel);

std::cout << "Writing glTF file: " << glTFFilename << "\n";
WriteGLTFFile(glTFDoc, gltfModel, std::filesystem::path(glTFFilename));
try {
retval = CreateGLTFModel(landXMLModel, glTFDoc, gltfModel);

std::cout << "Writing glTF file: " << glTFFilename << "\n";
WriteGLTFFile(glTFDoc, gltfModel, std::filesystem::path(glTFFilename));
}
catch (...)
{

}

// cleanup memory
delete LXDocument;
Expand Down Expand Up @@ -145,8 +151,10 @@ bool LandXMLModel2glTF::CreateGLTFModel(const LandXMLModel& landXMLModel, Micros
gltfSurfModel->name = LXSurf->m_name;

// build the surface meshes
for (LandXMLPoint3D lxPnt : LXSurf->m_surfacePoints)
for (unsigned int pid = 1; pid <= LXSurf->m_surfacePointCount; pid++)
{
LandXMLPoint3D lxPnt = LXSurf->m_surfacePoints[pid];

std::vector<float> glpnt1(3U);

//if (m_wgsTrans)
Expand Down
98 changes: 64 additions & 34 deletions LandXML2glTF/LandXML2glTFDll/source/Readers/LXParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool LXParser::ParseLandXMLHeader(tinyxml2::XMLDocument* LXDocument, LandXMLMode

XMLElement* LXRoot = LXDocument->RootElement();

if (!LXRoot || (stricmp(LXRoot->Name(), "LandXML") != 0) )
if (!LXRoot || (stricmp(LXRoot->Name(), "LandXML") != 0))
{
std::cout << "error: The XML file does not contain valid LandXML data: " << outLandXMLMDoc.m_fileName;
return retStat;
Expand Down Expand Up @@ -161,9 +161,9 @@ bool LXParser::ParseMaterialTable(XMLElement* LXMaterialsNode, LandXMLMaterialTa
{
bool retStat = false;
tinyxml2::XMLDocument defaultMatDoc;
std::filesystem::path defaultMatDocPath = "./data/DefaultTexture.xml";
std::filesystem::path defaultMatDocPath = "./data/DefaultTexture.xml";
std::filesystem::path defaultMatDocPathABS = std::filesystem::absolute(defaultMatDocPath);

std::string defaultMatDocFullPath = defaultMatDocPathABS.string().c_str();

XMLElement* defaultMatRootElem = nullptr;
Expand Down Expand Up @@ -240,7 +240,7 @@ bool LXParser::ParseMaterialTable(XMLElement* LXMaterialsNode, LandXMLMaterialTa
{
XMLNode* TextureHexString = textureImage->FirstChildElement("TextureHexString");

if (TextureHexString)
if (TextureHexString && TextureHexString->FirstChild())
{
size_t hexStrLen = strlen(TextureHexString->FirstChild()->Value()) + 1;
LXMaterialEntry.m_textureImageHexString = TextureHexString->FirstChild()->Value();
Expand Down Expand Up @@ -335,14 +335,28 @@ bool LXParser::ParseSurfacePoints(XMLElement* LXSurfaceDefNode, LandXMLSurface&

XMLElement* LXSurfacePnt = LXSurfacePnts->FirstChildElement("P");

outLandXMLSurface.m_surfacePointCount = 0;
// Parse the surface points
while (LXSurfacePnt)
{
LandXMLPoint3D surfPnt;

unsigned int pointID = 1, pointIDFix = 0;

const XMLAttribute* pIDAtt = LXSurfacePnt->FindAttribute("id");
if (pIDAtt)
{
pointID = std::atoi(pIDAtt->Value());

if (outLandXMLSurface.m_surfacePointCount == 0)
{
pointIDFix = pointID - 1;
}
}

if (ParsePoint(LXSurfacePnt, surfPnt))
{
outLandXMLSurface.m_surfacePoints.push_back(surfPnt);
outLandXMLSurface.m_surfacePoints[pointID] = surfPnt;

// find min surf coords
if (surfPnt.x < outLandXMLSurface.m_minSurfPoint.x)
Expand Down Expand Up @@ -378,9 +392,10 @@ bool LXParser::ParseSurfacePoints(XMLElement* LXSurfaceDefNode, LandXMLSurface&
}

LXSurfacePnt = LXSurfacePnt->NextSiblingElement("P");
outLandXMLSurface.m_surfacePointCount++;
}

if (outLandXMLSurface.m_surfacePoints.size() > 2)
if (outLandXMLSurface.m_surfacePointCount > 2)
{
retval = true;
}
Expand Down Expand Up @@ -413,9 +428,9 @@ bool LXParser::ParseSurfaceFaces(XMLElement* LXSurfaceDefNode, LandXMLSurface& o
{
// LandXML surface ids start at 1, reduce point IDs by 1 to make vertex face point ids match zero based surface TIN point array

UINT facedPointId1 = surfFace.m_pointIndices[0] - 1;
UINT facedPointId2 = surfFace.m_pointIndices[1] - 1;
UINT facedPointId3 = surfFace.m_pointIndices[2] - 1;
UINT facedPointId1 = surfFace.m_pointIndices[0];// -1;
UINT facedPointId2 = surfFace.m_pointIndices[1];// -1;
UINT facedPointId3 = surfFace.m_pointIndices[2];// -1;

LandXMLPoint3D p1 = outLandXMLSurface.m_surfacePoints[facedPointId1];
LandXMLPoint3D p2 = outLandXMLSurface.m_surfacePoints[facedPointId2];
Expand Down Expand Up @@ -483,16 +498,20 @@ bool LXParser::ParseSurfaceBoundaries(XMLElement* LXSurfaceNode, LandXMLSurface&
bndryPoly.m_description = typeAt->Value();
}

const XMLAttribute* materialAt = LXSurfaceBndry->FindAttribute("m");
if (materialAt)
if (!_stricmp(bndryPoly.m_description.c_str(), "texture"))
{
bndryPoly.m_materialID = std::atoi(materialAt->Value());
}

ParsePointList3D(LXPntList, bndryPoly.m_polylinePoints);
bndryPoly.m_area = MathHelper::PolygonArea(bndryPoly.m_polylinePoints);
const XMLAttribute* materialAt = LXSurfaceBndry->FindAttribute("m");
if (materialAt)
{
bndryPoly.m_materialID = std::atoi(materialAt->Value());
}

ParsePointList3D(LXPntList, bndryPoly.m_polylinePoints);
bndryPoly.m_area = MathHelper::PolygonArea(bndryPoly.m_polylinePoints);

outLandXMLSurface.m_textureBoundaries.push_back(bndryPoly);
outLandXMLSurface.m_textureBoundaries.push_back(bndryPoly);
}
}

LXSurfaceBndry = LXSurfaceBndry->NextSiblingElement("Boundary");
Expand Down Expand Up @@ -599,28 +618,39 @@ bool LXParser::ParseCgPoints(XMLElement* LXCgPoints, LandXMLModel& outLandXMLMDo
toAdd.m_name = nameAt->Value();
}

const XMLAttribute* descAt = LXCgPoint->FindAttribute("desc");
if (descAt)
const XMLAttribute* pntRefAt = LXCgPoint->FindAttribute("pntRef");
if (!pntRefAt)
{
toAdd.m_description = descAt->Value();
}
const XMLAttribute* descAt = LXCgPoint->FindAttribute("desc");
if (descAt)
{
toAdd.m_description = descAt->Value();
}

const XMLAttribute* codeAt = LXCgPoint->FindAttribute("code");
if (codeAt)
{
toAdd.m_code = codeAt->Value();
}
const XMLAttribute* codeAt = LXCgPoint->FindAttribute("code");
if (codeAt)
{
toAdd.m_code = codeAt->Value();
}

const XMLAttribute* materialAt = LXCgPoint->FindAttribute("m");
if (materialAt)
{
toAdd.m_materialID = std::atoi(materialAt->Value());
}
const XMLAttribute* materialAt = LXCgPoint->FindAttribute("m");
if (materialAt)
{
toAdd.m_materialID = std::atoi(materialAt->Value());
}

if (ParsePoint(LXCgPoint, toAdd))
if (ParsePoint(LXCgPoint, toAdd))
{
outLandXMLMDoc.m_landxmlPoints[toAdd.m_name] = toAdd;
retStat = true;
}
}
else
{
outLandXMLMDoc.m_landxmlPoints.push_back(toAdd);
retStat = true;
if (pntRefAt->Value())
{
outLandXMLMDoc.m_landxmlPoints[toAdd.m_name] = outLandXMLMDoc.m_landxmlPoints[pntRefAt->Value()];
}
}

LXCgPoint = LXCgPoint->NextSiblingElement("CgPoint");
Expand Down Expand Up @@ -685,7 +715,7 @@ bool LXParser::ParseCoordGeom(XMLElement* LXCoordGeom, std::vector<LandXMLPoint3
{
OutReturnPointList.push_back(startPnt);
}

}

XMLElement* LXEnd = LXLine->FirstChildElement("End");
Expand Down

0 comments on commit d41c96c

Please sign in to comment.