-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcalibration.py
204 lines (184 loc) · 8.34 KB
/
calibration.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
""" Snow Globe: Calibration Script for a Cheap DIY Spherical Projection Setup
Copyright (c) 2011, Nirav Patel <http://eclecti.cc>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
This script allows you to calibrate a Snow Globe. You use the arrow keys to
line up the longitude lines correctly to arrive at the x and y position,
plus and minus keys to adjust the radius size until it fits the full visible
area of the sphere, and 9 and 0 to adjust the lens offset until the latitudes
look properly aligned. Press s to save the calibration data to a file.
usage:
python calibration.py calibration.txt
"""
#!/usr/bin/env python
import sys
import math
import pickle
import pygame
from pygame.locals import *
class SnowGlobeCalibration(object):
def __init__(self, fullscreen, filename):
self.filename = filename
self.size = (848, 480)
self.clock = pygame.time.Clock()
if fullscreen:
self.display = pygame.display.set_mode(self.size, pygame.FULLSCREEN)
else:
self.display = pygame.display.set_mode(self.size, 0)
self.display.fill((0,0,0))
self.r = 378
self.offset = 370.0
self.center = (431,210)
self.lastrect = None
if self.filename != None:
self.open_calibration()
self.sphere = pygame.surface.Surface((self.r*2,self.r*2))
self.generate_sphere()
self.dx = 0
self.dy = 0
self.dr = 0
self.do = 0
self.going = True
# An abandoned path by which I went pixel by pixel, determined the correct
# theta and phi angles mapping to the hemisphere, and decided the color based
# on the angle. Significantly too slow to run real time in python
# def coord_color(self, theta, phi):
# r = 255
# g = 255
# b = 255
# if theta < math.pi/4:
# r = 0
# if phi > math.pi/2 or (phi > -math.pi/2 and phi < 0):
# g = 0
# return (r, g, b)
#
# def cartesian_to_polar(self, x, y, d):
# theta = math.acos(d/self.r)
# phi = math.atan2(x-self.r,y-self.r)
# return theta, phi
#
# def generate_sphere(self):
# self.sphere = pygame.surface.Surface((self.r*2, self.r*2))
# pixels = pygame.surfarray.pixels3d(self.sphere)
# for x, col in enumerate(pixels):
# for y, color in enumerate(col):
# d = math.sqrt((x-self.r)**2+(y-self.r)**2)
# if d < self.r:
# theta, phi = self.cartesian_to_polar(x, y, d)
# pixels[x][y] = self.coord_color(theta, phi)
''' A whole lot of voodoo involving the law of sines to map a sphere
to a hemisphere and then the hemisphere to a plane. '''
def radius_for_theta(self, theta):
side_a = math.sin(theta)*self.r
side_c = math.sin(math.pi/2.0-theta)*self.r
# mapping the sphere angle to the hemisphere angle
angle_b = math.atan2(side_a, side_c + self.offset)
angle_b = min(math.pi/2,max(0.0,angle_b))
# map the hemisphere angle to the radius of the projected image in pixels
# assumes the lens follows the r=2*f*sin(theta/2) mapping, but who knows
rad = (float(self.r)/math.sin(math.pi/4))*math.sin(angle_b/2.0)
return int(rad)
def generate_sphere(self):
self.sphere = pygame.surface.Surface((self.r*2, self.r*2))
# latitude colored regions
pygame.draw.circle(self.sphere, (255,0,64), (self.r, self.r), self.radius_for_theta(math.pi))
pygame.draw.circle(self.sphere, (192,0,128), (self.r, self.r), self.radius_for_theta(math.pi*3.0/4.0))
pygame.draw.circle(self.sphere, (128,0,192), (self.r, self.r), self.radius_for_theta(math.pi/2.0))
pygame.draw.circle(self.sphere, (64,0,255), (self.r, self.r), self.radius_for_theta(math.pi/4.0))
# longitude grid lines
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (0, 0), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (self.r, 0), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (self.r*2, 0), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (self.r*2, self.r), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (self.r*2, self.r*2), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (self.r, self.r*2), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (0, self.r*2), 5)
pygame.draw.line(self.sphere, (128,255,128), (self.r, self.r), (0, self.r), 5)
def composite(self):
self.display.fill((0,0,0),self.lastrect)
rect = self.display.blit(self.sphere, (self.center[0]-self.r, self.center[1]-self.r))
pygame.display.update([rect,self.lastrect])
self.lastrect = rect
def move(self):
self.center = (self.center[0] + self.dx, self.center[1] + self.dy)
if self.dr != 0 or self.do != 0:
self.r += self.dr
if self.offset < self.r or self.do == -1:
self.offset += self.do
elif self.offset > self.r:
self.offset = self.r
self.generate_sphere()
def open_calibration(self):
try:
f = open(self.filename, 'r')
except IOError:
print "Failed to open %s." % self.filename
else:
properties = pickle.load(f)
try:
self.r = properties["radius"]
self.offset = properties["offset"]
self.center = properties["center"]
except KeyError:
print "%s missing properties." % self.filename
f.close()
def save_calibration(self):
with open(self.filename, 'w') as f:
properties = dict(radius=self.r,offset=self.offset,center=self.center)
pickle.dump(properties,f)
def check_input(self):
events = pygame.event.get()
for e in events:
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
self.going = False
elif e.type == KEYDOWN:
if e.key == K_LEFT:
self.dx = -1
elif e.key == K_RIGHT:
self.dx = 1
elif e.key == K_UP:
self.dy = -1
elif e.key == K_DOWN:
self.dy = 1
elif e.key == K_MINUS:
self.dr = -1
elif e.key == K_EQUALS:
self.dr = 1
elif e.key == K_9:
self.do = -1
elif e.key == K_0:
self.do = 1
elif e.key == K_s:
if self.filename != None:
self.save_calibration()
elif e.type == KEYUP:
if (e.key == K_LEFT and self.dx == -1) or (e.key == K_RIGHT and self.dx == 1):
self.dx = 0
elif (e.key == K_UP and self.dy == -1) or (e.key == K_DOWN and self.dy == 1):
self.dy = 0
elif (e.key == K_MINUS and self.dr == -1) or (e.key == K_EQUALS and self.dr == 1):
self.dr = 0
elif (e.key == K_9 and self.do == -1) or (e.key == K_0 and self.do == 1):
self.do = 0
def main(self):
while self.going:
self.check_input()
self.move()
self.composite()
self.clock.tick(60)
print (self.r, self.offset, self.center)
if __name__ == '__main__':
pygame.init()
filename = None
if len(sys.argv) > 1:
filename = sys.argv[1]
snowglobe = SnowGlobeCalibration(True, filename)
snowglobe.main()