Skip to content

Commit

Permalink
Refactoring translation. Re #6137.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Mar 8, 2013
1 parent e34a5e4 commit 34251b3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ void Projection3D::setLightingModel(bool picking) const
float lamp0_pos[4]={0.0f, 0.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lamp0_pos);

// First light source
// Second light source
// Its a directional light which follows camera position
glEnable(GL_LIGHT1); // Enable opengl second light
float lamp1_diffuse[4]={lamp1_intensity, lamp1_intensity, lamp1_intensity, 1.0f};
Expand Down
88 changes: 62 additions & 26 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/Viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
Viewport::Viewport(int w, int h, ProjectionType type):
m_projectionType( type ),
m_width(w), m_height(h),
m_left(-1), m_right(1), m_bottom(-1), m_top(1), m_near(-1), m_far(1)
m_left(-1), m_right(1), m_bottom(-1), m_top(1), m_near(-1), m_far(1),
m_rotationspeed(180.0 / M_PI),
m_zoomFactor(1.0),
m_xTrans(0.0),
m_yTrans(0.0)
{
m_rotationspeed = 180 / M_PI;
//m_quaternion(30.0,Mantid::Kernel::V3D(1,0.5,0));
m_quaternion.GLMatrix( &m_rotationmatrix[0] );
m_zoomFactor = 1.0;
}

/**
Expand Down Expand Up @@ -95,28 +96,6 @@ void Viewport::correctForAspectRatio(double& xmin, double& xmax, double& ymin, d
}
}

/**
* This will set the projection to perspective.
* UNUSED! as of 2010-11-01.
*
* @param l :: left side of the perspective projection (xmin)
* @param r :: right side of the perspective projection (xmax)
* @param b :: bottom side of the perspective projection (ymin)
* @param t :: top side of the perspective projection (ymax)
* @param nearz :: near side of the perspective Projection (zmin)
* @param farz :: far side of the perspective Projection (zmax)
*/
//void GLViewport::setPrespective(double l,double r,double b,double t,double nearz,double farz)
//{
// mLeft=l;
// mRight=r;
// mBottom=b;
// mTop=t;
// mNear=nearz;
// mFar=farz;
// mProjection=GLViewport::PERSPECTIVE;
//}

Viewport::ProjectionType Viewport::getProjectionType()const
{
return m_projectionType;
Expand Down Expand Up @@ -323,11 +302,21 @@ void Viewport::wheelZoom( int a, int b, int d)
m_zoomFactor *= diff;
}

/**
* Start a trackball rotation from here.
* @param a :: The x mouse coordinate
* @param b :: The y mouse coordinate
*/
void Viewport::initRotationFrom(int a,int b)
{
projectOnSphere(a,b,m_lastpoint);
}

/**
* Generate the rotation matrix to rotate to this point.
* @param a :: The x mouse coordinate
* @param b :: The y mouse coordinate
*/
void Viewport::generateRotationTo(int a,int b)
{
Mantid::Kernel::V3D newpoint;
Expand All @@ -348,3 +337,50 @@ void Viewport::generateRotationTo(int a,int b)
m_quaternion.GLMatrix( &m_rotationmatrix[0] );
}

/**
* Initialize scene translation at a point on the screen
* @param a :: The x mouse coordinate
* @param b :: The y mouse coordinate
*/
void Viewport::initTranslateFrom(int a,int b)
{
generateTranslationPoint(a, b, m_lastpoint);
}

/**
* Generate scene translation such that a point of the last initTranslateFrom
* moved to the new position pointed by the mouse.
* @param a :: The x mouse coordinate
* @param b :: The y mouse coordinate
*/
void Viewport::generateTranslationTo(int a, int b)
{
Mantid::Kernel::V3D newpoint;
generateTranslationPoint(a, b, newpoint);
// This is now the difference
newpoint -= m_lastpoint;
m_xTrans += newpoint[0];
m_yTrans += newpoint[1];
}

void Viewport::generateTranslationPoint(int a, int b, Mantid::Kernel::V3D& point)const
{
double x,y,z=0.0;
double xmin,xmax,ymin,ymax,zmin,zmax;
zmin = m_near;
zmax = m_far;
correctForAspectRatio(xmin,xmax,ymin,ymax);
x=static_cast<double>((xmin+((xmax-xmin)*((double)a/(double)m_width))));
y=static_cast<double>((ymin+((ymax-ymin)*(m_height-b)/m_height)));
double factor=m_zoomFactor;
x*=factor;
y*=factor;
// Assign new values to point
point(x,y,z);
std::cerr << "-------------------------------" << std::endl;
std::cerr << a << ' ' << xmin << ' '<< xmax << std::endl;
std::cerr << m_width << ' ' << factor << std::endl;
std::cerr << point << std::endl;
}


32 changes: 26 additions & 6 deletions Code/Mantid/MantidPlot/src/Mantid/InstrumentWidget/Viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Viewport
/// Clear all transforamtions (rotation, translation. scaling)
void reset();

/* Rotation */

/// Call to set the View to X+ direction
void setViewToXPositive();
/// Call to set the View to Y+ direction
Expand All @@ -47,21 +49,29 @@ class Viewport
/// Call to set the View to Z- direction
void setViewToZNegative();

/// Init rotation at a point on the screen
void initRotationFrom(int a,int b);
/// Generate a new rotation matrix
void generateRotationTo(int a,int b);

/* Zooming */

/// Init zooming with a point on the screen
void initZoomFrom( int a, int b );
/// Generate new zooming factor
void generateZoomTo(int a, int b);
/// Generate zooming factor using mouse wheel
void wheelZoom( int a, int b, int d);

/// Init rotation at a point on the screen
void initRotationFrom(int a,int b);
/// Generate a new rotation matrix
void generateRotationTo(int a,int b);
/* Translation */

/// Call when the mouse button is pressed to translate
void initTranslateFrom(int,int);
/// Call when the mouse is moving during a translation
void generateTranslationTo(int,int);

// void getProjection(double&,double&,double&,double&,double&,double&);
void getInstantProjection(double&,double&,double&,double&,double&,double&)const;
//void setPrespective(double,double,double,double,double,double);
//void setZoomFactor(double);
//double getZoomFactor();
//void setTranslation(double,double);
Expand All @@ -70,8 +80,10 @@ class Viewport
protected:
/// Correct for aspect ratio
void correctForAspectRatio(double& xmin, double& xmax, double& ymin, double& ymax)const;
/// Project a point on a sphere centtered at rotation point
/// Project a point onto a sphere centered at rotation point
void projectOnSphere(int a, int b, Mantid::Kernel::V3D& point) const;
/// Generate a 3D point coordinates from coordinates on the viewport.
void generateTranslationPoint(int x,int y, Mantid::Kernel::V3D& p) const;

/* Projection */

Expand Down Expand Up @@ -99,6 +111,14 @@ class Viewport
/* Zooming */

double m_zoomFactor;

/* Translation */

/// Translation in x direction
double m_xTrans;
/// Translation in y direction
double m_yTrans;

};

#endif /*VIEWPORT_H_*/

0 comments on commit 34251b3

Please sign in to comment.