def pattern(distance):
    ray = odak.raytracing()
    source0 = np.array([1., 0., 0.])
    source1 = np.array([-1., 0., 0.])
    rectx = 20.
    recty = 20.
    nx = 1000
    ny = 1000
    wavelength = 532 * 10**-9
    result = np.zeros((nx, ny))
    for ix in range(0, nx):
        for iy in range(0, ny):
            x = rectx * 2. / nx * ix - rectx
            y = recty * 2. / ny * iy - recty
            point = np.array([x, y, distance])
            z0 = np.sqrt(np.sum((point - source0)**2)) * 2 * np.pi / wavelength
            z1 = np.sqrt(np.sum((point - source1)**2)) * 2 * np.pi / wavelength
            pd = np.abs(z0 - z1)
            result[ix, iy] = 1. + np.cos(pd)
            y = 0


#    plt.imshow(result,cmap='gray')
#    plt.colorbar()
#    plt.show()
    scipy.misc.imsave('/output/result_%s.png' % distance, result)
    print(distance)
    return True
Example #2
0
def example_of_ray_tracing_3():
    ray         = odak.raytracing()
    # Be careful points and pitch must be float
    pitch       = 10.
    cornercube0 = ray.plotcornercube(0,0,0,pitch,revert=False)
    cornercube1 = ray.plotcornercube(pitch/math.sqrt(3),-pitch/3,0,pitch,revert=True)
    vector0     = ray.createvector((1.,2.1,10.),(90,90,0))
    vector1     = ray.createvector((1.,1.,10.),(90,90,0))
    vector2     = ray.createvector((4.,-4.,10.),(90,90,0))
    vectorlist  = [vector0,vector1,vector2]
    cornercubes = [cornercube0,cornercube1]
    for vectors in vectorlist:
        for cubes in cornercubes:
            rayslist          = []
            distance          = 2
            rayslist.append(vectors)
            for rays in rayslist:
                if len(rayslist) > 3:
#                    rays[1] = -rays[1]
                    ray.plotvector(rays,10)
                    break
                for points in cubes:
                    distance,normvec = ray.findintersurface(rays,(points[0],points[1],points[2]))
                    if ray.isitontriangle(normvec[0],points[0],points[1],points[2]) == True:
                        ray.plotvector(normvec,pitch/10,'r')
                        ray.plotvector(rays,distance)
                        reflectvector = ray.reflect(rays,normvec)
                        rayslist.append(reflectvector)
    ray.defineplotshape((-5,5),(-5,5),(0,10))
    ray.showplot()
    return True
Example #3
0
def pattern(distance):
    ray      = odak.raytracing()
    source0    = np.array([ 1.,0.,0.])
    source1    = np.array([-1.,0.,0.])
    rectx      = 20.
    recty      = 20.
    nx         = 1000
    ny         = 1000
    wavelength = 532*10**-9
    result     = np.zeros((nx,ny))
    for ix in range(0,nx): 
        for iy in range(0,ny):
            x     = rectx*2./nx*ix-rectx
            y     = recty*2./ny*iy-recty
            point = np.array([x,y,distance])
            z0    = np.sqrt(np.sum((point-source0)**2))*2*np.pi/wavelength
            z1    = np.sqrt(np.sum((point-source1)**2))*2*np.pi/wavelength
            pd    = np.abs(z0-z1)
            result[ix,iy] = 1.+np.cos(pd)
            y=0
#    plt.imshow(result,cmap='gray')
#    plt.colorbar()
#    plt.show()
    scipy.misc.imsave('/output/result_%s.png' % distance,result)
    print(distance)
    return True
Example #4
0
def example_of_ray_tracing_2():
    ray               = odak.raytracing()
    point             = (0,math.sqrt(3)/2,1)
    point0            = (1,0,1)
    point1            = (0,math.sqrt(3),0)
    point2            = (-1,0,1)
    ray.plottriangle(point0,point1,point2)
    vector            = ray.createvector((0,1,5),(90,90,0))
    distance,normvec  = ray.findintersurface(vector,(point0,point1,point2))
    if ray.isitontriangle(normvec[0],point0,point1,point2) == True:
        ray.plotvector(normvec,distance)
        ray.plotvector(vector,distance)
        reflectvector     = ray.reflect(vector,normvec)
        ray.plotvector(reflectvector,distance)
    ray.defineplotshape((-20,20),(-20,20),(-20,20))
    ray.showplot()
    return True
