LOOS  v2.3.2
XForm.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <XForm.hpp>
23 
24 namespace loos {
25 
26  void XForm::push(void) { GMatrix M = stack.back(); stack.push_back(M); _unset = false; }
27  void XForm::pop(void) { stack.pop_back(); _unset = false; }
28  void XForm::load(const GMatrix& m) { stack.back() = m; _unset = false; }
29  void XForm::concat(const GMatrix& m) { stack.back() *= m; _unset = false; }
30 
31  void XForm::premult(const GMatrix& m) { GMatrix t = stack.back(); stack.back() = m * t; _unset = false; }
32 
33  void XForm::identity(void) { GMatrix m; stack.back() = m; _unset = true; }
34 
35  bool XForm::unset(void) const { return(_unset); }
36 
37  void XForm::translate(const greal x, const greal y, const greal z) {
38  GMatrix M;
39 
40  M(0, 3) = x;
41  M(1, 3) = y;
42  M(2, 3) = z;
43  concat(M);
44  }
45 
46  void XForm::translate(const GCoord& g) {
47  translate(g[0], g[1], g[2]);
48  }
49 
50  void XForm::scale(const greal x, const greal y, const greal z) {
51  GMatrix M;
52 
53  M(0,0) = x;
54  M(1,1) = y;
55  M(2,2) = z;
56  concat(M);
57  }
58 
59  void XForm::scale(const GCoord& g) {
60  scale(g[0], g[1], g[2]);
61  }
62 
63 
64  void XForm::rotate(const GCoord& ov, const greal angle) {
65  double l = ov.length();
66  if (l < very_small)
67  throw(std::invalid_argument("Axis of rotation vector must have non-zero length"));
68 
69  GCoord v = ov / ov.length();
70  greal theta = PI * angle / 180.0;
71  greal c = cos(theta);
72  greal s = sin(theta);
73  GMatrix M;
74 
75  M[0] = v.x() * v.x() * (1.0 - c) + c;
76  M[1] = v.x() * v.y() * (1.0 - c) - v.z() * s;
77  M[2] = v.x() * v.z() * (1.0 - c) + v.y() * s;
78 
79  M[4] = v.x() * v.y() * (1.0 - c) + v.z() * s;
80  M[5] = v.y() * v.y() * (1.0 - c) + c;
81  M[6] = v.y() * v.z() * (1.0 - c) - v.x() * s;
82 
83  M[8] = v.x() * v.z() * (1.0 - c) - v.y() * s;
84  M[9] = v.y() * v.z() * (1.0 - c) + v.x() * s;
85  M[10] = v.z() * v.z() * (1.0 - c) + c;
86 
87  concat(M);
88  }
89 
90  void XForm::rotate(const char axis, const greal angle) {
91  switch(axis) {
92  case 'x':
93  case 'X': rotate(GCoord(1,0,0), angle); break;
94 
95  case 'y':
96  case 'Y': rotate(GCoord(0,1,0), angle); break;
97 
98  case 'z':
99  case 'Z': rotate(GCoord(0,0,1), angle); break;
100 
101  default:
102  throw(std::logic_error("Invalid axis in XForm::rotate(const char, const greal)"));
103  }
104  }
105 
107  return(stack.back() * v);
108  }
109 
110  GMatrix XForm::current(void) const {
111  GMatrix M = stack.back();
112  return(M);
113  }
114 
115 }
void rotate(const GCoord &, const greal)
Definition: XForm.cpp:64
void identity(void)
Set the current transform to the identity.
Definition: XForm.cpp:33
void concat(const GMatrix &)
Concatenate (post-multiply) a matrix with the current transform.
Definition: XForm.cpp:29
double length(void) const
Length of the coordinate (as a vector)
Definition: Coord.hpp:423
GCoord transform(const GCoord &)
Transform a GCoord() with the current transformation.
Definition: XForm.cpp:106
void pop(void)
Pop the top matrix off the stack.
Definition: XForm.cpp:27
Specialized 4x4 Matrix class for handling coordinate transforms.
Definition: Coord.hpp:37
void translate(const greal, const greal, const greal)
Translation matrix.
Definition: XForm.cpp:37
GMatrix current(void) const
Get the current trasnformation.
Definition: XForm.cpp:110
void premult(const GMatrix &)
Premultiply the current transform.
Definition: XForm.cpp:31
void push(void)
Push the current matrix onto the stack.
Definition: XForm.cpp:26
void scale(const greal, const greal, const greal)
Scaling.
Definition: XForm.cpp:50
Namespace for most things not already encapsulated within a class.
void load(const GMatrix &)
Load a matrix onto the current transform.
Definition: XForm.cpp:28