forked from UXARRAY/uxarray
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_intersections.py
More file actions
255 lines (205 loc) · 10.2 KB
/
test_intersections.py
File metadata and controls
255 lines (205 loc) · 10.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import numpy as np
import pytest
import uxarray as ux
from uxarray.constants import ERROR_TOLERANCE
from uxarray.grid.arcs import extreme_gca_z
from uxarray.grid.coordinates import _lonlat_rad_to_xyz, _xyz_to_lonlat_rad,_xyz_to_lonlat_rad_scalar
from uxarray.grid.intersections import gca_gca_intersection, gca_const_lat_intersection, _gca_gca_intersection_cartesian, get_number_of_intersections
def test_get_GCA_GCA_intersections_antimeridian():
GCA1 = _lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(89.99))
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(89.99)),
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(10.0))
])
GCR2_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(70.0), 0.0),
_lonlat_rad_to_xyz(np.deg2rad(179.0), 0.0)
])
res_cart = _gca_gca_intersection_cartesian(GCR1_cart, GCR2_cart)
assert len(res_cart) == 0
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(89.0)),
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(-10.0))
])
GCR2_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(70.0), 0.0),
_lonlat_rad_to_xyz(np.deg2rad(175.0), 0.0)
])
res_cart = _gca_gca_intersection_cartesian(GCR1_cart, GCR2_cart)
res_cart = res_cart[0]
assert np.allclose(np.linalg.norm(res_cart, axis=0), 1.0, atol=ERROR_TOLERANCE)
res_lonlat_rad = _xyz_to_lonlat_rad(res_cart[0], res_cart[1], res_cart[2])
assert np.array_equal(res_lonlat_rad, np.array([np.deg2rad(170.0), np.deg2rad(0.0)]))
def test_get_GCA_GCA_intersections_parallel():
GCR1_cart = np.array([
_lonlat_rad_to_xyz(0.3 * np.pi, 0.0),
_lonlat_rad_to_xyz(0.5 * np.pi, 0.0)
])
GCR2_cart = np.array([
_lonlat_rad_to_xyz(0.5 * np.pi, 0.0),
_lonlat_rad_to_xyz(-0.5 * np.pi - 0.01, 0.0)
])
res_cart = _gca_gca_intersection_cartesian(GCR1_cart, GCR2_cart)
res_cart = res_cart[0]
expected_res = np.array(_lonlat_rad_to_xyz(0.5 * np.pi, 0.0))
assert np.isclose(np.linalg.norm(res_cart - expected_res), 0.0, atol=ERROR_TOLERANCE)
def test_get_GCA_GCA_intersections_perpendicular():
# Test the case where the two GCAs are perpendicular to each other
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0),
np.deg2rad(0.0)),
_lonlat_rad_to_xyz(np.deg2rad(170.0),
np.deg2rad(10.0))
])
GCR2_cart = np.array([
_lonlat_rad_to_xyz(*[0.5 * np.pi - 0.01, 0.0]),
_lonlat_rad_to_xyz(*[-0.5 * np.pi + 0.01, 0.0])
])
res_cart = _gca_gca_intersection_cartesian(GCR1_cart, GCR2_cart)
# rest_cart should be empty since these two GCAs are not intersecting
assert(len(res_cart) == 0)
# def test_GCA_GCA_single_edge_to_pole(self):
# # GCA_a - Face Center connected to South Pole
# # Point A - South Pole
# ref_point_lonlat = np.deg2rad(np.array([0.0, -90.0]))
# ref_point_xyz = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat))
# # Point B - Face Center
# face_lonlat = np.deg2rad(np.array([-175, 26.5]))
# face_xyz = np.array(_lonlat_rad_to_xyz(*face_lonlat))
# gca_a_xyz = np.array([face_xyz, ref_point_xyz])
#
# # GCA_b - Single Face Edge
# # Point A - First Edge Point
# edge_a_lonlat = np.deg2rad(np.array((-175, -24.5)))
# edge_b_lonlat = np.deg2rad(np.array((-173, 28.7)))
#
# # Point B - Second Edge Point
# edge_a_xyz = np.array(_lonlat_rad_to_xyz(*edge_a_lonlat))
# edge_b_xyz = np.array(_lonlat_rad_to_xyz(*edge_b_lonlat))
# gca_b_xyz = np.array([edge_a_xyz, edge_b_xyz])
#
# # The edge should intersect
# self.assertTrue(len(gca_gca_intersection(gca_a_xyz, gca_b_xyz)))
def test_GCA_GCA_south_pole():
# GCA_a - Face Center connected to South Pole
# Point A - South Pole
ref_point_lonlat = np.deg2rad(np.array([0.0, -90.0]))
ref_point_xyz = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat))
# Point B - Face Center
face_lonlat = np.deg2rad(np.array([0.0, 0.0]))
face_xyz = np.array(_lonlat_rad_to_xyz(*face_lonlat))
gca_a_xyz = np.array([face_xyz, ref_point_xyz])
# GCA_b - Single Face Edge
# Point A - First Edge Point
edge_a_lonlat = np.deg2rad(np.array((-45, -1.0)))
edge_b_lonlat = np.deg2rad(np.array((45, -1.0)))
# Point B - Second Edge Point
edge_a_xyz = np.array(_lonlat_rad_to_xyz(*edge_a_lonlat))
edge_b_xyz = np.array(_lonlat_rad_to_xyz(*edge_b_lonlat))
gca_b_xyz = np.array([edge_a_xyz, edge_b_xyz])
# The edge should intersect
assert(len(gca_gca_intersection(gca_a_xyz, gca_b_xyz)))
def test_GCA_GCA_north_pole():
# GCA_a - Face Center connected to South Pole
ref_point_lonlat = np.deg2rad(np.array([0.0, 90.0]))
ref_point_xyz = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat))
face_lonlat = np.deg2rad(np.array([0.0, 0.0]))
face_xyz = np.array(_lonlat_rad_to_xyz(*face_lonlat))
gca_a_xyz = np.array([face_xyz, ref_point_xyz])
# GCA_b - Single Face Edge
edge_a_lonlat = np.deg2rad(np.array((-45, 1.0)))
edge_b_lonlat = np.deg2rad(np.array((45, 1.0)))
edge_a_xyz = np.array(_lonlat_rad_to_xyz(*edge_a_lonlat))
edge_b_xyz = np.array(_lonlat_rad_to_xyz(*edge_b_lonlat))
gca_b_xyz = np.array([edge_a_xyz, edge_b_xyz])
# The edge should intersect
assert(len(gca_gca_intersection(gca_a_xyz, gca_b_xyz)))
def test_GCA_GCA_north_pole_angled():
# GCA_a
ref_point_lonlat = np.deg2rad(np.array([0.0, 90.0]))
ref_point_xyz = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat))
face_lonlat = np.deg2rad(np.array([-45.0, 45.0]))
face_xyz = np.array(_lonlat_rad_to_xyz(*face_lonlat))
gca_a_xyz = np.array([face_xyz, ref_point_xyz])
# GCA_b
edge_a_lonlat = np.deg2rad(np.array((-45.0, 50.0)))
edge_b_lonlat = np.deg2rad(np.array((-40.0, 45.0)))
# Point B - Second Edge Point
edge_a_xyz = np.array(_lonlat_rad_to_xyz(*edge_a_lonlat))
edge_b_xyz = np.array(_lonlat_rad_to_xyz(*edge_b_lonlat))
gca_b_xyz = np.array([edge_a_xyz, edge_b_xyz])
# The edge should intersect
assert(len(gca_gca_intersection(gca_a_xyz, gca_b_xyz)))
def test_GCA_edge_intersection_count():
from uxarray.grid.utils import _get_cartesian_faces_edge_nodes
# Generate a normal face that is not crossing the antimeridian or the poles
vertices_lonlat = [[29.5, 11.0], [29.5, 10.0], [30.5, 10.0], [30.5, 11.0]]
vertices_lonlat = np.array(vertices_lonlat)
grid = ux.Grid.from_face_vertices(vertices_lonlat, latlon=True)
face_edge_nodes_cartesian = _get_cartesian_faces_edge_nodes(grid.face_node_connectivity.values, grid.n_face,
grid.n_max_face_edges, grid.node_x.values,
grid.node_y.values, grid.node_z.values)
face_center_xyz = np.array([grid.face_x.values[0], grid.face_y.values[0], grid.face_z.values[0]], dtype=np.float64)
north_pole_xyz = np.array([0.0, 0.0, 1.0], dtype=np.float64)
south_pole_xyz = np.array([0.0, 0.0, -1.0], dtype=np.float64)
gca_face_center_north_pole = np.array([face_center_xyz, north_pole_xyz], dtype=np.float64)
gca_face_center_south_pole = np.array([face_center_xyz, south_pole_xyz], dtype=np.float64)
intersect_north_pole_count = 0
intersect_south_pole_count = 0
for edge in face_edge_nodes_cartesian[0]:
res1 = gca_gca_intersection(edge, gca_face_center_north_pole)
res2 = gca_gca_intersection(edge, gca_face_center_south_pole)
if len(res1):
intersect_north_pole_count += 1
if len(res2):
intersect_south_pole_count += 1
print(intersect_north_pole_count, intersect_south_pole_count)
assert(intersect_north_pole_count == 1)
assert(intersect_south_pole_count == 1)
def test_GCA_GCA_single_edge_to_pole():
# GCA_a - Face Center connected to South Pole
# Point A - South Pole
ref_point_lonlat_exact = np.deg2rad(np.array([0.0, -90.0]))
ref_point_lonlat_close = np.deg2rad(np.array([0.0, -89.99999]))
ref_point_xyz_exact = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat_exact))
ref_point_xyz_close = np.array(_lonlat_rad_to_xyz(*ref_point_lonlat_close))
# Point B - Face Center
face_lonlat = np.deg2rad(np.array([-175.0, 26.5]))
face_xyz = np.array(_lonlat_rad_to_xyz(*face_lonlat))
gca_a_xyz_close = np.array([face_xyz, ref_point_xyz_close])
gca_a_xyz_exact = np.array([face_xyz, ref_point_xyz_exact])
# GCA_b - Single Face Edge
# Point A - First Edge Point
edge_a_lonlat = np.deg2rad(np.array((-175.0, -24.5)))
edge_b_lonlat = np.deg2rad(np.array((-173.0, 28.7)))
# Point B - Second Edge Point
edge_a_xyz = np.array(_lonlat_rad_to_xyz(*edge_a_lonlat))
edge_b_xyz = np.array(_lonlat_rad_to_xyz(*edge_b_lonlat))
gca_b_xyz = np.array([edge_a_xyz, edge_b_xyz])
# The edge should intersect
assert(len(gca_gca_intersection(gca_a_xyz_close, gca_b_xyz)))
assert(len(gca_gca_intersection(gca_a_xyz_exact, gca_b_xyz)))
def test_GCA_constLat_intersections_antimeridian():
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(89.99)),
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(10.0))
])
res = gca_const_lat_intersection(GCR1_cart, np.sin(np.deg2rad(60.0)))
res_lonlat_rad = _xyz_to_lonlat_rad(*(res[0].tolist()))
assert np.allclose(res_lonlat_rad, np.array([np.deg2rad(170.0), np.deg2rad(60.0)]))
def test_GCA_constLat_intersections_empty():
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(89.99)),
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(10.0))
])
res = gca_const_lat_intersection(GCR1_cart, np.sin(np.deg2rad(-10.0)))
assert get_number_of_intersections(res) == 0
def test_GCA_constLat_intersections_two_pts():
GCR1_cart = np.array([
_lonlat_rad_to_xyz(np.deg2rad(10.0), np.deg2rad(10)),
_lonlat_rad_to_xyz(np.deg2rad(170.0), np.deg2rad(10.0))
])
max_lat = extreme_gca_z(GCR1_cart, 'max')
query_lat = (np.deg2rad(10.0) + max_lat) / 2.0
res = gca_const_lat_intersection(GCR1_cart, np.sin(query_lat))
assert res.shape[0] == 2