File: KoulesPlayback.py

package info (click to toggle)
ompl 1.5.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 52,448 kB
  • sloc: cpp: 78,358; python: 4,904; sh: 228; xml: 24; javascript: 15; makefile: 11
file content (135 lines) | stat: -rwxr-xr-x 5,551 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3

######################################################################
# Software License Agreement (BSD License)
#
#  Copyright (c) 2010, Rice University
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above
#     copyright notice, this list of conditions and the following
#     disclaimer in the documentation and/or other materials provided
#     with the distribution.
#   * Neither the name of the Rice University nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
#  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
#  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
#  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
######################################################################

# Author: Beck Chen, Mark Moll

from sys import argv, stdout
from os.path import basename, splitext
from math import cos, sin, atan2, pi, ceil
import matplotlib.pyplot as plt
import matplotlib.animation as animation

targetFrameRate = 30 # desired number of frames per second
speedUp = 1.
# the parameters will be read from file
sideLength = 0
shipRadius = 0
kouleRadius = 0
propagationStepSize = 0
shipAcceleration = 0
shipRotVel = 0
shipDelta = 0
shipEps = 0

fig = plt.figure(figsize=(6, 6))
ax = plt.axes(xlim=(0, 1), ylim=(0, 1))
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
handle, = ax.plot([], [])
path = None

def normalizeAngle(theta):
    if theta < -pi:
        return theta + 2. * pi
    if theta > pi:
        return theta - 2. * pi
    return theta

def plotShip(x, u):
    pos = (x[0], x[1])
    theta = x[4]
    (cs, ss) = (shipRadius*cos(theta), shipRadius * sin(theta))
    v = [u[0] - x[2], u[1] - x[3]]
    deltaTheta = normalizeAngle(atan2(v[1], v[0]) - theta)
    if v[0]*v[0] + v[1]*v[1] >= shipDelta * shipDelta:
        if abs(deltaTheta) < shipEps:
            # accelerate forward, draw thruster on the back
            ax.add_patch(plt.Circle((pos[0] - cs, pos[1] - ss), .3 * shipRadius, color="red"))
        elif deltaTheta > 0:
            # rotate counterclockwise, draw thruster on right side
            ax.add_patch(plt.Circle((pos[0] + ss, pos[1] - cs), .3 * shipRadius, color="red"))
        else:
            # rotate clockwise, draw thruster on left side
            ax.add_patch(plt.Circle((pos[0] - ss, pos[1] + cs), .3 * shipRadius, color="red"))
    # draw ship
    ax.add_patch(plt.Circle(x[:2], shipRadius, color="yellow"))
    # draw two blue "eyes"
    ax.add_patch(plt.Circle((pos[0] + .7*shipRadius*cos(theta + .75), \
        pos[1] + .7*shipRadius*sin(theta + .75)), .2 * shipRadius, color="blue"))
    ax.add_patch(plt.Circle((pos[0] + .7*shipRadius*cos(theta - .75), \
        pos[1] + .7*shipRadius*sin(theta - .75)), .2 * shipRadius, color="blue"))

def plotKoules(state):
    numKoules = int(len(state)/4)
    for i in range(numKoules):
        ax.add_patch(plt.Circle((state[4 * i], state[4 * i + 1]), kouleRadius, color="red"))

def plotSystem(index):
    ax.clear()
    ax.add_patch(plt.Rectangle((0, 0), 1, 1, color='black'))
    plotKoules(path[index][5:-3])
    plotShip(path[index][0:5], path[index][-3:])
    if index % 10 == 0:
        stdout.write('.')
        stdout.flush()
    return handle,

def makeMovie(fname):
    with open(fname, 'r') as f:
        global sideLength, shipRadius, kouleRadius, propagationStepSize, shipAcceleration, \
            shipRotVel, shipDelta, shipEps, path
        sideLength, shipRadius, kouleRadius, propagationStepSize, shipAcceleration, \
            shipRotVel, shipDelta, shipEps = [float(x) for x in next(f).split()]
        path = [[float(x) for x in line.split(' ')] for line in f]
        if not path:
            print('Error: %s contains no solution path' % fname)
            return
        step = int(ceil(speedUp / (propagationStepSize * targetFrameRate)))
        path = path[0:len(path):step]
        print('Creating a movie with %d frames...' % len(path))
        print('Printing a \'.\' for every 10th frame:')
        ani = animation.FuncAnimation(fig, plotSystem, frames=len(path), \
            interval=1000. / step, blit=True)
        (base, _) = splitext(basename(fname))
        outfname = base + '.mp4'
        ani.save(outfname, bitrate=300, fps=targetFrameRate)
        print('')

if __name__ == '__main__':
    if len(argv) == 1:
        print('Usage: KoulesPlayback.py <filename> [<filename2> ...]')
    else:
        for trajectory_file in argv[1:]:
            makeMovie(trajectory_file)