File: create_grid_file.cpp

package info (click to toggle)
libminc 2.4.07-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,288 kB
  • sloc: ansic: 57,268; cpp: 3,654; sh: 100; makefile: 23; ruby: 18
file content (132 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (6)
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
#include <iostream>
#include <fstream>
#include <algorithm>

#include <minc_1_simple.h>
#include <assert.h>
#include <string.h>
// for get_opt_long
#include <getopt.h>
#include <unistd.h>
#include <minc_1_simple_rw.h>


using namespace  std;
using namespace  minc;

static void show_usage(const char *name)
{
  std::cerr 
    << "Usage: "<<name<<" <output_grid_mnc>" << std::endl
    << "Optional parameters:" << std::endl
    << "\t--verbose be verbose" << std::endl
    << "\t--clobber clobber the output files" << std::endl
    << "\t--version print version" << std::endl ;
}

  int main (int argc, char **argv)
  {
  int clobber=0;
  int verbose=0;

  // read the arguments
  static struct option long_options[] =
    {
  	  {"verbose", no_argument, &verbose, 1},
  	  {"quiet",   no_argument, &verbose, 0},
  	  {"clobber", no_argument, &clobber, 1},
      {"version", no_argument, 0, 'v'},
  	  {0, 0, 0, 0}
    };

  int c;
  for (;;)
  {
  	/* getopt_long stores the option index here. */
  	int option_index = 0;

  	c = getopt_long (argc, argv, "vt:m:", long_options, &option_index);

  	/* Detect the end of the options. */
  	if (c == -1)
  		break;

  	switch (c)
  	{
  	case 0:
  		break;
  	case 'v':
  		cout << "Version: 1.0" << endl;
  		return 0;
  	case '?':
  		/* getopt_long already printed an error message. */
  	default:
  		show_usage(argv[0]);
  		return 1;
  	}
  }

  if ((argc - optind) < 1)
  {
  	show_usage(argv[0]);
  	return 1;
  }
  
  std::string output  =argv[optind];
  
  if(!clobber && !access (output.c_str(), F_OK))
  {
    std::cerr << output.c_str () << " Exists!" << std::endl;
    return 1;
  }

  try
  {
    minc_info output_info_grid;
    
    const size_t nx=20,ny=20,nz=20;
    const double step=10;
    const double start_x=-100,start_y=-100,start_z=-100;
    const double amp=2.0; // amplitude of the harmonic deformation 

    // describe minc volume
    output_info_grid.push_back(
      dim_info(3,0,1,dim_info::DIM_VEC,false));
    output_info_grid.push_back(
      dim_info(nx,start_x,step,dim_info::DIM_X,false));
    output_info_grid.push_back(
      dim_info(ny,start_y,step,dim_info::DIM_Y,false));
    output_info_grid.push_back(
      dim_info(nz,start_z,step,dim_info::DIM_Z,false));
    
    //allocate volume
    minc::minc_grid_volume     grid(nx,ny,nz);
    //assign coordinates
    grid.start()=IDX<double>(start_x,start_y,start_z);
    grid.step()=IDX<double>(step,step,step);
    
    for(size_t x=0;x<nx;x++)
      for(size_t y=0;y<ny;y++)
        for(size_t z=0;z<nz;z++)
    {
      fixed_vec<3,double> pos=grid.voxel_to_world(IDX<size_t>(x,y,z));
      fixed_vec<3,float> def;
      
      def[0]=amp*sin(M_PI*pos[0]/100.0);
      def[1]=amp*sin(2*M_PI*pos[1]/100.0);
      def[2]=amp*cos(3*M_PI*pos[2]/100.0);
      
      grid.set(x,y,z,def);
    }
    //
    minc_1_writer wrt_vec;
    wrt_vec.open(output.c_str(),output_info_grid,3,NC_FLOAT);
    save_simple_volume(wrt_vec,grid);
    
  } catch (const minc::generic_error & err) {
    std::cerr << "Got an error at:" << err.file () << ":" << err.line () << std::endl;
    std::cerr << err.msg() << std::endl;
    return 1;
  }
  return 0;
}