<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Lens Library to Facilitate Composing of Virtual Scene for Optic Experiments</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Victor Debelov</string-name>
          <email>debelov@oapmg.sscc.ru</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Nikita Dolgov</string-name>
          <email>nikitabrn1999@gmail.com</email>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Institute of Computational Mathematics and Mathematical Geophysics of Siberian Branch of Russian Academy</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Novosibirsk State University</institution>
          ,
          <addr-line>1, Pirogova str., Novosibirsk, 630090</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>of Sciences</institution>
          ,
          <addr-line>6, Ac. Lavrentieva ave., Novosibirsk, 630090</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>While the mathematical modeling of optical phenomena, a computer calculation is often performed, confirming the conclusions made. To do this, a virtual computer model of the optical installation is created in the form of a 3D scene. Also, virtual scenes are often used in training when creating presentations. This paper describes the SphL library, which provides a convenient assignment of spherical lenses and the calculation of the interaction of linear polarized light rays with them. It is focused on applications that use ray tracing. It is known that light of any polarization can be represented on the basis of the mentioned one. The reflected and all rays passing through the lens that arise due to internal reflections are calculated from the ray incident on the scene object. The number of internal reflections is set by the parameter. All output rays are calculated based on the application of Fresnel's equations and are characterized by intensity values and polarization parameters. In this version of SphL, the main objects at the end-user level are spherical lenses, since they are most often used in optic installations. They are constructed on the basis of the application of the set-theoretic intersection of geometric primitives: a half-space, a sphere, a cone, a cylinder and their complements to the scene space. An advanced user can build their own objects by analogy, for example, cylindrical lenses.</p>
      </abstract>
      <kwd-group>
        <kwd>light</kwd>
        <kwd>ray tracing</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>In the mathematical modeling of natural phenomena, computer experiments are often performed
with the similar computer model to confirm its reliability. The construction and analysis of a
mathematical model often leads to the creation of a 3D scene, which is a virtual analogue of a real
installation. Another area of creating virtual 3D scenes is education, especially when it is necessary to
demonstrate a phenomenon physically correctly. In optics, when obtaining images, lenses are used in
one way or another, for example, a lens, an eyepiece, an eye.</p>
      <p>The idea to develop a library of lenses arose after conducting experiments on interference modeling,
we often had to change the shape and parameters, and even the type of lens. We came to the conclusion
that such objects should be isolated so that the corresponding software modules are responsible for their
interaction with linear polarized light rays. Why linear polarized light?</p>
      <p>Historical background. In connection with the development of algorithms for photorealistic
visualization of scenes involving optically anisotropic transparent crystals, one considered only
polarized, moreover, linear polarized rays, since a light ray with any polarization incident on these</p>
      <p>
        2021 Copyright for this paper by its authors.
crystals generates up to four linear polarized rays (up to two reflected and two refracted) at the
boundary [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].
2. This does not limit the freedom of choice. Light of any polarization (unpolarized, partially
polarized, polarized) can be represented as a combination of a certain number of linear polarized
rays with a lower intensity, see [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].
3. Note one very important property for ray tracing: when a ray falls on an optically isotropic
object, regardless of the state of polarization, the trajectory of the ray is the same. The polarization
of the incident ray affects the state of polarization and the intensity of the reflected and refracted
rays obtained when interacting with the surface. This is especially evident when performing
calculations using Fresnel’s formulas [
        <xref ref-type="bibr" rid="ref1 ref2">1, 2</xref>
        ]. This property does not hold for anisotropic objects.
      </p>
      <p>
        In [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], the prerequisites for creating a library of lenses, new features compared to existing software
were identified. Thus, this article is a natural continuation of the work [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] and contains technical
information about the contents of the library and how to use it. First of all, we paid attention to spherical
lenses, as the most common. Indeed, in the vast majority of cases, in order to visualize the required
optical effects, it is necessary to introduce a lens (camera lens, etc.) into a virtual scene, as well as into
a real experimental stage.
      </p>
      <p>The second section describes the contents of the library and the suggested principles of lens design.
The third section is devoted to the conclusions.</p>
    </sec>
    <sec id="sec-2">
      <title>2. Library of spherical lenses</title>
      <p>
        The lens library should make it easier for the user to design spherical lenses, add them to a 3D scene,
