From 8f0a82da441544162178134a514106c04b961ea7 Mon Sep 17 00:00:00 2001 From: Tristan Youngs Date: Fri, 26 Aug 2016 13:46:45 +0100 Subject: [PATCH] Fixed - Setting view origin was incorrect within periodic systems. --- src/gui/viewer_model.cpp | 9 +++++---- src/model/model.h | 4 ++-- src/model/transform.cpp | 6 +++--- src/model/view.cpp | 11 ++++++----- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/gui/viewer_model.cpp b/src/gui/viewer_model.cpp index 796c5986e..50aeaa1c6 100644 --- a/src/gui/viewer_model.cpp +++ b/src/gui/viewer_model.cpp @@ -52,9 +52,9 @@ void Viewer::renderModel(Model* source, int viewPortX, int viewPortY, int viewPo // Setup view for model, in the supplied viewport source->setupView(viewPortX, viewPortY, viewPortWidth, viewPortHeight); - // Set initial transformation matrix, including any translation occurring from cell... + // Set initial transformation matrix, including any translation occurring from cell or viewOrigin... modelTransformationMatrix_ = source->modelViewMatrix(); - modelTransformationMatrix_.applyTranslation(-source->cell().centre() - source->viewOrigin()); + modelTransformationMatrix_.applyTranslation(-source->viewOriginOrCellOrigin()); // Set target matrix mode and reset it, and set colour mode glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); @@ -106,10 +106,11 @@ void Viewer::renderModel(Model* source, int viewPortX, int viewPortY, int viewPo colour[2] = 0.0f; colour[3] = 1.0f; - // Set current view matrix to account for model rotation, cell axes size, and translation to cell LLC + // Set current view matrix to account for model rotation, cell axes size, and translation to view origin Matrix A = source->modelViewMatrix() * source->cell().axes(); glLoadMatrixd(A.matrix()); - glTranslated(-0.5, -0.5, -0.5); + Vec3 translation = source->cell().inverse() * source->viewOriginOrCellOrigin(); + glTranslated(-translation.x, -translation.y, -translation.z); // Draw a wire cube for the cell glColor4fv(colour); diff --git a/src/model/model.h b/src/model/model.h index 6f8281dd0..4ffd901ed 100644 --- a/src/model/model.h +++ b/src/model/model.h @@ -523,8 +523,8 @@ class Model : public ListItem void setCommonViewMatrixFromLocal(); // Set view origin void setViewOrigin(Vec3 origin); - // Return view origin - Vec3 viewOrigin(); + // Return view origin, or cell origin if no view origin is defined + Vec3 viewOriginOrCellOrigin(); /* diff --git a/src/model/transform.cpp b/src/model/transform.cpp index 9d04fbee9..a2e3cdf98 100644 --- a/src/model/transform.cpp +++ b/src/model/transform.cpp @@ -106,7 +106,7 @@ void Model::rotateSelectionWorld(double dx, double dy) // Rotate this atom's position about the geometric centre of all selected atoms. newr = (rotmat * (modelToWorld(ri->item->r()) - transformationCentre_)) + transformationCentre_; newr = inverse.transform(newr); - ri->item->r() = newr + cell_.centre() + viewOrigin_; + ri->item->r() = newr + viewOriginOrCellOrigin(); } // Update model measurements @@ -162,7 +162,7 @@ void Model::rotateSelectionZaxis(double dz) // Rotate this atom's position about the geometric centre of all selected atoms. newr = (rotmat * (modelToWorld(ri->item->r()) - transformationCentre_)) + transformationCentre_; newr = inverse.transform(newr); - ri->item->r() = newr + cell_.centre() + viewOrigin_; + ri->item->r() = newr + viewOriginOrCellOrigin(); } // Update model measurements @@ -185,7 +185,7 @@ void Model::translateSelectionWorld(const Vec3& v, bool markonly) for (RefListItem* ri = selection(markonly); ri != NULL; ri = ri->next) { newr = modelToWorld(ri->item->r()) + v; - newr = inverse * newr + cell_.centre() + viewOrigin_; + newr = inverse * newr + viewOriginOrCellOrigin(); positionAtom(ri->item, newr); } diff --git a/src/model/view.cpp b/src/model/view.cpp index ee5f291d1..ec5455e0c 100644 --- a/src/model/view.cpp +++ b/src/model/view.cpp @@ -321,7 +321,7 @@ Vec3& Model::modelToWorld(Vec3& modelr, Vec4* screenr, d // Get the world coordinates of the atom - Multiply by modelview matrix 'view' vmat = modelViewMatrix(); - Vec3 translation = -cell_.centre() - viewOrigin_; + Vec3 translation = -viewOriginOrCellOrigin(); vmat.applyTranslation(translation); temp = vmat * pos; worldr.set(temp.x, temp.y, temp.z); @@ -360,7 +360,7 @@ Vec3& Model::screenToModel(int x, int y, double z) // Grab transformation matrix, apply cell centre correction, and invert Matrix itransform = modelViewMatrix_; - Vec3 translation = -cell_.centre() - viewOrigin_; + Vec3 translation = -viewOriginOrCellOrigin(); itransform.applyTranslation(translation); itransform.invert(); @@ -431,9 +431,10 @@ void Model::setViewOrigin(Vec3 origin) viewOrigin_ = origin; } -// Return view origin -Vec3 Model::viewOrigin() +// Return view origin, or cell origin if no view origin is defined +Vec3 Model::viewOriginOrCellOrigin() { - return viewOrigin_; + if (viewOrigin_.magnitude() > 1.0e-4) return viewOrigin_; + else return cell_.centre(); }