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 #include <../tests/resources/config.h>
44 
45 #include <boost/filesystem.hpp>
46 #include <iostream>
47 
48 namespace ob = ompl::base;
49 namespace og = ompl::geometric;
50 namespace ot = ompl::tools;
51 
52 class Plane2DEnvironment
53 {
54 public:
55 
56  Plane2DEnvironment(const char *ppm_file, bool useThunder = true)
57  {
58  bool ok = false;
59  try
60  {
61  ppm_.loadFile(ppm_file);
62  ok = true;
63  }
64  catch(ompl::Exception &ex)
65  {
66  OMPL_ERROR("Unable to load %s.\n%s", ppm_file, ex.what());
67  }
68  if (ok)
69  {
71  space->addDimension(0.0, ppm_.getWidth());
72  space->addDimension(0.0, ppm_.getHeight());
73  maxWidth_ = ppm_.getWidth() - 1;
74  maxHeight_ = ppm_.getHeight() - 1;
75  if (useThunder)
76  {
77  expPlanner_.reset(new ot::Thunder(ob::StateSpacePtr(space)));
78  expPlanner_->setFilePath("thunder.db");
79  }
80  else
81  {
82  expPlanner_.reset(new ot::Lightning(ob::StateSpacePtr(space)));
83  expPlanner_->setFilePath("lightning.db");
84  }
85  // set state validity checking for this space
86  expPlanner_->setStateValidityChecker(std::bind(&Plane2DEnvironment::isStateValid, this, std::placeholders::_1));
87  space->setup();
88  expPlanner_->getSpaceInformation()->setStateValidityCheckingResolution(1.0 / space->getMaximumExtent());
89  vss_ = expPlanner_->getSpaceInformation()->allocValidStateSampler();
90 
91  // DTC
92  //experience_setup_->setPlanner(ob::PlannerPtr(new og::RRTConnect( si_ )));
93  // Set the repair planner
94  // std::shared_ptr<og::RRTConnect> repair_planner( new og::RRTConnect( si_ ) );
95  // experience_setup_->setRepairPlanner(ob::PlannerPtr( repair_planner ));
96  }
97  }
98 
99  ~Plane2DEnvironment()
100  {
101  expPlanner_->save();
102  }
103 
104  bool plan()
105  {
106  std::cout << std::endl;
107  std::cout << "-------------------------------------------------------" << std::endl;
108  std::cout << "-------------------------------------------------------" << std::endl;
109 
110  if (!expPlanner_)
111  {
112  OMPL_ERROR("Simple setup not loaded");
113  return false;
114  }
115  expPlanner_->clear();
116 
117  ob::ScopedState<> start(expPlanner_->getStateSpace());
118  vss_->sample(start.get());
119  ob::ScopedState<> goal(expPlanner_->getStateSpace());
120  vss_->sample(goal.get());
121  expPlanner_->setStartAndGoalStates(start, goal);
122 
123  bool solved = expPlanner_->solve(10.);
124  if (solved)
125  OMPL_INFORM("Found solution in %g seconds",
126  expPlanner_->getLastPlanComputationTime());
127  else
128  OMPL_INFORM("No solution found");
129 
130  expPlanner_->doPostProcessing();
131 
132  return false;
133  }
134 
135 private:
136 
137  bool isStateValid(const ob::State *state) const
138  {
139  const int w = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[0], maxWidth_);
140  const int h = std::min((int)state->as<ob::RealVectorStateSpace::StateType>()->values[1], maxHeight_);
141 
142  const ompl::PPM::Color &c = ppm_.getPixel(h, w);
143  return c.red > 127 && c.green > 127 && c.blue > 127;
144  }
145 
146  ot::ExperienceSetupPtr expPlanner_;
148  int maxWidth_;
149  int maxHeight_;
150  ompl::PPM ppm_;
151 };
152 
153 int main(int argc, char *argv[])
154 {
155  std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
156 
157  boost::filesystem::path path(TEST_RESOURCES_DIR);
158  Plane2DEnvironment env((path / "ppm" / "floor.ppm").string().c_str(), argc==1);
159 
160  for (unsigned int i = 0; i < 100; ++i)
161  env.plan();
162 
163  return 0;
164 }
Load and save .ppm files - "portable pixmap format" an image file formats designed to be easily excha...
Definition: PPM.h:47
A shared pointer wrapper for ompl::base::ValidStateSampler.
Definition of a scoped state.
Definition: ScopedState.h:56
A shared pointer wrapper for ompl::base::StateSpace.
Built off of SimpleSetup but provides support for planning from experience.
Definition: Thunder.h:85
virtual double getMaximumExtent() const
Get the maximum value a call to distance() can return (or an upper bound). For unbounded state spaces...
State StateType
Define the type of state allocated by this space.
Definition: StateSpace.h:80
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
Definition: Console.h:64
A state space representing Rn. The distance function is the L2 norm.
Definition of an abstract state.
Definition: State.h:50
The exception type for ompl.
Definition: Exception.h:47
Built off of SimpleSetup but provides support for planning from experience.
Definition: Lightning.h:98
void addDimension(double minBound=0.0, double maxBound=0.0)
Increase the dimensionality of the state space by 1. Optionally, bounds can be specified for this add...
const T * as() const
Cast this instance to a desired type.
Definition: State.h:74
virtual void setup()
Perform final setup steps. This function is automatically called by the SpaceInformation. If any default projections are to be registered, this call will set them and call their setup() functions. It is safe to call this function multiple times. At a subsequent call, projections that have been previously user configured are not re-instantiated, but their setup() method is still called.
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition: Console.h:68