Monado OpenXR Runtime
deformation_northstar.h
Go to the documentation of this file.
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 {
14 public:
16 
17  OpticalSystem(const OpticalSystem &_in);
18 
19  void
20  LoadOpticalData(struct ns_eye *eye);
21 
22  Vector3
24  {
25  return eyePosition;
26  }
27 
28  Vector2
30 
31  Vector2
33 
34  Vector2
36  Vector2 initailGuess,
37  int iterations);
38 
39  Vector2
41 
42  void
44 
45  void
47  {
48  eyePosition.x = pos.x;
49  eyePosition.y = pos.y;
50  eyePosition.z = pos.z;
51  }
52 
53  const Vector4
55  {
56  return cameraProjection;
57  }
58 
59  void
60  setiters(int init, int opt)
61  {
62  m_iniSolverIters = init;
63  m_optSolverIters = opt;
64  }
65 
66  void
67  UpdateClipToWorld(Matrix4x4 eyeRotationMatrix)
68  {
69  Matrix4x4 eyeToWorld =
70  Matrix4x4::Translate(eyePosition) * eyeRotationMatrix;
71  eyeToWorld.m02 *= -1;
72  eyeToWorld.m12 *= -1;
73  eyeToWorld.m22 *= -1;
74  clipToWorld =
75  eyeToWorld * cameraProjection.ComposeProjection().Inverse();
76  }
77 
79 
80  inline void
82  Vector3 cameraPosition,
83  Matrix4x4 clipToWorld,
84  Vector3 &out)
85  {
86  Vector3 tmp;
87  tmp.x = UV.x - 0.5f;
88  tmp.y = UV.y - 0.5f;
89  tmp.z = 0.f;
90  Vector3 dir =
91  clipToWorld.MultiplyPoint(tmp * 2.f) - cameraPosition;
92 
93  float mag = dir.Magnitude();
94  out = dir / mag;
95  return;
96  }
97 
98 private:
99  float ellipseMinorAxis;
100  float ellipseMajorAxis;
101  Vector3 screenForward;
102  Vector3 screenPosition;
103 
104  Vector4 cameraProjection;
105  Matrix4x4 worldToSphereSpace;
106  Matrix4x4 sphereToWorldSpace;
107  Matrix4x4 worldToScreenSpace;
108  Matrix4x4 clipToWorld;
109 
110  int m_iniSolverIters;
111  int m_optSolverIters;
112 
113  std::map<float, std::map<float, Vector2> > m_requestedUVs;
114 };
115 
116 // supporting functions
117 inline Vector3
119 {
120  Vector3 v2Norm = (v2 / v2.Magnitude());
121  return v2Norm * Vector3::Dot(v1, v2Norm);
122 }
123 
124 inline float
126  Vector3 Direction,
127  Vector3 spherePos,
128  float SphereRadiusSqrd,
129  bool frontSide = true)
130 {
131  Vector3 L = spherePos - Origin;
132  Vector3 offsetFromSphereCenterToRay = Project(L, Direction) - L;
133  return (offsetFromSphereCenterToRay.sqrMagnitude() <= SphereRadiusSqrd)
134  ? Vector3::Dot(L, Direction) -
135  (sqrt(SphereRadiusSqrd -
136  offsetFromSphereCenterToRay.sqrMagnitude()) *
137  (frontSide ? 1.f : -1.f))
138  : -1.f;
139 }
140 
141 inline float
143 {
144 
145  float denom = Vector3::Dot((Vector3::Zero() - n), l);
146  if (denom > 1.4e-45f) {
147  Vector3 p0l0 = p0 - l0;
148  float t = Vector3::Dot(p0l0, (Vector3::Zero() - n)) / denom;
149  return t;
150  }
151  return -1.f;
152 }
float m02
Definition: utility_northstar.h:683
Vector3 Project(Vector3 v1, Vector3 v2)
Definition: deformation_northstar.h:118
const Vector4 GetCameraProjection()
Definition: deformation_northstar.h:54
float x
Definition: utility_northstar.h:277
Matrix4x4 ComposeProjection()
Definition: utility_northstar.h:733
Definition: utility_northstar.h:368
float Magnitude()
Definition: utility_northstar.h:186
static float Dot(Vector3 lhs, Vector3 rhs)
Definition: utility_northstar.h:151
float intersectPlane(Vector3 n, Vector3 p0, Vector3 l0, Vector3 l)
Definition: deformation_northstar.h:142
Distortion information about an eye parsed from the configuration file.
Definition: ns_hmd.h:99
void ViewportPointToRayDirection(Vector2 UV, Vector3 cameraPosition, Matrix4x4 clipToWorld, Vector3 &out)
Definition: deformation_northstar.h:81
void UpdateClipToWorld(Matrix4x4 eyeRotationMatrix)
Definition: deformation_northstar.h:67
Definition: utility_northstar.h:283
void setiters(int init, int opt)
Definition: deformation_northstar.h:60
float x
Definition: utility_northstar.h:364
void UpdateEyePosition(const Vector3 pos)
Definition: deformation_northstar.h:46
Vector3 GetEyePosition()
Definition: deformation_northstar.h:23
void RegenerateMesh()
Definition: deformation_northstar.cpp:86
float m22
Definition: utility_northstar.h:693
Vector3 eyePosition
Definition: deformation_northstar.h:78
float sqrMagnitude()
Definition: utility_northstar.h:180
Definition: utility_northstar.h:703
Vector3 MultiplyPoint(Vector3 point)
Definition: utility_northstar.h:551
Vector2 RenderUVToDisplayUV(Vector3 inputUV)
Definition: deformation_northstar.cpp:121
float y
Definition: utility_northstar.h:278
float m12
Definition: utility_northstar.h:688
Vector2 SolveDisplayUVToRenderUV(Vector2 inputUV, Vector2 initailGuess, int iterations)
Definition: deformation_northstar.cpp:191
static Matrix4x4 Translate(Vector3 vector)
Definition: utility_northstar.h:569
Matrix4x4 Inverse()
Definition: utility_northstar.h:635
float y
Definition: utility_northstar.h:365
OpticalSystem()
Definition: deformation_northstar.h:15
float intersectLineSphere(Vector3 Origin, Vector3 Direction, Vector3 spherePos, float SphereRadiusSqrd, bool frontSide=true)
Definition: deformation_northstar.h:125
Definition: utility_northstar.h:16
float z
Definition: utility_northstar.h:279
Vector2 DisplayUVToRenderUVPreviousSeed(Vector2 inputUV)
Definition: deformation_northstar.cpp:239
static Vector3 Zero()
Definition: utility_northstar.h:192
Definition: deformation_northstar.h:12
void LoadOpticalData(struct ns_eye *eye)
Definition: deformation_northstar.cpp:23