ProjectMatrixUtility.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental
  10. {
  11. using NRKernal;
  12. using System;
  13. using UnityEngine;
  14. /// <summary> (Serializable) a fov 4f. </summary>
  15. [Serializable]
  16. public class Fov4f
  17. {
  18. /// <summary> The left. </summary>
  19. public double left;
  20. /// <summary> The right. </summary>
  21. public double right;
  22. /// <summary> The top. </summary>
  23. public double top;
  24. /// <summary> The bottom. </summary>
  25. public double bottom;
  26. /// <summary> Default constructor. </summary>
  27. public Fov4f() { }
  28. /// <summary> Constructor. </summary>
  29. /// <param name="l"> A double to process.</param>
  30. /// <param name="r"> A double to process.</param>
  31. /// <param name="t"> A double to process.</param>
  32. /// <param name="b"> A double to process.</param>
  33. public Fov4f(double l, double r, double t, double b)
  34. {
  35. this.left = l;
  36. this.right = r;
  37. this.top = t;
  38. this.bottom = b;
  39. }
  40. /// <summary> Convert this object into a string representation. </summary>
  41. /// <returns> A string that represents this object. </returns>
  42. public override string ToString()
  43. {
  44. return string.Format("{0} {1} {2} {3}", left, right, top, bottom);
  45. }
  46. }
  47. /// <summary> A project matrix utility. </summary>
  48. public class ProjectMatrixUtility
  49. {
  50. /// <summary> Calculates the fov by fcc. </summary>
  51. /// <param name="k_mat"> The matrix.</param>
  52. /// <param name="width"> (Optional) The width.</param>
  53. /// <param name="height"> (Optional) The height.</param>
  54. /// <returns> The calculated fov by fcc. </returns>
  55. public static Fov4f CalculateFOVByFCC(NativeMat3f k_mat, int width = 1920, int height = 1080)
  56. {
  57. Fov4f fov = new Fov4f();
  58. float param = 1.0f / k_mat[2, 2];
  59. NativeMat3f k_mat_normal = NativeMat3f.identity;
  60. for (int i = 0; i < 3; ++i)
  61. {
  62. for (int j = 0; j < 3; ++j)
  63. {
  64. k_mat_normal[i, j] = k_mat[i, j] * param;
  65. }
  66. }
  67. float fc0 = k_mat_normal[0, 0];
  68. float fc1 = k_mat_normal[1, 1];
  69. float cc0 = k_mat_normal[0, 2];
  70. float cc1 = k_mat_normal[1, 2];
  71. float l = -cc0;
  72. float r = width - cc0;
  73. float t = cc1;
  74. float b = cc1 - height;
  75. float x_margin = 0;
  76. float y_margin = 0;
  77. l -= x_margin;
  78. r += x_margin;
  79. t += y_margin;
  80. b -= y_margin;
  81. fov.left = Math.Abs(l / fc0);
  82. fov.right = Math.Abs(r / fc0);
  83. fov.top = Math.Abs(t / fc1);
  84. fov.bottom = Math.Abs(b / fc1);
  85. return fov;
  86. }
  87. /// <summary> Perspective off center. </summary>
  88. /// <param name="left"> The left.</param>
  89. /// <param name="right"> The right.</param>
  90. /// <param name="bottom"> The bottom.</param>
  91. /// <param name="top"> The top.</param>
  92. /// <param name="near"> The near.</param>
  93. /// <param name="far"> The far.</param>
  94. /// <returns> A Matrix4x4. </returns>
  95. public static Matrix4x4 PerspectiveOffCenter(double left, double right, double bottom, double top, float near, float far)
  96. {
  97. left = -left;
  98. bottom = -bottom;
  99. float x = (float)(2.0F / (right - left));
  100. float y = (float)(2.0F / (top - bottom));
  101. float a = (float)((right + left) / (right - left));
  102. float b = (float)((top + bottom) / (top - bottom));
  103. float c = -(far + near) / (far - near);
  104. float d = -(2.0F * far * near) / (far - near);
  105. float e = -1.0F;
  106. Matrix4x4 m = new Matrix4x4();
  107. m[0, 0] = x;
  108. m[0, 1] = 0;
  109. m[0, 2] = a;
  110. m[0, 3] = 0;
  111. m[1, 0] = 0;
  112. m[1, 1] = y;
  113. m[1, 2] = b;
  114. m[1, 3] = 0;
  115. m[2, 0] = 0;
  116. m[2, 1] = 0;
  117. m[2, 2] = c;
  118. m[2, 3] = d;
  119. m[3, 0] = 0;
  120. m[3, 1] = 0;
  121. m[3, 2] = e;
  122. m[3, 3] = 0;
  123. return m;
  124. }
  125. }
  126. }