~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to munge/setorigin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// setorigin.cpp
 
3
// set the origin of one image to correspond to that of another
 
4
// Copyright (c) 1998-2004 by The VoxBo Development Team
 
5
 
 
6
// VoxBo is free software: you can redistribute it and/or modify it
 
7
// under the terms of the GNU General Public License as published by
 
8
// the Free Software Foundation, either version 3 of the License, or
 
9
// (at your option) any later version.
 
10
// 
 
11
// VoxBo is distributed in the hope that it will be useful, but
 
12
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
// 
 
16
// You should have received a copy of the GNU General Public License
 
17
// along with VoxBo.  If not, see <http://www.gnu.org/licenses/>.
 
18
// 
 
19
// For general information on VoxBo, including the latest complete
 
20
// source code and binary distributions, manual, and associated files,
 
21
// see the VoxBo home page at: http://www.voxbo.org/
 
22
//
 
23
// original version written by Dan Kimberg
 
24
 
 
25
using namespace std;
 
26
 
 
27
#include "vbutil.h"
 
28
#include "vbio.h"
 
29
#include <math.h>
 
30
#include <sstream>
 
31
#include "setorigin.hlp.h"
 
32
 
 
33
enum {m_strip,m_center};
 
34
 
 
35
int setorigin_map(tokenlist &args);
 
36
int setorigin_copy(tokenlist &args);
 
37
int setorigin_new(tokenlist &args,int mode);
 
38
int setorigin_guess(tokenlist &args);
 
39
int setorigin_set(tokenlist &args);
 
40
void setorigin_help();
 
41
 
 
42
int
 
43
main(int argc,char *argv[])
 
44
{
 
45
  tzset();                     // make sure all times are timezone corrected
 
46
  if (argc < 2) {              // not enough args, display autodocs
 
47
    setorigin_help();
 
48
    exit(0);
 
49
  }
 
50
 
 
51
  tokenlist args;
 
52
  args.Transfer(argc-1,argv+1);
 
53
  int err;
 
54
  if (args[0]=="-c")
 
55
    err=setorigin_copy(args);
 
56
  else if (args[0]=="-m")
 
57
    err=setorigin_map(args);
 
58
  else if (args[0]=="-g")
 
59
    err=setorigin_guess(args);
 
60
  else if (args[0]=="-s")
 
61
    err=setorigin_new(args,m_strip);
 
62
  else if (args[0]=="-x")
 
63
    err=setorigin_new(args,m_center);
 
64
  else
 
65
    err=setorigin_set(args);
 
66
 
 
67
  exit(err);
 
68
}
 
69
 
 
70
int
 
71
setorigin_set(tokenlist &args)
 
72
{
 
73
  stringstream tmps;
 
74
 
 
75
  // FIXME maybe handle x,y,z syntax
 
76
  if (args.size() !=4) {
 
77
    setorigin_help();
 
78
    return(101);
 
79
  }
 
80
 
 
81
  Cube cb;
 
82
  Tes ts;
 
83
  int err=0;
 
84
 
 
85
  float o1=0.0,o2=0.0,o3=0.0;
 
86
  if (args.size()==4) {
 
87
    o1=strtod(args[1]);
 
88
    o2=strtod(args[2]);
 
89
    o3=strtod(args[3]);
 
90
  }
 
91
 
 
92
  if (cb.ReadFile(args[0])==0) {
 
93
    cb.SetOrigin(o1,o2,o3);
 
94
    if (cb.WriteFile()) {
 
95
      printf("[E] setorigin: error writing %s\n",args(0));
 
96
      err=100;
 
97
    }
 
98
    else {
 
99
      printf("[I] setorigin: origin set for file %s\n",args(0));
 
100
    }
 
101
  }
 
102
  else if (ts.ReadFile(args[0])==0) {
 
103
    ts.SetOrigin(o1,o2,o3);
 
104
    if (ts.WriteFile()) {
 
105
      printf("[E] setorigin: error writing %s\n",args(0));
 
106
      err=100;
 
107
    }
 
108
    else {
 
109
      printf("[I] setorigin: origin set for %s\n",args(0));
 
110
    }
 
111
  }
 
112
  else {
 
113
    tmps.str("");
 
114
    tmps << "setorigin: couldn't read " << args[0] << " as 3D or 4D data";
 
115
    printErrorMsg(VB_ERROR,tmps.str());
 
116
  }
 
117
 
 
118
  return(err);
 
119
}
 
120
 
 
121
int
 
122
setorigin_guess(tokenlist &args)
 
