ODIN
filter_reduction.h
1 /***************************************************************************
2  filter_reduction.h - description
3  -------------------
4  begin : Fri Feb 26 2010
5  copyright : (C) 2000-2021 by Thies Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef FILTER_REDUCTION_H
19 #define FILTER_REDUCTION_H
20 
21 
22 #include <odindata/filter_step.h>
23 
24 enum reductionOp { minip=0, maxip, meanp, sump };
25 static const char* reductionOpLabel[]={"minip", "maxip", "proj", "sum"};
26 
27 template<int Op>
28 class FilterReduction : public FilterStep
29 {
30  LDRenum dir;
31 
32  STD_string label() const {return reductionOpLabel[Op];}
33 
34  STD_string description() const {
35  STD_string opstr;
36  if(Op==minip) opstr="minimum intensity ";
37  if(Op==maxip) opstr="maximum intensity ";
38  if(Op==meanp) opstr="mean ";
39  if(Op==sump) opstr="sum ";
40  return "Perform "+opstr+"projection over given direction";
41  }
42 
43  void init() {
44  for(int i=0; i<n_dataDim; i++) dir.add_item(dataDimLabel[i]);
45  dir.add_item(ODIN_NONE_STR); // default to throw error when no dim is given
46  dir.set_actual(dir.n_items()-1);
47  dir.set_cmdline_option("dir").set_description("direction");
48  append_arg(dir,"dir");
49  }
50 
51  bool process(Data<float,4>& data, Protocol& prot) const {
52  Log<Filter> odinlog(c_label(),"process");
53 
54  if(dir==ODIN_NONE_STR){
55  ODINLOG(odinlog,errorLog) << "no valid dimension given" << STD_endl;
56  return false;
57  }
58 
59  TinyVector<int,4> inshape=data.shape();
60 
61  TinyVector<int,4> outshape=data.shape();
62  outshape(dir)=1;
63 
64  Data<float,4> outdata(outshape);
65 
66  for(unsigned int i=0; i<outdata.size(); i++) {
67  TinyVector<int,4> index=outdata.create_index(i);
68 
69  TinyVector<int,4> lowerBounds=index;
70  TinyVector<int,4> upperBounds=index;
71  upperBounds(dir)=inshape(dir)-1;
72 
73  if(Op==minip) outdata(index)=min (data(RectDomain<4>(lowerBounds, upperBounds)));
74  if(Op==maxip) outdata(index)=max (data(RectDomain<4>(lowerBounds, upperBounds)));
75  if(Op==meanp) outdata(index)=mean(data(RectDomain<4>(lowerBounds, upperBounds)));
76  if(Op==sump) outdata(index)=sum (data(RectDomain<4>(lowerBounds, upperBounds)));
77  }
78 
79  data.reference(outdata);
80 
81  // Adjusting protocol, center of slicepack and FOV remains the same
82  if(dir==timeDim) {
84  } else {
85  if(dir==sliceDim) prot.geometry.set_nSlices(1);
86  prot.seqpars.set_MatrixSize(direction(3-dir),1);
87  }
88  return true;
89  }
90 
91  FilterStep* allocate() const {return new FilterReduction<Op>();}
92 };
93 
94 #endif
virtual bool process(Data< float, 4 > &data, Protocol &prot) const
Geometry & set_nSlices(unsigned int nslices)
Definition: tjlog.h:218
Protocol proxy.
Definition: protocol.h:33
SeqPars seqpars
Definition: protocol.h:88
Geometry geometry
Definition: protocol.h:83
SeqPars & set_MatrixSize(direction dir, unsigned int size, parameterMode parmode=edit)
SeqPars & set_NumOfRepetitions(unsigned int times, parameterMode parmode=edit)
virtual FilterStep * allocate() const=0
void append_arg(LDRbase &arg, const STD_string &arglabel)
Definition: step_code.h:76
virtual STD_string description() const=0
virtual void init()=0
virtual STD_string label() const=0
direction
Definition: odinpara.h:41