Sun, 21 Jan 2018 15:03:38 +0200
Got the skew test working
13 | 1 | from math import acos, degrees, radians, pi as π |
2 | from testsuite import warning, error | |
12 | 3 | from geometry import * |
4 | ||
5 | def sign_consistency(container): | |
6 | # Returns whether all elements in container have the same sign | |
7 | return min(container) * max(container) >= 0 | |
8 | ||
9 | def transform_to_xy(geometry): | |
10 | a, b, c = geometry.vertices[:3] | |
11 | ||
12 | ||
13 | def concave_test(model): | |
13 | 14 | for quadrilateral in model.quadrilaterals: |
12 | 15 | print([cross_product(v2 - v1, v3 - v1) for v1, v2, v3 in pairs(quadrilateral.geometry.vertices, count = 3)]) |
16 | z_scores = [ | |
17 | cross_product(v2 - v1, v3 - v1).z | |
18 | for v1, v2, v3 in pairs(quadrilateral.geometry.vertices, count = 3) | |
19 | ] | |
20 | print(z_scores) | |
21 | if not sign_consistency(z_scores): | |
13 | 22 | yield warning(quadrilateral, 'Concave quadrilateral') |
23 | ||
24 | def bowtie_quadrilateral_test(model): | |
25 | for quadrilateral in model.quadrilaterals: | |
26 | vertices = IndexRing(quadrilateral.geometry.vertices) | |
27 | for i in (0, 1): | |
28 | line1 = LineSegment(vertices[0 + i], vertices[1 + i]) | |
29 | line2 = LineSegment(vertices[2 + i], vertices[3 + i]) | |
30 | try: | |
31 | line_intersection(line1, line2) | |
32 | except NoIntersection: | |
33 | pass | |
34 | else: | |
35 | yield error(quadrilateral, 'Bowtie quadrilateral') | |
36 | break | |
37 | ||
38 | def vector_angle(vec_1, vec_2, normalized = False): | |
39 | cosine = dot_product(vec_1, vec_2) | |
40 | try: | |
41 | cosine /= vec_1.length() * vec_2.length() | |
42 | except ZeroDivisionError: | |
43 | return 0 | |
44 | angle = acos(cosine) | |
45 | if normalized and angle > π / 2: | |
46 | angle = π - angle | |
47 | return angle | |
48 | ||
49 | def skew_test(model): | |
50 | for quadrilateral in model.quadrilaterals: | |
51 | vertices = IndexRing(quadrilateral.geometry.vertices) | |
52 | for i in (0, 1): | |
53 | a, b = vertices[0 + i], vertices[1 + i] | |
54 | c, d = vertices[2 + i], vertices[3 + i] | |
55 | plane_1 = cross_product(b - a, d - a) | |
56 | plane_2 = cross_product(d - c, b - c) | |
57 | angle = vector_angle(plane_1, plane_2, normalized = True) | |
58 | if angle > radians(0.1): | |
59 | yield error(quadrilateral, | |
60 | 'Skew quadrilateral (plane angle {}°)', | |
61 | '%.3f' % degrees(angle)) | |
62 | break |