CoordinateConverter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using BBIWARG.Input.InputProviding;
  3. namespace BBIWARG.Utility
  4. {
  5. /// <summary>
  6. /// Converts between 2d and 3d coordinates.
  7. /// </summary>
  8. public class CoordinateConverter
  9. {
  10. private IInputProvider inputProvider;
  11. /// <summary>
  12. /// Constructs a CoordinateConverter.
  13. /// </summary>
  14. /// <param name="imageSize">size of the image the coordinates are from</param>
  15. /// <param name="hfov">horizontal field of view angle</param>
  16. /// <param name="vfov">vertical field of view angle</param>
  17. public CoordinateConverter(IInputProvider inputProvider)
  18. {
  19. this.inputProvider = inputProvider;
  20. }
  21. /// <summary>
  22. /// Converts a point with a depth to a 3d position.
  23. /// </summary>
  24. /// <param name="p">the point</param>
  25. /// <param name="depth">the depth</param>
  26. /// <returns>the 3d position</returns>
  27. public Vector3D convertCoordinate2Dto3D(Vector2D p, float depth)
  28. {
  29. return convertCoordinate2Dto3D(p.X, p.Y, depth);
  30. }
  31. /// <summary>
  32. /// Converts a position with a depth to a 3d position.
  33. /// </summary>
  34. /// <param name="x">x coordinate of the position</param>
  35. /// <param name="y">y coordinate of the position</param>
  36. /// <param name="depth">the depth</param>
  37. /// <returns>the 3d position</returns>
  38. public Vector3D convertCoordinate2Dto3D(float x, float y, float depth)
  39. {
  40. float deltaX = 2 * ((x / inputProvider.ImageWidth) - 0.5f);
  41. float deltaY = -2 * ((y / inputProvider.ImageHeight) - 0.5f);
  42. float tanX = deltaX * (float)Math.Tan(inputProvider.FieldOfViewHorizontal / 2);
  43. float tanY = deltaY * (float)Math.Tan(inputProvider.FieldOfViewVertical / 2);
  44. float a = depth * depth;
  45. float b = (float)(Math.Pow(tanX, 2) + Math.Pow(tanY, 2));
  46. float z3 = (float)Math.Sqrt(0.5f * a + Math.Sqrt(0.25f * Math.Pow(a, 2) - b));
  47. float x3 = tanX * z3;
  48. float y3 = tanY * z3;
  49. return new Vector3D(x3, y3, z3);
  50. }
  51. /// <summary>
  52. /// Converts the length of a 3d line segment parallel to the camera at the given depth to the length of the line segment in 2d.
  53. /// </summary>
  54. /// <param name="length">3d length of the line segment</param>
  55. /// <param name="depth">depth of the line segment</param>
  56. /// <returns>2d length of the line segment</returns>
  57. public float convertLength3Dto2D(float length, float depth)
  58. {
  59. float fullLengthX = (float)(2 * Math.Cos(inputProvider.FieldOfViewHorizontal / 2) * depth);
  60. float fullLengthY = (float)(2 * Math.Cos(inputProvider.FieldOfViewVertical / 2) * depth);
  61. float fullLengthDiagonal = (float)Math.Sqrt(Math.Pow(fullLengthX, 2) + Math.Pow(fullLengthY, 2));
  62. float ratio = length / fullLengthDiagonal;
  63. float imageSizeDiagonalLength = (float)Math.Sqrt(inputProvider.ImageWidth*inputProvider.ImageWidth + inputProvider.ImageHeight*inputProvider.ImageHeight);
  64. return ratio * imageSizeDiagonalLength;
  65. }
  66. }
  67. }