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 <boost/filesystem.hpp>
45 #include <iostream>
46 
47 namespace ob = ompl::base;
48 namespace og = ompl::geometric;
49 namespace ot = ompl::tools;
50 
51 class Plane2DEnvironment
52 {
53 public:
54 
55  Plane2DEnvironment(const char *ppm_file, bool useThunder = true)
56  {
57  bool ok = false;
58  try
59  {
60  ppm_.loadFile(ppm_file);
61  ok = true;
62  }
63  catch(ompl::Exception &ex)
64  {
65  OMPL_ERROR("Unable to load %s.\n%s", ppm_file, ex.what());
66  }
67  if (ok)
68  {
69  auto space(std::make_shared<ob::RealVectorStateSpace>());
70  space->addDimension(0.0, ppm_.getWidth());
71  space->addDimension(0.0, ppm_.getHeight());
72  maxWidth_ = ppm_.getWidth() - 1;
73  maxHeight_ = ppm_.getHeight() - 1;
74  if (useThunder)
75  {
76  expPlanner_ = std::make_shared<ot::Thunder>(space);
77  expPlanner_->setFilePath("thunder.db");
78  }
79  else
80  {
81  expPlanner_ = std::make_shared<ot::Lightning>(space);
82  expPlanner_->setFilePath("lightning.db");
83  }
84  // set state validity checking for this space
85  expPlanner_->setStateValidityChecker([this](const ob::State *state)
86  { return isStateValid(state); });
87  space->setup();
88  expPlanner_->getSpaceInformation()->setStateValidityCheckingResolution(1.0 / space->getMaximumExtent());
89  vss_ = expPlanner_->getSpaceInformation()->allocValidStateSampler();
90 
91  // DTC
92  //experience_setup_->setPlanner(std::make_shared<og::RRTConnect>(si_));
93  // Set the repair planner
94  // experience_setup_->setRepairPlanner(std::make_shared<og::RRTConnect>(si_));
95  }
96  }
97 
98  ~Plane2DEnvironment()
99  {
100  expPlanner_->save();
101  }
102 
103  bool plan()
104  {
105  std::cout << std::endl;
106  std::cout << "-------------------------------------------------------" << std::endl;
107  std::cout << "-------------------------------------------------------" << std::endl;
108 
109  if (!expPlanner_)
110  {
111  OMPL_ERROR("Simple setup not loaded");
112  return false;
113  }
114  expPlanner_->clear();
115 
116  ob::ScopedState<> start(expPlanner_->getStateSpace());
117  vss_->sample(start.get());
118  ob::ScopedState<> goal(expPlanner_->getStateSpace());
119  vss_->sample(goal.get());
120  expPlanner_->setStartAndGoalStates(start, goal);
121 
122  bool solved = expPlanner_->solve(10.);
123  if (solved)
124  OMPL_INFORM("Found solution in %g seconds",
125  expPlanner_->getLastPlanComputationTime());
126  else
127  OMPL_INFORM("No solution found");
128 
129  expPlanner_->doPostProcessing();
130 
131  return false;
132  }
133 
134 private:
135 
136  bool isStateValid(const ob::State *state) const
137  {
138  const int w = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[0], maxWidth_);
139  const int h = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[1], maxHeight_);
140 
141  const ompl::PPM::Color &c = ppm_.getPixel(h, w);
142  return c.red > 127 && c.green > 127 && c.blue > 127;
143  }
144 
145  ot::ExperienceSetupPtr expPlanner_;
146  ob::ValidStateSamplerPtr vss_;
147  int maxWidth_;
148  int maxHeight_;
149  ompl::PPM ppm_;
150 };
151 
152 int main(int argc, char **)
153 {
154  std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
155 
156  boost::filesystem::path path(TEST_RESOURCES_DIR);
157  Plane2DEnvironment env((path / "ppm" / "floor.ppm").string().c_str(), argc==1);
158 
159  for (unsigned int i = 0; i < 100; ++i)
160  env.plan();
161 
162  return 0;
163 }
Includes various tools such as self config, benchmarking, etc.
This namespace contains code that is specific to planning under geometric constraints.
Definition: GeneticSearch.h:79
Definition of an abstract state.
Definition: State.h:113
This namespace contains sampling based planning routines shared by both planning under geometric cons...
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition: Console.h:68
const T * as() const
Cast this instance to a desired type.
Definition: State.h:162
ompl::base::State StateType
Define the type of state allocated by this space.
Definition: StateSpace.h:142
Load and save .ppm files - "portable pixmap format" an image file formats designed to be easily excha...
Definition: PPM.h:78
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
Definition of a scoped state.
Definition: ScopedState.h:120
The exception type for ompl.
Definition: Exception.h:78