77#include " polyscope/utilities.h"
88#include " polyscope/widget.h"
99
10+ #include " ImGuizmo.h"
11+
1012namespace polyscope {
1113
1214// A visual widget with handles for translations/rotations
@@ -16,76 +18,108 @@ class TransformationGizmo : public Widget {
1618public:
1719 // == Constructors
1820
19- TransformationGizmo (std::string name, glm::mat4& T, PersistentValue<glm::mat4>* Tpers = nullptr );
21+ // Construct a gizmo.
22+ //
23+ // If T is null, this gizmo owns its transform matrix which can be accessed via set/getTransform().
24+ // If T is non-null, the gizmo will manipulate that external transform. Optionally, a pointer can also be passed to a
25+ // PersistentValue<glm::mat4>, which will be updated as the transform is changed.
26+ //
27+ // Users creating additional gizmos should not call this, use addTransformationGizmo() instead.
28+ TransformationGizmo (std::string name, glm::mat4* T = nullptr , PersistentValue<glm::mat4>* Tpers = nullptr );
2029
2130
2231 // == Key members
2332
2433 // a unique name
2534 const std::string name;
2635
27- // the main transform encoded by the gizmo
28- PersistentValue<bool > enabled;
36+ // NOTE: this is only meaningful to call on use-created gizmos from addTransformationGizmo(),
37+ // it will have no effect on gizmos created other ways
38+ // After removing, the object is destructed
39+ void remove ();
2940
30- // the main transform encoded by the gizmo
31- // note that this is a reference set on construction; the gizmo wraps a transform defined somewhere else
32- glm::mat4& T;
33- PersistentValue<glm::mat4>* Tpers; // optional, a persistent value defined elsewhere that goes with T
41+ // == Getters and setters
3442
35- // == Member functions
43+ glm::mat4 getTransform ();
44+ void setTransform (glm::mat4 newT);
3645
37- void prepare ();
38- void draw () override ;
39- bool interact () override ;
46+ // the are helpers which access/update only the position component of the transform
47+ glm::vec3 getPosition () ;
48+ void setPosition (glm::vec3 newPos) ;
4049
41- protected:
42- enum class TransformHandle { None, Rotation, Translation, Scale } ;
50+ bool getEnabled ();
51+ void setEnabled ( bool newVal) ;
4352
53+ bool getAllowTranslation ();
54+ void setAllowTranslation (bool newVal);
4455
45- // parameters
46- const float gizmoSizeRel = 0.08 ;
47- const float diskWidthObj = 0.1 ; // in object coordinates, before transformation
48- const float vecLength = 1.5 ;
49- const float sphereRad = 0.32 ;
50- const std::string material = " wax" ;
56+ bool getAllowRotation ();
57+ void setAllowRotation (bool newVal);
5158
52- // state
53- int selectedDim = -1 ; // must be {0,1,2} if selectedType == Rotation/Translation
54- TransformHandle selectedType = TransformHandle::None;
55- bool currentlyDragging = false ;
56- glm::vec3 dragPrevVec{1 ., 0 .,
57- 0 .}; // the normal vector from the previous frame of the drag OR previous translation center
59+ bool getAllowScaling ();
60+ void setAllowScaling (bool newVal);
5861
59- std::array<glm::vec3, 3 > niceRGB = {{glm::vec3{ 211 / 255 ., 45 / 255 ., 62 / 255 .},
60- glm::vec3{ 65 / 255 ., 121 / 255 ., 225 / 255 .},
61- glm::vec3{ 95 / 255 ., 175 / 255 ., 35 / 255 .}}} ;
62+ // sadly this is not really supported by ImGuizmo
63+ // bool getUniformScaling();
64+ // void setUniformScaling(bool newVal) ;
6265
63- void markUpdated ();
66+ bool getInteractInLocalSpace ();
67+ void setInteractInLocalSpace (bool newVal);
6468
65- // Render stuff
66- std::shared_ptr<render::ShaderProgram> ringProgram;
67- std::shared_ptr<render::ShaderProgram> arrowProgram;
68- std::shared_ptr<render::ShaderProgram> sphereProgram;
69+ // Size is relative, with 1.0 as the default size
70+ float getGizmoSize ();
71+ void setGizmoSize (float newVal);
6972
70- // Geometry helpers used to test hits
73+ // == Member functions
7174
72- // returns <tRay, distNearest, nearestPoint>
73- std::tuple< float , float , glm::vec3> circleTest (glm::vec3 raySource, glm::vec3 rayDir, glm::vec3 center,
74- glm::vec3 normal, float radius) ;
75- std::tuple< float , float , glm::vec3> lineTest (glm::vec3 raySource, glm::vec3 rayDir, glm::vec3 center,
76- glm::vec3 tangent, float length );
77- std::tuple< float , float , glm::vec3> sphereTest (glm::vec3 raySource, glm::vec3 rayDir, glm::vec3 center, float radius,
78- bool allowHitSurface = true );
75+ std::string uniquePrefix () override ;
76+ void draw () override ;
77+ bool interact () override ;
78+ void buildUI () override ;
79+ void buildInlineTransformUI ( );
80+ void buildMenuItems ();
81+ void markUpdated ( );
7982
83+ protected:
84+ // The main transform encoded by the gizmo
85+ // This can be either a reference to an external wrapped transform, or the internal storage below
86+ glm::mat4& Tref;
8087
81- std::tuple<std::vector<glm::vec3>, std::vector<glm::vec3>, std::vector<glm::vec3>, std::vector<glm::vec2>,
82- std::vector<glm::vec3>>
83- triplePlaneCoords () ;
88+ // Optional, a persistent value defined elsewhere that goes with T, which
89+ // will be marked as updated when the gizmo changes the transform.
90+ PersistentValue<glm::mat4>* Tpers ;
8491
85- std::tuple<std::vector<glm::vec3>, std::vector<glm::vec3>, std::vector<glm::vec3>, std::vector<glm::vec3>>
86- tripleArrowCoords ();
92+ // Local stoarage for a transformation.
93+ // This may or may not be used; based on the constructor args this class may wrap an external transform, or simply use
94+ // this one.
95+ glm::mat4 Towned = glm::mat4(1.0 );
8796
88- // std::tuple<std::vector<glm::vec3>, std::vector<glm::vec3>> unitCubeCoords();
97+ // options
98+ PersistentValue<bool > enabled;
99+ PersistentValue<bool > allowTranslation;
100+ PersistentValue<bool > allowRotation;
101+ PersistentValue<bool > allowScaling;
102+ // PersistentValue<bool> uniformScaling; // not really supported by the ImGuizmo
103+ PersistentValue<bool > interactInLocalSpace;
104+ PersistentValue<bool > showUIWindow;
105+ PersistentValue<float > gizmoSize;
106+
107+ // internal
108+ bool lastInteractResult = false ;
89109};
90110
111+ // Create a user-defined transformation gizmo in the scene.
112+ // By default, the gizmo maintains its own transformation matrix which
113+ // can be accessed by by .getTransform(). Optionally, it can instead wrap
114+ // and existin transform passed as transformToWrap.
115+ TransformationGizmo* addTransformationGizmo (std::string name = " " , glm::mat4* transformToWrap = nullptr );
116+
117+ // Get a user-created transformation gizmo by name
118+ TransformationGizmo* getTransformationGizmo (std::string name);
119+
120+ // Remove a user-created transformation gizmo
121+ void removeTransformationGizmo (TransformationGizmo* gizmo);
122+ void removeTransformationGizmo (std::string name);
123+ void removeAllTransformationGizmos ();
124+
91125} // namespace polyscope
0 commit comments