OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
image_state_impl.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-2011 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  Authors: Ansgar Philippsen, Andreas Schenk
23 */
24 
25 #ifndef IMAGE_STATE_IMPL_HH
26 #define IMAGE_STATE_IMPL_HH
27 
28 #include <iostream>
29 #include <boost/shared_ptr.hpp>
30 
31 #include <ost/base.hh>
32 #include <ost/img/value_util.hh>
34 
35 #include "image_state_base.hh"
37 #include "value_holder.hh"
38 
39 namespace ost { namespace img {
40 
41 class Size;
42 
43 namespace image_state {
44 
45 /*
46  Provides concrete implementation of abstract
47  ImageStateBase interface. The two template
48  parameters allow a combination of value
49  and domain to be specified, which is
50  utilized in the image state algorithms
51  as well as the binary dispatch code.
52 
53  Internally, the ValueHolder and Domain interplay
54  to provide the functionality
55 
56 */
57 // \internal
58 template <typename T, class D>
59 class TEMPLATE_EXPORT ImageStateImpl: public ImageStateBase
60 {
61 public:
63  typedef boost::shared_ptr<ImageStateImpl<T,D> > SharedPtrType;
64 
66 
68 
69  ImageStateImpl<T,D>& operator=(const ImageStateImpl<T,D>& s);
70 
71  // requires logical extent, not physical one!
72  ImageStateImpl(const Extent& e, const PixelSampling& s);
73 
74  // also takes absolute center, requires logical extent, not physical one!
75  ImageStateImpl(const Extent& e, const PixelSampling& s, const Vec3& c);
76 
77  /*
78  special ctor to utilize pre-allocated memory: the data area allocated
79  by ValueHolder will be transfered to this image state
80  */
81  template <typename U>
83  domain_(e),
84  data_(domain_.GetExtent().GetSize(),d),
85  sampling_(s)
86  {
87  assert(d.GetPhysicalSize()*sizeof(U)==data_.GetPhysicalSize()*sizeof(T));
88  }
89 
90  virtual ~ImageStateImpl();
91 
92  SharedPtrType CloneState(bool cc=true) const;
93 
94  // image state base interface
95  virtual ImageStateBasePtr Clone(bool cc=true) const;
96 
97  virtual long MemSize() const;
98 
99  virtual DataType GetType() const;
100 
101  virtual DataDomain GetDomain() const;
102 
103  virtual void SetSpatialOrigin(const Point& o);
104 
105  virtual void CenterSpatialOrigin();
106 
107  virtual Point GetSpatialOrigin() const;
108 
109  virtual Extent GetExtent() const;
110 
111  virtual Extent GetLogicalExtent() const;
112 
113  virtual Real GetReal(const Point &p) const;
114 
115  virtual void SetReal(const Point &p, const Real& r);
116 
117  virtual Complex GetComplex(const Point &p) const;
118 
119  virtual void SetComplex(const Point &p, const Complex& c);
120 
121  virtual Real GetIntpolReal(const Vec3 &v) const;
122  virtual Real GetIntpolReal(const Vec2 &v) const;
123  virtual Real GetIntpolReal(const Real &d) const;
124 
125  virtual Complex GetIntpolComplex(const Vec3 &v) const;
126  virtual Complex GetIntpolComplex(const Vec2 &v) const;
127  virtual Complex GetIntpolComplex(const Real &d) const;
128 
129  virtual PixelSampling& GetSampling();
130 
131  virtual const PixelSampling& GetSampling() const;
132 
133  virtual void SetSampling(const PixelSampling& s);
134 
135  virtual Vec3 GetAbsoluteOrigin() const;
136 
137  virtual void SetAbsoluteOrigin(const Vec3& c);
138 
139  virtual Vec3 IndexToCoord(const Point &p) const;
140 
141  virtual Vec3 CoordToIndex(const Vec3& c) const;
142 
143  virtual Vec3 FractionalIndexToCoord(const Vec3 &p) const;
144 
145  void AdjustPhaseOrigin(const Point& p);
146 
147  // value interface
149  // non-virtual member functions
150 
151  // retrieve value at specified point
152  /*
153  First converts point to index using domain policy,
154  then uses ValueHolderType::Value member function to
155  retrieve actual value. No boundary check is performed
156  here, this is the responsibility of the caller!
157  */
158  T& Value(const Point& p);
159 
160  // retrieve ro value at specified point
161  const T& Value(const Point& p) const;
162 
164  T GetCheckedValue(const Point& p) const;
165 
167  void SetCheckedValue(const Point& p, const T& v);
168 
169  // calculate interpolated value at given point
170  /*
171  out-of-bounds values are automatically set to zero
172  */
173  T CalcIntpolValue(const Vec3& v) const;
174  T CalcIntpolValue(const Vec2& v) const;
175  T CalcIntpolValue(const Real& v) const;
176 
177  // pass-through to value holder
178  T& Value(const Index& i);
179 
180  // pass-through to value holder
181  const T& Value(const Index& i) const;
182 
183  // pass-through to value holder
184  T& Value(unsigned int i);
185 
186  // pass-through to value holder
187  const T& Value(unsigned int i) const;
188 
190  ValueHolder<T>& Data();
192  const ValueHolder<T>& Data() const;
193 
194  // other
195  Size GetSize() const;
196 
197  // visitor interface
198 
199  virtual void ApplyIP(ImageStateNonModVisitorBase& v) const;
200 
201  virtual void Apply(ImageStateNonModVisitorBase& v) const;
202 
203  virtual void ApplyIP(ImageStateModIPVisitorBase& v);
204 
205  virtual void ApplyIP(const ImageStateConstModIPVisitorBase& v);
206 
207  virtual ImageStateBasePtr Apply(ImageStateModIPVisitorBase& v) const;
208 
209  virtual ImageStateBasePtr Apply(const ImageStateConstModIPVisitorBase& v) const;
210 
211  virtual ImageStateBasePtr Apply(ImageStateModOPVisitorBase& v) const;
212 
213  virtual ImageStateBasePtr Apply(const ImageStateConstModOPVisitorBase& v) const;
214 
216 
217  // operators
218  virtual ImageStateBase& operator+=(Real v);
219  virtual ImageStateBase& operator+=(const Complex& v);
220  virtual ImageStateBase& operator-=(Real v);
221  virtual ImageStateBase& operator-=(const Complex& v);
222  virtual ImageStateBase& operator*=(Real v);
223  virtual ImageStateBase& operator*=(const Complex& v);
224  virtual ImageStateBase& operator/=(Real v);
225  virtual ImageStateBase& operator/=(const Complex& v);
226 
227  virtual void operator+=(const Function& b);
228  virtual void operator-=(const Function& b);
229  virtual void operator*=(const Function& b);
230  virtual void operator/=(const Function& b);
231 
232 protected:
233  // helper methods for operators above
234  template <typename U> void add_scalar(const U& v);
235  template <typename U> void mul_scalar(const U& v);
236 
237 private:
238  D domain_;
239  ValueHolder<T> data_;
240  PixelSampling sampling_;
241  geom::Vec3 absolute_origin_;
242 };
243 
244 
245 }}} // ns
246 
247 #endif