Skip to content

Commit

Permalink
Merge pull request #11 from tum-pbs/fix-colormap-sanitization
Browse files Browse the repository at this point in the history
Fix colormap sanitization
  • Loading branch information
wi-re authored Nov 6, 2024
2 parents 584360b + bd19938 commit 89fc440
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
26 changes: 25 additions & 1 deletion guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,28 @@ particles.erase(std::remove_if(particles.begin(), particles.end(), [](const Part
}), particles.end());
```

You may also want to consider making the lifetime a GUI element or updating it based on the interactions, e.g., a particle that was dragged around has the lifetime counter reset.
You may also want to consider making the lifetime a GUI element or updating it based on the interactions, e.g., a particle that was dragged around has the lifetime counter reset.

## Colormapping

If you would like to color things in your simulation based on some attribute then color mapping is a useful approach where our framework supports most common matplotlib colormaps you can find [here](https://matplotlib.org/stable/users/explain/colors/colormaps.html). A colormap within framework is consturcted by giving it a string for the colormap name and then calling it with a float in the range 0 to 1 (the input is clamped to this value, so you need to manually map the inputs), e.g.,
```cpp
auto cmap = Colormap("viridis");
glm::vec3 color = cmap(0.5);
```

A way we can incorporate this into our example is by coloring spheres not randomly but based on their lifetime:

```cpp
// Scene1.cpp
void Scene1::onDraw(Renderer& renderer){
// ...
auto cmap = Colormap("viridis");

for (auto& particle : particles){
renderer.drawSphere(particle.position, 0.1f, glm::vec4(cmap(particle.lifetime / 1.5f), 1));
}
}
```
Note that the colormap returns no alpha channel so we need to expand the returned color to be RGBA. Also note the division by 1.5, the expected maximum lifetime of a particle before deletion and needs to be kept in sync with other lifetime based parts of the code, which could be ensured by using a member of the Scene class to represent the maximum lifetime of a particle instead of hardcoding it.
![image](https://github.com/user-attachments/assets/37f5c448-ea09-4c0f-884b-21101b8f02dd)
6 changes: 5 additions & 1 deletion src/Colormap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ void Colormap::init()
std::string line;
int offset = 0;
while (std::getline(file, line))
{
{
// Sanitize inputs in case the file contains windows line endings, i.e. \r\n instead of \n
line.erase(std::remove(line.begin(), line.end(), '\r' ), line.end());
line.erase(std::remove(line.begin(), line.end(), '\n' ), line.end());

names.push_back(line);
indices[line] = offset;
offset++;
Expand Down

0 comments on commit 89fc440

Please sign in to comment.