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
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 "data_observer.hh"
41 #include "function.hh"
42 #include "extent_iterator.hh"
43 
46 
47 #include <ost/img/module_config.hh>
48 
49 namespace test_image {
50  void test_Observer();
51 }
52 
53 namespace ost { namespace img {
54 
55 using image_state::ImageStateBase;
57 using image_state::ImageStateNonModVisitorBase;
58 using image_state::ImageStateModIPVisitorBase;
59 using image_state::ImageStateConstModIPVisitorBase;
60 using image_state::ImageStateModOPVisitorBase;
61 using image_state::ImageStateConstModOPVisitorBase;
62 using image_state::ImageStateMorphVisitorBase;
63 
64 // fw declarations
65 class Point;
66 class Extent;
67 class Size;
68 class NonModAlgorithm;
69 class ModIPAlgorithm;
70 class ConstModIPAlgorithm;
71 class ModOPAlgorithm;
72 class ConstModOPAlgorithm;
73 
75 class DLLEXPORT InvalidImageHandle: public Error {
76 public:
77  InvalidImageHandle(): Error("Attempt to use invalid ImageHandle") {}
78 };
79 
80 // fw
81 class ConstImageHandle;
82 
84 
113  friend ImageHandle DoCreateImage(const Extent& e, DataType type, DataDomain dom);
114 
115  // for internal observer checking
116  friend void test_image::test_Observer();
117 
118  // for access to Sampling
119  friend class ConstImageHandle;
120 
122  typedef boost::shared_ptr<DataObservable> ObsPtr;
123 
124  typedef boost::shared_ptr<ImageStateBasePtr> StatePtrPtr;
125 
126 public:
128 
133  ImageHandle();
134 
136 
139  ImageHandle(const ImageHandle& h);
140 
142 
146  ImageHandle& operator=(const ImageHandle& h);
147 
149 
150 
154  ImageHandle Copy(bool cc=true) const;
155 
157 
161  ImageHandle Extract(const Extent& e) const;
162 
164 
170  void Paste(const Data& d);
171 
173 
180  void Set(const ImageHandle& h);
181 
183  void Swap(ImageHandle& h);
184 
186 
194  void Reset(const Extent &e, DataType type=REAL,DataDomain dom=SPATIAL);
195 
197  bool IsValid() const;
198 
200  long MemSize() const;
202 
203 
207 
208  virtual DataType GetType() const;
209 
211  virtual DataDomain GetDomain() const;
212 
214 
217  virtual Extent GetExtent() const;
218 
220 
225  virtual Point GetSpatialOrigin() const;
226 
228  /*
229  For images in the frequency domain, a modification of the spatial origin
230  usually has drastic consequences, since all values are affected by a phase shift.
231  */
232  void SetSpatialOrigin(const Point& p);
233 
235  /*
236  See Extent for difference in even and odd dimensions. Only
237  has an effect for spatial images, since frequency data always
238  has origin in center.
239 
240  May have effect on values if image is in frequency domain.
241  */
242  void CenterSpatialOrigin();
244 
246  /*
247  This method returns the aboslute coordinate of the real space spatial
248  origin even when the map is in frequency space
249  */
250  Vec3 GetAbsoluteOrigin() const;
251 
253  /*
254  This method sets the aboslutes coordinate of the real space spatial
255  origin even when the map is in frequency space
256  */
257  void SetAbsoluteOrigin(const Vec3& c) ;
258 
260  /*
261  Returns the physical dimensions of a given Point based on
262  the sampling of the current domain and the absolute coordinates
263  of the origin of the image
264  */
265  Vec3 IndexToCoord(const Point &p) const;
266 
268  /*
269  Returns the location within the Data that corresponds to
270  the given physical coordinate, bases on the sampling of
271  the current domain and the absolute coordinates
272  of the origin of the image
273  */
274  Vec3 CoordToIndex(const Vec3& c) const;
275 
277  /*
278  Returns the physical dimensions of a Vec containing fractional Point
279  coordinates. The returned value is based on
280  the sampling of the current domain and the absolute coordinates
281  of the origin of the image
282  */
283  Vec3 FractionalIndexToCoord(const Vec3 &p) const;
284 
291 
292  virtual Real GetReal(const Point& p) const;
293 
296  void SetReal(const Point &p, Real r);
297 
299  virtual Complex GetComplex(const Point& p) const;
300 
302  void SetComplex(const Point &p, const Complex& c);
304 
306  virtual Real GetIntpolReal(const Vec3 &v) const;
307  virtual Real GetIntpolReal(const Vec2 &v) const;
308  virtual Real GetIntpolReal(const Real &d) const;
309 
311  virtual Complex GetIntpolComplex(const Vec3 &v) const;
312  virtual Complex GetIntpolComplex(const Vec2 &v) const;
313  virtual Complex GetIntpolComplex(const Real &d) const;
314 
315 
316 
321 
322  virtual void Attach(DataObserver *o) const;
323 
325  virtual void Detach(DataObserver *o) const;
326 
328  virtual void Notify() const;
329  virtual void Notify(const Extent& e) const;
330  virtual void Notify(const Point& p) const;
331 
332 
339 
341 
344  void Apply(NonModAlgorithm& a) const;
345 
347 
351  void ApplyIP(NonModAlgorithm& a) const;
352 
354  void ApplyIP(ModIPAlgorithm& a);
355 
357 
361  ImageHandle Apply(ModIPAlgorithm& a) const;
362 
364  void ApplyIP(const ConstModIPAlgorithm& a);
365 
367 
371  ImageHandle Apply(const ConstModIPAlgorithm& a) const;
372 
374 
380  void ApplyIP(ModOPAlgorithm& a);
381 
383 
386  ImageHandle Apply(ModOPAlgorithm& a) const;
387 
389 
395  void ApplyIP(const ConstModOPAlgorithm& a);
396 
398 
401  ImageHandle Apply(const ConstModOPAlgorithm& a) const;
403 
404 
410 
411  void StateApply(ImageStateNonModVisitorBase& v) const;
412 
414  void StateApplyIP(ImageStateModIPVisitorBase& v);
416  ImageHandle StateApply(ImageStateModIPVisitorBase& v) const;
418  void StateApplyIP(const ImageStateConstModIPVisitorBase& v);
420  ImageHandle StateApply(const ImageStateConstModIPVisitorBase& v) const;
422  void StateApplyIP(ImageStateModOPVisitorBase& v);
424  ImageHandle StateApply(ImageStateModOPVisitorBase& v) const;
426  void StateApplyIP(const ImageStateConstModOPVisitorBase& v);
428  ImageHandle StateApply(const ImageStateConstModOPVisitorBase& v) const;
430  void StateApplyIP(ImageStateMorphVisitorBase& v);
432  ImageHandle StateApply(ImageStateMorphVisitorBase& v) const;
434  ExtentIterator GetIterator() const ;
435 
437 
443  bool operator==(const ImageHandle& ih) const;
444  bool operator!=(const ImageHandle& ih) const;
446 
450  ImageHandle& operator+=(Real v);
451  ImageHandle& operator+=(const Complex& v);
452  ImageHandle& operator-=(Real v);
453  ImageHandle& operator-=(const Complex& v);
454  ImageHandle& operator*=(Real v);
455  ImageHandle& operator*=(const Complex& v);
456  ImageHandle& operator/=(Real v);
457  ImageHandle& operator/=(const Complex& v);
458 
459  void Reciproce();
460 
461  ImageHandle& operator+=(const ConstImageHandle& h);
462  ImageHandle& operator-=(const ConstImageHandle& h);
463  ImageHandle& operator*=(const ConstImageHandle& h);
464  ImageHandle& operator/=(const ConstImageHandle& h);
465 
466  ImageHandle& operator+=(const Function& h);
467  ImageHandle& operator-=(const Function& h);
468  ImageHandle& operator*=(const Function& h);
469  ImageHandle& operator/=(const Function& h);
470 
472 
476  ImageStateBasePtr& ImageStatePtr();
477  const ImageStateBasePtr& ImageStatePtr() const;
479 
480 protected:
482  ImageHandle(const StatePtrPtr& spp);
483 
485  virtual PixelSampling& Sampling();
487  virtual const PixelSampling& Sampling() const;
488 
490  static ImageHandle Create(const Extent& e, DataType type, DataDomain dom);
491 
492 private:
493  // this is all there is to it: two shared_ptr<> instances
494  StatePtrPtr impl_;
495  ObsPtr obs_;
496 
497  bool equal(const ImageHandle& ih) const;
498 };
499 
500 
502 
511 /*
512  In contrast to ConstData and Data, which use is-a inheritance,
513  ConstImageHandle implements its functionality using an instance
514  of ImageHandle as a private member variable, and an implicit
515  copy ctor and assignement op. This avoids Diamond-of-Death
516  virtual inheritance.
517 
518  On the downside, the complete ConstData interface needs to be
519  retyped and forwarded to the ImageHandle instance. It is a reference
520  to this instance that is returned for the 'upcasting' to const Data&
521 */
523  friend class ImageHandle;
524 public:
526  operator const Data& () const {return handle_;}
527 
530 
532 
536 
538 
542  ConstImageHandle(const ImageHandle& h);
543 
545 
549  ConstImageHandle& operator=(const ConstImageHandle& h);
550 
552 
555  ConstImageHandle& operator=(const ImageHandle& h);
556 
558 
559  ImageHandle Copy(bool cc=true) const;
560 
562  ImageHandle Extract(const Extent& e) const;
563 
565  bool IsValid() const;
566 
568  long MemSize() const;
570 
574 
575  DataType GetType() const;
576 
578  DataDomain GetDomain() const;
579 
581  Extent GetExtent() const;
582 
584  Point GetSpatialOrigin() const;
586 
588  Vec3 GetAbsoluteOrigin() const;
589 
591  Vec3 IndexToCoord(const Point &p) const;
592 
594  Vec3 CoordToIndex(const Vec3& c) const;
595 
597  Vec3 FractionalIndexToCoord(const Vec3 &p) const;
598 
602 
603  Real GetReal(const Point& p) const;
604 
606  Complex GetComplex(const Point& p) const;
608 
610  virtual Real GetIntpolReal(const Vec3 &v) const;
611  virtual Real GetIntpolReal(const Vec2 &v) const;
612  virtual Real GetIntpolReal(const Real &d) const;
613 
615  virtual Complex GetIntpolComplex(const Vec3 &v) const;
616  virtual Complex GetIntpolComplex(const Vec2 &v) const;
617  virtual Complex GetIntpolComplex(const Real &d) const;
618 
619 
622 
623  void Attach(DataObserver *o) const;
624 
626  void Detach(DataObserver *o) const;
627 
629  void Notify() const;
630  void Notify(const Extent& e) const;
631  void Notify(const Point& p) const;
632 
633 
638 
640  void Apply(NonModAlgorithm& a) const;
641  void ApplyIP(NonModAlgorithm& a) const;
642 
644  ImageHandle Apply(ModIPAlgorithm& a) const;
645 
647  ImageHandle Apply(const ConstModIPAlgorithm& a) const;
648 
650  ImageHandle Apply(ModOPAlgorithm& a) const;
651 
653  ImageHandle Apply(const ConstModOPAlgorithm& a) const;
655 
660 
661  void StateApply(ImageStateNonModVisitorBase& v) const;
662 
664  ImageHandle StateApply(ImageStateModIPVisitorBase& v) const;
665 
667  ImageHandle StateApply(const ImageStateConstModIPVisitorBase& v) const;
668 
670  ImageHandle StateApply(ImageStateModOPVisitorBase& v) const;
671 
673  ImageHandle StateApply(const ImageStateConstModOPVisitorBase& v) const;
674 
676  ImageHandle StateApply(ImageStateMorphVisitorBase& v) const;
678  ExtentIterator GetIterator() const ;
680 
684  bool operator==(const ConstImageHandle& ih) const;
685  bool operator==(const ImageHandle& ih) const;
686  bool operator!=(const ConstImageHandle& ih) const;
687  bool operator!=(const ImageHandle& ih) const;
689 
690  const ImageStateBasePtr& ImageStatePtr() const;
691 
692 protected:
693 
695  PixelSampling& Sampling();
697  const PixelSampling& Sampling() const;
698 
699 private:
700  // actual implementation
701  ImageHandle handle_;
702 
703  bool equal(const ConstImageHandle& h) const;
704  bool equal(const ImageHandle& h) const;
705 };
706 
707 
715 // the other comparison ops are handled as member operators
716 DLLEXPORT_OST_IMG_BASE bool operator==(const ImageHandle& lhs, const ConstImageHandle& rhs);
717 DLLEXPORT_OST_IMG_BASE bool operator!=(const ImageHandle& lhs, const ConstImageHandle& rhs);
718 
719 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h, Real v);
720 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h, const Complex& v);
721 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(Real v, const ConstImageHandle& h);
722 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const Complex& v, const ConstImageHandle& h);
723 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h, Real v);
724 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h, const Complex& v);
725 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(Real v, const ConstImageHandle& h);
726 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const Complex& v, const ConstImageHandle& h);
727 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h, Real v);
728 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h, const Complex& v);
729 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(Real v, const ConstImageHandle& h);
730 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const Complex& v, const ConstImageHandle& h);
731 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h, Real v);
732 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h, const Complex& v);
733 
734 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h1, const ConstImageHandle& h2);
735 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h1, const ConstImageHandle& h2);
736 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h1, const ConstImageHandle& h2);
737 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h1, const ConstImageHandle& h2);
738 
739 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const ConstImageHandle& h1, const Function& h2);
740 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const ConstImageHandle& h1, const Function& h2);
741 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const ConstImageHandle& h1, const Function& h2);
742 DLLEXPORT_OST_IMG_BASE ImageHandle operator/(const ConstImageHandle& h1, const Function& h2);
743 
744 DLLEXPORT_OST_IMG_BASE ImageHandle operator+(const Function& h1, const ConstImageHandle& h2);
745 DLLEXPORT_OST_IMG_BASE ImageHandle operator-(const Function& h1, const ConstImageHandle& h2);
746 DLLEXPORT_OST_IMG_BASE ImageHandle operator*(const Function& h1, const ConstImageHandle& h2);
747 
748 }} // namespace img
749 
750 #endif