123
{
 
124
  if (args.size() != 2) {
 
125
    setorigin_help();
 
126
    return(101);
 
127
  }
 
128
 
 
129
  Cube cb;
 
130
  Tes ts;
 
131
  int err=0;
 
132
  int o1,o2,o3;
 
133
  stringstream tmps;
 
134
 
 
135
  if (cb.ReadFile(args[1])==0) {
 
136
    o1=cb.dimx;
 
137
    o2=cb.dimy;
 
138
    o3=cb.dimz;
 
139
    guessorigin(o1,o2,o3);
 
140
    cb.SetOrigin(o1,o2,o3);
 
141
    if (cb.WriteFile()) {
 
142
      printf("[E] setorigin: error writing %s\n",args(1));
 
143
      err=100;
 
144
    }
 
145
    else {
 
146
      printf("[I] setorigin: origin set for file %s\n",args(1));
 
147
      printErrorMsg(VB_INFO,tmps.str());
 
148
    }
 
149
  }
 
150
  else if (ts.ReadFile(args[1])==0) {
 
151
    o1=ts.dimx;
 
152
    o2=ts.dimy;
 
153
    o3=ts.dimz;
 
154
    guessorigin(o1,o2,o3);
 
155
    ts.SetOrigin(o1,o2,o3);
 
156
    if (ts.WriteFile()) {
 
157
      printf("[E] setorigin: error writing %s\n",args(1));
 
158
      err=100;
 
159
    }
 
160
    else {
 
161
      printf("[I] setorigin: origin set for %s\n",args(1));
 
162
    }
 
163
  }
 
164
 
 
165
  return(err);
 
166
}
 
167
 
 
168
int
 
169
setorigin_map(tokenlist &args)
 
170
{
 
171
  Cube mycube,refcube;
 
172
  stringstream tmps;
 
173
 
 
174
  if (args.size() < 3) {
 
175
    setorigin_help();
 
176
    return(101);
 
177
  }
 
178
 
 
179
  if (refcube.ReadFile(args[1])) {
 
180
    tmps.str("");
 
181
    tmps << "setorigin: invalid 3D file " << args[1];
 
182
    printErrorMsg(VB_ERROR,tmps.str());
 
183
    exit(5);
 
184
  }
 
185
  if (mycube.ReadFile(args[2])) {
 
186
    tmps.str("");
 
187
    tmps << "setorigin: invalid 3D file " << args[2];
 
188
    printErrorMsg(VB_ERROR,tmps.str());
 
189
    exit(100);
 
190
  }
 
191
  double ourxpos,ourypos,ourzpos;
 
192
  double refxpos,refypos,refzpos;
 
193
  mycube.GetCorner(ourxpos,ourypos,ourzpos);
 
194
  refcube.GetCorner(refxpos,refypos,refzpos);
 
195
 
 
196
  // first do the z, which is complicated
 
197
  int refx=refcube.origin[0];  // for convenience
 
198
  int refy=refcube.origin[1];
 
199
  int refz=refcube.origin[2];
 
200
  if (refx==0 && refy==0 && refz==0) {
 
201
    refx=refcube.dimx;
 
202
    refy=refcube.dimy;
 
203
    refz=refcube.dimz;
 
204
    guessorigin(refx,refy,refz);
 
205
  }
 
206
  // first calculate the absolute position of the origin in mm
 
207
  double xposition=refxpos+(refx*refcube.voxsize[0]);
 
208
  double yposition=refypos+(refy*refcube.voxsize[1]);
 
209
  double zposition=refzpos+(refz*refcube.voxsize[2]);
 
210
  tmps.str("");
 
211
  tmps << "setorigin: x position " << xposition << "  refx: " << refx;
 
212
  printErrorMsg(VB_INFO,tmps.str());
 
213
  tmps.str("");
 
214
  tmps << "setorigin: y position " << yposition << "  refy: " << refy;
 
215
  printErrorMsg(VB_INFO,tmps.str());
 
216
  tmps.str("");
 
217
  tmps << "setorigin: z position " << zposition << "  refz: " << refz;
 
218
  printErrorMsg(VB_INFO,tmps.str());
 
219
 
 
220
  // now figure out what voxel that is on the reference brain
 
221
  xposition=(xposition-ourxpos)/mycube.voxsize[0];
 
222
  yposition=(yposition-ourypos)/mycube.voxsize[1];
 
223
  zposition=(zposition-ourzpos)/mycube.voxsize[2];
 
224
 
 
225
  // now do the x and y, which is just a matter of converting voxel
 
226
  // sizes, since we assume the images are aligned
 
227
  mycube.origin[0]=lround(xposition);
 
228
  mycube.origin[1]=lround(yposition);
 
229
  mycube.origin[2]=lround(zposition);
 
230
 
 
231
  int err=mycube.WriteFile();
 
232
  if (err) {
 
233
    tmps.str("");
 
234
    tmps << "setorigin: error writing the origin-corrected cube " << mycube.GetFileName();
 
235
    printErrorMsg(VB_ERROR,tmps.str());
 
236
    return (102);
 
237
  }
 
238
  else {
 
239
    tmps.str("");
 
240
    tmps << "setorigin: wrote origin-corrected cube " << mycube.GetFileName();
 
241
    printErrorMsg(VB_INFO,tmps.str());
 
242
    return(0);
 
243
  }
 
244
  return (0);
 
245
}
 
246
 
 
247
int
 
248
setorigin_copy(tokenlist &args)
 
