GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/grid.c Lines: 12 64 18.8 %
Date: 2017-05-26 Branches: 1 46 2.2 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "unrrdu.h"
25
#include "privateUnrrdu.h"
26
27
#define INFO "generate list of oriented grid locations"
28
static const char *_unrrdu_gridInfoL =
29
(INFO ". For a N-D grid, the output is a 2-D M-by-S array of grid sample "
30
 "locations, where M is the space dimension of the oriented grid, and S "
31
 "is the total number of real samples in the grid. "
32
 "Implementation currently incomplete, because of the number of "
33
 "unresolved design questions.\n "
34
 "* (not based on any particular nrrd function)");
35
36
static int
37
gridGen(Nrrd *nout, int typeOut, const Nrrd *nin) {
38
  static const char me[]="gridGen";
39
  size_t II, NN, size[NRRD_DIM_MAX], coord[NRRD_DIM_MAX];
40
  double loc[NRRD_SPACE_DIM_MAX],
41
    sdir[NRRD_DIM_MAX][NRRD_SPACE_DIM_MAX],
42
    (*ins)(void *v, size_t I, double d);
43
  unsigned int axi, dim, sdim, base;
44
  void *out;
45
46
  if (nrrdTypeBlock == typeOut) {
47
    biffAddf(UNRRDU, "%s: can't use type %s", me,
48
             airEnumStr(nrrdType, nrrdTypeBlock));
49
    return 1;
50
  }
51
  if (!(nin->spaceDim)) {
52
    biffAddf(UNRRDU, "%s: can currently only work on arrays "
53
             "with space directions and space origin", me);
54
    return 1;
55
  }
56
  dim = nin->dim;
57
  sdim = nin->spaceDim;
58
  if (!nrrdSpaceVecExists(sdim, nin->spaceOrigin)) {
59
    biffAddf(UNRRDU, "%s: space origin didn't exist", me);
60
    return 1;
61
  }
62
  if (!( sdim <= dim )) {
63
    biffAddf(UNRRDU, "%s: can't handle space dimension %u > dimension %u",
64
             me, sdim, dim);
65
    return 1;
66
  }
67
  base = dim - sdim;
68
  NN = 1;
69
  nrrdAxisInfoGet_nva(nin, nrrdAxisInfoSize, size);
70
  nrrdAxisInfoGet_nva(nin, nrrdAxisInfoSpaceDirection, sdir);
71
  for (axi=base; axi<dim; axi++) {
72
    if (!nrrdSpaceVecExists(sdim, sdir[axi])) {
73
      biffAddf(UNRRDU, "%s: axis %u space dir didn't exist", me, axi);
74
      return 1;
75
    }
76
    NN *= size[axi];
77
  }
78
  ins = nrrdDInsert[typeOut];
79
80
  if (nrrdMaybeAlloc_va(nout, typeOut, 2,
81
                        AIR_CAST(size_t, sdim),
82
                        NN)) {
83
    biffMovef(UNRRDU, NRRD, "%s: couldn't allocate output", me);
84
    return 1;
85
  }
86
  out = AIR_CAST(void *, nout->data);
87
  for (axi=0; axi<dim; axi++) {
88
    coord[axi] = 0;
89
  }
90
  for (II=0; II<NN; II++) {
91
    nrrdSpaceVecCopy(loc, nin->spaceOrigin);
92
    for (axi=base; axi<dim; axi++) {
93
      nrrdSpaceVecScaleAdd2(loc, 1, loc, coord[axi], sdir[axi]);
94
    }
95
    /*
96
    fprintf(stderr, "!%s: (%u) %u %u %u: %g %g\n", me,
97
            AIR_CAST(unsigned int, II),
98
            AIR_CAST(unsigned int, coord[0]),
99
            AIR_CAST(unsigned int, coord[1]),
100
            AIR_CAST(unsigned int, coord[2]),
101
            loc[0], loc[1]);
102
    */
103
    for (axi=0; axi<sdim; axi++) {
104
      ins(out, axi + sdim*II, loc[axi]);
105
    }
106
    NRRD_COORD_INCR(coord, size, dim, base);
107
  }
108
109
  return 0;
110
}
111
112
int
113
unrrdu_gridMain(int argc, const char **argv, const char *me,
114
                   hestParm *hparm) {
115
2
  hestOpt *opt = NULL;
116
1
  char *out, *err;
117
1
  Nrrd *nin, *nout;
118
  int pret;
119
  airArray *mop;
120
121
1
  int typeOut;
122
123
1
  hestOptAdd(&opt, "i,input", "nin", airTypeOther, 1, 1, &nin, NULL,
124
             "input nrrd.  That this argument is required instead of "
125
             "optional, as with most unu commands, is a quirk caused by the "
126
             "need to have \"unu grid\" generate usage info, combined "
127
             "with the fact that the other arguments have sensible "
128
             "defaults",
129
1
             NULL, NULL, nrrdHestNrrd);
130
1
  OPT_ADD_TYPE(typeOut, "type of output", "double");
131
1
  OPT_ADD_NOUT(out, "output nrrd");
132
133
1
  mop = airMopNew();
134
1
  airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
135
136
2
  USAGE(_unrrdu_gridInfoL);
137
  PARSE();
138
  airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
139
140
  nout = nrrdNew();
141
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
142
143
  if (gridGen(nout, typeOut, nin)) {
144
    airMopAdd(mop, err = biffGetDone(UNRRDU), airFree, airMopAlways);
145
    fprintf(stderr, "%s: error generating output:\n%s", me, err);
146
    airMopError(mop);
147
    return 1;
148
  }
149
150
  SAVE(out, nout, NULL);
151
152
  airMopOkay(mop);
153
  return 0;
154
1
}
155
156
UNRRDU_CMD_HIDE(grid, INFO);