GCC Code Coverage Report


Directory: ./
File: src/graph/guided-state-selector.cc
Date: 2025-03-07 11:10:46
Exec Total Coverage
Lines: 0 67 0.0%
Branches: 0 114 0.0%

Line Branch Exec Source
1 // Copyright (c) 2014, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // 1. Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 // DAMAGE.
28
29 #include "hpp/manipulation/graph/guided-state-selector.hh"
30
31 #include <cstdlib>
32 #include <hpp/core/steering-method.hh>
33 #include <hpp/pinocchio/configuration.hh>
34 #include <hpp/util/assertion.hh>
35
36 #include "../astar.hh"
37 #include "hpp/manipulation/roadmap-node.hh"
38 #include "hpp/manipulation/roadmap.hh"
39
40 namespace hpp {
41 namespace manipulation {
42 namespace graph {
43 GuidedStateSelectorPtr_t GuidedStateSelector::create(
44 const std::string& name, const core::RoadmapPtr_t& roadmap) {
45 GuidedStateSelector* ptr = new GuidedStateSelector(name, roadmap);
46 GuidedStateSelectorPtr_t shPtr(ptr);
47 ptr->init(shPtr);
48 return shPtr;
49 }
50
51 void GuidedStateSelector::init(const GuidedStateSelectorPtr_t& weak) {
52 StateSelector::init(weak);
53 wkPtr_ = weak;
54 }
55
56 void GuidedStateSelector::setStateList(const States_t& stateList) {
57 stateList_ = stateList;
58 }
59
60 EdgePtr_t GuidedStateSelector::chooseEdge(RoadmapNodePtr_t from) const {
61 if (stateList_.empty()) return StateSelector::chooseEdge(from);
62 Astar::States_t list;
63 bool reverse = false;
64 if (from->connectedComponent() ==
65 roadmap_->initNode()->connectedComponent()) {
66 Astar alg(roadmap_->distance(), wkPtr_.lock(),
67 static_cast<RoadmapNodePtr_t>(roadmap_->initNode()));
68 list = alg.solution(from);
69 } else {
70 core::NodeVector_t::const_iterator itg = roadmap_->goalNodes().begin();
71 for (; itg != roadmap_->goalNodes().end(); ++itg)
72 if ((*itg)->connectedComponent() == from->connectedComponent()) break;
73 if (itg == roadmap_->goalNodes().end()) {
74 hppDout(error,
75 "This configuration can reach neither the initial "
76 "configuration nor any of the goal configurations.");
77 return EdgePtr_t();
78 }
79 reverse = true;
80 Astar alg(roadmap_->distance(), wkPtr_.lock(), from);
81 list = alg.solution(static_cast<RoadmapNodePtr_t>(*itg));
82 }
83 list.erase(std::unique(list.begin(), list.end()), list.end());
84 // Check if the beginning of stateList is list
85 if (list.size() <= stateList_.size()) {
86 Neighbors_t nn;
87 if (reverse) {
88 States_t::const_reverse_iterator it1 = stateList_.rbegin();
89 Astar::States_t::const_reverse_iterator it2 = list.rbegin();
90 Astar::States_t::const_reverse_iterator itEnd2 = list.rend();
91 do {
92 if (*it1 != *it2) {
93 hppDout(error,
94 "The target sequence of nodes does not end with "
95 "the sequence of nodes to reach this configuration.");
96 return EdgePtr_t();
97 }
98 ++it1;
99 } while (++it2 != itEnd2);
100 StatePtr_t state = getState(from);
101 HPP_ASSERT(state == list.front());
102 const Neighbors_t& n = state->neighbors();
103 /// You stay in the same state
104 for (Neighbors_t::const_iterator it = n.begin(); it != n.end(); ++it)
105 if (it->second->stateTo() == state) nn.insert(it->second, it->first);
106 /// Go from state it1 to state
107 // The path will be build from state. So we must find an edge from
108 // state to it1, that will be reversely
109 for (Neighbors_t::const_iterator it = n.begin(); it != n.end(); ++it)
110 if (it->second->stateTo() == *it1) nn.insert(it->second, it->first);
111 } else {
112 States_t::const_iterator it1 = stateList_.begin();
113 Astar::States_t::const_iterator it2 = list.begin();
114 Astar::States_t::const_iterator itEnd2 = list.end();
115 do {
116 if (*it1 != *it2) {
117 hppDout(error,
118 "The target sequence of nodes does not start with "
119 "the sequence of nodes to reach this configuration.");
120 return EdgePtr_t();
121 }
122 ++it1;
123 } while (++it2 != itEnd2);
124 StatePtr_t state = getState(from);
125 HPP_ASSERT(state == list.back());
126 const Neighbors_t& n = state->neighbors();
127 for (Neighbors_t::const_iterator it = n.begin(); it != n.end(); ++it)
128 /// You stay in the same state
129 /// or go from state to state it1
130 if (it->second->stateTo() == state || it->second->stateTo() == *it1)
131 nn.insert(it->second, it->first);
132 }
133 if (nn.size() > 0 && nn.totalWeight() > 0) return nn();
134 hppDout(error,
135 "This state has no neighbors to get to an admissible states.");
136 }
137 return EdgePtr_t();
138 }
139
140 std::ostream& GuidedStateSelector::dotPrint(std::ostream& os,
141 dot::DrawingAttributes) const {
142 for (WeighedStates_t::const_iterator it = orderedStates_.begin();
143 orderedStates_.end() != it; ++it)
144 it->second->dotPrint(os);
145 return os;
146 }
147
148 std::ostream& GuidedStateSelector::print(std::ostream& os) const {
149 return StateSelector::print(os);
150 }
151 } // namespace graph
152 } // namespace manipulation
153 } // namespace hpp
154