r642: no message
[ctsim.git] / include / interpolator.h
index cea4961809cda3300db2c8b227d7639c3d1d734e..d807ff805f08748c94c7610784cbd50de0f7f3ab 100644 (file)
@@ -2,7 +2,7 @@
 **  This is part of the CTSim program
 **  Copyright (c) 1983-2001 Kevin Rosenberg
 **
-**  $Id: interpolator.h,v 1.2 2001/03/21 21:45:31 kevin Exp $
+**  $Id: interpolator.h,v 1.3 2001/03/22 02:30:00 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -91,17 +91,18 @@ public:
 template<class T>
 class LinearInterpolator {
 private:
-  T* const m_pY;
   T* const m_pX;
+  T* const m_pY;
   const unsigned int m_n;
+  const bool m_bZeroOutsideRange;
 
 public:
-  LinearInterpolator (T* pY, unsigned int n)
-  : m_pY(pY), m_pX(0), m_n(n)
+  LinearInterpolator (T* pY, unsigned int n, bool bZeroOutside = true)
+  : m_pX(0), m_pY(pY), m_n(n), m_bZeroOutsideRange(bZeroOutside)
   {}
   
-  LinearInterpolator (T* pY, T* pX, unsigned int n)
-  : m_pY(pY), m_pX(pX), m_n(n)
+  LinearInterpolator (T* pX, T* pY, unsigned int n, bool bZeroOutside = true)
+  : m_pX(pX), m_pY(pY), m_n(n), m_bZeroOutsideRange(bZeroOutside)
   {}
   
   double interpolate (double dX, int* piLastFloor = NULL)
@@ -111,13 +112,19 @@ public:
     if (! m_pX) {
       if (dX == 0)
         result = m_pY[0];
-      else if (dX < 0)
-        result = 0;
-      else if (dX == m_n - 1)
+      else if (dX < 0) {
+        if (m_bZeroOutsideRange)
+          result = 0;
+        else
+          result = m_pY[0];
+      } else if (dX == m_n - 1)
         result = m_pY[m_n-1];
-      else if (dX > m_n - 1)
-        result = 0;
-      else {
+      else if (dX > m_n - 1) {
+        if (m_bZeroOutsideRange)
+          result = 0;
+        else
+          result = m_pY[m_n - 1];
+      } else {
        int iFloor = floor(dX);
        result = m_pY[iFloor] + (m_pY[iFloor+1] - m_pY[iFloor]) * (dX - iFloor);
       }
@@ -136,13 +143,19 @@ public:
       }
       if (dX == m_pX[0])
         result = m_pY[0];
-      else if (dX < m_pX[0])
-        result = 0;
-      else if (dX == m_pX[m_n-1])
+      else if (dX < m_pX[0]) {
+        if (m_bZeroOutsideRange)
+          result = 0;
+        else
+          result = m_pY[0];
+      } else if (dX == m_pX[m_n-1])
         result = m_pY[m_n-1];
-      else if (dX > m_pX[m_n-1])
-        result = 0;
-      else {
+      else if (dX > m_pX[m_n - 1]) {
+        if (m_bZeroOutsideRange)
+          result = 0;
+        else
+          result = m_pY[m_n - 1];
+      } else {
         if (iLower < 0 || iLower >= m_n) {
           sys_error (ERR_SEVERE, "Coordinate out of range [linearInterpolate]");
           return 0;