CoordinateConverter.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public class CoordinateConverter
  9. {
  10. private ImageSize imageSize;
  11. private float hfov;
  12. private float vfov;
  13. private float hRatio;
  14. private float vRatio;
  15. public CoordinateConverter(ImageSize imageSize, float hfov, float vfov)
  16. {
  17. this.imageSize = imageSize;
  18. this.hfov = hfov;
  19. this.vfov = vfov;
  20. hRatio = (float)Math.Tan(hfov / 2);
  21. vRatio = (float)Math.Tan(vfov / 2);
  22. }
  23. public Vector3D convertCoordinate2Dto3D(Vector2D p, float depth) {
  24. return convertCoordinate2Dto3D(p.X, p.Y, depth);
  25. }
  26. public Vector3D convertCoordinate2Dto3D(float x, float y, float depth)
  27. {
  28. float deltaX = 2 * ((x / imageSize.Width) - 0.5f);
  29. float deltaY = -2 * ((y / imageSize.Height) - 0.5f);
  30. float tanX = deltaX * hRatio;
  31. float tanY = deltaY * vRatio;
  32. float a = depth * depth;
  33. float b = (float)(Math.Pow(tanX, 2) + Math.Pow(tanY, 2));
  34. float z3 = (float)(Math.Sqrt(0.5f*a + Math.Sqrt(0.25f*Math.Pow(a,2)-b)));
  35. float x3 = tanX * z3;
  36. float y3 = tanY * z3;
  37. return new Vector3D(x3, y3, z3);
  38. }
  39. public float convertLength3Dto2D(float length, float depth) {
  40. float fullLengthX = (float)(2 * Math.Cos(hfov / 2) * depth);
  41. float fullLengthY = (float)(2 * Math.Cos(vfov / 2) * depth);
  42. float fullLengthDiagonal = (float)Math.Sqrt(Math.Pow(fullLengthX, 2) + Math.Pow(fullLengthY, 2));
  43. float ratio = length / fullLengthDiagonal;
  44. return ratio * imageSize.DiagonalLength;
  45. }
  46. }
  47. }