Skip to content

Commit ecd5b2e

Browse files
committed
Better be safe
1 parent 6b5681a commit ecd5b2e

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

src/ScatterplotPlugin.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <algorithm>
3333
#include <cassert>
34+
#include <exception>
3435
#include <map>
3536
#include <optional>
3637
#include <ranges>
@@ -738,44 +739,56 @@ void ScatterplotPlugin::loadColors(const Dataset<Points>& pointsColor, const std
738739
// If number of points do not match, prefer checking for derived data over selection mapping
739740
if (numColorPoints != _numPoints) {
740741

741-
const bool hasSameNumPointsAsFull =
742-
/*if*/ _positionDataset->isDerivedData() ?
743-
/*then*/ _positionSourceDataset->getFullDataset<Points>()->getNumPoints() == numColorPoints :
744-
/*else*/ false;
742+
try {
745743

746-
if (hasSameNumPointsAsFull) {
747-
std::vector<std::uint32_t> globalIndices;
748-
_positionDataset->getGlobalIndices(globalIndices);
744+
const bool hasSameNumPointsAsFull =
745+
/*if*/ _positionDataset->isDerivedData() ?
746+
/*then*/ _positionSourceDataset->getFullDataset<Points>()->getNumPoints() == numColorPoints :
747+
/*else*/ false;
749748

750-
std::vector<float> localScalars(_numPoints, 0);
751-
std::int32_t localColorIndex = 0;
749+
if (hasSameNumPointsAsFull) {
750+
std::vector<std::uint32_t> globalIndices;
751+
_positionDataset->getGlobalIndices(globalIndices);
752752

753-
for (const auto& globalIndex : globalIndices)
754-
localScalars[localColorIndex++] = scalars[globalIndex];
753+
std::vector<float> localScalars(_numPoints, 0);
754+
std::int32_t localColorIndex = 0;
755755

756-
std::swap(localScalars, scalars);
757-
}
758-
else if ( // only get map if derived check failed
759-
const auto selectionMapping = getSelectionMapping(pointsColor, _positionDataset);
760-
/* check if valid */ selectionMapping.has_value() && selectionMapping.value() != nullptr
761-
)
762-
{
763-
std::vector<float> localScalars(_numPoints, 0);
756+
for (const auto& globalIndex : globalIndices)
757+
localScalars[localColorIndex++] = scalars[globalIndex];
758+
759+
std::swap(localScalars, scalars);
760+
}
761+
else if ( // only get map if derived check failed
762+
const auto selectionMapping = getSelectionMapping(pointsColor, _positionDataset);
763+
/* check if valid */ selectionMapping.has_value() && selectionMapping.value() != nullptr
764+
)
765+
{
766+
std::vector<float> localScalars(_numPoints, 0);
764767

765-
// Map values like selection
766-
const mv::SelectionMap::Map& linkedMap = selectionMapping.value()->getMapping().getMap();
767-
const std::uint32_t numPointsInTarget = _positionDataset->getNumPoints();
768+
// Map values like selection
769+
const mv::SelectionMap::Map& linkedMap = selectionMapping.value()->getMapping().getMap();
770+
const std::uint32_t numPointsInTarget = _positionDataset->getNumPoints();
768771

769-
for (const auto& [fromID, vecOfIDs] : linkedMap) {
770-
for (std::uint32_t toID : vecOfIDs) {
771-
localScalars[toID] = scalars[fromID];
772+
for (const auto& [fromID, vecOfIDs] : linkedMap) {
773+
for (std::uint32_t toID : vecOfIDs) {
774+
localScalars[toID] = scalars[fromID];
775+
}
772776
}
777+
778+
std::swap(localScalars, scalars);
779+
}
780+
else {
781+
qWarning("Number of points used for coloring does not match number of points in data, aborting attempt to color plot");
782+
return;
773783
}
774784

775-
std::swap(localScalars, scalars);
776785
}
777-
else {
778-
qWarning("Number of points used for coloring does not match number of points in data, aborting attempt to color plot");
786+
catch (const std::exception& e) {
787+
qDebug() << "ScatterplotPlugin::loadColors: mapping failed -> " << e.what();
788+
return;
789+
}
790+
catch (...) {
791+
qDebug() << "ScatterplotPlugin::loadColors: mapping failed for an unknown reason.";
779792
return;
780793
}
781794
}

0 commit comments

Comments
 (0)