Skip to content

Commit

Permalink
Refactoring viewport to improve navigation. Re #6137.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Mar 8, 2013
1 parent 67ff7c5 commit e34a5e4
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 174 deletions.
10 changes: 6 additions & 4 deletions Code/Mantid/MantidPlot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ set ( MANTID_SRCS src/Mantid/AbstractMantidLog.cpp
src/Mantid/InstrumentWidget/GLActorVisitor.cpp
src/Mantid/InstrumentWidget/GLColor.cpp
src/Mantid/InstrumentWidget/GLObject.cpp
src/Mantid/InstrumentWidget/GLTrackball.cpp
src/Mantid/InstrumentWidget/GLViewport.cpp
#src/Mantid/InstrumentWidget/GLTrackball.cpp
#src/Mantid/InstrumentWidget/GLViewport.cpp
src/Mantid/InstrumentWidget/ICompAssemblyActor.cpp
src/Mantid/InstrumentWidget/InputController.cpp
src/Mantid/InstrumentWidget/InstrumentActor.cpp
Expand Down Expand Up @@ -228,6 +228,7 @@ set ( MANTID_SRCS src/Mantid/AbstractMantidLog.cpp
src/Mantid/InstrumentWidget/Shape2D.cpp
src/Mantid/InstrumentWidget/Shape2DCollection.cpp
src/Mantid/InstrumentWidget/SimpleWidget.cpp
src/Mantid/InstrumentWidget/Viewport.cpp
)

###########################################################################
Expand Down Expand Up @@ -424,8 +425,8 @@ set ( MANTID_HDRS src/Mantid/AbstractMantidLog.h
src/Mantid/InstrumentWidget/GLActorVisitor.h
src/Mantid/InstrumentWidget/GLColor.h
src/Mantid/InstrumentWidget/GLObject.h
src/Mantid/InstrumentWidget/GLTrackball.h
src/Mantid/InstrumentWidget/GLViewport.h
#src/Mantid/InstrumentWidget/GLTrackball.h
#src/Mantid/InstrumentWidget/GLViewport.h
src/Mantid/InstrumentWidget/ICompAssemblyActor.h
src/Mantid/InstrumentWidget/InputController.h
src/Mantid/InstrumentWidget/InstrumentActor.h
Expand Down Expand Up @@ -457,6 +458,7 @@ set ( MANTID_HDRS src/Mantid/AbstractMantidLog.h
src/Mantid/InstrumentWidget/Shape2D.h
src/Mantid/InstrumentWidget/Shape2DCollection.h
src/Mantid/InstrumentWidget/SimpleWidget.h
src/Mantid/InstrumentWidget/Viewport.h
)

###########################################################################
Expand Down
109 changes: 55 additions & 54 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/GLTrackball.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
#include "GLTrackball.h"
#include <cmath>
#include "MantidGeometry/Rendering/OpenGL_Headers.h"
#include "GLViewport.h"
#include "Viewport.h"
#include "OpenGLError.h"

GLTrackball::GLTrackball(GLViewport* parent):_viewport(parent)
#include "MantidGeometry/Rendering/OpenGL_Headers.h"

#include <cmath>

GLTrackball::GLTrackball(Viewport* viewport):m_viewport(viewport)
{
reset();
// Rotation speed defines as such is equal 1 in relative units,
// i.e. the trackball will follow exactly the displacement of the mouse
// on the sceen. The factor 180/M_PI is simply rad to deg conversion. THis
// prevent recalculation of this factor every time a generateRotationTo call is issued.
_rotationspeed=180/M_PI;
_modelCenter=Mantid::Kernel::V3D(0.0,0.0,0.0);
hasOffset=false;
}
GLTrackball::~GLTrackball()
{
m_rotationspeed = 180 / M_PI;
m_modelCenter = Mantid::Kernel::V3D(0.0, 0.0, 0.0);
m_hasOffset = false;
}

void GLTrackball::initRotationFrom(int a,int b)
{
projectOnSphere(a,b,_lastpoint);
projectOnSphere(a,b,m_lastpoint);
}

void GLTrackball::generateRotationTo(int a,int b)
{
Mantid::Kernel::V3D _newpoint;
projectOnSphere(a,b,_newpoint);
Mantid::Kernel::V3D diff(_lastpoint);
Mantid::Kernel::V3D newpoint;
projectOnSphere( a, b, newpoint );
Mantid::Kernel::V3D diff( m_lastpoint );
// Difference between old point and new point
diff-=_newpoint;
diff -= newpoint;
// Angle is given in degrees as the dot product of the two vectors
double angle=_rotationspeed*_newpoint.angle(_lastpoint);
diff=_lastpoint.cross_prod(_newpoint);
double angle = m_rotationspeed * newpoint.angle( m_lastpoint );
diff = m_lastpoint.cross_prod( newpoint );
// Create a quaternion from the angle and vector direction
Mantid::Kernel::Quat temp(angle,diff);
Mantid::Kernel::Quat temp( angle, diff );
// Left multiply
temp*=_quaternion;
temp *= m_quaternion;
// Assignment of _quaternion
_quaternion(temp);
m_quaternion( temp );
// Get the corresponding OpenGL rotation matrix
_quaternion.GLMatrix(&_rotationmatrix[0]);
return;
m_quaternion.GLMatrix( &m_rotationmatrix[0] );
}

void GLTrackball::initTranslateFrom(int a,int b)
{
generateTranslationPoint(a,b,_lastpoint);
generateTranslationPoint(a, b, m_lastpoint);
}

void GLTrackball::generateTranslationTo(int a, int b)
{
Mantid::Kernel::V3D _newpoint;
generateTranslationPoint(a,b,_newpoint);
Mantid::Kernel::V3D newpoint;
generateTranslationPoint(a, b, newpoint);
// This is now the difference
_newpoint-=_lastpoint;
double x,y;
_viewport->getTranslation(x,y);
_viewport->setTranslation(x+_newpoint[0],y+_newpoint[1]);
newpoint -= m_lastpoint;
double x, y;
m_viewport->getTranslation( x, y );
m_viewport->setTranslation( x + newpoint[0], y + newpoint[1] );
}

void GLTrackball::initZoomFrom(int a,int b)
void GLTrackball::initZoomFrom( int a, int b )
{
if (a<=0 || b<=0)
return;
Expand All @@ -70,7 +70,7 @@ void GLTrackball::initZoomFrom(int a,int b)
return;
x=static_cast<double>((_viewport_w-a));
y=static_cast<double>((b-_viewport_h));
_lastpoint(x,y,z);
m_lastpoint(x,y,z);
}

void GLTrackball::generateZoomTo(int a, int b)
Expand All @@ -80,27 +80,27 @@ void GLTrackball::generateZoomTo(int a, int b)
_viewport->getViewport(&_viewport_w,&_viewport_h);
if(a>=_viewport_w || b>=_viewport_h||a <= 0||b<=0)return;
y=static_cast<double>((b-_viewport_h));
if(y==0) y=_lastpoint[1];
double diff= _lastpoint[1]/y ;
if(y==0) y=m_lastpoint[1];
double diff= m_lastpoint[1]/y ;
diff*=_viewport->getZoomFactor();
_viewport->setZoomFactor(diff);
}


void GLTrackball::IssueRotation() const
{
if (_viewport)
if (m_viewport)
{
// Translate if offset is defined
if (hasOffset)
if (m_hasOffset)
{
glTranslated(_modelCenter[0],_modelCenter[1],_modelCenter[2]);
glTranslated( m_modelCenter[0], m_modelCenter[1], m_modelCenter[2] );
}
// Rotate with respect to the centre
glMultMatrixd(_rotationmatrix);
glMultMatrixd( m_rotationmatrix );
// Translate back
if (hasOffset)
glTranslated(-_modelCenter[0],-_modelCenter[1],-_modelCenter[2]);
if ( m_hasOffset )
glTranslated( - m_modelCenter[0], - m_modelCenter[1], - m_modelCenter[2] );
}

OpenGLError::check("GLTrackball::IssueRotation()");
Expand All @@ -109,24 +109,24 @@ void GLTrackball::IssueRotation() const

void GLTrackball::setModelCenter(const Mantid::Kernel::V3D& center)
{
_modelCenter=center;
if (_modelCenter.nullVector())
hasOffset=false;
m_modelCenter = center;
if ( m_modelCenter.nullVector() )
m_hasOffset=false;
else
hasOffset=true;
m_hasOffset=true;
}

Mantid::Kernel::V3D GLTrackball::getModelCenter() const
{
return _modelCenter;
return m_modelCenter;
}

void GLTrackball::projectOnSphere(int a,int b,Mantid::Kernel::V3D& point)
{
// z initiaised to zero if out of the sphere
double x,y,z=0;
int _viewport_w, _viewport_h;
_viewport->getViewport(&_viewport_w,&_viewport_h);
m_viewport->getViewport(_viewport_w, _viewport_h);
x=static_cast<double>((2.0*a-_viewport_w)/_viewport_w);
y=static_cast<double>((_viewport_h-2.0*b)/_viewport_h);
double norm=x*x+y*y;
Expand Down Expand Up @@ -156,25 +156,26 @@ void GLTrackball::generateTranslationPoint(int a,int b,Mantid::Kernel::V3D& poin
y*=factor;
// Assign new values to point
point(x,y,z);
std::cerr << "-------------------------------" << std::endl;
std::cerr << a << ' ' << xmin << ' '<< xmax << std::endl;
std::cerr << _viewport_w << ' ' << factor << std::endl;
std::cerr << point << std::endl;
}

void GLTrackball::setRotationSpeed(double r)
{
// Rotation speed needs to contains conversion to degrees.
//
if (r>0) _rotationspeed=r*180.0/M_PI;
}
void GLTrackball::setViewport(GLViewport* v)
{
if (v) _viewport=v;
if ( r > 0 ) m_rotationspeed = r * 180.0 / M_PI;
}

void GLTrackball::reset()
{
//Reset rotation,scale and translation
_quaternion.init();
_quaternion.GLMatrix(&_rotationmatrix[0]);
_viewport->setTranslation(0.0,0.0);
_viewport->setZoomFactor(1.0);
m_quaternion.init();
m_quaternion.GLMatrix(&m_rotationmatrix[0]);
m_viewport->setTranslation(0.0,0.0);
m_viewport->setZoomFactor(1.0);
}

void GLTrackball::setViewToXPositive()
Expand Down
44 changes: 13 additions & 31 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/GLTrackball.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,15 @@
This class is an implementation of virtual trackball using mouse interactions. It uses Quaternions for
performing the rotations.
Copyright &copy; 2007 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
*/

/// Forward declaration of class GLViewport
class GLViewport;
class Viewport;
///
class GLTrackball
{
public:
GLTrackball(GLViewport* parent=0);
virtual ~GLTrackball();
GLTrackball(Viewport* viewport);
//! Call when the mouse button is pressed to initiate rotation
void initRotationFrom(int,int);
//! Call when the mouse is moving during a rotation
Expand All @@ -51,7 +32,7 @@ class GLTrackball
//! To be called in the application drawing the OpenGL Scene
void IssueRotation() const;
//! Associate the Trackball to a new viewport.
void setViewport(GLViewport*);
//void setViewport(GLViewport*);
//! Call when the mouse button is pressed to initiate zoom
void initZoomFrom(int,int);
//! Call when the mouse motion to issue zoom
Expand Down Expand Up @@ -82,26 +63,27 @@ class GLTrackball
void reset();
//! Rotates a bounding box
void rotateBoundingBox(double& xmin,double& xmax,double& ymin,double& ymax,double& zmin,double& zmax);
Mantid::Kernel::Quat getRotation() const {return _quaternion;}
Mantid::Kernel::Quat getRotation() const {return m_quaternion;}
private:
//! Pointer to the viewport to which the trackball is attached
GLViewport* _viewport;
//! Project a point on the trackball sphere from viewport coordinates x,y
void projectOnSphere(int x,int y, Mantid::Kernel::V3D& p);
//! Generate a 3D point coordinates from coordinates on the viewport.
void generateTranslationPoint(int x,int y, Mantid::Kernel::V3D& p);

//! Pointer to the viewport to which the trackball is attached
Viewport* m_viewport;
//! Previous point selected on sphere
Mantid::Kernel::V3D _lastpoint;
Mantid::Kernel::V3D m_lastpoint;
//! Rotation matrix stored as a quaternion
Mantid::Kernel::Quat _quaternion;
Mantid::Kernel::Quat m_quaternion;
//! Rotation matrix (4x4 stored as linear array) used in OpenGL
double _rotationmatrix[16];
double m_rotationmatrix[16];
//! Rotation speed of the trackball
double _rotationspeed;
double m_rotationspeed;
//! Center of rotation
Mantid::Kernel::V3D _modelCenter;
Mantid::Kernel::V3D m_modelCenter;
//! Is the centre of rotation offcentered
bool hasOffset;
bool m_hasOffset;
};


Expand Down
35 changes: 22 additions & 13 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/GLViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@
#include "MantidKernel/V3D.h"
#include "OpenGLError.h"

GLViewport::GLViewport(int w, int h):mWidth(w),mHeight(h)
GLViewport::GLViewport(int w, int h):
mProjection(GLViewport::ORTHO),
mWidth(w),
mHeight(h),
mZoomFactor(1.0),
mXTrans(0.0),
mYTrans(0.0),
mLeft(-1),
mRight(1),
mBottom(-1),
mTop(1),
mNear(-1),
mFar(1)
{
mProjection=GLViewport::ORTHO;
mZoomFactor=1.0;
mXTrans=0.0;
mYTrans=0.0;
mLeft = -1;
mRight = 1;
mBottom = -1;
mTop = 1;
mNear = -1;
mFar = 1;
}

GLViewport::~GLViewport()
{
}

void GLViewport::resize(int w,int h)
{
mWidth=w;
mHeight=h;
}

/**
* Get the size of the viewport in screen pixels.
* @param w :: Buffer to accept the viewport width value.
* @param h :: Buffer to accept the viewport height value.
*/
void GLViewport::getViewport(int* w, int* h) const
{
*w=mWidth;
Expand Down Expand Up @@ -219,9 +228,9 @@ void GLViewport::issueGL() const
{
glOrtho(xmin,xmax,ymin,ymax,zmin,zmax);

if (OpenGLError::hasError("GLViewport::issueGL()"))
//if (OpenGLError::hasError("GLViewport::issueGL()"))
{
OpenGLError::log() << "Arguments to glOrtho:\n";
OpenGLError::log() << "GL: Arguments to glOrtho:\n";
OpenGLError::log()
<< xmin << ' ' << xmax << '\n'
<< ymin << ' ' << ymax << '\n'
Expand Down

0 comments on commit e34a5e4

Please sign in to comment.