and process the interaction of rays of linear polarized light with them. This section describes the
requirements that the software package SphL and its implementation must meet. The book [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] describes
in detail all types of thin spherical lenses. Since this is the most common type of lenses, we decided to
provide users of the software package with a simple design of all six types of spherical lenses: biconvex,
plano-convex, concave-convex, biconcave, plano-concave, convex-concave (see Figure 1). Each lens
can have a cylindrical or conical rim. The rim is the surface of the lens attachment; it can be made of
both transparent (working surface) and opaque material. We decided not to limit ourselves to only thin
lenses, but to allow the use of similar geometric shapes of any size. Moreover, since we perform
physically correct ray tracing, there are no problems with accuracy as in the cases of using the
approximations of thin [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] or thick lenses [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ].
      </p>
      <p>
        Our experience has shown that the set of these lenses significantly facilitates the creation of scenes
and the execution of virtual optical experiments. Note that even complex optical devices, for example,
polarizing microscopes (see Figure 2), are constructed mainly with the help of these lenses: each
transparent object in the design of the microscope is a simple spherical lens or is composed of several
spherical lenses [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>
        Since it is necessary to take into account the polarization of light to calculate interference, the
software package must be able to process the interaction of rays of linear polarized monochromatic
light with lenses. When tracing a ray through a lens, according to [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ], three possible events are taken
into account:
 Event 1. Total internal reflection. In this case, the user can receive two calculated linear
polarized rays, and further ray tracing stops.
 Event 2. In this case, the user receives the ray that hits the rim and the coordinates of the hit
point, as well as further ray tracing stops.
 Event 3. The next output ray of linear polarized light has been successfully calculated, or the
specified tracing depth inside the lens has been reached. Tracing depth is determined by a separate
parameter. For example, if the user has set the tracing depth  , then  + 1 output rays should be
calculated (see Figure 3).
      </p>
      <p>To construct lenses, a set-theoretic intersection operation is used over several geometric primitives.
The set of primitives includes a half-space, a part of the space inside or outside a sphere, a cylinder, a
cone. We consider only objects with a simply connected boundary.
2.1.</p>
    </sec>
    <sec id="sec-3">
      <title>Half-space</title>
      <p>The half-space of the scene can be set by specifying a plane in three-dimensional space using a point
and a normal. The normal indicates the selected half of the scene space (Figure 4). Here and further,
the wire cube denotes a 3-dimensional space.</p>
    </sec>
    <sec id="sec-4">
      <title>Space inside or outside sphere</title>
      <p>The sphere is defined by the center C(  ,   ,   ), the radius  and the Boolean sign INSIDE: true
means that the inner region is taken, if false, then the complement to the space is taken, see Figure 5.
2.3.</p>
    </sec>
    <sec id="sec-5">
      <title>Space inside or outside cylinder</title>
      <p>The infinite cylinder can be set using the coordinates of a point on the axis of rotation, the direction
of the axis of rotation, and the radius of the circle. The point is needed to link the vector of the axis of
rotation to a certain place in space. Similarly, there are two possible options: a part of the space inside
or outside the cylinder is taken (see Figure 6).</p>
    </sec>
    <sec id="sec-6">
      <title>Space inside or outside cone</title>
      <p>The infinite cone is a second-order surface generated by the motion of a straight line (generatrix)
passing through a fixed point. In this case, the cone can be set by the coordinate of the vertex of the
cone, the vector of the axis of the cone and the value of the angle at the vertex of the cone, i.e., the
doubled angle between the axis and the generatrix. As in the case of a sphere, in order to define the
primitive completely, it is specified which part of the space is taken: inside or outside the cone (see
Figure 7).</p>
    </sec>
    <sec id="sec-7">
      <title>An example of constructing a lens from primitives</title>
      <p>Let us consider the application of the set-theoretic intersection operation on a set of primitives by
constructing a certain lens. Let us take for example a biconvex lens. We will set in the scene space the
coordinates of the lens center point  , the unit vector of the main optical axis of the lens  and the
width of the lens  ℎ, see Figure 8. A biconvex lens has two spherical surfaces, let us denote their
radii   and   . Obviously, the following conditions must be met:
 ℎ &gt; 0 &amp;   ≥  ℎ &amp;   ≥  ℎ.</p>
      <p>Similarly, the center of a sphere with radius  
 2 =  + 
· (</p>
      <p>2
is at the point
−  ℎ).
(see Figure 8). The intersection of the spheres gives a biconvex lens without a rim.
cylindrical rim, at the bottom there is a lens with a conical rim</p>
      <p>Let us add a cylindrical rim to the resulting lens, which is formed when adding a part of the space
inside the cylinder to the set of primitives, i.e. the intersection of two spheres and the interior of the
infinite cylinder. The cylinder has a rotation axis 
= 
the maximum height of the lens, then the radius of the cylinder  
lens similar to the upper one shown in Figure 8 will be obtained.
≤ 
ℎ</p>
      <p>2
and a starting point  =  . Let ℎ</p>
      <p>be
. If this condition is met, a
The condition |
Figure 8).</p>
      <p>Important note: the axis of the rim coincides with the axis of the lens.</p>
      <p>To set a conical rim, you must specify the vertex  
