CardboardProfile.cs 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using UnityEngine;

/// @cond
/// Measurements of a particular phone in a particular Cardboard device.
[System.Serializable]
public class CardboardProfile {
  public CardboardProfile Clone() {
    return new CardboardProfile {
      screen = this.screen,
      device = this.device
    };
  }

  /// Information about the screen.  All distances are in meters, measured as the phone is expected
  /// to be placed in the Cardboard, i.e. landscape orientation.
  [System.Serializable]
  public struct Screen {
    public float width;   // The long edge of the phone.
    public float height;  // The short edge of the phone.
    public float border;  // Distance from bottom of the cardboard to the bottom edge of screen.
  }

  /// Information about the lens placement in the Cardboard.  All distances are in meters.
  [System.Serializable]
  public struct Lenses {
    public float separation;     // Center to center.
    public float offset;         // Offset of lens center from top or bottom of cardboard.
    public float screenDistance; // Distance from lens center to the phone screen.

    public int alignment;  // Determines whether lenses are placed relative to top, bottom or
                           // center.  It is actually a signum (-1, 0, +1) relating the scale of
                           // the offset's coordinates to the device coordinates.

    public const int AlignTop = -1;    // Offset is measured down from top of device.
    public const int AlignCenter = 0;  // Center alignment ignores offset, hence scale is zero.
    public const int AlignBottom = 1;  // Offset is measured up from bottom of device.
  }

  /// Information about the viewing angles through the lenses.  All angles in degrees, measured
  /// away from the optical axis, i.e. angles are all positive.  It is assumed that left and right
  /// eye FOVs are mirror images, so that both have the same inner and outer angles.  Angles do not
  /// need to account for the limits due to screen size.
  [System.Serializable]
  public struct MaxFOV {
    public float outer;  // Towards the side of the screen.
    public float inner;  // Towards the center line of the screen.
    public float upper;  // Towards the top of the screen.
    public float lower;  // Towards the bottom of the screen.
  }

  /// Information on how the lens distorts light rays.  Also used for the (approximate) inverse
  /// distortion.  Assumes a radially symmetric pincushion/barrel distortion model.
  [System.Serializable]
  public struct Distortion {
    public float k1;
    public float k2;

    public float distort(float r) {
      float r2 = r * r;
      return ((k2 * r2 + k1) * r2 + 1) * r;
    }

    public float distortInv(float radius) {
      // Secant method.
      float r0 = 0;
      float r1 = 1;
      float dr0 = radius - distort(r0);
      while (Mathf.Abs(r1 - r0) > 0.0001f) {
        float dr1 = radius - distort(r1);
        float r2 = r1 - dr1 * ((r1 - r0) / (dr1 - dr0));
        r0 = r1;
        r1 = r2;
        dr0 = dr1;
      }
      return r1;
    }
  }

  /// Information about a particular device, including specfications on its lenses, FOV,
  /// and distortion and inverse distortion coefficients.
  [System.Serializable]
  public struct Device {
    public Lenses lenses;
    public MaxFOV maxFOV;
    public Distortion distortion;
    public Distortion inverse;
  }

  /// Screen parameters of a Cardboard device.
  public Screen screen;

  /// Device parameters of a Cardboard device.
  public Device device;

  /// The vertical offset of the lens centers from the screen center.
  public float VerticalLensOffset {
    get {
      return (device.lenses.offset - screen.border - screen.height/2) * device.lenses.alignment;
    }
  }

  /// Some known screen profiles.
  public enum ScreenSizes {
    Nexus5,
    Nexus6,
    GalaxyS6,
    GalaxyNote4,
    LGG3,
    iPhone4,
    iPhone5,
    iPhone6,
    iPhone6p,
  };

  /// Parameters for a Nexus 5 device.
  public static readonly Screen Nexus5 = new Screen {
    width = 0.110f,
    height = 0.062f,
    border = 0.004f
  };

  /// Parameters for a Nexus 6 device.
  public static readonly Screen Nexus6 = new Screen {
    width = 0.133f,
    height = 0.074f,
    border = 0.004f
  };

  /// Parameters for a Galaxy S6 device.
  public static readonly Screen GalaxyS6 = new Screen {
    width = 0.114f,
    height = 0.0635f,
    border = 0.0035f
  };

  /// Parameters for a Galaxy Note4 device.
  public static readonly Screen GalaxyNote4 = new Screen {
    width = 0.125f,
    height = 0.0705f,
    border = 0.0045f
  };

