caf: Add support for user controlled fixed rotation sensitivity in trackball based navigations

This commit is contained in:
Jacob Støren 2019-10-09 19:27:13 +02:00
parent 35004ef7b2
commit f5f8df43a1
2 changed files with 37 additions and 24 deletions

View File

@ -335,32 +335,39 @@ double caf::RotationSensitivityCalculator::calculateSensitivity(QMouseEvent* eve
if ( m_isEnabled )
{
#if QT_VERSION >= 0x050000
auto presentTime = eventWhenRotating->timestamp();
unsigned long timeSinceLast = presentTime - m_lastTime;
if ( timeSinceLast == 0 ) timeSinceLast = 1; // one millisecond
if ( m_fixedSensitivity == std::numeric_limits<double>::infinity() )
{
#if QT_VERSION >= 0x050000
auto presentTime = eventWhenRotating->timestamp();
unsigned long timeSinceLast = presentTime - m_lastTime;
if ( timeSinceLast == 0 ) timeSinceLast = 1; // one millisecond
int deltaX = eventWhenRotating->x() - m_lastPosX;
int deltaY = eventWhenRotating->y() - m_lastPosY;
int deltaX = eventWhenRotating->x() - m_lastPosX;
int deltaY = eventWhenRotating->y() - m_lastPosY;
cvf::Vec2d mouseVelocity(deltaX, deltaY);
mouseVelocity /= 1.0e-3*timeSinceLast;
cvf::Vec2d mouseVelocity(deltaX, deltaY);
mouseVelocity /= 1.0e-3*timeSinceLast;
double mouseVelocityLength = mouseVelocity.length();
double mouseVelocityLengthCorr = 0.3*mouseVelocityLength + 0.7*m_lastMouseVelocityLenght;
double mouseVelocityLength = mouseVelocity.length();
double mouseVelocityLengthCorr = 0.1*mouseVelocityLength + 0.9*m_lastMouseVelocityLenght;
double slowLimit = 170.0;
double slowLimit = 170.0;
if ( mouseVelocityLengthCorr < slowLimit ) sensitivity = mouseVelocityLengthCorr*mouseVelocityLengthCorr/(slowLimit*slowLimit);
if ( mouseVelocityLengthCorr < slowLimit ) sensitivity = mouseVelocityLengthCorr*mouseVelocityLengthCorr/(slowLimit*slowLimit);
m_lastPosX = eventWhenRotating->x();
m_lastPosY = eventWhenRotating->y();
m_lastTime = eventWhenRotating->timestamp();
m_lastMouseVelocityLenght = 0.8*mouseVelocityLength + 0.2*m_lastMouseVelocityLenght;
m_lastPosX = eventWhenRotating->x();
m_lastPosY = eventWhenRotating->y();
m_lastTime = eventWhenRotating->timestamp();
m_lastMouseVelocityLenght = 0.8*mouseVelocityLength + 0.2*m_lastMouseVelocityLenght;
//openDebugWindow();
//std::cout << sensitivity << " Speed: " << mouseVelocity.length() << " " << mouseVelocityLengthCorr << " \tDelta " << deltaX << ", " << deltaY << " "<< timeSinceLast << std::endl;
#endif
//openDebugWindow();
//std::cout << sensitivity << " Speed: Raw: " << mouseVelocityLength << " Smooth: " << mouseVelocityLengthCorr << " \tDelta " << deltaX << ", " << deltaY << " "<< timeSinceLast << std::endl;
#endif
}
else
{
sensitivity = m_fixedSensitivity;
}
}
return sensitivity;

View File

@ -48,6 +48,7 @@ class QMouseEvent;
namespace caf
{
class RotationSensitivityCalculator
{
public:
@ -56,9 +57,12 @@ public:
, m_lastPosY(0)
, m_lastMouseVelocityLenght(200)
, m_isEnabled(false)
, m_fixedSensitivity(std::numeric_limits<double>::infinity())
{}
void enable(bool enable) { m_isEnabled = enable; }
void enableAdaptiveRotationSensitivity(bool enable) { m_isEnabled = enable; m_fixedSensitivity = std::numeric_limits<double>::infinity(); }
void enableFixedSensitivity(double senstivity) { m_isEnabled = true; m_fixedSensitivity = senstivity;}
void init(QMouseEvent* eventAtRotationStart);
double calculateSensitivity(QMouseEvent* eventWhenRotating);
@ -69,6 +73,7 @@ private:
int m_lastPosY;
unsigned long m_lastTime;
double m_lastMouseVelocityLenght;
double m_fixedSensitivity;
};
} // End namespace caf
@ -92,7 +97,8 @@ public:
~TrackBallBasedNavigation() override;
void enableEventEating(bool enable) { m_consumeEvents = enable; }
void enableRotation(bool enable) { m_isRotationEnabled = enable; }
void enableAdaptiveRotationSensitivity(bool enable) { m_roationSensitivityCalculator.enable(enable); }
void enableAdaptiveRotationSensitivity(bool enable) { m_roationSensitivityCalculator.enableAdaptiveRotationSensitivity(enable); }
void enableFixedSensitivity(double senstivity) { m_roationSensitivityCalculator.enableFixedSensitivity(senstivity); }
protected:
// General navigation policy overrides
@ -101,8 +107,8 @@ protected:
void setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ) override;
cvf::Vec3d pointOfInterest() override;
void setPointOfInterest(cvf::Vec3d poi) override;
void updatePointOfInterestDuringZoomIfNecessary(int zoomX, int zoomY);
void forcePointOfInterestUpdateDuringNextWheelZoom();
void updatePointOfInterestDuringZoomIfNecessary(int zoomX, int zoomY);
void forcePointOfInterestUpdateDuringNextWheelZoom();
// Track ball navigation specific
void initializeRotationCenter();