and the angle  of the cone, which will cut
off the excess part of the lens. The angle  should be such that the cone has an intersection with the
lens. The vertex of the cone is given by the distance 
main optical axis, and then the vertex of the cone can be found by the formula
from the center of the lens along the</p>
      <p>=  + 
| ≥

2
ℎ must be met, and 
·</p>
      <p>.
can be either positive or negative (see</p>
      <p>The library provides specialized constructors and allows the user to avoid difficulties and errors
when designing lenses using set-theoretic intersection. Here is an example of using the lens constructor
that eliminates annoying geometric calculations shown above:
using namespace Lens;
double cone_angle = 30;
double cone_vertex_distance = 100;
/* Create biconvex lens */
BiconvexLensBuilder builder;
LENS_DATA lens = builder.set_axis(CLENSVector{1, 0, 0})
.set_center(CLENSVector{0, 0, 0})
.set_front_radius(200)
.set_back_radius(400)
.set_width(50)
.set_inner_material(GLASS_INDEX)
.set_outer_material(AIR_INDEX)
// Specify conical opaque rim
.set_rim(coneRim(cone_vertex_distance, cone_angle, BORDER))
.build();</p>
      <p>Here we show how one can create a biconvex lens with a conical rim using the appropriate
constructor. The cylindrical rim is set similarly using the following line</p>
      <p>.set_rim(cylinderRim(cylinder_radius, BORDER)).</p>
      <p>In addition to geometric parameters, refraction indices or materials of the lens interior and the
external environment are also set.
2.6.</p>
    </sec>
    <sec id="sec-8">
      <title>Implementation</title>
      <p>The SphL software package is designed as a set of libraries in the C++ programming language. For
all six types of spherical lenses, constructor classes are implemented. Examples of possible lenses are
shown in Figure 9. When designing the lens, the correctness of the parameters set by the user is checked.
If it is impossible to construct the correct lens based on the parameters, the user receives an error in the
form of an exception.</p>
      <p>
        Let us represent a linear polarized zero-thickness tracing ray with attributes based on the work [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]
in the form
      </p>
      <p>= { 0,  ,  ,  ,  ,  ,   ,  ,  }, (1)
where: { 0,  } is the mathematical ray,  0 is the origin of the ray,  is the direction, { ,  ,  } is the
associated right-hand coordinate system. In other words, the ray is represented as</p>
      <p>( ) =  0 +  ×  .</p>
      <p>
        The oscillations of the electric vector of electromagnetic wave occur along the  axis,  is the
intensity,  is the wavelength of the light,  id is the identifier of the point light source that generated
the ray. Two rays are coherent if their source IDs are non-zero and match.  is the optical path of a
geometric path traveled from the source, which is used to calculate the current phase of the
electromagnetic wave [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ].  is the phase jump accumulated during reflections from a denser medium,
see [
        <xref ref-type="bibr" rid="ref2 ref7">2, 7</xref>
        ] for details.
      </p>
      <p>The generated rays (reflected and refracted) inherit some of the attributes, or they are recalculated
during the contact of the incident ray with the scene surface. Note that the Fresnel’s equations apply.</p>
      <p>A corresponding data structure was written for each type of primitive. The primitive is defined,
firstly, by the parameters that determine its geometry and location in space, and secondly, by the
refraction indices of two media: belonging (interior) and not belonging to the primitive (medium). Basic
operations on primitives are the following:
 Use a given point to determine whether it belongs to a primitive,
 Using a given geometric ray, if possible, calculate: a) the value of the parameter  for the point
of its intersection with the primitive boundary, b) the coordinates of the intersection point, c) the
normal vector at this point. There may be several such points.</p>
      <p>Let us consider a simple case presented in Figure 10. The lens is represented as a list of primitives.