Example #5
0
def example_of_ray_tracing_2():
    ray = odak.raytracing()
    ray.PlotInit()
    point = (0, math.sqrt(3) / 2, 1)
    point0 = (1, 0, 1)
    point1 = (0, 1, 0)
    point2 = (-1, 0, 1)
    ray.plottriangle(point0, point1, point2)
    vector = ray.createvector((0, 0.5, 5), (90, 90, 0))
    distance, normvec = ray.findintersurface(vector, (point0, point1, point2))
    if ray.isitontriangle(normvec[0], point0, point1, point2) == True:
        ray.plotvector(normvec, distance, 'r')
        ray.plotvector(vector, distance, 'g')
        reflectvector = ray.reflect(vector, normvec)
        ray.plotvector(reflectvector, distance, 'b')
    ray.defineplotshape((-20, 20), (-20, 20), (-20, 20))
    ray.showplot()
    return True
Example #6
0
def example_of_ray_tracing():
    n         = 1
    ray       = odak.raytracing()
#    rotvec = ray.transform(vector,(45,0,0),(0.5,1,0))
#    print 'Output vector: \n %s' % rotvec
    spherical = ray.plotsphericallens(20,0,0,10)
    for angle in xrange(100,120):
        vector            = ray.createvector((0,5,5),(45,angle,angle))
        distance,normvec  = ray.findinterspher(vector,spherical)
        if distance != 0:      
            ray.plotvector(vector,distance)
            refractvector = ray.snell(vector,normvec,1.0,1.51)
            #ray.plotvector(refractvector,20)
            #reflectvector     = ray.reflect(vector,normvec)
            #ray.plotvector(reflectvector,distance)
        distance,normvec  = ray.findinterspher(refractvector,spherical)
        if distance != 0:
            ray.plotvector(refractvector,distance)
            refractvector2 = ray.snell(refractvector,normvec,1.51,1.)
            ray.plotvector(refractvector2,20)
    ray.showplot()
    return True
Example #7
0
def example_of_ray_tracing_3():
    ray = odak.raytracing()
    ray.PlotInit()
    # Be careful points and pitch must be float
    pitch = 10.
    cornercube0 = ray.plotcornercube(0, 0, 0, pitch, revert=False)
    cornercube1 = ray.plotcornercube(pitch / math.sqrt(3),
                                     -pitch / 3,
                                     0,
                                     pitch,
                                     revert=True)
    vector0 = ray.createvector((1., 2.1, 10.), (90, 90, 0))
    vector1 = ray.createvector((1., 1., 10.), (90, 90, 0))
    vector2 = ray.createvector((4., -4., 10.), (90, 90, 0))
    vectorlist = [vector0, vector1, vector2]
    cornercubes = [cornercube0, cornercube1]
    for vectors in vectorlist:
        for cubes in cornercubes:
            rayslist = []
            distance = 2
            rayslist.append(vectors)
            for rays in rayslist:
                if len(rayslist) > 3:
                    #                    rays[1] = -rays[1]
                    ray.plotvector(rays, 10)
                    break
                for points in cubes:
                    distance, normvec = ray.findintersurface(
                        rays, (points[0], points[1], points[2]))
                    if ray.isitontriangle(normvec[0], points[0], points[1],
                                          points[2]) == True:
                        ray.plotvector(normvec, pitch / 10, 'r')
                        ray.plotvector(rays, distance)
                        reflectvector = ray.reflect(rays, normvec)
                        rayslist.append(reflectvector)
    ray.defineplotshape((-5, 5), (-5, 5), (0, 10))
    ray.showplot()
    return True
Example #8
0
def example_of_ray_tracing():
    n = 1
    ray = odak.raytracing()
    ray.PlotInit()
    #    rotvec = ray.transform(vector,(45,0,0),(0.5,1,0))
    #    print 'Output vector: \n %s' % rotvec
    spherical = ray.plotsphericallens(20, 0, 0, 10)
    for angle in xrange(100, 120):
        vector = ray.createvector((0, 5, 5), (45, angle, angle))
        distance, normvec = ray.findinterspher(vector, spherical)
        if distance != 0:
            ray.plotvector(vector, distance)
            refractvector = ray.snell(vector, normvec, 1.0, 1.51)
            #ray.plotvector(refractvector,20)
            #reflectvector     = ray.reflect(vector,normvec)
            #ray.plotvector(reflectvector,distance)
        distance, normvec = ray.findinterspher(refractvector, spherical)
        if distance != 0:
            ray.plotvector(refractvector, distance)
            refractvector2 = ray.snell(refractvector, normvec, 1.51, 1.)
            ray.plotvector(refractvector2, 20)
    ray.showplot()
    return True
