KOSMIndoorMap

navmesh.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "navmesh.h"
7#include "navmesh_p.h"
8#include "logging.h"
9
10#include <QFile>
11
12#include <cstdint>
13
14using namespace KOSMIndoorRouting;
15
16#if HAVE_RECAST
17constexpr inline const uint32_t NAVMESHSET_MAGIC = 'M' << 24 | 'S' << 16 | 'E' << 8 | 'T';
18constexpr inline const uint32_t NAVMESHSET_VERSION = 1;
19
20struct NavMeshSetHeader
21{
22 uint32_t magic = NAVMESHSET_MAGIC;
23 uint32_t version = NAVMESHSET_VERSION;
24 uint32_t numTiles = 0;
25 dtNavMeshParams params;
26};
27
28struct NavMeshTileHeader
29{
30 dtTileRef tileRef;
31 uint32_t dataSize;
32};
33#endif
34
35
36NavMesh::NavMesh() = default;
37NavMesh::NavMesh(NavMesh&&) noexcept = default;
38NavMesh::NavMesh(const NavMesh&) = default;
39NavMesh::~NavMesh() = default;
40NavMesh& NavMesh::operator=(NavMesh&&) noexcept = default;
41NavMesh& NavMesh::operator=(const NavMesh&) = default;
42
43bool NavMesh::isValid() const
44{
45 return d && !d->m_dirty;
46}
47
48void NavMesh::clear()
49{
50 d.reset();
51}
52
53NavMeshTransform NavMesh::transform() const
54{
55 return d ? d->m_transform : NavMeshTransform();
56}
57
58void NavMesh::writeToFile(const QString &fileName) const
59{
60 QFile f(fileName);
61 if (!f.open(QFile::WriteOnly)) {
62 qCWarning(Log) << fileName << f.errorString();
63 return;
64 }
65#if HAVE_RECAST
66 const auto *mesh = d->m_navMesh.get();
67
68 NavMeshSetHeader header;
69 for (auto i = 0; i < mesh->getMaxTiles(); ++i) {
70 const auto tile = mesh->getTile(i);
71 if (!tile || !tile->header || !tile->dataSize) {
72 continue;
73 }
74 header.numTiles++;
75 }
76 std::memcpy(&header.params, d->m_navMesh->getParams(), sizeof(dtNavMeshParams));
77 f.write(reinterpret_cast<const char*>(&header), sizeof(NavMeshSetHeader));
78
79 for (auto i = 0; i < mesh->getMaxTiles(); ++i) {
80 const auto tile = mesh->getTile(i);
81 if (!tile || !tile->header || !tile->dataSize) {
82 continue;
83 }
84
85 NavMeshTileHeader tileHeader;
86 tileHeader.tileRef = mesh->getTileRef(tile);
87 tileHeader.dataSize = tile->dataSize;
88
89 f.write(reinterpret_cast<const char*>(&tileHeader), sizeof(NavMeshTileHeader));
90 f.write(reinterpret_cast<const char*>(tile->data), tile->dataSize);
91 }
92#endif
93}
Compiled nav mesh for routing.
Definition navmesh.h:22
void writeToFile(const QString &fileName) const
Write nav mesh data to the given file.
Definition navmesh.cpp:58
KDB_EXPORT KDbVersionInfo version()
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
QString errorString() const const
qint64 write(const QByteArray &data)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:55:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.