OpenWalnut  1.5.0dev
WHcoord.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 //---------------------------------------------------------------------------
26 //
27 // Project: hClustering
28 //
29 // Whole-Brain Connectivity-Based Hierarchical Parcellation Project
30 // David Moreno-Dominguez
31 // d.mor.dom@gmail.com
32 // moreno@cbs.mpg.de
33 // www.cbs.mpg.de/~moreno//
34 // This file is also part of OpenWalnut ( http://www.openwalnut.org ).
35 //
36 // hClustering is free software: you can redistribute it and/or modify
37 // it under the terms of the GNU Lesser General Public License as published by
38 // the Free Software Foundation, either version 3 of the License, or
39 // (at your option) any later version.
40 // http://creativecommons.org/licenses/by-nc/3.0
41 //
42 // hClustering is distributed in the hope that it will be useful,
43 // but WITHOUT ANY WARRANTY; without even the implied warranty of
44 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 // GNU Lesser General Public License for more details.
46 //
47 //---------------------------------------------------------------------------
48 
49 
50 // std library
51 #include <iostream>
52 #include <string>
53 #include <sstream>
54 #include <vector>
55 #include <cmath>
56 #include <algorithm>
57 
58 #include "WHcoord.h"
59 
60 WHcoord::WHcoord(): m_x( 0 ), m_y( 0 ), m_z( 0 )
61 {
62 }
63 
64 WHcoord::WHcoord( coord_t x_init, coord_t y_init, coord_t z_init ): m_x( x_init ), m_y( y_init ), m_z( z_init )
65 {
66 }
67 
68 WHcoord::WHcoord( const WHcoord &object ): m_x( object.m_x ), m_y( object.m_y ), m_z( object.m_z )
69 {
70 }
71 
73 {
74  //Cleanup
75 }
76 
77 
78 // === PUBLIC MEMBER FUNCTIONS ===
79 
80 
81 // "phys_dist()": returns the euclidean distance between this voxel and the input voxel
82 float WHcoord::getPhysDist( const WHcoord voxel ) const
83 {
84  float xdif( m_x-voxel.m_x ), ydif( m_y-voxel.m_y ), zdif( m_z-voxel.m_z );
85  return sqrt( ( xdif*xdif )+( ydif*ydif )+( zdif*zdif ) );
86 } // end "phys_dist()" -----------------------------------------------------------------
87 
88 
89 // "get_phys_neighbours()": returns a vector containing the coordinates of the physical neighbours adjacent to the voxel,
90 // the neighbourhood level is defined by nb_level
91 std::vector<WHcoord> WHcoord::getPhysNbs( const WHcoord dataSize, const unsigned int nbLevel ) const
92 {
93  std::vector<WHcoord> physNeighbours;
94 
95  int condition( 0 );
96  int range( 0 );
97 
98  switch( nbLevel )
99  {
100  case 6:
101  range = 1;
102  condition = 1;
103  break;
104  case 18:
105  range = 1;
106  condition = 2;
107  break;
108  case 26:
109  case 32:
110  range = 1;
111  condition = 3;
112  break;
113  case 56:
114  range = 2;
115  condition = 3;
116  break;
117  case 92:
118  range = 2;
119  condition = 4;
120  break;
121  case 116:
122  range = 2;
123  condition = 5;
124  break;
125  case 124:
126  range = 2;
127  condition = 6;
128  break;
129  default:
130  std::cerr << "ERROR @ coordinate::getPhysNbs(): Unrecognized nbhood level: " << nbLevel << std::endl;
131  return physNeighbours;
132  break;
133  }
134 
135  for( coord_t i = -range; i <= range ; ++i )
136  {
137  for( coord_t j = -range; j <= range ; ++j )
138  {
139  for( coord_t k = -range; k <= range ; ++k )
140  {
141  const int test( std::abs( i ) + std::abs( j ) + std::abs( k ) );
142  if( test == 0 )
143  {
144  continue; // dont use the voxel itself as neighbour
145  }
146  else if( test > condition )
147  {
148  continue;
149  }
150  else
151  {
152  WHcoord temp;
153  temp.m_z = this->m_z + i;
154  temp.m_y = this->m_y + j;
155  temp.m_x = this->m_x + k;
156  if( ( temp.m_z < 0 ) || ( temp.m_y < 0 ) || ( temp.m_x < 0 ) || ( temp.m_z > dataSize.m_z ) ||
157  ( temp.m_y > dataSize.m_y ) ||( temp.m_x > dataSize.m_x ) )
158  {
159  continue; // voxel is out of mask boundaries
160  }
161  physNeighbours.push_back( temp );
162  }
163  }
164  }
165  }
166 
167  // extra nbs for nb level 32
168  if( nbLevel == 32 )
169  {
170  for( coord_t i =- 2; i <= 2; i += 2 )
171  {
172  for( coord_t j =- 2; j <= 2; j += 2 )
173  {
174  for( coord_t k =- 2; k <= 2; k += 2 )
175  {
176  const int test( std::abs( i ) + std::abs( j ) + std::abs( k ) );
177  if( test != 2 )
178  {
179  continue;
180  }
181  else
182  {
183  WHcoord temp;
184  temp.m_z = this->m_z + i;
185  temp.m_y = this->m_y + j;
186  temp.m_x = this->m_x + k;
187  if( ( temp.m_z < 0 ) || ( temp.m_y < 0 ) || ( temp.m_x < 0 ) || ( temp.m_z > dataSize.m_z ) ||
188  ( temp.m_y > dataSize.m_y ) || ( temp.m_x > dataSize.m_x ) )
189  {
190  continue; // voxel is out of mask boundaries
191  }
192  physNeighbours.push_back( temp );
193  }
194  }
195  }
196  }
197  }
198 
199  std::sort( physNeighbours.begin(), physNeighbours.end() );
200  return physNeighbours;
201 } // end "phys_dist()" -----------------------------------------------------------------
202 
203 
204 // "getNameString()": returns a string with the coordinates of the voxel in the form "xxx_yyy_zzz"
205 std::string WHcoord::getNameString() const
206 {
207  std::stringstream stream;
208  std::string name;
209  stream << m_x << "_" << m_y << "_" << m_z;
210  stream >> name;
211  return name;
212 } // end "getNameString()" -----------------------------------------------------------------
213 
214 
215 WHcoord WHcoord::nifti2vista( const WHcoord dataSize ) const
216 {
217  WHcoord returnVista;
218  returnVista.m_x = this->m_x;
219  returnVista.m_y = ( dataSize.m_y-1 )-this->m_y;
220  returnVista.m_z = ( dataSize.m_z-1 )-this->m_z;
221  return returnVista;
222 } // end "nifti2Vista()" -----------------------------------------------------------------
223 
224 WHcoord WHcoord::surf2vista( const WHcoord dataSize ) const
225 {
226  WHcoord returnVista;
227  returnVista.m_x = this->m_x+( ( dataSize.m_x-1.0 )/2.0 );
228  returnVista.m_y = ( ( dataSize.m_y-1.0 )/2.0 )-this->m_y;
229  returnVista.m_z = ( ( dataSize.m_z-1.0 )/2.0 )-this->m_z;
230  return returnVista;
231 } // end "nifti2Vista()" -----------------------------------------------------------------
232 
233 WHcoord WHcoord::vista2nifti( const WHcoord dataSize ) const
234 {
235  WHcoord returnNifti;
236  returnNifti.m_x = this->m_x;
237  returnNifti.m_y = ( dataSize.m_y-1 )-this->m_y;
238  returnNifti.m_z = ( dataSize.m_z-1 )-this->m_z;
239  return returnNifti;
240 } // end "vista2Nifti()" -----------------------------------------------------------------
241 
242 
243 
244 // === MEMBER OPERATORS ===
245 
246 
247 
249 {
250  m_x = rhs.m_x;
251  m_y = rhs.m_y;
252  m_z = rhs.m_z;
253  return *this;
254 } // end "operator =" -----------------------------------------------------------------
255 
256 
257 
258 // === NON-MEMBER OPERATORS ===
259 
260 
261 
262 std::ostream& operator <<( std::ostream& os, const WHcoord& object )
263 {
264  os << boost::format( "%03d %03d %03d" ) % object.m_x % object.m_y % object.m_z;
265  return os;
266 } // end "operator << " -----------------------------------------------------------------
267 
268 
269 bool operator <( const WHcoord &lhs, const WHcoord &rhs )
270 {
271  if( lhs.m_z < rhs.m_z ) // z1<z2
272  {
273  return true;
274  }
275  else if( lhs.m_z > rhs.m_z ) // z1>z2
276  {
277  return false;
278  }
279  else if( lhs.m_y < rhs.m_y ) // z1=z2, y1<y2
280  {
281  return true;
282  }
283  else if( lhs.m_y > rhs.m_y ) // z1=z2, y1>y2
284  {
285  return false;
286  }
287  else if( lhs.m_x < rhs.m_x ) // z1=z2, y1=y2, x1<x2
288  {
289  return true;
290  }
291  else if( lhs.m_x > rhs.m_x ) // z1=z2, y1=y2, x1>x2
292  {
293  return false;
294  }
295  else // coord1 = coord2
296  {
297  return false;
298  }
299 } // end "operator <" -----------------------------------------------------------------
300 
301 bool operator ==( const WHcoord &lhs, const WHcoord &rhs )
302 {
303  if( lhs.m_z == rhs.m_z )
304  {
305  if( lhs.m_y == rhs.m_y )
306  {
307  if( lhs.m_x == rhs.m_x )
308  {
309  return true;
310  }
311  }
312  }
313 
314  return false;
315 } // end "operator ==" -----------------------------------------------------------------
316 
317 bool operator !=( const WHcoord &lhs, const WHcoord &rhs )
318 {
319  return !( lhs == rhs );
320 } // end "operator !=" -----------------------------------------------------------------
321 
322 std::string getGridString( const HC_GRID gridType )
323 {
324  if( gridType == HC_VISTA )
325  {
326  return "vista";
327  }
328  else if( gridType == HC_NIFTI )
329  {
330  return "nifti";
331  }
332  else if( gridType == HC_SURF )
333  {
334  return "surf";
335  }
336  else
337  {
338  std::string emptyString;
339  return emptyString;
340  }
341 }
this class to contain a seed voxel coordinate.
Definition: WHcoord.h:76
std::vector< WHcoord > getPhysNbs(const WHcoord dataSize, const unsigned int nbLevel) const
returns a vector containing the coordinates of the physical neighbours adjacent to the voxel,...
Definition: WHcoord.cpp:91
~WHcoord()
Destructor.
Definition: WHcoord.cpp:72
WHcoord surf2vista(const WHcoord dataSize) const
transform coordinates to vista format
Definition: WHcoord.cpp:224
WHcoord nifti2vista(const WHcoord dataSize) const
transform coordinates to vista format
Definition: WHcoord.cpp:215
float getPhysDist(const WHcoord voxel) const
returns the euclidean distance between this voxel and the input voxel
Definition: WHcoord.cpp:82
coord_t m_z
z coordinate
Definition: WHcoord.h:156
std::string getNameString() const
returns a string with the coordinates of the voxel in the form "xxx_yyy_zzz"
Definition: WHcoord.cpp:205
WHcoord()
Constructor.
Definition: WHcoord.cpp:60
WHcoord & operator=(const WHcoord &rhs)
= member operator
Definition: WHcoord.cpp:248
coord_t m_y
y coordinate
Definition: WHcoord.h:155
WHcoord vista2nifti(const WHcoord dataSize) const
transform coordinates to vista format
Definition: WHcoord.cpp:233
coord_t m_x
x coordinate
Definition: WHcoord.h:154