SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
LensConfig Struct Reference

#include <SLGLOVRWorkaround.h>

Public Member Functions

float DistortionFnScaleRadiusSquared (float rsq) const
 
SLVec3f DistortionFnScaleRadiusSquaredChroma (float rsq) const
 
float DistortionFn (float r) const
 
float DistortionFnInverse (float r) const
 
float DistortionFnInverseApprox (float r) const
 
void SetUpInverseApprox ()
 
void SetToIdentity ()
 

Public Attributes

DistortionEqnType Eqn
 
float K [NumCoefficients]
 
float MaxR
 
float MetersPerTanAngleAtCenter
 
float ChromaticAberration [4]
 
float InvK [NumCoefficients]
 
float MaxInvR
 

Detailed Description

Definition at line 217 of file SLGLOVRWorkaround.h.

Member Function Documentation

◆ DistortionFn()

float LensConfig::DistortionFn ( float  r) const
inline

Definition at line 263 of file SLGLOVRWorkaround.h.

264  {
265  return r * DistortionFnScaleRadiusSquared(r * r);
266  }
float DistortionFnScaleRadiusSquared(float rsq) const

◆ DistortionFnInverse()

float LensConfig::DistortionFnInverse ( float  r) const
inline

Definition at line 269 of file SLGLOVRWorkaround.h.

270  {
271  assert((r <= 20.0f));
272 
273  float s, d;
274  float delta = r * 0.25f;
275 
276  // Better to start guessing too low & take longer to converge than too high
277  // and hit singularities. Empirically, r * 0.5f is too high in some cases.
278  s = r * 0.25f;
279  d = fabs(r - DistortionFn(s));
280 
281  for (int i = 0; i < 20; i++)
282  {
283  float sUp = s + delta;
284  float sDown = s - delta;
285  float dUp = fabs(r - DistortionFn(sUp));
286  float dDown = fabs(r - DistortionFn(sDown));
287 
288  if (dUp < d)
289  {
290  s = sUp;
291  d = dUp;
292  }
293  else if (dDown < d)
294  {
295  s = sDown;
296  d = dDown;
297  }
298  else
299  {
300  delta *= 0.5f;
301  }
302  }
303 
304  return s;
305  }
SLScene * s
Definition: SLScene.h:31
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
float DistortionFn(float r) const

◆ DistortionFnInverseApprox()

float LensConfig::DistortionFnInverseApprox ( float  r) const
inline

Definition at line 308 of file SLGLOVRWorkaround.h.

309  {
310  float rsq = r * r;
311  float scale = 1.0f;
312  switch (Eqn)
313  {
314  case Distortion_Poly4:
315  // Deprecated
316  assert(false);
317  break;
319  scale = 1.0f / (InvK[0] + rsq * (InvK[1] + rsq * (InvK[2] + rsq * InvK[3])));
320  break;
322  {
323  // A Catmull-Rom spline through the values 1.0, K[1], K[2] ... K[9]
324  // evenly spaced in R^2 from 0.0 to MaxR^2
325  // K[0] controls the slope at radius=0.0, rather than the actual value.
326  const int NumSegments = NumCoefficients;
327  assert(NumSegments <= NumCoefficients);
328  float scaledRsq = (float)(NumSegments - 1) * rsq / (MaxInvR * MaxInvR);
329  scale = EvalCatmullRom10Spline(InvK, scaledRsq);
330  }
331  break;
332  default:
333  assert(false);
334  break;
335  }
336  return r * scale;
337  }
@ NumCoefficients
@ Distortion_RecipPoly4
@ Distortion_CatmullRom10
@ Distortion_Poly4
float EvalCatmullRom10Spline(float const *K, float scaledVal)
DistortionEqnType Eqn
float InvK[NumCoefficients]

◆ DistortionFnScaleRadiusSquared()

float LensConfig::DistortionFnScaleRadiusSquared ( float  rsq) const
inline

Definition at line 220 of file SLGLOVRWorkaround.h.

221  {
222  float scale = 1.0f;
223  switch (Eqn)
224  {
225  case Distortion_Poly4:
226  // This version is deprecated! Prefer one of the other two.
227  scale = (K[0] + rsq * (K[1] + rsq * (K[2] + rsq * K[3])));
228  break;
230  scale = 1.0f / (K[0] + rsq * (K[1] + rsq * (K[2] + rsq * K[3])));
231  break;
233  {
234  // A Catmull-Rom spline through the values 1.0, K[1], K[2] ... K[10]
235  // evenly spaced in R^2 from 0.0 to MaxR^2
236  // K[0] controls the slope at radius=0.0, rather than the actual value.
237  const int NumSegments = NumCoefficients;
238  assert(NumSegments <= NumCoefficients);
239  float scaledRsq = (float)(NumSegments - 1) * rsq / (MaxR * MaxR);
240  scale = EvalCatmullRom10Spline(K, scaledRsq);
241  }
242  break;
243  default:
244  assert(false);
245  break;
246  }
247  return scale;
248  }
float K[NumCoefficients]

◆ DistortionFnScaleRadiusSquaredChroma()

SLVec3f LensConfig::DistortionFnScaleRadiusSquaredChroma ( float  rsq) const
inline

Definition at line 250 of file SLGLOVRWorkaround.h.

251  {
252  float scale = DistortionFnScaleRadiusSquared(rsq);
253  SLVec3f scaleRGB;
254  scaleRGB.x = scale * (1.0f + ChromaticAberration[0] + rsq * ChromaticAberration[1]); // Red
255  scaleRGB.y = scale; // Green
256  scaleRGB.z = scale * (1.0f + ChromaticAberration[2] + rsq * ChromaticAberration[3]); // Blue
257  return scaleRGB;
258  }
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43
float ChromaticAberration[4]

