Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions demos/cornell_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@
# objects in enclosure
box = Box(Point3D(-0.4, 0, -0.4), Point3D(0.3, 1.4, 0.3),
parent=world,
transform=translate(0.4, -1 + 1e-6, 0.4)*rotate(30, 0, 0),
transform=translate(0.4, -1 + 1e-6, 0.4) * rotate(30, 0, 0),
material=schott("N-BK7"))

sphere = Sphere(0.4,
parent=world,
transform=translate(-0.4, -0.6 + 1e-6, -0.4)*rotate(0, 0, 0),
transform=translate(-0.4, -0.6 + 1e-6, -0.4) * rotate(0, 0, 0),
material=schott("N-BK7"))


Expand Down Expand Up @@ -154,6 +154,16 @@
camera.ray_extinction_prob = 0.01


from raysect_mayavi import visualise_scenegraph
# Visualising with mayavi
from raysect_mayavi.mayavi import visualise_scenegraph
from mayavi import mlab

visualise_scenegraph(world)
fig = visualise_scenegraph(world)
mlab.show()


# Visualising with pyvista
from raysect_mayavi.pyvista import visualise_scenegraph

plotter = visualise_scenegraph(world)
plotter.show()
36 changes: 28 additions & 8 deletions demos/logging_trajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import numpy as np

# Raysect imports
from raysect.optical import World, translate, rotate, Point3D, d65_white, Ray, Vector3D
from raysect.optical import World, translate, Point3D, Vector3D
from raysect.optical.material.absorber import AbsorbingSurface
from raysect.optical.library import schott
from raysect.primitive import Sphere, Box
from raysect.primitive import Box
from raysect.optical.loggingray import LoggingRay
from raysect.primitive.lens.spherical import *

Expand All @@ -20,7 +20,7 @@
lens_glass = schott("N-BK7")
lens_glass.transmission_only = True
lens = BiConvex(0.0254, 0.0052, 0.0506, 0.0506, parent=world, material=lens_glass)
lens.meta['viz-color'] = (66/255, 188/255, 244/255)
lens.meta['viz-color'] = (66 / 255, 188 / 255, 244 / 255)
lens.meta['viz-opacity'] = 0.5

# lens.meta['viz']['color'] = (66/255, 188/255, 244/255)
Expand All @@ -29,7 +29,7 @@
# Create a target plane behind the lens.
target = Box(lower=Point3D(-0.05, -0.05, -0), upper=Point3D(0.05, 0.05, 0), material=AbsorbingSurface(),
transform=translate(0, 0, 0.1), parent=world)
target.meta['viz-color'] = (224/255, 100/255, 17/255)
target.meta['viz-color'] = (224 / 255, 100 / 255, 17 / 255)


# for each sample direction trace a logging ray and plot the ray trajectory
Expand All @@ -51,21 +51,41 @@

plt.plot(p[:, 0], p[:, 1], 'k-')
plt.plot(p[:, 0], p[:, 1], 'r.')
plt.draw()
plt.pause(0.01)


from raysect_mayavi import visualise_scenegraph
# Visualising with mayavi
from raysect_mayavi.mayavi import visualise_scenegraph, assign_visualiser
from raysect_mayavi import to_mesh
from mayavi import mlab


fig = visualise_scenegraph(world)

for v in np.linspace(-0.012, 0.012, 11):
start = Point3D(v, 0, -0.05)
log_ray = LoggingRay(start, Vector3D(0, 0, 1))
log_ray.trace(world)
log_ray_mayavi = to_mesh(log_ray)
log_ray_mayavi.mayavi_plot(fig)
log_ray_mayavi = assign_visualiser(to_mesh(log_ray))
log_ray_mayavi.plot(fig)

mlab.show()


# Visualising with pyvista
from raysect_mayavi.pyvista import visualise_scenegraph, assign_visualiser
from raysect_mayavi import to_mesh

plotter = visualise_scenegraph(world)

for v in np.linspace(-0.012, 0.012, 11):
start = Point3D(v, 0, -0.05)
log_ray = LoggingRay(start, Vector3D(0, 0, 1))
log_ray.trace(world)
log_ray_mayavi = assign_visualiser(to_mesh(log_ray))
log_ray_mayavi.plot(plotter)

plotter.show()

plt.ioff()
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from raysect.core import translate, Point3D, rotate_basis, Vector3D, rotate
from raysect.optical import World
from raysect.primitive import Box, Sphere, Cylinder, Cone
from raysect.primitive import Sphere, Mesh, Intersect, Subtract, Union
from raysect.primitive import Intersect, Subtract, Union

