SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PiecewiseInterp.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 ///
31 ///@class PiecewiseInterp
32 ///@brief Base class for local family of interpolation techniques
33 ///
34 ///@author
35 /// Alexei Samsonov
36 /// Department of Computer Science
37 /// University of Utah
38 ///
39 ///@date July 2000
40 ///
41 
42 
43 #ifndef SCI_PIECEWISEINTERP_H__
44 #define SCI_PIECEWISEINTERP_H__
45 
46 #include <Core/Math/MiscMath.h>
47 #include <Core/Containers/Array1.h>
48 #include <sci_debug.h>
49 
50 #include <iostream>
51 
52 #if DEBUG
53 # define msg(m) std::cout << m << std::endl;
54 #else
55 # define msg(m)
56 #endif
57 
58 namespace SCIRun {
59 
60 template <class T> class PiecewiseInterp {
61 protected:
62  bool data_valid; // set to true in an interpolation specific cases
63  int curr_intrv; // number of last interval used
64  double min_bnd; // and its boundaries (for camera path optimizations)
65  double max_bnd;
66  Array1<double> points; // sorted parameter points
67  inline bool fill_data(const Array1<double>&);
68 
69 public:
71  virtual ~PiecewiseInterp() {}
72  inline int get_interval(double);
73 
74  virtual bool get_value(double, T&) = 0;
75  virtual bool set_data(const Array1<double>& pts, const Array1<T>& vals) = 0;
76 
77  void reset() {
78  data_valid = false;
79  curr_intrv = -1;
80  min_bnd = max_bnd = 0;
81  }
82 };
83 
84 // returns -1 if data is not valid or the value is out of boundaries
85 template <class T> inline int PiecewiseInterp<T>::get_interval(double w)
86 {
87  if (data_valid) {
88  if (w < min_bnd || w >= max_bnd) { // taking advantage of smooth parameter changing
89  int lbnd = 0, rbnd = points.size()-1, delta = 0;
90 
91  if (w>=points[lbnd] && w<=points[rbnd]) {
92 
93  if (curr_intrv >= 0)
94  if (w < min_bnd) { // the series of optimizations that will take advantage
95  // on monotonic parameter changing (camera path interp.)
96  if (w >= points[curr_intrv - 1])
97  lbnd=curr_intrv-1;
98  rbnd=curr_intrv;
99 
100  } else {
101  if (w <= points[curr_intrv+1]) {
102  rbnd = curr_intrv+1;
103  }
104  lbnd = curr_intrv;
105  }
106 
107  while ((delta = rbnd - lbnd) != 1){
108  if (w < points[lbnd + delta/2]) {
109  rbnd = lbnd+delta/2;
110  }
111  else {
112  lbnd += delta/2;
113  }
114  }
115 
116  curr_intrv = lbnd;
117  min_bnd = points[curr_intrv];
118  max_bnd = points[curr_intrv+1];
119  }
120  else
121  {
122  curr_intrv = (w < points[lbnd]) ? lbnd : rbnd;
123  min_bnd = 0;
124  max_bnd = 0;
125  }
126  }
127  }
128  else
129  reset();
130 
131  return curr_intrv;
132 }
133 
134 template<class T> inline bool PiecewiseInterp<T>::fill_data(const Array1<double>& pts){
135  for (int i = 1; i < pts.size(); i++) {
136  if ((pts[i] - pts[i-1]) < 1e-7) {
137  return false;
138  }
139  }
140 
141  points.resize(pts.size());
142  points = pts;
143  msg ("Filled!!!");
144  return true;
145 }
146 
147 } // End namespace SCIRun
148 
149 #endif //SCI_INTERPOLATION_H__
150 
151 
152 
153 
#define msg(m)
Definition: PiecewiseInterp.h:55
virtual ~PiecewiseInterp()
Definition: PiecewiseInterp.h:71
int get_interval(double)
Definition: PiecewiseInterp.h:85
Interface to dynamic 1D array class.
int curr_intrv
Definition: PiecewiseInterp.h:63
double min_bnd
Definition: PiecewiseInterp.h:64
void reset()
Definition: PiecewiseInterp.h:77
Definition: PiecewiseInterp.h:60
PiecewiseInterp()
Definition: PiecewiseInterp.h:70
virtual bool get_value(double, T &)=0
virtual bool set_data(const Array1< double > &pts, const Array1< T > &vals)=0
bool fill_data(const Array1< double > &)
Definition: PiecewiseInterp.h:134
Array1< double > points
Definition: PiecewiseInterp.h:66
double max_bnd
Definition: PiecewiseInterp.h:65
bool data_valid
Definition: PiecewiseInterp.h:62