3
// set the origin of one image to correspond to that of another
4
// Copyright (c) 1998-2004 by The VoxBo Development Team
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.
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.
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/>.
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/
23
// original version written by Dan Kimberg
31
#include "setorigin.hlp.h"
33
enum {m_strip,m_center};
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();
43
main(int argc,char *argv[])
45
tzset(); // make sure all times are timezone corrected
46
if (argc < 2) { // not enough args, display autodocs
52
args.Transfer(argc-1,argv+1);
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);
65
err=setorigin_set(args);
71
setorigin_set(tokenlist &args)
75
// FIXME maybe handle x,y,z syntax
76
if (args.size() !=4) {
85
float o1=0.0,o2=0.0,o3=0.0;
92
if (cb.ReadFile(args[0])==0) {
93
cb.SetOrigin(o1,o2,o3);
95
printf("[E] setorigin: error writing %s\n",args(0));
99
printf("[I] setorigin: origin set for file %s\n",args(0));
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));
109
printf("[I] setorigin: origin set for %s\n",args(0));
114
tmps << "setorigin: couldn't read " << args[0] << " as 3D or 4D data";
115
printErrorMsg(VB_ERROR,tmps.str());
122
setorigin_guess(tokenlist &args)
124
if (args.size() != 2) {
135
if (cb.ReadFile(args[1])==0) {
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));
146
printf("[I] setorigin: origin set for file %s\n",args(1));
147
printErrorMsg(VB_INFO,tmps.str());
150
else if (ts.ReadFile(args[1])==0) {
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));
161
printf("[I] setorigin: origin set for %s\n",args(1));
169
setorigin_map(tokenlist &args)
174
if (args.size() < 3) {
179
if (refcube.ReadFile(args[1])) {
181
tmps << "setorigin: invalid 3D file " << args[1];
182
printErrorMsg(VB_ERROR,tmps.str());
185
if (mycube.ReadFile(args[2])) {
187
tmps << "setorigin: invalid 3D file " << args[2];
188
printErrorMsg(VB_ERROR,tmps.str());
191
double ourxpos,ourypos,ourzpos;
192
double refxpos,refypos,refzpos;
193
mycube.GetCorner(ourxpos,ourypos,ourzpos);
194
refcube.GetCorner(refxpos,refypos,refzpos);
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) {
204
guessorigin(refx,refy,refz);
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]);
211
tmps << "setorigin: x position " << xposition << " refx: " << refx;
212
printErrorMsg(VB_INFO,tmps.str());
214
tmps << "setorigin: y position " << yposition << " refy: " << refy;
215
printErrorMsg(VB_INFO,tmps.str());
217
tmps << "setorigin: z position " << zposition << " refz: " << refz;
218
printErrorMsg(VB_INFO,tmps.str());
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];
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);
231
int err=mycube.WriteFile();
234
tmps << "setorigin: error writing the origin-corrected cube " << mycube.GetFileName();
235
printErrorMsg(VB_ERROR,tmps.str());
240
tmps << "setorigin: wrote origin-corrected cube " << mycube.GetFileName();
241
printErrorMsg(VB_INFO,tmps.str());
248
setorigin_copy(tokenlist &args)
250
if (args.size() < 3) {
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];
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];
271
printf("[E] setorigin: data must be in a recognized 3D or 4D format\n");
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()) {
283
tmps << "setorigin: error writing to 4D image " << args[i];
284
printErrorMsg(VB_ERROR,tmps.str());
288
tmps << "setorigin: copied origin to 4D image " << args[i];
289
printErrorMsg(VB_INFO,tmps.str());
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()) {
298
tmps << "setorigin: error writing to 3D image " << args[i];
299
printErrorMsg(VB_ERROR,tmps.str());
303
tmps << "setorigin: copied origin to 3D image " << args[i];
304
printErrorMsg(VB_INFO,tmps.str());
309
tmps << "setorigin: file " << args[i] << " is not a recognized 3D or 4D file type";
310
printErrorMsg(VB_ERROR,tmps.str());
318
setorigin_new(tokenlist &args,int mode)
320
if (args.size() < 2) {
330
for (int i=1; i<args.size(); i++) {
331
if (cb.ReadFile(args[i])==0) {
334
else if (mode==m_center)
335
cb.SetOrigin(cb.dimx/2,cb.dimy/2,cb.dimz/2);
336
if (cb.WriteFile()) {
338
tmps << "setorigin: error writing " << args[i];
339
printErrorMsg(VB_ERROR,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());
349
else if (ts.ReadFile(args[i])==0) {
352
else if (mode==m_center)
353
ts.SetOrigin(ts.dimx/2,ts.dimy/2,ts.dimz/2);
354
if (ts.WriteFile()) {
356
tmps << "setorigin: error writing " << args[i];
357
printErrorMsg(VB_ERROR,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());
375
cout << boost::format(myhelp) % vbversion;