From 5ccfd2b849ada2f872b6c9abe8b340701cd3f602 Mon Sep 17 00:00:00 2001 From: Sven Erdem Date: Fri, 11 Oct 2024 16:49:14 +0200 Subject: [PATCH] wip --- Intern/rayx-core/src/Shader/Efficiency.h | 10 +-- docs/src/Model/Efficiency.md | 93 +++++++++++++++++++----- 2 files changed, 80 insertions(+), 23 deletions(-) diff --git a/Intern/rayx-core/src/Shader/Efficiency.h b/Intern/rayx-core/src/Shader/Efficiency.h index bd52f2472..249c4a03d 100644 --- a/Intern/rayx-core/src/Shader/Efficiency.h +++ b/Intern/rayx-core/src/Shader/Efficiency.h @@ -210,7 +210,7 @@ struct RotationBase { // A convention for the up and right vectors is implemented, making this function well defined and for all forward directions // TODO(Sven): this convention should be exchanged with one that does not branch RAYX_FN_ACC -inline RotationBase forwardVectorToBaseConvention(glm::dvec3 forward) { +inline RotationBase forwardVectorToBaseConvention(const glm::dvec3 forward) { auto up = glm::dvec3(0, 1, 0); glm::dvec3 right; @@ -246,22 +246,22 @@ inline glm::dmat3 rotationMatrix(const glm::dvec3 forward, const glm::dvec3 up) } RAYX_FN_ACC -inline ElectricField localToGlobalElectricField(LocalElectricField localField, glm::dvec3 forward) { +inline ElectricField localToGlobalElectricField(const LocalElectricField localField, const glm::dvec3 forward) { return rotationMatrix(forward) * ElectricField(localField, complex::Complex{0, 0}); } RAYX_FN_ACC -inline ElectricField localToGlobalElectricField(LocalElectricField localField, glm::dvec3 forward, glm::dvec3 up) { +inline ElectricField localToGlobalElectricField(const LocalElectricField localField, const glm::dvec3 forward, const glm::dvec3 up) { return rotationMatrix(forward, up) * ElectricField(localField, complex::Complex{0, 0}); } RAYX_FN_ACC -inline LocalElectricField globalToLocalElectricField(ElectricField field, glm::dvec3 forward) { +inline LocalElectricField globalToLocalElectricField(const ElectricField field, const glm::dvec3 forward) { return glm::transpose(rotationMatrix(forward)) * field; } RAYX_FN_ACC -inline LocalElectricField globalToLocalElectricField(ElectricField field, glm::dvec3 forward, glm::vec3 up) { +inline LocalElectricField globalToLocalElectricField(const ElectricField field, const glm::dvec3 forward, const glm::vec3 up) { return glm::transpose(rotationMatrix(forward, up)) * field; } diff --git a/docs/src/Model/Efficiency.md b/docs/src/Model/Efficiency.md index 03078877b..2fc153de3 100644 --- a/docs/src/Model/Efficiency.md +++ b/docs/src/Model/Efficiency.md @@ -1,4 +1,5 @@ -# Efficiency + +# Efficiency wiki for efficiency calculations @@ -6,28 +7,28 @@ wiki for efficiency calculations A fraction of the light is reflected and another transmitted: ![refraction_fresnel](https://upload.wikimedia.org/wikipedia/commons/8/89/Fresnel1.svg)
-\\(\theta_i =\\) (normal) incidence angle
-\\(\theta_r =\\) (normal) reflection angle (same as \\(\theta_i\\))
-\\(\theta_t =\\) (normal) transmittance angle
-\\(N_1 =\\) refraction index of material from which the ray is coming (left in image)
-\\(N_2 =\\) refraction index of material into which the ray is going (right in image)
+$\theta_i =$ (normal) incidence angle +$\theta_r =$ (normal) reflection angle (same as $\theta_i$) +$\theta_t =$ (normal) transmittance angle +$N_1 =$ refraction index of material from which the ray is coming (left in image) +$N_2 =$ refraction index of material into which the ray is going (right in image) -all parameters are potentially complex numbers. The refractive indices are retrieved from files (Palik, Henke, Cromer..) +All parameters are potentially complex numbers. +The refractive indices $N_1$ and $N_2$ are retrieved from databases (Palik, Henke, Cromer..). Snell's law: -\\[ +$$ N_1 \sin \theta_i = N_2 \sin \theta_t \rightarrow \sin \theta_t = \frac{N_1}{N_2} \sin \theta_i -\\] +$$ -\\(\theta_i\\), \\(N_1\\), \\(N_2\\) are known, we are looking for \\(\theta_t\\).
-We do not calculate the angle specifically but only the cosinus, which is sufficient for further calculations and more efficient/precise than calculating the angle itself because we do not need to use more trigonometric functions. -We can calculate the incidence angle \\(\theta_i\\) of each ray from its direction and the surface normal. Then we calculate \\(\cos(\theta_i)\\) and from that we can derive \\(\cos(\theta_t)\\) with snell's law: +With $\theta_i$, $\theta_r$, $N_1$, $N_2$ being known, we are looking for $\theta_t$. +We can calculate the incidence angle $\theta_i$ of each ray from its direction and the surface normal. Then we calculate $\cos(\theta_i)$ and from that we can derive $\cos(\theta_t)$ with snell's law: -\\[ +$$ (\sin \theta_i)^2 = 1 - (\cos \theta_i)^2 \\\\ (\sin \theta_t)^2 = (\frac{N_1}{N_2})^2 (\sin \theta_i)^2 \\\\ \cos \theta_t = \sqrt{1 - (\sin \theta_t)^2} = \sqrt{1 - \Big(\frac{N_1}{N_2} \sin \theta_i\Big)^2} -\\] +$$ The cosine of both angles is then used in the Fresnel equations to calculate the s- and p-polarization @@ -38,9 +39,65 @@ p-polarization (parallel, left image) lies parallel in the plane of incidence an ppol spol -the reflectance of both polarizations is calculated with the fresnel equations: +the amplitude coefficient of a reflection for both polarizations is calculated with the fresnel equations: + +$$r_s = \frac{N_1 \cdot \cos \theta_i - N_2 \cdot \cos \theta_t}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}$$ +$$r_p = \frac{N_2 \cdot \cos \theta_i - N_1 \cdot \cos \theta_t}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}$$ + +the amplitude coefficient of a refraction for both polarizations is calculated with the fresnel equations: + +$$t_s = \frac{2.0 \cdot N_1 \cdot \cos \theta_i}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}$$ +$$t_p = \frac{2.0 \cdot N_1 \cdot \cos \theta_i}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}$$ + +### Mirror reflection + +Reflecting a ray on a mirror involves polarization and phase changes of the incident electric field of the ray. The update of the electric field can be described by 3 steps: + +1. Rotate incident electric field from global coordinates into local basis of incoming propagation vector $k$. +1. Multiply fresnel amplitude coefficients with components of the electric field. +1. Rotate resulting electric field back into global coordinates by rotating into basis of outgoing propagation vector $k'$. + +In order to rotate the basis... + +$$ +\begin{aligned} + \vec{s}_{q} &= \frac{\vec{k}_{q-1} \times \vec{\eta}_{q}}{|\vec{k}_{q-1} \times \vec{\eta}_{q}|} \\ + \quad \vec{p}_{q} &= \vec{k}_{q-1} \times \vec{s}_{q} \\ + \vec{s}'_{q} &= \vec{s}_{q}, \quad \vec{p}'_{q} = \vec{k}_{q} \times \vec{s}_{q} +\end{aligned} +$$ + +Both incident vector $\vec{k}_{q-1}$ and reflected vector $\vec{k}_{q}$ lie within the plane of incidence and normal vector $\vec{\eta}_{q}$ is perpendicular and defining the front facing direction of the plane. +We define vectors $\vec{s}$ and $\vec{p}$ perpendicular to propagation vector \(k\), while \(s\) is perpendicular to the plane of incidence and \(k\) lies within the plane. + +$$ +\begin{aligned} + O_{q,in} &= \begin{pmatrix} + s'_{x,q} & p'_{x,q} & k_{x,q} \\ + s'_{y,q} & p'_{y,q} & k_{y,q} \\ + s'_{z,q} & p'_{z,q} & k_{z,q} + \end{pmatrix} \\ + J_{q} &= \begin{pmatrix} + r_{s,q} & 0 & 0 \\ + 0 & r_{p,q} & 0 \\ + 0 & 0 & 1 + \end{pmatrix} \\ + O_{q,out} &= \begin{pmatrix} + s_{x,q} & s_{y,q} & s_{z,q} \\ + p_{x,q} & p_{y,q} & p_{z,q} \\ + k_{x,q-1} & k_{y,q-1} & k_{z,q-1} + \end{pmatrix} \\ + P_{q} &= O_{q,in} \cdot J_{q} \cdot O_{q,out} +\end{aligned} +$$ + +All operations are composed into the Polarization Ray Tracing Matrix $P_{q}$, encorparating fresnel amplitude coefficients into $J_{q}$. + +$$ +E_{q} = P_q E_{q-1} +$$ -\\[r_s = \frac{N_1 \cdot \cos \theta_i - N_2 \cdot \cos \theta_t}{N_1 \cdot \cos \theta_i + N_2 \cdot \cos \theta_t}\\] -\\[r_p = \frac{N_2 \cdot \cos \theta_i - N_1 \cdot \cos \theta_t}{N_2 \cdot \cos \theta_i + N_1 \cdot \cos \theta_t}\\] +$P_{q}$ is multiplied by the incident electric field $E_{q-1}$, resulting in the ray's a new electric field after the intercept with the mirror. -(The transmitted power is then "the rest": \\(t_s = 1 - r_s\\) and \\(t_p = 1 -r_p\\)) \ No newline at end of file +This code implements several formulas from the following book: +Russel A. Chipman, Wai-Sze Tiffany Lam, Garam Young "Polarized Light and Optical Systems" (2019).