Skip to content

Commit

Permalink
Add colormap file support
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk committed Mar 11, 2024
1 parent f0ecafb commit bf56eaf
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 1 deletion.
8 changes: 8 additions & 0 deletions application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/F3DConfig.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/F3DConfig.h")

set(F3D_SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/F3DColorMapTools.cxx
${CMAKE_CURRENT_SOURCE_DIR}/F3DConfigFileTools.cxx
${CMAKE_CURRENT_BINARY_DIR}/F3DIcon.cxx
${CMAKE_CURRENT_SOURCE_DIR}/F3DOptionsParser.cxx
Expand Down Expand Up @@ -182,6 +183,13 @@ install(
COMPONENT configuration
EXCLUDE_FROM_ALL)

# Default color maps
install(
DIRECTORY "${F3D_SOURCE_DIR}/resources/colormaps/"
DESTINATION "${f3d_config_dir}"
COMPONENT colormaps
EXCLUDE_FROM_ALL)

# Other ressoure files
if(UNIX AND NOT APPLE AND NOT ANDROID)
install(FILES "${F3D_SOURCE_DIR}/resources/f3d.desktop"
Expand Down
61 changes: 61 additions & 0 deletions application/F3DColorMapTools.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "F3DColorMapTools.h"

#include "image.h"

#include <filesystem>

// todo: remove
#include <iostream>

namespace fs = std::filesystem;

namespace F3DColorMapTools
{
std::string Find(const std::string& str)
{
if (fs::exists(str))
{
// already full path;
return str;
}

// todo: stem, find file in standard paths

return {};

Check warning on line 24 in application/F3DColorMapTools.cxx

View check run for this annotation

Codecov / codecov/patch

application/F3DColorMapTools.cxx#L24

Added line #L24 was not covered by tests
}

std::vector<double> Read(const std::string& path)
{
try
{
f3d::image img(path);

if (img.getChannelCount() < 3)
{
// TODO: warning
return {};

Check warning on line 36 in application/F3DColorMapTools.cxx

View check run for this annotation

Codecov / codecov/patch

application/F3DColorMapTools.cxx#L36

Added line #L36 was not covered by tests
}

int w = img.getWidth();

std::vector<double> cm;
cm.resize(4 * w);

for (int i = 0; i < w; i++)
{
cm[4 * i] = static_cast<double>(i) / w;
for (int j = 0; j < 3; j++)
{
cm[4 * i + j + 1] = img(i, 0, j);
}
}

return cm;
}
catch (const f3d::image::read_exception&)

Check warning on line 55 in application/F3DColorMapTools.cxx

View check run for this annotation

Codecov / codecov/patch

application/F3DColorMapTools.cxx#L55

Added line #L55 was not covered by tests
{
// TODO
return {};
}

Check warning on line 59 in application/F3DColorMapTools.cxx

View check run for this annotation

Codecov / codecov/patch

application/F3DColorMapTools.cxx#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}
19 changes: 19 additions & 0 deletions application/F3DColorMapTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @class F3DColorMapReader
* @brief A class used to convert images to libf3d colormap option
*
*/

#ifndef F3DColorMapReader_h
#define F3DColorMapReader_h

#include <string>
#include <vector>

namespace F3DColorMapTools
{
std::string Find(const std::string& str);
std::vector<double> Read(const std::string& path);
}

#endif
1 change: 0 additions & 1 deletion application/F3DConfigFileTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace F3DConfigFileTools
{
std::filesystem::path GetUserConfigFileDirectory();
std::filesystem::path GetBinaryConfigFileDirectory();
std::filesystem::path GetSystemConfigFileDirectory();
std::filesystem::path GetConfigPath(const std::string& configSearch);
}

Expand Down
1 change: 1 addition & 0 deletions application/F3DOptionsParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ void ConfigurationOptions::GetOptions(F3DAppOptions& appOptions, f3d::options& o
this->DeclareOption(grp4, "cells", "c", "Use a scalar array from the cells", options.getAsBoolRef("model.scivis.cells"), HasDefault::YES, MayHaveConfig::YES);
this->DeclareOption(grp4, "range", "", "Custom range for the coloring by array", options.getAsDoubleVectorRef("model.scivis.range"), HasDefault::YES, MayHaveConfig::YES, "<min,max>");
this->DeclareOption(grp4, "bar", "b", "Show scalar bar", options.getAsBoolRef("ui.bar"), HasDefault::YES, MayHaveConfig::YES);
this->DeclareOption(grp4, "colormap-file", "", "Specify a colormap image (ignored if \"colormap\" is specified)", appOptions.UserColorMap, HasDefault::YES, MayHaveConfig::YES, "<TODO>");
this->DeclareOption(grp4, "colormap", "", "Specify a custom colormap", options.getAsDoubleVectorRef("model.scivis.colormap"), HasDefault::YES, MayHaveConfig::YES, "<color_list>");
this->DeclareOption(grp4, "volume", "v", "Show volume if the file is compatible", options.getAsBoolRef("model.volume.enable"), HasDefault::YES, MayHaveConfig::YES);
this->DeclareOption(grp4, "inverse", "i", "Inverse opacity function for volume rendering", options.getAsBoolRef("model.volume.inverse"), HasDefault::YES, MayHaveConfig::YES);
Expand Down
1 change: 1 addition & 0 deletions application/F3DOptionsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class options;
struct F3DAppOptions
{
std::string UserConfigFile = "";
std::string UserColorMap = "";
bool DryRun = false;
bool GeometryOnly = false;
bool GroupGeometries = false;
Expand Down
26 changes: 26 additions & 0 deletions application/F3DStarter.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "F3DStarter.h"

#include "F3DColorMapTools.h"
#include "F3DConfig.h"
#include "F3DIcon.h"
#include "F3DNSDelegate.h"
Expand Down Expand Up @@ -313,6 +314,31 @@ int F3DStarter::Start(int argc, char** argv)
}
#endif
}

// Parse colormap
if (!this->Internals->AppOptions.UserColorMap.empty())
{
std::string fullPath = F3DColorMapTools::Find(this->Internals->AppOptions.UserColorMap);

if (!fullPath.empty())
{
auto cm = F3DColorMapTools::Read(fullPath);

if (!cm.empty())
{
this->Internals->Engine->getOptions().set("model.scivis.colormap", cm);
}
else
{
// TODO: warning
}
}
else
{
// TODO: warning
}
}

f3d::log::debug("Engine configured");

f3d::log::debug("========== Loading 3D file ==========");
Expand Down
1 change: 1 addition & 0 deletions application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ f3d_test(NAME TestUpDirectionNoSign DATA suzanne.ply ARGS --up=X DEFAULT_LIGHTS)
f3d_test(NAME TestTextureMatCap DATA suzanne.ply ARGS --texture-matcap=${F3D_SOURCE_DIR}/testing/data/skin.png DEFAULT_LIGHTS)
f3d_test(NAME TestTextureMatCapWithTCoords DATA WaterBottle.glb ARGS --geometry-only --texture-matcap=${F3D_SOURCE_DIR}/testing/data/skin.png DEFAULT_LIGHTS)
f3d_test(NAME TestConfigOrder DATA suzanne.ply ARGS CONFIG ${F3D_SOURCE_DIR}/testing/data/config-order.json DEFAULT_LIGHTS)
f3d_test(NAME TestColorMapFileFullPath DATA dragon.vtu ARGS --colormap-file=${F3D_SOURCE_DIR}/resources/colormaps/viridis.png --scalars --comp=1 DEFAULT_LIGHTS)

# Needs SSBO: https://gitlab.kitware.com/vtk/vtk/-/merge_requests/10675
if(VTK_VERSION VERSION_GREATER_EQUAL 9.3.20231108)
Expand Down
1 change: 1 addition & 0 deletions doc/user/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Options|Default|Description
-c, \-\-cells||Specify that the scalar array is to be found *on the cells* instead of on the points.<br>Use with the scalar option.
\-\-range=\<min,max\>||Set a *custom range for the coloring* by the array.<br>Use with the scalar option.
-b, \-\-bar||Show *scalar bar* of the coloring by array.<br>Use with the scalar option.
\-\-colormap\-file=\<name\>||Set a *colormap file for the coloring*.<br>Valid values are inferno, todo, todo.<br>Use with the scalar option.
\-\-colormap=\<color_list\>||Set a *custom colormap for the coloring*.<br>This is a list of colors in the format `val1,red1,green1,blue1,...,valN,redN,greenN,blueN`<br>where all values are in the range (0,1).<br>Use with the scalar option.
-v, \-\-volume||Enable *volume rendering*. It is only available for 3D image data (vti, dcm, nrrd, mhd files) and will display nothing with other formats.
-i, \-\-inverse||Inverse the linear opacity function used for volume rendering.
Expand Down
5 changes: 5 additions & 0 deletions library/public/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class F3D_EXPORT image
bool operator!=(const image& reference) const;
///@}

/**
* TODO: doc
*/
double operator()(int x, int y, int channel) const;

/**
* Get the list of supported image format when opening a file.
*/
Expand Down
18 changes: 18 additions & 0 deletions library/src/image.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ bool image::operator!=(const image& reference) const
return !this->operator==(reference);
}

//----------------------------------------------------------------------------
double image::operator()(int x, int y, int channel) const
{
double v = this->Internals->Image->GetScalarComponentAsDouble(x, y, 0, channel);

switch (getChannelType())
{
case ChannelType::BYTE:
return v / 255.0;
case ChannelType::SHORT:
return v / 65535.0;
default:
break;

Check warning on line 368 in library/src/image.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/image.cxx#L365-L368

Added lines #L365 - L368 were not covered by tests
}

return v;

Check warning on line 371 in library/src/image.cxx

View check run for this annotation

Codecov / codecov/patch

library/src/image.cxx#L371

Added line #L371 was not covered by tests
}

//----------------------------------------------------------------------------
void image::save(const std::string& path, SaveFormat format) const
{
Expand Down
Binary file added resources/colormaps/cividis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/cubehelix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/gist_earth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/hot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/inferno.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/magma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/plasma.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/seismic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/colormaps/viridis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testing/baselines/TestColorMapFileFullPath.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bf56eaf

Please sign in to comment.