◆ SetToIdentity()

void LensConfig::SetToIdentity ( )
inline

Definition at line 425 of file SLGLOVRWorkaround.h.

426  {
427  for (int i = 0; i < NumCoefficients; i++)
428  {
429  K[i] = 0.0f;
430  InvK[i] = 0.0f;
431  }
433  K[0] = 1.0f;
434  InvK[0] = 1.0f;
435  MaxR = 1.0f;
436  MaxInvR = 1.0f;
437  ChromaticAberration[0] = 0.0f;
438  ChromaticAberration[1] = 0.0f;
439  ChromaticAberration[2] = 0.0f;
440  ChromaticAberration[3] = 0.0f;
442  }
float MetersPerTanAngleAtCenter

◆ SetUpInverseApprox()

void LensConfig::SetUpInverseApprox ( )
inline

Definition at line 339 of file SLGLOVRWorkaround.h.

340  {
341  float maxR = MaxInvR;
342 
343  switch (Eqn)
344  {
345  case Distortion_Poly4:
346  // Deprecated
347  assert(false);
348  break;
350  {
351 
352  float sampleR[4];
353  float sampleRSq[4];
354  float sampleInv[4];
355  float sampleFit[4];
356 
357  // Found heuristically...
358  sampleR[0] = 0.0f;
359  sampleR[1] = maxR * 0.4f;
360  sampleR[2] = maxR * 0.8f;
361  sampleR[3] = maxR * 1.5f;
362  for (int i = 0; i < 4; i++)
363  {
364  sampleRSq[i] = sampleR[i] * sampleR[i];
365  sampleInv[i] = DistortionFnInverse(sampleR[i]);
366  sampleFit[i] = sampleR[i] / sampleInv[i];
367  }
368  sampleFit[0] = 1.0f;
369  FitCubicPolynomial(InvK, sampleRSq, sampleFit);
370 
371 #if 0
372  // Should be a nearly exact match on the chosen points.
373  OVR_ASSERT ( fabs ( DistortionFnInverse ( sampleR[0] ) - DistortionFnInverseApprox ( sampleR[0] ) ) / maxR < 0.0001f );
374  OVR_ASSERT ( fabs ( DistortionFnInverse ( sampleR[1] ) - DistortionFnInverseApprox ( sampleR[1] ) ) / maxR < 0.0001f );
375  OVR_ASSERT ( fabs ( DistortionFnInverse ( sampleR[2] ) - DistortionFnInverseApprox ( sampleR[2] ) ) / maxR < 0.0001f );
376  OVR_ASSERT ( fabs ( DistortionFnInverse ( sampleR[3] ) - DistortionFnInverseApprox ( sampleR[3] ) ) / maxR < 0.0001f );
377  // Should be a decent match on the rest of the range.
378  const int maxCheck = 20;
379  for ( int i = 0; i < maxCheck; i++ )
380  {
381  float checkR = (float)i * maxR / (float)maxCheck;
382  float realInv = DistortionFnInverse ( checkR );
383  float testInv = DistortionFnInverseApprox ( checkR );
384  float error = fabsf ( realInv - testInv ) / maxR;
385  OVR_ASSERT ( error < 0.1f );
386  }
387 #endif
388  }
389  break;
391  {
392 
393  const int NumSegments = NumCoefficients;
394  assert(NumSegments <= NumCoefficients);
395  for (int i = 1; i < NumSegments; i++)
396  {
397  float scaledRsq = (float)i;
398  float rsq = scaledRsq * MaxInvR * MaxInvR / (float)(NumSegments - 1);
399  float r = sqrtf(rsq);
400  float inv = DistortionFnInverse(r);
401  InvK[i] = inv / r;
402  InvK[0] = 1.0f; // TODO: fix this.
403  }
404 
405 #if 0
406  const int maxCheck = 20;
407  for ( int i = 0; i <= maxCheck; i++ )
408  {
409  float checkR = (float)i * MaxInvR / (float)maxCheck;
410  float realInv = DistortionFnInverse ( checkR );
411  float testInv = DistortionFnInverseApprox ( checkR );
412  float error = fabsf ( realInv - testInv ) / MaxR;
413  OVR_ASSERT ( error < 0.01f );
414  }
415 #endif
416  }
417  break;
418 
419  default:
420  break;
421  }
422  }
bool FitCubicPolynomial(float *pResult, const float *pFitX, const float *pFitY)
float DistortionFnInverse(float r) const
float DistortionFnInverseApprox(float r) const

Member Data Documentation

◆ ChromaticAberration

float LensConfig::ChromaticAberration[4]

Definition at line 455 of file SLGLOVRWorkaround.h.

◆ Eqn

DistortionEqnType LensConfig::Eqn

Definition at line 444 of file SLGLOVRWorkaround.h.

◆ InvK

float LensConfig::InvK[NumCoefficients]

Definition at line 457 of file SLGLOVRWorkaround.h.

◆ K

float LensConfig::K[NumCoefficients]

Definition at line 445 of file SLGLOVRWorkaround.h.

◆ MaxInvR

float LensConfig::MaxInvR

Definition at line 458 of file SLGLOVRWorkaround.h.

◆ MaxR

float LensConfig::MaxR

Definition at line 446 of file SLGLOVRWorkaround.h.

◆ MetersPerTanAngleAtCenter

float LensConfig::MetersPerTanAngleAtCenter

Definition at line 448 of file SLGLOVRWorkaround.h.


The documentation for this struct was generated from the following file: