-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_sphere.py
144 lines (114 loc) · 2.9 KB
/
py_sphere.py
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
import math
import svg
import numpy as np
from pyraydeon import (
Camera,
Point3,
Scene,
Sphere,
Vec3,
Geometry,
Material,
PointLight,
Plane,
LineSegment3D,
CameraOptions,
)
class PySphere(Geometry):
def __init__(self, point, radius):
self.sphere = Sphere(point, radius)
def collision_geometry(self):
return [self.sphere]
def paths(self, cam):
hyp = np.linalg.norm(self.sphere.center - cam.eye)
opp = self.sphere.radius
theta = math.asin(opp / hyp)
adj = opp / math.tan(theta)
d = math.cos(theta) * adj
r = math.sin(theta) * adj
w = self.sphere.center - cam.eye
w = w / np.linalg.norm(w)
u = np.cross(w, cam.up)
u = u / np.linalg.norm(u)
v = np.cross(w, u)
v = v / np.linalg.norm(v)
points = []
c = cam.eye + d * w
for i in range(0, 180):
a = math.radians(float(i))
p = c
p = p + u * (math.cos(a) * r)
p = p + v * (math.sin(a) * r)
push = p - self.sphere.center
push = push / np.linalg.norm(push)
p += push * 0.00015
points.append(p)
paths = []
for i in range(0, len(points) - 1):
paths.append(LineSegment3D(points[i], points[(i + 1)]))
return paths
class PyPlane(Geometry):
def __init__(self, point, normal):
self.plane = Plane(point, normal)
def collision_geometry(self):
return [self.plane]
def paths(self, cam):
return []
scene = Scene(
geometry=[
PySphere(
Point3(0, 0, 0),
1.0,
).with_material(Material(3.0, 3.0, 3)),
PyPlane(
Point3(0, -2, 0),
Vec3(0, 1, 0),
).with_material(Material(9000.0, 3.0, 3)),
],
lights=[PointLight((4, 3, 10), 3.6, 2.0, 0.15, 0.4, 0.11)],
ambient_light=0.13,
)
eye = Point3(0, 0, 0)
focus = Point3(0, 0, -1)
up = Vec3(0, 1, 0)
fovy = 50.0
width = 1024
height = 1024
znear = 0.1
zfar = 100.0
render_opts = CameraOptions(pen_px_size=4.0)
assert render_opts.hatch_slice_forgiveness == 1
cam = (
Camera()
.look_at(eye, focus, up)
.perspective(fovy, width, height, znear, zfar)
.render_options(render_opts)
)
cam.translate((0, 0, 5))
paths = scene.render_with_lighting(cam, seed=5)
canvas = svg.SVG(
width="8in",
height="8in",
viewBox="0 0 1024 1024",
)
backing_rect = svg.Rect(
x=0,
y=0,
width="100%",
height="100%",
fill="white",
)
svg_lines = [
svg.Line(
x1=f"{path.p1[0]}",
y1=f"{path.p1[1]}",
x2=f"{path.p2[0]}",
y2=f"{path.p2[1]}",
stroke_width="0.7mm",
stroke="black",
)
for path in paths
]
line_group = svg.G(transform=f"translate(0, {height}) scale(1, -1)", elements=svg_lines)
canvas.elements = [backing_rect, line_group]
print(canvas)