Mercurial > hg > orthanc-stone
comparison UnitTestsSources/ComputationalGeometryTests.cpp @ 1874:08f2476e8f5e
added classes OrientedIntegerLine2D and RectanglesIntegerProjection
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Jan 2022 18:58:37 +0100 |
parents | e0966648ebd0 |
children | b896f20d24ca |
comparison
equal
deleted
inserted
replaced
1873:e0966648ebd0 | 1874:08f2476e8f5e |
---|---|
20 **/ | 20 **/ |
21 | 21 |
22 | 22 |
23 #include <gtest/gtest.h> | 23 #include <gtest/gtest.h> |
24 | 24 |
25 #include "../OrthancStone/Sources/Toolbox/Internals/OrientedIntegerLine2D.h" | |
26 #include "../OrthancStone/Sources/Toolbox/Internals/RectanglesIntegerProjection.h" | |
25 #include "../OrthancStone/Sources/Toolbox/SegmentTree.h" | 27 #include "../OrthancStone/Sources/Toolbox/SegmentTree.h" |
26 | 28 |
27 #include <Logging.h> | 29 #include <Logging.h> |
28 #include <OrthancException.h> | 30 #include <OrthancException.h> |
29 | 31 |
324 ASSERT_TRUE(CheckCounter(root.GetRightChild(), 0)); | 326 ASSERT_TRUE(CheckCounter(root.GetRightChild(), 0)); |
325 | 327 |
326 root.VisitSegment(8, 9, minus); | 328 root.VisitSegment(8, 9, minus); |
327 ASSERT_TRUE(CheckCounter(root, 0)); | 329 ASSERT_TRUE(CheckCounter(root, 0)); |
328 } | 330 } |
331 | |
332 | |
333 TEST(UnionOfRectangles, RectanglesIntegerProjection) | |
334 { | |
335 std::list<OrthancStone::Extent2D> rectangles; | |
336 rectangles.push_back(OrthancStone::Extent2D(10, 20, 30, 40)); | |
337 | |
338 { | |
339 OrthancStone::Internals::RectanglesIntegerProjection h(rectangles, true); | |
340 ASSERT_EQ(2u, h.GetEndpointsCount()); | |
341 ASSERT_EQ(10, h.GetEndpointCoordinate(0)); | |
342 ASSERT_EQ(30, h.GetEndpointCoordinate(1)); | |
343 ASSERT_EQ(1u, h.GetProjectedRectanglesCount()); | |
344 ASSERT_EQ(0u, h.GetProjectedRectangleLow(0)); | |
345 ASSERT_EQ(1u, h.GetProjectedRectangleHigh(0)); | |
346 | |
347 ASSERT_THROW(h.GetEndpointCoordinate(2), Orthanc::OrthancException); | |
348 ASSERT_THROW(h.GetProjectedRectangleLow(1), Orthanc::OrthancException); | |
349 ASSERT_THROW(h.GetProjectedRectangleHigh(1), Orthanc::OrthancException); | |
350 } | |
351 | |
352 { | |
353 OrthancStone::Internals::RectanglesIntegerProjection h(rectangles, false); | |
354 ASSERT_EQ(2u, h.GetEndpointsCount()); | |
355 ASSERT_EQ(20, h.GetEndpointCoordinate(0)); | |
356 ASSERT_EQ(40, h.GetEndpointCoordinate(1)); | |
357 ASSERT_EQ(1u, h.GetProjectedRectanglesCount()); | |
358 ASSERT_EQ(0u, h.GetProjectedRectangleLow(0)); | |
359 ASSERT_EQ(1u, h.GetProjectedRectangleHigh(0)); | |
360 } | |
361 | |
362 rectangles.push_back(OrthancStone::Extent2D(20, 30, 40, 50)); | |
363 | |
364 { | |
365 OrthancStone::Internals::RectanglesIntegerProjection h(rectangles, true); | |
366 ASSERT_EQ(4u, h.GetEndpointsCount()); | |
367 ASSERT_EQ(10, h.GetEndpointCoordinate(0)); | |
368 ASSERT_EQ(20, h.GetEndpointCoordinate(1)); | |
369 ASSERT_EQ(30, h.GetEndpointCoordinate(2)); | |
370 ASSERT_EQ(40, h.GetEndpointCoordinate(3)); | |
371 ASSERT_EQ(2u, h.GetProjectedRectanglesCount()); | |
372 ASSERT_EQ(0u, h.GetProjectedRectangleLow(0)); | |
373 ASSERT_EQ(2u, h.GetProjectedRectangleHigh(0)); | |
374 ASSERT_EQ(1u, h.GetProjectedRectangleLow(1)); | |
375 ASSERT_EQ(3u, h.GetProjectedRectangleHigh(1)); | |
376 } | |
377 | |
378 { | |
379 OrthancStone::Internals::RectanglesIntegerProjection h(rectangles, false); | |
380 ASSERT_EQ(4u, h.GetEndpointsCount()); | |
381 ASSERT_EQ(20, h.GetEndpointCoordinate(0)); | |
382 ASSERT_EQ(30, h.GetEndpointCoordinate(1)); | |
383 ASSERT_EQ(40, h.GetEndpointCoordinate(2)); | |
384 ASSERT_EQ(50, h.GetEndpointCoordinate(3)); | |
385 ASSERT_EQ(2u, h.GetProjectedRectanglesCount()); | |
386 ASSERT_EQ(0u, h.GetProjectedRectangleLow(0)); | |
387 ASSERT_EQ(2u, h.GetProjectedRectangleHigh(0)); | |
388 ASSERT_EQ(1u, h.GetProjectedRectangleLow(1)); | |
389 ASSERT_EQ(3u, h.GetProjectedRectangleHigh(1)); | |
390 } | |
391 } | |
392 | |
393 | |
394 static void Convert(std::vector<size_t>& horizontal, | |
395 std::vector<size_t>& vertical, | |
396 const OrthancStone::Internals::OrientedIntegerLine2D::Chain& chain) | |
397 { | |
398 horizontal.clear(); | |
399 vertical.clear(); | |
400 | |
401 for (OrthancStone::Internals::OrientedIntegerLine2D::Chain::const_iterator | |
402 it = chain.begin(); it != chain.end(); ++it) | |
403 { | |
404 horizontal.push_back(it->first); | |
405 vertical.push_back(it->second); | |
406 } | |
407 } | |
408 | |
409 | |
410 TEST(UnionOfRectangles, ExtractChains) | |
411 { | |
412 std::vector<OrthancStone::Internals::OrientedIntegerLine2D> edges; | |
413 edges.push_back(OrthancStone::Internals::OrientedIntegerLine2D(0, 0, 10, 0)); | |
414 edges.push_back(OrthancStone::Internals::OrientedIntegerLine2D(10, 0, 10, 20)); | |
415 edges.push_back(OrthancStone::Internals::OrientedIntegerLine2D(10, 20, 0, 20)); | |
416 | |
417 std::list<OrthancStone::Internals::OrientedIntegerLine2D::Chain> chains; | |
418 OrthancStone::Internals::OrientedIntegerLine2D::ExtractChains(chains, edges); | |
419 | |
420 std::vector<size_t> h, v; | |
421 | |
422 ASSERT_EQ(1u, chains.size()); | |
423 | |
424 Convert(h, v, chains.front()); | |
425 ASSERT_EQ(4u, h.size()); | |
426 ASSERT_EQ(0u, h[0]); | |
427 ASSERT_EQ(10u, h[1]); | |
428 ASSERT_EQ(10u, h[2]); | |
429 ASSERT_EQ(0u, h[3]); | |
430 ASSERT_EQ(4u, v.size()); | |
431 ASSERT_EQ(0u, v[0]); | |
432 ASSERT_EQ(0u, v[1]); | |
433 ASSERT_EQ(20u, v[2]); | |
434 ASSERT_EQ(20u, v[3]); | |
435 | |
436 edges.push_back(OrthancStone::Internals::OrientedIntegerLine2D(5, 5, 10, 5)); | |
437 OrthancStone::Internals::OrientedIntegerLine2D::ExtractChains(chains, edges); | |
438 | |
439 ASSERT_EQ(2u, chains.size()); | |
440 | |
441 Convert(h, v, chains.front()); | |
442 ASSERT_EQ(4u, h.size()); | |
443 ASSERT_EQ(0u, h[0]); | |
444 ASSERT_EQ(10u, h[1]); | |
445 ASSERT_EQ(10u, h[2]); | |
446 ASSERT_EQ(0u, h[3]); | |
447 ASSERT_EQ(4u, v.size()); | |
448 ASSERT_EQ(0u, v[0]); | |
449 ASSERT_EQ(0u, v[1]); | |
450 ASSERT_EQ(20u, v[2]); | |
451 ASSERT_EQ(20u, v[3]); | |
452 | |
453 Convert(h, v, chains.back()); | |
454 ASSERT_EQ(2u, h.size()); | |
455 ASSERT_EQ(5u, h[0]); | |
456 ASSERT_EQ(10u, h[1]); | |
457 ASSERT_EQ(2u, v.size()); | |
458 ASSERT_EQ(5u, v[0]); | |
459 ASSERT_EQ(5u, v[1]); | |
460 | |
461 edges.push_back(OrthancStone::Internals::OrientedIntegerLine2D(0, 20, 5, 5)); | |
462 OrthancStone::Internals::OrientedIntegerLine2D::ExtractChains(chains, edges); | |
463 | |
464 ASSERT_EQ(1u, chains.size()); | |
465 | |
466 Convert(h, v, chains.front()); | |
467 ASSERT_EQ(6u, h.size()); | |
468 ASSERT_EQ(0u, h[0]); | |
469 ASSERT_EQ(10u, h[1]); | |
470 ASSERT_EQ(10u, h[2]); | |
471 ASSERT_EQ(0u, h[3]); | |
472 ASSERT_EQ(5u, h[4]); | |
473 ASSERT_EQ(10u, h[5]); | |
474 ASSERT_EQ(6u, v.size()); | |
475 ASSERT_EQ(0u, v[0]); | |
476 ASSERT_EQ(0u, v[1]); | |
477 ASSERT_EQ(20u, v[2]); | |
478 ASSERT_EQ(20u, v[3]); | |
479 ASSERT_EQ(5u, v[4]); | |
480 ASSERT_EQ(5u, v[5]); | |
481 } |