249
{
 
250
  if (args.size() < 3) {
 
251
    setorigin_help();
 
252
    return(101);
 
253
  }
 
254
  
 
255
  int origin[3],err=0;
 
256
  stringstream tmps;
 
257
  Tes ts;
 
258
  Cube cb;
 
259
  
 
260
  if (ts.ReadFile(args[1])==0) {
 
261
    origin[0]=ts.origin[0];
 
262
    origin[1]=ts.origin[1];
 
263
    origin[2]=ts.origin[2];
 
264
  }
 
265
  else if (cb.ReadFile(args[1])==0) {
 
266
    origin[0]=cb.origin[0];
 
267
    origin[1]=cb.origin[1];
 
268
    origin[2]=cb.origin[2];
 
269
  }
 
270
  else {
 
271
    printf("[E] setorigin: data must be in a recognized 3D or 4D format\n");
 
272
    exit(10);
 
273
  }
 
274
  
 
275
  for (int i=2; i<args.size(); i++) {
 
276
    // vector<VBFF>tfiletypes=EligibleFileTypes(args[i]);
 
277
    if (ts.ReadFile(args[i])==0) {
 
278
      ts.origin[0]=origin[0];
 
279
      ts.origin[1]=origin[1];
 
280
      ts.origin[2]=origin[2];
 
281
      if (ts.WriteFile()) {
 
282
        tmps.str("");
 
283
        tmps << "setorigin: error writing to 4D image " << args[i];
 
284
        printErrorMsg(VB_ERROR,tmps.str());
 
285
      }
 
286
      else {
 
287
        tmps.str("");
 
288
        tmps << "setorigin: copied origin to 4D image " << args[i];
 
289
        printErrorMsg(VB_INFO,tmps.str());
 
290
      }
 
291
    }
 
292
    else if (cb.ReadFile(args[i])==0) {
 
293
      cb.origin[0]=origin[0];
 
294
      cb.origin[1]=origin[1];
 
295
      cb.origin[2]=origin[2];
 
296
      if (cb.WriteFile()) {
 
297
        tmps.str("");
 
298
        tmps << "setorigin: error writing to 3D image " << args[i];
 
299
        printErrorMsg(VB_ERROR,tmps.str());
 
300
      }
 
301
      else {
 
302
        tmps.str("");
 
303
        tmps << "setorigin: copied origin to 3D image " << args[i];
 
304
        printErrorMsg(VB_INFO,tmps.str());
 
305
      }
 
306
    }
 
307
    else {
 
308
      tmps.str("");
 
309
      tmps << "setorigin: file " << args[i] << " is not a recognized 3D or 4D file type";
 
310
      printErrorMsg(VB_ERROR,tmps.str());
 
311
      err=190;
 
312
    }
 
313
  }
 
314
  return(err);
 
315
}
 
316
 
 
317
int
 
318
setorigin_new(tokenlist &args,int mode)
 
319
{
 
320
  if (args.size() < 2) {
 
321
    setorigin_help();
 
322
    return(101);
 
323
  }
 
324
 
 
325
  Cube cb;
 
326
  Tes ts;
 
327
  int err=0;
 
328
  stringstream tmps;
 
329
 
 
330
  for (int i=1; i<args.size(); i++) {
 
331
    if (cb.ReadFile(args[i])==0) {
 
332
      if (mode==m_strip)
 
333
        cb.SetOrigin(0,0,0);
 
334
      else if (mode==m_center)
 
335
        cb.SetOrigin(cb.dimx/2,cb.dimy/2,cb.dimz/2);
 
336
      if (cb.WriteFile()) {
 
337
        tmps.str("");
 
338
        tmps << "setorigin: error writing " << args[i];
 
339
        printErrorMsg(VB_ERROR,tmps.str());
 
340
        err=100;
 
341
      }
 
342
      else {
 
343
        tmps.str("");
 
344
        if (mode==m_strip) tmps << "setorigin: stripped origin from " << args[i];
 
345
        if (mode==m_center) tmps << "setorigin: set origin to center for " << args[i];
 
346
        printErrorMsg(VB_INFO,tmps.str());
 
347
      }
 
348
    }
 
349
    else if (ts.ReadFile(args[i])==0) {
 
350
      if (mode==m_strip)
 
351
        ts.SetOrigin(0,0,0);
 
352
      else if (mode==m_center)
 
353
        ts.SetOrigin(ts.dimx/2,ts.dimy/2,ts.dimz/2);
 
354
      if (ts.WriteFile()) {
 
355
        tmps.str("");
 
356
        tmps << "setorigin: error writing " << args[i];
 
357
        printErrorMsg(VB_ERROR,tmps.str());
 
358
        err=100;
 
359
      }
 
360
      else {
 
361
        tmps.str("");
 
362
        if (mode==m_strip) tmps << "setorigin: stripped origin from " << args[i];
 
363
        if (mode==m_center) tmps << "setorigin: set origin to center for " << args[i];
 
364
        printErrorMsg(VB_ERROR,tmps.str());
 
365
      }
 
366
    }
 
367
  }
 
368
 
 
369
  return(err);
 
370
}
 
371
 
 
372
void
 
373
setorigin_help()
 
374
{
 
375
  cout << boost::format(myhelp) % vbversion;
 
376
}