PathOpsLineParametetersTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/pathops/SkLineParameters.h"
  8. #include "tests/PathOpsTestCommon.h"
  9. #include "tests/Test.h"
  10. // tests to verify that distance calculations are coded correctly
  11. static const CubicPts tests[] = {
  12. {{{0, 0}, {1, 1}, {2, 2}, {0, 3}}},
  13. {{{0, 0}, {1, 1}, {2, 2}, {3, 0}}},
  14. {{{0, 0}, {5, 0}, {-2, 4}, {3, 4}}},
  15. {{{0, 2}, {1, 0}, {2, 0}, {3, 0}}},
  16. {{{0, .2}, {1, 0}, {2, 0}, {3, 0}}},
  17. {{{0, .02}, {1, 0}, {2, 0}, {3, 0}}},
  18. {{{0, .002}, {1, 0}, {2, 0}, {3, 0}}},
  19. {{{0, .0002}, {1, 0}, {2, 0}, {3, 0}}},
  20. {{{0, .00002}, {1, 0}, {2, 0}, {3, 0}}},
  21. {{{0, FLT_EPSILON * 2}, {1, 0}, {2, 0}, {3, 0}}},
  22. };
  23. static const double answers[][2] = {
  24. {1, 2},
  25. {1, 2},
  26. {4, 4},
  27. {1.1094003924, 0.5547001962},
  28. {0.133038021, 0.06651901052},
  29. {0.0133330370, 0.006666518523},
  30. {0.001333333037, 0.0006666665185},
  31. {0.000133333333, 6.666666652e-05},
  32. {1.333333333e-05, 6.666666667e-06},
  33. {1.5894571940104115e-07, 7.9472859700520577e-08},
  34. };
  35. static const size_t tests_count = SK_ARRAY_COUNT(tests);
  36. DEF_TEST(PathOpsLineParameters, reporter) {
  37. for (size_t index = 0; index < tests_count; ++index) {
  38. SkLineParameters lineParameters;
  39. const CubicPts& c = tests[index];
  40. SkDCubic cubic;
  41. cubic.debugSet(c.fPts);
  42. SkASSERT(ValidCubic(cubic));
  43. lineParameters.cubicEndPoints(cubic, 0, 3);
  44. double denormalizedDistance[2];
  45. denormalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1);
  46. denormalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2);
  47. double normalSquared = lineParameters.normalSquared();
  48. size_t inner;
  49. for (inner = 0; inner < 2; ++inner) {
  50. double distSq = denormalizedDistance[inner];
  51. distSq *= distSq;
  52. double answersSq = answers[index][inner];
  53. answersSq *= answersSq;
  54. if (AlmostEqualUlps(distSq, normalSquared * answersSq)) {
  55. continue;
  56. }
  57. SkDebugf("%s [%d,%d] denormalizedDistance:%g != answer:%g"
  58. " distSq:%g answerSq:%g normalSquared:%g\n",
  59. __FUNCTION__, static_cast<int>(index), (int)inner,
  60. denormalizedDistance[inner], answers[index][inner],
  61. distSq, answersSq, normalSquared);
  62. }
  63. lineParameters.normalize();
  64. double normalizedDistance[2];
  65. normalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1);
  66. normalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2);
  67. for (inner = 0; inner < 2; ++inner) {
  68. if (AlmostEqualUlps(fabs(normalizedDistance[inner]), answers[index][inner])) {
  69. continue;
  70. }
  71. SkDebugf("%s [%d,%d] normalizedDistance:%1.9g != answer:%g\n",
  72. __FUNCTION__, static_cast<int>(index), (int)inner,
  73. normalizedDistance[inner], answers[index][inner]);
  74. REPORTER_ASSERT(reporter, 0);
  75. }
  76. }
  77. }