OpenStructure
threshold.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2020 by the OpenStructure authors
5 // Copyright (C) 2003-2010 by the IPLT authors
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3.0 of the License, or (at your option)
10 // any later version.
11 // This library is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 // details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //------------------------------------------------------------------------------
20 
21 /*
22  Author: Andreas Schenk
23 */
24 
25 #include <ost/img/image_state.hh>
27 
28 namespace ost { namespace img { namespace alg {
29 
30 namespace {
31 
32 template<typename T>
33 bool bigger(T& v, Real rv, const Complex& cv);
34 
35 template<>
36 bool bigger<Real>(Real& v, Real rv, const Complex& cv) {return v>=rv;}
37 
38 template<>
39 bool bigger<Complex>(Complex& v, Real rv, const Complex& cv) {return std::abs(v)>=std::abs(cv);}
40 
41 template<typename T>
42 bool bigger(T& v, Real rv, const Complex& cv)
43 {
44  return static_cast<Real>(v)>=rv;
45 }
46 
47 struct ThresholdFnc {
48  explicit ThresholdFnc(): r_fv_(0.0) { set_rval(0.0); }
49  explicit ThresholdFnc(Real f) {set_rval(f);}
50  explicit ThresholdFnc(const Complex& f) {set_cval(f);}
51 
52  void SetThreshold(Real f) {set_rval(f);}
53  void SetThreshold(const Complex& f) {set_cval(f);}
54 
55  template <typename T, class D>
56  void VisitState(ImageStateImpl<T,D>& isi) const {
57  static const T zero=T(); // default ctor for zero value
58  static const T one=T(1); // default ctor for one value
59  for(T* p = isi.Data().GetData(); p<isi.Data().GetEnd();++p) {
60  (*p)= bigger<T>(*p,r_fv_,c_fv_)? one : zero ;
61  }
62  }
63 
64  static String GetAlgorithmName() {return "Threshold";}
65 
66 private:
67  Real r_fv_;
68  Complex c_fv_;
69 
70  void set_rval(Real f) {r_fv_=f; c_fv_=Val2Val<Real,Complex>(f);}
71  void set_cval(const Complex& f) {c_fv_=f; r_fv_=Val2Val<Complex,Real>(f);}
72 };
73 
74 } // anon ns
75 
77 
78 }
79 
81 
82 
83 }} // ns
84 
85 
in-place modifying image state const visitor plus ip algorithm
in-place modifying image state visitor plus ip algorithm
#define OST_IMG_ALG_EXPLICIT_INST_DECL(c, t)
float Real
Definition: base.hh:44
std::complex< Real > Complex
Definition: base.hh:51
std::string String
Definition: base.hh:54
ImageStateConstModIPAlgorithm< ThresholdFnc > Threshold
Definition: threshold.hh:76
Real Val2Val< Complex, Real >(const Complex &c)
Definition: value_util.hh:91
Definition: base.dox:1