poly2tri/python/poly2tri.py

121 lines
6.0 KiB
Python
Raw Normal View History

2009-11-12 20:52:23 +01:00
#!/usr/bin/env python2.6
2009-11-30 22:36:03 +01:00
from framework import Game, draw_polygon, reset_zoom, draw_line, decompose_poly, makeCCW
2009-11-17 20:21:30 +01:00
from seidel import Triangulator
2009-11-12 20:52:23 +01:00
class Poly2Tri(Game):
screen_size = 800.0, 600.0
2009-11-30 22:36:03 +01:00
dude = [[174.50415,494.59368],[215.21844,478.87939],[207.36129,458.87939],[203.07558,441.02225],[203.07558,418.1651],
[210.93272,394.59367],[224.50415,373.1651],[241.64701,358.1651],[257.36129,354.59367],[275.93272,356.73653],
[293.07558,359.59367],[309.50415,377.45082],[322.36129,398.1651],[331.64701,421.73653],[335.21844,437.45082],
[356.64701,428.52225],[356.1113,428.34367],[356.1113,428.34367],[368.78987,419.59368],[349.50415,384.59367],
[323.78987,359.59367],[290.93272,343.87939],[267.36129,341.02225],[264.50415,331.02225],[264.50415,321.02225],
[268.78987,306.02225],[285.93272,286.02225],[295.21844,270.30796],[303.78987,254.59367],[306.64701,213.87939],
[320,202.36218],[265,202.36218],[286.64701,217.45082],[293.78987,241.02225],[285,257.36218],[270.93272,271.73653],
[254.50415,266.02225],[250.93272,248.1651],[256.64701,233.1651],[256.64701,221.02225],[245.93272,215.30796],
[238.78987,216.73653],[233.78987,232.45082],[232.36129,249.59367],[243.07558,257.09367],[242.89701,270.30796],
[235.93272,279.95082],[222.36129,293.1651],[205.21844,300.6651],[185,297.36218],[170,242.36218],[175,327.36218],
[185,322.36218],[195,317.36218],[230.75415,301.02225],[235.39701,312.45082],[240.57558,323.52225],
[243.61129,330.48653],[245.21844,335.12939],[245.03987,344.4151],[229.86129,349.4151],[209.14701,362.09367],
[192.89701,377.80796],[177.18272,402.27225],[172.36129,413.87939],[169.14701,430.48653],[168.61129,458.52225],
[168.61129,492.80796]]
2009-12-01 17:19:22 +01:00
test = [[235.04275999999999, 739.07534999999996], [218.13066000000001, 719.73902999999996],
[218.15215000000001, 709.96821], [218.17362, 700.19740000000002], [243.15215000000001, 685.27858000000003],
[268.13065, 670.35974999999996], [268.13065, 660.81429000000003], [268.13065, 651.26882999999998],
[314.55921999999998, 651.26882999999998], [360.98779000000002, 651.26882999999998],
[360.98683999999997, 666.44740000000002], [360.98046561000007, 669.27438118000009],
[360.96234088000011,672.68539864000013], [360.93345946999995, 676.58895225999993],
[360.89481504000003, 680.89354191999996], [360.84740125000002, 685.50766749999991],
[360.79221175999999, 690.33982888000003], [360.73024022999999, 695.29852593999999],
[360.66248032000004, 700.29225856000005], [360.58992569000003, 705.22952662000012],
[360.51357000000002, 710.01882999999998], [360.04131999999998, 738.41168000000005],
[310.51454999999999, 738.41168000000005], [260.98779999999999, 738.41168000000005],
[260.98779999999999, 748.41168000000005], [260.98779999999999, 758.41168000000005],
[256.47133000000002, 758.41168000000005], [251.95484999999999, 758.41168000000005]]
2009-11-12 20:52:23 +01:00
def __init__(self):
2009-11-17 20:21:30 +01:00
super(Poly2Tri, self).__init__(*self.screen_size)
# Load point set
2009-11-30 22:36:03 +01:00
file_name = "../data/dude.dat"
2009-11-18 19:58:59 +01:00
self.points = self.load_points(file_name)
2009-11-17 20:21:30 +01:00
# Triangulate
t1 = self.time
2009-11-18 19:58:59 +01:00
seidel = Triangulator(self.points)
dt = (self.time - t1) * 1000.0
2009-11-17 20:21:30 +01:00
self.triangles = seidel.triangles()
2009-11-20 18:13:19 +01:00
#self.trapezoids = seidel.trapezoids
self.trapezoids = seidel.trapezoidal_map.map
self.edges = seidel.edge_list
2009-11-18 19:58:59 +01:00
print "time (ms) = %f , num triangles = %d" % (dt, len(self.triangles))
2009-11-30 22:36:03 +01:00
for p in self.dude:
p[0] -= 75
2009-11-17 20:21:30 +01:00
2009-11-30 22:36:03 +01:00
makeCCW(self.dude)
self.dude_poly = []
t1 = self.time
decompose_poly(self.dude, self.dude_poly)
dt = (self.time - t1) * 1000.0
print "time (ms) = %f , num polies = %d" % (dt, len(self.dude_poly))
2009-12-01 17:19:22 +01:00
spam = []
makeCCW(self.test)
clean = []
for t in self.test:
x = round(t[0], 0)
y = round(t[1], 0)
clean.append([x,y])
decompose_poly(clean, spam)
2009-11-12 20:52:23 +01:00
self.main_loop()
def update(self):
pass
def render(self):
2009-12-01 17:19:22 +01:00
reset_zoom(2.2, (300, 450), self.screen_size)
2009-11-17 20:21:30 +01:00
red = 255, 0, 0
2009-11-20 18:13:19 +01:00
yellow = 255, 255, 0
green = 0, 255, 0
2009-11-17 20:21:30 +01:00
for t in self.triangles:
draw_polygon(t, red)
2009-11-20 18:13:19 +01:00
2009-11-30 22:36:03 +01:00
for p in self.dude_poly:
draw_polygon(p, red)
2009-11-18 19:58:59 +01:00
'''
for t in self.trapezoids:
2009-11-20 18:13:19 +01:00
verts = self.trapezoids[t].vertices()
#verts = t.vertices()
2009-11-18 19:58:59 +01:00
draw_polygon(verts, yellow)
'''
2009-11-20 18:13:19 +01:00
for e in self.edges:
p1 = e.p.x, e.p.y
p2 = e.q.x, e.q.y
draw_line(p1, p2, green)
#draw_polygon(self.points, green)
2009-11-18 19:58:59 +01:00
2009-11-17 20:21:30 +01:00
def load_points(self, 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
2009-11-19 18:27:34 +01:00
2009-11-20 18:13:19 +01:00
spam = (544.80998999999997, 579.86046999999996), (544.80998999999997, 450.57477), (594.09569999999997, 450.57477), (643.38142000000005, 450.57477), (643.38142000000005, 525.26486999999997), (643.38142000000005, 599.95487000000003), (603.67391999999995, 654.55056999999999), (563.96655999999996, 709.14621999999997), (554.38819999999998, 709.14621999999997), (544.80998999999997, 709.14621999999997)
eggs = [474.80999000000003, 555.15656999999999], [474.80999000000003, 530.87086999999997], [509.09570000000002, 530.87086999999997], [543.38142000000005, 530.87086999999997], [543.38142000000005, 555.15656999999999], [543.38142000000005, 579.44227000000001], [509.09570000000002, 579.44227000000001], [474.80999000000003, 579.44227000000001]
2009-11-12 20:52:23 +01:00
if __name__ == '__main__':
demo = Poly2Tri()