summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/path/test/test_path.py
blob: a510a44ddb72ed1f4181dad02a42b14ec885fd24 (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
# 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 sys
sys.path.append ('../../../../../host')
from Tkinter import *
from inter.drawable import *
from subprocess import Popen, PIPE
import re

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

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 = [ ]

    def draw (self):
	self.reset ()
	self.draw_rectangle (self.border_min, self.border_max, fill = 'white')
	for o in self.obstacles:
	    self.draw_circle (o.pos, o.radius, fill = 'gray25')
	self.draw_circle (self.src, 10, fill = 'green')
	self.draw_circle (self.dst, 10, fill = 'red')
	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+)$')
	self.path = [ ]
	for line in output:
	    m = r.match (line)
	    if m is not None:
		self.path.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)

    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)

    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.updateButton = Button (self.rightFrame, text = 'Update', command
		= self.update)
	self.updateButton.pack (side  ='top', fill = 'x')
	self.areaview = AreaView (border_min, border_max, self)
	self.areaview.pack (expand = True, fill = 'both')

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

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