Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / mapping / python / map.py @ 934

History | View | Annotate | Download (5.42 KB)

1
#!/usr/bin/env python
2

    
3
from scipy import linalg, array
4
from gtk import *
5
from socket import *
6
from struct import *
7
import gobject
8
import sys, pickle, pygtk, gtk, string, cairo, gobject, math, weakref
9
from random import random
10

    
11
#mine
12
from model import model
13

    
14
pi = 3.1415927
15

    
16
#endmine
17

    
18
class map_model(model):
19
    def __init__(self):
20
        self.eqn = []
21

    
22
    def __setattr__(self,name,value):
23
        self.__dict__[name] = value
24
        self.changed()
25

    
26
    def add_eqn(self, eqn):
27
        self.eqn = [eqn] + self.eqn
28

    
29
hostname = 'localhost'
30

    
31
class Screen(gtk.DrawingArea):
32
    __gsignals__ = { "expose-event" : "override" }
33
    
34
    def __init__ (self, socket):
35
        gtk.DrawingArea.__init__(self)
36

    
37
        self.xres, self.yres = (800,800)
38

    
39
        self.set_size_request(self.xres, self.yres)
40

    
41
        self.socket = socket
42
        self.bots = [i for i in xrange(16)]
43

    
44
        self.draw = map_model()
45
        self.draw.bot = [[1,1,pi/2],0] # x y theta
46
        self.draw.add_dependent(self.redraw)
47

    
48
        self.scale_const = 50
49
        self.xdisp = 400
50
        self.ydisp = 400
51
        self.nticks = 21
52
        self.units = 10
53

    
54
    def redraw(self, eqn):
55
        self.clear_area()
56
        self.draw_bot()
57
        self.draw_grid()
58
        
59
    def do_expose_event(self, event):
60
        self.redraw(None)
61

    
62
    def clear_area(self):
63
        self.ctx = self.window.cairo_create()
64
        self.ctx.rectangle(0,0,1000,1000)
65
        self.ctx.set_source_rgb(1,1,1)
66
        self.ctx.fill()
67
        
68

    
69
    def draw_bot(self):
70
        self.ctx = self.window.cairo_create()
71
        self.ctx.new_path()
72
        
73
        tbot = self.draw.bot
74
        print tbot
75
        x, y, theta = tbot[:3]
76
        y = -y #because we don't want to draw down more units
77
        
78
        #draw blob
79
        self.ctx.set_source_rgb(0,1,0)
80
        xscl = (self.xres - self.xdisp) / self.units
81
        yscl = (self.yres - self.ydisp) / self.units
82

    
83
        cx = x * xscl + self.xdisp
84
        cy = y * yscl + self.ydisp
85
        self.ctx.arc(cx, cy, 20, 0, 2*pi)
86
        self.ctx.fill()
87

    
88
        #draw "arrow"
89
        self.ctx.set_source_rgb(0,0,1)
90
        self.ctx.arc(cx,cy, 5, 0, 2*pi)
91
        self.ctx.fill()
92
        
93
        self.ctx.move_to(cx,cy)
94
        dx = 20 * math.cos(theta + 0.05)
95
        dy = -20 * math.sin(theta + 0.05)
96
        dx = int(dx)
97
        dy = int(dy)
98
        
99
        self.ctx.rel_line_to(dx, dy)
100
        self.ctx.stroke()
101

    
102
    def draw_grid(self):
103

    
104
        self.ctx.set_source_rgb(0,0,0)
105
        self.ctx.new_path()
106
        
107
        #vertical
108
        self.ctx.move_to(400,0)
109
        self.ctx.rel_line_to(0,800)
110
        self.ctx.stroke()
111

    
112
        #vticks
113
        self.ctx.move_to(400,0)
114
        for i in range( self.nticks + 1 ):
115
            self.ctx.rel_line_to(5,0)
116
            self.ctx.stroke()
117
            self.ctx.move_to(400, i * 800 / self.nticks)
118
        
119
        #horiz
120
        self.ctx.move_to(0,400)
121
        self.ctx.rel_line_to(800,0)
122
        self.ctx.stroke()
123

    
124
        #hticks
125
        self.ctx.move_to(0,400)
126
        for i in range(self.nticks + 1):
127
            self.ctx.rel_line_to(0,-5)
128
            self.ctx.stroke()
129
            self.ctx.move_to(i * 800 / self.nticks, 400)
130

    
131
    def draw_graph(self, equations, n):
132
        self.ctx = self.window.cairo_create()
133
        self.ctx.set_source_rgb(0,0,0)
134
        self.ctx.new_path()
135
        
136
        for equn in equations:
137
            xmin, xmax = equn[-2],equn[-1]
138
            eqn = equn[:-2]
139
            dx = float(xmax - xmin) / n
140
            x = xmin
141
            y = 0
142
            for i in range(len(eqn)):
143
                y += eqn[-1 * i - 1] * ( x ** i)
144

    
145
            self.ctx.move_to(self.x_disp + x * self.scale_const,400 - y * self.scale_const)
146
            
147
            for diffel in range(1,n+1):
148
                x = xmin + diffel * dx
149
                y = 0
150
                for i in range(len(eqn)):
151
                    y += eqn[-1 * i - 1] * ( x ** i)
152
                self.ctx.line_to(self.x_disp + int(x * self.scale_const),400 - int(y * self.scale_const))
153
                self.ctx.stroke()
154
                self.ctx.move_to(self.x_disp + int(x * self.scale_const),400 - int(y * self.scale_const))
155

    
156
    def receive_info(self):
157
        
158
        buf = self.socket.recv(100)
159
        if (buf):
160
            items = unpack('4c2hf5h',buf)
161
            src = ord(items[0])
162
            old = self.draw.bot #the info of bot src at prev iter
163
            newx = items[4] / 30.0
164
            newy = items[5] / 30.0
165
            new = [newx, newy] + [items[i] for i in range(6,12)]
166
            self.draw.bot = new
167
        
168
        return True
169

    
170

    
171
def solve(lisvals):
172
    p0y, p1y, m0y, m1y, x0, x1 = lisvals
173
    A = array([[x0**3 , x0**2 , x0 , 1],
174
               [x1**3 , x1**2 , x1 , 1],
175
               [3 * x0**2 , 2 * x0 , 1 , 0],
176
               [3 * x1**2 , 2 * x1 , 1 , 0]])
177
    
178
    B = array([p0y, p1y, m0y, m1y])
179
    tarr = linalg.solve(A,B)
180
    lis = []
181
    for i in range(4):
182
        lis.append(tarr[i])
183
    return lis + [0,1]
184

    
185
def main():
186
    
187
    window = gtk.Window()
188
    window.connect("delete-event", gtk.main_quit)
189

    
190
    sock = socket()
191
    sock.connect((hostname, int(sys.argv[1])))
192
    
193
    mywidg = Screen(sock)
194

    
195
    bigbox = gtk.HBox()
196
    bigbox.pack_end(mywidg, True, True, 0)
197
    
198
    window.add(bigbox)
199
    bigbox.show()
200
    mywidg.show()
201
    window.show()
202

    
203
    gobject.idle_add(mywidg.receive_info)
204
    gtk.main()
205

    
206
if __name__=='__main__':
207
    if (len(sys.argv) < 2): #or not str.isdecimal(sys.argv[1])):
208
        print("Usage: %s <portno>" % sys.argv[0])
209
        sys.exit()
210

    
211
    main()