-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
163 lines (136 loc) · 4.54 KB
/
test.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
#!/usr/bin/env python2.6
import sys
from time import clock
from p2t import *
# PyGame Constants
import pygame
from pygame.gfxdraw import trigon, line
from pygame.locals import *
from pygame import Color
head_hole = [[325, 437],[320, 423], [329, 413], [332, 423]]
chest_hole = [[320.72342,480],[338.90617,465.96863],[347.99754,480.61584],
[329.8148,510.41534], [339.91632,480.11077],[334.86556,478.09046]]
def load_points(file_name):
infile = open(file_name, "r")
points = []
while infile:
line = infile.readline()
s = line.split()
if len(s) == 0:
break
points.append([float(s[0]), float(s[1])])
return points
def main(file_name, translate, zoom):
SCREEN_SIZE = 800,600
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE,0,8)
pygame.display.set_caption('poly2tri demo')
pygame.mouse.set_visible(True)
black = Color(0,0,0)
red = Color(255, 0, 0)
green = Color(0, 255, 0)
screen.fill(black)
points = load_points(file_name)
polyline = []
for p in points:
p[0] = p[0]*zoom + translate[0]
p[1] = p[1]*zoom + translate[1]
polyline.append(Point(p[0],p[1]))
# initialize clock
t0 = clock()
##
## Step 1: Initialize
## NOTE: polyline must be a simple polygon. The polyline's points
## constitute constrained edges. No repeat points!!!
##
cdt = CDT(polyline)
##
## Step 2: Add holes and interior Steiner points if necessary
##
if file_name == "data/dude.dat":
hole = []
for p in head_hole:
p[0] = p[0]*zoom + translate[0]
p[1] = p[1]*zoom + translate[1]
hole.append(Point(p[0],p[1]))
# Add a hole
cdt.add_hole(hole)
hole = []
for p in chest_hole:
p[0] = p[0]*zoom + translate[0]
p[1] = p[1]*zoom + translate[1]
hole.append(Point(p[0],p[1]))
# Add a hole
cdt.add_hole(hole)
# Add an interior Steiner point
x = 361*zoom + translate[0]
y = 381*zoom + translate[1]
cdt.add_point(Point(x, y))
##
## Step 3: Triangulate
##
triangles = cdt.triangulate()
print "Elapsed time (ms) = " + str(clock()*1000.0)
# The Main Event Loop
done = False
while not done:
# Draw triangles
for t in triangles:
x1 = int(t.a.x)
y1 = int(t.a.y)
x2 = int(t.b.x)
y2 = int(t.b.y)
x3 = int(t.c.x)
y3 = int(t.c.y)
trigon(screen, x1, y1, x2, y2, x3, y3, red)
# Draw outline
for i in range(len(points)):
j = i+1 if i < len(points) - 1 else 0
x1 = int(points[i][0])
y1 = int(points[i][1])
x2 = int(points[j][0])
y2 = int(points[j][1])
line(screen, x1, y1, x2, y2, green)
# Draw holes if necessary
if file_name == "data/dude.dat":
for i in range(len(head_hole)):
j = i+1 if i < len(head_hole) - 1 else 0
x1 = int(head_hole[i][0])
y1 = int(head_hole[i][1])
x2 = int(head_hole[j][0])
y2 = int(head_hole[j][1])
line(screen, x1, y1, x2, y2, green)
for i in range(len(chest_hole)):
j = i+1 if i < len(chest_hole) - 1 else 0
x1 = int(chest_hole[i][0])
y1 = int(chest_hole[i][1])
x2 = int(chest_hole[j][0])
y2 = int(chest_hole[j][1])
line(screen, x1, y1, x2, y2, green)
# Update the screen
pygame.display.update()
# Event Handling:
events = pygame.event.get( )
for e in events:
if( e.type == QUIT ):
done = True
break
elif (e.type == KEYDOWN):
if( e.key == K_ESCAPE ):
done = True
break
if( e.key == K_f ):
pygame.display.toggle_fullscreen()
return
if __name__=="__main__":
if len(sys.argv) == 5:
file_name = sys.argv[1]
tx = float(sys.argv[2])
ty = float(sys.argv[3])
zoom = float(sys.argv[4])
main(file_name, [tx, ty], zoom)
exit()
print
print(" Usage: filename translate-x translate-y zoom")
print("Example: python test.py data/dude.dat 100 -200 1")
print(" python test.py data/nazca_monkey.dat 400 300 4.5")