Skip to content

Commit

Permalink
Merge with obbtree_development
Browse files Browse the repository at this point in the history
  • Loading branch information
bryancole committed Jan 6, 2022
2 parents 350485e + a98ce0b commit 26fe849
Show file tree
Hide file tree
Showing 20 changed files with 1,881 additions and 228 deletions.
3 changes: 2 additions & 1 deletion README.rst
Expand Up @@ -6,11 +6,12 @@ Raypier is a non-sequential ray-tracing framework for modelling optical systems.

#. It's pretty fast. The tracing algorithms are written in Cython (i.e. C) and use efficient data-structures for handling many thousands of rays.
#. Correctly handles polarisation
#. Sequential and non-sequential tracing.
#. Support for dispersion including a comprehensive library of glass-types (taken from refractive-index.info)
and diffraction-gratings
#. Point Spread Function (PSF) and E-Field evaluation by summation of of Gaussian modes.
#. Paraxial Gaussian mode evaluation covers generally astigmatic modes.
#. Tracing support for conics section and general aspherics (conic + polnomial)
#. Tracing support for conics section, general aspherics (conic + polnomial) and 2D polynomial surfaces.
#. Zurnike polynomial surface distortions.
#. An interactive user-interface with 3D model/ray rendering through VTK. Tracing is performed "live".
#. A modest selection of optic-types including singlet and achromatic doublet lenses (with AR coating), plane-mirrors,
Expand Down
3 changes: 2 additions & 1 deletion doc/source/introduction.rst
@@ -1,11 +1,12 @@
Introduction to Raypier
=======================

Raypier is a non-sequential optical ray-tracing program. It is intended as a
Raypier is a optical ray-tracing program. It is intended as a
design tools for modelling optical systems (cameras, imaging systems, telescopes etc.).

The main features of ray-trace are:
- Non-sequential tracing (no need to specify the order of optical components)
- Optional sequential tracing where the surface-sequence is discovered using a non-sequential initial trace.
- Physical optics propagation with "Gausslet" tracing and beam decomposition
- Nice visualisation of the traced result
- Live update to the traced result as the user adjusts the model
Expand Down
4 changes: 4 additions & 0 deletions doc/source/ray_sources.rst
Expand Up @@ -5,6 +5,10 @@ Ray sources generate the input rays for the model. The Ray-source objects also h
:py:attr:`traced_rays` attribute, as a list of :py:class:`RayCollection` objects. Each item in this list represents one "generation"
of rays.

Ray sources have a :py:attr:`sequential` attribute. When set True, the source will be traced sequentially using a pre-defined list of surfaces.
If no list of surfaces exists, the trace will default to the usual non-sequential mode and the surface-sequence obtained by this trace
will be stored for use in subsequent sequential traces. The stored sequence can be cleared by assigning to the :py:attr:`clear_sequence` event-trait.

Ray source classes are subclasses of :py:class:`raypier.sources.BaseRaySource`. Besides generating the :py:attr:`input_rays` (a :py:class:`RayCollection`
instance) attribute as the input to the model, and holding the trace results in the :py:attr:`traced_rays` member, the source
objects also control the visualisation of the rays. The following visualisation-related attributes are available.
Expand Down
62 changes: 62 additions & 0 deletions examples/extended_polynomial_example.py
@@ -0,0 +1,62 @@

from raypier.tracer import RayTraceModel
from raypier.faces import PlanarFace, ExtendedPolynomialFace, ConicFace
from raypier.shapes import CircleShape
from raypier.general_optic import GeneralLens
from raypier.materials import OpticalMaterial
from raypier.gausslet_sources import CollimatedGaussletSource
from raypier.fields import EFieldPlane
from raypier.probes import GaussletCapturePlane
from raypier.intensity_image import IntensityImageView
from raypier.intensity_surface import IntensitySurface

import numpy

coefs = numpy.array([[0.1, 0.1, 1],
[0.1, 0.2, 0.0],
[0.1, 0.0, 0.0]])

#coefs = numpy.zeros((3,3), 'd')


shape = CircleShape(radius=10.0)
f1 = ExtendedPolynomialFace(z_height=-2.0, curvature=-25.0, conic_const=0.0, norm_radius=5.0, coefs=coefs)
#f1 = ConicFace(z_height=0.0, curvature=-25.0, conic_const=0.0)
f2 = PlanarFace(z_height=5.0)

mat = OpticalMaterial(glass_name="N-BK7")
lens = GeneralLens(shape=shape, surfaces=[f1,f2], materials=[mat])

src = CollimatedGaussletSource(radius=8.0, resolution=6,
origin=(0,0,-15), direction=(0,0,1),
display="wires", opacity=0.2, show_normals=True)
src.max_ray_len=50.0


cap = GaussletCapturePlane(centre = (0,0,50),
direction= (0,0,1),
width=20,
height=20)

field = EFieldPlane(detector=cap,
align_detector=True,
size=100,
width=1,
height=1)

img = IntensityImageView(field_probe=field)
surf = IntensitySurface(field_probe=field)


model = RayTraceModel(optics=[lens], sources=[src], probes=[field,cap],
results=[img,surf])
model.configure_traits()









15 changes: 15 additions & 0 deletions examples/mesh_tracing_example.py
@@ -0,0 +1,15 @@


from raypier.tracer import RayTraceModel
from raypier.meshes import STLFileMesh
from raypier.sources import ParallelRaySource


src = ParallelRaySource(origin=(0.,0.,-50.0)
)

