Skip to content

Commit

Permalink
begin
Browse files Browse the repository at this point in the history
  • Loading branch information
RamAddict authored and baioc committed Aug 31, 2021
1 parent 6e6b916 commit 1301cb2
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 4 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,28 @@ $ source venv/bin/activate

[PySide2]: https://doc.qt.io/qtforpython-5/api.html
[OBJ]: http://www.martinreddy.net/gfx/3d/OBJ.spec


## Entregas a serem revisadas:

$1.1$ -> Passamos a permitir entradas no padrão (x1,y1),(x2,y2).

$1.2$ -> Passamos a permitir entradas no padrão (x1,y1),(x2,y2).
Criamos a lista personalizada de transformações para serem aplicadas em conjunto.
Criamos o arquivo requirements.txt.

$1.3$ -> Criamos a lista personalizada de transformações para serem aplicadas em conjunto.
O OBJ passou a salvar o mundo inteiro.

$1.4$ -> Criamos a lista personalizada de transformações para serem aplicadas em conjunto.

$1.5$ -> As matrizes M e G do algoritmo de bezier passaram a ser multiplicadas antes do loop.

$1.6$ -> As matrizes M e G do algoritmo de bezier passaram a ser multiplicadas antes do loop.

$1.7$ -> Foram resolvidos os problemas das arestas inexistentes com o teapot.
Agora é permitido especificar arestas e wireframes sem faces (um Wireframe nunca tem faces preenchidas, diferentemente do Mesh).

$1.8$ -> Foram resolvidos os problemas das arestas inexistentes com o teapot.
Agora é permitido especificar arestas e wireframes sem faces (um Wireframe nunca tem faces preenchidas, diferentemente do Mesh).

5 changes: 5 additions & 0 deletions data/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@
<string>Bezier Surface</string>
</property>
</item>
<item>
<property name="text">
<string>B-Spline Surface</string>
</property>
</item>
</widget>
</item>
<item row="3" column="1">
Expand Down
11 changes: 9 additions & 2 deletions pycg/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

from blas import Vector
from graphics import (Painter, Camera, Transformation, Drawable, Point, Linestring,
Polygon, Color, Bezier, BSpline, Wireframe, Mesh, Surface)
Polygon, Color, Bezier, BSpline, Wireframe, Mesh,
BezierSurface, BSplineSurface)
from utilities import experp, begin, sign, to_float, lerp
import obj as wavefront_obj
from ui.main import Ui_MainWindow
Expand Down Expand Up @@ -206,7 +207,13 @@ def new_object():
for line in lines:
parsed = literal_eval(line)
points.extend([Point(*p) for p in parsed])
obj = Surface(points)
obj = BezierSurface(points)
elif typename == 'B-Spline Surface':
points = []
for line in lines:
parsed = literal_eval(line)
points.append([Point(*p) for p in parsed])
obj = BSplineSurface(points, bSpline=True)

