-
Notifications
You must be signed in to change notification settings - Fork 76
/
TOPP.cpp
2777 lines (2497 loc) · 99.6 KB
/
TOPP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- coding: utf-8 -*-
// Copyright (C) 2013 Quang-Cuong Pham <cuong.pham@normalesup.org>
//
// This file is part of the Time-Optimal Path Parameterization (TOPP) library.
// TOPP is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option, any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "TOPP.h"
namespace TOPP {
////////////////////////////////////////////////////////////////////
/////////////////////////// Constraints ////////////////////////////
////////////////////////////////////////////////////////////////////
bool Constraints::Preprocess() {
switchpointslist.clear();
resprofileslist.resize(0);
// Change discrtimestep so as it becomes a divisor of trajectory duration
int ndiscrsteps = int((trajectory.duration+1e-10)/discrtimestep);
if(ndiscrsteps<1) {
return false;
}
discrtimestep = trajectory.duration/ndiscrsteps;
Discretize();
ComputeMVCBobrow();
ComputeMVCCombined();
FindSwitchPoints();
if(passswitchpointnsteps == 0) {
passswitchpointnsteps = 5;
}
// Set integration timestep automatically if it is initially set to 0
dReal meanmvc = 0;
if(integrationtimestep == 0) {
for(size_t i=0; i< mvccombined.size(); i++) {
meanmvc += std::min(mvccombined[i],10.);
}
meanmvc /= mvccombined.size();
meanmvc = std::min(1.,meanmvc);
integrationtimestep = discrtimestep/meanmvc;
}
return true;
}
void Constraints::Discretize() {
ndiscrsteps = int((trajectory.duration+TINY)/discrtimestep);
ndiscrsteps++;
discrsvect.resize(ndiscrsteps);
for(int i=0; i<ndiscrsteps; i++) {
discrsvect[i] = i*discrtimestep;
}
}
void Constraints::ComputeMVCBobrow() {
mvcbobrow.resize(ndiscrsteps);
for(int i=0; i<ndiscrsteps; i++) {
mvcbobrow[i] = SdLimitBobrowInit(discrsvect[i]);
}
}
void Constraints::ComputeMVCCombined()
{
mvccombined.resize(ndiscrsteps);
for(int i=0; i<ndiscrsteps; i++) {
mvccombined[i] = SdLimitCombinedInit(discrsvect[i]);
}
}
dReal Constraints::Interpolate1D(dReal s, const std::vector<dReal>& v) {
assert(s>=-TINY && s<=trajectory.duration+TINY);
if(s<0) {
s=0;
}
if(s>=trajectory.duration) {
int n = ndiscrsteps-1;
return v[n];
}
int n = int(s/discrtimestep);
dReal coef = (s-n*discrtimestep)/discrtimestep;
return (1-coef)*v[n] + coef*v[n+1];
}
dReal Constraints::SdLimitCombinedInit(dReal s){
dReal res = SdLimitBobrow(s);
std::vector<dReal> qd(trajectory.dimension);
if(hasvelocitylimits) {
trajectory.Evald(s, qd);
for(int i=0; i<trajectory.dimension; i++) {
if(std::abs(qd[i])>TINY && std::abs(vmax[i])>0) {
res = std::min(res,vmax[i]/std::abs(qd[i]));
}
}
}
return res;
}
dReal Constraints::SdLimitCombined(dReal s) {
return Interpolate1D(s,mvccombined);
}
dReal Constraints::SdLimitBobrow(dReal s) {
return Interpolate1D(s,mvcbobrow);
}
void Constraints::WriteMVCBobrow(std::stringstream& ss, dReal dt){
dReal duration = trajectory.duration;
ss << duration << " " << dt << "\n";
for(dReal t=0; t<=duration; t+=dt) {
ss << t << " ";
}
ss << "\n";
for(dReal t=0; t<=duration; t+=dt) {
ss << SdLimitBobrow(t) << " ";
}
}
void Constraints::WriteMVCDirect(std::stringstream& ss, dReal dt){
std::vector<dReal> qd(trajectory.dimension);
dReal duration = trajectory.duration;
ss << duration << " " << dt << "\n";
for(dReal t=0; t<=duration; t+=dt) {
ss << t << " ";
}
ss << "\n";
for(dReal t=0; t<=duration; t+=dt) {
dReal res = INF;
trajectory.Evald(t, qd);
for(int i=0; i<trajectory.dimension; i++) {
if(std::abs(qd[i])>TINY && std::abs(vmax[i])>0) {
res = std::min(res,vmax[i]/std::abs(qd[i]));
}
}
ss << res << " ";
}
}
void Constraints::FindSwitchPoints()
{
switchpointslist.clear();
FindSingularSwitchPoints();
FindTangentSwitchPoints();
FindDiscontinuousSwitchPoints();
TrimSwitchPoints();
}
void Constraints::AddSwitchPoint(int i, int switchpointtype, dReal sd){
dReal s = discrsvect[i];
// If no sd is specified, then take the value of the mvc
// (The case when sd is specified corresponds to a singular switchpoint in some cases)
if(sd<0) {
sd = mvcbobrow[i];
}
if(sd > MAXSD) {
return;
}
std::list<SwitchPoint>::iterator it = switchpointslist.begin();
while(it!=switchpointslist.end()) {
if(s == it->s) {
return;
}
if(s<=it->s) {
break;
}
it++;
}
SwitchPoint sw(s,sd,switchpointtype);
if(switchpointtype == SP_SINGULAR) {
sw.slopesvector.resize(0);
ComputeSlopeDynamicSingularity(s,sd,sw.slopesvector);
}
switchpointslist.insert(it,sw);
}
bool CompareSwitchPoint(const SwitchPoint& sw0, const SwitchPoint& sw1)
{
return sw0.s < sw1.s;
}
void Constraints::AddSwitchPoint2(dReal s, dReal sd, int switchpointtype)
{
// If no sd is specified, then take the value of the mvc
// (The case when sd is specified corresponds to a singular switchpoint in some cases)
if(sd > MAXSD) {
return;
}
SwitchPoint sw(s,sd,switchpointtype);
std::list<SwitchPoint>::iterator it = std::lower_bound(switchpointslist.begin(), switchpointslist.end(), sw, CompareSwitchPoint);
if( it != switchpointslist.end() ) {
if( s >= it->s+TINY ) {
std::cout << "switch point already exists, type=" << it->switchpointtype;
return;
}
}
if(switchpointtype == SP_SINGULAR) {
sw.slopesvector.resize(0);
ComputeSlopeDynamicSingularity(s,sd,sw.slopesvector);
}
switchpointslist.insert(it,sw);
}
void Constraints::FindTangentSwitchPoints(){
if(ndiscrsteps<3)
return;
int i = 1;
dReal s,sd,snext,sdnext,alpha,diff,diffprev,tangent,prevtangent;
std::pair<dReal,dReal> sddlimits;
s = discrsvect[i];
snext = discrsvect[i+1];
sd = SdLimitBobrow(s);
sdnext = SdLimitBobrow(snext);
tangent = (sdnext-sd)/discrtimestep;
prevtangent = (sd - SdLimitBobrow(discrsvect[i-1]))/discrtimestep;
sddlimits = SddLimits(s,sd);
alpha = sddlimits.first;
//beta = sddlimits.second;
diffprev = alpha/sd - tangent;
for(int i=2; i<ndiscrsteps-1; i++) {
s = discrsvect[i];
snext = discrsvect[i+1];
sd = SdLimitBobrow(s);
sdnext = SdLimitBobrow(snext);
sddlimits = SddLimits(s,sd);
alpha = sddlimits.first;
if(std::abs(prevtangent-tangent)>2 && prevtangent < 0 && tangent >0) {
AddSwitchPoint2(s,sd,SP_DISCONTINUOUS);
}
prevtangent = tangent;
tangent = (sdnext-sd)/discrtimestep;
//if(std::abs(tangent-prevtangent)>1.) {
// continue;
//}
//beta = sddlimits.second;
diff = alpha/sd - tangent;
if(diffprev*diff<0 && std::abs(diff)<1) {
AddSwitchPoint2(s,sd,SP_TANGENT);
}
diffprev = diff;
}
}
void Constraints::FindDiscontinuousSwitchPoints() {
if(ndiscrsteps<3)
return;
int i = 0;
dReal sd, sdn, sdnn;
sd = SdLimitBobrow(discrsvect[i]);
sdn = SdLimitBobrow(discrsvect[i+1]);
// also look for the start of the chunks for the trajectory
//std::list<dReal>::const_iterator itchunkstart = trajectory.chunkcumulateddurationslist.begin();
//int nLastAddedSwitchIndex = -1;
for(int i=0; i<ndiscrsteps-2; i++) {
sdnn = SdLimitBobrow(discrsvect[i+2]);
if(std::abs(sdnn-sdn)>100*std::abs(sdn-sd)) {
if(sdn<sdnn) {
AddSwitchPoint2(discrsvect[i+1],mvcbobrow[i+1],SP_DISCONTINUOUS);
}
else{
AddSwitchPoint2(discrsvect[i+2],mvcbobrow[i+2],SP_DISCONTINUOUS);
}
}
// if( trajectory.degree <= 3 ) {
// // if the trajectory degree is <= 3, then the accelerations will not be differentiable at the trajectory chunk edges.
// // therefore add those discontinuity points.
// // perhaps there's a better way to compute this, but the above threshold doesn't catch it.
// if( itchunkstart != trajectory.chunkcumulateddurationslist.end() && *itchunkstart <= discrsvect[i+2]+TINY ) {
// if( nLastAddedSwitchIndex < i+1 ) {
// AddSwitchPoint2(discrsvect[i+1],mvcbobrow[i+1],SP_DISCONTINUOUS);
// nLastAddedSwitchIndex = i+1;
// }
// ++itchunkstart;
// }
// }
sd = sdn;
sdn = sdnn;
}
}
void InsertInSpList(std::list<SwitchPoint>& splist, SwitchPoint sp){
if(splist.size()==0) {
splist.push_back(sp);
return;
}
std::list<SwitchPoint>::iterator it = splist.begin();
while(it!=splist.end()) {
if(sp.switchpointtype == SP_SINGULAR) {
if((it->switchpointtype == SP_SINGULAR && it->sd >= sp.sd) || it->switchpointtype!=SP_SINGULAR) {
splist.insert(it,sp);
return;
}
}
else if(it->switchpointtype!=SP_SINGULAR && it->sd >= sp.sd ) {
splist.insert(it,sp);
return;
}
it++;
}
splist.push_back(sp);
}
void Constraints::TrimSwitchPoints() {
dReal radius = discrtimestep*2.1;
dReal scur = -1, snext, sdcur = -1, sdnext;
int stypenext;
std::list<SwitchPoint>::iterator itcur;
std::vector<dReal> slopesvector;
std::list<SwitchPoint>::iterator it = switchpointslist.begin();
// Merge singular points
// Find the first singular point, if any
while(it!=switchpointslist.end()) {
if(it->switchpointtype == SP_SINGULAR) {
scur = it->s;
sdcur = it->sd;
itcur = it;
break;
}
it++;
}
// Merge consecutive singular points that are in a small radius
if(scur>=0 && it!=switchpointslist.end()) {
it++;
while(it!=switchpointslist.end()) {
snext = it->s;
sdnext = it->sd;
stypenext = it->switchpointtype;
if(stypenext == SP_SINGULAR) {
if(snext-scur<radius) {
if(sdcur < sdnext) {
slopesvector = it->slopesvector;
it = switchpointslist.erase(it);
}
else{
slopesvector = itcur->slopesvector;
switchpointslist.erase(itcur);
scur = snext;
sdcur = sdnext;
itcur = it;
it++;
}
for(int j=0; j<int(slopesvector.size()); j++) {
itcur->slopesvector.push_back(slopesvector[j]);
}
}
else{
scur = snext;
sdcur = sdnext;
itcur = it;
it++;
}
}
else{
it++;
}
}
}
// Merge non-singular switchpoints
// Find the first non singular switchpoint, if any
it = switchpointslist.begin();
while(it!=switchpointslist.end()) {
if(it->switchpointtype != SP_SINGULAR) {
scur = it->s;
sdcur = it->sd;
itcur = it;
break;
}
it++;
}
// Merge consecutive non-singular switchpoints that are in a small radius
if(scur>=0 && it!=switchpointslist.end()) {
it++;
while(it!=switchpointslist.end()) {
snext = it->s;
sdnext = it->sd;
stypenext = it->switchpointtype;
if(stypenext != SP_SINGULAR) {
if(snext-scur<radius) {
if(sdcur < sdnext) {
slopesvector = it->slopesvector;
it = switchpointslist.erase(it);
}
else{
slopesvector = itcur->slopesvector;
switchpointslist.erase(itcur);
// don't set scur/sdcur since then radius will be sliding
//scur = snext;
//sdcur = sdnext;
itcur = it;
it++;
}
for(int j=0; j<int(slopesvector.size()); j++) {
itcur->slopesvector.push_back(slopesvector[j]);
}
}
else{
scur = snext;
sdcur = sdnext;
itcur = it;
it++;
}
}
else{
it++;
}
}
}
// Sort by sd
std::list<SwitchPoint> splist;
it = switchpointslist.begin();
while(it!=switchpointslist.end()) {
InsertInSpList(splist,*it);
it++;
}
switchpointslist = splist;
}
////////////////////////////////////////////////////////////////////
/////////////////// Quadratic Constraints //////////////////////////
////////////////////////////////////////////////////////////////////
QuadraticConstraints::QuadraticConstraints(const std::string& constraintsstring) {
std::vector<dReal> tmpvect;
std::string buff;
std::istringstream iss(constraintsstring);
getline(iss, buff, '\n');
discrtimestep = atof(buff.c_str());
getline(iss, buff, '\n');
VectorFromString(buff, vmax);
while(iss.good()) {
getline(iss, buff, '\n');
VectorFromString(buff, tmpvect);
avect.push_back(tmpvect);
getline(iss, buff, '\n');
VectorFromString(buff,tmpvect);
bvect.push_back(tmpvect);
getline(iss, buff, '\n');
VectorFromString(buff,tmpvect);
cvect.push_back(tmpvect);
}
nconstraints = int(avect.front().size());
hasvelocitylimits = VectorMax(vmax) > TINY;
}
void QuadraticConstraints::WriteConstraints(std::stringstream& ss){
ss << discrtimestep << "\n";
for(int i=0; i<int(vmax.size()); i++) {
ss << vmax[i] << " ";
}
ss << "\n";
for(int i=0; i<int(avect.size()); i++) {
for(int j=0; j<int(avect[0].size()); j++) {
ss << avect[i][j] << " ";
}
ss << "\n";
for(int j=0; j<int(avect[0].size()); j++) {
ss << bvect[i][j] << " ";
}
ss << "\n";
for(int j=0; j<int(avect[0].size()); j++) {
ss << cvect[i][j] << " ";
}
if(i<int(avect.size())-1) {
ss << "\n";
}
}
}
void QuadraticConstraints::CheckInput() {
if ((int)vmax.size() != trajectory.dimension) {
std::ostringstream msg;
msg << "vmax has dimension " << vmax.size()
<< " but trajectory has dimension " << trajectory.dimension << ".";
std::cout << "[TOPP] " << msg.str() << std::endl;
throw TOPPException(msg.str());
}
}
void QuadraticConstraints::InterpolateDynamics(dReal s, std::vector<dReal>& a, std::vector<dReal>& b, std::vector<dReal>& c) {
a.resize(nconstraints);
b.resize(nconstraints);
c.resize(nconstraints);
BOOST_ASSERT(s>=-TINY && s<=trajectory.duration+TINY);
if(s < 0)
s = 0;
if(s >= trajectory.duration-TINY) {
int n = ndiscrsteps-1;
for(int i = 0; i < nconstraints; i++) {
a[i]= avect[n][i];
b[i]= bvect[n][i];
c[i]= cvect[n][i];
}
return;
}
int n = int(s/discrtimestep);
dReal coef = (s-n*discrtimestep);
if( std::abs(coef) <= TINY ) {
a = avect[n];
b = bvect[n];
c = cvect[n];
}
else {
coef /= discrtimestep;
for (int i = 0; i < nconstraints; i++) {
a[i] = (1-coef)*avect[n][i] + coef*avect[n+1][i];
b[i] = (1-coef)*bvect[n][i] + coef*bvect[n+1][i];
c[i] = (1-coef)*cvect[n][i] + coef*cvect[n+1][i];
}
}
}
void QuadraticConstraints::ComputeSlopeDynamicSingularity(dReal s, dReal sd, std::vector<dReal>& slopesvector) {
dReal delta = TINY2, s2, ap, bp, cp, slope;
std::vector<dReal> a, b, c, a2, b2, c2;
if(s>delta) {
delta = -delta;
}
s2 = s + delta;
InterpolateDynamics(s,a,b,c);
InterpolateDynamics(s2,a2,b2,c2);
dReal idelta=1/delta;
slopesvector.resize(a.size());
for(size_t i=0; i< a.size(); i++) {
ap = (a2[i]-a[i])*idelta;
bp = (b2[i]-b[i])*idelta;
cp = (c2[i]-c[i])*idelta;
if(std::abs((2*b[i]+ap)*sd)>TINY) {
slope = (-bp*sd*sd-cp)/((2*b[i]+ap)*sd);
}
else{
slope = 0;
}
slopesvector[i] = slope;
}
}
std::pair<dReal,dReal> QuadraticConstraints::SddLimits(dReal s, dReal sd){
dReal dtsq = integrationtimestep;
dtsq = dtsq*dtsq;
dReal alpha = -INF;
dReal beta = INF;
dReal sdsq = sd*sd;
std::vector<dReal> a, b, c;
dReal alpha_i, beta_i;
InterpolateDynamics(s,a,b,c);
for(int i=0; i<nconstraints; i++) {
if(std::abs(a[i])<TINY) {
if(b[i]*sdsq+c[i]>0) {
// Constraint not satisfied
beta = -INF;
alpha = INF;
}
continue;
}
if(a[i]>0) {
beta_i = (-sdsq*b[i]-c[i])/a[i];
beta = std::min(beta,beta_i);
}
else{
alpha_i = (-sdsq*b[i]-c[i])/a[i];
alpha = std::max(alpha,alpha_i);
}
}
std::pair<dReal,dReal> result(alpha,beta);
return result;
}
dReal QuadraticConstraints::SdLimitBobrowInit(dReal s){
std::vector<dReal> a, b, c;
InterpolateDynamics(s,a,b,c);
if(VectorNorm(a)<TINY) {
if(s<1e-2) {
s+=1e-3;
}
else{
s-=1e-3;
}
InterpolateDynamics(s,a,b,c);
}
std::pair<dReal,dReal> sddlimits = SddLimits(s,0);
if(sddlimits.first > sddlimits.second) {
return 0;
}
dReal sdmin = INF;
for(int k=0; k<nconstraints; k++) {
for(int m=k+1; m<nconstraints; m++) {
dReal num, denum, r;
// If we have a pair of alpha and beta bounds, then determine the sdot for which the two bounds are equal
if(a[k]*a[m]<0) {
num = a[k]*c[m]-a[m]*c[k];
denum = -a[k]*b[m]+a[m]*b[k];
if(std::abs(denum)>TINY) {
r = num/denum;
if(r>=0) {
sdmin = std::min(sdmin,sqrt(r));
}
}
}
}
}
return sdmin;
}
// Compute the SdLimitBobrow after removing one inequality (sdot^\dag in Pham 2014)
dReal QuadraticConstraints::SdLimitBobrowExclude(dReal s, int iexclude){
std::vector<dReal> a, b, c;
InterpolateDynamics(s,a,b,c);
if(VectorNorm(a)<TINY) {
if(s<1e-2) {
s+=1e-3;
}
else{
s-=1e-3;
}
InterpolateDynamics(s,a,b,c);
}
std::pair<dReal,dReal> sddlimits = SddLimits(s,0);
if(sddlimits.first > sddlimits.second) {
return 0;
}
dReal sdmin = INF;
for(int k=0; k<nconstraints; k++) {
for(int m=k+1; m<nconstraints; m++) {
if (k==iexclude || m==iexclude) {
continue;
}
dReal num, denum, r;
// If we have a pair of alpha and beta bounds, then determine the sdot for which the two bounds are equal
if(a[k]*a[m]<0) {
num = a[k]*c[m]-a[m]*c[k];
denum = -a[k]*b[m]+a[m]*b[k];
if(std::abs(denum)>TINY) {
r = num/denum;
if(r>=0) {
sdmin = std::min(sdmin,sqrt(r));
}
}
}
}
}
return sdmin;
}
void QuadraticConstraints::FindSingularSwitchPoints() {
if(ndiscrsteps<3) {
return;
}
int i = 0;
std::vector<dReal> a,b,c, aprev, bprev, cprev;
InterpolateDynamics(discrsvect[i],aprev,bprev,cprev);
for(int i=1; i<ndiscrsteps-1; i++) {
InterpolateDynamics(discrsvect[i],a,b,c);
dReal minsd = INF;
dReal mins = INF;
bool found = false;
for(int j=0; j< int(a.size()); j++) {
if(a[j]*aprev[j]<=0) {
dReal adiff = a[j] - aprev[j];
dReal ccur=c[j], bcur=b[j], scur=discrsvect[i];
if( fabs(adiff) > TINY ) {
// compute the zero-crossing and linearly interpolate dynamics
dReal interp=-aprev[j]/adiff;
scur = discrsvect[i-1] + interp*(discrsvect[i]-discrsvect[i-1]);
bcur = bprev[j] + interp*(b[j]-bprev[j]);
ccur = cprev[j] + interp*(c[j]-cprev[j]);
}
dReal f = ccur/bcur;
if(f<0) {
dReal sdstar = sqrt(-f);
dReal sdplus = SdLimitBobrowExclude(scur,j);
if(sdplus >0 && sdplus < sdstar) {
continue;
}
if( !found || sdstar < minsd ) {
found = true;
minsd = sdstar;
mins = scur;
}
}
}
}
if(found) {
AddSwitchPoint2(mins, minsd, SP_SINGULAR);
}
aprev.swap(a);
bprev.swap(b);
cprev.swap(c);
}
}
////////////////////////////////////////////////////////////////////
//////////////////////////// Profile ///////////////////////////////
////////////////////////////////////////////////////////////////////
Profile::Profile(const std::list<dReal>& slist, const std::list<dReal>& sdlist, const std::list<dReal>& sddlist, dReal integrationtimestep, bool forward) : integrationtimestep(integrationtimestep), forward(forward) {
if( forward ) {
svect.insert(svect.end(), slist.begin(), slist.end());
sdvect.insert(sdvect.end(), sdlist.begin(), sdlist.end());
sddvect.insert(sddvect.end(), sddlist.begin(), sddlist.end());
}
else {
svect.insert(svect.end(), slist.rbegin(), slist.rend());
sdvect.insert(sdvect.end(), sdlist.rbegin(), sdlist.rend());
sddvect.insert(sddvect.end(), sddlist.rbegin(), sddlist.rend());
}
BOOST_ASSERT(svect.size()>0);
BOOST_ASSERT(svect.size()==sdvect.size());
BOOST_ASSERT(svect.size()==sddvect.size());
// TODO: handle the case of last step with variable width
nsteps = svect.size();
duration = integrationtimestep * (nsteps-1);
}
Profile::Profile(const std::vector<dReal>& svectin, const std::vector<dReal>& sdvectin, const std::vector<dReal>& sddvectin, dReal integrationtimestep, bool forward) : integrationtimestep(integrationtimestep), forward(forward) {
BOOST_ASSERT(svectin.size()>0);
BOOST_ASSERT(svectin.size()==sdvectin.size());
BOOST_ASSERT(svectin.size()==sddvectin.size());
if( forward ) {
svect = svectin;
sdvect = sdvectin;
sddvect = sddvectin;
}
else {
svect.resize(svectin.size());
std::copy(svectin.rbegin(), svectin.rend(), svect.begin());
sdvect.resize(sdvectin.size());
std::copy(sdvectin.rbegin(), sdvectin.rend(), sdvect.begin());
sddvect.resize(sddvectin.size());
std::copy(sddvectin.rbegin(), sddvectin.rend(), sddvect.begin());
}
// TODO: handle the case of last step with variable width
nsteps = svect.size();
duration = integrationtimestep * (nsteps-1);
}
bool Profile::FindTimestepIndex(dReal t, int &index, dReal& remainder) const {
if (t < -TINY || t > duration+TINY)
return false;
else if (t < 0)
t = 0;
else if (t > duration)
t = duration;
if (duration - t <= TINY)
index = nsteps - 1;
else
index = int(t / integrationtimestep);
remainder = t - index * integrationtimestep;
return true;
}
bool Profile::Invert(dReal s, dReal& t) const {
if( svect.size() == 0 ) {
return false;
}
if(s<svect[0]-TINY || s>svect.back()+TINY) {
return false;
}
std::vector<dReal>::const_iterator it = std::lower_bound(svect.begin(), svect.end(), s);
size_t index = it - svect.begin();
if( index == 0 ) {
t = 0;
}
else {
index -= 1;
dReal tres;
if(!SolveQuadraticEquation(svect[index]-s,sdvect[index],0.5*sddvect[index],tres,0,integrationtimestep)) {
//std::cout << "***************** Inversion warning: tres=" << tres << " while integrationtimestep= "<< integrationtimestep << "****************\n";
SolveQuadraticEquation(svect[index]-s,sdvect[index],0.5*sddvect[index],tres,0,integrationtimestep);
}
t = index*integrationtimestep + tres;
}
return true;
}
dReal Profile::Eval(dReal t) const {
int index;
dReal remainder;
if(FindTimestepIndex(t, index, remainder))
return svect[index] + remainder*sdvect[index]
+ 0.5*remainder*remainder*sddvect[index];
return INF;
}
dReal Profile::Evald(dReal t) const {
int index;
dReal remainder;
if (FindTimestepIndex(t, index, remainder))
return sdvect[index] + remainder * sddvect[index];
return INF;
}
dReal Profile::Evaldd(dReal t) const {
int index;
dReal remainder;
if (FindTimestepIndex(t, index, remainder))
return sddvect[index];
return INF;
}
void Profile::Print() const {
for(dReal t=0; t<=duration; t+=integrationtimestep) {
std::cout<< Eval(t) << " ; " << Evald(t) << " ; " << Evaldd(t) << "\n";
}
}
void Profile::Write(std::stringstream& ss, dReal dt) const {
ss << duration << " " << dt << "\n";
for(dReal t=0; t<=duration; t+=dt) {
ss << Eval(t) << " ";
}
ss << "\n";
for(dReal t=0; t<=duration; t+=dt) {
ss << Evald(t) << " ";
}
}
////////////////////////////////////////////////////////////////////
////////////////// Address switch points ///////////////////////////
////////////////////////////////////////////////////////////////////
// Determine whether one can go more than passswitchpointnsteps steps away from (s,sd)
bool PassSwitchPoint(Constraints& constraints, dReal s, dReal sd, dReal dt){
int ret;
Profile resprofile;
bool testaboveexistingprofiles = false, testmvc = true, zlajpah = false;
ret = IntegrateBackward(constraints,s,sd,dt,resprofile,constraints.passswitchpointnsteps);
if(ret==INT_MAXSTEPS||ret==INT_END) {
ret = IntegrateForward(constraints,s,sd,dt,resprofile,constraints.passswitchpointnsteps,testaboveexistingprofiles, testmvc, zlajpah);
if(ret==INT_MAXSTEPS||ret==INT_END) {
return true;
}
}
return false;
}
// Bisection search to find the highest sd that allows going through a tangent or discontinuous switch point
dReal BisectionSearch(Constraints& constraints, dReal s, dReal sdbottom, dReal sdtop, dReal dt, int position){
if(position!=1 && PassSwitchPoint(constraints,s,sdtop,dt)) {
return sdtop;
}
if(sdtop-sdbottom<constraints.bisectionprecision) {
if(position!=-1 && PassSwitchPoint(constraints,s,sdbottom,dt)) {
return sdbottom;
}
return -1;
}
dReal sdmid = (sdbottom+sdtop)*0.5;
return std::max(BisectionSearch(constraints,s,sdbottom,sdmid,dt,-1),BisectionSearch(constraints,s,sdmid,sdtop,dt,1));
}
// Return false if cannot integrate backward or forward from the switchpoint. This does not mean that the algorithm must fail.
// Return true otherwise. In this case, (sbackward,sdbackward) is the point where the backward integration should start and (sforward,sdforward) is the point where the forward integration should start.
bool AddressSwitchPoint(Constraints& constraints, const SwitchPoint &switchpoint, dReal& sbackward, dReal& sdbackward, dReal& sforward, dReal& sdforward){
dReal s = switchpoint.s;
dReal sd = switchpoint.sd;
dReal dt = constraints.integrationtimestep;
bool testaboveexistingprofiles = false, testmvc = true, zlajpah = false;
Profile resprofile;
// Singular switchpoints
if(switchpoint.switchpointtype == SP_SINGULAR) {
dReal bestsstep = 0.1;
dReal bestslope = 0;
dReal bestscore = INF;
const dReal magicconst = 3.1; // what is this??
const dReal slopethresh = 0.1;
const dReal stepthresh = 0.04; /// slopes within 0.02 of the singularity are also pretty big, so need longer to stabilize them.
dReal sstep = 1e-3/magicconst;
while(sstep <= stepthresh) {
sstep = sstep * magicconst;
for(int i = 0; i<int(switchpoint.slopesvector.size()); i++) {
sforward = std::min(s + sstep,constraints.trajectory.duration);
sbackward = std::max(s - sstep,0.);
dReal slope = switchpoint.slopesvector[i];
// Avoid too big stubs
if(std::abs(slope*sstep)>slopethresh) {
continue;
}
sdforward = sd + (sforward-s)*slope;
sdbackward = sd - (s-sbackward)*slope;
bool canintegrate = false;
int ret = IntegrateBackward(constraints,sbackward,sdbackward,dt,resprofile,constraints.passswitchpointnsteps);
if(!(ret==INT_MAXSTEPS||ret==INT_END)) {
// need to try smaller discretization in case slope has noise
ret = IntegrateBackward(constraints,sbackward,sdbackward,dt*0.1,resprofile,constraints.passswitchpointnsteps);
}
if(ret==INT_MAXSTEPS||ret==INT_END) {
ret = IntegrateForward(constraints,sforward,sdforward,dt,resprofile,constraints.passswitchpointnsteps,testaboveexistingprofiles, testmvc, zlajpah);
if(!(ret==INT_MAXSTEPS||ret==INT_END)) {
// need to try smaller discretization in case slope has noise
ret = IntegrateForward(constraints,sforward,sdforward,dt*0.1,resprofile,constraints.passswitchpointnsteps,testaboveexistingprofiles, testmvc, zlajpah);
}
if(ret==INT_MAXSTEPS||ret==INT_END) {
canintegrate = true;
}
}
if(!canintegrate) {
continue;
}
dReal alphabackward = constraints.SddLimits(sbackward,sdbackward).first/sd;
dReal betaforward = constraints.SddLimits(sforward,sdforward).second/sd;
dReal slopediff1 = std::abs(alphabackward-slope);
dReal slopediff2 = std::abs(betaforward-slope);
if(sdbackward > 0.01 && sdforward > 0.01) {
//dReal score = (slopediff1+slopediff2)/std::abs(std::log(sstep));
dReal score = slopediff1+slopediff2;
if(score<bestscore && score<10) {
bestsstep = sstep;
bestslope = slope;
bestscore = score;
}
}
}
}
sforward = std::min(s + bestsstep,constraints.trajectory.duration);
sbackward = std::max(s - bestsstep,0.);
sdforward = sd + (sforward-s)*bestslope;
sdbackward = sd - (s-sbackward)*bestslope;
constraints.nsingulartreated++;
return bestscore<INF;
}
// Tangent, Discontinuous and Zlajpah switchpoints
else{
dReal sdtop = -1;
dReal testsd = sd;
for(int itry = 0; itry < 5; ++itry) {
testsd *= constraints.loweringcoef;
sdtop = BisectionSearch(constraints,s,testsd,sd,constraints.integrationtimestep,0);
if(sdtop>0) {
break;
}
}
if(sdtop<=0) {