from raysect_mayavi import visualise_scenegraph
from raysect_mayavi.mayavi import visualise_scenegraph


########################################################################################################################
Expand Down Expand Up @@ -105,7 +105,7 @@
# Cone and Cylinder

c1 = Cone(0.15, 1)
c2 = Cylinder(0.15, 0.5, transform=translate(-0.25, 0, 0.5)*rotate_basis(Vector3D(1, 0, 0), Vector3D(0, 0, 1)))
c2 = Cylinder(0.15, 0.5, transform=translate(-0.25, 0, 0.5) * rotate_basis(Vector3D(1, 0, 0), Vector3D(0, 0, 1)))

world = World()
Union(c1, c2, parent=world)
Expand All @@ -127,9 +127,9 @@
# CSG Hell

world = World()
cyl_x = Cylinder(1, 4.2, transform=rotate(90, 0, 0)*translate(0, 0, -2.1))
cyl_y = Cylinder(1, 4.2, transform=rotate(0, 90, 0)*translate(0, 0, -2.1))
cyl_z = Cylinder(1, 4.2, transform=rotate(0, 0, 0)*translate(0, 0, -2.1))
cyl_x = Cylinder(1, 4.2, transform=rotate(90, 0, 0) * translate(0, 0, -2.1))
cyl_y = Cylinder(1, 4.2, transform=rotate(0, 90, 0) * translate(0, 0, -2.1))
cyl_z = Cylinder(1, 4.2, transform=rotate(0, 0, 0) * translate(0, 0, -2.1))
cube = Box(Point3D(-1.5, -1.5, -1.5), Point3D(1.5, 1.5, 1.5))
sphere = Sphere(2.0)

Expand Down
118 changes: 118 additions & 0 deletions demos/mesh_csg/mesh_csg_examples_pyvista.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@

from raysect.core import translate, Point3D, rotate_basis, Vector3D, rotate
from raysect.optical import World
from raysect.primitive import Box, Sphere, Cylinder, Cone
from raysect.primitive import Intersect, Subtract, Union

from raysect_mayavi.pyvista import visualise_scenegraph


########################################################################################################################
# Spheres


s1 = Sphere(0.5, transform=translate(-0.25, 0, 0), name='s1')
s2 = Sphere(0.5, transform=translate(0.25, 0, 0), name='s2')


world = World()
Union(s1, s2, parent=world)
visualise_scenegraph(world).show()

world = World()
Intersect(s1, s2, parent=world)
visualise_scenegraph(world).show()

world = World()
Subtract(s1, s2, parent=world)
visualise_scenegraph(world).show()


########################################################################################################################
# Cubes

b1 = Box(Point3D(0, 0, 0), Point3D(1, 1, 1))
b2 = Box(Point3D(0, 0, 0), Point3D(1, 1, 1), transform=translate(0.6, 0.6, 0.6))

world = World()
Union(b1, b2, parent=world)
visualise_scenegraph(world).show()

world = World()
Intersect(b1, b2, parent=world)
visualise_scenegraph(world).show()

world = World()
Subtract(b1, b2, parent=world)
visualise_scenegraph(world).show()

########################################################################################################################
# Cylinders

c1 = Cylinder(0.5, 2)
c2 = Cylinder(0.5, 2, transform=translate(0.15, 0, 0.4))

world = World()
Union(c1, c2, parent=world)
visualise_scenegraph(world).show()

world = World()
Intersect(c1, c2, parent=world)
visualise_scenegraph(world).show()

world = World()
Subtract(c1, c2, parent=world)
visualise_scenegraph(world).show()


########################################################################################################################
# Box and Sphere

s1 = Sphere(0.5)
b2 = Box(Point3D(-0.5, -0.5, -0.5), Point3D(0.5, 0.5, 0.5), transform=translate(-0.2, 0, -0.2))

world = World()
Union(s1, b2, parent=world)
visualise_scenegraph(world).show()

world = World()
Intersect(s1, b2, parent=world)
visualise_scenegraph(world).show()

world = World()
Subtract(s1, b2, parent=world)
visualise_scenegraph(world)


########################################################################################################################
# Cone and Cylinder

c1 = Cone(0.15, 1)
c2 = Cylinder(0.15, 0.5, transform=translate(-0.25, 0, 0.5) * rotate_basis(Vector3D(1, 0, 0), Vector3D(0, 0, 1)))

world = World()
Union(c1, c2, parent=world)
visualise_scenegraph(world).show()

