Blue Brain BioExplorer
Utils.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 "Utils.h"
24 
27 
28 #include <algorithm>
29 #include <charconv>
30 #include <cmath>
31 #include <set>
32 #include <sstream>
33 #include <string>
34 
35 namespace core
36 {
37 strings parseFolder(const std::string& folder, const strings& filters)
38 {
39  strings files;
40  fs::directory_iterator endIter;
41  if (fs::is_directory(folder))
42  {
43  for (fs::directory_iterator dirIter(folder); dirIter != endIter; ++dirIter)
44  {
45  if (fs::is_regular_file(dirIter->status()))
46  {
47  const auto filename = dirIter->path().c_str();
48  if (filters.empty())
49  files.push_back(filename);
50  else
51  {
52  const auto& fileExtension = dirIter->path().extension();
53  const auto found = std::find(filters.begin(), filters.end(), fileExtension);
54  if (found != filters.end())
55  files.push_back(filename);
56  }
57  }
58  }
59  }
60  std::sort(files.begin(), files.end());
61  return files;
62 }
63 
64 std::string extractExtension(const std::string& filename)
65 {
66  auto extension = fs::path(filename).extension().string();
67  if (!extension.empty())
68  extension = extension.erase(0, 1);
69 
70  return extension;
71 }
72 
73 Vector4f getBezierPoint(const Vector4fs& controlPoints, const float t)
74 {
75  if (t < 0.f || t > 1.f)
76  CORE_THROW("Invalid value with t=" + std::to_string(t) + ". Must be between 0 and 1");
77 
78  const size_t n = controlPoints.size();
79  Vector4fs tempPoints = controlPoints;
80 
81  for (uint64_t k = 1; k < n; ++k)
82  {
83  for (uint64_t i = 0; i < n - k; ++i)
84  {
85  tempPoints[i].x = (1 - t) * tempPoints[i].x + t * tempPoints[i + 1].x;
86  tempPoints[i].y = (1 - t) * tempPoints[i].y + t * tempPoints[i + 1].y;
87  tempPoints[i].z = (1 - t) * tempPoints[i].z + t * tempPoints[i + 1].z;
88  tempPoints[i].w = (1 - t) * tempPoints[i].w + t * tempPoints[i + 1].w;
89  }
90  }
91  return tempPoints[0];
92 }
93 
94 struct RGBColor
95 {
96  int r, g, b;
97 };
98 
99 Vector3f hsvToRgb(float h, float s, float v)
100 {
101  int i = h * 6;
102  float f = h * 6 - i;
103  float p = v * (1 - s);
104  float q = v * (1 - f * s);
105  float t = v * (1 - (1 - f) * s);
106 
107  switch (i % 6)
108  {
109  case 0:
110  return {v, t, p};
111  case 1:
112  return {q, v, p};
113  case 2:
114  return {p, v, t};
115  case 3:
116  return {p, q, v};
117  case 4:
118  return {t, p, v};
119  case 5:
120  return {v, p, q};
121  default:
122  return {0, 0, 0}; // Should not reach here
123  }
124 }
125 
126 Vector3fs getRainbowColormap(const uint32_t colormapSize)
127 {
128  Vector3fs colormap;
129  for (uint32_t i = 0; i < colormapSize; ++i)
130  {
131  const float hue = static_cast<float>(i) / colormapSize;
132  colormap.push_back(hsvToRgb(hue, 1.0f, 1.0f));
133  }
134 
135  return colormap;
136 }
137 
138 template <typename To, typename From>
139 To lexical_cast(const From& from)
140 {
141  To to;
142  std::from_chars(from.data(), from.data() + from.size(), to);
143  return to;
144 }
145 
146 } // namespace core
glm::vec3 Vector3f
Definition: MathTypes.h:137
Vector3f hsvToRgb(float h, float s, float v)
Definition: Utils.cpp:99
strings parseFolder(const std::string &folder, const strings &filters)
Definition: Utils.cpp:37
glm::vec4 Vector4f
Definition: MathTypes.h:138
To lexical_cast(const From &from)
Function template taking two template parameters (To and From) representing the source and target typ...
Definition: Utils.cpp:139
Vector3fs getRainbowColormap(const uint32_t colormapSize)
Get the Rainbow Colormap.
Definition: Utils.cpp:126
std::vector< Vector4f > Vector4fs
Definition: MathTypes.h:140
std::vector< Vector3f > Vector3fs
Definition: MathTypes.h:139
Vector4f getBezierPoint(const Vector4fs &controlPoints, const float t)
Get the Bezier Point from a curve defined by the provided control points.
Definition: Utils.cpp:73
std::string extractExtension(const std::string &filename)
Definition: Utils.cpp:64
#define CORE_THROW(__msg)
Definition: Logs.h:42
std::vector< std::string > strings
Definition: Types.h:44