Skip to content

Commit

Permalink
Fixes the occasional black hex seen in schnakenberg by enforcing limi…
Browse files Browse the repository at this point in the history
…ts on the range of the input to ColourMap::convert()
  • Loading branch information
Seb James committed Sep 15, 2020
1 parent 9713481 commit 2e03538
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
13 changes: 9 additions & 4 deletions morph/ColourMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ namespace morph {
//! Convert the scalar datum into an RGB (or BGR) colour
std::array<float, 3> convert (Flt datum) {
std::array<float, 3> c = {0.0f, 0.0f, 0.0f};

// Enforce range of datum
datum = datum > Flt{1.0} ? Flt{1.0} : datum;
datum = datum < Flt{0.0} ? Flt{0.0} : datum;

switch (this->type) {
case ColourMapType::Jet:
{
Expand All @@ -65,14 +70,14 @@ namespace morph {
}
case ColourMapType::RainbowZeroBlack:
{
if (datum != static_cast<Flt>(0.0)) {
if (datum != Flt{0.0}) {
c = ColourMap::rainbow (datum);
}
break;
}
case ColourMapType::RainbowZeroWhite:
{
if (datum != static_cast<Flt>(0.0)) {
if (datum != Flt{0.0}) {
c = ColourMap::rainbow (datum);
} else {
c = {1.0f, 1.0f, 1.0f};
Expand Down Expand Up @@ -251,8 +256,8 @@ namespace morph {
std::array<float,3> col = {0.0f, 0.0f, 0.0f};
float ivl = 1.0/8.0;
for (int i=0; i<8; i++) {
Flt llim = (i==0) ? static_cast<Flt>(0.0) : (Flt)i/static_cast<Flt>(8.0);
Flt ulim = (i==7) ? static_cast<Flt>(1.0) : ((Flt)(i+1))/static_cast<Flt>(8.0);
Flt llim = (i==0) ? Flt{0.0} : (Flt)i/Flt{8.0};
Flt ulim = (i==7) ? Flt{1.0} : ((Flt)(i+1))/Flt{8.0};
if (datum >= llim && datum <= ulim) {
for (int j=0; j<3; j++) {
float c = static_cast<float>(datum - llim);
Expand Down
21 changes: 9 additions & 12 deletions morph/HexGridVisual.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ namespace morph {
// this->initializeVerticesTris();
}

//! Initialize vertex buffer objects and vertex array object.
//@{
// Initialize vertex buffer objects and vertex array object.

//! Initialize as triangled. Gives a smooth surface with much
//! less compute than initializeVerticesHexesInterpolated.
void initializeVerticesTris (void) {
Expand All @@ -233,8 +233,6 @@ namespace morph {
Flt datumC = this->zScale.transform ((*this->data)[hi]);
// Scale colour
Flt datum = this->colourScale.transform ((*this->data)[hi]);
//shouldn't need: datum = datum > static_cast<Flt>(1.0) ? static_cast<Flt>(1.0) : datum;
//datum = datum < static_cast<Flt>(0.0) ? static_cast<Flt>(0.0) : datum;
// And turn it into a colour:
std::array<float, 3> clr = this->cm.convert (datum);
this->vertex_push (this->hg->d_x[hi], this->hg->d_y[hi], datumC, this->vertexPositions);
Expand Down Expand Up @@ -290,13 +288,13 @@ namespace morph {
for (unsigned int hi = 0; hi < nhex; ++hi) {

// Use the linear scaled copy of the data, dcopy.
datumC = dcopy[hi];
datumNE = dcopy[NE(hi)]; // datum Neighbour East
datumNNE = dcopy[NNE(hi)]; // datum Neighbour North East
datumNNW = dcopy[NNW(hi)]; // etc
datumNW = dcopy[NW(hi)];
datumNSW = dcopy[NSW(hi)];
datumNSE = dcopy[NSE(hi)];
datumC = dcopy[hi];
datumNE = HAS_NE(hi) ? dcopy[NE(hi)] : datumC; // datum Neighbour East
datumNNE = HAS_NNE(hi) ? dcopy[NNE(hi)] : datumC; // datum Neighbour North East
datumNNW = HAS_NNW(hi) ? dcopy[NNW(hi)] : datumC; // etc
datumNW = HAS_NW(hi) ? dcopy[NW(hi)] : datumC;
datumNSW = HAS_NSW(hi) ? dcopy[NSW(hi)] : datumC;
datumNSE = HAS_NSE(hi) ? dcopy[NSE(hi)] : datumC;

// Use a single colour for each hex, even though hex z positions are
// interpolated. Do the _colour_ scaling:
Expand Down Expand Up @@ -440,7 +438,6 @@ namespace morph {
//! Initialize as hexes, with a step quad between each
//! hex. Might look cool. Writeme.
void initializeVerticesHexesStepped (void) {}
//@}

private:
//! The HexGrid to visualize
Expand Down

0 comments on commit 2e03538

Please sign in to comment.