world = World()
Intersect(c1, c2, parent=world)
visualise_scenegraph(world).show()

world = World()
Subtract(c1, c2, parent=world)
visualise_scenegraph(world).show()


########################################################################################################################
# CSG Hell

world = World()
cyl_x = Cylinder(1, 4.2, transform=rotate(90, 0, 0) * translate(0, 0, -2.1))
cyl_y = Cylinder(1, 4.2, transform=rotate(0, 90, 0) * translate(0, 0, -2.1))
cyl_z = Cylinder(1, 4.2, transform=rotate(0, 0, 0) * translate(0, 0, -2.1))
cube = Box(Point3D(-1.5, -1.5, -1.5), Point3D(1.5, 1.5, 1.5))
sphere = Sphere(2.0)

csg = Intersect(sphere, Subtract(cube, Union(Union(cyl_x, cyl_y), cyl_z)), world)

visualise_scenegraph(world).show()
15 changes: 11 additions & 4 deletions demos/optical_elements.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from raysect_mayavi import visualise_scenegraph

from raysect.core import translate
from raysect.optical import World
from raysect.primitive import Cylinder
from raysect.primitive.lens.spherical import BiConvex, BiConcave, PlanoConvex, PlanoConcave, Meniscus

from mayavi import mlab
# Display lens and cylinder primitives

world = World()
Expand All @@ -22,5 +19,15 @@
cylinder_primitive = Cylinder(radius=0.5 * diameter, height=center_thickness, parent=world, transform=translate(0, 0, 5))


# Visualising with mayavi
from raysect_mayavi.mayavi import visualise_scenegraph
from mayavi import mlab

visualise_scenegraph(world)
mlab.show()
mlab.show()

# Visualising with pyvista
from raysect_mayavi.pyvista import visualise_scenegraph

plotter = visualise_scenegraph(world)
plotter.show()
2 changes: 2 additions & 0 deletions raysect_mayavi/mayavi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .scenegraph_viewer import visualise_scenegraph
from .parse import assign_visualiser
11 changes: 5 additions & 6 deletions raysect_mayavi/mayavi/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ class TriangularMeshVisualiser(MayaviVisualiser):
"""
This class serves as base class for Raysect objetcs visualised with triangular meshses.
"""

def __init__(self, source):


self._init_plot_kwargs()


super().__init__(source)
self.plot_method = mlab.triangular_mesh

def set_source(self, source):

if not isinstance(source, TriangularMeshSource):
raise TypeError("source has to be instance of SourceBase.")
raise TypeError("source has to be instance of TriangularMeshSource.")

self._source = source

def _init_plot_kwargs(self):
Expand All @@ -33,5 +32,5 @@ def _init_plot_kwargs(self):
def _mayavi_plot(self, figure):
vertices = self._source.vertices

mlab.triangular_mesh(vertices[:,0], vertices[:,1], vertices[:,2], self._source.triangles,
figure=figure, **self.plot_kwargs)
mlab.triangular_mesh(vertices[:, 0], vertices[:, 1], vertices[:, 2], self._source.triangles,
figure=figure, **self.plot_kwargs)
7 changes: 4 additions & 3 deletions raysect_mayavi/mayavi/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class ObserverVisualiser(MayaviVisualiser):
"""
Class for graphical representation of the Raysect Box primitive.

:param raysect_object: Raysect Observer instance
"""

Expand All @@ -25,8 +26,8 @@ def _init_plot_kwargs(self):
def set_source(self, source):

if not isinstance(source, ObserverSource):
raise TypeError("source has to be instance of SourceBase.")
raise TypeError("source has to be instance of ObserverSource.")

self._source = source

def _mayavi_plot(self, figure):
Expand All @@ -42,4 +43,4 @@ def _mayavi_plot(self, figure):
w = np.array([direction_raysect.z], ndmin=1)

mlab.quiver3d(x, y, z, u, v, w,
figure=figure, **self.plot_kwargs)
figure=figure, **self.plot_kwargs)
9 changes: 4 additions & 5 deletions raysect_mayavi/mayavi/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@


def parse_sources(sources):

visualisers = {}

for key, source in sources.items():
visualisers[key] = assign_visualiser(source)


return visualisers


def assign_visualiser(source):

if not isinstance(source, SourceBase):
raise TypeError("sources items has to be of type SourceBase.")
if isinstance(source, TriangularMeshSource):
Expand All @@ -30,4 +30,3 @@ def assign_visualiser(source):
return ObserverVisualiser(source)
else:
raise TypeError("Source type not recognised")

Loading