1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include "pathgridcheck.hpp"
#include <algorithm>
#include <memory>
#include <sstream>
#include <apps/opencs/model/doc/messages.hpp>
#include <apps/opencs/model/prefs/category.hpp>
#include <apps/opencs/model/prefs/setting.hpp>
#include <apps/opencs/model/world/record.hpp>
#include <components/esm3/loadpgrd.hpp>
#include "../prefs/state.hpp"
#include "../world/idcollection.hpp"
#include "../world/pathgrid.hpp"
#include "../world/subcellcollection.hpp"
#include "../world/universalid.hpp"
CSMTools::PathgridCheckStage::PathgridCheckStage(const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids)
: mPathgrids(pathgrids)
{
mIgnoreBaseRecords = false;
}
int CSMTools::PathgridCheckStage::setup()
{
mIgnoreBaseRecords = CSMPrefs::get()["Reports"]["ignore-base-records"].isTrue();
return mPathgrids.getSize();
}
void CSMTools::PathgridCheckStage::perform(int stage, CSMDoc::Messages& messages)
{
const CSMWorld::Record<CSMWorld::Pathgrid>& record = mPathgrids.getRecord(stage);
// Skip "Base" records (setting!) and "Deleted" records
if ((mIgnoreBaseRecords && record.mState == CSMWorld::RecordBase::State_BaseOnly) || record.isDeleted())
return;
const CSMWorld::Pathgrid& pathgrid = record.get();
CSMWorld::UniversalId id(CSMWorld::UniversalId::Type_Pathgrid, pathgrid.mId);
// check the number of pathgrid points
if (pathgrid.mData.mPoints < pathgrid.mPoints.size())
messages.add(id, "Less points than expected", "", CSMDoc::Message::Severity_Error);
else if (pathgrid.mData.mPoints > pathgrid.mPoints.size())
messages.add(id, "More points than expected", "", CSMDoc::Message::Severity_Error);
std::vector<CSMTools::Point> pointList(pathgrid.mPoints.size());
std::vector<size_t> duplList;
for (const auto& edge : pathgrid.mEdges)
{
if (edge.mV0 < pathgrid.mPoints.size())
{
auto& point = pointList[edge.mV0];
point.mConnectionNum++;
// first check for duplicate edges
size_t j = 0;
for (; j < point.mOtherIndex.size(); ++j)
{
if (point.mOtherIndex[j] == edge.mV1)
{
std::ostringstream ss;
ss << "Duplicate edge between points #" << edge.mV0 << " and #" << edge.mV1;
messages.add(id, ss.str(), {}, CSMDoc::Message::Severity_Error);
break;
}
}
// only add if not a duplicate
if (j == point.mOtherIndex.size())
point.mOtherIndex.push_back(edge.mV1);
}
else
{
std::ostringstream ss;
ss << "An edge is connected to a non-existent point #" << edge.mV0;
messages.add(id, ss.str(), {}, CSMDoc::Message::Severity_Error);
}
}
for (size_t i = 0; i < pathgrid.mPoints.size(); ++i)
{
// check that edges are bidirectional
bool foundReverse = false;
for (const auto& otherIndex : pointList[i].mOtherIndex)
{
for (const auto& other : pointList[otherIndex].mOtherIndex)
{
if (other == i)
{
foundReverse = true;
break;
}
}
if (!foundReverse)
{
std::ostringstream ss;
ss << "Missing edge between points #" << i << " and #" << otherIndex;
messages.add(id, ss.str(), {}, CSMDoc::Message::Severity_Error);
}
}
// check duplicate points
// FIXME: how to do this efficiently?
for (size_t j = 0; j != i; ++j)
{
if (pathgrid.mPoints[i].mX == pathgrid.mPoints[j].mX && pathgrid.mPoints[i].mY == pathgrid.mPoints[j].mY
&& pathgrid.mPoints[i].mZ == pathgrid.mPoints[j].mZ)
{
auto it = std::find(duplList.begin(), duplList.end(), i);
if (it == duplList.end())
{
std::ostringstream ss;
ss << "Point #" << i << " duplicates point #" << j << " (" << pathgrid.mPoints[i].mX << ", "
<< pathgrid.mPoints[i].mY << ", " << pathgrid.mPoints[i].mZ << ")";
messages.add(id, ss.str(), {}, CSMDoc::Message::Severity_Warning);
duplList.push_back(i);
break;
}
}
}
}
// check pathgrid points that are not connected to anything
for (size_t i = 0; i < pointList.size(); ++i)
{
if (pointList[i].mConnectionNum == 0)
{
std::ostringstream ss;
ss << "Point #" << i << " (" << pathgrid.mPoints[i].mX << ", " << pathgrid.mPoints[i].mY << ", "
<< pathgrid.mPoints[i].mZ << ") is disconnected from other points";
messages.add(id, ss.str(), {}, CSMDoc::Message::Severity_Warning);
}
}
// TODO: check whether there are disconnected graphs
}
|