OpenVDB  0.104.0
LeafArray.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 
31 #ifndef OPENVDB_TREE_LEAF_ARRAY_HAS_BEEN_INCLUDED
32 #define OPENVDB_TREE_LEAF_ARRAY_HAS_BEEN_INCLUDED
33 
34 #warning This file is deprecated and should be replaced by tree/LeafManager.h
35 
36 #include <iostream>
37 #include <algorithm> // for std::swap
38 #include <cstring> // for std::memcpy()
39 #include <boost/shared_ptr.hpp>
40 #include <boost/static_assert.hpp>
41 #include <boost/bind.hpp>
42 #include <tbb/blocked_range.h>
43 #include <tbb/parallel_for.h>
44 #include <openvdb/tools/Morphology.h>
45 #include <openvdb/tree/LeafManager.h>
46 
47 class TestLeaf;
48 template<typename> class TestLeafIO;
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tree {
54 
62 template<typename TreeT, int BufferCount>
63 class LeafArray
64 {
65 public:
66  typedef boost::shared_ptr<LeafArray> Ptr;
67  typedef TreeT TreeType;
68  typedef typename TreeType::ValueType ValueType;
69  typedef typename TreeType::LeafNodeType LeafType;
70  typedef typename LeafType::Buffer BufferType;
71 
73  struct BufferStruct {
75  BufferStruct(LeafType& _leaf): leaf(&_leaf) {}
77  void swap(Index n) { assert(n < BufferCount); leaf->swap(buffer[n]); }
78  void copy(Index n) { assert(n < BufferCount); buffer[n]=leaf->buffer(); }
79  void sync() { for (Index n = 0; n < BufferCount; ++n) this->copy(n); }
80  LeafType* leaf;// pointer to leaf containing the internal read buffer
81  BufferType buffer[BufferCount];//static array of external write buffers
82  };
83 
84  typedef typename std::vector<BufferStruct> ArrayType;
85  typedef typename ArrayType::iterator IterType;
86  typedef typename tbb::blocked_range<IterType> IterRangeType;
87 
89  OPENVDB_DEPRECATED LeafArray(TreeType &tree, bool sync=true) : mTree(NULL)
90  {
91  this->init(tree, sync);
92  }
93  void init(TreeType &tree, bool sync)
94  {
95  mTree = &tree;
96  mArray.clear();
97  mArray.reserve(tree.leafCount());
98  for (typename TreeType::LeafIter i = tree.beginLeaf(); i; ++i) {
99  mArray.push_back( BufferStruct(*i) );
100  }
101  if (sync) this->syncBuffers();
102  }
103  void clear() { mArray.clear(); }
104  Index size() const { return mArray.size(); }
105  TreeType& tree() { return *mTree; }
106  LeafType* operator[](int i) { return mArray[i].leaf; }
107 
109  IterRangeType getRange(size_t grainsize=1)
110  {
111  return IterRangeType(mArray.begin(), mArray.end(), grainsize);
112  }
113 
120  static void swapBufferRange(const IterRangeType& r, Index n)
121  {
122  if (n < BufferCount) {
123  for (IterType i = r.begin(), e = r.end(); i != e; ++i) i->swap(n);
124  }
125  }
132  static void copyBufferRange(const IterRangeType& r, Index n)
133  {
134  if (n < BufferCount) {
135  for (IterType i = r.begin(), e = r.end(); i != e; ++i) i->copy(n);
136  }
137  }
143  static void syncBufferRange(const IterRangeType& r)
144  {
145  for (IterType i = r.begin(), e = r.end(); i != e; ++i) i->sync();
146  }
150  void swapBuffers(Index n = 0, bool serial = false)
151  {
152  if (n < BufferCount) {
153  if (serial) {
154  this->swapBufferRange(this->getRange(), n);
155  } else {
156  tbb::parallel_for(this->getRange(512), boost::bind(&swapBufferRange, _1, n));
157  }
158  }
159  }
163  void copyBuffers(Index n = 0, bool serial = false)
164  {
165  if (n < BufferCount) {
166  if (serial) {
167  this->copyBufferRange(this->getRange(), n);
168  } else {
169  tbb::parallel_for(this->getRange(8), boost::bind(&copyBufferRange, _1, n));
170  }
171  }
172  }
175  void syncBuffers(bool serial = false)
176  {
177  if (serial) {
178  this->syncBufferRange(this->getRange());
179  } else {
180  tbb::parallel_for(this->getRange(8), boost::bind(&syncBufferRange, _1));
181  }
182  }
183 
184 private:
185  ArrayType mArray;
186  TreeType* mTree;
187 };//end of LeafArray class
188 
191 template<typename TreeT>
192 class LeafArray<TreeT,0>
193 {
194 public:
195  typedef boost::shared_ptr<LeafArray> Ptr;
196  typedef TreeT TreeType;
197  typedef typename TreeType::ValueType ValueType;
198  typedef typename TreeType::LeafNodeType LeafType;
199  typedef typename LeafType::Buffer BufferType;
200 
202  struct BufferStruct {
204  BufferStruct(LeafType& _leaf): leaf(&_leaf) {}
205  LeafType* leaf;// pointer to leaf containing the internal read buffer
206  };
207 
208  typedef typename std::vector<BufferStruct> ArrayType;
209  typedef typename ArrayType::iterator IterType;
210  typedef typename tbb::blocked_range<IterType> IterRangeType;
211 
213  OPENVDB_DEPRECATED LeafArray(TreeType &tree) : mTree(NULL) { this->init(tree); }
214  void init(TreeType &tree)
215  {
216  mTree = &tree;
217  mArray.clear();
218  mArray.reserve(tree.leafCount());
219  for (typename TreeType::LeafIter i = tree.beginLeaf(); i; ++i) {
220  mArray.push_back( BufferStruct(*i) );
221  }
222  }
223  void clear() { mArray.clear(); }
224  Index size() const { return mArray.size(); }
225  TreeType& tree() { return *mTree; }
226  LeafType* operator[](int i) { return mArray[i].leaf; }
227 
229  IterRangeType getRange(size_t grainsize=1)
230  {
231  return IterRangeType(mArray.begin(), mArray.end(), grainsize);
232  }
233 
234 private:
235  ArrayType mArray;
236  TreeType* mTree;
237 };//end of specialized LeafArray class
238 
239 template<typename TreeType, int BufferCount>
240 inline void
242 {
243  tree::LeafManager<TreeType> manager(leafArray.tree());
244  dilateVoxels<TreeType>(manager);
245 }
246 
247 } // namespace tree
248 } // namespace OPENVDB_VERSION_NAME
249 } // namespace openvdb
250 
251 
252 #endif // OPENVDB_TREE_LEAF_ARRAY_HAS_BEEN_INCLUDED
253 
254 // Copyright (c) 2012 DreamWorks Animation LLC
255 // All rights reserved. This software is distributed under the
256 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )