CoordinateConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. return convertCoordinate2Dto3D(p.X, p.Y, depth);
  55. }
  56. /// <summary>
  57. /// Converts a position with a depth to a 3d position.
  58. /// </summary>
  59. /// <param name="x">x coordinate of the position</param>
  60. /// <param name="y">y cooridnate of the position</param>
  61. /// <param name="depth">the depth</param>
  62. /// <returns>the 3d position</returns>
  63. public Vector3D convertCoordinate2Dto3D(float x, float y, float depth)
  64. {
  65. float deltaX = 2 * ((x / imageSize.Width) - 0.5f);
  66. float deltaY = -2 * ((y / imageSize.Height) - 0.5f);
  67. float tanX = deltaX * hRatio;
  68. float tanY = deltaY * vRatio;
  69. float a = depth * depth;
  70. float b = (float)(Math.Pow(tanX, 2) + Math.Pow(tanY, 2));
  71. float z3 = (float)(Math.Sqrt(0.5f*a + Math.Sqrt(0.25f*Math.Pow(a,2)-b)));
  72. float x3 = tanX * z3;
  73. float y3 = tanY * z3;
  74. return new Vector3D(x3, y3, z3);
  75. }
  76. /// <summary>
  77. /// Converts the length of a 3d linesegment parallel to the camera at the given depth to the length of the linesegment in 2d.
  78. /// </summary>
  79. /// <param name="length">3d length of the linesegment</param>
  80. /// <param name="depth">depth of the linesegment</param>
  81. /// <returns>2d length of the linesegment</returns>
  82. public float convertLength3Dto2D(float length, float depth) {
  83. float fullLengthX = (float)(2 * Math.Cos(hfov / 2) * depth);
  84. float fullLengthY = (float)(2 * Math.Cos(vfov / 2) * depth);
  85. float fullLengthDiagonal = (float)Math.Sqrt(Math.Pow(fullLengthX, 2) + Math.Pow(fullLengthY, 2));
  86. float ratio = length / fullLengthDiagonal;
  87. return ratio * imageSize.DiagonalLength;
  88. }
  89. }
  90. }