Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the spatial relationship of ray tracing realized by python optical simulation?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the spatial relationship of python optical simulation to achieve light tracing, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Spatial relation

Change begins with meeting, so the point of intersection is the core of everything.

Intersection decision

First of all, examine whether a beam of light can hit a plane mirror. The light is abstracted into a list [aMagib], and the plane mirror is abstracted into a line segment made up of two points [(x1), (x2)]. The problem of the intersection of two straight lines belongs to the category of elementary mathematics, so it is necessary to convert the line segment into the form of a straight line before finding the intersection point. However, the intersection of two lines may fall on the outside of the line segment, so it does not have the significance of judgment.

If there are a large number of optical elements in our optical system, it will be faster if there is a way to quickly determine whether the light intersects with the optical element. So, if a line passes through a segment, it means that the two endpoints of the segment must be on both sides of the line.

Import numpy as npdef isCross (abc,dots): abc = np.array (abc) # converts the format of abc into the array flag1 = abc.dot (list (dots [0]) + [1]) # the dot product of the array flag2 = abc.dot (list (dots [1])) + [1]) return flag1*flag2

We are very familiar with the operator "+", but for now, only numeric values and arrays support addition. Therefore, the expression of list (dots [0]) + [1] is really confusing.

This meaning is actually in line with human intuition. The number of elements in a list is variable, and the addition of two lists can be understood as the convergence of two lists. Of course, tuples do not support this operation.

For example

> [1Jing 2jue 3] + [4] [1Jing 2jue 3,4] > > (1Jing 2jue 3) + (4) Traceback (most recent call last): File ", line 1, in TypeError: can only concatenate tuple (not" int ") to tuple

Joining two lists through the addition operator is actually tantamount to creating a new variable that needs to open up new memory space. Fortunately, it is not easy for beginners to make mistakes.

In numpy, the +, -, *, and / operators represent the operations of the corresponding position elements. If you want to use other operations such as dot multiplication, you need to call other functions in numpy.

> > np.array ([1pje 2jue 3]) * np.array ([4je 5je 6]) array ([4,10,18]) > np.array ([1je 2jue 3]) + np.array ([4je 5jue 6]) array ([5,7,9]) > > np.array ([1je 2jue 3]). Dot ([4jue 5jue 6]) # .dot point product 32

So, flag1 = [a, b, c] ⋅ [x 0, y 0,1] = a x 0 + b y 0 + c of course, we can also write flag1 = abc [0] * dots [0] [0] + abc [1] * dots [0] [1] + c, but it doesn't look very elegant.

Then, you get the values of flag1 and flag2, and if they have different signs, you can conclude that the line is in the middle of the two points. That is, as long as the flag1*flag2 0: # judgment statement poses+ = 1 elif flag 0, then poses is assigned to poses+1; if flag= 0 zFlag = np.abs (xFlag) + np.abs (yFlag) > 0 flag= yFlag*xFlag*zFlag return [(xCross [I], yCross [I]) for i in range (len (yFlag)) if values [I]]

Although this program is short, it is informative and uses an lambda expression.

PlusMinus = lambda a _ r _ b: np.array ([a _ r _ b _ m _ r _ b]) defines a function called plusMius

This function is written in the regular form as:

Def plusMinus (a _ r _ r b) return np.array ([a _ r _ b _ m _ r _ r _ b])

It is important to note that only one expression can be followed by a lambda expression, that is, a function that can only be defined on one line.

In this code, we also see a strange operator set, which is also a data type of python, collection. Like the set we know mathematically, the same value is not allowed in the set. So, if baked zero, then set (a) = set (a), that is, plays the role of weight removal. The collection is then converted into computable array data through np.array.

In addition, the comparison operator is introduced here. The operations we have mentioned so far are all numerical, but in fact there are other types of operations that we are exposed to. For example, when we make a judgment, if delt > 1 percent 2

False

> [3prime3] = [3pyr3]

True

