39 from __future__
import print_function
42 from functools
import partial
44 from ConstrainedPlanningCommon
import *
53 def __init__(self, outer, inner, maze):
54 super(TorusConstraint, self).__init__(3, 1)
58 self.
ppm.loadFile(maze)
60 def getStartAndGoalStates(self):
61 h = self.
ppm.getHeight()
62 w = self.
ppm.getWidth()
66 p = np.array([x / (w - 1.), y / (h - 1.)], dtype=np.float64)
67 c = self.
ppm.getPixel(x, y)
68 if c.red == 255
and c.blue == 0
and c.green == 0:
70 elif c.green == 255
and c.blue == 0
and c.red == 0:
74 def function(self, x, out):
75 c = np.array([x[0], x[1], 0])
76 nrm = math.sqrt(x[0] * x[0] + x[1] * x[1])
77 if not np.isfinite(nrm)
or nrm == 0:
79 out[0] = np.linalg.norm(x - self.
outer * c / nrm) - self.
inner
81 def jacobian(self, x, out):
82 xySquaredNorm = x[0] * x[0] + x[1] * x[1]
83 xyNorm = math.sqrt(xySquaredNorm)
84 denom = math.sqrt(x[2] * x[2] + (xyNorm - self.
outer)
85 * (xyNorm - self.
outer))
86 c = (xyNorm - self.
outer) * (xyNorm * xySquaredNorm) / \
87 (xySquaredNorm * xySquaredNorm * denom)
88 out[0, :] = [x[0] * c, x[1] * c, x[2] / denom]
90 def ambientToMaze(self, x):
91 nrm = math.sqrt(x[0] * x[0] + x[1] * x[1])
92 h = self.
ppm.getHeight()
93 w = self.
ppm.getWidth()
95 mx = math.atan2(x[2], nrm - self.
outer) / PI2
98 my = math.atan2(x[1], x[0]) / PI2
103 def mazeToAmbient(self, x):
105 b = [math.cos(a[0]), 0, math.sin(a[0])] * self.
inner
108 norm = math.sqrt(b[0] * b[0] + b[1] * b[1])
109 out = np.array([math.cos(a[1]), math.sin(a[1]), 0], dtype=np.float64)
114 def mazePixel(self, x):
115 h = self.
ppm.getHeight()
116 w = self.
ppm.getWidth()
118 if x[0] < 0
or x[0] >= w
or x[1] < 0
or x[1] >= h:
121 c = self.
ppm.getPixel(int(x[0]), int(x[1]))
122 return not (c.red == 0
and c.blue == 0
and c.green == 0)
124 def isValid(self, state):
128 def torusPlanningBench(cp, planners):
130 cp.setupBenchmark(planners,
"torus")
134 def torusPlanningOnce(cp, planner, output):
135 cp.setPlanner(planner)
138 stat = cp.solveOnce(output,
"torus")
141 ou.OMPL_INFORM(
"Dumping problem information to `torus_info.txt`.")
142 with open(
"torus_info.txt",
"w")
as infofile:
143 print(cp.spaceType, file=infofile)
148 cp.dumpGraph(
"torus")
153 def torusPlanning(options):
158 bounds.setLow(-(options.outer + options.inner))
159 bounds.setHigh(options.outer + options.inner)
161 rvss.setBounds(bounds)
164 constraint =
TorusConstraint(options.outer, options.inner, options.maze)
168 start, goal = constraint.getStartAndGoalStates()
169 print(
"Start = ", start)
170 print(
"Goal = ", goal)
177 cp.setStartAndGoalStates(sstart, sgoal)
179 TorusConstraint.isValid, constraint)))
181 planners = options.planner.split(
",")
182 if not options.bench:
183 torusPlanningOnce(cp, planners[0], options.output)
185 torusPlanningBench(cp, planners)
187 if __name__ ==
"__main__":
188 defaultMaze = join(join(dirname(__file__),
"mazes"),
"normal.ppm")
189 parser = argparse.ArgumentParser()
190 parser.add_argument(
"-o",
"--output", action=
"store_true",
191 help=
"Dump found solution path (if one exists) in plain text and planning "
192 "graph in GraphML to `torus_path.txt` and `torus_graph.graphml` "
194 parser.add_argument(
"--bench", action=
"store_true",
195 help=
"Do benchmarking on provided planner list.")
196 parser.add_argument(
"--outer", type=float, default=2,
197 help=
"Outer radius of torus.")
198 parser.add_argument(
"--inner", type=float, default=1,
199 help=
"Inner radius of torus.")
200 parser.add_argument(
"--maze", default=defaultMaze,
201 help=
"Filename of maze image (in .ppm format) to use as obstacles on the "
202 "surface of the torus.")
203 addSpaceOption(parser)
204 addPlannerOption(parser)
205 addConstrainedOptions(parser)
206 addAtlasOptions(parser)
208 torusPlanning(parser.parse_args())