1 // Copyright 2019-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <functional>
7 #include <string>
8 #include <vector>
9 
10 #include <GLFW/glfw3.h>
11 #ifdef _WIN32
12 #undef APIENTRY
13 #define GLFW_EXPOSE_NATIVE_WIN32
14 #define GLFW_EXPOSE_NATIVE_WGL
15 #include <GLFW/glfw3native.h>
16 #endif
17 
18 #include "rkcommon/math/range.h"
19 #include "rkcommon/math/vec.h"
20 #include "sg/scene/transfer_function/TransferFunction.h"
21 
22 using namespace rkcommon::math;
23 
24 using ColorPoint   = vec4f;
25 using OpacityPoint = vec2f;
26 
27 class TransferFunctionWidget
28 {
29  public:
30   TransferFunctionWidget(
31       std::function<void(const range1f &, const std::vector<vec4f> &)>
32           transferFunctionUpdatedCallback,
33       const range1f &valueRange     = range1f(-1.f, 1.f),
34       const std::string &widgetName = "Transfer Function");
35   ~TransferFunctionWidget();
36 
37   // update UI and process any UI events
38   void updateUI();
39 
40   // setters/getters for current transfer function data
41   void setValueRange(const range1f &);
42   void setColorsAndOpacities(const std::vector<vec4f> &);
43   range1f getValueRange();
44   std::vector<vec4f> getSampledColorsAndOpacities(int numSamples = 256);
45 
46  private:
47   void loadDefaultMaps();
48   void setMap(int);
49 
50   vec3f interpolateColor(const std::vector<ColorPoint> &controlPoints, float x);
51 
52   float interpolateOpacity(const std::vector<OpacityPoint> &controlPoints,
53                            float x);
54 
55   void updateTfnPaletteTexture();
56 
57   void drawEditor();
58 
59   // callback called whenever transfer function is updated
60   std::function<void(const range1f &, const std::vector<vec4f> &)>
61       transferFunctionUpdatedCallback{nullptr};
62 
63   // all available transfer functions
64   std::vector<ospray::sg::NodePtr> tfnsNodes;
65   std::vector<std::string> tfnsNames;
66   std::vector<std::vector<ColorPoint>> tfnsColorPoints;
67   std::vector<std::vector<OpacityPoint>> tfnsOpacityPoints;
68   std::vector<bool> tfnsEditable;
69 
70   // properties of currently selected transfer function
71   int currentMap{0};
72   std::vector<ColorPoint> *tfnColorPoints;
73   std::vector<OpacityPoint> *tfnOpacityPoints;
74   bool tfnEditable{true};
75 
76   // flag indicating transfer function has changed in UI
77   bool tfnChanged{true};
78 
79   // scaling factor for generated opacities
80   float globalOpacityScale{1.f};
81 
82   // domain (value range) of transfer function
83   range1f valueRange{-1.f, 1.f};
84 
85   // texture for displaying transfer function color palette
86   GLuint tfnPaletteTexture{0};
87 
88   // widget name (use different names to support multiple concurrent widgets)
89   std::string widgetName;
90 };
91