Example #9
0
def Solve(ds,
          dhp,
          r,
          dpp,
          dea,
          dwe,
          tel,
          nel,
          nie,
          nair,
          dpl,
          xel,
          yel,
          DetPos,
          ShowPlot=False):
    # Z position of the lens is calculated.
    zel = dpl - tel
    # Define the center of the first circular pinhole in 3D space.
    HoleCenter1 = (-dhp / 2, 0, 0)
    # Define the radius of the first circular pinhole in mm.
    Radius1 = r
    # Define the center of the second circular pinhole in 3D space.
    HoleCenter2 = (dhp / 2, 0, 0)
    # Define the radius of the second circular pinhole in mm.
    Radius2 = r
    # Call the library to trace.
    ray = odak.raytracing()
    # Position of the first point source.
    Point1 = (dpp / 2, 0, ds)
    # Position of the second point source.
    Point2 = (-dpp / 2, 0, ds)
    # First section of the solution for first point source and first pinhole.
    # Exterior points on the pinholes.
    PointList1 = [HoleCenter1]
    PointList2 = [HoleCenter2]
    SourceList = [Point1, Point2]
    # Add additional points on the exterior of the pinholes.
    for k in xrange(0, 360, 60):
        # Converting degrees to radians.
        k = ray.DegreesToRadians(k)
        # Appending new item to the point lists.
        PointList1.append((HoleCenter1[0] + Radius1 * cos(k),
                           HoleCenter1[1] + Radius1 * sin(k), HoleCenter1[2]))
        PointList2.append((HoleCenter2[0] + Radius2 * cos(k),
                           HoleCenter2[1] + Radius2 * sin(k), HoleCenter2[2]))
    # Plot a spherical lens.


#    AsphericalLens = ray.plotasphericallens(xel,yel,zel,dea,dea,tel,nel,'b')
# Create the whole eye ball.
    EyeBall = (xel, yel, zel - dwe, dwe)
    # Plot the eye ball.
    SphericalLens = ray.plotsphericallens(EyeBall[0], EyeBall[1], EyeBall[2],
                                          EyeBall[3], 'y', 0.1)
    # Create a dummy spherical lens to find the intersection point of the incoming ray with aspherical lens.
    DummySL1 = (xel, yel, zel - dea, dea)
    # Plot a spherical lens.
    SphericalLens = ray.plotsphericallens(DummySL1[0], DummySL1[1],
                                          DummySL1[2], DummySL1[3], 'g', 0.1)
    # Calculate and print power of the spherical lens.
    D = (nel - nair) * (pow(dea, -1) - pow(-dea, -1) +
                        (nel - nair) * 2 * dea / nel / (-pow(dea, 2)))
    #    print 'Power of the spherical lens: ', 1000.*D
    #    print 'Focal length of the spherical lens (mm): ', 1./D
    # Calculate the effective focal length of a ball lens.
    # See http://www.edmundoptics.com/technical-resources-center/optics/understanding-ball-lenses/
    EFL = nel * 2 * dea / 4 / (nel - nair)
    #    print 'Effective focal length of the ball lens (mm):', EFL
    # Arrays to save rays by their source of origin.
    rays1 = []
    rays2 = []
    # Plot the pinholes.
    ray.PlotCircle(HoleCenter1, Radius1)
    ray.PlotCircle(HoleCenter2, Radius2)
    # Iterate ray tracing for every point source.
    for origin in SourceList:
        # Make destination choice according to the source, color the rays accordingly.
        if origin == Point1:
            DestinationList = PointList1
            RayColor = 'r'
        elif origin == Point2:
            DestinationList = PointList2
            RayColor = 'b'
        # Iterate ray tracing for every destination.
        for item in DestinationList:
            # Finding the angle in between the origin and the destination point.
            angles = ray.findangles(origin, item)
            # Creating the new vector in the direction of destination using the previous angles calculated.
            NewRay = ray.createvector(origin, angles)
            # Chiefray color selection.
            if item == DestinationList[0]:
                color = RayColor + '+--'
            else:
                color = RayColor + '+-'
            # Intersect the vector with dummy sphere.
            distance, normvec = ray.findinterspher(NewRay, DummySL1)
            # Plot the ray from origin to the destination.
            if distance != 0:
                ray.plotvector(NewRay, distance, color)
                # Storing ray to the rays vector by their source of origin.
                if origin == Point1:
                    rays1.append(NewRay)
                else:
                    rays2.append(NewRay)
                # Refracting into the eye.
                RefractRay = ray.snell(NewRay, normvec, nair, nel)
                # The ray travels inside the eye ball.
                distance, normvec = ray.findinterspher(RefractRay, DummySL1)
                # Plot the refracting ray.
                ray.plotvector(RefractRay, distance, color)
                # Refracting to outside of the eye.
                RefractOutsideRay = ray.snell(RefractRay, normvec, nel, nie)
                # Shine rays to the retina.
                distance, normvec = ray.findinterspher(RefractOutsideRay,
                                                       EyeBall)
                # Find the intersection of the refracted ray with the detector surface.
                #                distance,normvec = ray.findintersurface(RefractOutsideRay,(DetPos[0],DetPos[1],DetPos[2]))
                #                # Plot the refracting ray.
                ray.plotvector(RefractOutsideRay, distance, color)
    # Loop to find intersection of the plotted rays.
    InterPoints = []
    for RaySource1 in rays1:
        for RaySource2 in rays2:
            # Find the intersection of two rays.
            intersection, distances = ray.CalculateIntersectionOfTwoVectors(
                RaySource1, RaySource2)
            # Check if the calculated distance value has a logical value.
            CheckValue = "%.2f" % abs(distances[0])
            if CheckValue != '0.00' and float(CheckValue) < 100000:
                ray.PlotPoint(intersection, 'go', False, True)
                # Storing intersection point in a list.
                InterPoints.append(intersection)
    # Transpose of the InterPoints 2D list.
    l = map(list, map(None, *InterPoints))
    # Finding Voxel height.
    m1 = max(l[2])
    m2 = min(l[2])
    VoxelHeight = abs(m1 - m2)
    # Finding Voxel width.
    m3 = max(l[0])
    m4 = min(l[0])
    VoxelWidth = abs(m3 - m4)
    # Show the ray tracing envorinment in three-dimensional space.
    if ShowPlot == True:
        limit = 0.8 * ds
        ray.defineplotshape((-limit, limit), (-limit, limit), (-limit, limit))
        ray.showplot()
    else:
        # Otherwise destroy figure.
        ray.CloseFigure()
    return VoxelHeight, VoxelWidth
