Skip to content

Commit

Permalink
Merge pull request #270 from ABRG-Models/colourmap_browser
Browse files Browse the repository at this point in the history
Adds a Colourmap browser
  • Loading branch information
sebjameswml authored Oct 17, 2024
2 parents e6c856b + a9f96a0 commit a4f3414
Show file tree
Hide file tree
Showing 7 changed files with 722 additions and 13 deletions.
18 changes: 15 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,16 @@ if(ARMADILLO_FOUND)
target_link_libraries(grid_simple GLEW::GLEW)
endif()

add_executable(colourbar_test colourbar_test.cpp)
target_link_libraries(colourbar_test OpenGL::GL glfw Freetype::Freetype)
add_executable(colourmap_test colourmap_test.cpp)
target_link_libraries(colourmap_test OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
target_link_libraries(colourbar_test GLEW::GLEW)
target_link_libraries(colourmap_test GLEW::GLEW)
endif()

add_executable(colourmap_browser colourmap_browser.cpp)
target_link_libraries(colourmap_browser OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
target_link_libraries(colourmap_browser GLEW::GLEW)
endif()

# grid_simple, but orthographic view is the default
Expand Down Expand Up @@ -202,6 +208,12 @@ if(ARMADILLO_FOUND)
target_link_libraries(hsvwheel GLEW::GLEW)
endif()

add_executable(cyclic_colour cyclic_colour.cpp)
target_link_libraries(cyclic_colour OpenGL::GL glfw Freetype::Freetype)
if(USE_GLEW)
target_link_libraries(cyclic_colour GLEW::GLEW)
endif()

# ${ARMADILLO_LIBRARY} ${ARMADILLO_LIBRARIES}
add_executable(hexgrid hexgrid.cpp)
target_link_libraries(hexgrid OpenGL::GL glfw Freetype::Freetype)
Expand Down
115 changes: 115 additions & 0 deletions examples/colourmap_browser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Make a colourbar tester using a morph::Grid/GridVisual
*
* This shows a min to max gradient of a ColourMap, with a decaying sine wave added to
* the signal. Poor colour maps like Jet show structure in the features that is not part
* of the data.
*/

#include <iostream>
#include <vector>
#include <cmath>

#include <morph/Scale.h>
#include <morph/vec.h>
#include <morph/Visual.h>
#include <morph/VisualDataModel.h>
#include <morph/GridVisual.h>
#include <morph/Grid.h>
#include <morph/CyclicColourVisual.h>

struct myvisual final : public morph::Visual<>
{
myvisual (int width, int height, const std::string& title) : morph::Visual<> (width, height, title) {}
morph::ColourMapType curr_map_type = morph::ColourMapType::Plasma;
bool forwards = true;
protected:
void key_callback_extra (int key, [[maybe_unused]] int scancode, int action, [[maybe_unused]] int mods) override
{
if (key == morph::key::right && (action == morph::keyaction::press || action == morph::keyaction::repeat)) {
++this->curr_map_type;
this->forwards = true;
}
if (key == morph::key::left && (action == morph::keyaction::press || action == morph::keyaction::repeat)) {
--this->curr_map_type;
this->forwards = false;
}
if (key == morph::key::h && action == morph::keyaction::press) { std::cout << "left/right switch maps\n"; }
}
};

morph::VisualModel<>* addmap (myvisual& v, morph::ColourMapType display_map_type, const morph::Grid<>& grid, const std::vector<float>& data)
{
morph::VisualModel<>* vmp = nullptr;
morph::ColourMap<float> nextmap (display_map_type);
if ((nextmap.flags & morph::ColourMapFlags::cyclic) == true) {
morph::vec<float, 3> offset = {0,0,0};
auto cv = std::make_unique<morph::CyclicColourVisual<float>>(offset);
v.bindmodel (cv);
cv->outer_radius = 0.6;
cv->inner_radius = 0.2;
cv->cm = nextmap;
cv->draw_ticks = false;
cv->addLabel (cv->cm.getTypeStr() + std::string(" (") + cv->cm.getFlagsStr() + std::string(")"),
morph::vec<float>({-1.3, -0.4, 0}), morph::TextFeatures(0.05f));
cv->finalize();
vmp = v.addVisualModel (cv);
} else {
morph::vec<float, 3> offset = { -0.5f * grid.width(), -0.5f * grid.height(), 0.0f };
auto gv = std::make_unique<morph::GridVisual<float>>(&grid, offset);
v.bindmodel (gv);
gv->gridVisMode = morph::GridVisMode::Triangles;
gv->setScalarData (&data);
gv->cm = nextmap;
gv->zScale.setParams (0, 0);
gv->addLabel (gv->cm.getTypeStr() + std::string(" (") + gv->cm.getFlagsStr() + std::string(")"),
morph::vec<float>({0,-0.1,0}), morph::TextFeatures(0.05f));
gv->finalize();
vmp = v.addVisualModel (gv);
}
return vmp;
}

int main()
{
myvisual v(2100, 1100, "Colourbar perceptual uniformity test");
v.setSceneTrans (morph::vec<float,3>{ float{-0.00636619}, float{0.0518834}, float{-3} });

// Create a grid for the colourmaps
constexpr unsigned int Nside_w = 512;
constexpr unsigned int Nside_h = 256;
constexpr float barw = 2.56f;
constexpr float barh = 0.5f;
constexpr morph::vec<float, 2> grid_spacing = {barw/static_cast<float>(Nside_w), barh/static_cast<float>(Nside_h)};
morph::Grid grid(Nside_w, Nside_h, grid_spacing);

// Our data is a ramp with a sine wave embossed on it
std::vector<float> data(grid.n, 0.0);
for (unsigned int ri=0; ri<grid.n; ++ri) {
auto x = grid[ri][0];
auto y = grid[ri][1];
data[ri] = x / grid.width() + 0.1f * (y / grid.height()) * (y / grid.height()) * std::sin (120.0f * x);
}

morph::ColourMapType display_map_type = v.curr_map_type;
morph::VisualModel<>* gvp = addmap (v, v.curr_map_type, grid, data);

while (v.readyToFinish == false) {
v.render();
v.waitevents (0.017);
if (v.curr_map_type != display_map_type) {
// Change to v.curr_map_type
morph::ColourMap<float> nextmap(v.curr_map_type);
if ((nextmap.flags & morph::ColourMapFlags::one_d) == true) {
// Update the map
v.removeVisualModel (gvp);
gvp = addmap (v, v.curr_map_type, grid, data);
} else {
// The map wasn't 1D, so skip
if (v.forwards) { ++v.curr_map_type; } else { --v.curr_map_type; }
}
}
}

return 0;
}
File renamed without changes.
36 changes: 36 additions & 0 deletions examples/cyclic_colour.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Use cartgridvisuals to illustrate use of ColourMapType::HSV colourmap.
*/

#include <iostream>
#include <vector>
#include <cmath>

#include <morph/Visual.h>
#include <morph/ColourMap.h>
#include <morph/CyclicColourVisual.h>

int main()
{
// The main function is simple. Create a morph::Visual, add a single SquareGridVisual and then 'keep it open'
std::string titlestr = "Cyclic colour map";
morph::Visual v(1600, 1000, titlestr, {-0.8,-0.8}, {.05,.05,.05}, 2.0f, 0.0f);
v.backgroundBlack();

morph::vec<float, 3> offset = { 0.0f, 0.0f, 0.0f };

// TextFeatures is a nice way to specify font size, colour (and other things) for your addLabel() calls.
morph::TextFeatures tf (0.5f, morph::colour::white);

// HSVWHeel for Grid1
morph::vec<float, 3> woffset = offset;
auto hsvw_vis = std::make_unique<morph::CyclicColourVisual<float>>(woffset);
v.bindmodel (hsvw_vis);
hsvw_vis->setColour (morph::colour::white);
hsvw_vis->cm = morph::ColourMapType::CET_C6;// CET_C6
hsvw_vis->finalize();
v.addVisualModel (hsvw_vis);

v.keepOpen();
return 0;
}
2 changes: 1 addition & 1 deletion examples/myvisual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct myvisual final : public morph::Visual<my_gl_version>
}
// Also optionally, add actions for extra keys:
static constexpr bool debug_callback_extra = false;
void key_callback_extra (int key, int scancode, int action, int mods) override
void key_callback_extra (int key, [[maybe_unused]] int scancode, int action, [[maybe_unused]] int mods) override
{
if constexpr (debug_callback_extra) {
std::cout << "myvisual::key_callback_extra called for key=" << key << " scancode="
Expand Down
Loading

0 comments on commit a4f3414

Please sign in to comment.