|
Katana Plug-in APIs 0.1
|
00001 // Copyright (c) 2017 The Foundry Visionmongers Ltd. All Rights Reserved. 00002 00003 #ifndef GLTRANSFORMMANIPULATOR_H_ 00004 #define GLTRANSFORMMANIPULATOR_H_ 00005 00006 #include <stdint.h> 00007 #include <string> 00008 00009 #include <FnViewer/utils/FnGLManipulator.h> 00010 #include <FnViewer/plugin/FnMathTypes.h> 00011 00012 #include <ImathVec.h> 00013 #include <ImathMatrix.h> 00014 #include <ImathEuler.h> 00015 #include <ImathMatrixAlgo.h> 00016 00017 namespace Foundry 00018 { 00019 namespace Katana 00020 { 00021 namespace ViewerUtils 00022 { 00023 using namespace Foundry::Katana::ViewerAPI; 00024 00026 enum Orientation { OBJECT = 0, WORLD, VIEW }; 00027 00028 static constexpr std::size_t kSnappingMappingSize = 6; 00029 static const std::array<IMATH_NAMESPACE ::V3d, kSnappingMappingSize> 00030 kSnappingMappingFromIndexToAxis = { 00031 // clang-tidy off 00032 IMATH_NAMESPACE ::V3d(1.0, 0.0, 0.0), 00033 00034 IMATH_NAMESPACE ::V3d(-1.0, 0.0, 0.0), 00035 IMATH_NAMESPACE ::V3d(0.0, 1.0, 0.0), 00036 00037 IMATH_NAMESPACE ::V3d(0.0, -1.0, 0.0), 00038 IMATH_NAMESPACE ::V3d(0.0, 0.0, 1.0), 00039 00040 IMATH_NAMESPACE ::V3d(0.0, 0.0, -1.0), 00041 // clang-tidy on 00042 }; 00043 00045 enum TransformMode { 00046 kObject, // Transform applies to objects (default mode). 00047 kCenterOfInterest, // Transform applies to the center of interest. 00048 kAroundCenterOfInterest, // Transform applies to objects, which will remain 00049 // oriented towards the center of interest. 00050 }; 00051 00056 struct SnappingData 00057 { 00058 SnappingData() : snap(false) {} 00059 bool snap; // Whether the manipulator handle should snap or not. 00060 Vec3d point; // The point the manipulator handle should snap to. 00061 Vec3d normal; // The orientation the manipulator handle should adopt. 00062 }; 00063 00101 class GLTransformManipulator : public GLManipulator 00102 { 00103 00104 public: 00105 00107 GLTransformManipulator(); 00108 00110 virtual ~GLTransformManipulator(); 00111 00117 IMATH_NAMESPACE::Eulerd::Order getRotationOrder( 00118 const std::string& locationPath); 00119 00127 std::string getLastLocationPath(); 00128 00135 virtual void setOption( 00136 Foundry::Katana::ViewerAPI::OptionIdGenerator::value_type optionId, 00137 FnAttribute::Attribute attr); 00138 00139 FnAttribute::Attribute getOption( 00140 Foundry::Katana::ViewerAPI::OptionIdGenerator::value_type optionId) 00141 override; 00142 00150 double getFixedSizeScale(IMATH_NAMESPACE::V3d point); 00151 00159 IMATH_NAMESPACE::M44d getInitialTranslateXformFromLocation( 00160 const std::string& locationPath); 00161 IMATH_NAMESPACE::M44d getTranslateXformFromLocation( 00162 const std::string& locationPath); 00163 IMATH_NAMESPACE::M44d getInitialRotateXformFromLocation( 00164 const std::string& locationPath); 00165 IMATH_NAMESPACE::M44d getRotateXformFromLocation( 00166 const std::string& locationPath); 00167 IMATH_NAMESPACE::M44d getInitialScaleXformFromLocation( 00168 const std::string& locationPath); 00169 IMATH_NAMESPACE::M44d getScaleXformFromLocation( 00170 const std::string& locationPath); 00171 00176 IMATH_NAMESPACE::V3d getInitialPositionFromLocation( 00177 const std::string& locationPath); 00178 00183 IMATH_NAMESPACE::V3d getInitialDirectionFromLocation( 00184 const std::string& locationPath); 00185 00190 IMATH_NAMESPACE::V3d getInitialCoiFromLocation( 00191 const std::string& locationPath); 00192 00198 IMATH_NAMESPACE::V3d getInitialShadowFallPositionFromLocation( 00199 const std::string& locationPath); 00200 00206 IMATH_NAMESPACE::M44d getInitialOrientationXform( 00207 const std::string& locationPath, 00208 Orientation orientation); 00209 00214 IMATH_NAMESPACE::M44d getOrientationXform(const std::string& locationPath, 00215 Orientation orientation); 00216 00221 void applyRotationAroundCoiToAllLocations(double angle, 00222 const IMATH_NAMESPACE::V3d& axis, 00223 Orientation orientation, 00224 bool isFinal); 00225 00233 void applyCoiTranslationToAllLocations(const IMATH_NAMESPACE::V3d& delta, 00234 double distance, 00235 Orientation orientation, 00236 bool isFinal); 00237 00247 void applyTranslationAroundCoiToAllLocations(bool isFinal); 00248 00253 void applyOrientationToLocation(const std::string& locationPath, 00254 const IMATH_NAMESPACE::M44d& rotateMatrix, 00255 bool isFinal); 00256 00260 void setLocationOrientation(const std::string& locationPath, 00261 const IMATH_NAMESPACE::M44d& rotateMatrix, 00262 bool isFinal); 00263 00267 void setInteractiveRotation(const std::string& locationPath, 00268 const IMATH_NAMESPACE::M44d& rotateMatrix, 00269 bool isFinal); 00270 00274 void applyTranslationToLocation(const std::string& locationPath, 00275 const IMATH_NAMESPACE::V3d& deltaWorldSpace, 00276 bool isFinal); 00277 00281 unsigned int getSnappingHitAreaSize() const 00282 { 00283 return m_snappingHitAreaSize; 00284 } 00285 00286 IMATH_NAMESPACE::V3d getAxisNormalOrientation() const 00287 { 00288 // Assert that the index is not out of bounds 00289 assert(m_snappingAxisNormalOrientationIndex < 00290 kSnappingMappingSize); 00291 return kSnappingMappingFromIndexToAxis 00292 [m_snappingAxisNormalOrientationIndex]; 00293 } 00294 00295 IMATH_NAMESPACE::V3d getAxisUpOrientation() const 00296 { 00297 // Assert that the index is not out of bounds 00298 assert(m_snappingAxisUpOrientationIndex < 00299 kSnappingMappingSize); 00300 return kSnappingMappingFromIndexToAxis 00301 [m_snappingAxisUpOrientationIndex]; 00302 } 00303 00304 // Returns a matrix that rotates the "fromDir" vector 00305 // so that it points towards "toDir". You may also 00306 // specify that you want the "fromUpDir" vector to be pointing 00307 // in a certain direction "toUpDir". 00308 // This has been taken from Imath/ImathMatrixAlgo.h:rotationMatrixWithUpDir 00309 // and modified to accept an arbitrary source 'up' direction. 00310 static IMATH_NAMESPACE::M44d rotationMatrixWithUpDir( 00311 const IMATH_NAMESPACE::V3d& fromDir, 00312 const IMATH_NAMESPACE::V3d& toDir, 00313 const IMATH_NAMESPACE::V3d& fromUpDir, 00314 const IMATH_NAMESPACE::V3d& toUpDir = {0.0, 1.0, 0.0}); 00315 00316 protected: 00318 ViewerDelegateWrapperPtr getViewerDelegate(); 00319 00320 private: 00324 void setSnappingAxisNormalOrientation(const FnAttribute::Attribute& attr); 00325 00329 void setSnappingAxisUpOrientation(const FnAttribute::Attribute& attr); 00330 00331 private: 00342 double m_globalScale; 00343 00349 unsigned int m_snappingHitAreaSize = 100; 00350 00357 std::size_t m_snappingAxisNormalOrientationIndex = 4; 00358 00365 std::size_t m_snappingAxisUpOrientationIndex = 2; 00366 }; 00367 00459 class GLTransformManipulatorHandle : public GLManipulatorHandle 00460 { 00461 public: 00470 explicit GLTransformManipulatorHandle(bool alwaysAtLocationOrigin); 00471 00473 virtual ~GLTransformManipulatorHandle(); 00474 00481 virtual void cancelManipulation(); 00482 00492 virtual void draw(); 00493 00503 virtual void pickerDraw(int64_t pickerID); 00504 00512 virtual Orientation getOrientation(); 00513 00521 Vec4f getDisplayColor(const Vec4f& color); 00522 00533 virtual std::string getComponentName() = 0; 00534 00543 double getScale() { return m_manipScale; } 00544 00552 IMATH_NAMESPACE::V3d getOrigin() { return m_manipOrigin; } 00553 00560 void placeOnCenterOfInterest(bool placeOnCoi) 00561 { 00562 m_placeOnCoi = placeOnCoi; 00563 } 00564 00569 bool isPlacedOnCenterOfInterest() const 00570 { 00571 return m_placeOnCoi; 00572 } 00573 00580 void setTransformMode(TransformMode transformMode) 00581 { 00582 m_transformMode = transformMode; 00583 } 00584 00591 TransformMode getTransformMode() { return m_transformMode; } 00592 00593 protected: 00596 static bool isSnapWithOrientation(const SnappingData& snappingData); 00597 00598 protected: 00599 00601 GLTransformManipulator* getGLTransformManipulator(); 00602 00604 bool isParallelToCamera(const IMATH_NAMESPACE::V3d& axis); 00605 00607 bool isPerpendicularToCamera(const IMATH_NAMESPACE::V3d& axis); 00608 00613 IMATH_NAMESPACE::V3d getCameraRayDirection(); 00614 00625 void startDrag(const Vec3d& initialPointOnPlane 00626 , const Vec2i& initialMousePosition); 00627 00639 virtual void drag(const Vec3d& initialPointOnPlane, 00640 const Vec3d& previousPointOnPlane, 00641 const Vec3d& currentPointOnPlane, 00642 const Vec2i& initialMousePosition, 00643 const Vec2i& previousMousePosition, 00644 const Vec2i& currentMousePosition, 00645 bool isFinal) {} 00646 00656 void endDrag(); 00657 00669 void applyXformToAllLocations(const IMATH_NAMESPACE::M44d& xform, 00670 bool isFinal); 00671 00688 virtual void applyXformToLocation(const std::string& locationPath, 00689 const IMATH_NAMESPACE::M44d& xform, 00690 bool isFinal) = 0; 00691 00707 virtual IMATH_NAMESPACE::M44d applyManipulationXform( 00708 const IMATH_NAMESPACE::M44d& xform, 00709 const std::string& locationPath); 00710 00714 int m_handlePriority; 00715 00731 IMATH_NAMESPACE::M44d calculateXform(const std::string& locationPath, 00732 bool includeComponent = false); 00733 00745 virtual void calculateAndSetLocalXform(const std::string& locationPath); 00746 00759 SnappingData pickSnappingTarget(const Vec2i& mousePosition); 00760 00767 void setAllLocationsOrientation(const IMATH_NAMESPACE::M44d& rotateMatrix, 00768 bool isFinal); 00769 00776 void restoreAllLocationsXform(); 00777 00778 private: 00786 virtual IMATH_NAMESPACE::M44d getOrientationXform( 00787 const std::string& location); 00788 00796 void restoreLocationXform(const std::string& locationPath); 00797 00804 void copyLocationAttr(const std::string& locationPath, 00805 const char* attrSrc, 00806 const char* attrDst); 00815 bool m_alwaysAtObjectOrigin; 00816 00817 // Caches the scale of the manipulator. Set by calculateXform(). 00818 double m_manipScale; 00819 00820 // Caches the origin of the manipulator. Set by calculateXform(). 00821 IMATH_NAMESPACE::V3d m_manipOrigin; 00822 00824 bool m_placeOnCoi; 00825 00827 TransformMode m_transformMode; 00828 }; 00829 00830 00831 } 00832 } 00833 } 00834 00835 #endif /* GLTRANSFORMMANIPULATOR_H_ */
1.7.3