When tracing, it is necessary to determine which primitive the ray falls on, the coordinates of the hit
point, the normal at this point, and the refraction indices of the media. Knowing these values, it is
possible to calculate the reflected and refracted rays using Fresnel's formulas. To support the
intersection operation, it is necessary to check for each point that it belongs to all primitives.</p>
      <p>Based on the example, we will consider how the set-theoretic intersection operation is implemented.
It is based on the well-known even-odd principle when calculating intersections of a ray with the
boundaries of primitives. In Figure 10, the ray  falls on the surface of a lens constructed from two
primitives: parts of space inside spheres with centers  1,  2. The ray  crosses the boundary of the
Primitive  1 at points A and C, the boundary of the Primitive  2 at points B and D. Thus, we
get a set of values of  :</p>
      <p>( ,  1) –  ( ,  1) –  ( ,  2) –  ( ,  2),
sort it in ascending order  :</p>
      <p>( ,  1) –  ( ,  2) –  ( ,  1) –  ( ,  2),
analyze the sequence and find the segment  belonging to both primitives, i.e. their intersection.</p>
      <p>We have considered the simplest case of the mutual arrangement of the ray and the boundaries of
primitives. A well-known problem is when the ray hits the intersection point of the primitives
themselves. In order to have an accurate idea of how to perform sorting, a different implementation was
made for each type of lens.</p>
      <p>
        When creating a lens, the user sets handlers for the three above-mentioned events he is interested in.
A user can use the standard (or default) handlers already included in the library, or implement them
himself. Each of the three possible events has its own handler class interface. Let us look at the work
of standard handlers:
 The handler for the next calculated output ray allows the user to get a list of calculated output
rays after the tracing is completed.
 The handler for the case of total internal reflection allows the user to check whether a total
internal reflection has occurred after the tracing is completed and, if it has occurred, to obtain two
calculated linear polarized rays (see [
        <xref ref-type="bibr" rid="ref2 ref7">2, 7</xref>
        ]).
 The handler for the case of a ray hitting the rim allows the user, after the tracing is completed,
to check whether a certain ray has hit the rim and, if it has happened, to get this ray and the
coordinates of the hit point.
      </p>
      <p>The handlers are installed after the lens is set. Different lenses may have different handlers. After
setting the handlers, the user can trace the ray through the lens. To do this, the user needs to set the
input ray, the tracing depth, the energy level of the ray  , at which further tracing stops, the lens through
which the tracing will be performed, and finally calls the tracing function.</p>
      <p>After the trace is executed, the user can get the result and apply the appropriate handler. The value
returned by the tracing function is the code of the last event that occurred:
 FRESNEL_SUCCESS means that the required number of rays has been successfully
calculated,
 FRESNEL_RIM means a ray was found that hit the rim,
 FRESNEL_TIR means the case of total internal reflection,
 FRESNEL_DATA_ERROR means that error in user data is detected,
 FRESNEL_NO_ENERGY means that the incident ray has an energy less than  ,
 FRESNEL_NO_INTERSECTION means that the ray does not intersect with the lens.
2.7.</p>
    </sec>
    <sec id="sec-9">
      <title>Additional library objects</title>
      <p>Spherical lenses represent only the starting set of the library. Already in the course of our
experiments, we were faced with the need for such basic geometric shapes as a cube for the instrument
glass, a cylinder for the instrument glass, a wedge for experiments with interference. We illustrate these
additional objects in Figure 11.</p>
    </sec>
    <sec id="sec-10">
      <title>Function Fresnel: base level of library</title>
      <p>Tracing is carried out by rays of linear polarized light. To calculate their interaction with optically
isotropic transparent objects, the Fresnel function is developed, which, based on the application of the
Fresnel’s equations and the parameters of the ray incident on the boundary between two optically
isotropic transparent media, calculates the characteristics of the generated reflected and refracted rays:
direction, polarization, and others according to the representation (1). This function also determines the
events of total internal reflection and others mentioned above.</p>
    </sec>
    <sec id="sec-11">
      <title>3. Conclusions</title>
      <p>
        In this paper, we described the current state of the development of SphL Library. Testing and
debugging were carried out on simple scenes, for example, calculating interference patterns related to
fringes of equal thickness [
        <xref ref-type="bibr" rid="ref1 ref3">1, 3</xref>
        ], including the calculation of Newton's rings in transmitted and reflected
