-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateSpace.cpp
More file actions
1635 lines (1439 loc) · 58 KB
/
StateSpace.cpp
File metadata and controls
1635 lines (1439 loc) · 58 KB
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
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2010, Rice University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Rice University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#include "ompl/base/StateSpace.h"
#include "ompl/util/Exception.h"
#include "ompl/tools/config/MagicConstants.h"
#include "ompl/base/spaces/RealVectorStateSpace.h"
#include <mutex>
#include <boost/scoped_ptr.hpp>
#include <numeric>
#include <limits>
#include <queue>
#include <cmath>
#include <list>
#include <set>
const std::string ompl::base::StateSpace::DEFAULT_PROJECTION_NAME = "";
/// @cond IGNORE
namespace ompl
{
namespace base
{
namespace
{
struct AllocatedSpaces
{
AllocatedSpaces() = default;
std::list<StateSpace *> list_;
std::mutex lock_;
unsigned int counter_{0};
};
static boost::scoped_ptr<AllocatedSpaces> g_allocatedSpaces;
static std::once_flag g_once;
void initAllocatedSpaces()
{
g_allocatedSpaces.reset(new AllocatedSpaces);
}
AllocatedSpaces &getAllocatedSpaces()
{
std::call_once(g_once, &initAllocatedSpaces);
return *g_allocatedSpaces;
}
} // namespace
}
}
/// @endcond
ompl::base::StateSpace::StateSpace()
{
AllocatedSpaces &as = getAllocatedSpaces();
std::lock_guard<std::mutex> smLock(as.lock_);
// autocompute a unique name
name_ = "Space" + std::to_string(as.counter_++);
longestValidSegment_ = 0.0;
longestValidSegmentFraction_ = 0.01; // 1%
longestValidSegmentCountFactor_ = 1;
type_ = STATE_SPACE_UNKNOWN;
maxExtent_ = std::numeric_limits<double>::infinity();
params_.declareParam<double>("longest_valid_segment_fraction",
[this](double segmentFraction) { setLongestValidSegmentFraction(segmentFraction); },
[this] { return getLongestValidSegmentFraction(); });
params_.declareParam<unsigned int>("valid_segment_count_factor",
[this](unsigned int factor) { setValidSegmentCountFactor(factor); },
[this] { return getValidSegmentCountFactor(); });
as.list_.push_back(this);
}
ompl::base::StateSpace::~StateSpace()
{
AllocatedSpaces &as = getAllocatedSpaces();
std::lock_guard<std::mutex> smLock(as.lock_);
as.list_.remove(this);
}
/// @cond IGNORE
namespace ompl
{
namespace base
{
static void computeStateSpaceSignatureHelper(const StateSpace *space, std::vector<int> &signature)
{
signature.push_back(space->getType());
signature.push_back(space->getDimension());
if (space->isCompound())
{
unsigned int c = space->as<CompoundStateSpace>()->getSubspaceCount();
for (unsigned int i = 0; i < c; ++i)
computeStateSpaceSignatureHelper(space->as<CompoundStateSpace>()->getSubspace(i).get(), signature);
}
}
void computeLocationsHelper(const StateSpace *s,
std::map<std::string, StateSpace::SubstateLocation> &substateMap,
std::vector<StateSpace::ValueLocation> &locationsArray,
std::map<std::string, StateSpace::ValueLocation> &locationsMap,
StateSpace::ValueLocation loc)
{
loc.stateLocation.space = s;
substateMap[s->getName()] = loc.stateLocation;
State *test = s->allocState();
if (s->getValueAddressAtIndex(test, 0) != nullptr)
{
loc.index = 0;
locationsMap[s->getName()] = loc;
// if the space is compound, we will find this value again in the first subspace
if (!s->isCompound())
{
if (s->getType() == base::STATE_SPACE_REAL_VECTOR)
{
const std::string &name = s->as<base::RealVectorStateSpace>()->getDimensionName(0);
if (!name.empty())
locationsMap[name] = loc;
}
locationsArray.push_back(loc);
while (s->getValueAddressAtIndex(test, ++loc.index) != nullptr)
{
if (s->getType() == base::STATE_SPACE_REAL_VECTOR)
{
const std::string &name = s->as<base::RealVectorStateSpace>()->getDimensionName(loc.index);
if (!name.empty())
locationsMap[name] = loc;
}
locationsArray.push_back(loc);
}
}
}
s->freeState(test);
if (s->isCompound())
for (unsigned int i = 0; i < s->as<base::CompoundStateSpace>()->getSubspaceCount(); ++i)
{
loc.stateLocation.chain.push_back(i);
computeLocationsHelper(s->as<base::CompoundStateSpace>()->getSubspace(i).get(), substateMap,
locationsArray, locationsMap, loc);
loc.stateLocation.chain.pop_back();
}
}
void computeLocationsHelper(const StateSpace *s,
std::map<std::string, StateSpace::SubstateLocation> &substateMap,
std::vector<StateSpace::ValueLocation> &locationsArray,
std::map<std::string, StateSpace::ValueLocation> &locationsMap)
{
substateMap.clear();
locationsArray.clear();
locationsMap.clear();
computeLocationsHelper(s, substateMap, locationsArray, locationsMap, StateSpace::ValueLocation());
}
}
}
/// @endcond
const std::string &ompl::base::StateSpace::getName() const
{
return name_;
}
void ompl::base::StateSpace::setName(const std::string &name)
{
name_ = name;
// we don't want to call this function during the state space construction because calls to virtual functions are
// made,
// so we check if any values were previously inserted as value locations;
// if none were, then we either have none (so no need to call this function again)
// or setup() was not yet called
if (!valueLocationsInOrder_.empty())
computeLocationsHelper(this, substateLocationsByName_, valueLocationsInOrder_, valueLocationsByName_);
}
void ompl::base::StateSpace::computeLocations()
{
computeLocationsHelper(this, substateLocationsByName_, valueLocationsInOrder_, valueLocationsByName_);
}
void ompl::base::StateSpace::computeSignature(std::vector<int> &signature) const
{
signature.clear();
computeStateSpaceSignatureHelper(this, signature);
signature.insert(signature.begin(), signature.size());
}
ompl::base::State *ompl::base::StateSpace::cloneState(const State *source) const
{
State *copy = allocState();
copyState(copy, source);
return copy;
}
void ompl::base::StateSpace::registerProjections()
{
}
void ompl::base::StateSpace::setup()
{
maxExtent_ = getMaximumExtent();
longestValidSegment_ = maxExtent_ * longestValidSegmentFraction_;
if (longestValidSegment_ < std::numeric_limits<double>::epsilon())
{
std::stringstream error;
error << "The longest valid segment for state space " + getName() + " must be positive." << std::endl;
error << "Space settings:" << std::endl;
printSettings(error);
throw Exception(error.str());
}
computeLocationsHelper(this, substateLocationsByName_, valueLocationsInOrder_, valueLocationsByName_);
// make sure we don't overwrite projections that have been configured by the user
std::map<std::string, ProjectionEvaluatorPtr> oldProjections = projections_;
registerProjections();
for (auto &oldProjection : oldProjections)
if (oldProjection.second->userConfigured())
{
auto o = projections_.find(oldProjection.first);
if (o != projections_.end())
if (!o->second->userConfigured())
projections_[oldProjection.first] = oldProjection.second;
}
// remove previously set parameters for projections
std::vector<std::string> pnames;
params_.getParamNames(pnames);
for (const auto &pname : pnames)
if (pname.substr(0, 11) == "projection.")
params_.remove(pname);
// setup projections and add their parameters
for (const auto &projection : projections_)
{
projection.second->setup();
if (projection.first == DEFAULT_PROJECTION_NAME)
params_.include(projection.second->params(), "projection");
else
params_.include(projection.second->params(), "projection." + projection.first);
}
}
const std::map<std::string, ompl::base::StateSpace::SubstateLocation> &
ompl::base::StateSpace::getSubstateLocationsByName() const
{
return substateLocationsByName_;
}
ompl::base::State *ompl::base::StateSpace::getSubstateAtLocation(State *state, const SubstateLocation &loc) const
{
std::size_t index = 0;
while (loc.chain.size() > index)
state = state->as<CompoundState>()->components[loc.chain[index++]];
return state;
}
const ompl::base::State *ompl::base::StateSpace::getSubstateAtLocation(const State *state,
const SubstateLocation &loc) const
{
std::size_t index = 0;
while (loc.chain.size() > index)
state = state->as<CompoundState>()->components[loc.chain[index++]];
return state;
}
double *ompl::base::StateSpace::getValueAddressAtIndex(State * /*state*/, const unsigned int /*index*/) const
{
return nullptr;
}
const double *ompl::base::StateSpace::getValueAddressAtIndex(const State *state, const unsigned int index) const
{
double *val = getValueAddressAtIndex(const_cast<State *>(state),
index); // this const-cast does not hurt, since the state is not modified
return val;
}
const std::vector<ompl::base::StateSpace::ValueLocation> &ompl::base::StateSpace::getValueLocations() const
{
return valueLocationsInOrder_;
}
const std::map<std::string, ompl::base::StateSpace::ValueLocation> &
ompl::base::StateSpace::getValueLocationsByName() const
{
return valueLocationsByName_;
}
void ompl::base::StateSpace::copyToReals(std::vector<double> &reals, const State *source) const
{
const auto &locations = getValueLocations();
reals.resize(locations.size());
for (std::size_t i = 0; i < locations.size(); ++i)
reals[i] = *getValueAddressAtLocation(source, locations[i]);
}
void ompl::base::StateSpace::copyFromReals(State *destination, const std::vector<double> &reals) const
{
const auto &locations = getValueLocations();
assert(reals.size() == locations.size());
for (std::size_t i = 0; i < reals.size(); ++i)
*getValueAddressAtLocation(destination, locations[i]) = reals[i];
}
double *ompl::base::StateSpace::getValueAddressAtLocation(State *state, const ValueLocation &loc) const
{
std::size_t index = 0;
while (loc.stateLocation.chain.size() > index)
state = state->as<CompoundState>()->components[loc.stateLocation.chain[index++]];
return loc.stateLocation.space->getValueAddressAtIndex(state, loc.index);
}
const double *ompl::base::StateSpace::getValueAddressAtLocation(const State *state, const ValueLocation &loc) const
{
std::size_t index = 0;
while (loc.stateLocation.chain.size() > index)
state = state->as<CompoundState>()->components[loc.stateLocation.chain[index++]];
return loc.stateLocation.space->getValueAddressAtIndex(state, loc.index);
}
double *ompl::base::StateSpace::getValueAddressAtName(State *state, const std::string &name) const
{
const auto &locations = getValueLocationsByName();
auto it = locations.find(name);
return (it != locations.end()) ? getValueAddressAtLocation(state, it->second) : nullptr;
}
const double *ompl::base::StateSpace::getValueAddressAtName(const State *state, const std::string &name) const
{
const auto &locations = getValueLocationsByName();
auto it = locations.find(name);
return (it != locations.end()) ? getValueAddressAtLocation(state, it->second) : nullptr;
}
unsigned int ompl::base::StateSpace::getSerializationLength() const
{
return 0;
}
void ompl::base::StateSpace::serialize(void * /*serialization*/, const State * /*state*/) const
{
}
void ompl::base::StateSpace::deserialize(State * /*state*/, const void * /*serialization*/) const
{
}
void ompl::base::StateSpace::printState(const State *state, std::ostream &out) const
{
out << "State instance [" << state << ']' << std::endl;
}
void ompl::base::StateSpace::printSettings(std::ostream &out) const
{
out << "StateSpace '" << getName() << "' instance: " << this << std::endl;
printProjections(out);
}
void ompl::base::StateSpace::printProjections(std::ostream &out) const
{
if (projections_.empty())
out << "No registered projections" << std::endl;
else
{
out << "Registered projections:" << std::endl;
for (const auto &projection : projections_)
{
out << " - ";
if (projection.first == DEFAULT_PROJECTION_NAME)
out << "<default>";
else
out << projection.first;
out << std::endl;
projection.second->printSettings(out);
}
}
}
/// @cond IGNORE
namespace ompl
{
namespace base
{
static bool StateSpaceIncludes(const StateSpace *self, const StateSpace *other)
{
std::queue<const StateSpace *> q;
q.push(self);
while (!q.empty())
{
const StateSpace *m = q.front();
q.pop();
if (m->getName() == other->getName())
return true;
if (m->isCompound())
{
unsigned int c = m->as<CompoundStateSpace>()->getSubspaceCount();
for (unsigned int i = 0; i < c; ++i)
q.push(m->as<CompoundStateSpace>()->getSubspace(i).get());
}
}
return false;
}
static bool StateSpaceCovers(const StateSpace *self, const StateSpace *other)
{
if (StateSpaceIncludes(self, other))
return true;
else if (other->isCompound())
{
unsigned int c = other->as<CompoundStateSpace>()->getSubspaceCount();
for (unsigned int i = 0; i < c; ++i)
if (!StateSpaceCovers(self, other->as<CompoundStateSpace>()->getSubspace(i).get()))
return false;
return true;
}
return false;
}
struct CompareSubstateLocation
{
bool operator()(const StateSpace::SubstateLocation &a, const StateSpace::SubstateLocation &b) const
{
if (a.space->getDimension() != b.space->getDimension())
return a.space->getDimension() > b.space->getDimension();
return a.space->getName() > b.space->getName();
}
};
}
}
/// @endcond
bool ompl::base::StateSpace::covers(const StateSpacePtr &other) const
{
return StateSpaceCovers(this, other.get());
}
bool ompl::base::StateSpace::includes(const StateSpacePtr &other) const
{
return StateSpaceIncludes(this, other.get());
}
bool ompl::base::StateSpace::covers(const StateSpace *other) const
{
return StateSpaceCovers(this, other);
}
bool ompl::base::StateSpace::includes(const StateSpace *other) const
{
return StateSpaceIncludes(this, other);
}
void ompl::base::StateSpace::getCommonSubspaces(const StateSpacePtr &other, std::vector<std::string> &subspaces) const
{
getCommonSubspaces(other.get(), subspaces);
}
void ompl::base::StateSpace::getCommonSubspaces(const StateSpace *other, std::vector<std::string> &subspaces) const
{
std::set<StateSpace::SubstateLocation, CompareSubstateLocation> intersection;
const std::map<std::string, StateSpace::SubstateLocation> &S = other->getSubstateLocationsByName();
for (const auto &it : substateLocationsByName_)
{
if (S.find(it.first) != S.end())
intersection.insert(it.second);
}
bool found = true;
while (found)
{
found = false;
for (auto it = intersection.begin(); it != intersection.end(); ++it)
for (auto jt = intersection.begin(); jt != intersection.end(); ++jt)
if (it != jt)
if (StateSpaceCovers(it->space, jt->space))
{
intersection.erase(jt);
found = true;
break;
}
}
subspaces.clear();
for (const auto &it : intersection)
subspaces.push_back(it.space->getName());
}
void ompl::base::StateSpace::List(std::ostream &out)
{
AllocatedSpaces &as = getAllocatedSpaces();
std::lock_guard<std::mutex> smLock(as.lock_);
for (auto &it : as.list_)
out << "@ " << it << ": " << it->getName() << std::endl;
}
void ompl::base::StateSpace::list(std::ostream &out) const
{
std::queue<const StateSpace *> q;
q.push(this);
while (!q.empty())
{
const StateSpace *m = q.front();
q.pop();
out << "@ " << m << ": " << m->getName() << std::endl;
if (m->isCompound())
{
unsigned int c = m->as<CompoundStateSpace>()->getSubspaceCount();
for (unsigned int i = 0; i < c; ++i)
q.push(m->as<CompoundStateSpace>()->getSubspace(i).get());
}
}
}
void ompl::base::StateSpace::diagram(std::ostream &out) const
{
out << "digraph StateSpace {" << std::endl;
out << '"' << getName() << '"' << std::endl;
std::queue<const StateSpace *> q;
q.push(this);
while (!q.empty())
{
const StateSpace *m = q.front();
q.pop();
if (m->isCompound())
{
unsigned int c = m->as<CompoundStateSpace>()->getSubspaceCount();
for (unsigned int i = 0; i < c; ++i)
{
const StateSpace *s = m->as<CompoundStateSpace>()->getSubspace(i).get();
q.push(s);
out << '"' << m->getName() << R"(" -> ")" << s->getName() << R"(" [label=")"
<< std::to_string(m->as<CompoundStateSpace>()->getSubspaceWeight(i)) << R"("];)" << std::endl;
}
}
}
out << '}' << std::endl;
}
void ompl::base::StateSpace::Diagram(std::ostream &out)
{
AllocatedSpaces &as = getAllocatedSpaces();
std::lock_guard<std::mutex> smLock(as.lock_);
out << "digraph StateSpaces {" << std::endl;
for (auto it = as.list_.begin(); it != as.list_.end(); ++it)
{
out << '"' << (*it)->getName() << '"' << std::endl;
for (auto jt = as.list_.begin(); jt != as.list_.end(); ++jt)
if (it != jt)
{
if ((*it)->isCompound() && (*it)->as<CompoundStateSpace>()->hasSubspace((*jt)->getName()))
out << '"' << (*it)->getName() << R"(" -> ")" << (*jt)->getName() << R"(" [label=")"
<< std::to_string((*it)->as<CompoundStateSpace>()->getSubspaceWeight((*jt)->getName())) <<
R"("];)" << std::endl;
else if (!StateSpaceIncludes(*it, *jt) && StateSpaceCovers(*it, *jt))
out << '"' << (*it)->getName() << R"(" -> ")" << (*jt)->getName() << R"(" [style=dashed];)"
<< std::endl;
}
}
out << '}' << std::endl;
}
void ompl::base::StateSpace::sanityChecks() const
{
unsigned int flags = isMetricSpace() ? ~0 : ~(STATESPACE_DISTANCE_SYMMETRIC | STATESPACE_TRIANGLE_INEQUALITY);
sanityChecks(std::numeric_limits<double>::epsilon(), std::numeric_limits<float>::epsilon(), flags);
}
void ompl::base::StateSpace::sanityChecks(double zero, double eps, unsigned int flags) const
{
{
double maxExt = getMaximumExtent();
State *s1 = allocState();
State *s2 = allocState();
StateSamplerPtr ss = allocStateSampler();
char *serialization = nullptr;
if ((flags & STATESPACE_SERIALIZATION) && getSerializationLength() > 0)
serialization = new char[getSerializationLength()];
for (unsigned int i = 0; i < magic::TEST_STATE_COUNT; ++i)
{
ss->sampleUniform(s1);
if (distance(s1, s1) > eps)
throw Exception("Distance from a state to itself should be 0");
if (!equalStates(s1, s1))
throw Exception("A state should be equal to itself");
if ((flags & STATESPACE_RESPECT_BOUNDS) && !satisfiesBounds(s1))
throw Exception("Sampled states should be within bounds");
copyState(s2, s1);
if (!equalStates(s1, s2))
throw Exception("Copy of a state is not the same as the original state. copyState() may not work "
"correctly.");
if (flags & STATESPACE_ENFORCE_BOUNDS_NO_OP)
{
enforceBounds(s1);
if (!equalStates(s1, s2))
throw Exception("enforceBounds() seems to modify states that are in fact within bounds.");
}
if (flags & STATESPACE_SERIALIZATION)
{
ss->sampleUniform(s2);
serialize(serialization, s1);
deserialize(s2, serialization);
if (!equalStates(s1, s2))
throw Exception("Serialization/deserialization operations do not seem to work as expected.");
}
ss->sampleUniform(s2);
if (!equalStates(s1, s2))
{
double d12 = distance(s1, s2);
if ((flags & STATESPACE_DISTANCE_DIFFERENT_STATES) && d12 < zero)
throw Exception("Distance between different states should be above 0");
double d21 = distance(s2, s1);
if ((flags & STATESPACE_DISTANCE_SYMMETRIC) && fabs(d12 - d21) > eps)
throw Exception("The distance function should be symmetric (A->B=" + std::to_string(d12) +
", B->A=" + std::to_string(d21) + ", difference is " +
std::to_string(fabs(d12 - d21)) + ")");
if (flags & STATESPACE_DISTANCE_BOUND)
if (d12 > maxExt + zero)
throw Exception("The distance function should not report values larger than the maximum extent "
"(" +
std::to_string(d12) + " > " + std::to_string(maxExt) + ")");
}
}
if (serialization)
delete[] serialization;
freeState(s1);
freeState(s2);
}
// Test that interpolation works as expected and also test triangle inequality
if (!isDiscrete() && !isHybrid() && (flags & (STATESPACE_INTERPOLATION | STATESPACE_TRIANGLE_INEQUALITY)))
{
State *s1 = allocState();
State *s2 = allocState();
State *s3 = allocState();
StateSamplerPtr ss = allocStateSampler();
for (unsigned int i = 0; i < magic::TEST_STATE_COUNT; ++i)
{
ss->sampleUniform(s1);
ss->sampleUniform(s2);
ss->sampleUniform(s3);
interpolate(s1, s2, 0.0, s3);
if ((flags & STATESPACE_INTERPOLATION) && distance(s1, s3) > eps)
throw Exception("Interpolation from a state at time 0 should be not change the original state");
interpolate(s1, s2, 1.0, s3);
if ((flags & STATESPACE_INTERPOLATION) && distance(s2, s3) > eps)
throw Exception("Interpolation to a state at time 1 should be the same as the final state");
interpolate(s1, s2, 0.5, s3);
double diff = distance(s1, s3) + distance(s3, s2) - distance(s1, s2);
if ((flags & STATESPACE_TRIANGLE_INEQUALITY) && fabs(diff) > eps)
throw Exception("Interpolation to midpoint state does not lead to distances that satisfy the triangle "
"inequality (" +
std::to_string(diff) + " difference)");
interpolate(s3, s2, 0.5, s3);
interpolate(s1, s2, 0.75, s2);
if ((flags & STATESPACE_INTERPOLATION) && distance(s2, s3) > eps)
throw Exception("Continued interpolation does not work as expected. Please also check that "
"interpolate() works with overlapping memory for its state arguments");
}
freeState(s1);
freeState(s2);
freeState(s3);
}
}
bool ompl::base::StateSpace::hasDefaultProjection() const
{
return hasProjection(DEFAULT_PROJECTION_NAME);
}
bool ompl::base::StateSpace::hasProjection(const std::string &name) const
{
return projections_.find(name) != projections_.end();
}
ompl::base::ProjectionEvaluatorPtr ompl::base::StateSpace::getDefaultProjection() const
{
if (hasDefaultProjection())
return getProjection(DEFAULT_PROJECTION_NAME);
else
{
OMPL_ERROR("No default projection is set. Perhaps setup() needs to be called");
return ProjectionEvaluatorPtr();
}
}
ompl::base::ProjectionEvaluatorPtr ompl::base::StateSpace::getProjection(const std::string &name) const
{
auto it = projections_.find(name);
if (it != projections_.end())
return it->second;
else
{
OMPL_ERROR("Projection '%s' is not defined", name.c_str());
return ProjectionEvaluatorPtr();
}
}
const std::map<std::string, ompl::base::ProjectionEvaluatorPtr> &
ompl::base::StateSpace::getRegisteredProjections() const
{
return projections_;
}
void ompl::base::StateSpace::registerDefaultProjection(const ProjectionEvaluatorPtr &projection)
{
registerProjection(DEFAULT_PROJECTION_NAME, projection);
}
void ompl::base::StateSpace::registerProjection(const std::string &name, const ProjectionEvaluatorPtr &projection)
{
if (projection)
projections_[name] = projection;
else
OMPL_ERROR("Attempting to register invalid projection under name '%s'. Ignoring.", name.c_str());
}
bool ompl::base::StateSpace::isCompound() const
{
return false;
}
bool ompl::base::StateSpace::isDiscrete() const
{
return false;
}
bool ompl::base::StateSpace::isHybrid() const
{
return false;
}
bool ompl::base::StateSpace::hasSymmetricDistance() const
{
return true;
}
bool ompl::base::StateSpace::hasSymmetricInterpolate() const
{
return true;
}
void ompl::base::StateSpace::setStateSamplerAllocator(const StateSamplerAllocator &ssa)
{
ssa_ = ssa;
}
void ompl::base::StateSpace::clearStateSamplerAllocator()
{
ssa_ = StateSamplerAllocator();
}
ompl::base::StateSamplerPtr ompl::base::StateSpace::allocStateSampler() const
{
if (ssa_)
return ssa_(this);
else
return allocDefaultStateSampler();
}
ompl::base::StateSamplerPtr ompl::base::StateSpace::allocSubspaceStateSampler(const StateSpacePtr &subspace) const
{
return allocSubspaceStateSampler(subspace.get());
}
ompl::base::StateSamplerPtr ompl::base::StateSpace::allocSubspaceStateSampler(const StateSpace *subspace) const
{
if (subspace->getName() == getName())
return allocStateSampler();
return std::make_shared<SubspaceStateSampler>(this, subspace, 1.0);
}
void ompl::base::StateSpace::setValidSegmentCountFactor(unsigned int factor)
{
if (factor < 1)
throw Exception("The multiplicative factor for the valid segment count between two states must be strictly "
"positive");
longestValidSegmentCountFactor_ = factor;
}
void ompl::base::StateSpace::setLongestValidSegmentFraction(double segmentFraction)
{
if (segmentFraction < std::numeric_limits<double>::epsilon() ||
segmentFraction > 1.0 - std::numeric_limits<double>::epsilon())
throw Exception("The fraction of the extent must be larger than 0 and less than 1");
longestValidSegmentFraction_ = segmentFraction;
}
unsigned int ompl::base::StateSpace::getValidSegmentCountFactor() const
{
return longestValidSegmentCountFactor_;
}
double ompl::base::StateSpace::getLongestValidSegmentFraction() const
{
return longestValidSegmentFraction_;
}
double ompl::base::StateSpace::getLongestValidSegmentLength() const
{
return longestValidSegment_;
}
unsigned int ompl::base::StateSpace::validSegmentCount(const State *state1, const State *state2) const
{
return longestValidSegmentCountFactor_ * (unsigned int)ceil(distance(state1, state2) / longestValidSegment_);
}
ompl::base::CompoundStateSpace::CompoundStateSpace()
{
setName("Compound" + getName());
}
ompl::base::CompoundStateSpace::CompoundStateSpace(const std::vector<StateSpacePtr> &components,
const std::vector<double> &weights)
: StateSpace(), componentCount_(0), weightSum_(0.0), locked_(false)
{
if (components.size() != weights.size())
throw Exception("Number of component spaces and weights are not the same");
setName("Compound" + getName());
for (unsigned int i = 0; i < components.size(); ++i)
addSubspace(components[i], weights[i]);
}
void ompl::base::CompoundStateSpace::addSubspace(const StateSpacePtr &component, double weight)
{
if (locked_)
throw Exception("This state space is locked. No further components can be added");
if (weight < 0.0)
throw Exception("Subspace weight cannot be negative");
components_.push_back(component);
weights_.push_back(weight);
weightSum_ += weight;
componentCount_ = components_.size();
}
bool ompl::base::CompoundStateSpace::isCompound() const
{
return true;
}
bool ompl::base::CompoundStateSpace::isHybrid() const
{
bool c = false;
bool d = false;
for (unsigned int i = 0; i < componentCount_; ++i)
{
if (components_[i]->isHybrid())
return true;
if (components_[i]->isDiscrete())
d = true;
else
c = true;
}
return c && d;
}
unsigned int ompl::base::CompoundStateSpace::getSubspaceCount() const
{
return componentCount_;
}
const ompl::base::StateSpacePtr &ompl::base::CompoundStateSpace::getSubspace(const unsigned int index) const
{
if (componentCount_ > index)
return components_[index];
else
throw Exception("Subspace index does not exist");
}
bool ompl::base::CompoundStateSpace::hasSubspace(const std::string &name) const
{
for (unsigned int i = 0; i < componentCount_; ++i)
if (components_[i]->getName() == name)
return true;
return false;
}
unsigned int ompl::base::CompoundStateSpace::getSubspaceIndex(const std::string &name) const
{
for (unsigned int i = 0; i < componentCount_; ++i)
if (components_[i]->getName() == name)
return i;
throw Exception("Subspace " + name + " does not exist");
}
const ompl::base::StateSpacePtr &ompl::base::CompoundStateSpace::getSubspace(const std::string &name) const
{
return components_[getSubspaceIndex(name)];
}
double ompl::base::CompoundStateSpace::getSubspaceWeight(const unsigned int index) const
{
if (componentCount_ > index)
return weights_[index];
else
throw Exception("Subspace index does not exist");
}
double ompl::base::CompoundStateSpace::getSubspaceWeight(const std::string &name) const
{
for (unsigned int i = 0; i < componentCount_; ++i)
if (components_[i]->getName() == name)
return weights_[i];
throw Exception("Subspace " + name + " does not exist");
}
void ompl::base::CompoundStateSpace::setSubspaceWeight(const unsigned int index, double weight)
{
if (weight < 0.0)
throw Exception("Subspace weight cannot be negative");
if (componentCount_ > index)
{
weightSum_ += weight - weights_[index];
weights_[index] = weight;
}
else
throw Exception("Subspace index does not exist");
}
void ompl::base::CompoundStateSpace::setSubspaceWeight(const std::string &name, double weight)
{
for (unsigned int i = 0; i < componentCount_; ++i)
if (components_[i]->getName() == name)
{
setSubspaceWeight(i, weight);
return;
}
throw Exception("Subspace " + name + " does not exist");
}
const std::vector<ompl::base::StateSpacePtr> &ompl::base::CompoundStateSpace::getSubspaces() const
{
return components_;
}
const std::vector<double> &ompl::base::CompoundStateSpace::getSubspaceWeights() const
{
return weights_;
}
unsigned int ompl::base::CompoundStateSpace::getDimension() const
{
unsigned int dim = 0;
for (unsigned int i = 0; i < componentCount_; ++i)
dim += components_[i]->getDimension();
return dim;
}
double ompl::base::CompoundStateSpace::getMaximumExtent() const
{
double e = 0.0;
for (unsigned int i = 0; i < componentCount_; ++i)
if (weights_[i] >= std::numeric_limits<double>::epsilon()) // avoid possible multiplication of 0 times infinity