Ejemplo n.º 1
0
def test_undense_point_source(session, angular_distribution, wavelengths):
    source = sources.PointSource(
        (0.0, 0.0), 0.0, angular_distribution, wavelengths, dense=False
    )
    result = session.run(source.rays)
    assert source.rays.dtype == tf.float64
    assert result.shape[1] == 5
    assert result.shape[0] == wavelengths.shape[0]
Ejemplo n.º 2
0
def test_simple_params_constant(
    session, center, central_angle, sample_angular_distribution, sample_wavelengths
):
    source = sources.PointSource(
        center,
        central_angle,
        sample_angular_distribution,
        sample_wavelengths,
        dense=True,
    )
    result = session.run(source.rays)
    assert source.rays.dtype == tf.float64
    assert result.shape == (25, 5)
Ejemplo n.º 3
0
def test_simple_params_feed(
    session, center, central_angle, sample_angular_distribution, sample_wavelengths
):
    source = sources.PointSource(
        feed_center,
        feed_central_angle,
        sample_angular_distribution,
        sample_wavelengths,
        dense=True,
    )
    result = session.run(
        source.rays, feed_dict={feed_center: center, feed_central_angle: central_angle}
    )
    assert source.rays.dtype == tf.float64
    assert result.shape == (25, 5)
Ejemplo n.º 4
0
import tfrt.distributions as dist
import tfrt.drawing as drawing
import tfrt.sources as sources
import tfrt.boundaries as boundaries
import tfrt.engine as engine

PI = tf.constant(pi, dtype=tf.float64)

# Set up the source
angular_size = PI / 8
print(f"Angular Size: {np.degrees(angular_size)}")
#sphere = dist.StaticUniformSphere(angular_size, 100000)
#sphere = dist.RandomUniformSphere(angular_size, 100000)
#sphere = dist.StaticLambertianSphere(angular_size, 100000)
sphere = dist.RandomLambertianSphere(angular_size, 100000)
source = sources.PointSource(3, (0, 0, 0), (1, 0, 0), sphere, [drawing.YELLOW])
source.frozen = True
print(f"Minimum phi generated: {np.degrees(tf.reduce_min(sphere.ranks[:,0]))}")
print(f"Maximum phi generated: {np.degrees(tf.reduce_max(sphere.ranks[:,0]))}")

# Set up the target
angular_step_size = 5
plane_mesh = pv.Plane((1, 0, 0), (1, 0, 0), i_size=.1, j_size=.1).triangulate()
target = boundaries.ManualTriangleBoundary(mesh=plane_mesh)

# set up the system
system = engine.OpticalSystem3D()
system.sources = [source]
system.targets = [target]

eng = engine.OpticalEngine(3, [], optical_system=system)
Ejemplo n.º 5
0
import tensorflow as tf
import numpy as np

import tfrt.sources as sources
import tfrt.distributions as distributions
import tfrt.drawing as drawing

PI = sources.PI

source = sources.PrecompiledSource(2, do_downsample=False)

angles = distributions.RandomUniformAngularDistribution(-PI / 4, PI / 4, 5)
sampling_source = sources.PointSource(2, (0, 0), 0, angles, [drawing.YELLOW])

samples = [sampling_source.snapshot() for i in range(5)]
source.from_samples(samples)

for field, item in source.items():
    print(f"{field}: {item}")
from tfrt.spectrumRGB import rgb

angular_size = itertools.cycle(
    np.array([PI / 2, PI / 3, PI / 4, PI / 8, PI / 12, PI / 48],
             dtype=np.float64))
sample_count = itertools.cycle([5, 9, 25])
center = itertools.cycle([(0, 0), (3, 0), (0, 3)])
central_angle = itertools.cycle([0, PI / 4, PI / 2, PI, -PI / 2])

# build the source rays
start_angle = next(angular_size)
angles = distributions.StaticUniformAngularDistribution(
    -start_angle, start_angle, next(sample_count))
source = sources.PointSource(2,
                             next(center),
                             next(central_angle),
                             angles, [drawing.YELLOW],
                             dense=True)

fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.set_aspect("equal")
ax.set_xbound(-4, 4)
ax.set_ybound(-4, 4)
drawer = drawing.RayDrawer2D(ax)
drawing.disable_figure_key_commands()


def redraw():
    source.update()
    drawer.rays = source
    drawer.draw()
Ejemplo n.º 7
0
import tfrt.sources as sources
import tfrt.distributions as distributions
import tfrt.drawing as drawing
import tfrt.boundaries as boundaries
import tfrt.engine as engine
import tfrt.operation as operation
import tfrt.materials as materials

PI = tf.constant(pi, dtype=tf.float64)

# build the source rays
#base_points = distributions.StaticUniformSquare(1, 5)
angles = distributions.StaticUniformSphere(PI / 8.0, 25)
source = sources.PointSource(3, (-1, 0, 0), (1, 0, 0),
                             angles,
                             drawing.RAINBOW_6,
                             dense=True)
