Loading...
Searching...
No Matches
ThunderLightning.cpp
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Mark Moll, Dave Coleman, Ioan Sucan */
36
37#include <ompl/base/spaces/RealVectorStateSpace.h>
38#include <ompl/tools/thunder/Thunder.h>
39#include <ompl/tools/lightning/Lightning.h>
40#include <ompl/util/PPM.h>
41
42#include <ompl/config.h>
43
44#include <filesystem>
45#include <iostream>
46
47namespace ob = ompl::base;
48namespace og = ompl::geometric;
49namespace ot = ompl::tools;
50
51class Plane2DEnvironment
52{
53public:
54 Plane2DEnvironment(const char *ppm_file, bool useThunder = true)
55 {
56 bool ok = false;
57 try
58 {
59 ppm_.loadFile(ppm_file);
60 ok = true;
61 }
62 catch (ompl::Exception &ex)
63 {
64 OMPL_ERROR("Unable to load %s.\n%s", ppm_file, ex.what());
65 }
66 if (ok)
67 {
68 auto space(std::make_shared<ob::RealVectorStateSpace>());
69 space->addDimension(0.0, ppm_.getWidth());
70 space->addDimension(0.0, ppm_.getHeight());
71 maxWidth_ = ppm_.getWidth() - 1;
72 maxHeight_ = ppm_.getHeight() - 1;
73 if (useThunder)
74 {
75 expPlanner_ = std::make_shared<ot::Thunder>(space);
76 expPlanner_->setFilePath("thunder.db");
77 }
78 else
79 {
80 expPlanner_ = std::make_shared<ot::Lightning>(space);
81 expPlanner_->setFilePath("lightning.db");
82 }
83 // set state validity checking for this space
84 expPlanner_->setStateValidityChecker([this](const ob::State *state) { return isStateValid(state); });
85 space->setup();
86 expPlanner_->getSpaceInformation()->setStateValidityCheckingResolution(1.0 / space->getMaximumExtent());
87 vss_ = expPlanner_->getSpaceInformation()->allocValidStateSampler();
88
89 // DTC
90 // experience_setup_->setPlanner(std::make_shared<og::RRTConnect>(si_));
91 // Set the repair planner
92 // experience_setup_->setRepairPlanner(std::make_shared<og::RRTConnect>(si_));
93 }
94 }
95
96 ~Plane2DEnvironment()
97 {
98 expPlanner_->save();
99 }
100
101 bool plan()
102 {
103 std::cout << std::endl;
104 std::cout << "-------------------------------------------------------" << std::endl;
105 std::cout << "-------------------------------------------------------" << std::endl;
106
107 if (!expPlanner_)
108 {
109 OMPL_ERROR("Simple setup not loaded");
110 return false;
111 }
112 expPlanner_->clear();
113
114 ob::ScopedState<> start(expPlanner_->getStateSpace());
115 vss_->sample(start.get());
116 ob::ScopedState<> goal(expPlanner_->getStateSpace());
117 vss_->sample(goal.get());
118 expPlanner_->setStartAndGoalStates(start, goal);
119
120 bool solved = expPlanner_->solve(10.);
121 if (solved)
122 OMPL_INFORM("Found solution in %g seconds", expPlanner_->getLastPlanComputationTime());
123 else
124 OMPL_INFORM("No solution found");
125
126 expPlanner_->doPostProcessing();
127
128 return false;
129 }
130
131private:
132 bool isStateValid(const ob::State *state) const
133 {
134 const int w = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[0], maxWidth_);
135 const int h = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[1], maxHeight_);
136
137 const ompl::PPM::Color &c = ppm_.getPixel(h, w);
138 return c.red > 127 && c.green > 127 && c.blue > 127;
139 }
140
141 ot::ExperienceSetupPtr expPlanner_;
142 ob::ValidStateSamplerPtr vss_;
143 int maxWidth_;
144 int maxHeight_;
145 ompl::PPM ppm_;
146};
147
148int main(int argc, char **)
149{
150 std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
151
152 std::filesystem::path path(TEST_RESOURCES_DIR);
153 Plane2DEnvironment env((path / "ppm" / "floor.ppm").string().c_str(), argc == 1);
154
155 for (unsigned int i = 0; i < 100; ++i)
156 env.plan();
157
158 return 0;
159}
ompl::base::State StateType
Define the type of state allocated by this space.
Definition StateSpace.h:78
const T * as() const
Cast this instance to a desired type.
Definition State.h:66
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition Console.h:68
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition Console.h:64
This namespace contains sampling based planning routines shared by both planning under geometric cons...
This namespace contains code that is specific to planning under geometric constraints.
Includes various tools such as self config, benchmarking, etc.