File: antsUtilities.cxx

package info (click to toggle)
ants 2.5.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,672 kB
  • sloc: cpp: 85,685; sh: 15,850; perl: 863; xml: 115; python: 111; makefile: 68
file content (237 lines) | stat: -rw-r--r-- 4,757 bytes parent folder | download
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
235
236
237

#include "antsUtilities.h"

#include <deque>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>

namespace ants
{
// We need to ensure that only one of these exists!
// boost::iostreams::stream<ants_Sink> std::cout( ( ants_Sink() ) );
}

TRAN_FILE_TYPE
CheckFileType(const char * const str)
{
  std::string            filename = str;
  std::string::size_type pos = filename.rfind(".");
  std::string            filepre = std::string(filename, 0, pos);

  if (pos != std::string::npos)
  {
    std::string extension = std::string(filename, pos, filename.length() - 1);
    if (extension == std::string(".gz"))
    {
      pos = filepre.rfind(".");
      extension = std::string(filepre, pos, filepre.length() - 1);
    }
    if (extension == ".txt" || extension == ".mat" || extension == ".hdf5" || extension == ".hdf" ||
        extension == ".xfm")
    {
      return AFFINE_FILE;
    }
    else
    {
      return DEFORMATION_FILE;
    }
  }
  else
  {
    return INVALID_FILE;
  }
}

TRAN_FILE_TYPE
CheckFileType(const std::string & str)
{
  return CheckFileType(str.c_str());
}

void
SetAffineInvFlag(TRAN_OPT & opt, bool & set_current_affine_inv)
{
  opt.do_affine_inv = set_current_affine_inv;
  if (set_current_affine_inv)
  {
    set_current_affine_inv = false;
  }
}

void
FilePartsWithgz(const std::string & filename, std::string & path, std::string & name, std::string & ext)
{
  std::string            extension;
  std::string::size_type pos = filename.rfind(".");
  std::string            filepre = std::string(filename, 0, pos);

  if (pos != std::string::npos)
  {
    extension = std::string(filename, pos, filename.length() - 1);
    if (extension == std::string(".gz"))
    {
      pos = filepre.rfind(".");
      if (pos != std::string::npos)
      {
        extension = std::string(filepre, pos, filepre.length() - 1) + ".gz";
        filepre = std::string(filepre, 0, pos);
      }
    }
  }
  else
  {
    extension = std::string("");
  }

  ext = extension;

  pos = filepre.rfind('/');

  if (pos != std::string::npos)
  {
    path = std::string(filepre, 0, pos + 1);
    name = std::string(filepre, pos + 1, filepre.length() - 1);
  }
  else
  {
    path = std::string("");
    name = filepre;
  }
}

bool
CheckFileExistence(const char * const str)
{
  std::ifstream myfile(str);
  bool          b = myfile.is_open();

  myfile.close();
  return b;
}

// adapted from http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c
bool
get_a_double_number(const char * const str, double & v)
{
  errno = 0;
  char * end;
  v = strtod(str, &end);
  if ((errno == ERANGE && v == HUGE_VAL))
  {
    return false; // OVERFLOW
  }
  if ((errno == ERANGE && itk::Math::FloatAlmostEqual(v, 0.0)))
  {
    return false; // UNDERFLOW
  }
  if (*str == '\0' || *end != '\0')
  {
    return false; // INCONVERTIBLE;
  }
  return true;
}

void
DisplayOptQueue(const TRAN_OPT_QUEUE & opt_queue)
{
  const itk::SizeValueType kQueueSize = opt_queue.size();

  for (itk::SizeValueType i = 0; i < kQueueSize; i++)
  {
    std::cout << "[" << i << "/" << kQueueSize << "]: ";

    switch (opt_queue[i].file_type)
    {
      case AFFINE_FILE:
      {
        std::cout << "AFFINE";
      }
      break;
      case DEFORMATION_FILE:
      {
        std::cout << "FIELD";
      }
      break;
      case IDENTITY_TRANSFORM:
      {
        std::cout << "IDENTITY";
      }
      break;
      case IMAGE_AFFINE_HEADER:
      {
        std::cout << "HEADER";
      }
      break;
      case INVALID_FILE:
      default:
      {
        std::cout << "Invalid Format!!!";
      }
      break;
    }
    if (opt_queue[i].do_affine_inv)
    {
      std::cout << "-INV";
    }
    std::cout << ": " << opt_queue[i].filename << std::endl;
  }
}

void
DisplayOpt(const TRAN_OPT & opt)
{
  switch (opt.file_type)
  {
    case AFFINE_FILE:
    {
      std::cout << "AFFINE";
    }
    break;
    case DEFORMATION_FILE:
    {
      std::cout << "FIELD";
    }
    break;
    case IDENTITY_TRANSFORM:
    {
      std::cout << "IDENTITY";
    }
    break;
    case IMAGE_AFFINE_HEADER:
    {
      std::cout << "HEADER";
    }
    break;
    case INVALID_FILE:
    default:
    {
      std::cout << "Invalid Format!!!";
    }
    break;
  }
  if (opt.do_affine_inv)
  {
    std::cout << "-INV";
  }
  std::cout << ": " << opt.filename << std::endl;
}

std::string
GetPreferredTransformFileType()
{
  // return ".mat";
  return ".txt";
}

void
ConvertToLowerCase(std::string & str)
{
  std::transform(str.begin(), str.end(), str.begin(), tolower);
  // You may need to cast the above line to (int(*)(int))
  // tolower - this works as is on VC 7.1 but may not work on
  // other compilers
}