Blue Brain BioExplorer
TransferFunction.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "TransferFunction.h"
24 
26 
27 #include <algorithm>
28 
29 namespace
30 {
31 double _interpolatedOpacity(const core::Vector2ds& controlPointsSorted, const double x)
32 {
33  const auto& firstPoint = controlPointsSorted.front();
34  if (x <= firstPoint.x)
35  return firstPoint.y;
36 
37  for (size_t i = 1; i < controlPointsSorted.size(); ++i)
38  {
39  const auto& current = controlPointsSorted[i];
40  const auto& previous = controlPointsSorted[i - 1];
41  if (x <= current.x)
42  {
43  const auto t = (x - previous.x) / (current.x - previous.x);
44  return (1.0 - t) * previous.y + t * current.y;
45  }
46  }
47 
48  const auto& lastPoint = controlPointsSorted.back();
49  return lastPoint.y;
50 }
51 } // namespace
52 
53 namespace core
54 {
55 bool ColorMap::operator==(const ColorMap& rhs) const
56 {
57  if (this == &rhs)
58  return true;
59  return name == rhs.name && colors == rhs.colors;
60 }
61 
63 {
64  colors = {{0, 0, 0}, {1, 1, 1}};
65 }
66 
68 {
69  clear();
70 }
71 
73 {
74  _colorMap.clear();
75  _controlPoints = {{0, 0}, {1, 1}};
76  _valuesRange = {0, 255};
77  markModified();
78 }
79 
81 {
82  constexpr size_t numSamples = 256;
83  constexpr double dx = 1. / (numSamples - 1);
84 
85  auto tfPoints = getControlPoints();
86  std::sort(tfPoints.begin(), tfPoints.end(), [](auto a, auto b) { return a.x < b.x; });
87 
88  floats opacities;
89  opacities.reserve(numSamples);
90  for (size_t i = 0; i < numSamples; ++i)
91  opacities.push_back(_interpolatedOpacity(tfPoints, i * dx));
92  return opacities;
93 }
94 } // namespace core
void markModified(const bool triggerCallback=true)
Definition: BaseObject.h:65
floats calculateInterpolatedOpacities() const
const Vector2ds & getControlPoints() const
std::vector< Vector2d > Vector2ds
Definition: MathTypes.h:145
std::vector< float > floats
Definition: Types.h:45
bool operator==(const ColorMap &rhs) const
std::string name