source.frozen = True

# build the boundary
surface1 = boundaries.ManualTriangleBoundary(
    file_name="./stl/short_pyramid.stl",
    material_dict={
        "mat_in": 1,
        "mat_out": 0
    })
surface2 = boundaries.ManualTriangleBoundary(mesh=pv.Sphere(radius=.5,
                                                            center=(1, 0, 0)),
                                             material_dict={
                                                 "mat_in": 1,
                                                 "mat_out": 0
    drawer.draw()
    drawing.redraw_current_figure()


if __name__ == "__main__":
    drawing.disable_figure_key_commands()
    # set up the figure and axes
    fig, ax = plt.subplots(1, 1, figsize=(9, 9))

    # configure axes
    ax.set_aspect("equal")
    ax.set_xbound(-2, 2)
    ax.set_ybound(-2, 2)

    angles = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 5)
    source = sources.PointSource((0.0, 0.0), 0.0, angles, [drawing.YELLOW])
    
    engine = eng.OpticalEngine(2, [op.OldestAncestor(), op.StandardReaction()])
    system = eng.OpticalSystem2D()
    system.sources = [source]
    system.update()

    # set up drawer
    drawer = drawing.RayDrawer2D(ax)
    drawer.rays = source
    drawer.draw()

    # hand over to user
    fig.canvas.mpl_connect(
        "key_press_event",
        lambda event: on_key(event, drawer, source, system),
import tfrt.engine as engine
import tfrt.materials as materials
import tfrt.boundaries as boundaries
import tfrt.operation as operation

PI = tf.constant(math.pi, dtype=tf.float64)
sample_count = 1000
angular_cutoff = PI / 2

plot = pv.Plotter()
plot.add_axes()

# build and draw the source
visual_dist = distributions.SquareRankLambertianSphere(sample_count,
                                                       angular_cutoff)
visual_source = sources.PointSource(3, (5, 0, 0), (0, 0, 1), visual_dist,
                                    [drawing.YELLOW])

test_dist = distributions.SquareRankLambertianSphere(1000 * sample_count,
                                                     angular_cutoff)
test_source = sources.PointSource(3, (5, 0, 0), (0, 0, 1), test_dist,
                                  [drawing.YELLOW])

ray_drawer = drawing.RayDrawer3D(plot, visual_source)
ray_drawer.draw()

# draw the ranks and circle points, with lines between them, showing the square-circle
# transformation
fake_rays = {
    "x_start": visual_dist.ranks[:, 0],
    "y_start": visual_dist.ranks[:, 1],
    "z_start": tf.zeros((sample_count, ), dtype=tf.float64),
Ejemplo n.º 10
0
)

focal_plane = np.array([[
    -max_target_size, -height, max_target_size, -height
], [
    -2.0 * max_target_size, -height, -max_target_size, 10.0 * max_target_size
], [max_target_size, -height, 2.0 * max_target_size, 10.0 * max_target_size]],
                       dtype=np.float64)

# build the source
angular_distribution = sources.StaticLambertianAngularDistribution(
    -source_angular_cutoff, source_angular_cutoff, ray_count)
central_angle_placeholder = tf.placeholder(tf.float64,
                                           name="central_angle_placeholder")
source = sources.PointSource((0.0, 0.0),
                             central_angle_placeholder,
                             angular_distribution, [drawing.YELLOW],
                             ray_length=10.0)


def polynomial(name, max_order, min_order=0, dtype=tf.float64):
    """
        Generates a parametrized polynomial, for use in interpolating stuff.
        Automatically creates a tf variable to store its parametrization.
        
        x : float tensor
            The independant variable used to evaluate the polynomial.
        max_order : int
            The maximum power used in the polynomial.
        min_order : int
            The minumum power used in the polynomial.
            
import tfrt.distributions as dist
import tfrt.sources as sources

d = dist.StaticUniformAngularDistribution(-1, 1, 3)
s = sources.PointSource(2, (0, 0), 0, d, [.5, .6, .7], dense=True, rank_source=("angle", "ranks"))

print(f"source._ranks: {s._ranks}")
print(f"source[ranks]: {s['ranks']}")
Ejemplo n.º 12
0
import matplotlib as mpl
import matplotlib.pyplot as plt
import tensorflow as tf
import math

import tfrt.distributions as distributions
import tfrt.sources as sources
import tfrt.drawing as drawing
import tfrt.engine as eng
import tfrt.operation as op

PI = math.pi

a1 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 5)
s1 = sources.PointSource((0.0, 0.0), 0.0, a1, [drawing.YELLOW])

a2 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 1)
s2 = sources.PointSource((0.0, 1.0), 0.0, a2, [drawing.YELLOW])

a3 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 7)
s3 = sources.PointSource((0.0, 2.0), 0.0, a3, [drawing.YELLOW])

engine = eng.OpticalEngine(2, [op.OldestAncestor()])
system = eng.OpticalSystem2D()
engine.optical_system = system
system.sources = [s1, s2, s3]
engine.annotate()
engine.update()

print(f"system validation:")
engine.validate_system()
import tfrt.sources as sources
import tfrt.distributions as distributions
import tfrt.drawing as drawing
import tfrt.boundaries as boundaries
import tfrt.engine as engine
import tfrt.operation as operation
import tfrt.materials as materials
import tfrt.graph as graph

PI = tf.constant(pi, dtype=tf.float64)

# build the source rays
angles = distributions.ManualAngularDistribution([(1, 0, 0)])
source = sources.PointSource(3, (-5, 0, 0), (1, 0, 0),
                             angles, [drawing.YELLOW],
                             dense=False,
                             rank_type=None)

# build the boundaries
zero_points = pv.PolyData(np.array([(0, 0, 1), (0, -1, -1), (0, 1, -1)]),
                          np.array([3, 0, 1, 2]))

# do the mesh tricks
top_parent = graph.get_closest_point(zero_points, (0, 0, 0))
vertex_update_map, accumulator = graph.mesh_parametrization_tools(
    zero_points, top_parent)

print(f"accumulator: {accumulator}")
print(f"vertex update map: {vertex_update_map}")

vg = boundaries.FromVectorVG((1, 0, 0))
def plot_angle_test(session, ax, x, angle):
    # generate and draw the starting rays
    ray_count = 11
    angular_distribution = sources.StaticUniformAngularDistribution(
        -0.9 * PI / 2.0, 0.9 * PI / 2.0, ray_count
    )
    wavelengths = np.linspace(drawing.VISIBLE_MIN, drawing.VISIBLE_MAX, ray_count)

    external_rays = sources.PointSource(
        (x, 0.25),
        angle,
        angular_distribution,
        wavelengths,
        start_on_center=False,
        dense=False,
        ray_length=0.1,
    )
    external_drawer = drawing.RayDrawer(ax, rays=session.run(external_rays.rays))
    external_drawer.draw()

    internal_rays = sources.PointSource(
        (x, -0.25),
        angle + PI,
        angular_distribution,
        wavelengths,
        start_on_center=False,
        dense=False,
        ray_length=0.1,
    )
    internal_drawer = drawing.RayDrawer(ax, rays=session.run(internal_rays.rays))
    internal_drawer.draw()

    # generate and draw the boundary
    segment_length = 0.2
    segment_angle = angle + PI / 2.0
    segments = np.array(
        [
            [
                x + segment_length * math.cos(segment_angle),
                0.25 + segment_length * math.sin(segment_angle),
                x - segment_length * math.cos(segment_angle),
                0.25 - segment_length * math.sin(segment_angle),
            ],
            [
                x + segment_length * math.cos(segment_angle),
                -0.25 + segment_length * math.sin(segment_angle),
                x - segment_length * math.cos(segment_angle),
                -0.25 - segment_length * math.sin(segment_angle),
            ],
        ]
    )
    segment_drawer = drawing.SegmentDrawer(
        ax, segments=segments, color=(0, 1, 1), draw_norm_arrows=True
    )
    segment_drawer.draw()

    # react the rays and draw the reactions
    n_in = tf.cast(1.5, tf.float64)
    n_out = tf.cast(1.0, tf.float64)
    angle = tf.cast(angle, tf.float64)
    external_reacted = geometry.ray_reaction(
        external_rays.rays, angle, n_in, n_out, new_ray_length=0.2
    )
    ex_reacted_drawer = drawing.RayDrawer(
        ax, rays=session.run(external_reacted), style="--"
    )
    ex_reacted_drawer.draw()

    internal_reacted = geometry.ray_reaction(
        internal_rays.rays, angle, n_in, n_out, new_ray_length=0.2
    )
    in_reacted_drawer = drawing.RayDrawer(
        ax, rays=session.run(internal_reacted), style="--"
    )
    in_reacted_drawer.draw()
        angular_distribution,
        base_point_distribution,
        drawing.RAINBOW_6,
        name="AngularSource",
        dense=True,
        start_on_base=True,
        ray_length=1.0,
    )
    
    angles_2 = distributions.RandomUniformAngularDistribution(
        -PI / 4.0, PI / 4.0, 6, name="StaticUniformAngularDistribution"
    )
    fixed_source = sources.PointSource(
        [-1.0, 0.0],
        PI,
        angles_2,
        drawing.RAINBOW_6,
        dense=False
    )
    
    optical_system = engine.OpticalSystem2D()
    optical_system.sources = [movable_source, fixed_source]
    optical_system.update()

    """# Make an optical surface, so we can check that the rays are oriented
    # correctly
    segment_boundary = np.array([[1, -2, 1, 2, 1, 0]], dtype=np.float64)
    material_list = [materials.vacuum, materials.acrylic]

    # Make a ray tracer.  Concat all the ray types into traced_rays, for ease of
    # use