tests/concave.py

Sun, 21 Jan 2018 15:03:38 +0200

author
Santeri Piippo
date
Sun, 21 Jan 2018 15:03:38 +0200
changeset 13
12d4ddc4bfd8
parent 12
eb74680a5e43
child 14
d383f319f35b
permissions
-rw-r--r--

Got the skew test working

13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
1 from math import acos, degrees, radians, pi as π
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 transform_to_xy(geometry):
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
10 a, b, c = geometry.vertices[:3]
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
11
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
13 def concave_test(model):
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
14 for quadrilateral in model.quadrilaterals:
12
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
15 print([cross_product(v2 - v1, v3 - v1) for v1, v2, v3 in pairs(quadrilateral.geometry.vertices, count = 3)])
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
16 z_scores = [
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
17 cross_product(v2 - v1, v3 - v1).z
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
18 for v1, v2, v3 in pairs(quadrilateral.geometry.vertices, count = 3)
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
19 ]
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
20 print(z_scores)
eb74680a5e43 commit work done on test suites...
Santeri Piippo
parents:
diff changeset
21 if not sign_consistency(z_scores):
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
22 yield warning(quadrilateral, 'Concave quadrilateral')
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 bowtie_quadrilateral_test(model):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
25 for quadrilateral in model.quadrilaterals:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
26 vertices = IndexRing(quadrilateral.geometry.vertices)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
27 for i in (0, 1):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
28 line1 = LineSegment(vertices[0 + i], vertices[1 + i])
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
29 line2 = LineSegment(vertices[2 + i], vertices[3 + i])
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
30 try:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
31 line_intersection(line1, line2)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
32 except NoIntersection:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
33 pass
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
34 else:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
35 yield error(quadrilateral, 'Bowtie quadrilateral')
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
36 break
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
37
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
38 def vector_angle(vec_1, vec_2, normalized = False):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
39 cosine = dot_product(vec_1, vec_2)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
40 try:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
41 cosine /= vec_1.length() * vec_2.length()
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
42 except ZeroDivisionError:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
43 return 0
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
44 angle = acos(cosine)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
45 if normalized and angle > π / 2:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
46 angle = π - angle
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
47 return angle
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
48
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
49 def skew_test(model):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
50 for quadrilateral in model.quadrilaterals:
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
51 vertices = IndexRing(quadrilateral.geometry.vertices)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
52 for i in (0, 1):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
53 a, b = vertices[0 + i], vertices[1 + i]
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
54 c, d = vertices[2 + i], vertices[3 + i]
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
55 plane_1 = cross_product(b - a, d - a)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
56 plane_2 = cross_product(d - c, b - c)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
57 angle = vector_angle(plane_1, plane_2, normalized = True)
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
58 if angle > radians(0.1):
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
59 yield error(quadrilateral,
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
60 'Skew quadrilateral (plane angle {}°)',
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
61 '%.3f' % degrees(angle))
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 12
diff changeset
62 break

mercurial