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

Feature: Add an option to control the discretization of colormap #1678

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

gapry
Copy link

@gapry gapry commented Oct 25, 2024

Related Issue:

This work addresses the Issue: #228

Current Progress

Over the past two weeks, I have made significant progress on #228, with my work available for review in the following commits:

These commits introduce a new CLI option and extend the vtkF3DRenderer class to handle this option.

Please note that the PR isn’t fully complete yet, as it does not fully resolve #228. I will continue to work on it to ensure that all aspects are addressed.

Since I’ve already spent two weeks on this task, I wanted to report my progress, especially as we’re working remotely.

Next Steps

I will review the VTK documentation to learn how to adjust color settings, specifically focusing on the vtkDiscretizableColorTransferFunction.

@@ -246,6 +246,10 @@
"type": "double_vector",
"default_value": "0.0, 0.0, 0.0, 0.0, 0.4, 0.9, 0.0, 0.0, 0.8, 0.9, 0.9, 0.0, 1.0, 1.0, 1.0, 1.0"
},
"discretization" : {
"type": "bool",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option definitely should not be a bool but an int I think.

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.90%. Comparing base (caa99fc) to head (9f9a2e9).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1678   +/-   ##
=======================================
  Coverage   96.90%   96.90%           
=======================================
  Files         105      105           
  Lines        8629     8634    +5     
=======================================
+ Hits         8362     8367    +5     
  Misses        267      267           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mwestphal
Copy link
Contributor

@gapry any news on this ?

@gapry
Copy link
Author

gapry commented Nov 8, 2024

Dear @mwestphal, I apologize for the delayed response. Here is my current progress:

Part 1 - VTK

I have learned some VTK examples from the official document, https://examples.vtk.org/site/Cxx/. I have chosen the Cube example as a starting point to learn the VTK API and successfully compile and execute in my Ubuntu. The experiment code is host here, https://github.com/gapry/code-for-blog/tree/main/2024/VTK_Cube. Next, I will learn how to use vtkDiscretizableColorTransferFunction to change to Cube.

Also, I have spent the time to read the book, https://book.vtk.org/en/latest/ to learn VTK knowledge

Part 2 - F3D API

According to the document of https://github.com/f3d-app/f3d/tree/master/examples, I need to use the flag Df3d_DIR with the value /path/to/f3d/install/lib/cmake/f3d, but I find it doesn't exist in the f3d directory in the path f3d/install/lib/cmake/

Here is the step to reproduce the issue

cd Workspaces

mkdir f3d
cd f3d

git clone https://github.com/f3d-app/f3d src

mkdir -p build install

cd build
cmake -GNinja -DCMAKE_PREFIX_PATH=../vtk/install -DCMAKE_INSTALL_PREFIX=../install ../src
ninja
ninja install

cd ..

ls -al install/lib
total 1800
drwxrwxr-x 2 gapry gapry    4096 Nov  8 06:26 .
drwxrwxr-x 5 gapry gapry    4096 Nov  8 06:26 ..
lrwxrwxrwx 1 gapry gapry      11 Nov  8 06:26 libf3d.so -> libf3d.so.2
lrwxrwxrwx 1 gapry gapry      13 Nov  8 06:26 libf3d.so.2 -> libf3d.so.2.5
-rw-r--r-- 1 gapry gapry 1745760 Nov  8 06:26 libf3d.so.2.5
-rw-r--r-- 1 gapry gapry   83800 Nov  8 06:26 libvtkext.so

As you can see, it just has some *.so files

So, I decided to try to use cmake -Df3d_DIR:PATH=~/Workspaces/f3d/build/cmake to compile,
it occurs the following error

CMake Error at /home/gapry/Workspaces/f3d/build/cmake/f3dConfig.cmake:93 (message):
  library was requested but could not be found,
  /home/gapry/Workspaces/f3d/build/cmake/library-config.cmake is missing,
  please check component was installed
Call Stack (most recent call first):
  CMakeLists.txt:7 (find_package)

The experiment code is host here, https://github.com/gapry/code-for-blog/tree/main/2024/F3D_RenderImage, you can see I use cmake -Df3d_DIR:PATH=~/Workspaces/f3d/build/cmake .. in the Makefile, https://github.com/gapry/code-for-blog/blob/2b8ace13462c68dc6a7f0e3b1b6645199a8a489d/2024/F3D_RenderImage/Makefile#L14

Do I miss some important thing?

@mwestphal
Copy link
Contributor

You need to install the SDK:

cmake --install . --component sdk

from: https://f3d.app/doc/dev/BUILD.html

but you do not need really to build the libf3d examples to do this :)

