Calibration.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. namespace TouchlessA3D
  5. {
  6. public enum PreconfiguredCalibrations
  7. {
  8. TA3D_SAMSUNG_S10_26MM_4_BY_3 = 0,
  9. END = 1
  10. }
  11. public interface ICalibration
  12. {
  13. IntPtr getNativeCalibration();
  14. }
  15. // a sample construction of a the camera calibration
  16. public class Calibration : ICalibration
  17. {
  18. //default calibration values for a samsung galaxy s10
  19. public float width = 1440f;
  20. public float height = 1080f;
  21. public float[,] calibrationMatrix = new float[3, 3] { { 1.0901655419118702E3f, 0f, 720f }, { 0f, 1.1031809655612601E3f, 540f }, { 0f, 0f, 1f }
  22. };
  23. public float[] distortionCoefficients = new float[8] {
  24. 1.3524293927664879E-1f, -4.4922888561930802E-1f, 2.3574067791219083E-3f, -8.2965119619095784E-4f, 5.1354760994403104E-1f, 0f, 0f, 0f
  25. };
  26. public NativeCalls.ta3d_calibration_s m_nativeCalibration;
  27. private IntPtr ta3d_calibration_s = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeCalls.ta3d_calibration_s)));
  28. public Calibration()
  29. {
  30. m_nativeCalibration.calibration_size.width = width;
  31. m_nativeCalibration.calibration_size.height = height;
  32. m_nativeCalibration.camera_matrix.elements = new float[9];
  33. for (int row = 0; row < 3; row++)
  34. {
  35. for (int col = 0; col < 3; col++)
  36. {
  37. m_nativeCalibration.camera_matrix.elements[row * 3 + col] = calibrationMatrix[row, col];
  38. }
  39. }
  40. m_nativeCalibration.distortion_coefficients.elements = distortionCoefficients;
  41. }
  42. public Calibration(float width, float height, float[,] calibrationMatrix, float[] distortionCoefficients)
  43. {
  44. this.width = width;
  45. this.height = height;
  46. this.calibrationMatrix = calibrationMatrix;
  47. this.distortionCoefficients = distortionCoefficients;
  48. m_nativeCalibration.calibration_size.width = width;
  49. m_nativeCalibration.calibration_size.height = height;
  50. m_nativeCalibration.camera_matrix.elements = new float[9];
  51. for (int row = 0; row < 3; row++)
  52. {
  53. for (int col = 0; col < 3; col++)
  54. {
  55. m_nativeCalibration.camera_matrix.elements[row * 3 + col] = calibrationMatrix[row, col];
  56. }
  57. }
  58. m_nativeCalibration.distortion_coefficients.elements = distortionCoefficients;
  59. }
  60. public IntPtr getNativeCalibration()
  61. {
  62. Marshal.StructureToPtr(m_nativeCalibration, ta3d_calibration_s, false);
  63. return ta3d_calibration_s;
  64. }
  65. ~Calibration()
  66. {
  67. Marshal.FreeHGlobal(ta3d_calibration_s);
  68. }
  69. }
  70. public class NativeCalibration : ICalibration
  71. {
  72. private IntPtr ta3d_calibration_s = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeCalls.ta3d_calibration_s)));
  73. private PreconfiguredCalibrations type;
  74. public NativeCalibration(PreconfiguredCalibrations type = PreconfiguredCalibrations.TA3D_SAMSUNG_S10_26MM_4_BY_3)
  75. {
  76. this.type = type;
  77. }
  78. public IntPtr getNativeCalibration()
  79. {
  80. NativeCalls.ta3d_get_calibration(type, ta3d_calibration_s);
  81. return ta3d_calibration_s;
  82. }
  83. ~NativeCalibration()
  84. {
  85. Marshal.FreeHGlobal(ta3d_calibration_s);
  86. }
  87. }
  88. }