2 #include "gtest/gtest.h" 4 #include <boost/concept_check.hpp> 30 enum class StateNmVars
38 enum class StateCfVars
47 enum class ParamFuncVars
54 enum class ParamFuncLattices
71 double stateArc()
const override 73 return stateCf_.value(
asInt(StateCfVars::T));
77 double stateDist()
const override 79 return stateCf_.value(
asInt(StateCfVars::S));
98 paramFuncs_.
setEvalArc(_arc, _evalArcGuarantee);
99 stateCf_.value(
asInt(StateCfVars::T)) = _arc;
101 stateCf_.value(
asInt(StateCfVars::W)) = paramFuncs_.computeFuncVal(
asInt(ParamFuncVars::W));
102 stateCf_.value(
asInt(StateCfVars::S)) = paramFuncs_.computeS();
106 State& stateNmDot()
override 108 stateNmDotCache_.
value(
asInt(StateNmVars::X)) =
110 stateNmDotCache_.value(
asInt(StateNmVars::Y)) =
112 stateNmDotCache_.value(
asInt(StateNmVars::THETA)) = stateCf_.value(
asInt(StateCfVars::W));
113 return stateNmDotCache_;
117 State& stateNmDelta(
const double& _dArc)
override 120 const double& w = stateCf_.value(
asInt(StateCfVars::W));
121 const double& theta = stateNm_.value(
asInt(StateNmVars::THETA));
122 double& dx = stateNmDotCache_.value(
asInt(StateNmVars::X));
123 double& dy = stateNmDotCache_.value(
asInt(StateNmVars::Y));
124 double& da = stateNmDotCache_.value(
asInt(StateNmVars::THETA));
126 if (fabs(w) > FLT_MIN)
128 const double r = v / w;
129 dx = (-r * sin(theta) + r * sin(theta + w * _dArc));
130 dy = (+r * cos(theta) - r * cos(theta + w * _dArc));
135 dx = v * _dArc * cos(theta);
136 dy = v * _dArc * sin(theta);
139 return stateNmDotCache_;
146 using SNV = StateSimTest::StateNmVars;
147 using SCF = StateSimTest::StateCfVars;
148 using PFV = StateSimTest::ParamFuncVars;
260 class TrajectorySimulatorsTest :
public ::testing::Test
266 TrajectorySimulatorsTest()
269 sp = std::shared_ptr<StateSimTest>(
new StateSimTest);
272 vector<PFS> pf(2, PFS());
273 static constexpr
const int paramFuncArcRefIdxAll = 0;
275 pf[
asInt(
PFV::V)].ctrlPtsArcRefIdx = paramFuncArcRefIdxAll;
276 pf[
asInt(PFV::W)].ctrlPtsSize = 3;
277 pf[
asInt(PFV::W)].ctrlPtsArcRefIdx = paramFuncArcRefIdxAll;
285 sp->paramFuncs()->init(pf);
293 virtual ~TrajectorySimulatorsTest()
307 virtual void TearDown()
318 bool checkSimLatticeConsistency(vector<TrajectorySimulator::LatticePointType>& _lattice)
320 bool consistent =
true;
325 for (
size_t i = 1; i < trajSim->simulationLattice_.size(); ++i)
327 if (!(trajSim->simulationLattice_[i - 1].arc <= trajSim->simulationLattice_[i].arc))
337 size_t nrPartStates = 0;
338 for (
size_t i = 0; i < _tsp->partLattices_.size(); ++i)
340 nrPartStates += _tsp->partLattices_[i]->size();
344 void checkSimLatticeStatesEqual(vector<TrajectorySimulator::LatticePointType>& _latticeRef,
345 vector<TrajectorySimulator::LatticePointType>& _latticeTest)
351 ASSERT_EQ(_latticeRef.size(), _latticeTest.size());
352 for (
size_t i = 0; i < _latticeTest.size(); ++i)
354 for (
size_t j = 0; j < _latticeTest[i].statePtr->valueSize(); ++j)
356 EXPECT_DOUBLE_EQ(_latticeRef[i].statePtr->value(j), _latticeTest[i].statePtr->value(j));
363 ASSERT_EQ(_partLatRef.size(), _partLatTest.size());
364 for (
size_t i = 0; i < _partLatRef.size(); ++i)
366 ASSERT_EQ(_partLatRef[i]->size(), _partLatTest[i]->size());
367 for (
size_t j = 0; j < _partLatRef[i]->size(); ++j)
369 EXPECT_DOUBLE_EQ(_partLatRef[i]->at(j).arc, _partLatTest[i]->at(j).arc);
370 for (
size_t k = 0; k < _partLatRef[i]->at(j).statePtr->valueSize(); ++k)
372 EXPECT_DOUBLE_EQ(_partLatRef[i]->at(j).statePtr->value(k), _partLatTest[i]->at(j).statePtr->value(k));
377 size_t expectedDtStates(
const double _DeltaT,
const double _dt)
379 return (ceil(_DeltaT / _dt) + 1);
385 TEST_F(TrajectorySimulatorsTest, SimPrecalcFrom0Dt)
387 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
408 trajSim->dsBase() = -1;
410 size_t statesBeginEnd = 2, statesDt, statesDs, statesLattice0;
411 double sumStates = 0;
414 trajSim->dtBase() = 0.1;
415 trajSim->simulateTrajectory(0);
416 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
419 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
420 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
421 statesSimExp = statesDt;
422 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
423 checkSimLatticeConsistency(trajSim->simulationLattice_);
425 trajSim->dtBase() = 0.12;
426 trajSim->simulateTrajectory(0);
427 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
430 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
431 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
432 statesSimExp = statesDt;
433 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
434 checkSimLatticeConsistency(trajSim->simulationLattice_);
436 trajSim->dtBase() = 0.25;
437 trajSim->simulateTrajectory(0);
438 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
441 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
442 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
443 statesSimExp = statesDt;
444 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
445 checkSimLatticeConsistency(trajSim->simulationLattice_);
448 TEST_F(TrajectorySimulatorsTest, SimPrecalcFrom0DtDsCtrlPt)
450 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
471 vector<vector<double*> > funcKnotsLattice(1, vector<double*>(funcs->
funcsArcSize(0), 0));
472 for (
size_t i = 0; i < funcKnotsLattice[0].size(); ++i)
474 funcKnotsLattice[0][i] = &funcs->
funcsArc(0, i);
476 trajSim->setUserDefLattice(funcKnotsLattice);
477 size_t statesBeginEnd = 2, statesDt, statesDs, statesLattice0;
479 double sumStates = 0;
481 trajSim->dtBase() = 0.1;
482 trajSim->dsBase() = 0.1;
483 trajSim->simulateTrajectory(0);
484 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
487 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
488 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
491 statesSimExp = statesDt + statesLattice0 - 2;
492 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
493 checkSimLatticeConsistency(trajSim->simulationLattice_);
495 trajSim->dtBase() = 0.12;
496 trajSim->dsBase() = 0.12;
497 trajSim->simulateTrajectory(0);
498 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
501 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
502 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
503 statesSimExp = statesDt + statesLattice0 - 2;
504 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
505 checkSimLatticeConsistency(trajSim->simulationLattice_);
507 trajSim->dtBase() = 0.25;
508 trajSim->dsBase() = 0.01;
509 trajSim->simulateTrajectory(0);
510 statesDt = expectedDtStates(funcs->
funcsArcEnd(), trajSim->dt());
513 sumStates = statesBeginEnd + statesDt + statesDs + statesLattice0;
514 EXPECT_EQ(sumStates, sumAllPartStates(trajSim));
515 statesSimExp = statesDt + statesDs - 2 - 3 + statesLattice0 - 2;
516 EXPECT_EQ(statesSimExp, trajSim->simLattice().size());
517 checkSimLatticeConsistency(trajSim->simulationLattice_);
520 TEST_F(TrajectorySimulatorsTest, SimPrecalcFromNon0DtDsCtrlPt)
522 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
543 vector<vector<double*> > funcKnotsLattice(1, vector<double*>(funcs->
funcsArcSize(0), 0));
544 for (
size_t i = 0; i < funcKnotsLattice[0].size(); ++i)
546 funcKnotsLattice[0][i] = &funcs->
funcsArc(0, i);
548 trajSim->setUserDefLattice(funcKnotsLattice);
550 trajSim->dtBase() = 0.13;
551 trajSim->dsBase() = 0.0795;
552 trajSim->simulateTrajectory(0);
554 vector<TrajectorySimulator::LatticePointType> trajStatesRef;
555 for (
size_t i = 0; i < trajSim->simulationLattice_.size(); ++i)
557 StateSPtr stateNow = trajSim->simulationLattice_[i].statePtr->cloneState();
559 trajSim->simulationLattice_[i].arc, trajSim->simulationLattice_[i].latticeType, stateNow));
562 partLatRef.resize(trajSim->partLattices_.size());
563 for (
auto& latRefI : partLatRef)
567 for (
size_t i = 0; i < trajSim->partLattices_.size(); ++i)
569 partLatRef[i]->resize(trajSim->partLattices_[i]->size());
570 for (
size_t j = 0; j < trajSim->partLattices_[i]->size(); ++j)
572 partLatRef[i]->at(j).arc = trajSim->partLattices_[i]->at(j).arc;
573 partLatRef[i]->at(j).statePtr = trajSim->partLattices_[i]->at(j).statePtr->cloneState();
577 trajSim->simulateTrajectory(0.10);
578 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
579 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
580 trajSim->simulateTrajectory(0.34);
581 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
582 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
583 trajSim->simulateTrajectory(0.79);
584 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
585 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
586 trajSim->simulateTrajectory(1.00);
587 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
588 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
589 trajSim->simulateTrajectory(0.54);
590 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
591 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
592 trajSim->simulateTrajectory(1.50);
593 checkSimLatticeStatesEqual(trajStatesRef, trajSim->simulationLattice_);
594 checkPartLatticeStatesEqual(partLatRef, trajSim->partLattices_);
599 TEST_F(TrajectorySimulatorsTest, SimOnlineFrom0Dt)
601 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
622 trajSim->dsBase() = -1;
623 trajSimOnline->dsBase() = -1;
625 trajSim->dtBase() = 0.10;
626 trajSim->simulateTrajectory(0);
627 trajSimOnline->dtBase() = 0.10;
628 trajSimOnline->simulateTrajectory(0);
629 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
630 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
632 trajSim->dtBase() = 0.12;
633 trajSim->simulateTrajectory(0);
634 trajSimOnline->dtBase() = 0.12;
635 trajSimOnline->simulateTrajectory(0);
636 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
637 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
639 trajSim->dtBase() = 0.25;
640 trajSim->simulateTrajectory(0);
641 trajSimOnline->dtBase() = 0.25;
642 trajSimOnline->simulateTrajectory(0);
643 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
644 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
647 TEST_F(TrajectorySimulatorsTest, SimOnlineFrom0DtDsCtrlPt)
649 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
669 vector<vector<double*> > funcKnotsLattice(1, vector<double*>(funcs->
funcsArcSize(0), 0));
670 for (
size_t i = 0; i < funcKnotsLattice[0].size(); ++i)
672 funcKnotsLattice[0][i] = &funcs->
funcsArc(0, i);
674 trajSim->setUserDefLattice(funcKnotsLattice);
675 trajSimOnline->setUserDefLattice(funcKnotsLattice);
677 trajSim->dtBase() = 0.10;
678 trajSim->dsBase() = 0.10;
679 trajSim->simulateTrajectory(0);
680 trajSimOnline->dtBase() = 0.10;
681 trajSimOnline->dsBase() = 0.10;
682 trajSimOnline->simulateTrajectory(0);
683 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
684 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
686 trajSim->dtBase() = 0.12;
687 trajSim->dsBase() = 0.12;
688 trajSim->simulateTrajectory(0);
689 trajSimOnline->dtBase() = 0.12;
690 trajSimOnline->dsBase() = 0.12;
691 trajSimOnline->simulateTrajectory(0);
692 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
693 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
695 trajSim->dtBase() = 0.25;
696 trajSim->dsBase() = 0.01;
697 trajSim->simulateTrajectory(0);
698 trajSimOnline->dtBase() = 0.25;
699 trajSimOnline->dsBase() = 0.01;
700 trajSimOnline->simulateTrajectory(0);
701 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
702 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
705 TEST_F(TrajectorySimulatorsTest, SimOnlineFromNon0DtDsCtrlPt)
707 ParamFuncs* funcs = trajSim->stateSim()->paramFuncs();
727 vector<vector<double*> > funcKnotsLattice(1, vector<double*>(funcs->
funcsArcSize(0), 0));
728 for (
size_t i = 0; i < funcKnotsLattice[0].size(); ++i)
730 funcKnotsLattice[0][i] = &funcs->
funcsArc(0, i);
732 trajSim->setUserDefLattice(funcKnotsLattice);
733 trajSimOnline->setUserDefLattice(funcKnotsLattice);
735 trajSim->dtBase() = 0.13;
736 trajSim->dsBase() = 0.0795;
737 trajSim->simulateTrajectory(0);
738 trajSimOnline->dtBase() = 0.13;
739 trajSimOnline->dsBase() = 0.0795;
740 trajSimOnline->simulateTrajectory(0);
742 trajSim->simulateTrajectory(0.10);
743 trajSimOnline->simulateTrajectory(0.10);
744 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
745 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
746 trajSim->simulateTrajectory(0.34);
747 trajSimOnline->simulateTrajectory(0.34);
748 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
749 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
750 trajSim->simulateTrajectory(0.79);
751 trajSimOnline->simulateTrajectory(0.79);
752 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
753 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
754 trajSim->simulateTrajectory(1.00);
755 trajSimOnline->simulateTrajectory(1.00);
756 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
757 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
758 trajSim->simulateTrajectory(0.52);
759 trajSimOnline->simulateTrajectory(0.52);
760 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
761 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
762 trajSim->simulateTrajectory(1.50);
763 trajSimOnline->simulateTrajectory(1.50);
764 checkSimLatticeStatesEqual(trajSim->simulationLattice_, trajSimOnline->simulationLattice_);
765 checkPartLatticeStatesEqual(trajSim->partLattices_, trajSimOnline->partLattices_);
772 int main(
int argc,
char** argv)
774 ::testing::InitGoogleTest(&argc, argv);
775 return RUN_ALL_TESTS();
double & funcsArc(const std::size_t &_i, const std::size_t &_j)
Access to the arc parameter vector at index _i, control point _j.
std::shared_ptr< State > StateSPtr
previous evaluation arc <= this evaluation arc
virtual double & value(const std::size_t &_i)=0
Access state variable based on index _i.
std::unique_ptr< StateSim > StateSimUPtr
control point arc parameter
Structure containing the lattice type afferent to a.
Helper function needed to upgrade c++ 2011.
constexpr auto asInt(Enumeration const value) -> typename std::underlying_type< Enumeration >::type
std::vector< LatticePoint > LatticeVec
double & funcsArcEnd()
Access to the final value of the arc parametrization.
Templated partial implementation of StateSim.
EvalArcGuarantee
Flags if any guarantees about evaluation arc relative to last evaluation arc are present.
agent base center linear velocity is parametric function
Containts parametric function initialization data.
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
double & ctrlPtVal(const std::size_t &_funcIdx, const std::size_t &_funcCtrlPtIdx, const CtrlPtDim &_ctrlPtDim=CtrlPtDim::VAL)
Access of a parametric function control point dimension.
int main(int argc, char **argv)
std::size_t funcCtrlPtSize(const std::size_t &_i) const
Number of control points for a parametric function.
CtrlPtDim
Control point variable dimension.
Extends manipulation of parametric functions collection with closed-form arc length (distance) comput...
std::shared_ptr< TrajectorySimulator > TrajectorySimulatorSPtr
FuncEvalMode
Required type of computation relative to the parametric function.
std::size_t funcsArcSize() const
Number of arc parametrizations.
std::vector< std::shared_ptr< std::vector< LatticePoint > > > LatticeVecSPtrVec
std::shared_ptr< StateSim > StateSimPtr
virtual void precompute()=0
Precomputes cached data.
agent base center linear velocity is parametric function
virtual void setEvalArc(const double &_funcsArcEval, const EvalArcGuarantee &_evalArcGuarantee=EvalArcGuarantee::NONE)=0
Sets parametric function evaluation arc.
Storage and manipulation of parametric functions collection.