test_vlib_box.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. TEST: box.c
  3. AUTHOR(S): Vaclav Petras
  4. PURPOSE: Test functions related to bounding box
  5. COPYRIGHT: (C) 2015 Vaclav Petras, and by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. """
  10. import ctypes
  11. import grass.lib.vector as libvect
  12. from grass.gunittest.case import TestCase
  13. from grass.gunittest.main import test
  14. class TestPointInBoundingBox(TestCase):
  15. """Test functions related to point in bounding box"""
  16. def setUp(self):
  17. """Create bbox object"""
  18. self.c_bbox = ctypes.pointer(libvect.bound_box())
  19. # x range
  20. self.c_bbox.contents.E = 220
  21. self.c_bbox.contents.W = 215
  22. # y range
  23. self.c_bbox.contents.N = 135
  24. self.c_bbox.contents.S = 125
  25. # z range
  26. self.c_bbox.contents.T = 340
  27. self.c_bbox.contents.B = 330
  28. def test_bbox_3d_in(self):
  29. """Check Vect_point_in_box() function with 3D points inside bbox"""
  30. self.check_point_in_3d(217, 130, 335)
  31. self.check_point_in_3d(219.999999, 125.000001, 339.999999)
  32. def test_bbox_3d_out(self):
  33. """Check Vect_point_in_box() function with 3D points outside bbox"""
  34. self.check_point_out_3d(100, 100, 100)
  35. self.check_point_out_3d(500, 593, 900)
  36. self.check_point_out_3d(-220, 130, 335)
  37. self.check_point_out_3d(220, -130, 335)
  38. self.check_point_out_3d(220, 130, -335)
  39. def check_point_in_3d(self, x, y, z):
  40. """Wraps Vect_point_in_box() with assert and a message"""
  41. self.assertTrue(
  42. libvect.Vect_point_in_box(x, y, z, self.c_bbox),
  43. msg="Point should be inside the bbox",
  44. )
  45. def check_point_out_3d(self, x, y, z):
  46. """Wraps Vect_point_in_box() with assert and a message"""
  47. self.assertFalse(
  48. libvect.Vect_point_in_box(x, y, z, self.c_bbox),
  49. msg="Point should be outside the bbox",
  50. )
  51. def test_bbox_2d_in(self):
  52. """Check Vect_point_in_box_2d() function with 2D points inside bbox"""
  53. self.check_point_in_2d(217, 130)
  54. self.check_point_in_2d(219.999999, 125.000001)
  55. def test_bbox_2d_out(self):
  56. """Check Vect_point_in_box_2d() function with 2D points outside bbox"""
  57. self.check_point_out_2d(100, 100)
  58. self.check_point_out_2d(500, 593)
  59. self.check_point_out_2d(-220, 130)
  60. self.check_point_out_2d(220, -130)
  61. self.check_point_out_2d(-220, -130)
  62. def check_point_in_2d(self, x, y):
  63. """Wraps Vect_point_in_box_2d() with assert, message and bbox"""
  64. self.assertTrue(
  65. libvect.Vect_point_in_box_2d(x, y, self.c_bbox),
  66. msg="Point should be inside the bbox",
  67. )
  68. def check_point_out_2d(self, x, y):
  69. """Wraps Vect_point_in_box_2d() with assert, message and bbox"""
  70. self.assertFalse(
  71. libvect.Vect_point_in_box_2d(x, y, self.c_bbox),
  72. msg="Point should be outside the bbox",
  73. )
  74. if __name__ == "__main__":
  75. test()