light.
      </p>
      <p>We are planning to expand it step by step with other types of geometric shapes: cylindrical and
aspherical lenses, other optically isotropic transparent objects necessary for particular experiments, etc.</p>
      <p>Note that the library allows one to find out all the data about the progress of ray tracing in the lens
(see Figure 3), and, therefore, the user can link the movement of the rays to the virtual scene, and
prepare presentations and reports on the flow of the experiment.</p>
    </sec>
    <sec id="sec-12">
      <title>4. Acknowledgements</title>
    </sec>
    <sec id="sec-13">
      <title>5. References</title>
      <p>This work was carried out under state contract with ICMMG SB RAS (0251-2021-0001).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>M.</given-names>
            <surname>Born</surname>
          </string-name>
          ,
          <string-name>
            <surname>E. Wolf.</surname>
          </string-name>
          <article-title>Principles of optics: Electromagnetic theory of propagation, interference and diffraction of light</article-title>
          , Cambridge University Press,
          <year>1980</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>V. A.</given-names>
            <surname>Debelov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K. G.</given-names>
            <surname>Kushner</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L. F.</given-names>
            <surname>Vasilyeva</surname>
          </string-name>
          .
          <article-title>Lens for a computer model of a polarizing Microscope</article-title>
          , Mathematica Montisnigri,
          <volume>41</volume>
          (
          <year>2018</year>
          )
          <fpage>151</fpage>
          -
          <lpage>165</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>G. S.</given-names>
            <surname>Landsberg</surname>
          </string-name>
          , Optics, 6th. ed.,
          <source>Fizmatlit</source>
          , Moscow,
          <year>2003</year>
          . [in Russian]
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>B. A.</given-names>
            <surname>Barsky</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D. R.</given-names>
            <surname>Horn</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S. A.</given-names>
            <surname>Klein</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. A.</given-names>
            <surname>Pang</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Yu</surname>
          </string-name>
          .
          <article-title>Camera Models and Optical Systems Used in Computer Graphics: Part I, Object-Based Techniques</article-title>
          , in: V.
          <string-name>
            <surname>Kumar</surname>
            ,
            <given-names>M. L.</given-names>
          </string-name>
          <string-name>
            <surname>Gavrilova</surname>
            ,
            <given-names>C. J. K.</given-names>
          </string-name>
          <string-name>
            <surname>Tan</surname>
          </string-name>
          ,
          <string-name>
            <surname>P. L'Ecuyer</surname>
          </string-name>
          (Eds.),
          <source>Proceedings of International Conference on Computational Science and Its Applications</source>
          ,
          <string-name>
            <surname>ICCSA</surname>
          </string-name>
          <year>2003</year>
          , Montreal, Canada, May
          <volume>18</volume>
          -21,
          <year>2003</year>
          , volume
          <volume>2669</volume>
          of Lecture Notes in Computer Science, Springer-Verlag, Berlin. Heidelberg,
          <year>2003</year>
          , pp.
          <fpage>246</fpage>
          -
          <lpage>255</lpage>
          . doi:
          <volume>10</volume>
          .1007/3-540-44842-X_
          <fpage>27</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <surname>G. I. Fedotov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>R. S.</given-names>
            <surname>Ilin</surname>
          </string-name>
          , et al.,
          <article-title>Laboratory optical devices. Textbook for optical specialties of Universities, 2nd</article-title>
          . ed.,
          <source>Mashinistroenie</source>
          , Moscow,
          <year>1979</year>
          . [In Russian]
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <article-title>Laboratory polarizing microscope of transmitted light</article-title>
          ,
          <year>2021</year>
          . URL: http://www.lomo.ru/production/grazhdanskogo-naznacheniya/mikroskopy/mikroskopytekhnicheskie/polam-l-213m/. [In Russian]
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>V. A.</given-names>
            <surname>Debelov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L. F.</given-names>
            <surname>Vasilieva</surname>
          </string-name>
          .
          <article-title>Visualization of interference pictures of 3D scenes including optically isotropic transparent objects</article-title>
          ,
          <source>Scientific visualization, 12.3</source>
          (
          <year>2020</year>
          )
          <fpage>119</fpage>
          -
          <lpage>136</lpage>
          . doi:
          <volume>10</volume>
          .26583/sv.12.3.11.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>