1.3.49
 
Loading...
Searching...
No Matches
Visualizer.h
Go to the documentation of this file.
1
16#ifndef HELIOS_VISUALIZER
17#define HELIOS_VISUALIZER
18
19#include "Context.h"
20
21// GLM Libraries (math-related functions for graphics)
22#define GLM_FORCE_RADIANS
23#ifndef APIENTRY
24#define APIENTRY
25#endif
26#include <GLFW/glfw3.h>
27#include "glm/glm.hpp"
28#include "glm/gtc/matrix_transform.hpp"
29#include "glm/gtx/transform.hpp"
30
31#include "GeometryHandler.h"
32#include "Visualizer.h"
33
34class Visualizer;
35
36
47bool validateTextureFile(const std::string &texture_file, bool pngonly = false);
48
50void mouseCallback(GLFWwindow *window, int button, int action, int mods);
51
53void cursorCallback(GLFWwindow *window, double x, double y);
54
56void scrollCallback(GLFWwindow *window, double xoffset, double yoffset);
57
59class Glyph {
60public:
61 Glyph() = default;
62 Glyph(const helios::uint2 &size, const std::vector<std::vector<unsigned char>> &data) : size(size), data(data) {
63 }
64 helios::uint2 size;
65 std::vector<std::vector<unsigned char>> data;
66};
67
69struct Shader {
70
72 void disableTextures() const;
73
75 void enableTextureMaps() const;
76
78 void enableTextureMasks() const;
79
81 void setTransformationMatrix(const glm::mat4 &matrix) const;
82
84 void setDepthBiasMatrix(const glm::mat4 &matrix) const;
85
87 void setLightDirection(const helios::vec3 &direction) const;
88
90 void setLightingModel(uint lightingmodel) const;
91
93 void setLightIntensity(float lightintensity) const;
94
96 void useShader() const;
97
99
104 void initialize(const char *vertex_shader_file, const char *fragment_shader_file, Visualizer *visualizer_ptr);
105
106 ~Shader();
107
108 // Primary Shader
109 uint shaderID;
110 GLint textureUniform;
111 GLint shadowmapUniform;
112 GLint transformMatrixUniform;
113 GLint depthBiasUniform;
114 GLint lightDirectionUniform;
115 GLint lightingModelUniform;
116 GLint RboundUniform;
117 GLint lightIntensityUniform;
118 std::vector<GLuint> vertex_array_IDs;
119 GLint uvRescaleUniform;
120
122 bool initialized = false;
123};
124
126struct Colormap {
127
128 Colormap() : cmapsize(0), minval(0.0f), maxval(1.0f) {};
129
130 Colormap(const std::vector<helios::RGBcolor> &ctable, const std::vector<float> &clocs, int size, float minval_, float maxval_) : cmapsize(size), minval(minval_), maxval(maxval_) {
131 set(ctable, clocs, size, minval_, maxval_);
132 }
133
134 void set(const std::vector<helios::RGBcolor> &ctable, const std::vector<float> &clocs, int size, float a_minval, float a_maxval) {
135 cmapsize = size;
136 minval = a_minval;
137 maxval = a_maxval;
138
139 size_t Ncolors = ctable.size();
140
141 assert(clocs.size() == Ncolors && minval < maxval);
142
143 cmap.resize(Ncolors);
144
145 std::vector<float> cinds;
146 cinds.resize(Ncolors);
147
148 for (uint i = 0; i < Ncolors; i++) {
149 cinds.at(i) = clocs.at(i) * static_cast<float>(cmapsize - 1);
150 }
151
152 cmap.resize(cmapsize);
153 for (uint c = 0; c < Ncolors - 1; c++) {
154 float cmin = cinds.at(c);
155 float cmax = cinds.at(c + 1);
156
157 for (uint i = 0; i < cmapsize; i++) {
158 auto i_f = static_cast<float>(i);
159
160 if (i_f >= cmin && i_f <= cmax) {
161 cmap.at(i).r = ctable.at(c).r + (i_f - cmin) / (cmax - cmin) * (ctable.at(c + 1).r - ctable.at(c).r);
162 cmap.at(i).g = ctable.at(c).g + (i_f - cmin) / (cmax - cmin) * (ctable.at(c + 1).g - ctable.at(c).g);
163 cmap.at(i).b = ctable.at(c).b + (i_f - cmin) / (cmax - cmin) * (ctable.at(c + 1).b - ctable.at(c).b);
164 }
165 }
166 }
167 }
168
169 [[nodiscard]] helios::RGBcolor query(float x) const {
170 assert(cmapsize > 0 && !cmap.empty());
171
172 helios::RGBcolor color;
173
174 uint color_ind;
175 if (minval == maxval) {
176 color_ind = 0;
177 } else {
178 float normalized_pos = (x - minval) / (maxval - minval) * float(cmapsize - 1);
179
180 // Handle values below minimum range
181 if (normalized_pos < 0) {
182 color_ind = 0;
183 }
184 // Handle values above maximum range
185 else if (normalized_pos > float(cmapsize - 1)) {
186 color_ind = cmapsize - 1;
187 }
188 // Handle values within range
189 else {
190 color_ind = std::round(normalized_pos);
191 }
192 }
193
194 color.r = cmap.at(color_ind).r;
195 color.g = cmap.at(color_ind).g;
196 color.b = cmap.at(color_ind).b;
197
198 return color;
199 }
200
201 void setRange(float min, float max) {
202 minval = min;
203 maxval = max;
204 }
205
206 [[nodiscard]] helios::vec2 getRange() const {
207 return {minval, maxval};
208 }
209
210 [[nodiscard]] float getLowerLimit() const {
211 return minval;
212 }
213
214 [[nodiscard]] float getUpperLimit() const {
215 return maxval;
216 }
217
218private:
219 std::vector<helios::RGBcolor> cmap;
220 unsigned int cmapsize;
221 float minval, maxval;
222};
223
225
237int read_JPEG_file(const char *filename, std::vector<unsigned char> &texture, uint &height, uint &width);
238
240
250int write_JPEG_file(const char *filename, uint width, uint height, bool print_messages);
251
253
265int write_JPEG_file(const char *filename, uint width, uint height, const std::vector<helios::RGBcolor> &data, bool print_messages);
266
268
277void read_png_file(const char *filename, std::vector<unsigned char> &texture, uint &height, uint &width);
278
281public:
283 Visualizer() = delete;
284
286
289 explicit Visualizer(uint Wdisplay);
290
292
296 Visualizer(uint Wdisplay, uint Hdisplay);
297
299
304 Visualizer(uint Wdisplay, uint Hdisplay, int aliasing_samples);
305
308
315 Visualizer(uint Wdisplay, uint Hdisplay, int aliasing_samples, bool window_decorations, bool headless);
316
318 ~Visualizer();
319
321 static int selfTest(int argc = 0, char **argv = nullptr);
322
324 void enableMessages();
325
327 void disableMessages();
328
338
356
358
362 void setCameraPosition(const helios::vec3 &cameraPosition, const helios::vec3 &lookAt);
363
365
369 void setCameraPosition(const helios::SphericalCoord &cameraAngle, const helios::vec3 &lookAt);
370
372
375 void setCameraFieldOfView(float angle_FOV);
376
378
381 void setLightDirection(const helios::vec3 &direction);
382
394
396
400 void setLightingModel(LightingModel lightingmodel);
401
403
406 void setLightIntensityFactor(float lightintensityfactor);
407
409
412 void setBackgroundColor(const helios::RGBcolor &color);
413
415
422 size_t addRectangleByCenter(const helios::vec3 &center, const helios::vec2 &size, const helios::SphericalCoord &rotation, const helios::RGBcolor &color, CoordinateSystem coordFlag);
423
425
432 size_t addRectangleByCenter(const helios::vec3 &center, const helios::vec2 &size, const helios::SphericalCoord &rotation, const helios::RGBAcolor &color, CoordinateSystem coordFlag);
433
435
442 size_t addRectangleByCenter(const helios::vec3 &center, const helios::vec2 &size, const helios::SphericalCoord &rotation, const char *texture_file, CoordinateSystem coordFlag);
443
445
453 size_t addRectangleByCenter(const helios::vec3 &center, const helios::vec2 &size, const helios::SphericalCoord &rotation, const helios::RGBcolor &color, const char *texture_file, CoordinateSystem coordFlag);
454
456
464 size_t addRectangleByCenter(const helios::vec3 &center, const helios::vec2 &size, const helios::SphericalCoord &rotation, const helios::RGBcolor &color, const Glyph *glyph, CoordinateSystem coordFlag);
465
467
472 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBcolor &color, CoordinateSystem coordFlag);
473
475
480 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBAcolor &color, CoordinateSystem coordFlag);
481
483
488 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const char *texture_file, CoordinateSystem coordFlag);
489
491
497 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const char *texture_file, const std::vector<helios::vec2> &uvs, CoordinateSystem coordFlag);
498
500
507 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBcolor &color, const char *texture_file, const std::vector<helios::vec2> &uvs, CoordinateSystem coordFlag);
508
510
516 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBcolor &color, const char *texture_file, CoordinateSystem coordFlag);
517
519
525 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBcolor &color, const Glyph *glyph, CoordinateSystem coordFlag);
526
528
534 size_t addRectangleByVertices(const std::vector<helios::vec3> &vertices, const helios::RGBAcolor &color, const Glyph *glyph, CoordinateSystem coordFlag);
535
537
544 size_t addTriangle(const helios::vec3 &vertex0, const helios::vec3 &vertex1, const helios::vec3 &vertex2, const helios::RGBcolor &color, CoordinateSystem coordFlag);
545
547
554 size_t addTriangle(const helios::vec3 &vertex0, const helios::vec3 &vertex1, const helios::vec3 &vertex2, const helios::RGBAcolor &color, CoordinateSystem coordFlag);
555
557
567 size_t addTriangle(const helios::vec3 &vertex0, const helios::vec3 &vertex1, const helios::vec3 &vertex2, const char *texture_file, const helios::vec2 &uv0, const helios::vec2 &uv1, const helios::vec2 &uv2, CoordinateSystem coordFlag);
568
570
581 size_t addTriangle(const helios::vec3 &vertex0, const helios::vec3 &vertex1, const helios::vec3 &vertex2, const char *texture_file, const helios::vec2 &uv0, const helios::vec2 &uv1, const helios::vec2 &uv2, const helios::RGBAcolor &color,
582 CoordinateSystem coordFlag);
583
585
592 std::vector<size_t> addVoxelByCenter(const helios::vec3 &center, const helios::vec3 &size, const helios::SphericalCoord &rotation, const helios::RGBcolor &color, CoordinateSystem coordFlag);
593
595
602 std::vector<size_t> addVoxelByCenter(const helios::vec3 &center, const helios::vec3 &size, const helios::SphericalCoord &rotation, const helios::RGBAcolor &color, CoordinateSystem coordFlag);
603
605
611 size_t addLine(const helios::vec3 &start, const helios::vec3 &end, const helios::RGBcolor &color, CoordinateSystem coordinate_system);
612
614
620 size_t addLine(const helios::vec3 &start, const helios::vec3 &end, const helios::RGBAcolor &color, CoordinateSystem coordFlag);
621
623
630 size_t addLine(const helios::vec3 &start, const helios::vec3 &end, const helios::RGBcolor &color, float line_width, CoordinateSystem coordinate_system);
631
633
640 size_t addLine(const helios::vec3 &start, const helios::vec3 &end, const helios::RGBAcolor &color, float line_width, CoordinateSystem coordFlag);
641
643
649 size_t addPoint(const helios::vec3 &position, const helios::RGBcolor &color, float pointsize, CoordinateSystem coordinate_system);
650
652
658 size_t addPoint(const helios::vec3 &position, const helios::RGBAcolor &color, float pointsize, CoordinateSystem coordinate_system);
659
661
668 std::vector<size_t> addSphereByCenter(float radius, const helios::vec3 &center, uint Ndivisions, const helios::RGBcolor &color, CoordinateSystem coordinate_system);
669
671
678 std::vector<size_t> addSphereByCenter(float radius, const helios::vec3 &center, uint Ndivisions, const helios::RGBAcolor &color, CoordinateSystem coordinate_system);
679
681
687 std::vector<size_t> addSkyDomeByCenter(float radius, const helios::vec3 &center, uint Ndivisions, const char *texture_file);
688
690
691 DEPRECATED(void addSkyDomeByCenter(float radius, const helios::vec3 &center, uint Ndivisions, const char *texture_file, int layer));
692
694
702 std::vector<size_t> addTextboxByCenter(const char *textstring, const helios::vec3 &center, const helios::SphericalCoord &rotation, const helios::RGBcolor &fontcolor, uint fontsize, const char *fontname, CoordinateSystem coordinate_system);
703
705
708 void deleteGeometry(size_t geometry_id);
709
711 void addCoordinateAxes();
712
714
719 void addCoordinateAxes(const helios::vec3 &origin, const helios::vec3 &length, const std::string &sign);
720
723
725
730 void addGridWireFrame(const helios::vec3 &center, const helios::vec3 &size, const helios::int3 &subdiv);
731
733 void enableColorbar();
734
736 void disableColorbar();
737
739
742 void setColorbarPosition(helios::vec3 position);
743
745
748 void setColorbarSize(helios::vec2 size);
749
751
755 void setColorbarRange(float cmin, float cmax);
756
758
762 void setColorbarTicks(const std::vector<float> &ticks);
763
765
768 void setColorbarTitle(const char *title);
769
771
775
777
780 void setColorbarFontSize(uint font_size);
781
783
787 void setColormap(Ctable colormap_name);
788
790
794 void setColormap(const std::vector<helios::RGBcolor> &colors, const std::vector<float> &divisions);
795
797 [[nodiscard]] Colormap getCurrentColormap() const;
798
800
803 void buildContextGeometry(helios::Context *context_ptr);
804
806
810 void buildContextGeometry(helios::Context *context_ptr, const std::vector<uint> &UUIDs);
811
813
819
821
825 void colorContextPrimitivesByData(const char *data_name);
826
828
833 void colorContextPrimitivesByData(const char *data_name, const std::vector<uint> &UUIDs);
834
836
840 void colorContextPrimitivesByObjectData(const char *data_name);
841
843
848 void colorContextPrimitivesByObjectData(const char *data_name, const std::vector<uint> &ObjIDs);
849
851
855 void colorContextPrimitivesRandomly(const std::vector<uint> &UUIDs);
856
858
862
864
867 void colorContextObjectsRandomly(const std::vector<uint> &ObjIDs);
868
870
874
876 void hideWatermark();
877
879 void showWatermark();
880
882 void updateWatermark();
883
885 std::vector<helios::vec3> plotInteractive();
886
888
891 void plotOnce(bool getKeystrokes);
892
894
897 void plotDepthMap();
898
900 void plotUpdate();
901
903
906 void plotUpdate(bool hide_window);
907
909 void printWindow();
910
912
916 void printWindow(const char *outfile) const;
917
929 void displayImage(const std::vector<unsigned char> &pixel_data, uint width_pixels, uint height_pixels);
930
938 void displayImage(const std::string &file_name);
939
941
945 void getWindowPixelsRGB(uint *buffer) const;
946
948
951 DEPRECATED(void getDepthMap(float *buffer));
952
953 void getDepthMap(std::vector<float> &depth_pixels, uint &width_pixels, uint &height_pixels);
954
956
960 void getWindowSize(uint &width, uint &height) const;
961
963
967 void getFramebufferSize(uint &width, uint &height) const;
968
970 void clearGeometry();
971
974
976 void closeWindow() const;
977
983 [[nodiscard]] helios::RGBcolor getBackgroundColor() const;
984
990 [[nodiscard]] std::vector<helios::vec3> getCameraPosition() const;
991
995 void clearColor();
996
1002 [[nodiscard]] void *getWindow() const;
1003
1009 [[nodiscard]] glm::mat4 getPerspectiveTransformationMatrix() const;
1010
1012
1016 void setPointCullingEnabled(bool enabled);
1017
1022 void setPointCullingThreshold(size_t threshold);
1023
1028 void setPointMaxRenderDistance(float distance);
1029
1034 void setPointLODFactor(float factor);
1035
1042 void getPointRenderingMetrics(size_t &total_points, size_t &rendered_points, float &culling_time_ms) const;
1043
1044private:
1050 [[nodiscard]] std::vector<uint> getFrameBufferSize() const;
1051
1058 void setFrameBufferSize(int width, int height);
1059
1065 [[nodiscard]] Shader getPrimaryShader() const;
1066
1072 [[nodiscard]] glm::mat4 getViewMatrix() const;
1073
1079 [[nodiscard]] std::vector<LightingModel> getPrimaryLightingModel();
1080
1086 [[nodiscard]] uint getDepthTexture() const;
1087
1088 void openWindow();
1089
1091 static void framebufferResizeCallback(GLFWwindow *window, int width, int height);
1092
1100 static void windowResizeCallback(GLFWwindow *window, int width, int height);
1101
1111 void initialize(uint window_width_pixels, uint window_height_pixels, int aliasing_samples, bool window_decorations, bool headless_mode);
1112
1118 void render(bool shadow) const;
1119
1126 void transferBufferData();
1127
1129 void transferTextureData();
1130
1137 [[nodiscard]] uint registerTextureImage(const std::string &texture_file);
1138
1147 [[nodiscard]] uint registerTextureImage(const std::vector<unsigned char> &texture_data, const helios::uint2 &image_resolution);
1148
1155 [[nodiscard]] uint registerTextureTransparencyMask(const std::string &texture_file);
1156
1163 [[nodiscard]] uint registerTextureGlyph(const Glyph *glyph);
1164
1171 [[nodiscard]] helios::uint2 getTextureResolution(uint textureID) const;
1172
1173 //~~~~~~~~~~~~~~~~ Primitives ~~~~~~~~~~~~~~~~~~~~//
1174
1175 std::string colorPrimitivesByObjectData, colorPrimitivesByData;
1176 std::map<uint, uint> colorPrimitives_UUIDs, colorPrimitives_objIDs;
1177
1178 std::vector<uint> contextUUIDs_build;
1179
1180 std::vector<float> depth_buffer_data;
1181
1182 void getViewKeystrokes(helios::vec3 &eye, helios::vec3 &center);
1183
1194 std::vector<size_t> addColorbarByCenter(const char *title, const helios::vec2 &size, const helios::vec3 &center, const helios::RGBcolor &font_color, const Colormap &colormap);
1195
1196 void updateDepthBuffer();
1197
1199 uint Wdisplay;
1201 uint Hdisplay;
1202
1204 uint Wframebuffer;
1206 uint Hframebuffer;
1207
1208 helios::uint2 shadow_buffer_size;
1209
1210 uint frame_counter;
1211
1213
1214 void *window;
1215
1217 helios::vec3 camera_lookat_center;
1218
1220 helios::vec3 camera_eye_location;
1221
1223 float minimum_view_radius;
1224
1226 Shader primaryShader;
1227
1229 Shader depthShader;
1230
1231 Shader *currentShader;
1232
1233 uint framebufferID;
1234 uint depthTexture;
1235
1237 std::vector<LightingModel> primaryLightingModel;
1238
1239 float lightintensity = 1.f;
1240
1241 bool isWatermarkVisible;
1242
1244 size_t watermark_ID;
1245
1247 helios::RGBcolor backgroundColor;
1248
1250 helios::vec3 light_direction;
1251
1253 std::vector<size_t> coordinate_axes_IDs;
1254
1256
1258 uint colorbar_flag;
1259
1261 std::string colorbar_title;
1262
1264 uint colorbar_fontsize;
1265
1267 float point_width;
1268
1270 bool point_culling_enabled;
1271 size_t point_culling_threshold;
1272 float point_max_render_distance;
1273 float point_lod_factor;
1274
1276 mutable size_t points_total_count;
1277 mutable size_t points_rendered_count;
1278 mutable float last_culling_time_ms;
1279
1281 helios::RGBcolor colorbar_fontcolor;
1282
1284 helios::vec3 colorbar_position;
1285
1287 helios::vec2 colorbar_size;
1288
1290 std::vector<size_t> colorbar_IDs;
1291
1293 std::vector<GLuint> face_index_buffer, vertex_buffer, uv_buffer;
1295 std::vector<GLuint> color_buffer, normal_buffer, texture_flag_buffer, texture_ID_buffer, coordinate_flag_buffer, hidden_flag_buffer;
1297 std::vector<GLuint> color_texture_object, normal_texture_object, texture_flag_texture_object, texture_ID_texture_object, coordinate_flag_texture_object, hidden_flag_texture_object;
1298
1300 GLuint uv_rescale_buffer;
1301 GLuint uv_rescale_texture_object;
1302
1304 std::vector<GLint> rectangle_vertex_group_firsts;
1305 std::vector<GLint> rectangle_vertex_group_counts;
1306
1312 [[nodiscard]] glm::mat4 computeShadowDepthMVP() const;
1313
1314 void updatePerspectiveTransformation(bool shadow);
1315
1317 void cullPointsByFrustum();
1318 void cullPointsByDistance(float maxDistance, float lodFactor);
1319 void updatePointCulling();
1320 std::vector<glm::vec4> extractFrustumPlanes() const;
1321
1322 glm::mat4 perspectiveTransformationMatrix;
1323
1324 glm::mat4 cameraViewMatrix;
1325 glm::mat4 cameraProjectionMatrix;
1326
1327 void updateCustomTransformation(const glm::mat4 &matrix);
1328
1329 glm::mat4 customTransformationMatrix;
1330
1332 float camera_FOV;
1333
1334 bool build_all_context_geometry = false;
1335
1336 bool primitiveColorsNeedUpdate;
1337
1338 helios::Context *context;
1339
1341 void buildContextGeometry_private();
1342
1343 float colorbar_min;
1344 float colorbar_max;
1345 std::vector<float> colorbar_ticks;
1346
1348 Colormap colormap_current;
1349
1351 Colormap colormap_hot;
1352
1354 Colormap colormap_cool;
1355
1357 Colormap colormap_lava;
1358
1360 Colormap colormap_rainbow;
1361
1363 Colormap colormap_parula;
1364
1366 Colormap colormap_gray;
1367
1368 bool message_flag;
1369
1371 bool headless;
1372
1373 GeometryHandler geometry_handler;
1374
1375 GLuint texArray;
1376 size_t texture_array_layers;
1377 bool textures_dirty;
1378
1379 helios::uint2 maximum_texture_size;
1380
1381 const glm::mat4 biasMatrix = {0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0};
1382
1383 struct Texture {
1384
1401 explicit Texture(const std::string &texture_file, uint textureID, const helios::uint2 &maximum_texture_size, bool loadalphaonly = false);
1402
1415 explicit Texture(const Glyph *glyph_ptr, uint textureID, const helios::uint2 &maximum_texture_size);
1416
1428 explicit Texture(const std::vector<unsigned char> &pixel_data, uint textureID, const helios::uint2 &image_resolution, const helios::uint2 &maximum_texture_size);
1429
1431 std::string texture_file;
1433 Glyph glyph;
1435 helios::uint2 texture_resolution;
1437 uint textureID;
1439 std::vector<unsigned char> texture_data;
1441 unsigned char num_channels;
1442
1451 void resizeTexture(const helios::uint2 &new_image_resolution);
1452 };
1453
1459 std::unordered_map<uint, Texture> texture_manager;
1460
1461 friend struct Shader;
1462 friend struct Texture;
1463};
1464
1465inline glm::vec3 glm_vec3(const helios::vec3 &v) {
1466 return {v.x, v.y, v.z};
1467}
1468
1469
1470int checkerrors();
1471
1473void check_opengl_errors_safe(const std::string &context);
1474
1475
1476#endif