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

How to realize Ray tracing refraction and reflection 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--

This article is to share with you about python optical simulation how to achieve ray tracing refraction and reflection, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

Refraction and reflection

There are only two things when light interacts with optical elements, reflection and transmission. At present, the optical elements we use have nothing more than two kinds of surfaces, namely, plane and sphere, which are simplified to the relationship between rays and line segments, rays and inferior arcs after two-dimensional transformation.

Plane reflection

No matter from which point of view, the reflection and refraction of the plane is simpler than that of the sphere, and the reflection problem is simpler than the refraction problem, so we first deal with the reflection problem of the plane.

The law of reflection is that the incident angle is equal to the reflection angle. With this in mind, the most regular way of thinking must be to find the angle between the incident light and the plane, and then use the angle and the slope of the plane (which is a straight line in two-dimensional space) in space. From this slope and the incident angle, the slope of the outgoing light can be obtained, and then the equation of the outgoing light can be obtained.

The problem with this method is that trigonometric functions and inverse trigonometric functions need to be used repeatedly, and trigonometric functions and inverse trigonometric functions are not strictly opposite to each other, so there may be some trouble in the process of passing parameters.

Relatively speaking, the less error-prone method is to find the symmetry point of the incident point with respect to the normal, then the connection between the symmetry point and the intersection point is the equation of outgoing light.

Plane refraction

The idea of refraction is the same as that of reflection. The original idea is still to obtain the incident angle, then calculate the incident angle according to the refraction law, and then work out the expression of the incident light according to the exit angle. The difficulty of this idea still lies in the transformation of trigonometric function and inverse function.

So far, we find that refraction and reflection are similar in expression. if the incident point is perpendicular to the normal and the vertical foot is C, and the intersection of the vertical line and the outgoing ray is stipulated as the exit point B, then the distance from the exit point to the perpendicular point BC and the distance from the incident point to the perpendicular point AC satisfy the proportional relationship. When the refractive index of the incident ray and the reflected ray is equal, the ratio is 1, otherwise the ratio is λ\ lambda λ.

We can also find that this λ\ lambda lambda does not necessarily have a solution, because there is a root expression in the denominator, which naturally has no solution when the internal value is less than 0. This is consistent with our physical intuition, that is, not all incident rays are refracted, and total reflection occurs when the refracted light disappears.

Therefore, the urgent task is to find the vertical foot according to the incident point, which is easy to get.

Then for the refraction problem that we are familiar with, the incident point can be symmetrical with respect to the reflection plane once, and then the exit point can be obtained by finding the symmetry of a proportional extension line.

Python implementation

So far, we have fully established a relationship between reflection and refraction, the code is as follows:

# get the symmetry point of the point with respect to the straight line, k is the proportional coefficient def getSymDot (point,line,k=1): return tuple ((np.array (getPedal (point,line)) * (1cm k)-point) / k) # get the vertical def getPedal (point,line) of the straight line: a dint bline cendline X0 Y0 = point Y1 = (a**2*y0-a*b*x0-b*c) / (a**2+b**2) x1 = (b**2*x0-a*b*y0-a*c) / (a**2+b**2) return (x1Powery1)

The function getSymDot is to solve the symmetry point by inputting points and lines. Its idea is to transform the problem of line symmetry of a point into the problem of vertical foot symmetry of a point. So I refer to the getPedal function, which returns the perpendicular of the line through the point by entering a point and a line.

All the code is a simple reproduction of the above mathematical formula.

Def cataDioLine (abc= [1qiang Lok 1je 1], line= [2je Yu 1je 1], sPoint= [], cross= [], n1Liv 1me n2j 1.5): normal = [- line [1], line [0], line [1] * cross [0]-line [0] * cross [1] # normal flecDot = getSymDot (sPoint,normal) flec=getABC ([cross] FlecDot]) dPara = np.sqrt (line [0] * * 2+line [1] * * 2) dNormal = np.abs (np.array (normal) .dot (list (sPoint) + [1])) / dPara# to normal distance dPane = np.abs (np.array (line) .dot (list (sPoint) + [1])) / dPara# distance to reflector if dNormal = 0: return flec Abc delt = (n2/n1) * * 2* (1 + (dPane/dNormal) * * 2)-determine that total reflection if delt > 0: K = dPane/dNormal/np.sqrt (delt) fracDot = getSymDot (sPoint,normal,k) fracDot = getSymDot (fracDot,line) frac = getABC ([cross,fracDot]) return flec,frac return flec, [0memo]

The function cataDioLine is the realization function of reflection and refraction. Note that the getABC introduced here is not a function defined earlier to find an expression by point and angle, but a function that is transferred from two points to [a _ reb _ c].

So can we write a function with the same name to achieve different functions? Unfortunately, Python does not support overloading of functions, so functions with the same name can only be encapsulated together:

Def getABC (* par): if len (par) = = 1: # the parameter passed in at this time is point-to-point dots= [(x0Powery0), (x1jingy1)] dots= par [0] abc = [dots [1] [1]-dots [0] [1], dots [0] [0]-dots [1] [0] -np.linalg.det (dots)] return np.array (abc) / (np.sqrt (abc [0] * * 2+abc [1] * * 2)) elif len (par) = = 2: # at this time, the passed parameters are point and angle (x0CoCoy y0), theta theta,sPoint = par a np.sin b = [np.sin (theta) -np.cos (theta)] c =-(a*sPoint [0] + b*sPoint [1]) return

When we see the input parameters (* par), many of us may have some not-so-wonderful associations, but don't get excited, it's just a way of passing parameters to python. (* args) indicates that the parameters will be passed into a list args; (* * kargs) means that the parameters will be passed into a dictionary kargs.

Arc surface problem

The reflection of light on the curved surface is a typical paper tiger problem that seems complex but simple, so simple that we can easily turn it into a flat problem as long as we find the normal.

Therefore, the problem is simply transformed into the problem of solving the tangent of a circle-this tangent is the reflection plane. Because the mathematical process is too simple to write formulas, readers can try to look at the code to deduce formulas.

# get the tangent def getTangent of arcs that pass through the intersection (corss= [0L1], circle= [0LECHERY 1]): a = corss [0]-circle [0] b = corss [1]-circle [1] c =-a*corss [0]-b*corss [1] return [aMagne BJ c]

At this point, we can get a complete solution to the refraction and reflection problem:

# def cataDio of refraction of light on a straight line or arc surface (abc= [1] if len (dots) = 3: line = getTangent (cross) = 3: line = getTangent (cross) Arc2cir (dots)) # tangent on the circle elif len (dots) = = 2: line = getABC (dots) flec,frac = cataDioLine (abc,line,sPoint,cross,n1,n2) return cross,flec,frac

Of course, the getCross here also needs to be rewritten to fit not only straight lines, but also arcs:

Def getCross (abc= [1jinlinger 1], dots= [[0mae1], [0d1], [0.5], point= []: if len (dots) = = 3: return getCrossArc (abc,dots,point) if len (dots) = 2: return getCrossDots (abc,dots,point)

At this time, we find that it is more comfortable to use two points to represent line segments and three points to represent arcs. At least the unity of the two forms of expression seems to bring us some inner pleasure.

The above is how to achieve ray tracing refraction and reflection in python optical simulation. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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