quadTransforms.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/util/quadTransforms.h"
  24. BiQuadToSqr::BiQuadToSqr( const Point2F &p00,
  25. const Point2F &p10,
  26. const Point2F &p11,
  27. const Point2F &p01 )
  28. : m_kP00( p00 )
  29. {
  30. m_kB = p10 - p00 ; // width
  31. m_kC = p01 - p00; // height
  32. m_kD = p11 + p00 - p10 - p01; // diagonal dist
  33. if(mFabs(m_kD.x) < POINT_EPSILON)
  34. m_kD.x = 0.f;
  35. if(mFabs(m_kD.y) < POINT_EPSILON)
  36. m_kD.y = 0.f;
  37. m_fBC = mDotPerp( m_kB, m_kC );
  38. m_fBD = mDotPerp( m_kB, m_kD );
  39. m_fCD = mDotPerp( m_kC, m_kD );
  40. }
  41. Point2F BiQuadToSqr::transform( const Point2F &p ) const
  42. {
  43. Point2F kA = m_kP00 - p;
  44. F32 fAB = mDotPerp( kA, m_kB );
  45. F32 fAC = mDotPerp( kA, m_kC);
  46. // 0 = ac*bc+(bc^2+ac*bd-ab*cd)*s+bc*bd*s^2 = k0 + k1*s + k2*s^2
  47. F32 fK0 = fAC*m_fBC;
  48. F32 fK1 = m_fBC*m_fBC + fAC*m_fBD - fAB*m_fCD;
  49. F32 fK2 = m_fBC*m_fBD;
  50. if (mFabs(fK2) > POINT_EPSILON)
  51. {
  52. // s-equation is quadratic
  53. F32 fInv = 0.5f/fK2;
  54. F32 fDiscr = fK1*fK1 - 4.0f*fK0*fK2;
  55. F32 fRoot = mSqrt( mFabs(fDiscr) );
  56. Point2F kResult0( 0, 0 );
  57. kResult0.x = (-fK1 - fRoot)*fInv;
  58. kResult0.y = fAB/(m_fBC + m_fBD*kResult0.x);
  59. F32 fDeviation0 = deviation(kResult0);
  60. if ( fDeviation0 == 0.0f )
  61. return kResult0;
  62. Point2F kResult1( 0, 0 );
  63. kResult1.x = (-fK1 + fRoot)*fInv;
  64. kResult1.y = fAB/(m_fBC + m_fBD*kResult1.x);
  65. F32 fDeviation1 = deviation(kResult1);
  66. if ( fDeviation1 == 0.0f )
  67. return kResult1;
  68. if (fDeviation0 <= fDeviation1)
  69. {
  70. if ( fDeviation0 < POINT_EPSILON )
  71. return kResult0;
  72. }
  73. else
  74. {
  75. if ( fDeviation1 < POINT_EPSILON )
  76. return kResult1;
  77. }
  78. }
  79. else
  80. {
  81. // s-equation is linear
  82. Point2F kResult( 0, 0 );
  83. kResult.x = -fK0/fK1;
  84. kResult.y = fAB/(m_fBC + m_fBD*kResult.x);
  85. F32 fDeviation = deviation(kResult);
  86. if ( fDeviation < POINT_EPSILON )
  87. return kResult;
  88. }
  89. // point is outside the quadrilateral, return invalid
  90. return Point2F(F32_MAX,F32_MAX);
  91. }
  92. F32 BiQuadToSqr::deviation( const Point2F &sp )
  93. {
  94. // deviation is the squared distance of the point from the unit square
  95. F32 fDeviation = 0.0f;
  96. F32 fDelta;
  97. if (sp.x < 0.0f)
  98. {
  99. fDeviation += sp.x*sp.x;
  100. }
  101. else if (sp.x > 1.0f)
  102. {
  103. fDelta = sp.x - 1.0f;
  104. fDeviation += fDelta*fDelta;
  105. }
  106. if (sp.y < 0.0f)
  107. {
  108. fDeviation += sp.y*sp.y;
  109. }
  110. else if (sp.y > 1.0f)
  111. {
  112. fDelta = sp.y - 1.0f;
  113. fDeviation += fDelta*fDelta;
  114. }
  115. return fDeviation;
  116. }
  117. BiSqrToQuad3D::BiSqrToQuad3D( const Point3F& pnt00,
  118. const Point3F& pnt10,
  119. const Point3F& pnt11,
  120. const Point3F& pnt01)
  121. {
  122. p00 = pnt00;
  123. p10 = pnt10;
  124. p11 = pnt11;
  125. p01 = pnt01;
  126. }
  127. Point3F BiSqrToQuad3D::transform( const Point2F &p ) const
  128. {
  129. //Let p00, p10, p01, and p11 be your 3-tuples that are the quad's
  130. //vertices. You can parameterize the quad as follows.
  131. //q(s,t) = (1-s)*((1-t)*p00 + t*p01) + s*((1-t)*p10 + t*p11)
  132. //for 0 <= s <= 1 and 0 <= t <= 1. Notice that q(0,0) = p00,
  133. //q(1,0) = p10, q(0,1) = p01, and q(1,1) = p11, so the parameter
  134. //"square" whose points are (s,t) will be mapped to the quad.
  135. const F32 &s = p.x;
  136. const F32 &t = p.y;
  137. Point3F result = (1.0f-s)*((1.0f-t)*p00 + t*p01) + s*((1.0f-t)*p10 + t*p11);
  138. return result;
  139. }