Interpolation.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. namespace UnityEngine.U2D.Animation.TriangleNet
  2. .Tools
  3. {
  4. using Animation.TriangleNet.Geometry;
  5. internal static class Interpolation
  6. {
  7. #if USE_ATTRIBS
  8. /// <summary>
  9. /// Linear interpolation of vertex attributes.
  10. /// </summary>
  11. /// <param name="vertex">The interpolation vertex.</param>
  12. /// <param name="triangle">The triangle containing the vertex.</param>
  13. /// <param name="n">The number of vertex attributes.</param>
  14. /// <remarks>
  15. /// The vertex is expected to lie inside the triangle.
  16. /// </remarks>
  17. internal static void InterpolateAttributes(Vertex vertex, ITriangle triangle, int n)
  18. {
  19. Vertex org = triangle.GetVertex(0);
  20. Vertex dest = triangle.GetVertex(1);
  21. Vertex apex = triangle.GetVertex(2);
  22. double xdo, ydo, xao, yao;
  23. double denominator;
  24. double dx, dy;
  25. double xi, eta;
  26. // Compute the circumcenter of the triangle.
  27. xdo = dest.x - org.x;
  28. ydo = dest.y - org.y;
  29. xao = apex.x - org.x;
  30. yao = apex.y - org.y;
  31. denominator = 0.5 / (xdo * yao - xao * ydo);
  32. //dx = (yao * dodist - ydo * aodist) * denominator;
  33. //dy = (xdo * aodist - xao * dodist) * denominator;
  34. dx = vertex.x - org.x;
  35. dy = vertex.y - org.y;
  36. // To interpolate vertex attributes for the new vertex, define a
  37. // coordinate system with a xi-axis directed from the triangle's
  38. // origin to its destination, and an eta-axis, directed from its
  39. // origin to its apex.
  40. xi = (yao * dx - xao * dy) * (2.0 * denominator);
  41. eta = (xdo * dy - ydo * dx) * (2.0 * denominator);
  42. for (int i = 0; i < n; i++)
  43. {
  44. // Interpolate the vertex attributes.
  45. vertex.attributes[i] = org.attributes[i]
  46. + xi * (dest.attributes[i] - org.attributes[i])
  47. + eta * (apex.attributes[i] - org.attributes[i]);
  48. }
  49. }
  50. #endif
  51. #if USE_Z
  52. /// <summary>
  53. /// Linear interpolation of a scalar value.
  54. /// </summary>
  55. /// <param name="p">The interpolation point.</param>
  56. /// <param name="triangle">The triangle containing the point.</param>
  57. /// <remarks>
  58. /// The point is expected to lie inside the triangle.
  59. /// </remarks>
  60. internal static void InterpolateZ(Point p, ITriangle triangle)
  61. {
  62. Vertex org = triangle.GetVertex(0);
  63. Vertex dest = triangle.GetVertex(1);
  64. Vertex apex = triangle.GetVertex(2);
  65. double xdo, ydo, xao, yao;
  66. double denominator;
  67. double dx, dy;
  68. double xi, eta;
  69. // Compute the circumcenter of the triangle.
  70. xdo = dest.x - org.x;
  71. ydo = dest.y - org.y;
  72. xao = apex.x - org.x;
  73. yao = apex.y - org.y;
  74. denominator = 0.5 / (xdo * yao - xao * ydo);
  75. //dx = (yao * dodist - ydo * aodist) * denominator;
  76. //dy = (xdo * aodist - xao * dodist) * denominator;
  77. dx = p.x - org.x;
  78. dy = p.y - org.y;
  79. // To interpolate z value for the given point inserted, define a
  80. // coordinate system with a xi-axis, directed from the triangle's
  81. // origin to its destination, and an eta-axis, directed from its
  82. // origin to its apex.
  83. xi = (yao * dx - xao * dy) * (2.0 * denominator);
  84. eta = (xdo * dy - ydo * dx) * (2.0 * denominator);
  85. p.z = org.z + xi * (dest.z - org.z) + eta * (apex.z - org.z);
  86. }
  87. #endif
  88. }
  89. }