Skip to content

Commit 061292f

Browse files
committed
Quick hack to get visualisation of ray trajectories working :(
1 parent 67a964b commit 061292f

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

demos/logging_trajectories.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
# External imports
3+
# import matplotlib as mpl
4+
# from mpl_toolkits.mplot3d import Axes3D
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
# Raysect imports
9+
from raysect.optical import World, translate, rotate, Point3D, d65_white, Ray, Vector3D
10+
from raysect.optical.material.absorber import AbsorbingSurface
11+
from raysect.optical.library import schott
12+
from raysect.primitive import Sphere, Box
13+
from raysect.optical.loggingray import LoggingRay
14+
from raysect.primitive.lens.spherical import *
15+
16+
17+
world = World()
18+
19+
# Create a glass BiConvex lens we want to study
20+
lens_glass = schott("N-BK7")
21+
lens_glass.transmission_only = True
22+
lens = BiConvex(0.0254, 0.0052, 0.0506, 0.0506, parent=world, material=lens_glass)
23+
lens.meta['viz-color'] = (66/255, 188/255, 244/255)
24+
lens.meta['viz-opacity'] = 0.5
25+
26+
# lens.meta['viz']['color'] = (66/255, 188/255, 244/255)
27+
# lens.meta['viz']['opacity'] = 0.5
28+
29+
# Create a target plane behind the lens.
30+
target = Box(lower=Point3D(-0.05, -0.05, -0), upper=Point3D(0.05, 0.05, 0), material=AbsorbingSurface(),
31+
transform=translate(0, 0, 0.1), parent=world)
32+
target.meta['viz-color'] = (224/255, 100/255, 17/255)
33+
34+
35+
# for each sample direction trace a logging ray and plot the ray trajectory
36+
plt.ion()
37+
fig = plt.figure()
38+
# ax = fig.gca(projection='3d')
39+
40+
# for u in np.linspace(-0.006, 0.006, 5):
41+
for v in np.linspace(-0.012, 0.012, 11):
42+
43+
start = Point3D(v, 0, -0.05)
44+
log_ray = LoggingRay(start, Vector3D(0, 0, 1))
45+
log_ray.trace(world)
46+
47+
vertices = log_ray.path_vertices
48+
49+
p = [(v.x, v.z) for v in vertices]
50+
p = np.array(p)
51+
52+
plt.plot(p[:, 0], p[:, 1], 'k-')
53+
plt.plot(p[:, 0], p[:, 1], 'r.')
54+
55+
56+
from raysect_mayavi import visualise_scenegraph
57+
from mayavi import mlab
58+
59+
60+
visualise_scenegraph(world)
61+
62+
63+
for v in np.linspace(-0.012, 0.012, 11):
64+
65+
start = Point3D(v, 0, -0.05)
66+
log_ray = LoggingRay(start, Vector3D(0, 0, 1))
67+
log_ray.trace(world)
68+
69+
vertices = log_ray.path_vertices
70+
71+
p = [(v.x, v.y, v.z) for v in vertices]
72+
p = np.array(p)
73+
74+
mlab.plot3d(p[:, 0], p[:, 1], p[:, 2], tube_radius=0.0005)
75+
76+
plt.ioff()
77+
plt.show()

raysect_mayavi/primitives/to_mesh.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from raysect.core import Point3D, Vector3D
66
from raysect.primitive import Mesh, Box, Sphere, Cylinder, Cone, Parabola, Intersect, Union, Subtract
7+
from raysect.primitive.lens.spherical import BiConvex
78

89
from raysect_mayavi.primitives.mesh_tools import subdivide
910
from raysect_mayavi.primitives.mesh_csg import perform_mesh_csg
@@ -419,6 +420,10 @@ def csg_to_mesh(csg_primitive):
419420
return vertices, triangles
420421

421422

423+
def biconvex_to_mesh(biconvex_primitive):
424+
return to_mesh(biconvex_primitive._primitive)
425+
426+
422427
_object_handlers = {
423428
Box: box_to_mesh,
424429
Sphere: sphere_to_mesh,
@@ -427,7 +432,8 @@ def csg_to_mesh(csg_primitive):
427432
Mesh: mesh_to_mesh,
428433
Intersect: csg_to_mesh,
429434
Union: csg_to_mesh,
430-
Subtract: csg_to_mesh
435+
Subtract: csg_to_mesh,
436+
BiConvex: biconvex_to_mesh
431437
}
432438

433439

raysect_mayavi/scenegraph_viewer.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _parse_nodes(node, mesh_list):
1414
elif isinstance(child, Observer):
1515
return
1616
else:
17-
mesh_list.append(to_mesh(child))
17+
mesh_list.append((to_mesh(child), child.meta))
1818

1919

2020
def visualise_scenegraph(world, show_axes=False, axes_length=1):
@@ -29,13 +29,25 @@ def visualise_scenegraph(world, show_axes=False, axes_length=1):
2929

3030
for mesh_description in meshes:
3131

32-
vertices, triangles = mesh_description
32+
vertices, triangles = mesh_description[0]
3333
dx = vertices[:, 0]
3434
dy = vertices[:, 1]
3535
dz = vertices[:, 2]
3636

37-
mlab.triangular_mesh(dx, dy, dz, triangles, color=(163 / 255.0, 163 / 255.0, 163 / 255.0),
38-
figure=fig) # , transparent=True, opacity=0.3)
37+
meta = mesh_description[1]
38+
try:
39+
color = meta['viz-color']
40+
except KeyError:
41+
color = (163 / 255.0, 163 / 255.0, 163 / 255.0)
42+
try:
43+
opacity = meta['viz-opacity']
44+
transparent = True
45+
except KeyError:
46+
opacity = 1
47+
transparent = False
48+
49+
mlab.triangular_mesh(dx, dy, dz, triangles, color=color,
50+
figure=fig, transparent=transparent, opacity=opacity)
3951

4052
if show_axes:
4153
mlab.plot3d([0, axes_length], [0, 0], [0, 0], tube_radius=axes_length/100, color=(1, 0, 0))

0 commit comments

Comments
 (0)