  /// Parameters for a LG G3 device.
  public static readonly Screen LGG3 = new Screen {
    width = 0.121f,
    height = 0.068f,
    border = 0.003f
  };

  /// Parameters for an iPhone 4 device.
  public static readonly Screen iPhone4 = new Screen {
    width = 0.075f,
    height = 0.050f,
    border = 0.0045f
  };

  /// Parameters for an iPhone 5 device.
  public static readonly Screen iPhone5 = new Screen {
    width = 0.089f,
    height = 0.050f,
    border = 0.0045f
  };

  /// Parameters for an iPhone 6 device.
  public static readonly Screen iPhone6 = new Screen {
    width = 0.104f,
    height = 0.058f,
    border = 0.005f
  };

  /// Parameters for an iPhone 6p device.
  public static readonly Screen iPhone6p = new Screen {
    width = 0.112f,
    height = 0.068f,
    border = 0.005f
  };

  /// Some known Cardboard device profiles.
  public enum DeviceTypes {
    CardboardJun2014,
    CardboardMay2015,
    GoggleTechC1Glass,
  };

  /// Parameters for a Cardboard v1.
  public static readonly Device CardboardJun2014 = new Device {
    lenses = {
      separation = 0.060f,
      offset = 0.035f,
      screenDistance = 0.042f,
      alignment = Lenses.AlignBottom,
    },
    maxFOV = {
      outer = 40.0f,
      inner = 40.0f,
      upper = 40.0f,
      lower = 40.0f
    },
    distortion = {
      k1 = 0.441f,
      k2 = 0.156f
    },
    inverse = ApproximateInverse(0.441f, 0.156f)
  };

  /// Parameters for a Cardboard v2.
  public static readonly Device CardboardMay2015 = new Device {
    lenses = {
      separation = 0.064f,
      offset = 0.035f,
      screenDistance = 0.039f,
      alignment = Lenses.AlignBottom,
    },
    maxFOV = {
      outer = 60.0f,
      inner = 60.0f,
      upper = 60.0f,
      lower = 60.0f
    },
    distortion = {
      k1 = 0.34f,
      k2 = 0.55f
    },
    inverse = ApproximateInverse(0.34f, 0.55f)
  };

  /// Parameters for a Go4D C1-Glass.
  public static readonly Device GoggleTechC1Glass = new Device {
    lenses = {
      separation = 0.065f,
      offset = 0.036f,
      screenDistance = 0.058f,
      alignment = Lenses.AlignBottom,
    },
    maxFOV = {
      outer = 50.0f,
      inner = 50.0f,
      upper = 50.0f,
      lower = 50.0f
    },
    distortion = {
      k1 = 0.3f,
      k2 = 0
    },
    inverse = ApproximateInverse(0.3f, 0)
  };

  /// Nexus 5 in a Cardboard v1.
  public static readonly CardboardProfile Default = new CardboardProfile {
    screen = Nexus5,
    device = CardboardJun2014
  };

  /// Returns a CardboardProfile with the given parameters.
  public static CardboardProfile GetKnownProfile(ScreenSizes screenSize, DeviceTypes deviceType) {
    Screen screen;
    switch (screenSize) {
      case ScreenSizes.Nexus6:
        screen = Nexus6;
        break;
      case ScreenSizes.GalaxyS6:
        screen = GalaxyS6;
        break;
      case ScreenSizes.GalaxyNote4:
        screen = GalaxyNote4;
        break;
      case ScreenSizes.LGG3:
        screen = LGG3;
        break;
      case ScreenSizes.iPhone4:
        screen = iPhone4;
        break;
      case ScreenSizes.iPhone5:
        screen = iPhone5;
        break;
      case ScreenSizes.iPhone6:
        screen = iPhone6;
        break;
      case ScreenSizes.iPhone6p:
        screen = iPhone6p;
        break;
      default:
        screen = Nexus5;
        break;
    }
    Device device;
    switch (deviceType) {
      case DeviceTypes.CardboardMay2015:
        device = CardboardMay2015;
        break;
      case DeviceTypes.GoggleTechC1Glass:
        device = GoggleTechC1Glass;
        break;
      default:
        device = CardboardJun2014;
        break;
    }
    return new CardboardProfile { screen = screen, device = device };
  }