@gapry
Copy link
Author

gapry commented Nov 9, 2024

@mwestphal

Thank you for your help, I can see in the f3d directory in the path f3d/install/lib/cmake/,
and update the -Df3d_DIR:PATH in my Makefile.

But it appears another issue I can't solve,

When I use mkdir build; cd build; cmake -Df3d_DIR:PATH=~/Workspaces/f3d/install/lib/cmake/f3d ..; make to compile one of libf3d examples, the code is here, it occurs the following error, https://gist.github.com/gapry/b5cdf79d3c5f52bf32e3c0aa06fbb51a

And I do the following checking:

  1. check the libf3d.so.2.5, and I make sure it exists

    $ file /home/gapry/Workspaces/f3d/install/lib/libf3d.so.2.5
    /home/gapry/Workspaces/f3d/install/lib/libf3d.so.2.5: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=324b457d94b4427c8817e986c72cc9722c1b0efe, not stripped
    
  2. read the document again, according to the overview, I make sure I have turned on the macro BUILD_SHARED_LIBS during compiling the libf3d, here is the full step

    $ cd Workspaces
    
    $ mkdir f3d
    $ cd f3d
    
    $ git clone https://github.com/f3d-app/f3d src
    
    $ mkdir -p build install
    
    $ cd build
    $ cmake -GNinja -DCMAKE_PREFIX_PATH=../vtk/install -DCMAKE_INSTALL_PREFIX=../install -DBUILD_SHARED_LIBS=ON ../src
    $ cmake --install . --component sdk
    $ ninja
    $ ninja install
    
    $ cd ../
    $ ls install
    bin  include  lib  share
    $ ls install/lib
    cmake  libf3d.so  libf3d.so.2  libf3d.so.2.5  libvtkext.so
    

Am I missing something important, doing something wrong?

@gapry
Copy link
Author

gapry commented Nov 9, 2024

@mwestphal

I know I can add some unit tests for the new features, but I find VTK have a lot of examples, https://examples.vtk.org/site/Cxx/, to help the new developers, so I think I can do similar things, write a new example application for libf3d.

Just want to share my thoughts to you.

@mwestphal
Copy link
Contributor

Hi @gapry

Do you need any help moving forward with this ?

@mwestphal
Copy link
Contributor

Hi @gapry

Do you need any help moving forward with this ?

@gapry
Copy link
Author

gapry commented Nov 24, 2024

@mwestphal I sincerely apologize for my delayed response.

Question:

  1. According to the document, Gallery, I find the colormap can be variadic, like void simple_printf(const char* fmt, ...), e.g.
$ f3d backpack.vti -vmn --range=300,1000 --colormap=0,0,0,0,1,1,1,1
$ f3d single-pin.vtp -xtbgans --range=-2,8 --colormap=0,0.3,0.7,0,0.7,0,0.1,1,1,0.8,0.8,0
$ f3d pointCloud.vtp -o --point-size=0.2 --colormap=0,0,0.8,0,0.4,0.9,0,0,0.8,0.9,0.9,0,1,0.5,0.5,1 --up=+Z

so, I don't know how to define the type. e.g. vector<RGB>, vector<RGBA>, ... , etc ???

  1. According to the document, Final shaders, I don't how to pass the colormap to the fragment shader.

@mwestphal
Copy link
Contributor

so, I don't know how to define the type. e.g. vector, vector, ... , etc ???

What do you mean ? You do not need to define a new type. You can just change the discretization of the colormap in the vtkF3DRenderer in ConfigureRangeAndCTFForColoring by using the VTK method: vtkDiscretizableColorTransferFunction::SetNumberOfValues
https://vtk.org/doc/nightly/html/classvtkDiscretizableColorTransferFunction.html#add05ecb2a72eeaec91588366fa7ea64d

@gapry
Copy link
Author

gapry commented Nov 24, 2024

According to the document, Gallery, I find the colormap the usage as following

First Usage

$ f3d backpack.vti -vmn --range=300,1000 --colormap=0,0,0,0,1,1,1,1

I can get two RGBA groups

0, 0, 0, 0
1, 1, 1, 1

Second Usage

$ f3d single-pin.vtp -xtbgans --range=-2,8 --colormap=0,0.3,0.7,0,0.7,0,0.1,1,1,0.8,0.8,0

I can get three RGBA groups

0,   0.3, 0.7, 0
0.7,   0, 0.1, 1
1,   0.8, 0.8, 0

Third Usage

$ f3d pointCloud.vtp -o --point-size=0.2 --colormap=0,0,0.8,0,0.4,0.9,0,0,0.8,0.9,0.9,0,1,0.5,0.5,1 --up=+Z

