Skip to content

Commit ecb0bf5

Browse files
committed
Add searchBinary() method to ControlLinear
1 parent 474f552 commit ecb0bf5

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

OpenSim/Simulation/Control/ControlLinear.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ getParameterList(double aT,Array<int> &rList)
286286

287287
// FIND THE NODE
288288
_searchNode.setTime(aT);
289-
int i = _xNodes.searchBinary(_searchNode);
289+
int i = searchBinary(_xNodes, _searchNode);
290290

291291
// LESS THAN TIME OF FIRST NODE
292292
if(i<0) {
@@ -327,7 +327,7 @@ getParameterList(double aTLower,double aTUpper,Array<int> &rList)
327327

328328
// LOWER NODE
329329
_searchNode.setTime(aTLower);
330-
int iL = _xNodes.searchBinary(_searchNode);
330+
int iL = searchBinary(_xNodes, _searchNode);
331331
if(iL==-1) {
332332
iL += 1;
333333
} else if(iL==(size-1)) {
@@ -340,7 +340,7 @@ getParameterList(double aTLower,double aTUpper,Array<int> &rList)
340340

341341
// UPPER NODE
342342
_searchNode.setTime(aTUpper);
343-
int iU = _xNodes.searchBinary(_searchNode);
343+
int iU = searchBinary(_xNodes, _searchNode);
344344
if(iU==-1) {
345345
return(0);
346346
} else if( (*_xNodes.get(iU)).isLessThan(_searchNode) ) {
@@ -380,7 +380,7 @@ void ControlLinear::
380380
setControlValue(ArrayPtrs<ControlLinearNode> &aNodes,double aT,double aValue)
381381
{
382382
ControlLinearNode node(aT,aValue);
383-
int lower = aNodes.searchBinary(node);
383+
int lower = searchBinary(aNodes, node);
384384

385385
// NO NODE
386386
if(lower<0) {
@@ -426,7 +426,7 @@ getControlValue(ArrayPtrs<ControlLinearNode> &aNodes,double aT)
426426

427427
// GET NODE
428428
_searchNode.setTime(aT);
429-
int i = aNodes.searchBinary(_searchNode);
429+
int i = searchBinary(aNodes, _searchNode);
430430

431431
// BEFORE FIRST
432432
double value;
@@ -512,6 +512,47 @@ extrapolateAfter(ArrayPtrs<ControlLinearNode> &aNodes,double aT) const
512512
return(value);
513513
}
514514

515+
int ControlLinear::
516+
searchBinary(const ArrayPtrs<ControlLinearNode>& nodes,
517+
const ControlLinearNode& value) const
518+
{
519+
const int size = nodes.getSize();
520+
if(size <= 0) {
521+
return -1;
522+
}
523+
524+
int lo = 0;
525+
int hi = size - 1;
526+
int mid = -1;
527+
528+
while(lo <= hi) {
529+
mid = (lo + hi) / 2;
530+
const ControlLinearNode& candidate = *nodes[mid];
531+
532+
if(value.isLessThan(candidate)) {
533+
hi = mid - 1;
534+
} else if(candidate.isLessThan(value)) {
535+
lo = mid + 1;
536+
} else {
537+
break;
538+
}
539+
}
540+
541+
if(mid < 0) {
542+
return -1;
543+
}
544+
545+
if(value.isLessThan(*nodes[mid])) {
546+
mid--;
547+
}
548+
549+
if(mid < 0) {
550+
return -1;
551+
}
552+
553+
return mid;
554+
}
555+
515556
//-----------------------------------------------------------------------------
516557
// CONTROL VALUE
517558
//-----------------------------------------------------------------------------
@@ -743,7 +784,7 @@ filter(double aT)
743784
// FIND CONTROL NODE
744785
// Find the control node at time aT
745786
_searchNode.setTime(aT);
746-
int i = _xNodes.searchBinary(_searchNode);
787+
int i = searchBinary(_xNodes, _searchNode);
747788
// The following property is true after binary search:
748789
// _xNodes[i].getValue() <= getControlValue(aT)
749790
// i.e. the node whose index (i) was returned is the node

OpenSim/Simulation/Control/ControlLinear.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ OpenSim_DECLARE_CONCRETE_OBJECT(ControlLinear, Control);
386386
double getControlValue(ArrayPtrs<ControlLinearNode> &aNodes,double aT);
387387
double extrapolateBefore(const ArrayPtrs<ControlLinearNode> &aNodes,double aT) const;
388388
double extrapolateAfter(ArrayPtrs<ControlLinearNode> &aNodes,double aT) const;
389+
int searchBinary(const ArrayPtrs<ControlLinearNode>& nodes,
390+
const ControlLinearNode& value) const;
389391

390392
//=============================================================================
391393
}; // END of class ControlLinear

0 commit comments

Comments
 (0)