  /// Calculates the tan-angles from the maximum FOV for the left eye for the
  /// current device and screen parameters.
  public void GetLeftEyeVisibleTanAngles(float[] result) {
    // Tan-angles from the max FOV.
    float fovLeft = Mathf.Tan(-device.maxFOV.outer * Mathf.Deg2Rad);
    float fovTop = Mathf.Tan(device.maxFOV.upper * Mathf.Deg2Rad);
    float fovRight = Mathf.Tan(device.maxFOV.inner * Mathf.Deg2Rad);
    float fovBottom = Mathf.Tan(-device.maxFOV.lower * Mathf.Deg2Rad);
    // Viewport size.
    float halfWidth = screen.width / 4;
    float halfHeight = screen.height / 2;
    // Viewport center, measured from left lens position.
    float centerX = device.lenses.separation / 2 - halfWidth;
    float centerY = -VerticalLensOffset;
    float centerZ = device.lenses.screenDistance;
    // Tan-angles of the viewport edges, as seen through the lens.
    float screenLeft = device.distortion.distort((centerX - halfWidth) / centerZ);
    float screenTop = device.distortion.distort((centerY + halfHeight) / centerZ);
    float screenRight = device.distortion.distort((centerX + halfWidth) / centerZ);
    float screenBottom = device.distortion.distort((centerY - halfHeight) / centerZ);
    // Compare the two sets of tan-angles and take the value closer to zero on each side.
    result[0] = Math.Max(fovLeft, screenLeft);
    result[1] = Math.Min(fovTop, screenTop);
    result[2] = Math.Min(fovRight, screenRight);
    result[3] = Math.Max(fovBottom, screenBottom);
  }

  /// Calculates the tan-angles from the maximum FOV for the left eye for the
  /// current device and screen parameters, assuming no lenses.
  public void GetLeftEyeNoLensTanAngles(float[] result) {
    // Tan-angles from the max FOV.
    float fovLeft = device.distortion.distortInv(Mathf.Tan(-device.maxFOV.outer * Mathf.Deg2Rad));
    float fovTop = device.distortion.distortInv(Mathf.Tan(device.maxFOV.upper * Mathf.Deg2Rad));
    float fovRight = device.distortion.distortInv(Mathf.Tan(device.maxFOV.inner * Mathf.Deg2Rad));
    float fovBottom = device.distortion.distortInv(Mathf.Tan(-device.maxFOV.lower * Mathf.Deg2Rad));
    // Viewport size.
    float halfWidth = screen.width / 4;
    float halfHeight = screen.height / 2;
    // Viewport center, measured from left lens position.
    float centerX = device.lenses.separation / 2 - halfWidth;
    float centerY = -VerticalLensOffset;
    float centerZ = device.lenses.screenDistance;
    // Tan-angles of the viewport edges, as seen through the lens.
    float screenLeft = (centerX - halfWidth) / centerZ;
    float screenTop = (centerY + halfHeight) / centerZ;
    float screenRight = (centerX + halfWidth) / centerZ;
    float screenBottom = (centerY - halfHeight) / centerZ;
    // Compare the two sets of tan-angles and take the value closer to zero on each side.
    result[0] = Math.Max(fovLeft, screenLeft);
    result[1] = Math.Min(fovTop, screenTop);
    result[2] = Math.Min(fovRight, screenRight);
    result[3] = Math.Max(fovBottom, screenBottom);
  }

  /// Calculates the screen rectangle visible from the left eye for the
  /// current device and screen parameters.
  public Rect GetLeftEyeVisibleScreenRect(float[] undistortedFrustum) {
    float dist = device.lenses.screenDistance;
    float eyeX = (screen.width - device.lenses.separation) / 2;
    float eyeY = VerticalLensOffset + screen.height / 2;
    float left = (undistortedFrustum[0] * dist + eyeX) / screen.width;
    float top = (undistortedFrustum[1] * dist + eyeY) / screen.height;
    float right = (undistortedFrustum[2] * dist + eyeX) / screen.width;
    float bottom = (undistortedFrustum[3] * dist + eyeY) / screen.height;
    return new Rect(left, bottom, right - left, top - bottom);
  }

  public static float GetMaxRadius(float[] tanAngleRect) {
    float x = Mathf.Max(Mathf.Abs(tanAngleRect[0]), Mathf.Abs(tanAngleRect[2]));
    float y = Mathf.Max(Mathf.Abs(tanAngleRect[1]), Mathf.Abs(tanAngleRect[3]));
    return Mathf.Sqrt(x * x + y * y);
  }

