Monado OpenXR Runtime
deformation_northstar.h
1// Copyright 2020, Hesham Wahba.
2// Copyright 2020, Nova King.
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include "utility_northstar.h"
8#include "../ns_hmd.h"
9#include <map>
10
11
13{
14public:
15 OpticalSystem(){};
16
17 OpticalSystem(const OpticalSystem &_in);
18
19 void
20 LoadOpticalData(struct ns_3d_eye *eye);
21
23 GetEyePosition() const
24 {
25 return eyePosition;
26 }
27
29 RenderUVToDisplayUV(const Vector3 &inputUV);
30
32 RenderUVToDisplayUV(const Vector2 &inputUV);
33
35 SolveDisplayUVToRenderUV(const Vector2 &inputUV, Vector2 const &initialGuess, int iterations);
36
38 DisplayUVToRenderUVPreviousSeed(const Vector2 &inputUV);
39
40 void
41 RegenerateMesh();
42
43 void
44 UpdateEyePosition(const Vector3 &pos)
45 {
46 eyePosition.x = pos.x;
47 eyePosition.y = pos.y;
48 eyePosition.z = pos.z;
49 }
50
52 GetCameraProjection()
53 {
54 return cameraProjection;
55 }
56
57 void
58 setiters(int init, int opt)
59 {
60 m_iniSolverIters = init;
61 m_optSolverIters = opt;
62 }
63
64 void
65 UpdateClipToWorld(const Matrix4x4 &eyeRotationMatrix)
66 {
67 Matrix4x4 eyeToWorld = Matrix4x4::Translate(eyePosition) * eyeRotationMatrix;
68 eyeToWorld.m02 *= -1;
69 eyeToWorld.m12 *= -1;
70 eyeToWorld.m22 *= -1;
71 clipToWorld = eyeToWorld * cameraProjection.ComposeProjection().Inverse();
72 }
73
74 Vector3 eyePosition;
75
76 static inline void
77 ViewportPointToRayDirection(const Vector2 &UV,
78 const Vector3 &cameraPosition,
79 const Matrix4x4 &clipToWorld,
80 Vector3 &out)
81 {
83 tmp.x = UV.x - 0.5f;
84 tmp.y = UV.y - 0.5f;
85 tmp.z = 0.f;
86 Vector3 dir = clipToWorld.MultiplyPoint(tmp * 2.f) - cameraPosition;
87
88 float mag = dir.Magnitude();
89 out = dir / mag;
90 }
91
92private:
93 float ellipseMinorAxis;
94 float ellipseMajorAxis;
95 Vector3 screenForward;
96 Vector3 screenPosition;
97
98 Vector4 cameraProjection;
99 Matrix4x4 worldToSphereSpace;
100 Matrix4x4 sphereToWorldSpace;
101 Matrix4x4 worldToScreenSpace;
102 Matrix4x4 clipToWorld;
103
104 int m_iniSolverIters;
105 int m_optSolverIters;
106
107 std::map<float, std::map<float, Vector2> > m_requestedUVs;
108};
109
110// supporting functions
111inline Vector3
112Project(const Vector3 &v1, const Vector3 &v2)
113{
114 Vector3 v2Norm = (v2 / v2.Magnitude());
115 return v2Norm * Vector3::Dot(v1, v2Norm);
116}
117
118inline float
119intersectLineSphere(const Vector3 &Origin,
120 const Vector3 &Direction,
121 const Vector3 &spherePos,
122 float SphereRadiusSqrd,
123 bool frontSide = true)
124{
125 Vector3 L = spherePos - Origin;
126 Vector3 offsetFromSphereCenterToRay = Project(L, Direction) - L;
127 return (offsetFromSphereCenterToRay.sqrMagnitude() <= SphereRadiusSqrd)
128 ? Vector3::Dot(L, Direction) - (sqrt(SphereRadiusSqrd - offsetFromSphereCenterToRay.sqrMagnitude()) *
129 (frontSide ? 1.f : -1.f))
130 : -1.f;
131}
132
133inline float
134intersectPlane(const Vector3 &n, const Vector3 &p0, const Vector3 &l0, const Vector3 &l)
135{
136
137 float denom = Vector3::Dot((Vector3::Zero() - n), l);
138 if (denom > 1.4e-45f) {
139 Vector3 p0l0 = p0 - l0;
140 float t = Vector3::Dot(p0l0, (Vector3::Zero() - n)) / denom;
141 return t;
142 }
143 return -1.f;
144}
Definition: utility_northstar.h:356
Definition: deformation_northstar.h:13
Definition: utility_northstar.h:270
Definition: utility_northstar.h:16
Definition: utility_northstar.h:665
Distortion information about an eye parsed from the configuration file.
Definition: ns_hmd.h:50
Definition: comp_scratch.c:122