I can get four RGBA groups

  0,   0, 0.8, 0, 
0.4, 0.9,   0, 0, 
0.8, 0.9, 0.9, 0, 
  1, 0.5, 0.5, 1

So, I think the colormap is used RGBA as unit, but according to the https://github.com/f3d-app/f3d/blob/master/vtkext/private/module/vtkF3DRenderer.cxx#L2548

void vtkF3DRenderer::ConfigureRangeAndCTFForColoring(
  const F3DColoringInfoHandler::ColoringInfo& info)
{
...
  for (size_t i = 0; i < this->Colormap.size(); i += 4)
  {
    double val = this->Colormap[i];
    double r = this->Colormap[i + 1];
    double g = this->Colormap[i + 2];
    double b = this->Colormap[i + 3];
    this->ColorTransferFunction->AddRGBPoint(
      this->ColorRange[0] + val * (this->ColorRange[1] - this->ColorRange[0]), r, g, b);
  }
...
}

It appears the colormap is decomposed into val, r, g, b, rather than r, g, b, a. This is causing some confusion for me. For now, I still don't understand the purpose of double val = this->Colormap[i];

@mwestphal
Copy link
Contributor

From https://f3d.app/doc/user/OPTIONS.html

Set a custom colormap for the coloring.
This is a list of colors in the format val1,red1,green1,blue1,...,valN,redN,greenN,blueN
where all values are in the range (0,1).
Ignored if --colormap-file option is specified.
Use with the scalar option.

Opacity is never mentioned. There are four values because the first value is the scalar value.

VAL R G B

@gapry
Copy link
Author

gapry commented Nov 24, 2024

@mwestphal Thank you! I understand now. I apologize for not reading the documentation carefully. I will use vtkDiscretizableColorTransferFunction::SetNumberOfValue, https://vtk.org/doc/nightly/html/classvtkDiscretizableColorTransferFunction.html#add05ecb2a72eeaec91588366fa7ea64d, to continue the work.

@gapry
Copy link
Author

gapry commented Nov 30, 2024

Current Progress

I pushed the new commits and the following screenshots are my testing results.

The key changes

  • set the default value of discretization is 256
  • in vtkF3DRenderer::SetColorDiscretization(const int discretization), I use
    vtkF3DMetaImporter::ColoringInfo info to call
    this->ConfigureRangeAndCTFForColoring(info);
  • in ConfigureRangeAndCTFForColoring, call the
    vtkDiscretizableColorTransferFunction::SetNumberOfValue

Test Case 1: don't use the option--colormap-discretization

./build/bin/f3d assets/f3d-data/dragon.vtu -xtganse --comp=0 \
  --colormap=1.0,255.0,127.0,63.0

Result
Screenshot from 2024-11-30 14-48-17

Test Case 2: set --colormap-discretization=25600

./build/bin/f3d assets/f3d-data/dragon.vtu -xtganse --comp=0 \
  --colormap-discretization=25600 --colormap=1.0,255.0,127.0,63.0

Result
Screenshot from 2024-11-30 14-49-12

Test Case 3: set --colormap-discretization=64

./build/bin/f3d assets/f3d-data/dragon.vtu -xtganse --comp=0 \
  --colormap-discretization=64 --colormap=1.0,255.0,127.0,63.0

Result
Screenshot from 2024-11-30 14-51-17

Conclusion

Unfortunately, this confirms that my implementation is incorrect.

@snoyer
Copy link
Contributor

snoyer commented Nov 30, 2024

Unfortunately, this confirms that my implementation is incorrect.

--colormap=1.0,255.0,127.0,63.0 will give you a colormap with a single color regardless of your implementation tho. Maybe your implementation works but you just can't tell?
dragon_1

Try --colormap=0,1,0,0,1,0,0,1 to map value 0 to red and 1 to blue
dragon_2

@@ -557,6 +563,10 @@ class vtkF3DRenderer : public vtkOpenGLRenderer
vtkF3DMetaImporter* Importer = nullptr;
vtkMTimeType ImporterTimeStamp = 0;

vtkNew<vtkDiscretizableColorTransferFunction> DiscretizableColorTransferFunction;
bool DiscretizableColorTransferFunctionConfigured = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think you need this

@@ -2472,6 +2489,12 @@ void vtkF3DRenderer::ConfigureRangeAndCTFForColoring(
return;
}

// Set Discretization
if(!this->DiscretizableColorTransferFunctionConfigured) {
this->DiscretizableColorTransferFunction->SetNumberOfValues(this->Discretization);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do that directly on the existing colorTransferFunction

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an option to control the discretization of colormap
3 participants