~ubuntu-branches/ubuntu/saucy/caret/saucy-proposed

1 by Michael Hanke
Import upstream version 5.6.1~dfsg.1
1
/*LICENSE_START*/
2
/*
3
 *  Copyright 1995-2002 Washington University School of Medicine
4
 *
5
 *  http://brainmap.wustl.edu
6
 *
7
 *  This file is part of CARET.
8
 *
9
 *  CARET 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; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 * 
14
 *  CARET is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with CARET; if not, write to the Free Software
21
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 *
23
 */
24
/*LICENSE_END*/
25
26
#include <QTextStream>
27
28
#include "GiftiCommon.h"
29
#include "GiftiMatrix.h"
30
31
/**
32
 * constructor.
33
 */
34
GiftiMatrix::GiftiMatrix()
35
{
36
   clear();
37
}
38
39
/**
40
 * copy constructor.
41
 */
42
GiftiMatrix::GiftiMatrix(const GiftiMatrix& gm)
43
{
44
   copyHelperGiftiMatrix(gm);
45
}
46
47
/**
48
 * destructor.
49
 */
50
GiftiMatrix::~GiftiMatrix()
51
{
52
    clear();
53
}
54
55
/**
56
 * assignment operator.
57
 */
58
GiftiMatrix& 
59
GiftiMatrix::operator=(const GiftiMatrix& gm)
60
{
61
   if (this != &gm) {
62
      copyHelperGiftiMatrix(gm);
63
   }
64
   return *this;
65
}
66
67
/**
68
 * copy helper used by copy constructor and assignement operators.
69
 */
70
void 
71
GiftiMatrix::copyHelperGiftiMatrix(const GiftiMatrix& gm)
72
{
73
   for (int i = 0; i < 4; i++) {
74
      for (int j = 0; j < 4; j++) {
75
         m[i][j] = gm.m[i][j];
76
      }
77
   }
78
   
79
   dataSpaceName     = gm.dataSpaceName;
80
   transformedSpaceName = gm.transformedSpaceName;
81
}
82
83
/**
84
 * clear the matrix (removes spaces, sets matrix to identity).
85
 */
86
void 
87
GiftiMatrix::clear()
88
{
89
   m[0][0] = 1.0;
90
   m[0][1] = 0.0;
91
   m[0][2] = 0.0;
92
   m[0][3] = 0.0;
93
   m[1][0] = 0.0;
94
   m[1][1] = 1.0;
95
   m[1][2] = 0.0;
96
   m[1][3] = 0.0;
97
   m[2][0] = 0.0;
98
   m[2][1] = 0.0;
99
   m[2][2] = 1.0;
100
   m[2][3] = 0.0;
101
   m[3][0] = 0.0;
102
   m[3][1] = 0.0;
103
   m[3][2] = 0.0;
104
   m[3][3] = 1.0;
105
   
106
   dataSpaceName     = "";
107
   transformedSpaceName = "";
108
}
109
110
/**
111
 * is the matrix empty (identity matrix with no spaces specified).
112
 */
113
bool 
114
GiftiMatrix::isEmpty() const
115
{
116
   return (dataSpaceName.isEmpty() &&
117
           transformedSpaceName.isEmpty() &&
118
           isIdentity());
119
}
120
      
121
/**
122
 * is the matrix the identity matrix ?
123
 */
124
bool 
125
GiftiMatrix::isIdentity() const
126
{
127
   if ((m[0][0] == 1.0) &&
128
       (m[0][1] == 0.0) &&
129
       (m[0][2] == 0.0) &&
130
       (m[0][3] == 0.0) &&
131
       (m[1][0] == 0.0) &&
132
       (m[1][1] == 1.0) &&
133
       (m[1][2] == 0.0) &&
134
       (m[1][3] == 0.0) &&
135
       (m[2][0] == 0.0) &&
136
       (m[2][1] == 0.0) &&
137
       (m[2][2] == 1.0) &&
138
       (m[2][3] == 0.0) &&
139
       (m[3][0] == 0.0) &&
140
       (m[3][1] == 0.0) &&
141
       (m[3][2] == 0.0) &&
142
       (m[3][3] == 1.0)) {
143
      return true;
144
   }
145
   return false;
146
}
147
148
/**
149
 * get the data (pre-transformed) space name.
150
 */
