SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearPWI.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 LinearPWI
32 ///@brief Linear piecewise interpolation templates
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_LINEARPWI_H__
44 #define SCI_LINEARPWI_H__
45 
47 
48 #include <Core/Containers/Array1.h>
50 
51 namespace SCIRun {
52 
53 
54 typedef struct Pair{
55  double a;
56  double b;
57 } PAIR;
58 
59 
60 class LinearPWI: public PiecewiseInterp<double> {
61 public:
62  LinearPWI();
63  LinearPWI(const Array1<double>&, const Array1<double>&);
64 
65  bool set_data(const Array1<double>&, const Array1<double>&);
66  inline bool get_value(double, double&);
67 
68 private:
69  Array1<PAIR> p;
70 };
71 
72 inline bool LinearPWI::get_value(double w, double& res){
73  int interv;
74  if (data_valid && (interv=get_interval(w))>=0){
75  res=p[interv].a+p[interv].b*w;
76  return true;
77  }
78  else
79  return false;
80 }
81 
82 template <class T> class Linear3DPWI: public PiecewiseInterp<T> {
83 public:
85  Linear3DPWI(const Array1<double>&, const Array1<T>&);
86 
87  bool set_data(const Array1<double>&, const Array1<T>&);
88  inline bool get_value(double, T&);
89 
90 private:
91  Array1<PAIR> X;
92  Array1<PAIR> Y;
93  Array1<PAIR> Z;
94 };
95 
96 
97 template <class T> Linear3DPWI<T>::Linear3DPWI(const Array1<double>& pts, const Array1<T>& vals)
98 {
99  set_data(pts, vals);
100 }
101 
102 template <class T> inline bool Linear3DPWI<T>::get_value(double w, T& res)
103 {
104  int i = 0;
105  if (this->data_valid && (i = this->get_interval(w)) >= 0) {
106  res = T(X[i].a + X[i].b * w, Y[i].a+Y[i].b * w, Z[i].a + Z[i].b * w);
107  return true;
108  }
109  else
110  return false;
111 }
112 
113 // takes sorted array of points
114 template <class T> bool Linear3DPWI<T>::set_data(const Array1<double>& pts, const Array1<T>& vals)
115 {
116  int sz = 0;
117  this->reset();
118  if (this->fill_data(pts) && (sz = this->points.size()) > 1) {
119  X.resize(sz);
120  Y.resize(sz);
121  Z.resize(sz);
122 
123  double lb, rb, delta;
124 
125  for (int i=0; i<this->points.size()-1; i++){
126  lb = this->points[i];
127  rb = this->points[i+1];
128  delta = rb - lb;
129  X[i].a = (vals[i].x() * rb-vals[i+1].x() * lb) / delta;
130  X[i].b = (vals[i+1].x() - vals[i].x()) / delta;
131  Y[i].a = (vals[i].y() * rb-vals[i+1].y() * lb) / delta;
132  Y[i].b = (vals[i+1].y() - vals[i].y()) / delta;
133  Z[i].a = (vals[i].z() * rb-vals[i+1].z() * lb) / delta;
134  Z[i].b = (vals[i+1].z() - vals[i].z()) / delta;
135  }
136  return this->data_valid = true;
137  }
138  else{
139  this->reset();
140  return this->data_valid = false;
141  }
142 }
143 
144 } // End namespace SCIRun
145 
146 #endif //SCI_LINEARPWI_H__
147 
148 
149 
150 
151 
bool get_value(double, T &)
Definition: LinearPWI.h:102
Interface to dynamic 1D array class.
Definition: LinearPWI.h:60
double b
Definition: LinearPWI.h:56
bool set_data(const Array1< double > &, const Array1< T > &)
Definition: LinearPWI.h:114
Definition: PiecewiseInterp.h:60
bool get_value(double, double &)
Definition: LinearPWI.h:72
Linear3DPWI()
Definition: LinearPWI.h:84
Definition: LinearPWI.h:54
LinearPWI()
Definition: LinearPWI.cc:46
bool set_data(const Array1< double > &, const Array1< double > &)
Definition: LinearPWI.cc:56
struct SCIRun::Pair PAIR
bool data_valid
Definition: PiecewiseInterp.h:62
double a
Definition: LinearPWI.h:55
Definition: LinearPWI.h:82