Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const getters for hrleSparseStarIterator #15

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions Tests/OffsetIterator/OffsetIterator.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <fstream>
#include <iostream>

#include <hrleGrid.hpp>
#include <hrleTestAsserts.hpp>
#include <hrleDenseIterator.hpp>
#include <hrleDomain.hpp>
#include <hrleFillDomainFromPointList.hpp>
#include <hrleDenseIterator.hpp>
#include <hrleGrid.hpp>
#include <hrleSparseBoxIterator.hpp>
#include <hrleSparseIterator.hpp>
#include <hrleTestAsserts.hpp>

constexpr int D = 2;
using DataType = char;
Expand Down Expand Up @@ -40,9 +40,10 @@ void fillDomain(hrleDomain<DataType, D> &domain) {
hrleVectorType<hrleIndexType, D> index;

// write an 'x' at all points on the circle
for(index[1] = -circleExtent; index[1] <= circleExtent; ++index[1]) {
for(index[0] = gridMin[0]; index[0] <= gridMax[0]; ++index[0]) {
if(std::abs(double(index[0]*index[0]) + double(index[1]*index[1]) - radius2) < 40.) {
for (index[1] = -circleExtent; index[1] <= circleExtent; ++index[1]) {
for (index[0] = gridMin[0]; index[0] <= gridMax[0]; ++index[0]) {
if (std::abs(double(index[0] * index[0]) + double(index[1] * index[1]) -
radius2) < 40.) {
pointData.push_back(std::make_pair(index, 'x'));
}
}
Expand All @@ -53,33 +54,36 @@ void fillDomain(hrleDomain<DataType, D> &domain) {
'.'); // last parameter is the background value to use
}

bool checkOffset(hrleGrid<D> &grid, VectorType coord, VectorType nbor, VectorType offset) {
bool checkOffset(hrleGrid<D> &grid, VectorType coord, VectorType nbor,
VectorType offset) {
VectorType realOffset;
auto gridMin = grid.getMinGridPoint();
auto gridMax = grid.getMaxGridPoint();
coord += offset;
for(unsigned i = 0; i < D; ++i) {
for (unsigned i = 0; i < D; ++i) {
// if neighbour is outside of domain
if(coord[i] > gridMax[i]) {
if(grid.isBoundaryReflective(i)) {
if (coord[i] > gridMax[i]) {
if (grid.isBoundaryReflective(i)) {
// reflect
coord[i] = gridMax[i] - (coord[i] - gridMax[i]);
} else {
// come in from other side
coord[i] = gridMin[i] + (coord[i] - gridMax[i] - 1);
}
} else if(coord[i] < gridMin[i]) {
if(grid.isBoundaryReflective(i)) {
} else if (coord[i] < gridMin[i]) {
if (grid.isBoundaryReflective(i)) {
// reflect
coord[i] = gridMin[i] + (gridMin[i] - coord[i]);
} else {
// come in from other side
coord[i] = gridMax[i] + (coord[i] - gridMin[i] + 1);
}
}
}

if(coord[i] != nbor[i]) {
std::cout << "ERROR: Wrong offset: index=" << coord << "; neighbor=" << nbor << "; correct offset=" << offset[i] << std::endl;
if (coord[i] != nbor[i]) {
std::cout << "ERROR: Wrong offset: index=" << coord
<< "; neighbor=" << nbor << "; correct offset=" << offset[i]
<< std::endl;
return false;
}
}
Expand All @@ -96,16 +100,18 @@ void runTest(hrleDomain<DataType, D> &domain) {
hrleConstSparseBoxIterator<hrleDomain<char, D>> it(domain, iteratorOrder);
const unsigned numNeighbors = unsigned(std::pow((1 + 2 * iteratorOrder), D));

for(; !it.isFinished(); ++it) {
if(!it.getCenter().isDefined()) {
for (; !it.isFinished(); ++it) {
if (!it.getCenter().isDefined()) {
continue;
}
for(unsigned i = 0; i < numNeighbors; ++i) {
for (unsigned i = 0; i < numNeighbors; ++i) {
const auto &neighbor = it.getNeighbor(i);
if(!neighbor.isDefined()) {
if (!neighbor.isDefined()) {
continue;
}
HRLETEST_ASSERT(checkOffset(grid, it.getIndices(), it.getNeighbor(i).getOffsetIndices(), it.getNeighbor(i).getOffset()));
HRLETEST_ASSERT(checkOffset(grid, it.getIndices(),
it.getNeighbor(i).getOffsetIndices(),
it.getNeighbor(i).getOffset()));
}
}
}
Expand All @@ -119,8 +125,7 @@ int main() {
// Test for periodic boundary condition
{
std::array<hrleGrid<D>::boundaryType, D> bounds = {
hrleGrid<D>::PERIODIC_BOUNDARY,
hrleGrid<D>::INFINITE_BOUNDARY};
hrleGrid<D>::PERIODIC_BOUNDARY, hrleGrid<D>::INFINITE_BOUNDARY};

hrleGrid<D> grid(min.data(), max.data(), 1.0, bounds.data());
hrleDomain<DataType, D> domain(&grid);
Expand All @@ -132,18 +137,14 @@ int main() {
// Test for reflective boundary condition
{
std::array<hrleGrid<D>::boundaryType, D> bounds = {
hrleGrid<D>::REFLECTIVE_BOUNDARY,
hrleGrid<D>::INFINITE_BOUNDARY};
hrleGrid<D>::REFLECTIVE_BOUNDARY, hrleGrid<D>::INFINITE_BOUNDARY};

hrleGrid<D> grid(min.data(), max.data(), 1.0, bounds.data());
hrleDomain<DataType, D> domain(&grid);

runTest(domain);
std::cout << "Passed reflective" << std::endl;
}




return 0;
}
8 changes: 4 additions & 4 deletions include/hrleSparseOffsetIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,16 +830,16 @@ class hrleSparseOffsetIterator : public hrleBaseIterator<hrleDomain> {
indices[i] += offset[i];
if (indices[i] > gridMax[i]) {
const auto lengthDiff = indices[i] - gridMax[i];
if(grid.isBoundaryReflective(i)){
if (grid.isBoundaryReflective(i)) {
indices[i] = gridMax[i] - lengthDiff;
}else{
} else {
indices[i] = gridMin[i] + (lengthDiff - 1);
}
} else if (indices[i] < gridMin[i]) {
const auto lengthDiff = indices[i] - gridMin[i];
if(grid.isBoundaryReflective(i)){
if (grid.isBoundaryReflective(i)) {
indices[i] = gridMin[i] - lengthDiff;
}else{
} else {
indices[i] = gridMax[i] + (lengthDiff + 1);
}
}
Expand Down
34 changes: 28 additions & 6 deletions include/hrleSparseStarIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ template <class hrleDomain> class hrleSparseStarIterator {

public:
using DomainType = hrleDomain;
using OffsetIterator = hrleSparseOffsetIterator<hrleDomain>;

hrleSparseStarIterator(hrleDomain &passedDomain,
const hrleVectorType<hrleIndexType, D> &v,
Expand Down Expand Up @@ -144,16 +145,25 @@ template <class hrleDomain> class hrleSparseStarIterator {
currentCoords = domain.getGrid().decrementIndices(start_coords);
}

hrleSparseOffsetIterator<hrleDomain> &getNeighbor(unsigned index) {
const OffsetIterator &getNeighbor(unsigned index) const {
return neighborIterators[index];
}

hrleSparseOffsetIterator<hrleDomain> &getNeighbor(int index) {
OffsetIterator &getNeighbor(unsigned index) {
return const_cast<OffsetIterator &>(
const_cast<const hrleSparseStarIterator *>(this)->getNeighbor(index));
}

const OffsetIterator &getNeighbor(int index) const {
return neighborIterators[index];
}

template <class V>
hrleSparseOffsetIterator<hrleDomain> &getNeighbor(V &relativeIndex) {
OffsetIterator &getNeighbor(int index) {
return const_cast<OffsetIterator &>(
const_cast<const hrleSparseStarIterator *>(this)->getNeighbor(index));
}

template <class V> const OffsetIterator &getNeighbor(V &relativeIndex) const {
// check first if it is a valid index
unsigned char directions = 0;
unsigned neighborIndex;
Expand All @@ -172,11 +182,23 @@ template <class hrleDomain> class hrleSparseStarIterator {
return neighborIterators[neighborIndex];
}

template <class V> OffsetIterator &getNeighbor(V &relativeIndex) {
return const_cast<OffsetIterator &>(
const_cast<const hrleSparseStarIterator *>(this)->getNeighbor(
relativeIndex));
}

hrleSparseIterator<hrleDomain> &getCenter() { return centerIterator; }

const hrleVectorType<hrleIndexType, D> &getIndices() { return currentCoords; }
const hrleSparseIterator<hrleDomain> &getCenter() const {
return centerIterator;
}

const hrleVectorType<hrleIndexType, D> &getIndices() const {
return currentCoords;
}

const DomainType &getDomain() { return domain; }
const DomainType &getDomain() const { return domain; }

bool isFinished() const { return centerIterator.isFinished(); }

Expand Down