if obj:
self.insert_object(
Expand Down
149 changes: 148 additions & 1 deletion pycg/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def center(self) -> Point:
return average / len(self.faces)


class Surface(Wireframe):
class BezierSurface(Wireframe):
"""Wireframe defined by a grid of points (assumes multiples of 16)."""

def __init__(self, points: List[Point], step=0.05):
Expand All @@ -407,6 +407,26 @@ def add_lines(grid):
super().__init__(wires)



class BSplineSurface(Wireframe):
"""Wireframe defined by a grid of control points (multiple of 4x4)."""

def __init__(self, points: List[List[Point]], step=0.05):

grid = Matrix(*bSplin(points, step))

wires = []

def add_lines(grid):
for line in grid:
wires.append(Linestring([Point(*p) for p in line]))

add_lines(grid)
add_lines(grid.transpose())

super().__init__(wires)


class Camera(Renderer):
"""Implements object rendering, calling a painter to draw to the screen."""

Expand Down Expand Up @@ -904,3 +924,130 @@ def bezierSurface(points: List[Point], step: float) -> List[List[Point]]:
si += 1

return surface


def bSpline(points: Sequence[Point], step: float) -> Sequence[Point]:
if len(points) < 3: return points
if len(points) == 3: points = [points[0], points[1], points[1], points[2]]

d1 = step
d2 = d1*step
d3 = d2*step
E = Matrix([0, 0, 0, 1],
[d3, d2, d1, 0],
[6*d3, 2*d2, 0, 0],
[6*d3, 0, 0, 0])
M = (1/6)*Matrix([-1, 3, -3, 1],
[3, -6, 3, 0],
[-3, 0, 3, 0],
[1, 4, 1, 0])
ExM = E @ M
return drawCurveFwdDif(points, step, ExM)

def drawCurveFwdDif(points: Sequence[Point], step: float, ExM: Matrix) -> Sequence[Point]:
curve = []
for i in range(0, len(points) - 3):
Gx = Vector(points[i].x, points[i+1].x, points[i+2].x, points[i+3].x)
Gy = Vector(points[i].y, points[i+1].y, points[i+2].y, points[i+3].y)
Gz = Vector(points[i].z, points[i+1].z, points[i+2].z, points[i+3].z)
Dx = ExM @ Gx
Dy = ExM @ Gy
Dz = ExM @ Gz
j = 0
while True:
j += step
if j >= 1: break
curve.append(Point(Dx[0], Dy[0], Dz[0]))
Dx[0] += Dx[1]
Dx[1] += Dx[2]
Dx[2] += Dx[3]
Dy[0] += Dy[1]
Dy[1] += Dy[2]
Dy[2] += Dy[3]
Dz[0] += Dz[1]
Dz[1] += Dz[2]
Dz[2] += Dz[3]
return curve

def bSplineSurface(points: Sequence[Point], step: float) -> Sequence[Point]:
if len(points) < 16: return points
s = t = step
curve = []

d1 = step
d2 = d1*step
d3 = d2*step
Es = Matrix([0, 0, 0, 1],
[d3, d2, d1, 0],
[6*d3, 2*d2, 0, 0],
[6*d3, 0, 0, 0])
Et = Es.transpose()

M: Matrix = (1/6)*Matrix([-1, 3, -3, 1],
[3, -6, 3, 0],
[-3, 0, 3, 0],
[1, 4, 1, 0])
# for every submatrix in the points (skipping 2 down and up)
# for i in range(0, len(points)-15, 15):
i = 0
# G = Matrix([points[i], points[i+1], points[i+2], points[i+3]],
# [points[i+4], points[i+5], points[i+6], points[i+7]],
# [points[i+8], points[i+9], points[i+10], points[i+11]],
# [points[i+12], points[i+13], points[i+14], points[i+15]])
Gx = Matrix([points[i].x, points[i+1].x, points[i+2].x, points[i+3].x],
[points[i+4].x, points[i+5].x, points[i+6].x, points[i+7].x],
[points[i+8].x, points[i+9].x, points[i+10].x, points[i+11].x],
[points[i+12].x, points[i+13].x, points[i+14].x, points[i+15].x])
Gy = Matrix([points[i].y, points[i+1].y, points[i+2].y, points[i+3].y],
[points[i+4].y, points[i+5].y, points[i+6].y, points[i+7].y],
[points[i+8].y, points[i+9].y, points[i+10].y, points[i+11].y],
[points[i+12].y, points[i+13].y, points[i+14].y, points[i+15].y])
Gz = Matrix([points[i].z, points[i+1].z, points[i+2].z, points[i+3].z],
[points[i+4].z, points[i+5].z, points[i+6].z, points[i+7].z],
[points[i+8].z, points[i+9].z, points[i+10].z, points[i+11].z],
[points[i+12].z, points[i+13].z, points[i+14].z, points[i+15].z])

# C: Matrix = M @ G @ M.transpose()
Cx: Matrix = M @ Gx @ M.transpose()
Cy: Matrix = M @ Gy @ M.transpose()
Cz: Matrix = M @ Gz @ M.transpose()

# DD: Matrix = Es @ C @ Et
DDx: Matrix = Es @ Cx @ Et
DDy: Matrix = Es @ Cy @ Et
DDz: Matrix = Es @ Cz @ Et
while True:
s += step
if s >= 1: break
print(DDx[0])
curve.append(drawCurveFwdDif(DDx[0], step))
curve.append(drawCurveFwdDif(DDy[0], step))
curve.append(drawCurveFwdDif(DDz[0], step))
# update DD Matrix
for i in range(0, 3):
# DD[i] += DD[i+1]
DDx[i] += DDx[i+1]
DDy[i] += DDy[i+1]
DDz[i] += DDz[i+1]
# DD: Matrix = Es @ C @ Et
# DD = DD.transpose()
DDx: Matrix = Es @ Cx @ Et
DDy: Matrix = Es @ Cy @ Et
DDz: Matrix = Es @ Cz @ Et
DDx = DDx.transpose()
DDy = DDy.transpose()
DDz = DDz.transpose()

while True:
t += step
if t >= 1: break
curve.append(bSpline(DDx[0], step))
# update DD Matrix
for i in range(0, 3):
# DD[i] += DD[i+1]
DDx[i] += DDx[i+1]
DDy[i] += DDy[i+1]
DDz[i] += DDz[i+1]

print(curve)
return curve
11 changes: 10 additions & 1 deletion pycg/tests/test_graphics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from math import pi

from graphics import Transformation, Point, Linestring, Polygon, Bezier, bezierSurface
from graphics import Transformation, Point, Linestring, Polygon, Bezier, bezierSurface, bSplineSurface


def test_transformations_center_pivot():
Expand Down Expand Up @@ -74,3 +74,12 @@ def test_surface_bezier():
Point(6, 3, 2), Point(8, 6, 5), Point(7, 10, 4.5 ), Point(6, 0, 2.5),
Point(10, 0, 0), Point(11, 3, 4), Point(11, 6, 3), Point(10, 9, 0)
], step=.1)


def test_surface_bSpline():
_ = bSplineSurface([
Point(0, 0, 0), Point(0, 3, 4), Point(0, 6, 3), Point(0, 10 ,0),
Point(3, 2.5, 2), Point(2, 6, 5), Point(3, 8, 5), Point(4, 0, 2),
Point(6, 3, 2), Point(8, 6, 5), Point(7, 10, 4.5 ), Point(6, 0, 2.5),
Point(10, 0, 0), Point(11, 3, 4), Point(11, 6, 3), Point(10, 9, 0)
], step=.1)

0 comments on commit 1301cb2

Please sign in to comment.