| 1285 | } |
| 1286 | |
| 1287 | double Envelope::IntegralOfInverse( double t0, double t1 ) const |
| 1288 | { |
| 1289 | if(t0 == t1) |
| 1290 | return 0.0; |
| 1291 | if(t0 > t1) |
| 1292 | { |
| 1293 | return -IntegralOfInverse(t1, t0); // this makes more sense than returning the default value |
| 1294 | } |
| 1295 | |
| 1296 | unsigned int count = mEnv.size(); |
| 1297 | if(count == 0) // 'empty' envelope |
| 1298 | return (t1 - t0) / mDefaultValue; |
| 1299 | |
| 1300 | t0 -= mOffset; |
| 1301 | t1 -= mOffset; |
| 1302 | |
| 1303 | double total = 0.0, lastT, lastVal; |
| 1304 | unsigned int i; // this is the next point to check |
| 1305 | if(t0 < mEnv[0].GetT()) // t0 preceding the first point |
| 1306 | { |
| 1307 | if(t1 <= mEnv[0].GetT()) |
| 1308 | return (t1 - t0) / mEnv[0].GetVal(); |
| 1309 | i = 1; |
| 1310 | lastT = mEnv[0].GetT(); |
| 1311 | lastVal = mEnv[0].GetVal(); |
| 1312 | total += (lastT - t0) / lastVal; |
| 1313 | } |
| 1314 | else if(t0 >= mEnv[count - 1].GetT()) // t0 at or following the last point |
| 1315 | { |
| 1316 | return (t1 - t0) / mEnv[count - 1].GetVal(); |
| 1317 | } |
| 1318 | else // t0 enclosed by points |
| 1319 | { |
| 1320 | // Skip any points that come before t0 using binary search |
| 1321 | int lo, hi; |
| 1322 | BinarySearchForTime(lo, hi, t0); |
| 1323 | lastVal = InterpolatePoints(mEnv[lo].GetVal(), mEnv[hi].GetVal(), (t0 - mEnv[lo].GetT()) / (mEnv[hi].GetT() - mEnv[lo].GetT()), mDB); |
| 1324 | lastT = t0; |
| 1325 | i = hi; // the point immediately after t0. |
| 1326 | } |
| 1327 | |
| 1328 | // loop through the rest of the envelope points until we get to t1 |
| 1329 | while (1) |
| 1330 | { |
| 1331 | if(i >= count) // the requested range extends beyond the last point |
| 1332 | { |
| 1333 | return total + (t1 - lastT) / lastVal; |
| 1334 | } |
| 1335 | else if(mEnv[i].GetT() >= t1) // this point follows the end of the range |
| 1336 | { |
| 1337 | double thisVal = InterpolatePoints(mEnv[i - 1].GetVal(), mEnv[i].GetVal(), (t1 - mEnv[i - 1].GetT()) / (mEnv[i].GetT() - mEnv[i - 1].GetT()), mDB); |
| 1338 | return total + IntegrateInverseInterpolated(lastVal, thisVal, t1 - lastT, mDB); |
| 1339 | } |
| 1340 | else // this point precedes the end of the range |
| 1341 | { |
| 1342 | total += IntegrateInverseInterpolated(lastVal, mEnv[i].GetVal(), mEnv[i].GetT() - lastT, mDB); |
| 1343 | lastT = mEnv[i].GetT(); |
| 1344 | lastVal = mEnv[i].GetVal(); |
no test coverage detected