| 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: 2016-11-22 |
| 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_mesh(a, b): |
| 30 |
editor = MeshEditor() |
| 31 |
mesh = Mesh() |
| 32 |
editor.open(mesh, "interval", 1, 2) |
| 33 |
editor.init_cells(1) |
| 34 |
editor.init_vertices(2) |
| 35 |
editor.add_cell(0, np.array( (0, 1), dtype='uint') ) |
| 36 |
editor.add_vertex(0, np.array( (a.x(), a.y()), dtype='float')) |
| 37 |
editor.add_vertex(1, np.array( (b.x(), b.y()), dtype='float')) |
| 38 |
editor.close() |
| 39 |
return mesh; |
| 40 |
|
| 41 |
@skip_in_parallel |
| 42 |
def test_L_version_1(): |
| 43 |
mesh0 = create_mesh(Point(0., 0.), Point(1., 0.)) |
| 44 |
mesh1 = create_mesh(Point(0., 0.), Point(0., 1.)) |
| 45 |
cell0 = Cell(mesh0, 0) |
| 46 |
cell1 = Cell(mesh1, 0) |
| 47 |
assert cell0.collides(cell1) == True |
| 48 |
|
| 49 |
@skip_in_parallel |
| 50 |
def test_L_version_2(): |
| 51 |
# mesh0 = create_mesh(Point(np.finfo(np.float32).eps, 0.), Point(1., 0.)) |
| 52 |
# mesh0 = create_mesh(Point(eps(), 0.), Point(1., 0.)) |
| 53 |
mesh0 = create_mesh(Point(2.23e-15, 0.), Point(1., 0.)) |
| 54 |
mesh1 = create_mesh(Point(0., 0.), Point(0., 1.)) |
| 55 |
# print(mesh0.str(True)) |
| 56 |
# print(mesh1.str(True)) |
| 57 |
cell0 = Cell(mesh0, 0) |
| 58 |
cell1 = Cell(mesh1, 0) |
| 59 |
assert cell0.collides(cell1) == False |
| 60 |
|
| 61 |
@skip_in_parallel |
| 62 |
def test_L_version_3(): |
| 63 |
# mesh0 = create_mesh(Point(np.finfo(np.float32).eps, 0.), Point(1., 0.)) |
| 64 |
# mesh0 = create_mesh(Point(eps(), 0.), Point(1., 0.)) |
| 65 |
a = Point(2.23e-100, 0.) # assume shewchuk works |
| 66 |
b = Point(1., 0.) |
| 67 |
c = Point(0., 0.) |
| 68 |
d = Point(0., 1.) |
| 69 |
|
| 70 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(a, b, c, d) == False |
| 71 |
|
| 72 |
@skip_in_parallel |
| 73 |
def test_aligned_version_1(): |
| 74 |
mesh0 = create_mesh(Point(0,0), Point(1,0)) |
| 75 |
mesh1 = create_mesh(Point(1,0), Point(2,0)) |
| 76 |
cell0 = Cell(mesh0, 0) |
| 77 |
cell1 = Cell(mesh1, 0) |
| 78 |
assert cell0.collides(cell1) == True |
| 79 |
|
| 80 |
@skip_in_parallel |
| 81 |
def test_aligned_version_2(): |
| 82 |
mesh0 = create_mesh(Point(0,0), Point(1,0)) |
| 83 |
mesh1 = create_mesh(Point(2,0), Point(3,0)) |
| 84 |
cell0 = Cell(mesh0, 0) |
| 85 |
cell1 = Cell(mesh1, 0) |
| 86 |
assert cell0.collides(cell1) == False |
| 87 |
|
| 88 |
@skip_in_parallel |
| 89 |
def test_collinear_1(): |
| 90 |
p0 = Point(0.05, 0.15) |
| 91 |
p1 = Point(0.85, 0.95) |
| 92 |
q0 = Point(0.2875, 0.3875) |
| 93 |
q1 = Point(0.6125, 0.7125) |
| 94 |
meshp = create_mesh(p0, p1) |
| 95 |
meshq = create_mesh(q0, q1) |
| 96 |
cellp = Cell(meshp, 0) |
| 97 |
cellq = Cell(meshq, 0) |
| 98 |
assert cellp.collides(cellq) == True |
| 99 |
|
| 100 |
@skip_in_parallel |
| 101 |
def test_collinear_2(): |
| 102 |
res = cpp.geometry.CollisionPredicates.collides_segment_segment_2d(Point(.5, .3), |
| 103 |
Point(.5, .4), |
| 104 |
Point(.5, .5), |
| 105 |
Point(.5, .6)) |
| 106 |
assert not res |
| 107 |
|
| 108 |
@skip_in_parallel |
| 109 |
def test_segment_segment_2d(): |
| 110 |
# p0 is on segment q0-q1 |
| 111 |
p0 = Point(1e-30, 0) |
| 112 |
p1 = Point(1, 2) |
| 113 |
p2 = Point(2, 1) |
| 114 |
q0 = Point(1, 0) |
| 115 |
q1 = Point(0, 0) |
| 116 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p1, q0, q1) |
| 117 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p1, p0, q0, q1) |
| 118 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p1, q1, q0) |
| 119 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p1, p0, q1, q0) |
| 120 |
|
| 121 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p2, q0, q1) |
| 122 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p2, p0, q0, q1) |
| 123 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p2, q1, q0) |
| 124 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p2, p0, q1, q0) |
| 125 |
|
| 126 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p1, q1, q0) |
| 127 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p1, p0, q1, q0) |
| 128 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p1, q0, q1) |
| 129 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p1, p0, q0, q1) |
| 130 |
|
| 131 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p2, q1, q0) |
| 132 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p2, p0, q1, q0) |
| 133 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p0, p2, q0, q1) |
| 134 |
assert cpp.geometry.CollisionPredicates.collides_segment_segment_2d(p2, p0, q0, q1) |