OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
image_handle.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  basic image handle, manages shared instances of an image
23 
24  offers the Image interface
25 
26  Author: Ansgar Philippsen, Andreas Schenk
27 */
28 
29 #ifndef IMG_IMAGE_HANDLE_H
30 #define IMG_IMAGE_HANDLE_H
31 
32 #include <boost/shared_ptr.hpp>
33 
34 #include <ost/img/module_config.hh>
35 #include <ost/message.hh>
36 #include <ost/base.hh>
37 
38 #include "data.hh"
39 #include "observable.hh"
40 #include "extent_iterator.hh"
41 
44 
45 #include <ost/img/module_config.hh>
46 
47 namespace test_image {
48  void test_Observer();
49 }
50 
51 namespace ost { namespace img {
52 
53 using image_state::ImageStateBase;
55 using image_state::ImageStateNonModVisitorBase;
56 using image_state::ImageStateModIPVisitorBase;
57 using image_state::ImageStateConstModIPVisitorBase;
58 using image_state::ImageStateModOPVisitorBase;
59 using image_state::ImageStateConstModOPVisitorBase;
60 using image_state::ImageStateMorphVisitorBase;
61 
62 // fw declarations
63 class Point;
64 class Extent;
65 class Size;
66 class NonModAlgorithm;
67 class ModIPAlgorithm;
68 class ConstModIPAlgorithm;
69 class ModOPAlgorithm;
70 class ConstModOPAlgorithm;
71 class DataObserver;
72 
74 class DLLEXPORT InvalidImageHandle: public Error {
75 public:
76  InvalidImageHandle(): Error("Attempt to use invalid ImageHandle") {}
77 };
78 
79 // fw
80 class ConstImageHandle;
81 
83 
112  friend ImageHandle DoCreateImage(const Extent& e, DataType type, DataDomain dom);
113 
114  // for internal observer checking
115  friend void test_image::test_Observer();
116 
117  // for access to Sampling
118  friend class ConstImageHandle;
119 
120  typedef Observable DataObservable;
121  typedef boost::shared_ptr<DataObservable> ObsPtr;
122 
123  typedef boost::shared_ptr<ImageStateBasePtr> StatePtrPtr;
124 
125 public:
127 
132  ImageHandle();
133 
135 
138  ImageHandle(const ImageHandle& h);
139 
141 
145  ImageHandle& operator=(const ImageHandle& h);
146 
148 
153  ImageHandle Copy(bool cc=true) const;
154 
156 
160  ImageHandle Extract(const Extent& e) const;
161 
163 
169  void Paste(const Data& d);
170 
172 
179  void Set(const ImageHandle& h);
180 
182  void Swap(ImageHandle& h);
183 
185 
193  void Reset(const Extent &e, DataType type=REAL,DataDomain dom=SPATIAL);
194 
196  bool IsValid() const;
197 
199  long MemSize() const;
201 
202 
206  virtual DataType GetType() const;
208 
210  virtual DataDomain GetDomain() const;
211 
213 
216  virtual Extent GetExtent() const;
217 
219 
224  virtual Point GetSpatialOrigin() const;
225 
227  /*
228  For images in the frequency domain, a modification of the spatial origin
229  usually has drastic consequences, since all values are affected by a phase shift.
230  */
231  void SetSpatialOrigin(const Point& p);
232 
234  /*
235  See Extent for difference in even and odd dimensions. Only
236  has an effect for spatial images, since frequency data always
237  has origin in center.
238 
239  May have effect on values if image is in frequency domain.
240  */
241  void CenterSpatialOrigin();
243 
245  /*
246  This method returns the aboslute coordinate of the real space spatial
247  origin even when the map is in frequency space
248  */
249  Vec3 GetAbsoluteOrigin() const;
250 
252  /*
253  This method sets the aboslutes coordinate of the real space spatial
254  origin even when the map is in frequency space
255  */
256  void SetAbsoluteOrigin(const Vec3& c) ;
257 
259  /*
260  Returns the physical dimensions of a given Point based on
261  the sampling of the current domain and the absolute coordinates
262  of the origin of the image
263  */
264  Vec3 IndexToCoord(const Point &p) const;
265 
267  /*
268  Returns the location within the Data that corresponds to
269  the given physical coordinate, bases on the sampling of
270  the current domain and the absolute coordinates
271  of the origin of the image
272  */
273  Vec3 CoordToIndex(const Vec3& c) const;
274 
276  /*
277  Returns the physical dimensions of a Vec containing fractional Point
278  coordinates. The returned value is based on
279  the sampling of the current domain and the absolute coordinates
280  of the origin of the image
281  */
282  Vec3 FractionalIndexToCoord(const Vec3 &p) const;
283 
290  virtual Real GetReal(const Point& p) const;
292 
295  void SetReal(const Point &p, Real r);
296 
298  virtual Complex GetComplex(const Point& p) const;
299 
301  void SetComplex(const Point &p, const Complex& c);
303 
305  virtual Real GetIntpolReal(const Vec3 &v) const;
306  virtual Real GetIntpolReal(const Vec2 &v) const;
307  virtual Real GetIntpolReal(const Real &d) const;
308 
310  virtual Complex GetIntpolComplex(const Vec3 &v) const;
311  virtual Complex GetIntpolComplex(const Vec2 &v) const;
312  virtual Complex GetIntpolComplex(const Real &d) const;
313 
314 
315 
320  virtual void Attach(DataObserver *o) const;
322 
324  virtual void Detach(DataObserver *o) const;
325 
327  virtual void Notify() const;
328  virtual void Notify(const Extent& e) const;
329  virtual void Notify(const Point& p) const;
330 
331 
338 
340 
343  void Apply(NonModAlgorithm& a) const;
344 
346 
350  void ApplyIP(NonModAlgorithm& a) const;
351 
353  void ApplyIP(ModIPAlgorithm& a);
354 
356 
360  ImageHandle Apply(ModIPAlgorithm& a) const;
361 
363  void ApplyIP(const ConstModIPAlgorithm& a);
364 
366 
370  ImageHandle Apply(const ConstModIPAlgorithm& a) const;
371 
373 
379  void ApplyIP(ModOPAlgorithm& a);
380 
382 
385  ImageHandle Apply(ModOPAlgorithm& a) const;
386 
388 
394  void ApplyIP(const ConstModOPAlgorithm& a);
395 
397 
400  ImageHandle Apply(const ConstModOPAlgorithm& a) const;
402 
403 
409  void StateApply(ImageStateNonModVisitorBase& v) const;
411 
413  void StateApplyIP(ImageStateModIPVisitorBase& v);
415  ImageHandle StateApply(ImageStateModIPVisitorBase& v) const;
417  void StateApplyIP(const ImageStateConstModIPVisitorBase& v);
419  ImageHandle StateApply(const ImageStateConstModIPVisitorBase& v) const;
421  void StateApplyIP(ImageStateModOPVisitorBase& v);
423  ImageHandle StateApply(ImageStateModOPVisitorBase& v) const;
425  void StateApplyIP(const ImageStateConstModOPVisitorBase& v);
427  ImageHandle StateApply(const ImageStateConstModOPVisitorBase& v) const;
429  void StateApplyIP(ImageStateMorphVisitorBase& v);
431  ImageHandle StateApply(ImageStateMorphVisitorBase& v) const;
433  ExtentIterator GetIterator() const ;
434 
436 
442  bool operator==(const ImageHandle& ih) const;
443  bool operator!=(const ImageHandle& ih) const;
445 
449  ImageHandle& operator+=(Real v);
450  ImageHandle& operator+=(const Complex& v);
451  ImageHandle& operator-=(Real v);
452  ImageHandle& operator-=(const Complex& v);
453  ImageHandle& operator*=(Real v);
454  ImageHandle& operator*=(const Complex& v);
455  ImageHandle& operator/=(Real v);
456  ImageHandle& operator/=(const Complex& v);
457 
458  void Reciproce();
459 
460  ImageHandle& operator+=(const ConstImageHandle& h);
461  ImageHandle& operator-=(const ConstImageHandle& h);
462  ImageHandle& operator*=(const ConstImageHandle& h);
463  ImageHandle& operator/=(const ConstImageHandle& h);
464 
466 
470  ImageStateBasePtr& ImageStatePtr();
471  const ImageStateBasePtr& ImageStatePtr() const;
473 
474 protected:
476  ImageHandle(const StatePtrPtr& spp);
477 
479  virtual PixelSampling& Sampling();
481  virtual const PixelSampling& Sampling() const;
482 
484  static ImageHandle Create(const Extent& e, DataType type, DataDomain dom);
485 
486 private:
487  // this is all there is to it: two shared_ptr<> instances
488  StatePtrPtr impl_;
489  ObsPtr obs_;
490 
491  bool equal(const ImageHandle& ih) const;
492 };
493 
494 
496 
505 /*
506  In contrast to ConstData and Data, which use is-a inheritance,
507  ConstImageHandle implements its functionality using an instance
508  of ImageHandle as a private member variable, and an implicit
509  copy ctor and assignement op. This avoids Diamond-of-Death
510  virtual inheritance.
511 
512  On the downside, the complete ConstData interface needs to be
513  retyped and forwarded to the ImageHandle instance. It is a reference
514  to this instance that is returned for the 'upcasting' to const Data&
515 */
517  friend class ImageHandle;
518 public:
520  operator const Data& () const {return handle_;}
521 
524 
526 
530 
532 
536  ConstImageHandle(const ImageHandle& h);
537 
539 
543  ConstImageHandle& operator=(const ConstImageHandle& h);
544 
546 
549  ConstImageHandle& operator=(const ImageHandle& h);
550 
552  ImageHandle Copy(bool cc=true) const;
554 
556  ImageHandle Extract(const Extent& e) const;
557 
559  bool IsValid() const;
560 
562  long MemSize() const;
564 
568  DataType GetType() const;
570 
572  DataDomain GetDomain() const;
573 
575  Extent GetExtent() const;
576 
578  Point GetSpatialOrigin() const;
580 
582  Vec3 GetAbsoluteOrigin() const;
583 
585  Vec3 IndexToCoord(const Point &p) const;
586 
588  Vec3 CoordToIndex(const Vec3& c) const;
589 
591  Vec3 FractionalIndexToCoord(const Vec3 &p) const;
592 
596  Real GetReal(const Point& p) const;
598 
600  Complex GetComplex(const Point& p) const;
602 
604  virtual Real GetIntpolReal(const Vec3 &v) const;
605  virtual Real GetIntpolReal(const Vec2 &v) const;
606  virtual Real GetIntpolReal(const Real &d) const;
607 
609  virtual Complex GetIntpolComplex(const Vec3 &v) const;
610  virtual Complex GetIntpolComplex(const Vec2 &v) const;
611  virtual Complex GetIntpolComplex(const Real &d) const;
612 
613 
616  void Attach(DataObserver *o) const;
618 
620  void Detach(DataObserver *o) const;
621 
623  void Notify() const;
624  void Notify(const Extent& e) const;
625  void Notify(const Point& p) const;
626 
627 
632 
634  void Apply(NonModAlgorithm& a) const;
635  void ApplyIP(NonModAlgorithm& a) const;
636 
638  ImageHandle Apply(ModIPAlgorithm& a) const;
639 
641  ImageHandle Apply(const ConstModIPAlgorithm& a) const;
642 
644  ImageHandle Apply(ModOPAlgorithm& a) const;
645 
647  ImageHandle Apply(const ConstModOPAlgorithm& a) const;
649 
654  void StateApply(ImageStateNonModVisitorBase& v) const;
656 
658  ImageHandle StateApply(ImageStateModIPVisitorBase& v) const;
659 
661  ImageHandle StateApply(const ImageStateConstModIPVisitorBase& v) const;
662 
664  ImageHandle StateApply(ImageStateModOPVisitorBase& v) const;
665 
667  ImageHandle StateApply(const ImageStateConstModOPVisitorBase& v) const;
668 
670  ImageHandle StateApply(ImageStateMorphVisitorBase& v) const;
672  ExtentIterator GetIterator() const ;
674 
678  bool operator==(const ConstImageHandle& ih) const;
679  bool operator==(const ImageHandle& ih) const;
680  bool operator!=(const ConstImageHandle& ih) const;
681  bool operator!=(const ImageHandle& ih) const;
683 
684  const ImageStateBasePtr& ImageStatePtr() const;
685 
686 protected:
687 
689  PixelSampling& Sampling();
691  const PixelSampling& Sampling() const;
692 
693 private:
694  // actual implementation
695  ImageHandle handle_;
696 
697  bool equal(const ConstImageHandle& h) const;
698  bool equal(const ImageHandle& h) const;
699 };
700 
701 
709 // the other comparison ops are handled as member operators
710 DLLEXPORT_OST_IMG_BASE bool operator==(const ImageHandle& lhs, const ConstImageHandle& rhs);
711 DLLEXPORT_OST_IMG_BASE bool operator!=(const ImageHandle& lhs, const ConstImageHandle& rhs);
712 
713 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h, Real v);
714 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h, const Complex& v);
715 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(Real v, const ConstImageHandle& h);
716 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const Complex& v, const ConstImageHandle& h);
717 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h, Real v);
718 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h, const Complex& v);
719 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(Real v, const ConstImageHandle& h);
720 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const Complex& v, const ConstImageHandle& h);
721 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h, Real v);
722 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h, const Complex& v);
723 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(Real v, const ConstImageHandle& h);
724 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const Complex& v, const ConstImageHandle& h);
725 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h, Real v);
726 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h, const Complex& v);
727 
728 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h1, const ConstImageHandle& h2);
729 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h1, const ConstImageHandle& h2);
730 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h1, const ConstImageHandle& h2);
731 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h1, const ConstImageHandle& h2);
732 
733 
734 }} // namespace img
735 
736 #endif
out-of-place modification const algorithm
Non-modifying Algorithm, abstract base class.
templated observable class
Definition: observable.hh:44
DataDomain
underlying data type
Definition: data_types.hh:42
float Real
Definition: base.hh:44
std::complex< Real > Complex
Definition: base.hh:51
DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle &h, Real v)
DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle &h, Real v)
Abstract base class for data.
Definition: data.hh:253
DLLEXPORT_OST_IMG_BASE bool operator!=(const ImageHandle &lhs, const ConstImageHandle &rhs)
Abstract base class for read-only data.
Definition: data.hh:70
T Extract(const info::InfoGroup &g, const String &n)
Convenience function to extract a value from an item.
Definition: info_item.hh:128
Helper class to handle pixel sampling.
Defines lower and upper valid indices.
Definition: extent.hh:60
void test_Observer()
In-place modification const algorithm.
Abstract base class of data observer.
In-place modification algorithm.
Exception thrown upon invalid image handle usage.
Definition: image_handle.hh:74
read-only ImageHandle implementation
DLLEXPORT_OST_IMG_BASE bool operator==(const ImageHandle &lhs, const ConstImageHandle &rhs)
out-of-place modification algorithm
Manage shared instances of images.
boost::shared_ptr< ImageStateBase > ImageStateBasePtr
class encapsulating 1D to 3D point
Definition: point.hh:43
#define DLLEXPORT_OST_IMG_BASE
DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle &h, Real v)
DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle &h, Real v)