xmsgeom  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
TrTin_pyt.py
1 import os
2 import unittest
3 import numpy as np
4 import xmsgeom_py.triangulate as xt
5 
6 
7 class TestTrTin(unittest.TestCase):
8  """Test Observer Class."""
9 
10  def setUp(self):
11  """Set up for each test case."""
12  self.trTin = xt.TrTin()
13  self.pts = ((0, 0, 0), (10, 0, 0), (20, 0, 0), (5, 10, 0), (15, 10, 0))
14  self.pts_np = np.array([(0, 0, 0), (10, 0, 0), (20, 0, 0), (5, 10, 0), (15, 10, 0)])
15  self.tris = (3, 0, 1, 1, 2, 4, 1, 4, 3)
16  self.tris_np = np.array([3, 0, 1, 1, 2, 4, 1, 4, 3])
17  self.tris_adj = ((0,), (0, 1, 2), (1,), (0, 2), (1, 2))
18  self.tris_adj_np = np.array([[0], [0, 1, 2], [1], [0, 2], [1, 2]])
19 
20  def test_to_string(self):
21  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 0 0 0 0 0 0 0 0\n"
22  self.assertEqual(base, str(self.trTin))
23 
24  def test_set_pts(self):
25  trtin = xt.TrTin()
26  pts = self.pts
27  trtin.set_points(pts)
28 
29  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 5 0 0 0 0.00000000000000000e+00 " \
30  "0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000000e+01 " \
31  "0.00000000000000000e+00 0.00000000000000000e+00 2.00000000000000000e+01 " \
32  "0.00000000000000000e+00 0.00000000000000000e+00 5.00000000000000000e+00 " \
33  "1.00000000000000000e+01 0.00000000000000000e+00 1.50000000000000000e+01 " \
34  "1.00000000000000000e+01 0.00000000000000000e+00 0 0 0 0 0 0\n"
35  self.assertEqual(base, str(trtin))
36 
37  def test_set_pts_numpy(self):
38  trtin = xt.TrTin()
39  pts = self.pts_np
40  trtin.set_points(pts)
41 
42  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 5 0 0 0 0.00000000000000000e+00 " \
43  "0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000000e+01 " \
44  "0.00000000000000000e+00 0.00000000000000000e+00 2.00000000000000000e+01 " \
45  "0.00000000000000000e+00 0.00000000000000000e+00 5.00000000000000000e+00 " \
46  "1.00000000000000000e+01 0.00000000000000000e+00 1.50000000000000000e+01 " \
47  "1.00000000000000000e+01 0.00000000000000000e+00 0 0 0 0 0 0\n"
48  self.assertEqual(base, str(trtin))
49 
50  def test_set_tris(self):
51  trtin = xt.TrTin()
52  tris = self.tris
53  trtin.set_triangles(tris)
54 
55  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 0 0 9 0 3 0 1 1 2 4 1 4 3 0 0 0 0\n"
56  self.assertEqual(base, str(trtin))
57 
58  def test_set_tris_numpy(self):
59  trtin = xt.TrTin()
60  tris = self.tris_np
61  trtin.set_triangles(tris)
62 
63  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 0 0 9 0 3 0 1 1 2 4 1 4 3 0 0 0 0\n"
64  self.assertEqual(base, str(trtin))
65 
66  def test_set_tris_adj(self):
67  trtin = xt.TrTin()
68  tris_adj = self.tris_adj
69  trtin.set_triangles_adjacent_to_points(tris_adj)
70 
71  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 0 0 0 0 0 0 5 0 1 0 0 3 0 0 1 " \
72  "2 1 0 1 2 0 0 2 2 0 1 2\n"
73  self.assertEqual(base, str(trtin))
74 
75  def test_set_tris_adj_numpy(self):
76  trtin = xt.TrTin()
77  tris_adj = self.tris_adj_np
78  trtin.set_triangles_adjacent_to_points(tris_adj)
79 
80  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 0 0 0 0 0 0 5 0 1 0 0 3 0 0 1 " \
81  "2 1 0 1 2 0 0 2 2 0 1 2\n"
82  self.assertEqual(base, str(trtin))
83 
84  def test_set_geometry(self):
85  trtin = xt.TrTin()
86  pts = self.pts
87  tris = self.tris
88  tris_adj = self.tris_adj
89  trtin.set_geometry(pts, tris, tris_adj)
90 
91  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 5 0 0 0 0.00000000000000000e+00 " \
92  "0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000000e+01 " \
93  "0.00000000000000000e+00 0.00000000000000000e+00 2.00000000000000000e+01 " \
94  "0.00000000000000000e+00 0.00000000000000000e+00 5.00000000000000000e+00 " \
95  "1.00000000000000000e+01 0.00000000000000000e+00 1.50000000000000000e+01 " \
96  "1.00000000000000000e+01 0.00000000000000000e+00 9 0 3 0 1 1 2 4 1 4 3 0 0 " \
97  "5 0 1 0 0 3 0 0 1 2 1 0 1 2 0 0 2 2 0 1 2\n"
98  self.assertEqual(base, str(trtin))
99 
100  def test_set_geometry_numpy(self):
101  trtin = xt.TrTin()
102  pts = self.pts_np
103  tris = self.tris_np
104  tris_adj = self.tris_adj_np
105  trtin.set_geometry(pts, tris, tris_adj)
106 
107  base = "22 serialization::archive 15 1 0\n0 0 0 0 0 5 0 0 0 0.00000000000000000e+00 " \
108  "0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000000e+01 " \
109  "0.00000000000000000e+00 0.00000000000000000e+00 2.00000000000000000e+01 " \
110  "0.00000000000000000e+00 0.00000000000000000e+00 5.00000000000000000e+00 " \
111  "1.00000000000000000e+01 0.00000000000000000e+00 1.50000000000000000e+01 " \
112  "1.00000000000000000e+01 0.00000000000000000e+00 9 0 3 0 1 1 2 4 1 4 3 0 0 " \
113  "5 0 1 0 0 3 0 0 1 2 1 0 1 2 0 0 2 2 0 1 2\n"
114  self.assertEqual(base, str(trtin))
115 
116  def test_get_pts(self):
117  trtin = xt.TrTin()
118  pts = self.pts
119  trtin.set_points(pts)
120  np.testing.assert_array_equal(pts, trtin.pts)
121 
122  def test_get_tris(self):
123  trtin = xt.TrTin()
124  tris = self.tris
125  trtin.set_triangles(tris)
126  np.testing.assert_array_equal(tris, trtin.tris)
127 
128  def test_get_tris_adj(self):
129  trtin = xt.TrTin()
130  tris_adj = self.tris_adj
131  trtin.set_triangles_adjacent_to_points(tris_adj)
132  self.assertEqual(tris_adj, trtin.tris_adj)
133 
134  def test_num_points(self):
135  trtin = xt.TrTin()
136  pts = self.pts_np
137  trtin.set_points(pts)
138  self.assertEqual(5, trtin.num_points)
139 
140  def test_num_triangles(self):
141  trtin = xt.TrTin()
142  tris = self.tris_np
143  trtin.set_triangles(tris)
144  self.assertEqual(3, trtin.num_triangles)
145 
146  def test_triangle_from_edge(self):
147  trtin = xt.TrTin()
148  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
149  rv, tri, idx1, idx2 = trtin.triangle_from_edge(1, 3)
150  self.assertEqual(rv, True)
151  self.assertEqual(tri, 0)
152  self.assertEqual(idx1, 2)
153  self.assertEqual(idx2, 0)
154  rv, tri, idx1, idx2 = trtin.triangle_from_edge(3, 1)
155  self.assertEqual(rv, True)
156  self.assertEqual(tri, 2)
157  self.assertEqual(idx1, 2)
158  self.assertEqual(idx2, 0)
159  rv, tri, idx1, idx2 = trtin.triangle_from_edge(1, 0)
160  self.assertEqual(rv, False)
161 
162  def test_triangle_adjacent_to_edge(self):
163  trtin = xt.TrTin()
164  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
165  tri = trtin.triangle_adjacent_to_edge(1, 3)
166  self.assertEqual(2, tri)
167  tri = trtin.triangle_adjacent_to_edge(3, 1)
168  self.assertEqual(0, tri)
169  tri = trtin.triangle_adjacent_to_edge(1, 0)
170  self.assertEqual(0, tri)
171 
172  def test_local_index(self):
173  trtin = xt.TrTin()
174  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
175  self.assertEqual(1, trtin.local_index(0, 0))
176  self.assertEqual(2, trtin.local_index(0, 1))
177  self.assertEqual(0, trtin.local_index(0, 3))
178  self.assertEqual(-1, trtin.local_index(0, 4))
179 
180  def test_global_index(self):
181  trtin = xt.TrTin()
182  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
183  self.assertEqual(3, trtin.global_index(0, 0))
184  self.assertEqual(0, trtin.global_index(0, 1))
185  self.assertEqual(1, trtin.global_index(0, 2))
186  self.assertEqual(-1, trtin.global_index(3, 2))
187 
188  def test_common_edge_index(self):
189  trtin = xt.TrTin()
190  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
191  self.assertEqual(2, trtin.common_edge_index(0, 2))
192  self.assertEqual(2, trtin.common_edge_index(2, 0))
193  self.assertEqual(2, trtin.common_edge_index(1, 2))
194  self.assertEqual(0, trtin.common_edge_index(2, 1))
195  self.assertEqual(-1, trtin.common_edge_index(0, 1))
196 
197  def test_adjacent_triangle(self):
198  trtin = xt.TrTin()
199  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
200  self.assertEqual(-1, trtin.adjacent_triangle(0, 0))
201  self.assertEqual(-1, trtin.adjacent_triangle(0, 1))
202  self.assertEqual(2, trtin.adjacent_triangle(0, 2))
203  self.assertEqual(-1, trtin.adjacent_triangle(1, 0))
204  self.assertEqual(-1, trtin.adjacent_triangle(1, 1))
205  self.assertEqual(2, trtin.adjacent_triangle(1, 2))
206  self.assertEqual(1, trtin.adjacent_triangle(2, 0))
207  self.assertEqual(-1, trtin.adjacent_triangle(2, 1))
208  self.assertEqual(0, trtin.adjacent_triangle(2, 2))
209 
210  def test_triangle_centroid(self):
211  trtin = xt.TrTin()
212  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
213  x0, y0, z0 = trtin.triangle_centroid(0)
214  self.assertEqual(5, x0)
215  self.assertAlmostEqual(3.333333333, y0, places=8)
216  self.assertEqual(0, z0)
217  x1, y1, z1 = trtin.triangle_centroid(1)
218  self.assertEqual(15, x1)
219  self.assertAlmostEqual(3.333333333, y1, places=8)
220  self.assertEqual(0, z1)
221  x2, y2, z2 = trtin.triangle_centroid(2)
222  self.assertEqual(10, x2)
223  self.assertAlmostEqual(6.666666666, y2, places=8)
224  self.assertEqual(0, z2)
225 
226  def test_triangle_area(self):
227  trtin = xt.TrTin()
228  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
229  self.assertAlmostEqual(50.00000, trtin.triangle_area(0), places=4)
230  self.assertAlmostEqual(50.00000, trtin.triangle_area(1), places=4)
231  self.assertAlmostEqual(50.00000, trtin.triangle_area(2), places=4)
232 
233  def test_next_boundry_point(self):
234  trtin = xt.TrTin()
235  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
236  self.assertEqual(3, trtin.next_boundary_point(0))
237  self.assertEqual(4, trtin.next_boundary_point(3))
238  self.assertEqual(2, trtin.next_boundary_point(4))
239  self.assertEqual(1, trtin.next_boundary_point(2))
240  self.assertEqual(0, trtin.next_boundary_point(1))
241 
242  def test_previous_boundary_point(self):
243  trtin = xt.TrTin()
244  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
245  self.assertEqual(1, trtin.previous_boundary_point(0))
246  self.assertEqual(2, trtin.previous_boundary_point(1))
247  self.assertEqual(4, trtin.previous_boundary_point(2))
248  self.assertEqual(3, trtin.previous_boundary_point(4))
249  self.assertEqual(0, trtin.previous_boundary_point(3))
250 
251  def test_get_boundary_points(self):
252  trtin = xt.TrTin()
253  pts = ((0, 0, 0), (10, 0, 0), (20, 0, 0), (0, 10, 0), (10, 10, 0),
254  (20, 10, 0), (0, 20, 0), (10, 20, 0), (20, 20, 0),)
255  tris = (0, 1, 3, 1, 6, 3, 1, 4, 6, 4, 7, 6,
256  1, 2, 4, 2, 7, 4, 2, 5, 7, 5, 8, 7)
257  tris_adj = ((0,), (0, 1, 2, 4), (4, 5, 6), (0, 1), (2, 4, 5, 3),
258  (6, 7), (1, 2, 3), (3, 5, 6, 7), (7,))
259  trtin.set_geometry(pts, tris, tris_adj)
260  np.testing.assert_array_equal(
261  np.array([0, 1, 2, 3, 5, 6, 7, 8]),
262  trtin.get_boundary_points()
263  )
264 
265  def test_get_boundary_polys(self):
266  trtin = xt.TrTin()
267  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
268  self.assertEqual(((0, 3, 4, 2, 1, 0),), trtin.get_boundary_polys())
269 
270  def test_get_extents(self):
271  trtin = xt.TrTin()
272  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
273  mn, mx = trtin.get_extents()
274  self.assertEqual((0, 0, 0), mn)
275  self.assertEqual((20, 10, 0), mx)
276 
277  def test_export_tin_file(self):
278  trtin = xt.TrTin()
279  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
280  trtin.export_tin_file("testfile.txt")
281  self.assertTrue(os.path.isfile("testfile.txt"))
282 
283  def test_swap_edge(self):
284  trtin = xt.TrTin()
285  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
286  rv = trtin.swap_edge(0, 2, True)
287  self.assertEqual(True, rv)
288  self.assertEqual(trtin.tris[0], 3)
289  self.assertEqual(trtin.tris[1], 0)
290  self.assertEqual(trtin.tris[2], 4)
291  self.assertEqual(trtin.tris[3], 1)
292  self.assertEqual(trtin.tris[4], 2)
293  self.assertEqual(trtin.tris[5], 4)
294  self.assertEqual(trtin.tris[6], 1)
295  self.assertEqual(trtin.tris[7], 4)
296  self.assertEqual(trtin.tris[8], 0)
297 
298  tris_adj = trtin.tris_adj
299  self.assertEqual((0, 2), tris_adj[0])
300  self.assertEqual((1, 2), tris_adj[1])
301  self.assertEqual((1,), tris_adj[2])
302  self.assertEqual((0,), tris_adj[3])
303  self.assertEqual((1, 2, 0), tris_adj[4])
304 
305  def test_delete_triangles(self):
306  # TODO: I don't think this is right.
307  trtin = xt.TrTin()
308  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
309  to_delete = (1, 0)
310  trtin.delete_triangles(to_delete)
311  self.assertEqual(5, trtin.num_points)
312  self.assertEqual(1, trtin.num_triangles)
313 
314  def test_delete_points(self):
315  trtin = xt.TrTin()
316  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
317  to_delete = (2, 4)
318  trtin.delete_points(to_delete)
319  self.assertEqual(3, trtin.num_points)
320  self.assertEqual(1, trtin.num_triangles)
321 
322  def test_optimize_triangulation(self):
323  trtin = xt.TrTin()
324  pts = ((0, 0, 0), (10, 0, 0), (20, 0, 0), (0, 10, 0), (10, 10, 0),
325  (20, 10, 0), (0, 20, 0), (10, 20, 0), (20, 20, 0),)
326  tris = (0, 1, 3, 1, 6, 3, 1, 4, 6, 4, 7, 6,
327  1, 2, 4, 2, 7, 4, 2, 5, 7, 5, 8, 7)
328  tris_adj = ((0,), (0, 1, 2, 4), (4, 5, 6), (0, 1), (2, 4, 5, 3),
329  (6, 7), (1, 2, 3), (3, 5, 6, 7), (7,))
330  trtin.set_geometry(pts, tris, tris_adj)
331  trtin.optimize_triangulation()
332  np.testing.assert_array_equal(np.array(tris), trtin.tris)
333 
334  def test_build_tris_adj_to_pts(self):
335  trtin = xt.TrTin()
336  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
337  trtin.build_tris_adj_to_pts()
338  self.assertEqual(((0,), (0, 1, 2), (1,), (0, 2), (1, 2)), trtin.tris_adj)
339 
340  def test_clear(self):
341  trtin = xt.TrTin()
342  trtin.set_geometry(self.pts, self.tris, self.tris_adj)
343  np.testing.assert_array_equal(self.pts_np, trtin.pts)
344  np.testing.assert_array_equal(self.tris_np, trtin.tris)
345  self.assertEqual(self.tris_adj, trtin.tris_adj)
346  trtin.clear()
347  self.assertEqual(0, len(trtin.pts))
348  self.assertEqual(0, len(trtin.tris))
349  self.assertEqual((), trtin.tris_adj)
350 
351  def test_from_string(self):
352  trtin = xt.TrTin()
353 
354  inpt = "22 serialization::archive 15 1 0\n0 0 0 0 0 5 0 0 0 0.00000000000000000e+00 " \
355  "0.00000000000000000e+00 0.00000000000000000e+00 1.00000000000000000e+01 " \
356  "0.00000000000000000e+00 0.00000000000000000e+00 2.00000000000000000e+01 " \
357  "0.00000000000000000e+00 0.00000000000000000e+00 5.00000000000000000e+00 " \
358  "1.00000000000000000e+01 0.00000000000000000e+00 1.50000000000000000e+01 " \
359  "1.00000000000000000e+01 0.00000000000000000e+00 9 0 3 0 1 1 2 4 1 4 3 0 0 " \
360  "5 0 1 0 0 3 0 0 1 2 1 0 1 2 0 0 2 2 0 1 2\n"
361 
362  trtin.from_string(inpt)
363 
364  np.testing.assert_array_equal(self.pts_np, trtin.pts)
365  np.testing.assert_array_equal(self.tris_np, trtin.tris)
366  self.assertEqual(self.tris_adj, trtin.tris_adj)
367  self.assertEqual(inpt, str(trtin))