  // Solves a least-squares matrix equation.  Given the equation A * x = y, calculate the
  // least-square fit x = inverse(A * transpose(A)) * transpose(A) * y.  The way this works
  // is that, while A is typically not a square matrix (and hence not invertible), A * transpose(A)
  // is always square.  That is:
  //   A * x = y
  //   transpose(A) * (A * x) = transpose(A) * y   <- multiply both sides by transpose(A)
  //   (transpose(A) * A) * x = transpose(A) * y   <- associativity
  //   x = inverse(transpose(A) * A) * transpose(A) * y  <- solve for x
  // Matrix A's row count (first index) must match y's value count.  A's column count (second index)
  // determines the length of the result vector x.
  private static double[] solveLeastSquares(double[,] matA, double[] vecY) {
    int numSamples = matA.GetLength(0);
    int numCoefficients = matA.GetLength(1);
    if (numSamples != vecY.Length) {
      Debug.LogError("Matrix / vector dimension mismatch");
      return null;
    }
    if (numCoefficients != 2) {
      Debug.LogError("Only 2 coefficients supported.");
      return null;
    }

    // Calculate transpose(A) * A
    double[,] matATA = new double[numCoefficients, numCoefficients];
    for (int k = 0; k < numCoefficients; ++k) {
      for (int j = 0; j < numCoefficients; ++j) {
        double sum = 0.0;
        for (int i = 0; i < numSamples; ++i) {
          sum += matA[i, j] * matA[i, k];
        }
        matATA[j, k] = sum;
      }
    }

    // Calculate the inverse of transpose(A) * A.  Inverting isn't recommended for numerical
    // stability, but should be ok for small and well-behaved data sets.  Using manual matrix
    // inversion here (hence the restriction of numCoefficients to 2 in this function).
    double[,] matInvATA = new double[numCoefficients, numCoefficients];
    double det = matATA[0, 0] * matATA[1, 1] - matATA[0, 1] * matATA[1, 0];
    matInvATA[0, 0] = matATA[1, 1] / det;
    matInvATA[1, 1] = matATA[0, 0] / det;
    matInvATA[0, 1] = -matATA[1, 0] / det;
    matInvATA[1, 0] = -matATA[0, 1] / det;

    // Calculate transpose(A) * y
    double[] vecATY = new double[numCoefficients];
    for (int j = 0; j < numCoefficients; ++j) {
      double sum = 0.0;
      for (int i = 0; i < numSamples; ++i) {
        sum += matA[i, j] * vecY[i];
      }
      vecATY[j] = sum;
    }

    // Now matrix multiply the previous values to get the result.
    double[] vecX = new double[numCoefficients];
    for (int j = 0; j < numCoefficients; ++j) {
      double sum = 0.0;
      for (int i = 0; i < numCoefficients; ++i) {
        sum += matInvATA[i, j] * vecATY[i];
      }
      vecX[j] = sum;
    }
    return vecX;
  }

  /// Calculates an approximate inverse to the given radial distortion parameters.
  public static Distortion ApproximateInverse(float k1, float k2, float maxRadius = 1,
                                              int numSamples = 10) {
    return ApproximateInverse(new Distortion { k1=k1, k2=k2 }, maxRadius, numSamples);
  }

  /// Calculates an approximate inverse to the given radial distortion parameters.
  public static Distortion ApproximateInverse(Distortion distort, float maxRadius = 1,
                                              int numSamples = 10) {
    const int numCoefficients = 2;

    // R + k1*R^3 + k2*R^5 = r, with R = rp = distort(r)
    // Repeating for numSamples:
    //   [ R0^3, R0^5 ] * [ K1 ] = [ r0 - R0 ]
    //   [ R1^3, R1^5 ]   [ K2 ]   [ r1 - R1 ]
    //   [ R2^3, R2^5 ]            [ r2 - R2 ]
    //   [ etc... ]                [ etc... ]
    // That is:
    //   matA * [K1, K2] = y
    // Solve:
    //   [K1, K2] = inverse(transpose(matA) * matA) * transpose(matA) * y
    double[,] matA = new double[numSamples, numCoefficients];
    double[] vecY = new double[numSamples];
    for (int i = 0; i < numSamples; ++i) {
      float r = maxRadius * (i + 1) / (float) numSamples;
      double rp = distort.distort(r);
      double v = rp;
      for (int j = 0; j < numCoefficients; ++j) {
        v *= rp * rp;
        matA[i, j] = v;
      }
      vecY[i] = r - rp;
    }
    double[] vecK = solveLeastSquares(matA, vecY);
    return new Distortion {
      k1 = (float)vecK[0],
      k2 = (float)vecK[1]
    };
  }
}
/// @endcond