-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrefoil.py
More file actions
executable file
·201 lines (174 loc) · 4.77 KB
/
Copy pathtrefoil.py
File metadata and controls
executable file
·201 lines (174 loc) · 4.77 KB
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
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from OpenGL.GLE import *
import sys
import math
import transform
import numpy as np
import polyline as poly
import cost_function_polyline as c
import time
eye = np.array([0.0,-13.0,1.5])
up = np.array([0.0,0.0,1.0])
prevX = 0
prevY = 0
r = 0
points = [(2.0, -0.40, 0.80), (-1.3464, 1.532, -0.80), (-1.3464, -1.532, 0.80), (2.0, 0.40, -0.80), (-0.6536, 1.932, 0.80), (-0.6536, -1.932, -0.80)]
circlePoints = points
degree = 3
for i in range(degree - 1):
circlePoints += [circlePoints[i]]
knotNum = len(circlePoints) + degree
circleKnots = [float(i)/(knotNum-1) for i in range( knotNum )]
samplingTolerance=1.0
nurb = None
global_step = 0.01
global_step_threshold = 0.00001
delay = 0.1
alpha = 1
beta = 1
gamma = 1
pol = poly.Polyline(points)
cost = c.cost_function(pol, alpha, beta, gamma)
lowest_cost = cost
past_cost = 0
colors = (
(0,0,1),
(0,1,0),
(1,0,0),
(1,1,0),
(1,0,1),
(0,1,1),
(0,0,0),
(1,1,1),
)
# def gradient_descent():
# # look at nearmissjohnson.py for algorithm
def init():
glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
glClearDepth(1.0) # Set background depth to farthest
glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL) # Set the type of depth-test
glShadeModel(GL_SMOOTH) # Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
def mainDisplay():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(eye[0],eye[1],eye[2], 0,0,0, up[0],up[1],up[2])
glBegin(GL_LINES)
# x axis
glColor3f(1.0, 0.0, 0.0)
glVertex3f(0.0,0.0,0.0)
glVertex3f(10.0,0.0,0.0)
# y axis
glColor3f(0.0,1.0,0.0)
glVertex3f(0.0,0.0,0.0)
glVertex3f(0.0,10.0,0.0)
# z axis
glColor3f(0.0,0.0,1.0)
glVertex3f(0.0,0.0,0.0)
glVertex3f(0.0,0.0,10.0)
glEnd()
#Draw Geometry
global pol, cost, past_cost, lowest_cost, colors
pol.rebuild()
cost = c.cost_function(pol, alpha, beta, gamma)
if (cost != past_cost):
print(cost - lowest_cost)
lowest_cost = min(lowest_cost, cost)
past_cost = cost
glBegin(GL_LINES)
glColor3f(1, 1, 1)
for edge in pol.edges:
for v in edge:
glVertex3fv(v)
glEnd()
# glePolyCone(((-6.0, 6.0, 0.0), (6.0, 6.0, 0.0), (6.0, -6.0, 0.0), (-6.0, -6.0, 0.0), (-6.0, 6.0, 0.0), (6.0, 6.0, 0.0)),
# ((0.0, 0.0, 0.0), (0.0, 0.8, 0.3), (0.8, 0.3, 0.0), (0.2, 0.3, 0.9), (0.2, 0.8, 0.5), (0.0, 0.0, 0.0)), (0.1, 0.1, 0.1, 0.1, 0.2, 0.1))
draw_bspline()
glutSwapBuffers()
def draw_bspline():
global nurb, samplingTolerance, circlePoints, circleKnots
nurb = gluNewNurbsRenderer()
glLineWidth(2.0)
gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, samplingTolerance)
glColor3f(0, 1, 1)
glBegin(GL_LINE_STRIP)
for coord in circlePoints:
glVertex3f(coord[0], coord[1], coord[2]);
glEnd()
glColor3f(1, 1, 1)
gluBeginCurve( nurb )
gluNurbsCurve ( nurb, circleKnots, circlePoints, GL_MAP1_VERTEX_3 )
gluEndCurve( nurb )
glutSwapBuffers( )
def mainReshape(w,h):
if h == 0:
h = 1
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, float(w) / h, 0.1, 100)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def idle():
glutPostRedisplay()
def drag(x,y):
global prevX, prevY, eye, up
diffX = x - prevX
diffY = -y + prevY
eye = transform.left(diffX,eye,up)
eye, up = transform.up(diffY,eye,up)
prevX = x
prevY = y
glutPostRedisplay()
def keyboard(key,x,y):
global pol, global_step, global_step_threshold, delay
if key == 27:
exit(0)
elif key == "p":
printValues()
elif key == " ":
gradient_descent()
elif key == "1":
global_step = float(global_step) / 2
elif key == "2":
global_step = global_step * 2
elif key == "g":
print("~~performing gradient descent~~")
print("initial values: ")
printValues()
while (global_step > global_step_threshold):
gradient_descent()
glutPostRedisplay()
time.sleep(delay)
print("final values: ")
printValues()
glutPostRedisplay()
def printValues():
global pol, cost, lowest_cost
print("global_step = " + str(global_step))
print("lowest cost = " + str(lowest_cost))
print("current cost = " + str(cost))
def mouse(button,state,x,y):
global prevX, prevY
if state == 1:
prevX = 0
prevY = 0
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE)
glutInitWindowSize(640, 480)
glutInitWindowPosition(0, 0)
glutCreateWindow("Trefoil")
glutDisplayFunc(mainDisplay)
glutReshapeFunc(mainReshape)
glutMotionFunc(drag)
glutMouseFunc(mouse)
glutKeyboardFunc(keyboard)
init()
glutMainLoop()
return
if __name__ == '__main__': main()