VTK-m  2.0
PiecewiseLinearFunction.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 // Copyright (c) 2018, The Regents of the University of California, through
11 // Lawrence Berkeley National Laboratory (subject to receipt of any required approvals
12 // from the U.S. Dept. of Energy). All rights reserved.
13 //
14 // Redistribution and use in source and binary forms, with or without modification,
15 // are permitted provided that the following conditions are met:
16 //
17 // (1) Redistributions of source code must retain the above copyright notice, this
18 // list of conditions and the following disclaimer.
19 //
20 // (2) Redistributions in binary form must reproduce the above copyright notice,
21 // this list of conditions and the following disclaimer in the documentation
22 // and/or other materials provided with the distribution.
23 //
24 // (3) Neither the name of the University of California, Lawrence Berkeley National
25 // Laboratory, U.S. Dept. of Energy nor the names of its contributors may be
26 // used to endorse or promote products derived from this software without
27 // specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
38 // OF THE POSSIBILITY OF SUCH DAMAGE.
39 //
40 //=============================================================================
41 //
42 // This code is an extension of the algorithm presented in the paper:
43 // Parallel Peak Pruning for Scalable SMP Contour Tree Computation.
44 // Hamish Carr, Gunther Weber, Christopher Sewell, and James Ahrens.
45 // Proceedings of the IEEE Symposium on Large Data Analysis and Visualization
46 // (LDAV), October 2016, Baltimore, Maryland.
47 //
48 // The PPP2 algorithm and software were jointly developed by
49 // Hamish Carr (University of Leeds), Gunther H. Weber (LBNL), and
50 // Oliver Ruebel (LBNL)
51 //==============================================================================
52 
53 #ifndef vtk_m_worklet_contourtree_augmented_process_contourtree_inc_piecewise_linear_function_h
54 #define vtk_m_worklet_contourtree_augmented_process_contourtree_inc_piecewise_linear_function_h
55 
56 #include <vtkm/Pair.h>
57 #include <vtkm/cont/ArrayHandle.h>
59 
60 namespace vtkm
61 {
62 namespace worklet
63 {
64 namespace contourtree_augmented
65 {
66 namespace process_contourtree_inc
67 {
68 
69 
70 template <typename T>
72 { // PairComparator()
73  inline bool operator()(const std::pair<T, T>& s1, const std::pair<T, T>& s2) const
74  {
75  return s1.second > s2.second;
76  }
77 }; // PairComparator()
78 
79 
80 // TODO Need to change the samples to use VTKM data structures instead of std::vector
81 template <typename T>
83 {
84  std::vector<std::pair<T, T>> samples;
85 
86 public:
87  void addSample(T sx, T sy)
88  {
89  auto it = std::find_if_not(
90  samples.begin(), samples.end(), [sx](std::pair<T, T> s) { return sx > s.first; });
91  samples.insert(it, std::pair<T, T>(sx, sy));
92  }
93 
94  T operator()(T px) const
95  {
96  if (samples.size() < 2)
97  return 0;
98  else if (px < samples.front().first || px > samples.back().first)
99  return 0;
100  else
101  {
102  auto end = std::find_if_not(
103  samples.begin(), samples.end(), [px](std::pair<T, T> s) { return px > s.first; });
104  if (end == samples.begin())
105  {
106  std::cerr << "WARNING!" << std::endl;
107  return 0;
108  }
109  else
110  {
111  auto begin = end - 1;
112  T t = (px - begin->first) / (end->first - begin->first);
113  return (1 - t) * begin->second + t * end->second;
114  }
115  }
116  }
117 
119  {
120  std::vector<std::pair<T, T>> new_samples;
121  auto it1 = samples.begin();
122  auto it2 = other.samples.begin();
123 
124  while (it1 != samples.end() && it2 != other.samples.end())
125  {
126  if (it1->first < it2->first)
127  {
128  new_samples.emplace_back(it1->first, it1->second + other(it1->first));
129  ++it1;
130  }
131  else if (it2->first < it1->first)
132  {
133  new_samples.emplace_back(it2->first, it2->second + (*this)(it2->first));
134  ++it2;
135  }
136  else
137  {
138  new_samples.emplace_back(it1->first, it1->second + it2->second);
139  ++it1;
140  ++it2;
141  }
142  }
143  while (it1 != samples.end())
144  {
145  new_samples.push_back(*it1);
146  ++it1;
147  }
148  while (it2 != other.samples.end())
149  {
150  new_samples.push_back(*it2);
151  ++it2;
152  }
153 
154  samples.swap(new_samples);
155  return *this;
156  }
157 
158  std::vector<T> nLargest(unsigned n)
159  {
160  std::vector<std::pair<T, T>> sCopy = samples;
161  std::sort(sCopy.begin(), sCopy.end(), PairComparator<T>());
162  std::vector<T> res;
163  for (unsigned i = 0; i < n; ++i)
164  {
165  res.push_back(sCopy[i].first);
166  }
167  return res;
168  }
169 
170  void print()
171  {
172  for (auto sample : samples)
173  std::cout << "(" << sample.first << ", " << sample.second << ") ";
174  std::cout << std::endl;
175  }
176 };
177 
178 
179 
180 } // process_contourtree_inc
181 } // namespace contourtree_augmented
182 } // namespace worklet
183 } // namespace vtkm
184 
185 #endif
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::operator+=
PiecewiseLinearFunction & operator+=(const PiecewiseLinearFunction &other)
Definition: PiecewiseLinearFunction.h:118
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::print
void print()
Definition: PiecewiseLinearFunction.h:170
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PairComparator
Definition: PiecewiseLinearFunction.h:71
ArrayHandle.h
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::samples
std::vector< std::pair< T, T > > samples
Definition: PiecewiseLinearFunction.h:84
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Pair.h
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction
Definition: PiecewiseLinearFunction.h:82
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PairComparator::operator()
bool operator()(const std::pair< T, T > &s1, const std::pair< T, T > &s2) const
Definition: PiecewiseLinearFunction.h:73
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::addSample
void addSample(T sx, T sy)
Definition: PiecewiseLinearFunction.h:87
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::nLargest
std::vector< T > nLargest(unsigned n)
Definition: PiecewiseLinearFunction.h:158
vtkm::worklet::contourtree_augmented::process_contourtree_inc::PiecewiseLinearFunction::operator()
T operator()(T px) const
Definition: PiecewiseLinearFunction.h:94
ContourTree.h