Skip to content

Commit

Permalink
Fixed grid deserialization bug. Format project.
Browse files Browse the repository at this point in the history
Fixed bug when reading negative numbers in gridExtent
  • Loading branch information
tobre1 authored Aug 3, 2021
2 parents 1b4e367 + 69f724a commit f7ce998
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Tests/gridSerialisation/gridSerialisation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main() {
constexpr int D = 2;
using BNCType = typename hrleGrid<D>::boundaryType;

hrleIndexType min[D] = {-10, -10};
hrleIndexType min[D] = {-10000000, -100000000};
hrleIndexType max[D] = {10, 10};
const double gridDelta = 1.0;
BNCType boundaryCons[D];
Expand All @@ -21,6 +21,9 @@ int main() {
// Initialise example grid
auto grid = hrleGrid<D>(min, max, gridDelta, boundaryCons);

// grid.print();
// std::cout << "\n\n" << std::flush;

// grid.print();

// Open file for writing and save serialized level set in it
Expand All @@ -39,6 +42,8 @@ int main() {

fin.close();

// newGrid.print();

HRLETEST_ASSERT(grid == newGrid)

return 0;
Expand Down
2 changes: 1 addition & 1 deletion include/hrleCartesianPlaneIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ template <class hrleDomain> class hrleCartesianPlaneIterator {

unsigned getSize() { return planeCoords.size(); }

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

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

Expand Down
2 changes: 1 addition & 1 deletion include/hrleDenseCellIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ template <class hrleDomain> class hrleDenseCellIterator {

const hrleIndexType &getIndices(unsigned i) { return currentCoords[i]; }

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

bool isFinished() const {
if (compare(currentCoords, maxIndex) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion include/hrleDenseIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ template <class hrleDomain> class hrleDenseIterator {
return runsIterator.getStartIndices();
}

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

void print() {
std::cout << currentIndices << std::endl;
Expand Down
36 changes: 25 additions & 11 deletions include/hrleGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,32 @@ template <int D> class hrleGrid {
// increasing and true if the grid_position function is
// strictly monotonic decreasing

static char getByteSizeOfNumber(int number) {
static char getBitSizeOfNumber(long number) {
const bool isNegative = number < 0;
number = std::abs(number);
char size = 0;
char bitSize = 0;
while (number != 0) {
number >>= 8;
++size;
number >>= 1;
++bitSize;
}
return size;
return isNegative ? bitSize + 1 : bitSize;
}

static char getByteSizeOfNumber(long number) {
auto bitSize = getBitSizeOfNumber(number);
return bitSize / 8 + (bitSize % 8 != 0);
}

hrleIndexType readGridBoundary(std::istream &stream, char gridBoundaryBytes) {
char number[sizeof(hrleIndexType)] = {};
for (unsigned i = 0; i < gridBoundaryBytes; ++i)
stream.read(number + i, 1);

if (number[gridBoundaryBytes - 1] >> 7) {
for (unsigned i = gridBoundaryBytes; i < sizeof(hrleIndexType); ++i)
number[i] = 0xFF;
}
return *reinterpret_cast<hrleIndexType *>(number);
}

public:
Expand Down Expand Up @@ -652,20 +670,16 @@ template <int D> class hrleGrid {

char gridBoundaryBytes = 0;
stream.read(&gridBoundaryBytes, 1);

// READ GRID
hrleIndexType gridMin[D], gridMax[D];
typename hrleGrid<D>::boundaryType boundaryCons[D];
double gridDelta = 0.;
for (int i = D - 1; i >= 0; --i) {
gridMin[i] = readGridBoundary(stream, gridBoundaryBytes);
gridMax[i] = readGridBoundary(stream, gridBoundaryBytes);
char condition = 0;
char min, max;
stream.read(&min, gridBoundaryBytes);
stream.read(&max, gridBoundaryBytes);
stream.read(&condition, 1);
boundaryCons[i] = boundaryType(condition);
gridMin[i] = hrleIndexType(min);
gridMax[i] = hrleIndexType(max);
}
stream.read((char *)&gridDelta, sizeof(double));
// initialize new grid
Expand Down
2 changes: 1 addition & 1 deletion include/hrleSparseBoxIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ template <class hrleDomain> class hrleSparseBoxIterator {

unsigned getSize() { return neighborIterators.size(); }

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

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

Expand Down
2 changes: 1 addition & 1 deletion include/hrleSparseCellIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ template <class hrleDomain> class hrleSparseCellIterator {

const hrleIndexType &getIndices(unsigned i) { return currentCoords[i]; }

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

bool isFinished() const { return cornerIterators[0].isFinished(); }
};
Expand Down
6 changes: 3 additions & 3 deletions include/hrleSparseMultiIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
/// points are defined at one index (i.e.: the point is defined in more
/// than one domain), it will only stop once.
template <class hrleDomain> class hrleSparseMultiIterator {
public:
public:
using DomainType = hrleDomain;
using DomainsType = std::vector<DomainType *>;
private:

private:
typedef typename std::conditional<std::is_const<hrleDomain>::value,
const typename hrleDomain::hrleValueType,
typename hrleDomain::hrleValueType>::type
Expand Down Expand Up @@ -190,7 +190,7 @@ template <class hrleDomain> class hrleSparseMultiIterator {
return definedIterators;
}

const DomainsType& getDomains() { return domains; }
const DomainsType &getDomains() { return domains; }

/// If all iterators in all domains are finished, this will return true.
bool isFinished() const {
Expand Down
2 changes: 1 addition & 1 deletion include/hrleSparseStarIterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ template <class hrleDomain> class hrleSparseStarIterator {

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

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

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

Expand Down

0 comments on commit f7ce998

Please sign in to comment.