| 1 |
"""Unit tests for the CollisionPredicates 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 |
# First added: 2014-02-16 |
| 21 |
# Last changed: 2017-09-21 |
| 22 |
|
| 23 |
import pytest |
| 24 |
from dolfin import * |
| 25 |
from dolfin_utils.test import skip_in_parallel |
| 26 |
import numpy as np |
| 27 |
|
| 28 |
@skip_in_parallel |
| 29 |
def create_triangular_mesh_3D(vertices, cells): |
| 30 |
editor = MeshEditor() |
| 31 |
mesh = Mesh() |
| 32 |
editor.open(mesh,"triangle", 2,3) |
| 33 |
editor.init_cells(2) |
| 34 |
editor.init_vertices(4) |
| 35 |
editor.add_cell(0, cells[0]) |
| 36 |
editor.add_cell(1, cells[1]) |
| 37 |
|
| 38 |
editor.add_vertex(0, vertices[0]) |
| 39 |
editor.add_vertex(1, vertices[1]) |
| 40 |
editor.add_vertex(2, vertices[2]) |
| 41 |
editor.add_vertex(3, vertices[3]) |
| 42 |
editor.close() |
| 43 |
return mesh; |
| 44 |
|
| 45 |
@skip_in_parallel |
| 46 |
def test_interval_collides_point(): |
| 47 |
"""Test if point collide with interval""" |
| 48 |
|
| 49 |
mesh = UnitIntervalMesh(1) |
| 50 |
cell = Cell(mesh, 0) |
| 51 |
|
| 52 |
assert cell.collides(Point(0.5)) == True |
| 53 |
assert cell.collides(Point(1.5)) == False |
| 54 |
|
| 55 |
@skip_in_parallel |
| 56 |
def test_segment_collides_point_2D(): |
| 57 |
"""Test if segment collide with point in 2D""" |
| 58 |
mesh = Mesh() |
| 59 |
editor = MeshEditor() |
| 60 |
editor.open(mesh, "interval", 1, 2) |
| 61 |
editor.init_vertices(2) |
| 62 |
editor.init_cells(1) |
| 63 |
a = np.array( (1./8., 1./4.), dtype='float') |
| 64 |
b = np.array( (2./8., 3./4.), dtype='float') |
| 65 |
editor.add_vertex(0, a) |
| 66 |
editor.add_vertex(1, b) |
| 67 |
editor.add_cell(0, np.array( (0,1), dtype='uint')) |
| 68 |
editor.close() |
| 69 |
cell = Cell(mesh, 0) |
| 70 |
mid = Point(1.5/8., 2./4.) |
| 71 |
mid_average = (a + b) / 2 |
| 72 |
assert cell.contains(mid) |
| 73 |
assert cell.contains(Point(a[0], a[1])) |
| 74 |
assert cell.contains(cell.midpoint()) |
| 75 |
|
| 76 |
@skip_in_parallel |
| 77 |
def test_point_on_segment(): |
| 78 |
a = Point(1e-30, 0) |
| 79 |
b = Point(1e-3, 0) |
| 80 |
c = Point(0, 0) |
| 81 |
d = Point(-1e-30, 0) |
| 82 |
q0 = Point(1, 0) |
| 83 |
q1 = Point(0, 0) |
| 84 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, a) |
| 85 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, b) |
| 86 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, c) |
| 87 |
assert not cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, d) |
| 88 |
|
| 89 |
@skip_in_parallel |
| 90 |
def test_point_on_small_segment(): |
| 91 |
a = Point(1e-30, 0) |
| 92 |
b = Point(0, 0) |
| 93 |
c = Point(1e-31, 0) |
| 94 |
d = Point(-1e-30, 0) |
| 95 |
q0 = Point(0, 0) |
| 96 |
q1 = Point(1e-30, 0) |
| 97 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, a) |
| 98 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, b) |
| 99 |
assert cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, c) |
| 100 |
assert not cpp.geometry.CollisionPredicates.collides_segment_point_2d(q0, q1, d) |
| 101 |
|
| 102 |
@skip_in_parallel |
| 103 |
def test_segment_collides_point_3D(): |
| 104 |
"""Test if segment collide with point in 3D""" |
| 105 |
mesh = Mesh() |
| 106 |
editor = MeshEditor() |
| 107 |
editor.open(mesh, "interval", 1, 3) |
| 108 |
editor.init_vertices(2) |
| 109 |
editor.init_cells(1) |
| 110 |
editor.add_vertex(0, np.array( (1./16., 1./8., 1./4.), dtype='float')) |
| 111 |
editor.add_vertex(1, np.array( ( 2./16., 3./8., 2./4.), dtype='float')) |
| 112 |
editor.add_cell(0, np.array( (0,1), dtype='uint') ) |
| 113 |
editor.close() |
| 114 |
cell = Cell(mesh, 0) |
| 115 |
mid = Point(1.5/16., 2./8., 1.5/4.) |
| 116 |
assert cell.contains(mid) |
| 117 |
assert cell.contains(cell.midpoint()) |
| 118 |
|
| 119 |
@skip_in_parallel |
| 120 |
def test_triangle_collides_point(): |
| 121 |
"""Tests if point collide with triangle""" |
| 122 |
|
| 123 |
mesh = UnitSquareMesh(1, 1) |
| 124 |
cell = Cell(mesh, 0) |
| 125 |
|
| 126 |
assert cell.collides(Point(0.5)) == True |
| 127 |
assert cell.collides(Point(1.5)) == False |
| 128 |
|
| 129 |
@skip_in_parallel |
| 130 |
def test_degenerate_triangle_collides_point(): |
| 131 |
"""Test a degenerate triangle that does not collide""" |
| 132 |
|
| 133 |
p0 = Point(-0.10950608157830554745,0.14049391842169450806) |
| 134 |
p1 = Point(-0.10950608157830354905,0.14049391842169650646) |
| 135 |
p2 = Point(0.32853262580480108168,0.57853262580480113719) |
| 136 |
q = Point(3.5952674716233090635e-06,0.25000359526747162331) |
| 137 |
|
| 138 |
assert cpp.geometry.CollisionPredicates.collides_triangle_point_2d(p0, p1, p2, q) == False |
| 139 |
|
| 140 |
@skip_in_parallel |
| 141 |
@pytest.mark.xfail(strict=True, raises=RuntimeError) |
| 142 |
def test_quadrilateral_collides_point(): |
| 143 |
mesh = UnitSquareMesh.create(1, 1, CellType.Type.quadrilateral) |
| 144 |
cell = Cell(mesh, 0) |
| 145 |
assert cell.collides(Point(0.5)) == True |
| 146 |
assert cell.collides(Point(1.5)) == False |
| 147 |
|
| 148 |
@skip_in_parallel |
| 149 |
@pytest.mark.xfail(strict=True, raises=RuntimeError) |
| 150 |
def test_hexahedron_collides_point(): |
| 151 |
"""Test if point collide with hexahedron""" |
| 152 |
mesh = UnitCubeMesh.create(1, 1, 1, CellType.Type.hexahedron) |
| 153 |
cell = Cell(mesh, 0) |
| 154 |
|
| 155 |
assert cell.collides(Point(0.5)) == True |
| 156 |
# FIXME: cell.collides(Point) returns True for any 1D, 2D Point |
| 157 |
# cell.collides(Point) returns False if Point[2] != 0 |
| 158 |
assert cell.collides(Point(1.5)) == False |
| 159 |
|
| 160 |
|
| 161 |
@skip_in_parallel |
| 162 |
def test_triangle_collides_triangle(): |
| 163 |
"""Test if triangle collide with triangle""" |
| 164 |
|
| 165 |
m0 = UnitSquareMesh(8, 8) |
| 166 |
c0 = Cell(m0, 0) |
| 167 |
|
| 168 |
m1 = UnitSquareMesh(8, 8) |
| 169 |
m1.translate(Point(0.1, 0.1)) |
| 170 |
c1 = Cell(m1, 0) |
| 171 |
c2 = Cell(m1, 1) |
| 172 |
|
| 173 |
assert c0.collides(c0) == True |
| 174 |
assert c0.collides(c1) == True |
| 175 |
assert c0.collides(c2) == True # touching edges |
| 176 |
assert c1.collides(c0) == True |
| 177 |
assert c1.collides(c1) == True |
| 178 |
assert c1.collides(c2) == True |
| 179 |
assert c2.collides(c0) == True # touching edges |
| 180 |
assert c2.collides(c1) == True |
| 181 |
assert c2.collides(c2) == True |
| 182 |
|
| 183 |
|
| 184 |
@skip_in_parallel |
| 185 |
def test_triangle_triangle_collision() : |
| 186 |
"Test that has been failing" |
| 187 |
assert cpp.geometry.CollisionPredicates.collides_triangle_triangle_2d(Point(0.177432070718943, 0.5), |
| 188 |
Point(0.176638957524249, 0.509972290857582), |
| 189 |
Point(0.217189283468892, 0.550522616802225), |
| 190 |
Point(0.333333333333333, 0.52399308981973), |
| 191 |
Point(0.333333333333333, 0.666666666666667), |
| 192 |
Point(0.211774439087554, 0.545107772420888)) |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
@skip_in_parallel |
| 197 |
def test_triangle_collides_point_3D(): |
| 198 |
"""Test if point collide with triangle (inspired by test_manifold_dg0_functions)""" |
| 199 |
vertices = [ np.array( (0.0, 0.0, 1.0), dtype='float'), |
| 200 |
np.array( (1.0, 1.0, 1.0), dtype='float'), |
| 201 |
np.array( (1.0, 0.0, 0.0), dtype='float'), |
| 202 |
np.array( (0.0, 1.0, 0.0), dtype='float') ] |
| 203 |
cells = [ np.array( (0, 1, 2), dtype='uint'), |
| 204 |
np.array( (0, 1, 3), dtype='uint') ] |
| 205 |
mesh = create_triangular_mesh_3D(vertices, cells) |
| 206 |
points = [ Point(0.0, 0.0, 1.0), |
| 207 |
Point(1.0, 1.0, 1.0), |
| 208 |
Point(1.0, 0.0, 0.0), |
| 209 |
Point(0.0, 1.0, 0.0), |
| 210 |
Point(0.25, 0.5, 0.75), |
| 211 |
Point(0.5, 0.25, 0.75) |
| 212 |
] |
| 213 |
A = Cell(mesh, 0) |
| 214 |
B = Cell(mesh, 1) |
| 215 |
assert A.collides(points[0]) == True |
| 216 |
assert B.collides(points[0]) == True |
| 217 |
assert A.collides(points[1]) == True |
| 218 |
assert B.collides(points[1]) == True |
| 219 |
assert A.collides(points[2]) == True |
| 220 |
assert B.collides(points[2]) == False |
| 221 |
assert A.collides(points[3]) == False |
| 222 |
assert B.collides(points[3]) == True |
| 223 |
assert A.collides(points[4]) == False |
| 224 |
assert B.collides(points[4]) == True |
| 225 |
assert A.collides(points[5]) == True |
| 226 |
assert B.collides(points[5]) == False |
| 227 |
|
| 228 |
#@pytest.mark.skipif(True, reason="Not implemented in 3D") |
| 229 |
@skip_in_parallel |
| 230 |
def test_tetrahedron_collides_point(): |
| 231 |
"""Test if point collide with tetrahedron""" |
| 232 |
|
| 233 |
mesh = UnitCubeMesh(1, 1, 1) |
| 234 |
cell = Cell(mesh, 0) |
| 235 |
|
| 236 |
assert cell.collides(Point(0.5)) == True |
| 237 |
assert cell.collides(Point(1.5)) == False |
| 238 |
|
| 239 |
@skip_in_parallel |
| 240 |
#@pytest.mark.skipif(True, reason="Not implemented in 3D") |
| 241 |
def test_tetrahedron_collides_triangle(): |
| 242 |
"""Test if point collide with tetrahedron""" |
| 243 |
|
| 244 |
tetmesh = UnitCubeMesh(2, 2, 2) |
| 245 |
vertices = [ np.array( (0, 0, 0.5), dtype='float'), |
| 246 |
np.array( (1, 0, 0.5), dtype='float'), |
| 247 |
np.array( (0, 1, 0.5), dtype='float'), |
| 248 |
np.array( (1, 1, 0.5), dtype='float')] |
| 249 |
cells = [ np.array( (0, 1, 2), dtype='uint'), |
| 250 |
np.array( (1, 2, 3), dtype='uint')] |
| 251 |
|
| 252 |
trimesh = create_triangular_mesh_3D(vertices, cells) |
| 253 |
dx = Point(0.1, 0.1, -0.1) |
| 254 |
trimesh_shift = create_triangular_mesh_3D(vertices, cells) |
| 255 |
trimesh_shift.translate(dx) |
| 256 |
|
| 257 |
tet0 = Cell(tetmesh, 18) |
| 258 |
tet1 = Cell(tetmesh, 19) |
| 259 |
tri0 = Cell(trimesh, 1) |
| 260 |
tri0shift = Cell(trimesh_shift, 1) |
| 261 |
|
| 262 |
# proper intersection |
| 263 |
assert tet0.collides(tri0shift) == True |
| 264 |
assert tri0shift.collides(tet0) == True |
| 265 |
assert tet1.collides(tri0shift) == True |
| 266 |
assert tri0shift.collides(tet1) == True |
| 267 |
|
| 268 |
# point touch |
| 269 |
assert tet0.collides(tri0) == True |
| 270 |
assert tri0.collides(tet0) == True |
| 271 |
|
| 272 |
# face alignment (true or false) |
| 273 |
assert tet1.collides(tri0) == True |
| 274 |
assert tri0.collides(tet1) == True |
| 275 |
|
| 276 |
@skip_in_parallel |
| 277 |
#@pytest.mark.skipif(True, reason="Not implemented in 3D") |
| 278 |
def test_tetrahedron_collides_tetrahedron(): |
| 279 |
"""Test if point collide with tetrahedron""" |
| 280 |
|
| 281 |
m0 = UnitCubeMesh(2, 2, 2) |
| 282 |
c19 = Cell(m0, 19) |
| 283 |
c26 = Cell(m0, 26) |
| 284 |
c37 = Cell(m0, 37) |
| 285 |
c43 = Cell(m0, 43) |
| 286 |
c45 = Cell(m0, 45) |
| 287 |
|
| 288 |
m1 = UnitCubeMesh(1,1,1) |
| 289 |
m1.translate(Point(0.5, 0.5, 0.5)) |
| 290 |
c3 = Cell(m0, 3) |
| 291 |
c5 = Cell(m1, 5) |
| 292 |
|
| 293 |
# self collisions |
| 294 |
assert c3.collides(c3) == True |
| 295 |
assert c45.collides(c45) == True |
| 296 |
|
| 297 |
# standard collisions |
| 298 |
assert c3.collides(c37) == True |
| 299 |
assert c37.collides(c3) == True |
| 300 |
|
| 301 |
# edge face |
| 302 |
assert c5.collides(c45) == True |
| 303 |
assert c45.collides(c5) == True |
| 304 |
|
| 305 |
# touching edges |
| 306 |
assert c5.collides(c19) == False |
| 307 |
assert c19.collides(c5) == False |
| 308 |
|
| 309 |
# touching faces |
| 310 |
assert c3.collides(c43) == True |
| 311 |
assert c43.collides(c3) == True |