Example #10
0
def Solve(ds,dhp,r,dpp,dea,dwe,tel,nel,nie,nair,dpl,xel,yel,DetPos,ShowPlot=False):
    # Z position of the lens is calculated.
    zel         = dpl-tel
    # Define the center of the first circular pinhole in 3D space.
    HoleCenter1 = (-dhp/2,0,0)
    # Define the radius of the first circular pinhole in mm.
    Radius1     = r
    # Define the center of the second circular pinhole in 3D space.
    HoleCenter2 = (dhp/2,0,0)
    # Define the radius of the second circular pinhole in mm.
    Radius2     = r
    # Call the library to trace.
    ray         = odak.raytracing()
    # Position of the first point source.
    Point1      = (dpp/2,0,ds)
    # Position of the second point source.
    Point2      = (-dpp/2,0,ds)
    # First section of the solution for first point source and first pinhole.
    # Exterior points on the pinholes.
    PointList1 = [HoleCenter1] 
    PointList2 = [HoleCenter2] 
    SourceList = [Point1,
                  Point2
                 ]
    # Add additional points on the exterior of the pinholes.
    for k in xrange(0,360,60):
        # Converting degrees to radians.
        k = ray.DegreesToRadians(k)
        # Appending new item to the point lists.
        PointList1.append((HoleCenter1[0]+Radius1*cos(k),HoleCenter1[1]+Radius1*sin(k),HoleCenter1[2]))
        PointList2.append((HoleCenter2[0]+Radius2*cos(k),HoleCenter2[1]+Radius2*sin(k),HoleCenter2[2]))
    # Plot a spherical lens.
