SpatialOps
SpatialMask.h
1 /*
2  * Copyright (c) 2014-2017 The University of Utah
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #ifndef SpatialOps_SpatialMask_h
24 #define SpatialOps_SpatialMask_h
25 
26 #include <vector>
27 #include <cassert>
28 
29 #include <spatialops/SpatialOpsConfigure.h>
30 
31 #include <spatialops/structured/BitField.h>
33 
34 namespace SpatialOps{
35 
69  template<typename FieldType>
71  {
72  public:
73 
74  typedef FieldType field_type;
78 
79  private:
80 
81  MemoryWindow maskWindow_;
82  MemoryWindow interiorMaskWindow_;
83 
84  const BoundaryCellInfo bcInfo_;
85  const GhostData ghosts_;
86  GhostData validGhosts_;
87 
88  boost::shared_ptr< std::vector<IntVec> > points_;
89  BitField bitField_;
90 
91  public:
92 
101  SpatialMask( const MemoryWindow & window,
102  const BoundaryCellInfo & bc,
103  const GhostData & ghosts,
104  const std::vector<IntVec> & points )
105  : maskWindow_( window ),
106  interiorMaskWindow_(MemoryWindow(window.glob_dim(),
107  window.offset() + ghosts.get_minus(),
108  window.extent() - ghosts.get_minus() - ghosts.get_plus())),
109  bcInfo_ ( bc ),
110  ghosts_ ( ghosts ),
111  validGhosts_( ghosts ),
112  points_ ( new std::vector<IntVec>(points) ),
113  bitField_ ( *points_, interiorMaskWindow_, validGhosts_ )
114  {};
115 
121  SpatialMask( const FieldType & prototype,
122  const std::vector<IntVec> & points )
123  : maskWindow_ ( prototype.window_with_ghost() ),
124  interiorMaskWindow_( prototype.window_without_ghost() ),
125  bcInfo_ ( prototype.boundary_info() ),
126  ghosts_ ( prototype.get_ghost_data() ),
127  validGhosts_ ( prototype.get_valid_ghost_data() ),
128  points_ ( new std::vector<IntVec>(points) ),
129  bitField_( *points_, interiorMaskWindow_, validGhosts_ )
130  {};
131 
136  SpatialMask( const SpatialMask& other )
137  : maskWindow_ ( other.maskWindow_ ),
138  interiorMaskWindow_( other.interiorMaskWindow_ ),
139  bcInfo_ ( other.bcInfo_ ),
140  ghosts_ ( other.ghosts_ ),
141  validGhosts_ (other.validGhosts_ ),
142  points_ ( other.points_ ),
143  bitField_ ( other.bitField_ )
144  {};
145 
149  SpatialMask(const MemoryWindow& window,
150  const SpatialMask& other)
151  : maskWindow_ ( window ),
152  interiorMaskWindow_( other.interiorMaskWindow_ ), // This should not be used!
153  bcInfo_ ( other.bcInfo_.limit_by_extent(window.extent()) ),
154  ghosts_ ( other.ghosts_.limit_by_extent(window.extent()) ),
155  validGhosts_ ( other.ghosts_.limit_by_extent(window.extent()) ),
156  points_ ( other.points_ ),
157  bitField_ ( other.bitField_ )
158  {
159  // ensure that we are doing sane operations with the new window:
160 # ifndef NDEBUG
161  assert( window.sanity_check() );
162 
163  const MemoryWindow& pWindow = other.window_with_ghost();
164  for( size_t i=0; i<3; ++i ){
165  assert( window.extent(i) + window.offset(i) <= pWindow.glob_dim(i) );
166  assert( window.offset(i) < pWindow.glob_dim(i) );
167  }
168 # endif
169  };
170 
171  template<typename PrototypeType>
173  static inline build( const PrototypeType & prototype,
174  const std::vector<IntVec> & points ){
175  return SpatialMask( create_new_memory_window<FieldType, PrototypeType>(prototype),
176  create_new_boundary_cell_info<FieldType, PrototypeType>(prototype),
177  prototype.get_valid_ghost_data(),
178  points );
179  }
180 
181  ~SpatialMask() {};
182 
187  inline const std::vector<IntVec> & points(void) const{
188  return *points_;
189  };
190 
196  inline bool operator()(const size_t i, const size_t j, const size_t k) const{
197  return operator()(IntVec(i,j,k));
198  };
199 
205  inline bool operator()(const IntVec& ijk) const { return bitField_(ijk); };
206 
211  inline const_iterator begin() const { return bitField_.begin(maskWindow_); };
212 
213  inline const_iterator end() const { return bitField_.end(maskWindow_); };
214 
215  inline const_iterator interior_begin() const{
216  return bitField_.begin(interiorMaskWindow_);
217  };
218 
219  inline const_iterator interior_end() const{
220  return bitField_.end(interiorMaskWindow_);
221  };
222 
223  inline void add_consumer(const short int consumerDeviceIndex){
224  bitField_.add_consumer(consumerDeviceIndex);
225  };
226 
227  inline bool find_consumer(const short int consumerDeviceIndex) const{
228  return bitField_.find_consumer(consumerDeviceIndex);
229  };
230 
231  inline bool has_consumers() { return bitField_.has_consumers(); };
232 
233  inline const BoundaryCellInfo& boundary_info() const{ return bcInfo_; };
234 
235  inline const MemoryWindow& window_without_ghost() const { return interiorMaskWindow_; };
236 
237  inline const MemoryWindow& window_with_ghost() const { return maskWindow_; };
238 
239  inline short int active_device_index() const { return bitField_.active_device_index(); };
240 
241  inline const unsigned int * mask_values(const short int consumerDeviceIndex = 0) const{
242  return bitField_.mask_values(consumerDeviceIndex);
243  };
244 
245  inline const GhostData& get_ghost_data() const{ return ghosts_; };
246 
247  inline const GhostData& get_valid_ghost_data() const{ return validGhosts_; };
248 
259  inline mask_type reshape( const IntVec& extentModify,
260  const IntVec& shift ) const
261  {
262  MemoryWindow w( maskWindow_.glob_dim(),
263  maskWindow_.offset() + shift,
264  maskWindow_.extent() + extentModify );
265  return mask_type( w, *this );
266  };
267  };
268 
269 } // namespace SpatialOps
270 
271 #endif // SpatialOps_SpatialMask_h
provides a template lightweight class for storing 3d vectors of numbers.
Definition: Numeric3Vec.h:45
Provides tools to index into a sub-block of memory.
Definition: MemoryWindow.h:63
mask_type reshape(const IntVec &extentModify, const IntVec &shift) const
Obtain a child field that is reshaped.
Definition: SpatialMask.h:259
const std::vector< IntVec > & points(void) const
return reference to list of points in given list NOTE: Not supported for external field types ...
Definition: SpatialMask.h:187
SpatialMask(const FieldType &prototype, const std::vector< IntVec > &points)
Construct a SpatialMask.
Definition: SpatialMask.h:121
SpatialMask(const MemoryWindow &window, const BoundaryCellInfo &bc, const GhostData &ghosts, const std::vector< IntVec > &points)
Construct a SpatialMask.
Definition: SpatialMask.h:101
STL namespace.
bool sanity_check() const
performs basic sanity checks to see if there is anything obviously wrong with this window...
Implements a mask as a bitfield.
Definition: BitField.h:246
const_iterator begin(const MemoryWindow &window) const
Iterator constructs for traversing memory windows. Note: Iteration is not directly supported for exte...
Definition: BitField.h:438
SpatialMask(const MemoryWindow &window, const SpatialMask &other)
Shallow copy constructor with new window.
Definition: SpatialMask.h:149
Abstracts a mask.
Definition: SpatialMask.h:70
bool operator()(const IntVec &ijk) const
Given an index in this mask, return whether or not index is a mask point. WARNING: slow! NOTE: Not su...
Definition: SpatialMask.h:205
const_iterator begin() const
Iterator constructs for traversing memory windows. Note: Iteration is not directly supported for exte...
Definition: SpatialMask.h:211
Holds information about the number of ghost cells on each side of the domain.
Definition: GhostData.h:54
SpatialMask(const SpatialMask &other)
Shallow copy constructor. This results in two fields that share the same underlying memory...
Definition: SpatialMask.h:136
bool operator()(const size_t i, const size_t j, const size_t k) const
Given an index in this mask, return whether or not index is a mask point. WARNING: slow! NOTE: Not su...
Definition: SpatialMask.h:196
Provides information about boundary cells for various fields.