In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to realize ray tracing in Python optical simulation tutorial". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python optical simulation tutorial how to achieve ray tracing" it!
Ray tracing
Thanks to the computing power of the computer, the performance of the optical system can be described more accurately by tracking the propagation trajectory of representative light, so the ray tracing method can greatly expand its power, such as Zemax, tracepro and other software provide the corresponding functions.
The ray tracing method based on refraction law requires lower mathematical skills, so it is more suitable to be used as an entry project for python beginners. In the following chapter, I hope to master the basic concepts of list, tuple, dictionary, collection and other data types in python through the implementation of ray tracing, and have a basic understanding of object-oriented and functional programming.
Geometric abstract light
Ray tracing describes the changes in the behavior of light rays after they come into contact with optical elements, so light and optical elements need to be characterized first. Since we hope to eventually establish a two-dimensional light path system, the light may be either horizontal or vertical, so a ray is represented by a x + b y + c = 0. That is, as long as $a b c $these three numbers, you can determine a straight line in two-dimensional space.
In python, a set of data can be represented by the sequence-list. In list, each element is assigned a number as an index, and the index can be used to extract or change the value in list. It is important to note that, like many programming languages, indexing in python starts at 0. So we can create a new variable in python to represent a straight line with a slope of 1 across the origin:
> abc= [1 abc [1]] # in python, list is indicated in square brackets > type (abc) # type function can return data types > abc [0] # in python, the values in square brackets can be changed > > abc [- 1] # in python Index-1 often represents the penultimate value 0 > > abc # modified abc [1,5,0]
Of course, light is essentially a kind of active ray, so it is necessary to make a symbolic agreement on the linear equation a x + b y + c = 0 so as to clarify its direction.
Considering that [a] and [- a] can represent the same line, we can indicate their direction by the positive and negative signs of an and b. Several sets of different symbolic conventions can be obtained by defining the positive and negative sign of [arecom b] in the quadrant of the ray end point. Here we choose the following convention:
We can see that the equations of the first and third rays represent the same straight line, but due to the introduction of symbolic conventions, we can distinguish the differences in their directions so that we can judge whether this ray will intersect with an optical element.
If you want to draw a ray, you can call python's UI library wxpython, which will be explained later in order not to affect the consistency of the content.
In addition to the abc representation of the line recommended here, we can also represent the light by the starting point and angle, and we can also use three parameters [x0memy0memtheta]. However, when carrying out the calculation, it needs to be converted into the representation of [aformab], so it is necessary to define the conversion function, that is, the function of generating rays through the initial point and angle, taking into account the previous symbolic convention, the conversion formula is
One thing to note is that you need to call the numpy package to use trigonometric functions.
Import numpy as npx0,y0,theta = [0re0jnp.piple 4] # np.pi, that is, pi abc = [np.sin (theta), # in python, the line wrapping in parentheses can not be marked-np.cos (theta),-(np.sin (theta) * x0-np.cos (theta) * y0)]
Because this method may be used frequently, it can be encapsulated as a function, which is defined by the keyword def in python. There is almost no limit to the number of parameters, the number of values returned, and the data type. So it can be written as
Import numpy as npdef getABC (x0Query y0theta): # function definition format is def function name (input parameter): a = np.sin (theta) # the following function needs to be indented b =-np.cos (theta) c =-(np.sin (theta) * x0-np.cos (theta) * y0) return a dint b # in python, the abc parameter is returned through return
It can also be written as
Import numpy as npdef getABC: return [np.sin (theta), # can wrap directly in parentheses-np.cos (theta),-(np.sin (theta) * x0-np.cos (theta) * y0)]
Called from the command line
> import test > import numpy as np > > test.getABC [- 0.7071067811865476,-0.70710678118654760.0] segments and arcs
In optical systems, the most common optical devices are plane mirrors and lenses, whose projection on the meridian plane of the incident light can be disassembled into line segments and arcs.
For line segments, they can be represented very intuitively by two points. In python, in addition to lists, sequences can be represented by tuples (tuple). The difference is that the elements of tuples cannot be modified. Here, a point is represented by a tuple full of two elements, and a list of two points is formed to represent the line segment:
> a = (0jin0) # in python, tuples are expressed in parentheses > b = (1jue 1) > seg = elements in the list of [Areco b] # can have different types > seg [(0J 0), (1J 1)] >
The circle can be expressed by the center and radius of the circle, which is concise and convenient for calculation. Arcs can be represented by circles and angles. However, as a part of the optical element, in order to facilitate the later processing, its expression had better be similar to the line segment, that is, through three points, namely A, B, C to confirm a circular arc.
But in the process of concrete calculation, it is often necessary to convert the arc into the expression of the circle for operation, so define the function arc2cir to achieve this function. Because its mathematical process is too simple, only some commands are explained, in which np.lialg is a linear algebraic package of numpy, np.linalg.norm is a norm, and np.linalg.norm (a Mel b) is equivalent to finding the distance between points an and b.
Def arc2cir (arc): arc = np.array (arc) # list cannot perform array operations Need to be converted to the form np.array dCD = np.linalg.norm (1 arc [0] + arc [1])-arc [2]) # CD length dBC2 = np.sum (np.square (arc [1]-arc [2])) # BC**2 radius = 0.5*dBC2/dCD # Circle Radius theta = (arc [2]-1CH2) * (arc [0] + arc [1]) / dCD # trigonometric function values zero = arc [2]-radius*theta return list (zero) + [radius] # the list is added to the merge of its internal elements
It is important to note that in python, neither list nor tuple supports overall mathematical calculations, which is easy to understand because what is stored in both is not necessarily numbers, but something else. If the format is different, it is naturally impossible to calculate. Therefore, before considering the calculation, you need to convert both to the array format of numpy.array.
The conversion between lists, tuples, and arrays is very simple and intuitive.
Import numpy as np > lis = [1meme2d3] > tup = tuple (lis) > arr = np.array (tup) > arrarray > lis [1mie 2pime 3] > > tup (1meme 2meme 3) so far, I believe you have a deeper understanding of the "Python Optical Simulation course how to achieve Ray tracing", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.