Skip to content

Commit

Permalink
Fixed - Setting view origin was incorrect within periodic systems.
Browse files Browse the repository at this point in the history
  • Loading branch information
trisyoungs committed Aug 26, 2016
1 parent 5779c02 commit 8f0a82d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/gui/viewer_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<double> translation = source->cell().inverse() * source->viewOriginOrCellOrigin();
glTranslated(-translation.x, -translation.y, -translation.z);

// Draw a wire cube for the cell
glColor4fv(colour);
Expand Down
4 changes: 2 additions & 2 deletions src/model/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ class Model : public ListItem<Model>
void setCommonViewMatrixFromLocal();
// Set view origin
void setViewOrigin(Vec3<double> origin);
// Return view origin
Vec3<double> viewOrigin();
// Return view origin, or cell origin if no view origin is defined
Vec3<double> viewOriginOrCellOrigin();


/*
Expand Down
6 changes: 3 additions & 3 deletions src/model/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -185,7 +185,7 @@ void Model::translateSelectionWorld(const Vec3<double>& v, bool markonly)
for (RefListItem<Atom,int>* 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);
}

Expand Down
11 changes: 6 additions & 5 deletions src/model/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ Vec3<double>& Model::modelToWorld(Vec3<double>& modelr, Vec4<double>* screenr, d

// Get the world coordinates of the atom - Multiply by modelview matrix 'view'
vmat = modelViewMatrix();
Vec3<double> translation = -cell_.centre() - viewOrigin_;
Vec3<double> translation = -viewOriginOrCellOrigin();
vmat.applyTranslation(translation);
temp = vmat * pos;
worldr.set(temp.x, temp.y, temp.z);
Expand Down Expand Up @@ -360,7 +360,7 @@ Vec3<double>& Model::screenToModel(int x, int y, double z)

// Grab transformation matrix, apply cell centre correction, and invert
Matrix itransform = modelViewMatrix_;
Vec3<double> translation = -cell_.centre() - viewOrigin_;
Vec3<double> translation = -viewOriginOrCellOrigin();
itransform.applyTranslation(translation);
itransform.invert();

Expand Down Expand Up @@ -431,9 +431,10 @@ void Model::setViewOrigin(Vec3<double> origin)
viewOrigin_ = origin;
}

// Return view origin
Vec3<double> Model::viewOrigin()
// Return view origin, or cell origin if no view origin is defined
Vec3<double> Model::viewOriginOrCellOrigin()
{
return viewOrigin_;
if (viewOrigin_.magnitude() > 1.0e-4) return viewOrigin_;
else return cell_.centre();
}

0 comments on commit 8f0a82d

Please sign in to comment.