Skip to content

Commit 6356e35

Browse files
RamAddictbaioc
authored andcommitted
Add b spline object
1 parent 2da4e6b commit 6356e35

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

data/main.ui

+8-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@
315315
<item row="2" column="1" rowspan="2">
316316
<widget class="QStackedWidget" name="componentWidget">
317317
<property name="currentIndex">
318-
<number>0</number>
318+
<number>1</number>
319319
</property>
320320
<widget class="QWidget" name="emptyPage">
321321
<layout class="QVBoxLayout" name="verticalLayout_2">
@@ -379,8 +379,8 @@
379379
<rect>
380380
<x>0</x>
381381
<y>0</y>
382-
<width>389</width>
383-
<height>322</height>
382+
<width>375</width>
383+
<height>337</height>
384384
</rect>
385385
</property>
386386
<layout class="QVBoxLayout" name="verticalLayout">
@@ -474,6 +474,11 @@
474474
<string>Point</string>
475475
</property>
476476
</item>
477+
<item>
478+
<property name="text">
479+
<string>BSpline</string>
480+
</property>
481+
</item>
477482
<item>
478483
<property name="text">
479484
<string>Line</string>

pycg/app.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from PySide2.QtCore import Qt, QPoint
1515

1616
from blas import Vector, Matrix
17-
from graphics import (Painter, Camera, Transformation, Drawable, Point, Line,
17+
from graphics import (BSpline, Painter, Camera, Transformation, Drawable, Point, Line,
1818
Wireframe, Polygon, Color, Bezier)
1919
from utilities import experp, begin, sign, to_float
2020
import obj as wavefront_obj
@@ -189,6 +189,8 @@ def new_object():
189189
obj = Polygon([Point(x, y) for x, y in parsed])
190190
elif typename == "Bezier" and len(parsed) > 2:
191191
obj = Bezier([Point(x, y) for x, y in parsed])
192+
elif typename == "BSpline" and len(parsed) > 2:
193+
obj = BSpline([Point(x, y) for x, y in parsed])
192194
else:
193195
return
194196

pycg/graphics.py

+35
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ class Bezier(Wireframe):
234234
def __init__(self, points: Sequence[Point], step=0.01):
235235
super().__init__(bezier(points, step))
236236

237+
class BSpline(Wireframe):
238+
def __init__(self, points: Sequence[Point], step=0.01):
239+
super().__init__(bSpline(points, step))
237240

238241
class Polygon(Wireframe):
239242
"""Filled polygon."""
@@ -587,3 +590,35 @@ def bezier(points: Sequence[Point], step: float) -> Sequence[Point]:
587590
else:
588591
return points
589592
return curve
593+
594+
def bSpline(points: Sequence[Point], step: float) -> Sequence[Point]:
595+
curve = []
596+
if len(points) == 3:
597+
points = [points[0], points[1], points[1], points[2]]
598+
if len(points) >= 4:
599+
d1 = step
600+
d2 = step*step
601+
d3 = step*step*step
602+
E = Matrix([0, 0, 0, 1],
603+
[d3, d2, d1, 0],
604+
[6*d3, 2*d2, 0, 0],
605+
[6*d3, 0, 0, 0])
606+
M = (1/6)*Matrix([-1, 3,-3, 1],
607+
[ 3,-6, 3 ,0],
608+
[-3, 0, 3, 0],
609+
[ 1, 4, 1, 0])
610+
ExM = E @ M
611+
for i in range(0, len(points)-3):
612+
G = Vector(points[i], points[i+1], points[i+2], points[i+3])
613+
D = ExM @ G
614+
j = 0
615+
while True:
616+
j += step
617+
if j >= 1: break
618+
curve.append(Point(D[0][0], D[0][1]))
619+
D[0] += D[1]
620+
D[1] += D[2]
621+
D[2] += D[3]
622+
else:
623+
return points
624+
return curve

0 commit comments

Comments
 (0)