blend2d.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //----------------------------------------------------------------------------------------
  2. /*
  3. Allows weighted blending of values defined by their 2D position in space.
  4. */
  5. class Blend2D<Class T>
  6. {
  7. private ref array<vector> m_Positions;
  8. private ref array<T> m_Values;
  9. private ref array<float> m_Weights;
  10. //----------------------------------------------------------------------------------------
  11. /*!
  12. Create new blend structure.
  13. */
  14. void Blend2D()
  15. {
  16. m_Positions = {};
  17. m_Weights = {};
  18. m_Values = {};
  19. }
  20. //----------------------------------------------------------------------------------------
  21. /*!
  22. Insert new `value` at coordinate [posX, posY].
  23. */
  24. void Insert(float posX, float posY, T value)
  25. {
  26. m_Positions.Insert(Vector(posX, posY, 0));
  27. m_Values.Insert(value);
  28. m_Weights.Insert(0);
  29. }
  30. //----------------------------------------------------------------------------------------
  31. /*!
  32. Empty the blend structure.
  33. */
  34. void Clear()
  35. {
  36. m_Positions.Clear();
  37. m_Values.Clear();
  38. m_Weights.Clear();
  39. }
  40. //----------------------------------------------------------------------------------------
  41. /*
  42. Evaluate and return the result of the blend sampled at coordinate [posX, posY].
  43. */
  44. T Blend(float posX, float posY)
  45. {
  46. vector samplePosition = Vector(posX, posY, 0);
  47. Math3D.BlendCartesian(samplePosition, m_Positions, m_Weights);
  48. T result;
  49. int numValues = m_Values.Count();
  50. for (int v = 0; v < numValues; ++v)
  51. {
  52. result += (m_Values[v] * m_Weights[v]);
  53. }
  54. return result;
  55. }
  56. }
  57. //----------------------------------------------------------------------------------------
  58. typedef Blend2D<vector> Blend2DVector;