[opengtl-commits] [226] add lookup1DCubic, and interpolate1D functions |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/opengtl-commits Archives
]
Revision: 226
Author: cyrille
Date: 2008-06-24 21:34:34 +0200 (Tue, 24 Jun 2008)
Log Message:
-----------
add lookup1DCubic, and interpolate1D functions
Modified Paths:
--------------
trunk/OpenGTL/OpenCTL/CtlStdLib/ctlstdlib.ctl
trunk/OpenGTL/OpenCTL/tests/stdlib/CMakeLists.txt
trunk/OpenGTL/OpenCTL/tests/stdlib/lookup.ctl
Added Paths:
-----------
trunk/OpenGTL/OpenCTL/tests/stdlib/interpolate.ctl
Modified: trunk/OpenGTL/OpenCTL/CtlStdLib/ctlstdlib.ctl
===================================================================
--- trunk/OpenGTL/OpenCTL/CtlStdLib/ctlstdlib.ctl 2008-06-24 19:32:47 UTC (rev 225)
+++ trunk/OpenGTL/OpenCTL/CtlStdLib/ctlstdlib.ctl 2008-06-24 19:34:34 UTC (rev 226)
@@ -69,3 +69,90 @@
float s = t - i;
return table[i] * ( 1 - s ) + table[i+1] * s;
}
+
+float lookupCubic1D (float table[], float pMin, float pMax, float p)
+{
+ if( p < pMin ) return table[ 0 ];
+ if( p > pMax ) return table[ table.size - 1 ];
+
+ float t = (p - pMin) / (pMax - pMin) * (table.size-1);
+ int i = floor (t);
+ float s = t - i;
+ float m0;
+ float m1;
+ if( i > 0 )
+ {
+ m0 = (table[i+1] - table[i-1]) / 2;
+ }
+ if( i < table.size-2 )
+ {
+ m1 = (table[i+2] - table[i]) / 2;
+ }
+ if( i == 0) {
+ m0 = (3 * table[i+1] - table[i] - m1);
+ }
+ if( i == table.size-2 )
+ {
+ m1 = (3 * table[i+1] - table[i] - m0);
+ }
+ return table[i] * (2 * s*s*s - 3 * s*s + 1) + m0 * (s*s*s - 2 * s*s + s) + table[i+1] * (-2 * s*s*s + 3 * s*s) + m1 * (s*s*s - s*s);
+}
+
+float interpolate1D (float table[][2], float p)
+{
+ if( p < table[0][0] ) return table[0][1];
+ if( p > table[table.size-1][0] ) return table[table.size-1][1];
+
+ for( int i = 0; i < table.size - 1; ++i )
+ {
+ if( table[i][0] <= p && p < table[i+1][0] )
+ {
+ float s = (p - table[i][0]) / (table[i+1][0] - table[i][0]);
+ return table[i][1] * ( 1 - s ) + table[i+1][1] * s;
+ }
+ }
+ return 0.0;
+}
+
+float interpolateCubic1D (float table[][2], float p)
+{
+ if( p < table[0][0] ) return table[0][1];
+ if( p > table[table.size-1][0] ) return table[table.size-1][1];
+
+ for( int i = 0; i < table.size - 1; ++i )
+ {
+ if( table[i][0] <= p && p < table[i+1][0] )
+ {
+ float s = (p - table[i][0]) / (table[i+1][0] - table[i][0]);
+ float dx0 = (table[i][0] - table[i-1][0]);
+ float dx1 = (table[i+1][0] - table[i][0]);
+ float dx2 = (table[i+2][0] - table[i+1][0]);
+ float dy0 = (table[i][1] - table[i-1][1]);
+ float dy1 = (table[i+1][1] - table[i][1]);
+ float dy2 = (table[i+2][1] - table[i+1][1]);
+
+ float m0;
+ float m1;
+ if( i > 0 )
+ {
+ m0 = (dy1 + dx1 * dy0 / dx0) / 2;
+ }
+ if( i < table.size-2 )
+ {
+ m1 = (dy1 + dx1 * dy2 / dx2) / 2;
+ }
+ if( i == 0) {
+ m0 = (3 * dy1 - m1) / 2;
+ }
+ if( i == table.size-2 )
+ {
+ m1 = (3 * dy1 - m0) / 2;
+ }
+ return table[i][1] * (2 * s*s*s - 3 * s*s + 1) +
+ m0 * (s*s*s - 2 * s*s + s) +
+ table[i+1][1] * (-2 * s*s*s + 3 * s*s) +
+ m1 * (s*s*s - s*s);
+ }
+ }
+ return 0.0;
+}
Modified: trunk/OpenGTL/OpenCTL/tests/stdlib/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenCTL/tests/stdlib/CMakeLists.txt 2008-06-24 19:32:47 UTC (rev 225)
+++ trunk/OpenGTL/OpenCTL/tests/stdlib/CMakeLists.txt 2008-06-24 19:34:34 UTC (rev 226)
@@ -7,6 +7,7 @@
floatfunctions.ctl
powers.ctl
lookup.ctl
+ interpolate.ctl
)
FOREACH( TEST_FILE ${TESTS_FILES} )
Added: trunk/OpenGTL/OpenCTL/tests/stdlib/interpolate.ctl
===================================================================
--- trunk/OpenGTL/OpenCTL/tests/stdlib/interpolate.ctl (rev 0)
+++ trunk/OpenGTL/OpenCTL/tests/stdlib/interpolate.ctl 2008-06-24 19:34:34 UTC (rev 226)
@@ -0,0 +1,19 @@
+import "TestFramework";
+
+int main()
+{
+ const float table[][] = { { 0.0, 1.0}, { 0.5, 4.0 }, {1.0, 5.0} };
+
+ int errorcount = 0;
+ Test::checkNearEqual( interpolate1D( table,-1.0 ), 1.0, errorcount);
+ Test::checkNearEqual( interpolate1D( table, 2.0 ), 5.0, errorcount);
+ Test::checkNearEqual( interpolate1D( table, 0.5 ), 4.0, errorcount);
+ Test::checkNearEqual( interpolate1D( table, 0.75 ), 4.5, errorcount);
+ Test::checkNearEqual( interpolate1D( table, 0.25 ), 2.5, errorcount);
+ Test::checkNearEqual( interpolateCubic1D( table,-1.0 ), 1.0, errorcount);
+ Test::checkNearEqual( interpolateCubic1D( table, 2.0 ), 5.0, errorcount);
+ Test::checkNearEqual( interpolateCubic1D( table, 0.5 ), 4.0, errorcount);
+
+
+ return errorcount;
+}
Modified: trunk/OpenGTL/OpenCTL/tests/stdlib/lookup.ctl
===================================================================
--- trunk/OpenGTL/OpenCTL/tests/stdlib/lookup.ctl 2008-06-24 19:32:47 UTC (rev 225)
+++ trunk/OpenGTL/OpenCTL/tests/stdlib/lookup.ctl 2008-06-24 19:34:34 UTC (rev 226)
@@ -7,7 +7,11 @@
Test::checkNearEqual( lookup1D( table, 0.0, 1.0, -1.0 ), 1.0, errorcount);
Test::checkNearEqual( lookup1D( table, 0.0, 1.0, 2.0 ), 5.0, errorcount);
Test::checkNearEqual( lookup1D( table, 0.0, 1.0, 0.5 ), 4.0, errorcount);
+ Test::checkNearEqual( lookup1D( table, 0.0, 1.0, 0.75 ), 4.5, errorcount);
+ Test::checkNearEqual( lookup1D( table, 0.0, 1.0, 0.25 ), 2.5, errorcount);
+ Test::checkNearEqual( lookupCubic1D( table, 0.0, 1.0, -1.0 ), 1.0, errorcount);
+ Test::checkNearEqual( lookupCubic1D( table, 0.0, 1.0, 2.0 ), 5.0, errorcount);
+ Test::checkNearEqual( lookupCubic1D( table, 0.0, 1.0, 0.5 ), 4.0, errorcount);
-
return errorcount;
}