-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplineTool.py
More file actions
142 lines (112 loc) · 4.22 KB
/
SplineTool.py
File metadata and controls
142 lines (112 loc) · 4.22 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
#!/usr/bin/env python3
# A proof of concept spline drawing and exporting app
import sys
import random
import functools
import json
from dataclasses import dataclass
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QDesktopWidget
@dataclass
class Point:
x : float
y : float
def ExportToJson(ControlPoints, ScreenWidth, ScreenHeight):
data = {}
data['points'] = []
for Point in ControlPoints:
x = (Point[0] - ControlPoints[0][0]) / ScreenWidth
y = (Point[1] - ControlPoints[0][1]) / ScreenHeight
data['points'].append({'x' : x, 'y' : y})
with open('ControlPoints.json', 'w') as outfile:
json.dump(data, outfile)
@functools.lru_cache(maxsize=100)
def factorial(n):
prod = 1
for i in range(1,n+1):
prod *= i
return prod
def randPt(minv, maxv):
return (random.randint(minv, maxv), random.randint(minv, maxv))
def B(i,n,u):
val = factorial(n)/(factorial(i)*factorial(n-i))
return val * (u**i) * ((1-u)**(n-i))
def C(u, pts):
x = 0
y = 0
n = len(pts)-1
for i in range(n+1):
binu = B(i,n,u)
x += binu * pts[i][0]
y += binu * pts[i][1]
return (x, y)
class BezierDrawer(QtWidgets.QMainWindow):
def __init__(self):
super(BezierDrawer, self).__init__()
self.setGeometry(0, 0, 1080, 720)
self.setWindowTitle('Spline tool')
self.controlPts = [] # [(100,100), (600,100), (600,600), (100,600)]
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
def paintEvent(self, e):
qp = QtGui.QPainter()
qp.begin(self)
qp.setRenderHints(QtGui.QPainter.Antialiasing, True)
self.doDrawing(qp)
qp.end()
def doDrawing(self, qp):
blackPen = QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.DashLine)
redPen = QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.DashLine)
bluePen = QtGui.QPen(QtCore.Qt.blue, 1, QtCore.Qt.DashLine)
greenPen = QtGui.QPen(QtCore.Qt.green, 1, QtCore.Qt.DashLine)
redBrush = QtGui.QBrush(QtCore.Qt.red)
steps = 600
min_t = 0.0
max_t = 1.0
dt = (max_t - min_t)/steps
# controlPts = [randPt(0,1000) for i in range(6)]
# controlPts.append(controlPts[1])
# controlPts.append(controlPts[0])
if(len(self.controlPts)):
oldPt = self.controlPts[0]
pn = 1
qp.setPen(redPen)
qp.setBrush(redBrush)
qp.drawEllipse(oldPt[0]-3, oldPt[1]-3, 6,6)
qp.drawText(oldPt[0]+5, oldPt[1]-3, '{}'.format(pn))
for pt in self.controlPts[1:]:
pn+=1
qp.setPen(blackPen)
qp.drawLine(oldPt[0],oldPt[1],pt[0],pt[1])
qp.setPen(redPen)
qp.drawEllipse(pt[0]-3, pt[1]-3, 6,6)
qp.drawText(pt[0]+5, pt[1]-3, '{}'.format(pn))
oldPt = pt
qp.setPen(bluePen)
oldPt = self.controlPts[0]
for i in range(steps+1):
t = dt*i
pt = C(t, self.controlPts)
qp.drawLine(oldPt[0],oldPt[1], pt[0],pt[1])
oldPt = pt
self.update()
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
if event.buttons() == QtCore.Qt.LeftButton:
position = event.pos()
if(position.x() in self.controlPts or position.y() in self.controlPts):
return 1
else:
self.controlPts.append([position.x(), position.y()])
return 1
if event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Control:
ExportToJson(self.controlPts, self.width(), self.height())
return super().eventFilter(source, event)
app = QtWidgets.QApplication(sys.argv)
ex = BezierDrawer()
ex.show()
app.installEventFilter(ex)
app.exec_()