test_rotatedrect.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. class Core_RotatedRectConstructorTest : public cvtest::BaseTest
  44. {
  45. public:
  46. Core_RotatedRectConstructorTest();
  47. protected:
  48. int prepare_test_case( int );
  49. void run_func();
  50. int validate_test_results( int );
  51. float MAX_COORD_VAL;
  52. Point2f a, b, c;
  53. RotatedRect rec;
  54. };
  55. Core_RotatedRectConstructorTest::Core_RotatedRectConstructorTest()
  56. {
  57. test_case_count = 100;
  58. MAX_COORD_VAL = 1000.0f;
  59. }
  60. int Core_RotatedRectConstructorTest::prepare_test_case( int test_case_idx )
  61. {
  62. cvtest::BaseTest::prepare_test_case( test_case_idx );
  63. RNG& rng = ts->get_rng();
  64. a = Point2f( rng.uniform(-MAX_COORD_VAL, MAX_COORD_VAL), rng.uniform(-MAX_COORD_VAL, MAX_COORD_VAL) );
  65. do
  66. {
  67. b = Point2f( rng.uniform(-MAX_COORD_VAL, MAX_COORD_VAL), rng.uniform(-MAX_COORD_VAL, MAX_COORD_VAL) );
  68. }
  69. while( cv::norm(a - b) <= FLT_EPSILON );
  70. Vec2f along(a - b);
  71. Vec2f perp = Vec2f(-along[1], along[0]);
  72. double d = (double) rng.uniform(1.0f, 5.0f);
  73. if( cvtest::randInt(rng) % 2 == 0 ) d = -d;
  74. c = Point2f( (float) ((double) b.x + d * perp[0]), (float) ((double) b.y + d * perp[1]) );
  75. return 1;
  76. }
  77. void Core_RotatedRectConstructorTest::run_func()
  78. {
  79. rec = RotatedRect(a, b, c);
  80. }
  81. int Core_RotatedRectConstructorTest::validate_test_results( int )
  82. {
  83. Point2f vertices[4];
  84. rec.points(vertices);
  85. int count_match = 0;
  86. for( int i = 0; i < 4; i++ )
  87. {
  88. if( cv::norm(vertices[i] - a) <= 0.001 ) count_match++;
  89. else if( cv::norm(vertices[i] - b) <= 0.001 ) count_match++;
  90. else if( cv::norm(vertices[i] - c) <= 0.001 ) count_match++;
  91. }
  92. if( count_match == 3 )
  93. return cvtest::TS::OK;
  94. ts->printf( cvtest::TS::LOG, "RotatedRect end points don't match those supplied in constructor");
  95. ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
  96. return cvtest::TS::OK;
  97. }
  98. TEST(Core_RotatedRect, three_point_constructor) { Core_RotatedRectConstructorTest test; test.safe_run(); }
  99. }} // namespace