Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OpenGIS Simple Features for OpenDRIVE 4 : * Purpose: Implementation of OGRXODRLayer. 5 : * Author: Michael Scholz, German Aerospace Center (DLR) 6 : * Gülsen Bardak, German Aerospace Center (DLR) 7 : * 8 : ****************************************************************************** 9 : * Copyright 2024 German Aerospace Center (DLR), Institute of Transportation Systems 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #include "ogr_api.h" 15 : #include "ogr_geometry.h" 16 : #include "ogr_xodr.h" 17 : 18 : #include "cpl_error.h" 19 : #include <cmath> 20 : #include <iostream> 21 : #include <memory> 22 : #include <string> 23 : #include <typeinfo> 24 : 25 39 : OGRXODRLayer::OGRXODRLayer(const RoadElements &xodrRoadElements, 26 39 : const std::string &proj4Defn) 27 39 : : OGRXODRLayer(xodrRoadElements, proj4Defn, false) 28 : { 29 39 : } 30 : 31 78 : OGRXODRLayer::OGRXODRLayer(const RoadElements &xodrRoadElements, 32 : const std::string &proj4Defn, 33 78 : const bool dissolveTriangulatedSurface) 34 : : m_roadElements(xodrRoadElements), 35 78 : m_bDissolveTIN(dissolveTriangulatedSurface) 36 : { 37 78 : if (!proj4Defn.empty()) 38 : { 39 72 : m_oSRS.importFromProj4(proj4Defn.c_str()); 40 : } 41 78 : resetRoadElementIterators(); 42 78 : } 43 : 44 702 : void OGRXODRLayer::ResetReading() 45 : { 46 702 : m_nNextFID = 0; 47 702 : resetRoadElementIterators(); 48 702 : } 49 : 50 780 : void OGRXODRLayer::resetRoadElementIterators() 51 : { 52 780 : m_roadIter = m_roadElements.roads.begin(); 53 780 : m_referenceLineIter = m_roadElements.referenceLines.begin(); 54 : 55 780 : m_laneIter = m_roadElements.lanes.begin(); 56 780 : m_laneSectionIter = m_roadElements.laneSections.begin(); 57 780 : m_laneRoadIDIter = m_roadElements.laneRoadIDs.begin(); 58 780 : m_laneMeshIter = m_roadElements.laneMeshes.begin(); 59 : 60 780 : m_laneLinesInnerIter = m_roadElements.laneLinesInner.begin(); 61 780 : m_laneLinesOuterIter = m_roadElements.laneLinesOuter.begin(); 62 : 63 780 : m_roadMarkIter = m_roadElements.roadMarks.begin(); 64 780 : m_roadMarkMeshIter = m_roadElements.roadMarkMeshes.begin(); 65 : 66 780 : m_roadObjectIter = m_roadElements.roadObjects.begin(); 67 780 : m_roadObjectMeshesIter = m_roadElements.roadObjectMeshes.begin(); 68 : 69 780 : m_roadSignalIter = m_roadElements.roadSignals.begin(); 70 780 : m_roadSignalMeshesIter = m_roadElements.roadSignalMeshes.begin(); 71 780 : } 72 : 73 : std::unique_ptr<OGRTriangulatedSurface> 74 32757 : OGRXODRLayer::triangulateSurface(const odr::Mesh3D &mesh) 75 : { 76 65514 : std::vector<odr::Vec3D> meshVertices = mesh.vertices; 77 65514 : std::vector<uint32_t> meshIndices = mesh.indices; 78 : 79 32757 : auto tin = std::make_unique<OGRTriangulatedSurface>(); 80 32757 : const size_t numIndices = meshIndices.size(); 81 : // Build triangles from mesh vertices. 82 : // Each triple of mesh indices defines which vertices form a triangle. 83 1275780 : for (std::size_t idx = 0; idx < numIndices; idx += 3) 84 : { 85 1243020 : uint32_t vertexIdx = meshIndices[idx]; 86 1243020 : odr::Vec3D vertexP = meshVertices[vertexIdx]; 87 2486040 : OGRPoint p(vertexP[0], vertexP[1], vertexP[2]); 88 : 89 1243020 : vertexIdx = meshIndices[idx + 1]; 90 1243020 : odr::Vec3D vertexQ = meshVertices[vertexIdx]; 91 2486040 : OGRPoint q(vertexQ[0], vertexQ[1], vertexQ[2]); 92 : 93 1243020 : vertexIdx = meshIndices[idx + 2]; 94 1243020 : odr::Vec3D vertexR = meshVertices[vertexIdx]; 95 2486040 : OGRPoint r(vertexR[0], vertexR[1], vertexR[2]); 96 : 97 2486040 : OGRTriangle triangle(p, q, r); 98 1243020 : tin->addGeometry(&triangle); 99 : } 100 : 101 65514 : return tin; 102 : }