151
QString 
152
GiftiMatrix::getDataSpaceName() const 
153
{ 
154
   return dataSpaceName; 
155
}
156
157
/**
158
 * set the data (pre-transformed) space name.
159
 */
160
void 
161
GiftiMatrix::setDataSpaceName(const QString& spaceName)
162
{
163
   dataSpaceName = spaceName;
164
}
165
166
/**
167
 * get the transformed space name.
168
 */
169
QString 
170
GiftiMatrix::getTransformedSpaceName() const 
171
{ 
172
   return transformedSpaceName; 
173
}
174
175
/**
176
 * set the transformed space name.
177
 */
178
void 
179
GiftiMatrix::setTransformedSpaceName(const QString& spaceName)
180
{
181
   transformedSpaceName = spaceName;
182
}
183
184
/**
185
 * get the matrix.
186
 */
187
void 
188
GiftiMatrix::getMatrix(double matrix[4][4]) const
189
{
190
   for (int i = 0; i < 4; i++) {
191
      for (int j = 0; j < 4; j++) {
192
         matrix[i][j] = m[i][j];
193
      }
194
   }
195
}
196
197
/**
198
 * set the matrix.
199
 */
200
void 
201
GiftiMatrix::setMatrix(const double matrix[4][4])
202
{
203
   for (int i = 0; i < 4; i++) {
204
      for (int j = 0; j < 4; j++) {
205
         m[i][j] = matrix[i][j];
206
      }
207
   }
208
}
209
210
/**
211
 * multiply a point.
212
 */
213
void 
214
GiftiMatrix::multiplyPoint(double& x, double& y, double& z) const
215
{
216
   const double xp = m[0][0]*x + m[0][1]*y + m[0][2]*z + m[0][3];
217
   const double yp = m[1][0]*x + m[1][1]*y + m[1][2]*z + m[1][3];
218
   const double zp = m[2][0]*x + m[2][1]*y + m[2][2]*z + m[2][3];
219
   x = xp;
220
   y = yp;
221
   z = zp;
222
}
223
224
/**
225
 * multiply a point.
226
 */
227
void 
228
GiftiMatrix::multiplyPoint(double xyz[3]) const
229
{
230
   multiplyPoint(xyz[0], xyz[1], xyz[2]);
231
}
232
      
233
/**
234
 * write metadata (used by other classes so static).
235
 */
236
void 
237
GiftiMatrix::writeAsXML(QTextStream& stream,
238
                        const int indentOffset) const
239
{
240
   if (isEmpty()) {
241
      return;
242
   }
243
   
244
   int indent = indentOffset;
245
   
246
   GiftiCommon::writeIndentationXML(stream, indent);
247
   stream << "<" << GiftiCommon::tagMatrix << ">" << "\n";
248
   indent++;
249
   
250
   GiftiCommon::writeIndentationXML(stream, indent);
251
   stream << "<" << GiftiCommon::tagMatrixDataSpace << "><![CDATA["
252
                 << dataSpaceName 
253
          << "]]></" << GiftiCommon::tagMatrixDataSpace << ">" << "\n";
254
          
255
   GiftiCommon::writeIndentationXML(stream, indent);
256
   stream << "<" << GiftiCommon::tagMatrixTransformedSpace << "><![CDATA["
257
                 << transformedSpaceName 
258
          << "]]></" << GiftiCommon::tagMatrixTransformedSpace << ">" << "\n";
259
          
260
   GiftiCommon::writeIndentationXML(stream, indent);
261
   stream << "<" << GiftiCommon::tagMatrixData << ">" << "\n";
262
          
263
   indent++;
264
   for (int i = 0; i < 4; i++) {
265
      GiftiCommon::writeIndentationXML(stream, indent);
266
      for (int j = 0; j < 4; j++) {
267
         stream << m[i][j] << " ";
268
      }
269
      stream << "\n";
270
   }
271
   indent--;
272
   
273
   GiftiCommon::writeIndentationXML(stream, indent);
274
   stream << "</" << GiftiCommon::tagMatrixData << ">" << "\n";
275
276
   indent--;
277
   GiftiCommon::writeIndentationXML(stream, indent);
278
   stream << "</" << GiftiCommon::tagMatrix << ">" << "\n";
279
}
280