CoordinateConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace bbiwarg.Utility
  7. {
  8. /// <summary>
  9. /// Converts between 2d and 3d coordinates.
  10. /// </summary>
  11. public class CoordinateConverter
  12. {
  13. /// <summary>
  14. /// size of the image the coordinates are from
  15. /// </summary>
  16. private ImageSize imageSize;
  17. /// <summary>
  18. /// the horizontal field of view angle
  19. /// </summary>
  20. private float hfov;
  21. /// <summary>
  22. /// the vertical field of view angle
  23. /// </summary>
  24. private float vfov;
  25. /// <summary>
  26. /// tangens of hfov / 2
  27. /// </summary>
  28. private float hRatio;
  29. /// <summary>
  30. /// tanges of vfov / 2
  31. /// </summary>
  32. private float vRatio;
  33. /// <summary>
  34. /// Constructs a CoordinateConverter.
  35. /// </summary>
  36. /// <param name="imageSize">size of the image the coordinates are from</param>
  37. /// <param name="hfov">horizontal field of view angle</param>
  38. /// <param name="vfov">vertical field of view angle</param>
  39. public CoordinateConverter(ImageSize imageSize, float hfov, float vfov)
  40. {
  41. this.imageSize = imageSize;
  42. this.hfov = hfov;
  43. this.vfov = vfov;
  44. hRatio = (float)Math.Tan(hfov / 2);
  45. vRatio = (float)Math.Tan(vfov / 2);
  46. }
  47. /// <summary>
  48. /// Converts a point with a depth to a 3d position.
  49. /// </summary>
  50. /// <param name="p">the point</param>
  51. /// <param name="depth">the depth</param>
  52. /// <returns>the 3d position</returns>
  53. public Vector3D convertCoordinate2Dto3D(Vector2D p, float depth)
  54. {
  55. return convertCoordinate2Dto3D(p.X, p.Y, depth);
  56. }
  57. /// <summary>
  58. /// Converts a position with a depth to a 3d position.
  59. /// </summary>
  60. /// <param name="x">x coordinate of the position</param>
  61. /// <param name="y">y cooridnate of the position</param>
  62. /// <param name="depth">the depth</param>
  63. /// <returns>the 3d position</returns>
  64. public Vector3D convertCoordinate2Dto3D(float x, float y, float depth)
  65. {
  66. float deltaX = 2 * ((x / imageSize.Width) - 0.5f);
  67. float deltaY = -2 * ((y / imageSize.Height) - 0.5f);
  68. float tanX = deltaX * hRatio;
  69. float tanY = deltaY * vRatio;
  70. float a = depth * depth;
  71. float b = (float)(Math.Pow(tanX, 2) + Math.Pow(tanY, 2));
  72. float z3 = (float)(Math.Sqrt(0.5f * a + Math.Sqrt(0.25f * Math.Pow(a, 2) - b)));
  73. float x3 = tanX * z3;
  74. float y3 = tanY * z3;
  75. return new Vector3D(x3, y3, z3);
  76. }
  77. /// <summary>
  78. /// Converts the length of a 3d linesegment parallel to the camera at the given depth to the length of the linesegment in 2d.
  79. /// </summary>
  80. /// <param name="length">3d length of the linesegment</param>
  81. /// <param name="depth">depth of the linesegment</param>
  82. /// <returns>2d length of the linesegment</returns>
  83. public float convertLength3Dto2D(float length, float depth)
  84. {
  85. float fullLengthX = (float)(2 * Math.Cos(hfov / 2) * depth);
  86. float fullLengthY = (float)(2 * Math.Cos(vfov / 2) * depth);
  87. float fullLengthDiagonal = (float)Math.Sqrt(Math.Pow(fullLengthX, 2) + Math.Pow(fullLengthY, 2));
  88. float ratio = length / fullLengthDiagonal;
  89. return ratio * imageSize.DiagonalLength;
  90. }
  91. }
  92. }