summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/path/test/test_path.py
blob: f46545198e5dd7f473e86d34bfd7e52c60dccfcd (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# avr.path - Path finding module. {{{
#
# Copyright (C) 2008 Nicolas Schodet
#
# APBTeam:
#        Web: http://apbteam.org/
#      Email: team AT apbteam DOT org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# }}}
"""Graphic interface for test_path."""
import re

from Tkinter import *
from simu.inter.drawable import *
from subprocess import Popen, PIPE

class Obstacle:
    def __init__ (self, pos, radius):
        self.pos = pos
        self.radius = radius

    def move (self, pos):
        self.pos = pos

class Area (Drawable):

    def __init__ (self, onto, border_min, border_max):
        Drawable.__init__ (self, onto)
        self.border_min = border_min
        self.border_max = border_max
        self.border = None
        self.src = None
        self.dst = None
        self.obstacles = [ ]
        self.path = [ ]
        self.points = { }
        self.arcs = [ ]
        self.draw_arcs = True

    def draw (self):
        self.reset ()
        self.draw_rectangle (self.border_min, self.border_max, fill = 'white')
        for o in self.obstacles:
            if o.pos is not None:
                self.draw_circle (o.pos, o.radius, fill = 'gray25')
        if self.src is not None:
            self.draw_circle (self.src, 10, fill = 'green')
        if self.dst is not None:
            self.draw_circle (self.dst, 10, fill = 'red')
        if self.draw_arcs and self.arcs:
            fmt = dict (fill = 'gray75')
            for a in self.arcs:
                self.draw_line (self.points[a[0]], self.points[a[1]], **fmt)
        if len (self.path) > 1:
            fmt = dict (fill = 'blue', arrow = LAST)
            self.draw_line (*self.path, **fmt)
    
    def test (self):
        self.src = (300, 750)
        self.dst = (1200, 750)
        self.obstacles.append (Obstacle ((600, 680), 100))
        self.obstacles.append (Obstacle ((900, 820), 100))

    def update (self):
        args = [ [ self.border_min[0], self.border_min[1], self.border_max[0],
            self.border_max[1] ], self.src, self.dst ]
        for o in self.obstacles:
            args.append ([o.pos[0], o.pos[1], o.radius])
        args = [ ','.join (str (ai) for ai in a) for a in args ]
        args[0:0] = [ './test_path.host' ]
        p = Popen (args, stdout = PIPE)
        output = p.communicate ()[0]
        del p
        output = output.split ('\n')
        r = re.compile ('^// (-?\d+), (-?\d+)$')
        r_point = re.compile ('^ (\d+) .* pos = "(-?\d+),(-?\d+)"')
        r_arc = re.compile ('^  (-?\d+) -- (-?\d+) .* label = "(\d+)"')
        self.path = [ ]
        self.points = { }
        self.arcs = [ ]
        for line in output:
            m = r.match (line)
            if m is not None:
                self.path.append (tuple (int (s) for s in m.groups ()))
            m = r_point.match (line)
            if m is not None:
                self.points[int (m.group (1))] = tuple (int (s)
                        for s in m.group (2, 3))
            m = r_arc.match (line)
            if m is not None:
                self.arcs.append (tuple (int (s) for s in m.groups ()))

class AreaView (DrawableCanvas):

    def __init__ (self, border_min, border_max, master = None):
        self.border_min = border_min
        self.border_max = border_max
        width = border_max[0] - border_min[0]
        height = border_max[1] - border_min[0]
        DrawableCanvas.__init__ (self, width * 1.1, height * 1.1, -width / 2,
                -height / 2,
                master, borderwidth = 1, relief = 'sunken',
                background = 'white')
        self.area = Area (self, border_min, border_max)
        self.area.test ()
        self.area.update ()

    def draw (self):
        self.area.draw ()

class TestPath (Frame):

    def __init__ (self, border_min, border_max, master = None):
        Frame.__init__ (self, master)
        self.pack (expand = 1, fill = 'both')
        self.createWidgets (border_min, border_max)
        self.move = None

    def createWidgets (self, border_min, border_max):
        self.rightFrame = Frame (self)
        self.rightFrame.pack (side = 'right', fill = 'y')
        self.quitButton = Button (self.rightFrame, text = 'Quit', command = self.quit)
        self.quitButton.pack (side = 'top', fill = 'x')
        self.arcsVar = IntVar ()
        self.arcsVar.set (1)
        self.arcsButton = Checkbutton (self.rightFrame,
                variable = self.arcsVar, command = self.arcs_toggle,
                text = 'Arcs', indicatoron = True)
        self.arcsButton.pack (side = 'top')
        self.areaview = AreaView (border_min, border_max, self)
        self.areaview.pack (expand = True, fill = 'both')
        self.areaview.bind ('<1>', self.click)

    def clear (self):
        self.areaview.area.path = [ ]
        self.areaview.area.points = { }
        self.areaview.area.arcs = [ ]
        self.areaview.area.draw ()

    def update (self):
        self.areaview.area.update ()
        self.areaview.area.draw ()

    def click (self, ev):
        pos = self.areaview.screen_coord ((ev.x, ev.y))
        pos = tuple (int (i) for i in pos)
        if self.move is None:
            def move_src (pos):
                self.areaview.area.src = pos
            def move_dst (pos):
                self.areaview.area.dst = pos
            objs = [ [ self.areaview.area.src, 10, move_src ],
                    [ self.areaview.area.dst, 10, move_dst ] ]
            for o in self.areaview.area.obstacles:
                objs.append ([ o.pos, o.radius, o.move ])
            for obj in objs:
                dx = obj[0][0] - pos[0]
                dy = obj[0][1] - pos[1]
                if dx * dx + dy * dy < obj[1] * obj[1]:
                    self.move = obj[2]
                    break
            if self.move is not None:
                self.move (None)
                self.clear ()
        else:
            self.move (pos)
            self.update ()
            self.move = None

    def arcs_toggle (self):
        self.areaview.area.draw_arcs = self.arcsVar.get () != 0
        print self.areaview.area.draw_arcs
        self.areaview.area.draw ()

if __name__ == '__main__':
    app = TestPath ((0, 0), (1500, 1500))
    app.mainloop ()