| 1 |
"""Unit tests for the IntersectionConstruction class""" |
| 2 |
|
| 3 |
# Copyright (C) 2014 Anders Logg and August Johansson |
| 4 |
# |
| 5 |
# This file is part of DOLFIN. |
| 6 |
# |
| 7 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# DOLFIN is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import pytest |
| 21 |
import numpy as np |
| 22 |
from dolfin import * |
| 23 |
from dolfin_utils.test import skip_in_parallel |
| 24 |
|
| 25 |
def triangulation_to_mesh_2d(triangulation): |
| 26 |
editor = MeshEditor() |
| 27 |
mesh = Mesh() |
| 28 |
editor.open(mesh, 2, 2) |
| 29 |
num_cells = len(triangulation) // 6 |
| 30 |
num_vertices = len(triangulation) // 2 |
| 31 |
editor.init_cells(num_cells) |
| 32 |
editor.init_vertices(num_vertices) |
| 33 |
for i in range(num_cells): |
| 34 |
editor.add_cell(i, np.array( (3*i, 3*i + 1, 3*i + 2), dtype='uint') ) |
| 35 |
for i in range(num_vertices): |
| 36 |
editor.add_vertex(i, np.array( (triangulation[2*i], triangulation[2*i + 1]), dtype='float')) |
| 37 |
editor.close() |
| 38 |
return mesh |
| 39 |
|
| 40 |
def triangulation_to_mesh_2d_3d(triangulation): |
| 41 |
editor = MeshEditor() |
| 42 |
mesh = Mesh() |
| 43 |
editor.open(mesh,2,3) |
| 44 |
num_cells = len(triangulation) // 9 |
| 45 |
num_vertices = len(triangulation) // 3 |
| 46 |
editor.init_cells(num_cells) |
| 47 |
editor.init_vertices(num_vertices) |
| 48 |
for i in range(num_cells): |
| 49 |
editor.add_cell(i, np.array( (3*i, 3*i+1, 3*i+2), dtype='uint')) |
| 50 |
for i in range(num_vertices): |
| 51 |
editor.add_vertex(i, np.array( (triangulation[3*i], triangulation[3*i+1], triangulation[3*i+2]), dtype='float') ) |
| 52 |
editor.close() |
| 53 |
return mesh |
| 54 |
|
| 55 |
def triangulation_to_mesh_3d(triangulation): |
| 56 |
editor = MeshEditor() |
| 57 |
mesh = Mesh() |
| 58 |
editor.open(mesh,3,3) |
| 59 |
num_cells = len(triangulation) // 12 |
| 60 |
num_vertices = len(triangulation) // 3 |
| 61 |
editor.init_cells(num_cells) |
| 62 |
editor.init_vertices(num_vertices) |
| 63 |
for i in range(num_cells): |
| 64 |
editor.add_cell(i, np.array( (4*i, 4*i+1, 4*i+2, 4*i+3), dtype='uint')) |
| 65 |
for i in range(num_vertices): |
| 66 |
editor.add_vertex(i, np.array( (triangulation[3*i], triangulation[3*i+1], triangulation[3*i+2]), dtype='float')) |
| 67 |
editor.close() |
| 68 |
return mesh |
| 69 |
|
| 70 |
@skip_in_parallel |
| 71 |
@pytest.mark.skipif(True, reason="Missing swig typemap") |
| 72 |
def test_triangulate_intersection_2d(): |
| 73 |
|
| 74 |
# Create two meshes of the unit square |
| 75 |
mesh_0 = UnitSquareMesh(1, 1) |
| 76 |
mesh_1 = UnitSquareMesh(1, 1) |
| 77 |
|
| 78 |
# Translate second mesh randomly |
| 79 |
#dx = Point(np.random.rand(),np.random.rand()) |
| 80 |
dx = Point(0.278498, 0.546881) |
| 81 |
mesh_1.translate(dx) |
| 82 |
|
| 83 |
# Exact volume of intersection |
| 84 |
exactvolume = (1 - abs(dx[0]))*(1 - abs(dx[1])) |
| 85 |
|
| 86 |
# Compute triangulation volume |
| 87 |
volume = 0 |
| 88 |
for c0 in cells(mesh_0): |
| 89 |
for c1 in cells(mesh_1): |
| 90 |
intersection = c0.intersection(c1) |
| 91 |
if len(intersection) >= 3 : |
| 92 |
triangulation = cpp.geometry.ConvexTriangulation.triangulate(intersection, 2, 2) |
| 93 |
tmesh = triangulation_to_mesh_2d(triangulation) |
| 94 |
for t in cells(tmesh): |
| 95 |
volume += t.volume() |
| 96 |
|
| 97 |
errorstring = "translation=" + str(dx[0]) + str(" ") + str(dx[1]) |
| 98 |
assert round(volume - exactvolume, 7) == 0, errorstring |
| 99 |
|
| 100 |
@skip_in_parallel |
| 101 |
@pytest.mark.skipif(True, reason="Not implemented in 3D") |
| 102 |
def test_triangulate_intersection_2d_3d(): |
| 103 |
|
| 104 |
# Note: this test will fail if the triangle mesh is aligned |
| 105 |
# with the tetrahedron mesh |
| 106 |
|
| 107 |
# Create a unit cube |
| 108 |
mesh_0 = UnitCubeMesh(1,1,1) |
| 109 |
|
| 110 |
# Create a 3D surface mesh |
| 111 |
editor = MeshEditor() |
| 112 |
mesh_1 = Mesh() |
| 113 |
editor.open(mesh_1,2,3) |
| 114 |
editor.init_cells(2) |
| 115 |
editor.init_vertices(4) |
| 116 |
|
| 117 |
# Add cells |
| 118 |
editor.add_cell(0, np.array( (0,1,2), dtype='uint')) |
| 119 |
editor.add_cell(1, np.array( (1,2,3), dtype='uint')) |
| 120 |
|
| 121 |
# Add vertices |
| 122 |
editor.add_vertex(0, np.array( (0, 0, 0.5), dtype='float')) |
| 123 |
editor.add_vertex(1, np.array( (1, 0, 0.5), dtype='float')) |
| 124 |
editor.add_vertex(2, np.array( (0, 1, 0.5), dtype='float')) |
| 125 |
editor.add_vertex(3, np.array( (1, 1, 0.5), dtype='float')) |
| 126 |
editor.close() |
| 127 |
|
| 128 |
# Rotate the triangle mesh around y axis |
| 129 |
angle = 23.46354 |
| 130 |
mesh_1.rotate(angle,1) |
| 131 |
|
| 132 |
# Exact area |
| 133 |
exact_volume = 1 |
| 134 |
|
| 135 |
# Compute triangulation |
| 136 |
volume = 0 |
| 137 |
for c0 in cells(mesh_0): |
| 138 |
for c1 in cells(mesh_1): |
| 139 |
intersection = c0.intersection(c1) |
| 140 |
triangulation = cpp.geometry.ConvexTriangulation.triangulate(intersection, 3, 2) |
| 141 |
if (triangulation.size>0): |
| 142 |
tmesh = triangulation_to_mesh_2d_3d(triangulation) |
| 143 |
for t in cells(tmesh): |
| 144 |
volume += t.volume() |
| 145 |
|
| 146 |
errorstring = "rotation angle = " + str(angle) |
| 147 |
assert round(volume - exact_volume, 7) == 0, errorstring |
| 148 |
|
| 149 |
@skip_in_parallel |
| 150 |
@pytest.mark.skipif(True, reason="Missing swig typemap for call to ConvexTriangulation") |
| 151 |
def test_triangulate_intersection_3d(): |
| 152 |
|
| 153 |
# Create two meshes of the unit cube |
| 154 |
mesh_0 = UnitCubeMesh(1, 1, 1) |
| 155 |
mesh_1 = UnitCubeMesh(1, 1, 1) |
| 156 |
|
| 157 |
# Translate second mesh |
| 158 |
# dx = Point(np.random.rand(),np.random.rand(),np.random.rand()) |
| 159 |
dx = Point(0.913375, 0.632359, 0.097540) |
| 160 |
|
| 161 |
mesh_1.translate(dx) |
| 162 |
exactvolume = (1 - abs(dx[0]))*(1 - abs(dx[1]))*(1 - abs(dx[2])) |
| 163 |
|
| 164 |
# Compute triangulation |
| 165 |
volume = 0 |
| 166 |
for c0 in cells(mesh_0): |
| 167 |
for c1 in cells(mesh_1): |
| 168 |
intersection = c0.intersection(c1) |
| 169 |
triangulation = cpp.geometry.ConvexTriangulation.triangulate(intersection, 3, 3) |
| 170 |
if (triangulation.size>0): |
| 171 |
tmesh = triangulation_to_mesh_3d(triangulation) |
| 172 |
for t in cells(tmesh): |
| 173 |
volume += t.volume() |
| 174 |
|
| 175 |
errorstring = "translation=" |
| 176 |
errorstring += str(dx[0])+" "+str(dx[1])+" "+str(dx[2]) |
| 177 |
assert round(volume - exactvolume, 7) == 0, errorstring |
| 178 |
|
| 179 |
def test_triangle_triangle_2d_trivial() : |
| 180 |
" These two triangles intersect in a common edge" |
| 181 |
res = cpp.geometry.IntersectionConstruction.intersection_triangle_triangle_2d(Point(0.0, 0.0), |
| 182 |
Point(1.0, 0.0), |
| 183 |
Point(0.5, 1.0), |
| 184 |
Point(0.5, 0.5), |
| 185 |
Point(1.0, 1.5), |
| 186 |
Point(0.0, 1.5)) |
| 187 |
assert len(res) == 4 |
| 188 |
|
| 189 |
def test_triangle_triangle_2d() : |
| 190 |
" These two triangles intersect in a common edge" |
| 191 |
res = cpp.geometry.IntersectionConstruction.intersection_triangle_triangle_2d(Point(0.4960412972015322, 0.3953317542541379), |
| 192 |
Point(0.5, 0.3997044273055517), |
| 193 |
Point(0.5, 0.4060889538943557), |
| 194 |
Point(0.4960412972015322, 0.3953317542541379), |
| 195 |
Point(0.5, 0.4060889538943557), |
| 196 |
Point(.5, .5)) |
| 197 |
for p in res: |
| 198 |
print(p[0], p[1]) |
| 199 |
|
| 200 |
assert len(res) == 2 |
| 201 |
|
| 202 |
@skip_in_parallel |
| 203 |
def test_parallel_segments_2d(): |
| 204 |
" These two segments should be parallel and the intersection computed accordingly" |
| 205 |
p0 = Point(0, 0) |
| 206 |
p1 = Point(1, 0) |
| 207 |
q0 = Point(0.4, 0) |
| 208 |
q1 = Point(1.4, 0) |
| 209 |
intersection = cpp.geometry.IntersectionConstruction.intersection_segment_segment_2d(p0, p1, q0, q1) |
| 210 |
assert len(intersection) == 2 |
| 211 |
|
| 212 |
def test_equal_segments_2d(): |
| 213 |
" These two segments are equal and the intersection computed accordingly" |
| 214 |
p0 = Point(DOLFIN_PI / 7., 9. / DOLFIN_PI) |
| 215 |
p1 = Point(9. / DOLFIN_PI, DOLFIN_PI / 7.) |
| 216 |
q0 = Point(DOLFIN_PI / 7., 9. / DOLFIN_PI) |
| 217 |
q1 = Point(9. / DOLFIN_PI, DOLFIN_PI / 7.) |
| 218 |
intersection = cpp.geometry.IntersectionConstruction.intersection_segment_segment_2d(p0, p1, q0, q1) |
| 219 |
assert len(intersection) == 2 |
| 220 |
|
| 221 |
@skip_in_parallel |
| 222 |
def test_triangle_segment_2D_1(): |
| 223 |
"The intersection of a specific triangle and a specific segment" |
| 224 |
p0 = Point(1e-30, 0) |
| 225 |
p1 = Point(1, 2) |
| 226 |
p2 = Point(2, 1) |
| 227 |
q0 = Point(1, 0) |
| 228 |
q1 = Point(0, 0) |
| 229 |
intersection = cpp.geometry.IntersectionConstruction.intersection_triangle_segment_2d(p0, p1, p2, q0, q1) |
| 230 |
assert len(intersection) == 1 |
| 231 |
intersection = cpp.geometry.IntersectionConstruction.intersection_triangle_segment_2d(p0, p1, p2, q1, q0) |
| 232 |
assert len(intersection) == 1 |
| 233 |
|
| 234 |
def compare_with_cgal(p0, p1, q0, q1, cgal): |
| 235 |
intersection = cpp.geometry.IntersectionConstruction.intersection_segment_segment_2d(p0, p1, q0, q1) |
| 236 |
|
| 237 |
#for p in intersection: |
| 238 |
# print(*p) |
| 239 |
|
| 240 |
return abs(intersection[0][0] - cgal[0]) < DOLFIN_EPS and \ |
| 241 |
abs(intersection[0][1] - cgal[1]) < DOLFIN_EPS |
| 242 |
|
| 243 |
@skip_in_parallel |
| 244 |
@pytest.mark.skipif(True, reason="This is a case where the intersection currently fails") |
| 245 |
def test_segment_segment_1(): |
| 246 |
"Case that fails CGAL comparison. We get a different intersection point but still correct area." |
| 247 |
p0 = Point(-0.50000000000000710543,-0.50000000000000710543) |
| 248 |
p1 = Point(0.99999999999999955591,-2) |
| 249 |
q0 = Point(0.9142135623730932581,-1.9142135623730944793) |
| 250 |
q1 = Point(-0.29289321881346941367,-0.70710678118654635149) |
| 251 |
|
| 252 |
# The intersection should according to CGAL be |
| 253 |
cgal = Point(0.91066799144849319703, -1.9106679914484945293) |
| 254 |
|
| 255 |
assert compare_with_cgal(p0, p1, q0, q1, cgal) |
| 256 |
|
| 257 |
@skip_in_parallel |
| 258 |
@pytest.mark.skipif(True, reason="This is a case where the intersection currently fails") |
| 259 |
def test_segment_segment_2(): |
| 260 |
"Case that fails CGAL comparison. We get a different intersection point but still correct area." |
| 261 |
p0 = Point(0.70710678118654746172,-0.70710678118654746172) |
| 262 |
p1 = Point(0.70710678118654612945,0.70710678118654612945) |
| 263 |
q0 = Point(0.70710678118654612945,0.70710678118654113344) |
| 264 |
q1 = Point(0.70710678118654657354,0.2928932188134645842) |
| 265 |
cgal = Point(0.70710678118654612945, 0.7071067811865050512) |
| 266 |
assert compare_with_cgal(p0, p1, q0, q1, cgal) |
| 267 |
|
| 268 |
|
| 269 |
@skip_in_parallel |
| 270 |
#@pytest.mark.skipif(True, reason="This test needs to be updated") |
| 271 |
def test_segment_segment_3(): |
| 272 |
"Case that fails CGAL comparison. We get a different intersection point but still correct area." |
| 273 |
p0 = Point(0.70710678118654746172,-0.70710678118654746172) |
| 274 |
p1 = Point(0.70710678118654612945,0.70710678118654612945) |
| 275 |
q0 = Point(0.70710678118654757274,-0.097631072937819973756) |
| 276 |
q1 = Point(0.70710678118654257673,-0.1601886205085209236) |
| 277 |
cgal = Point(0.70710678118654679558, -0.10611057050352221132) |
| 278 |
assert compare_with_cgal(p0, p1, q0, q1, cgal) |
| 279 |
|
| 280 |
|
| 281 |
@skip_in_parallel |
| 282 |
@pytest.mark.skipif(True, reason="This is a case where the intersection currently fails") |
| 283 |
def test_segment_segment_4(): |
| 284 |
"Case that fails CGAL comparison. We get a different intersection point but still correct area." |
| 285 |
p0 = Point(0.70710678118654746172,-0.70710678118654746172) |
| 286 |
p1 = Point(3.5527136788005009294e-14,3.5527136788005009294e-14) |
| 287 |
q0 = Point(0.35355339059326984508,-0.35355339059327078877) |
| 288 |
q1 = Point(0.70710678118655057034,-0.70710678118654701763) |
| 289 |
cgal = Point(0.67572340116162599166, -0.67572340116162288304) |
| 290 |
assert compare_with_cgal(p0, p1, q0, q1, cgal) |
| 291 |
|
| 292 |
|
| 293 |
@skip_in_parallel |
| 294 |
def test_segment_segment_5(): |
| 295 |
"Case that failed CGAL comparison but passed when scaling the numerator in x = p0 + o / d * v" |
| 296 |
p0 = Point(1.1429047494274684563e-12,0.5) |
| 297 |
p1 = Point(0.42146018366139809119,0.9214601836602551721) |
| 298 |
q0 = Point(0.34292036732279607136,0.8429203673205103442) |
| 299 |
q1 = Point(0.3429203673205103442,0.8429203673205103442) |
| 300 |
cgal = Point(0.3429203673216533188,0.8429203673205103442) |
| 301 |
assert compare_with_cgal(p0, p1, q0, q1, cgal) |
| 302 |
|
| 303 |
|
| 304 |
@skip_in_parallel |
| 305 |
def test_segment_segment_6(): |
| 306 |
"Test that demonstrates, among other things, that we must check the orientation for p0, p1 in intersection_segment_segment_2d" |
| 307 |
p0 = Point(0.045342566799435518599,0.41358248517265505662); |
| 308 |
p1 = Point(0.045342566799434436131,0.41358248517265394639); |
| 309 |
q0 = Point(1.8601965322712701917e-16,0.5); |
| 310 |
q1 = Point(1.873501354054951662e-16,0.3499999999999999778); |
| 311 |
|
| 312 |
intersection = cpp.geometry.IntersectionConstruction.intersection_segment_segment_2d(p0, p1, q0, q1) |
| 313 |
|
| 314 |
assert len(intersection) == 0 |