static label Time::findClosestTimeIndex(const instantList&, const scalar)

- avoid code duplication in vtkPV3Foam.C and checkTimeOption.H
  - can also be used in Time::findClosestTime(), but didn't touch that
This commit is contained in:
Mark Olesen
2008-07-25 15:37:38 +02:00
parent 8a2596a05c
commit 3cdc8ca03c
6 changed files with 54 additions and 62 deletions

View File

@ -418,19 +418,55 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const
return times[times.size() - 1];
}
label nearestIndex = -1;
scalar deltaT = GREAT;
label closesti = 0;
for (label i=1; i<times.size(); i++)
{
if (mag(times[i].value() - t) < deltaT)
scalar diff = mag(times[i].value() - t);
if (diff < deltaT)
{
deltaT = mag(times[i].value() - t);
closesti = i;
deltaT = diff;
nearestIndex = i;
}
}
return times[closesti];
return times[nearestIndex];
}
//
// This should work too,
// if we don't worry about checking "constant" explicitly
//
// Foam::instant Foam::Time::findClosestTime(const scalar t) const
// {
// instantList times = Time::findTimes(path());
// label timeIndex = min(findClosestTimeIndex(times, t), 0);
// return times[timeIndex];
// }
Foam::label Foam::Time::findClosestTimeIndex
(
const instantList& times,
const scalar t
)
{
label nearestIndex = -1;
scalar deltaT = GREAT;
forAll (times, i)
{
if (times[i].name() == "constant") continue;
scalar diff = fabs(times[i].value() - t);
if (diff < deltaT)
{
deltaT = diff;
nearestIndex = i;
}
}
return nearestIndex;
}