You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
16 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 KiCad Developers, see AUTHORS.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * Test suite for KiCad math code.
  25. */
  26. #include <qa_utils/wx_utils/unit_test_utils.h>
  27. // Code under test
  28. #include <trigo.h>
  29. #include <hash_128.h>
  30. #include <geometry/shape_arc.h>
  31. #include <iomanip>
  32. /*
  33. CircleCenterFrom3Points calculate the center of a circle defined by 3 points
  34. It is similar to CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd )
  35. but it was needed to debug CalcArcCenter, so I keep it available for other issues in CalcArcCenter
  36. The perpendicular bisector of the segment between two points is the
  37. set of all points equidistant from both. So if you take the
  38. perpendicular bisector of (x1,y1) and (x2,y2) and the perpendicular
  39. bisector of the segment from (x2,y2) to (x3,y3) and find the
  40. intersection of those lines, that point will be the center.
  41. To find the equation of the perpendicular bisector of (x1,y1) to (x2,y2),
  42. you know that it passes through the midpoint of the segment:
  43. ((x1+x2)/2,(y1+y2)/2), and if the slope of the line
  44. connecting (x1,y1) to (x2,y2) is m, the slope of the perpendicular
  45. bisector is -1/m. Work out the equations for the two lines, find
  46. their intersection, and bingo! You've got the coordinates of the center.
  47. An error should occur if the three points lie on a line, and you'll
  48. need special code to check for the case where one of the slopes is zero.
  49. see https://web.archive.org/web/20171223103555/http://mathforum.org/library/drmath/view/54323.html
  50. */
  51. static bool Ref0CircleCenterFrom3Points( const VECTOR2D& p1, const VECTOR2D& p2, const VECTOR2D& p3,
  52. VECTOR2D* aCenter )
  53. {
  54. // Move coordinate origin to p2, to simplify calculations
  55. VECTOR2D b = p1 - p2;
  56. VECTOR2D d = p3 - p2;
  57. double bc = ( b.x * b.x + b.y * b.y ) / 2.0;
  58. double cd = ( -d.x * d.x - d.y * d.y ) / 2.0;
  59. double det = -b.x * d.y + d.x * b.y;
  60. if( fabs( det ) < 1.0e-6 ) // arbitrary limit to avoid divide by 0
  61. return false;
  62. det = 1 / det;
  63. aCenter->x = ( -bc * d.y - cd * b.y ) * det;
  64. aCenter->y = ( b.x * cd + d.x * bc ) * det;
  65. *aCenter += p2;
  66. return true;
  67. }
  68. // Based on https://paulbourke.net/geometry/circlesphere/
  69. // Uses the Y'b equation too, depending on slope values
  70. static const VECTOR2D Ref1CalcArcCenter( const VECTOR2D& aStart, const VECTOR2D& aMid, const VECTOR2D& aEnd )
  71. {
  72. VECTOR2D center;
  73. double yDelta_21 = aMid.y - aStart.y;
  74. double xDelta_21 = aMid.x - aStart.x;
  75. double yDelta_32 = aEnd.y - aMid.y;
  76. double xDelta_32 = aEnd.x - aMid.x;
  77. // This is a special case for aMid as the half-way point when aSlope = 0 and bSlope = inf
  78. // or the other way around. In that case, the center lies in a straight line between
  79. // aStart and aEnd
  80. if( ( ( xDelta_21 == 0.0 ) && ( yDelta_32 == 0.0 ) )
  81. || ( ( yDelta_21 == 0.0 ) && ( xDelta_32 == 0.0 ) ) )
  82. {
  83. center.x = ( aStart.x + aEnd.x ) / 2.0;
  84. center.y = ( aStart.y + aEnd.y ) / 2.0;
  85. return center;
  86. }
  87. // Prevent div=0 errors
  88. /*if( xDelta_21 == 0.0 )
  89. xDelta_21 = std::numeric_limits<double>::epsilon();
  90. if( xDelta_32 == 0.0 )
  91. xDelta_32 = -std::numeric_limits<double>::epsilon();*/
  92. double aSlope = yDelta_21 / xDelta_21;
  93. double bSlope = yDelta_32 / xDelta_32;
  94. if( aSlope == bSlope )
  95. {
  96. if( aStart == aEnd )
  97. {
  98. // This is a special case for a 360 degrees arc. In this case, the center is halfway between
  99. // the midpoint and either newEnd point
  100. center.x = ( aStart.x + aMid.x ) / 2.0;
  101. center.y = ( aStart.y + aMid.y ) / 2.0;
  102. return center;
  103. }
  104. else
  105. {
  106. // If the points are colinear, the center is at infinity, so offset
  107. // the slope by a minimal amount
  108. // Warning: This will induce a small error in the center location
  109. /*aSlope += std::numeric_limits<double>::epsilon();
  110. bSlope -= std::numeric_limits<double>::epsilon();*/
  111. }
  112. }
  113. center.x = ( aSlope * bSlope * ( aStart.y - aEnd.y ) + bSlope * ( aStart.x + aMid.x )
  114. - aSlope * ( aMid.x + aEnd.x ) )
  115. / ( 2 * ( bSlope - aSlope ) );
  116. if( std::abs( aSlope ) > std::abs( bSlope ) )
  117. {
  118. center.y = ( ( ( aStart.x + aMid.x ) / 2.0 - center.x ) / aSlope
  119. + ( aStart.y + aMid.y ) / 2.0 );
  120. }
  121. else
  122. {
  123. center.y =
  124. ( ( ( aMid.x + aEnd.x ) / 2.0 - center.x ) / bSlope + ( aMid.y + aEnd.y ) / 2.0 );
  125. }
  126. return center;
  127. }
  128. struct TEST_CALC_ARC_CENTER_CASE
  129. {
  130. VECTOR2I istart;
  131. VECTOR2I imid;
  132. VECTOR2I iend;
  133. };
  134. /**
  135. * Declare the test suite
  136. */
  137. BOOST_AUTO_TEST_SUITE( KiMath )
  138. BOOST_AUTO_TEST_CASE( TestCalcArcCenter3Pts )
  139. {
  140. // Order: start, mid, end
  141. std::vector<TEST_CALC_ARC_CENTER_CASE> calc_center_cases = {
  142. //{ { 183000000, 89000000 }, { 185333332, 89000000 }, { 185333333, 91333333 } } // Fails currently
  143. };
  144. const double tolerance = 1.0;
  145. // Check random points (fails currently)
  146. /*for( int i = 0; i < 100; i++ )
  147. {
  148. TEST_CALC_ARC_CENTER_CASE cas;
  149. cas.istart.x = rand();
  150. cas.istart.y = rand();
  151. cas.imid.x = rand();
  152. cas.imid.y = rand();
  153. cas.iend.x = rand();
  154. cas.iend.y = rand();
  155. calc_center_cases.push_back( cas );
  156. }*/
  157. for( const TEST_CALC_ARC_CENTER_CASE& entry : calc_center_cases )
  158. {
  159. wxString msg;
  160. VECTOR2D start( entry.istart );
  161. VECTOR2D mid( entry.imid );
  162. VECTOR2D end( entry.iend );
  163. VECTOR2D calcCenter = CalcArcCenter( start, mid, end );
  164. double crs = ( calcCenter - start ).EuclideanNorm();
  165. double crm = ( calcCenter - mid ).EuclideanNorm();
  166. double cre = ( calcCenter - end ).EuclideanNorm();
  167. double cavg = ( crs + crm + cre ) / 3.0;
  168. if( std::abs( crs - cavg ) > tolerance || std::abs( crm - cavg ) > tolerance
  169. || std::abs( cre - cavg ) > tolerance )
  170. {
  171. msg << "CalcArcCenter failed.";
  172. msg << "\nstart: " << entry.istart.Format();
  173. msg << "\nmid: " << entry.imid.Format();
  174. msg << "\nend: " << entry.iend.Format();
  175. {
  176. msg << "\nCalculated center: " << wxString::Format( "%.15f", calcCenter.x ) << ", "
  177. << wxString::Format( "%.15f", calcCenter.y );
  178. msg << "\n Avg radius: " << wxString::Format( "%.15f", cavg );
  179. msg << "\nStart radius: " << wxString::Format( "%.15f", crs );
  180. msg << "\n Mid radius: " << wxString::Format( "%.15f", crm );
  181. msg << "\n End radius: " << wxString::Format( "%.15f", cre );
  182. msg << "\n";
  183. // Check mid/end points using the calculated center (like SHAPE_ARC)
  184. EDA_ANGLE angStart( start - calcCenter );
  185. EDA_ANGLE angMid( mid - calcCenter );
  186. EDA_ANGLE angEnd( end - calcCenter );
  187. EDA_ANGLE angCenter = angEnd - angStart;
  188. VECTOR2D newMid = start;
  189. VECTOR2D newEnd = start;
  190. RotatePoint( newMid, calcCenter, -angCenter / 2.0 );
  191. RotatePoint( newEnd, calcCenter, -angCenter );
  192. msg << "\nNew mid: " << wxString::Format( "%.15f", newMid.x ) << ", "
  193. << wxString::Format( "%.15f", newMid.y );
  194. msg << "\nNew end: " << wxString::Format( "%.15f", newEnd.x ) << ", "
  195. << wxString::Format( "%.15f", newEnd.y );
  196. msg << "\n";
  197. double endsDist = ( newEnd - end ).EuclideanNorm();
  198. msg << "\nNew end is off by " << wxString::Format( "%.15f", endsDist );
  199. msg << "\n";
  200. }
  201. {
  202. VECTOR2D ref0Center;
  203. Ref0CircleCenterFrom3Points( start, mid, end, &ref0Center );
  204. double r0_rs = ( ref0Center - start ).EuclideanNorm();
  205. double r0_rm = ( ref0Center - mid ).EuclideanNorm();
  206. double r0_rre = ( ref0Center - end ).EuclideanNorm();
  207. double r0_ravg = ( r0_rs + r0_rm + r0_rre ) / 3.0;
  208. msg << "\nReference0 center: " << wxString::Format( "%.15f", ref0Center.x ) << ", "
  209. << wxString::Format( "%.15f", ref0Center.y );
  210. msg << "\nRef0 Avg radius: " << wxString::Format( "%.15f", r0_ravg );
  211. msg << "\nRef0 Start radius: " << wxString::Format( "%.15f", r0_rs );
  212. msg << "\nRef0 Mid radius: " << wxString::Format( "%.15f", r0_rm );
  213. msg << "\nRef0 End radius: " << wxString::Format( "%.15f", r0_rre );
  214. msg << "\n";
  215. }
  216. {
  217. VECTOR2D ref1Center = Ref1CalcArcCenter( start, mid, end );
  218. double r1_rs = ( ref1Center - start ).EuclideanNorm();
  219. double r1_rm = ( ref1Center - mid ).EuclideanNorm();
  220. double r1_rre = ( ref1Center - end ).EuclideanNorm();
  221. double r1_ravg = ( r1_rs + r1_rm + r1_rre ) / 3.0;
  222. msg << "\nReference1 center: " << wxString::Format( "%.15f", ref1Center.x ) << ", "
  223. << wxString::Format( "%.15f", ref1Center.y );
  224. msg << "\nRef1 Avg radius: " << wxString::Format( "%.15f", r1_ravg );
  225. msg << "\nRef1 Start radius: " << wxString::Format( "%.15f", r1_rs );
  226. msg << "\nRef1 Mid radius: " << wxString::Format( "%.15f", r1_rm );
  227. msg << "\nRef1 End radius: " << wxString::Format( "%.15f", r1_rre );
  228. msg << "\n";
  229. }
  230. BOOST_CHECK_MESSAGE( false, msg );
  231. }
  232. }
  233. }
  234. BOOST_AUTO_TEST_CASE( TestInterceptsPositiveX )
  235. {
  236. BOOST_CHECK( !InterceptsPositiveX( 10.0, 20.0 ) );
  237. BOOST_CHECK( !InterceptsPositiveX( 10.0, 120.0 ) );
  238. BOOST_CHECK( !InterceptsPositiveX( 10.0, 220.0 ) );
  239. BOOST_CHECK( !InterceptsPositiveX( 10.0, 320.0 ) );
  240. BOOST_CHECK( InterceptsPositiveX( 20.0, 10.0 ) );
  241. BOOST_CHECK( InterceptsPositiveX( 345.0, 15.0 ) );
  242. }
  243. BOOST_AUTO_TEST_CASE( TestInterceptsNegativeX )
  244. {
  245. BOOST_CHECK( !InterceptsNegativeX( 10.0, 20.0 ) );
  246. BOOST_CHECK( !InterceptsNegativeX( 10.0, 120.0 ) );
  247. BOOST_CHECK( InterceptsNegativeX( 10.0, 220.0 ) );
  248. BOOST_CHECK( InterceptsNegativeX( 10.0, 320.0 ) );
  249. BOOST_CHECK( InterceptsNegativeX( 20.0, 10.0 ) );
  250. BOOST_CHECK( !InterceptsNegativeX( 345.0, 15.0 ) );
  251. BOOST_CHECK( InterceptsNegativeX( 145.0, 225.0 ) );
  252. }
  253. BOOST_AUTO_TEST_CASE( TestHash128 )
  254. {
  255. const HASH_128 zero{};
  256. for( size_t i = 0; i < 16; i++ )
  257. BOOST_CHECK( zero.Value8[i] == 0 );
  258. HASH_128* zero_h = new HASH_128;
  259. for( size_t i = 0; i < 16; i++ )
  260. BOOST_CHECK( zero_h->Value8[i] == 0 );
  261. delete zero_h;
  262. HASH_128 h;
  263. h.Value64[0] = 0x00CDEF0123456789ULL;
  264. h.Value64[1] = 0x56789ABCDEF01234ULL;
  265. BOOST_CHECK( h != zero );
  266. HASH_128 b = h;
  267. BOOST_CHECK( b != zero );
  268. BOOST_CHECK( b == h );
  269. BOOST_CHECK( h == b );
  270. BOOST_CHECK_EQUAL( h.ToString(), std::string( "00CDEF012345678956789ABCDEF01234" ) );
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Test MMH3_HASH against the original MurmurHash3_x64_128 implementation
  274. #include <mmh3_hash.h>
  275. void MurmurHash3_x64_128( const void* key, const int len, const uint32_t seed, void* out );
  276. BOOST_AUTO_TEST_CASE( TestMMH3Hash )
  277. {
  278. std::vector<std::vector<int32_t>> data;
  279. for( size_t i = 0; i < 10; i++ )
  280. {
  281. std::vector<int32_t>& vec = data.emplace_back();
  282. size_t vecSize = rand() % 128;
  283. for( size_t j = 0; j < vecSize; j++ )
  284. vec.emplace_back( rand() );
  285. }
  286. int64_t seed = 0; // Test 0 seed first
  287. for( const std::vector<int32_t>& vec : data )
  288. {
  289. MMH3_HASH mmh3( seed );
  290. for( const int32_t val : vec )
  291. mmh3.add( val );
  292. HASH_128 h128 = mmh3.digest();
  293. HASH_128 orig128;
  294. MurmurHash3_x64_128( (void*) vec.data(), vec.size() * sizeof( vec[0] ), seed,
  295. (void*) orig128.Value64 );
  296. BOOST_CHECK( h128 == orig128 );
  297. // Generate new random seed
  298. seed = rand();
  299. }
  300. }
  301. //-----------------------------------------------------------------------------
  302. // The original MurmurHash3_x64_128 implementation
  303. // Microsoft Visual Studio
  304. #if defined( _MSC_VER )
  305. #define FORCE_INLINE __forceinline
  306. #include <stdlib.h>
  307. #define ROTL64( x, y ) _rotl64( x, y )
  308. #define BIG_CONSTANT( x ) ( x )
  309. // Other compilers
  310. #else // defined(_MSC_VER)
  311. #define FORCE_INLINE inline __attribute__( ( always_inline ) )
  312. inline uint64_t rotl64( uint64_t x, int8_t r )
  313. {
  314. return ( x << r ) | ( x >> ( 64 - r ) );
  315. }
  316. #define ROTL64( x, y ) rotl64( x, y )
  317. #define BIG_CONSTANT( x ) ( x##LLU )
  318. #endif // !defined(_MSC_VER)
  319. FORCE_INLINE uint64_t getblock64 ( const uint64_t * p, int i )
  320. {
  321. return p[i];
  322. }
  323. FORCE_INLINE uint64_t fmix64 ( uint64_t k )
  324. {
  325. k ^= k >> 33;
  326. k *= BIG_CONSTANT(0xff51afd7ed558ccd);
  327. k ^= k >> 33;
  328. k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
  329. k ^= k >> 33;
  330. return k;
  331. }
  332. void MurmurHash3_x64_128 ( const void * key, const int len,
  333. const uint32_t seed, void * out )
  334. {
  335. const uint8_t * data = (const uint8_t*)key;
  336. const int nblocks = len / 16;
  337. uint64_t h1 = seed;
  338. uint64_t h2 = seed;
  339. const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  340. const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  341. //----------
  342. // body
  343. const uint64_t * blocks = (const uint64_t *)(data);
  344. for(int i = 0; i < nblocks; i++)
  345. {
  346. uint64_t k1 = getblock64(blocks,i*2+0);
  347. uint64_t k2 = getblock64(blocks,i*2+1);
  348. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  349. h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
  350. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  351. h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
  352. }
  353. //----------
  354. // tail
  355. const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  356. uint64_t k1 = 0;
  357. uint64_t k2 = 0;
  358. switch(len & 15)
  359. {
  360. case 15: k2 ^= ((uint64_t)tail[14]) << 48;
  361. case 14: k2 ^= ((uint64_t)tail[13]) << 40;
  362. case 13: k2 ^= ((uint64_t)tail[12]) << 32;
  363. case 12: k2 ^= ((uint64_t)tail[11]) << 24;
  364. case 11: k2 ^= ((uint64_t)tail[10]) << 16;
  365. case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
  366. case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
  367. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  368. case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
  369. case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
  370. case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
  371. case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
  372. case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
  373. case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
  374. case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
  375. case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
  376. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  377. };
  378. //----------
  379. // finalization
  380. h1 ^= len; h2 ^= len;
  381. h1 += h2;
  382. h2 += h1;
  383. h1 = fmix64(h1);
  384. h2 = fmix64(h2);
  385. h1 += h2;
  386. h2 += h1;
  387. ((uint64_t*)out)[0] = h1;
  388. ((uint64_t*)out)[1] = h2;
  389. }
  390. //-----------------------------------------------------------------------------
  391. #undef FORCE_INLINE
  392. #undef ROTL64
  393. #undef BIG_CONSTANT
  394. BOOST_AUTO_TEST_SUITE_END()