#    AsphericalLens = ray.plotasphericallens(xel,yel,zel,dea,dea,tel,nel,'b')
    # Create the whole eye ball.
    EyeBall  = (xel,yel,zel-dwe,dwe)
    # Plot the eye ball.
    SphericalLens = ray.plotsphericallens(EyeBall[0],EyeBall[1],EyeBall[2],EyeBall[3],'y',0.1)
    # Create a dummy spherical lens to find the intersection point of the incoming ray with aspherical lens.
    DummySL1 = (xel,yel,zel-dea,dea)
    # Plot a spherical lens.
    SphericalLens = ray.plotsphericallens(DummySL1[0],DummySL1[1],DummySL1[2],DummySL1[3],'g',0.1)
    # Calculate and print power of the spherical lens.
    D = (nel-nair)*(pow(dea,-1)-pow(-dea,-1)+(nel-nair)*2*dea/nel/(-pow(dea,2)))
#    print 'Power of the spherical lens: ', 1000.*D
#    print 'Focal length of the spherical lens (mm): ', 1./D     
    # Calculate the effective focal length of a ball lens.
    # See http://www.edmundoptics.com/technical-resources-center/optics/understanding-ball-lenses/
    EFL = nel*2*dea/4/(nel-nair)
#    print 'Effective focal length of the ball lens (mm):', EFL
    # Arrays to save rays by their source of origin.
    rays1    = []
    rays2    = []
    # Plot the pinholes.
    ray.PlotCircle(HoleCenter1,Radius1)
    ray.PlotCircle(HoleCenter2,Radius2)
    # Iterate ray tracing for every point source.
    for origin in SourceList:
        # Make destination choice according to the source, color the rays accordingly.
        if origin == Point1:
            DestinationList = PointList1
            RayColor           = 'r'
        elif origin == Point2:
            DestinationList = PointList2
            RayColor           = 'b'
        # Iterate ray tracing for every destination.
        for item in DestinationList:
            # Finding the angle in between the origin and the destination point.
            angles = ray.findangles(origin,item)
            # Creating the new vector in the direction of destination using the previous angles calculated.
            NewRay = ray.createvector(origin,angles)
            # Chiefray color selection.
            if item == DestinationList[0]:
                color = RayColor + '+--'
            else:
                color = RayColor + '+-'
            # Intersect the vector with dummy sphere.
            distance,normvec = ray.findinterspher(NewRay,DummySL1)  
            # Plot the ray from origin to the destination.
            if distance != 0:
                ray.plotvector(NewRay,distance,color)        
                # Storing ray to the rays vector by their source of origin.
                if origin == Point1:
                    rays1.append(NewRay)
                else:
                    rays2.append(NewRay)
                # Refracting into the eye.
                RefractRay = ray.snell(NewRay,normvec,nair,nel)
                # The ray travels inside the eye ball.
                distance,normvec = ray.findinterspher(RefractRay,DummySL1)
                # Plot the refracting ray.
                ray.plotvector(RefractRay,distance,color)
                # Refracting to outside of the eye.
                RefractOutsideRay = ray.snell(RefractRay,normvec,nel,nie)
                # Shine rays to the retina.
                distance,normvec = ray.findinterspher(RefractOutsideRay,EyeBall)
                # Find the intersection of the refracted ray with the detector surface.
#                distance,normvec = ray.findintersurface(RefractOutsideRay,(DetPos[0],DetPos[1],DetPos[2]))
#                # Plot the refracting ray.
                ray.plotvector(RefractOutsideRay,distance,color)
    # Loop to find intersection of the plotted rays.
    InterPoints = []
    for RaySource1 in rays1:
        for RaySource2 in rays2:
            # Find the intersection of two rays.
            intersection, distances = ray.CalculateIntersectionOfTwoVectors(RaySource1,RaySource2)
            # Check if the calculated distance value has a logical value.
            CheckValue = "%.2f" % abs(distances[0])
            if CheckValue != '0.00' and float(CheckValue) < 100000:
                ray.PlotPoint(intersection,'go',False,True)
                # Storing intersection point in a list.
                InterPoints.append(intersection)
    # Transpose of the InterPoints 2D list.
    l  = map(list,map(None,*InterPoints))
    # Finding Voxel height.
    m1          = max(l[2])
    m2          = min(l[2])
    VoxelHeight = abs(m1-m2) 
    # Finding Voxel width.
    m3         = max(l[0])
    m4         = min(l[0])
    VoxelWidth = abs(m3-m4)
    # Show the ray tracing envorinment in three-dimensional space.
    if ShowPlot == True:
        limit = 0.8*ds
        ray.defineplotshape((-limit,limit),(-limit,limit),(-limit,limit))
        ray.showplot()
    else:
        # Otherwise destroy figure.
        ray.CloseFigure()
    return VoxelHeight,VoxelWidth