OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
image_state_spatial_domain.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  Author: Ansgar Philippsen
23 */
24 
25 #ifndef IMAGE_STATE_SPATIAL_DOMAIN_HH
26 #define IMAGE_STATE_SPATIAL_DOMAIN_HH
27 
28 #include <iostream>
29 
30 #include <ost/base.hh>
31 #include <ost/img/point.hh>
32 #include <ost/img/size.hh>
33 #include <ost/img/extent.hh>
35 #include <ost/img/value_util.hh>
36 #include "value_holder.hh"
37 #include "index.hh"
38 
39 namespace ost { namespace img { namespace image_state {
40 
41 /*
42  while all domain implementations share a common interface
43  they should be seen as policy classes, used as a template
44  parameter, and thus are not derived from a common base class.
45 
46  copy ctor and assignment op should work automatically
47 */
48 
50 public:
51  SpatialDomain(const Extent& e):
52  extent_(e),
53  physical_extent_(ExtractPhysicalExtent(e))
54  {}
55 
56  // interface for ImageStateImpl
57  DataDomain GetDomain() const {return SPATIAL;}
58 
59  void SetSpatialOrigin(const Point& o) {
60  extent_=Extent(o,extent_.GetSize());
61  }
62 
64  return extent_.GetStart();
65  }
66 
67  Extent GetExtent() const {
68  return extent_;
69  }
70 
72  return extent_;
73  }
74 
76  return physical_extent_;
77  }
78 
79  template <typename V>
80  Real GetReal(const Point &p, const ValueHolder<V>& data) const {
81  if(extent_.Contains(p)) {
82  return Val2Val<V,Real>(data.Value(Point2Index(p)));
83  } else {
84  return 0.0;
85  }
86  }
87 
88  template <typename V>
89  void SetReal(const Point &p, const Real& r, ValueHolder<V>& data) {
90  if(extent_.Contains(p)) {
91  data.Value(Point2Index(p))=Val2Val<Real,V>(r);
92  }
93  }
94 
95  template <typename V>
96  Complex GetComplex(const Point &p, const ValueHolder<V>& data) const {
97  if(extent_.Contains(p)) {
98  return Val2Val<V,Complex>(data.Value(Point2Index(p)));
99  } else {
100  return Complex(0.0,0.0);
101  }
102  }
103 
104  template <typename V>
105  void SetComplex(const Point &p, const Complex& c, ValueHolder<V>& data) {
106  if(extent_.Contains(p)) {
107  data.Value(Point2Index(p))=Val2Val<Complex,V>(c);
108  }
109  }
110 
111  Index Point2Index(const Point& p) const {
112  return Index(p[0]-extent_.GetStart()[0],
113  p[1]-extent_.GetStart()[1],
114  p[2]-extent_.GetStart()[2]);
115  }
116 
117 private:
118  Extent extent_;
119  Extent physical_extent_;
120 
121  Extent ExtractPhysicalExtent(const Extent& e) const {
122 #if 1
123  Point pad;
124  if(e.GetDim()==1) {
125  pad=Point( (e.GetSize()[0]&0x1) ? 1 : 2,0,0);
126  } else if(e.GetDim()==2) {
127  pad=Point(0,(e.GetSize()[1]&0x1) ? 1 : 2,0);
128  } else {
129  pad=Point(0,0,(e.GetSize()[2]&0x1) ? 1 : 2);
130  }
131  return Extent(e.GetStart(),e.GetEnd()+pad);
132 #else
133  return Extent(e.GetStart(),e.GetEnd());
134 #endif
135  }
136 };
137 
138 }}} // ns
139 
140 #endif