> 3 > 3

False

> 3 > = 3

True

Then we look at the logic of this algorithm, because we are solving the intersection of a straight line and a circle, while the real light is a ray, then we must consider the position relationship between the intersection and the light source.

So the code

YFlag = (yCross-point [1]) * abc [0] > = 0xFlag = (point [0]-xCross) * abc [1] > = 0

The two criteria xFlag and yFlag are defined respectively. But when the two are 0 at the same time, it means that the intersection point is the light source, so it can not be counted as the intersection of the light and the circle. So there is another criterion, zFlag.

Only when these three criteria are satisfied, the value we get is valid, so the relationship between the general criterion and the three sub-criteria is' and', so it is written as flag = yFlag*xFlag*zFlag.

In addition, we do not know the number of intersection points, when the discriminant is 0, the lambda expression will have only one value, and there will be only one element in xCross and yCross respectively; if the discriminant is greater than 0, there will be two elements respectively. Here also involves a good feature of array, when the dimension does not want to wait for the calculation of two variables, it will automatically expand the low-dimensional data reasonably, such as

> np.array ([1jin2jue 3]) + 4array ([5,6,7]) > np.array ([[1], [2], [3]) + 4array ([[5], [6], [7]])

Finally, there is a deja vu expression.

Return [(xCross [I], yCross [I]) for i in range (len (yFlag)) if [I]]

This expression is easy to read: iterate through flag, and if the value of flag is true, put the corresponding intersection coordinates into the list and return valid intersection coordinates.

This is for what we used before [. For... In...] It is a kind of expansion, which is simple and powerful, and is highly recommended.

Point-arc relation

Generally speaking, a whole sphere rarely appears in an optical system, and in most cases it is a variety of lenses composed of partial spheres. Therefore, as a two-dimensional optical system, it may be more necessary to deal with the relationship between light and arcs, especially the relationship between inferior arcs.

There are many ways to determine whether a point is on an inferior arc, for example, if the symmetry point of any point on the arc ACB about AB falls into a circle, it is an inferior arc; if it falls outside the circle, it is a superior arc; if it is on a circle, it means that AB is a diameter, and the arc ACB is a semicircle.

Here, we choose another way. As shown in the figure, E means that for any point on the inferior arc, the connection between it and the midpoint D of the AB must be less than AB, otherwise it is on the superior arc.

So, the code is:

Def isOnArc (point,arc): arc = np.array (arc) dAB = 0.5*np.linalg.norm (arc [0]-arc [1]) # AB/2 length dCrossA = np.linalg.norm (0.5 * (arc [0] + arc [1])-point) # ED length return dAB > dCrossA

Therefore, when the inferior arc judgment is satisfied, the intersection point of the line circle is the intersection point of the line arc.

Def getCrossArc (abc= [1je 1jue 1], arc= [[0je 1], [0je Lay 1], [1je 0]], point= []): if point= = []: return [] crossDict = {np.sqrt ((p [0]-point [0])) * * 2 + (p [1]-point [1]) * * 2): p for p in getCrossCircle (abc,arc2cir (arc), point) if (isOnArc (p) Arc) and (pairing point)} if crossDict = = {}: return [] return crossDict [min (crossDict.keys ())]

Smart students actually noticed a long time ago that when defining a function, its incoming parameters were assigned a value. This is not a big deal in python, where the value entered is the default value.

In addition, when the function is called, of course, we can pass parameters in the order of the parameters, but we can also use the variable name to assign the parameters, so the order of the parameters is meaningless.

For example,

Test1 = getCrossArc ([1dje 1rect 1], [[0jue 1], [0rect 1], [0je 0]], (0je 0)] test2 = getCrossArc (abc= [1je 1], point= (0m 0), arc= [[0jue 1], [0je Lok 1], [1je 0]])

Both of the above two writing methods can get correct results.

After reading the above, do you have any further understanding of the spatial relationship of ray tracing realized by python optical simulation? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report