m = STLFileMesh(file_name = "../experiments/monkey.stl", scale_factor=20.0)

model = RayTraceModel(sources=[src], optics=[m])
model.configure_traits()

Binary file added experiments/monkey.stl
Binary file not shown.
136 changes: 136 additions & 0 deletions experiments/render_obb_model.py
@@ -0,0 +1,136 @@

from tvtk.api import tvtk

import numpy as np

from raypier.core.obbtree import OBBTree


def get_monkey_actor():
reader = tvtk.STLReader(file_name="monkey.stl")
m = tvtk.PolyDataMapper(input_connection=reader.output_port)
a = tvtk.Actor(mapper=m)
return a


def show_monkey():
a = get_monkey_actor()

ren = tvtk.Renderer()
ren.add_actor(a)

renwin = tvtk.RenderWindow()
renwin.add_renderer(ren)

iren = tvtk.RenderWindowInteractor(render_window=renwin)
iren.start()

def get_monkey_mesh():
reader = tvtk.STLReader(file_name="monkey.stl")
tris = tvtk.TriangleFilter(input_connection=reader.output_port)
tris.update()
pd = tris.output
points = np.asarray(pd.points)
cells = pd.polys.to_array().reshape(-1,4)[:,1:]
cells = np.ascontiguousarray(cells, dtype=np.int32)
return points.copy(), cells.copy()


def get_monkey_obbtree():
points, cells = get_monkey_mesh()
print(cells.dtype, cells.shape, points.shape)
obbtree = OBBTree(points, cells)
obbtree.max_level = 20
obbtree.number_of_cells_per_node = 8
obbtree.build_tree()
return obbtree


def append_obb_to_dataset(obb, point_list, line_list=None, poly_list=None):
corner = obb.corner
axes = obb.axes
points = [corner, #0
corner + axes[0], #1
corner + axes[1], #2
corner + axes[2], #3
corner + axes[0] + axes[1], #4
corner + axes[0] + axes[2], #5
corner + axes[1] + axes[2], #6
corner + axes[0] + axes[1] + axes[2]] #7
if any(any(not np.isfinite(a) for a in pt) for pt in points):
return
start = len(point_list)
point_list.extend(points)
if line_list is not None:
lines = [(0,1),(0,2),(0,3),(2,6),(3,6),(6,7),(1,4),(1,5),(4,7),(5,7),(2,4),(3,5)]
for i, line in enumerate(lines):
this = (start + line[0], start + line[1])
line_list.append(this)

if poly_list is not None:
tris = [
(0,1,2), (1,2,4),
(0,1,3), (1,3,5),
(0,2,3), (2,3,6),
(7,3,5), (7,6,3),
(7,5,1), (7,1,4),
(7,6,2), (7,2,4)
]
for i, tri in enumerate(tris):
this = tuple(start + vtx for vtx in tri)
poly_list.append(this)


def add_subtree(obb, points, lines, polys, level=-1):
append_obb_to_dataset(obb, points, lines, polys)
if level == 0:
return
level -= 1
if obb.child1 is not None:
add_subtree(obb.child1, points, lines, polys, level=level)
if obb.child2 is not None:
add_subtree(obb.child2, points, lines, polys, level=level)


def show_monkey_tree():
obbtree = get_monkey_obbtree()

print(obbtree.intersect_with_line([0.,0.,0.], [10.,10.,10.]))

print("Level", obbtree.level)

points = []
lines = []
polys=[]

add_subtree(obbtree.root, points, lines, polys, level=4)

points = np.array(points)
lines = np.array(lines)
polys = np.array(polys)

#print(points)
#print(polys)

pd = tvtk.PolyData(points=points, lines=lines, polys=polys)

mapper = tvtk.PolyDataMapper(color_mode=2)
mapper.set_input_data(pd)
act = tvtk.Actor(mapper=mapper)
act.property.opacity=0.1
ren = tvtk.Renderer()
ren.add_actor(act)

mk = get_monkey_actor()
ren.add_actor(mk)

renwin = tvtk.RenderWindow()
renwin.add_renderer(ren)
iren = tvtk.RenderWindowInteractor()
iren._set_render_window(renwin)
iren.start()


if __name__=="__main__":
show_monkey_tree()

32 changes: 29 additions & 3 deletions notebooks/2d FFT testing.ipynb
Expand Up @@ -158,6 +158,32 @@
"fft.fftfreq?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"print(\"hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# My title\n",
"\n",
"This is cool"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -168,9 +194,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:raypier]",
"display_name": "Python [conda env:raytrace]",
"language": "python",
"name": "conda-env-raypier-py"
"name": "conda-env-raytrace-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -182,7 +208,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions notebooks/FresnelEquations.ipynb
Expand Up @@ -589,9 +589,9 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python [conda env:myenv2]",
"display_name": "Python [conda env:raytrace]",
"language": "python",
"name": "conda-env-myenv2-py"
"name": "conda-env-raytrace-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -603,7 +603,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.7.10"
},
"toc": {
"base_numbering": 1,
Expand Down
6 changes: 3 additions & 3 deletions notebooks/General_Astigmatic_Gaussian_Beams.ipynb
Expand Up @@ -2056,9 +2056,9 @@
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python [conda env:myenv2]",
"display_name": "Python [conda env:raytrace]",
"language": "python",
"name": "conda-env-myenv2-py"
"name": "conda-env-raytrace-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -2070,7 +2070,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.7.10"
},
"toc": {
"base_numbering": 1,
Expand Down

0 comments on commit 26fe849

Please sign in to comment.