File: IRISVectorTypes.txx

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (234 lines) | stat: -rw-r--r-- 6,466 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*=========================================================================

  Program:   ITK-SNAP
  Module:    $RCSfile: IRISVectorTypes.txx,v $
  Language:  C++
  Date:      $Date: 2009/01/23 20:09:38 $
  Version:   $Revision: 1.3 $
  Copyright (c) 2007 Paul A. Yushkevich
  
  This file is part of ITK-SNAP 

  ITK-SNAP is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

  -----

  Copyright (c) 2003 Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information. 

=========================================================================*/

// Convert vector to integer vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<int,VSize> 
to_int(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<int,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<int>(x(i));

  return z;
}

// Convert vector to unsigned integer vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<unsigned int,VSize> 
to_unsigned_int(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<unsigned int,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<unsigned int>(x(i));

  return z;
}

// Convert vector to long vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<long,VSize> 
to_long(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<long,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<long>(x(i));

  return z;
}

// Convert vector to unsigned long vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<unsigned long,VSize> 
to_unsigned_long(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<unsigned long,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<unsigned long>(x(i));

  return z;
}

// Convert vector to float vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<float,VSize> 
to_float(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<float,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<float>(x(i));

  return z;
}
// Convert vector to double vector
template <class T, unsigned int VSize>
inline iris_vector_fixed<double,VSize> 
to_double(const vnl_vector_fixed<T,VSize> &x)
{
  iris_vector_fixed<double,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = static_cast<double>(x(i));

  return z;
}

/**
 * Given two vectors, get a vector that contains the smaller elements of the
 * two.  Used for bounding box computations
 */
template <class T, unsigned int VSize> 
inline iris_vector_fixed<T,VSize> 
vector_min(const vnl_vector_fixed<T,VSize> &x,
           const vnl_vector_fixed<T,VSize> &y)
{
  iris_vector_fixed<T,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = x(i) < y(i) ? x(i) : y(i);

  return z;
}

/**
 * Given two vectors, get a vector that contains the larger elements of the
 * two.  Used for bounding box computations
 */
template <class T, unsigned int VSize> 
inline iris_vector_fixed<T,VSize> 
vector_max(const vnl_vector_fixed<T,VSize> &x,
           const vnl_vector_fixed<T,VSize> &y)
{
  iris_vector_fixed<T,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = x(i) > y(i) ? x(i) : y(i);

  return z;
}

/**
 * Multiply the corresponding elements of two vectors and return
 * a vector containing the results 
 */
template <class T, unsigned int VSize> 
inline iris_vector_fixed<T,VSize> 
vector_multiply(const vnl_vector_fixed<T,VSize> &x,
                const vnl_vector_fixed<T,VSize> &y)
{
  iris_vector_fixed<T,VSize> z;

  for(unsigned int i=0;i<VSize;i++)
    z(i) = x(i) * y(i);

  return z;
}

/**
 * Multiply two inhomogeneous vectors, returning the result in the
 * type of the first vector
 */
template <class T1, class T2, unsigned int VSize> 
inline iris_vector_fixed<T1,VSize> 
vector_multiply_mixed(const vnl_vector_fixed<T1,VSize> &x,
                      const vnl_vector_fixed<T2,VSize> &y)
{
  iris_vector_fixed<T1,VSize> z;
  for(unsigned int i=0;i<VSize;i++)
    z(i) = x(i) * y(i);
  return z;
}

/**
 * Apply an affine transform (4x4 matrix) to a point (1x3 vector)
 * The vector may be of any type, the output is same as matrix
 */
template <class TReal, class TAny, unsigned int VSize> 
inline iris_vector_fixed<TReal, VSize> 
affine_transform_point(const vnl_matrix_fixed<TReal,VSize+1,VSize+1> &A,
                      const vnl_vector_fixed<TAny,VSize> &x)
{
  iris_vector_fixed<TReal,VSize> y;
  for(unsigned int i=0;i<VSize;i++)
    {
    y[i] = A(i,VSize);
    for(unsigned int j=0;j<VSize;j++)
      y[i] += A(i,j) * x[j];
    }
  return y;
}

/**
 * Apply an affine transform (4x4 matrix) to a vector (1x3 vector)
 * The vector may be of any type, the output is same as matrix
 * Offset is not applied!
 */
template <class TReal, class TAny, unsigned int VSize> 
inline iris_vector_fixed<TReal, VSize> 
affine_transform_vector(const vnl_matrix_fixed<TReal,VSize+1,VSize+1> &A,
                      const vnl_vector_fixed<TAny,VSize> &x)
{
  iris_vector_fixed<TReal,VSize> y;
  for(unsigned int i=0;i<VSize;i++)
    {
    y[i] = 0;
    for(unsigned int j=0;j<VSize;j++)
      y[i] += A(i,j) * x[j];
    }
  return y;
}



/**
 * Compute x*y+z for three inhomogeneous vectors, returning result in the 
 * same type as x
 */
template <class T1, class T2, class T3, unsigned int VSize> 
inline iris_vector_fixed<T1,VSize> 
vector_multiply_add_mixed(const vnl_vector_fixed<T1,VSize> &x,
                          const vnl_vector_fixed<T2,VSize> &y,
                          const vnl_vector_fixed<T3,VSize> &z)
{
  iris_vector_fixed<T1,VSize> r;
  for(unsigned int i=0;i<VSize;i++)
    r(i) = x(i) * y(i) + z(i);
  return r; 
}