1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"
import unittest
import math
from argaze.AreaOfInterest import AOIFeatures, AOI2DScene
from argaze import GazeFeatures
import numpy
class TestAOI2DSceneClass(unittest.TestCase):
"""Test AOI2DScene class."""
def test_new(self):
"""Test AOI2DScene creation."""
# Check empty AOI2DScene creation
aoi_2d_scene = AOI2DScene.AOI2DScene()
self.assertEqual(aoi_2d_scene.dimension, 2)
self.assertEqual(len(aoi_2d_scene.items()), 0)
# Check AOI2DScene creation
aoi_2D_A = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
aoi_2D_B = AOIFeatures.AreaOfInterest([[1, 1], [1, 2], [2, 2], [2, 1]])
aoi_2d_scene = AOI2DScene.AOI2DScene({"A": aoi_2D_A, "B": aoi_2D_B})
self.assertEqual(aoi_2d_scene.dimension, 2)
def test_raycast(self):
"""Test AOI2DScene raycast method."""
aoi_2D_A = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
aoi_2D_B = AOIFeatures.AreaOfInterest([[1, 1], [1, 2], [2, 2], [2, 1]])
aoi_2d_scene = AOI2DScene.AOI2DScene({"A": aoi_2D_A, "B": aoi_2D_B})
gaze_position_A = GazeFeatures.GazePosition((0.5, 0.5))
gaze_position_B = GazeFeatures.GazePosition((1.5, 1.5))
gaze_position_C = GazeFeatures.GazePosition((0.5, 1.5))
# Check raycast results for gaze postion A
for name, aoi, matching in aoi_2d_scene.raycast(gaze_position_A.value):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertEqual(matching, True)
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertEqual(matching, False)
# Check raycast results for gaze postion B
for name, aoi, matching in aoi_2d_scene.raycast(gaze_position_B.value):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertEqual(matching, False)
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertEqual(matching, True)
# Check raycast results for gaze postion C
for name, aoi, matching in aoi_2d_scene.raycast(gaze_position_C.value):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertEqual(matching, False)
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertEqual(matching, False)
def test_circlecast(self):
"""Test AOI2DScene circlecast method."""
aoi_2D_A = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
aoi_2D_B = AOIFeatures.AreaOfInterest([[1, 1], [1, 2], [2, 2], [2, 1]])
aoi_2d_scene = AOI2DScene.AOI2DScene({"A": aoi_2D_A, "B": aoi_2D_B})
gaze_position_A = GazeFeatures.GazePosition((0.5, 0.5), precision=0.5)
gaze_position_B = GazeFeatures.GazePosition((1.5, 1.5), precision=0.5)
gaze_position_C = GazeFeatures.GazePosition((1., 1.), precision=1)
gaze_position_D = GazeFeatures.GazePosition((0.5, 1.5), precision=0.25)
# Check circlecast results for gaze postion A
for name, aoi, matched_region, aoi_ratio, circle_ratio in aoi_2d_scene.circlecast(gaze_position_A.value, gaze_position_A.precision):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertTrue(math.isclose(aoi_ratio, math.pi / 4, abs_tol=1e-2))
self.assertTrue(math.isclose(circle_ratio, 1., abs_tol=1e-3))
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertEqual(aoi_ratio, 0.)
self.assertEqual(circle_ratio, 0.)
# Check circlecast results for gaze postion B
for name, aoi, matched_region, aoi_ratio, circle_ratio in aoi_2d_scene.circlecast(gaze_position_B.value, gaze_position_B.precision):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertEqual(aoi_ratio, 0.)
self.assertEqual(circle_ratio, 0.)
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertTrue(math.isclose(aoi_ratio, math.pi / 4, abs_tol=1e-2))
self.assertTrue(math.isclose(circle_ratio, 1., abs_tol=1e-3))
# Check circlecast results for gaze postion C
for name, aoi, matched_region, aoi_ratio, circle_ratio in aoi_2d_scene.circlecast(gaze_position_C.value, gaze_position_C.precision):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertTrue(math.isclose(aoi_ratio, math.pi / 4, abs_tol=1e-2))
self.assertTrue(math.isclose(circle_ratio, 1 / 4, abs_tol=1e-3))
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertTrue(math.isclose(aoi_ratio, math.pi / 4, abs_tol=1e-2))
self.assertTrue(math.isclose(circle_ratio, 1 / 4, abs_tol=1e-3))
# Check circlecast results for gaze postion D
for name, aoi, matched_region, aoi_ratio, circle_ratio in aoi_2d_scene.circlecast(gaze_position_D.value, gaze_position_D.precision):
if name == "A":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_A))
self.assertEqual(aoi_ratio, 0.)
self.assertEqual(circle_ratio, 0.)
elif name == "B":
self.assertIsNone(numpy.testing.assert_array_equal(aoi, aoi_2D_B))
self.assertEqual(aoi_ratio, 0.)
self.assertEqual(circle_ratio, 0.)
def test_copy(self):
"""Test AOI2DScene copy method."""
aoi_2D_A = AOIFeatures.AreaOfInterest([[0, 0], [0, 1], [1, 1], [1, 0]])
aoi_2D_B = AOIFeatures.AreaOfInterest([[1, 1], [1, 2], [2, 2], [2, 1]])
aoi_2d_scene = AOI2DScene.AOI2DScene({"A": aoi_2D_A, "B": aoi_2D_B})
# Check full copy
aoi_2d_scene_copy = aoi_2d_scene.copy()
self.assertEqual(aoi_2d_scene_copy.dimension, 2)
self.assertEqual(len(aoi_2d_scene_copy.items()), 2)
self.assertEqual(list(aoi_2d_scene_copy.keys()), ["A", "B"])
# Check cpy with exclusion
aoi_2d_scene_copy = aoi_2d_scene.copy(exclude=["B"])
self.assertEqual(aoi_2d_scene_copy.dimension, 2)
self.assertEqual(len(aoi_2d_scene_copy.items()), 1)
self.assertEqual(list(aoi_2d_scene_copy.keys()), ["A"])
if __name__ == '__main__':
unittest.main()
|