SpatialOps
PointOperators.h
1 /*
2  * Copyright (c) 2014 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 PointOperators_h
24 #define PointOperators_h
25 
26 #include "PointFieldTypes.h"
27 
28 #include <vector>
29 
30 namespace SpatialOps{
31 namespace Point{
32 
39  template< typename FieldT >
41  {
42  typedef std::vector<size_t> Indices;
43  Indices indices_;
44 
45  public:
46  typedef PointField SrcFieldType;
47  typedef FieldT DestFieldType;
48 
49  PointToField( const FieldT& fieldLocations );
50  PointToField( const std::vector<size_t>& flatIndices );
51 
52  void apply_to_field( const SrcFieldType&, DestFieldType& ) const;
53  };
54 
61  template< typename FieldT >
63  {
64  typedef std::vector<size_t> Indices;
65  Indices indices_;
66  public:
67  typedef FieldT SrcFieldType;
68  typedef PointField DestFieldType;
69 
70  FieldToPoint( const FieldT& fieldLocations );
71  FieldToPoint( const std::vector<size_t>& flatIndices );
72 
73  void apply_to_field( const SrcFieldType&, DestFieldType& ) const;
74  };
75 
76 
77 
78  // =================================================================
79  //
80  // Implementation
81  //
82  // =================================================================
83 
84 
85 
86  //------------------------------------------------------------------
87 
88  template< typename FieldT >
89  PointToField<FieldT>::PointToField( const Indices& locs )
90  {
91  indices_ = locs;
92  }
93 
94  //------------------------------------------------------------------
95 
96  template< typename FieldT >
97  PointToField<FieldT>::PointToField( const FieldT& locs )
98  {
99  size_t i=0;
100  for( typename FieldT::const_iterator ii=locs.begin(); ii!=locs.end(); ++ii, ++i ){
101  if( *ii != 0.0 ) indices_.push_back(i);
102  }
103  }
104 
105  //------------------------------------------------------------------
106 
107  template< typename FieldT >
108  void
109  PointToField<FieldT>::apply_to_field( const SrcFieldType& src, DestFieldType& dest ) const
110  {
111  typename SrcFieldType::const_iterator isrc = src.begin();
112  const typename SrcFieldType::const_iterator isrce = src.end();
113 
114  size_t i=0;
115  for( ; isrc!=isrce; ++isrc, ++i ){
116  dest[ indices_[i] ] = *isrc;
117  }
118 
119  assert( i == indices_.size() );
120  }
121 
122  //==================================================================
123 
124  template< typename FieldT >
125  FieldToPoint<FieldT>::FieldToPoint( const Indices& locs )
126  {
127  indices_ = locs;
128  }
129 
130  //------------------------------------------------------------------
131 
132  template< typename FieldT >
133  FieldToPoint<FieldT>::FieldToPoint( const FieldT& locs )
134  {
135  size_t i=0;
136  for( typename FieldT::const_iterator ii=locs.begin(); ii!=locs.end(); ++ii, ++i ){
137  if( *ii != 0.0 ) indices_.push_back(i);
138  }
139  }
140 
141  //------------------------------------------------------------------
142 
143  template< typename FieldT >
144  void
145  FieldToPoint<FieldT>::apply_to_field( const SrcFieldType& src, DestFieldType& dest ) const
146  {
147  typename DestFieldType::iterator idest = dest.begin();
148  const typename DestFieldType::iterator ideste = dest.end();
149 
150  size_t i=0;
151  for( ; idest!=ideste; ++idest, ++i ){
152  *idest = src[ indices_[i] ];
153  }
154 
155  assert( i == indices_.size() );
156  }
157 
158  //------------------------------------------------------------------
159 
160 
161 } // namespace Point
162 } // namespace SpatialOps
163 
164 
165 #endif // PointOperators_h
Puts point field data into a parent field.
ConstFieldIterator< field_type > const_iterator
const iterator type
Definition: SpatialField.h:146
const_iterator end() const
return a constant iterator to end for CPU with valid ghost cells
Definition: SpatialField.h:492
const_iterator begin() const
return a constant iterator for CPU with valid ghost cells
Definition: SpatialField.h:478
provides iterator support for SpatialField. Only works for CPU.
Definition: MemoryWindow.h:334
Abstracts a field.
Definition: SpatialField.h:132