tests/quadrilaterals.py

Tue, 23 Jan 2018 14:09:52 +0200

author
Santeri Piippo
date
Tue, 23 Jan 2018 14:09:52 +0200
changeset 29
db6ca177c6c4
parent 28
5933250813a3
child 30
0d9ca37901ed
permissions
-rw-r--r--

added a simple web frontend

22
9bb00170780b refactor
Santeri Piippo
parents: 20
diff changeset
1 from math import radians
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
2 from testsuite import warning, error
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
3 from geometry import *
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
4
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
5 def sign_consistency(container):
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
6 # Returns whether all elements in container have the same sign
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
7 return min(container) * max(container) >= 0
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
8
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
9 def concave_test(model):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 22
diff changeset
10 ''' Checks that all quadrilaterals are convex. '''
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
11 for quadrilateral in model.quadrilaterals:
28
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
12 # Rotate the polygon into the XY plane. Then z is facing
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
13 # away from the quadrilateral.
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
14 geometry = transform_to_xy(quadrilateral.geometry)
28
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
15 # Now do a 2D concavity test:
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
16 # https://math.stackexchange.com/a/1745427
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
17 z_scores = [
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
18 cross_product(v2 - v1, v3 - v1).z
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
19 for v1, v2, v3 in pairs(geometry.vertices, count = 3)
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
20 ]
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
21 if not sign_consistency(z_scores):
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
22 yield error(quadrilateral, 'concave-error')
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
23
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
24 def skew_test(model):
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 22
diff changeset
25 ''' Checks that all quadrilaterals are coplanar. '''
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
26 for quadrilateral in model.quadrilaterals:
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
27 for triangles in split_quadrilateral(quadrilateral.geometry):
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
28 plane_1 = triangle_plane_normal(triangles[0])
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
29 plane_2 = triangle_plane_normal(triangles[1])
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
30 skew_angle = vector_angle(plane_1, plane_2, normalized = True)
27
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
31 if skew_angle > radians(3.0):
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
32 yield error(quadrilateral, 'skew-error',
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
33 skew_angle = skew_angle,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
34 )
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
35 break
27
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
36 elif skew_angle > radians(1.0):
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
37 yield warning(quadrilateral, 'skew-warning',
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
38 skew_angle = skew_angle,
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
39 )
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
40 break
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
41
28
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
42 def bowtie_test(model):
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
43 for quadrilateral in model.quadrilaterals:
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
44 geometry = transform_to_xy(quadrilateral.geometry)
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
45 vertices = IndexRing(geometry.vertices)
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
46 for i in (0, 1):
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
47 line_1 = LineSegment(vertices[0 + i], vertices[1 + i])
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
48 line_2 = LineSegment(vertices[2 + i], vertices[3 + i])
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
49 intersection = line_segment_intersection_xy(line_1, line_2)
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
50 if intersection:
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
51 yield error(quadrilateral, 'self-intersecting')
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
52 break
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
53
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
54 manifest = {
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
55 'tests': {
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
56 'skew': skew_test,
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
57 'concave': concave_test,
28
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
58 'bowtie': bowtie_test,
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
59 },
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
60 'messages': {
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
61 'skew-error': lambda skew_angle:
22
9bb00170780b refactor
Santeri Piippo
parents: 20
diff changeset
62 str.format('skew quadrilateral (plane angle {})',
9bb00170780b refactor
Santeri Piippo
parents: 20
diff changeset
63 degree_rep(skew_angle),
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
64 ),
27
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
65 'skew-warning': lambda skew_angle:
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
66 str.format('slightly skew quadrilateral (plane angle {})',
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
67 degree_rep(skew_angle),
3089611c99d1 Changed threshold skew test, added warning for slightly skew quads
Santeri Piippo
parents: 26
diff changeset
68 ),
18
672ebc45685a made errors prettier
Santeri Piippo
parents: 16
diff changeset
69 'concave-error': 'concave quadrilateral',
28
5933250813a3 Added bowtie check
Santeri Piippo
parents: 27
diff changeset
70 'self-intersecting': 'bowtie quadrilateral',
16
09cc89622262 Commit work done on test loading
Santeri Piippo
parents: 14
diff changeset
71 },
14
d383f319f35b transform_to_xy implemented and concave test online
Santeri Piippo
parents: 13
diff changeset
72 }

mercurial