205 if (!checkAndCalculateBounds()) {
207 sumSquaredDifferences = std::numeric_limits<T>::quiet_NaN();
212 const auto &gridTarget = levelSetTarget->getGrid();
213 double gridDelta = gridTarget.getGridDelta();
216 viennahrle::ConstDenseCellIterator<typename Domain<T, D>::DomainType>
217 itSample(levelSetSample->getDomain(), minIndex);
218 viennahrle::ConstDenseCellIterator<typename Domain<T, D>::DomainType>
219 itTarget(levelSetTarget->getDomain(), minIndex);
221 sumSquaredDifferences = 0.0;
225 std::unordered_map<viennahrle::Index<D>, size_t,
226 typename viennahrle::Index<D>::hash>
228 std::vector<T> differenceValues;
229 size_t currentPointId = 0;
231 const bool generateMesh = outputMesh !=
nullptr;
236 for (
unsigned i = 0; i <
D; ++i) {
237 outputMesh->minimumExtent[i] = std::numeric_limits<T>::max();
238 outputMesh->maximumExtent[i] = std::numeric_limits<T>::lowest();
243 for (; itSample.getIndices() < maxIndex; itSample.next()) {
245 T xCoord = itSample.getIndices()[0] * gridDelta;
246 T yCoord = (
D > 1) ? itSample.getIndices()[1] * gridDelta : 0;
249 if (useXRange && (xCoord < xRangeMin || xCoord > xRangeMax)) {
254 if (
D > 1 && useYRange && (yCoord < yRangeMin || yCoord > yRangeMax)) {
259 itTarget.goToIndicesSequential(itSample.getIndices());
266 for (
int i = 0; i < (1 <<
D); ++i) {
267 valueSample += itSample.getCorner(i).getValue();
268 valueTarget += itTarget.getCorner(i).getValue();
270 valueTarget /= (1 <<
D);
271 valueSample /= (1 <<
D);
274 if (std::isinf(valueTarget) || std::isinf(valueSample) ||
275 std::abs(valueTarget) > 1000 || std::abs(valueSample) > 1000) {
280 T diff = std::abs(valueTarget - valueSample) * gridDelta;
281 T diffSquared = diff * diff;
282 sumSquaredDifferences += diffSquared;
283 sumDifferences += diff;
288 std::array<unsigned, 1 << D> voxel;
289 bool addVoxel =
true;
292 for (
unsigned i = 0; i < (1 <<
D); ++i) {
293 viennahrle::Index<D> index;
294 for (
unsigned j = 0; j <
D; ++j) {
296 itSample.getIndices(j) + itSample.getCorner(i).getOffset()[j];
297 if (index[j] > maxIndex[j]) {
303 auto pointIdValue = std::make_pair(index, currentPointId);
304 auto pointIdPair = pointIdMapping.insert(pointIdValue);
305 voxel[i] = pointIdPair.first->second;
306 if (pointIdPair.second) {
315 if constexpr (
D == 3) {
316 std::array<unsigned, 8> hexa{voxel[0], voxel[1], voxel[3],
317 voxel[2], voxel[4], voxel[5],
319 outputMesh->hexas.push_back(hexa);
320 }
else if constexpr (
D == 2) {
321 std::array<unsigned, 4> quad{voxel[0], voxel[1], voxel[3],
323 outputMesh->tetras.push_back(quad);
328 differenceValues.push_back(outputMeshSquaredDifferences ? diffSquared
335 if (generateMesh && !pointIdMapping.empty()) {
336 outputMesh->nodes.resize(pointIdMapping.size());
337 for (
auto it = pointIdMapping.begin(); it != pointIdMapping.end(); ++it) {
338 std::array<T, 3> coords{};
339 for (
unsigned i = 0; i <
D; ++i) {
340 coords[i] = gridDelta * it->first[i];
342 if (coords[i] < outputMesh->minimumExtent[i]) {
343 outputMesh->minimumExtent[i] = coords[i];
344 }
else if (coords[i] > outputMesh->maximumExtent[i]) {
345 outputMesh->maximumExtent[i] = coords[i];
348 outputMesh->nodes[it->second] = coords;
351 assert(differenceValues.size() ==
352 outputMesh->template getElements<1 <<
D>().size());
353 outputMesh->cellData.insertNextScalarData(std::move(differenceValues),
354 outputMeshSquaredDifferences
355 ?
"Squared differences"
356 :
"Absolute differences");