test_Contour.cpp 4.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "test_Sketch.hpp"

TEST(Contour, Triangle_CCW) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(1.0, 0.0);
    auto v1 = s.new_element<Vertex>(1.0, 0.0);
    auto v2 = s.new_element<Vertex>(0.0, 1.0);

    auto l0 = s.new_element<LineSegment>(v0, v1);
    auto l1 = s.new_element<LineSegment>(v1, v2);
    auto l2 = s.new_element<LineSegment>(v2, v0);

    std::vector<std::shared_ptr<Curve const>> vc{l0,l1,l2};

    Contour cont{vc};

    EXPECT_TRUE(cont.curve(0) == l0);
    EXPECT_TRUE(cont.curve(1) == l1);
    EXPECT_TRUE(cont.curve(2) == l2);

    EXPECT_TRUE(cont.vertex(0) == v0);
    EXPECT_TRUE(cont.vertex(1) == v1);
    EXPECT_TRUE(cont.vertex(2) == v2);

    EXPECT_TRUE(cont == cont);
}

TEST(Contour, Triangle_CW) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(1.0, 0.0);
    auto v1 = s.new_element<Vertex>(1.0, 0.0);
    auto v2 = s.new_element<Vertex>(0.0, 1.0);

    auto l0 = s.new_element<LineSegment>(v1, v0);
    auto l1 = s.new_element<LineSegment>(v2, v1);
    auto l2 = s.new_element<LineSegment>(v0, v2);

    std::vector<std::shared_ptr<Curve const>> vc{l0,l1,l2};

    Contour cont{vc};

    EXPECT_TRUE(cont.curve(0) == l0);
    EXPECT_TRUE(cont.curve(1) == l2);
    EXPECT_TRUE(cont.curve(2) == l1);

    EXPECT_TRUE(cont.vertex(0) == v1);
    EXPECT_TRUE(cont.vertex(1) == v0);
    EXPECT_TRUE(cont.vertex(2) == v2);

    EXPECT_TRUE(cont == cont);
}

55
TEST(Contour, DartTriangle) {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    Sketch s;

    auto v0 = s.new_element<Vertex>(1.0, 0.0);
    auto v1 = s.new_element<Vertex>(1.0, 0.0);
    auto v2 = s.new_element<Vertex>(0.0, 1.0);

    auto l0 = s.new_element<LineSegment>(v1, v0);
    auto l1 = s.new_element<LineSegment>(v1, v2);
    auto l2 = s.new_element<LineSegment>(v2, v0);

    std::vector<std::shared_ptr<Curve const>> vc{l2,l0,l1};

    Contour cont{vc};

    EXPECT_TRUE(cont.curve(0) == l2);
    EXPECT_TRUE(cont.curve(1) == l0);
    EXPECT_TRUE(cont.curve(2) == l1);

    EXPECT_TRUE(cont.vertex(0) == v2);
    EXPECT_TRUE(cont.vertex(1) == v0);
    EXPECT_TRUE(cont.vertex(2) == v1);

    EXPECT_TRUE(cont == cont);
}

TEST(Contour, Nonclosed_Failure) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(0.0, 0.0);
    auto v1 = s.new_element<Vertex>(1.0, 1.0);

    auto l0 = s.new_element<LineSegment>(v0, v1);

    std::vector<std::shared_ptr<Curve const>> c{l0};

    EXPECT_ANY_THROW(Contour cont{c}); // Construction should fail since the contour is not closed
}

TEST(Contour, Disjoint_Failure) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(0.0, 0.0);
    auto v1 = s.new_element<Vertex>(1.0, 1.0);
    auto v2 = s.new_element<Vertex>(0.0, 1.0);
    auto v3 = s.new_element<Vertex>(2.0, 2.0);
    auto v4 = s.new_element<Vertex>(3.0, 3.0);
    auto v5 = s.new_element<Vertex>(2.0, 3.0);

    auto l0 = s.new_element<LineSegment>(v0, v1);
    auto l1 = s.new_element<LineSegment>(v1, v2);
    auto l2 = s.new_element<LineSegment>(v2, v0);
    auto l3 = s.new_element<LineSegment>(v3, v4);
    auto l4 = s.new_element<LineSegment>(v4, v5);
    auto l5 = s.new_element<LineSegment>(v5, v3);

    std::vector<std::shared_ptr<Curve const>> c{l0,l1,l2,l3,l4,l5};

    EXPECT_ANY_THROW(Contour cont{c}); // Construction should fail since contour is not simple
}

TEST(Contour, Implicit_Self_Intersection_Failure) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(1.0, 1.0);
    auto v1 = s.new_element<Vertex>(-1.0, -1.0);
    auto v2 = s.new_element<Vertex>(-1.0, 1.0);
    auto v3 = s.new_element<Vertex>(1.0, -1.0);

    auto l0 = s.new_element<LineSegment>(v0, v1);
    auto l1 = s.new_element<LineSegment>(v1, v2);
    auto l2 = s.new_element<LineSegment>(v2, v3);
    auto l3 = s.new_element<LineSegment>(v3, v0);

    std::vector<std::shared_ptr<Curve const>> c{l0,l1,l2,l3};

    EXPECT_ANY_THROW(Contour cont{c}); // Construction should fail since contour is self intersecting

    /*
        TODO: Use nurbs representation to approximate potential intersection point.
        TODO: Use newton's method to solve intersection problem with the generated initial guesses.
    */
}

TEST(Contour, Explicit_Self_Intersection_Failure) {
    Sketch s;

    auto v0 = s.new_element<Vertex>(1.0, 1.0);
    auto v1 = s.new_element<Vertex>(-1.0, -1.0);
    auto v2 = s.new_element<Vertex>(-1.0, 1.0);
    auto v3 = s.new_element<Vertex>(1.0, -1.0);
    auto vc = s.new_element<Vertex>(0.0, 0.0);

    auto l0 = s.new_element<LineSegment>(v0, vc);
    auto l1 = s.new_element<LineSegment>(vc, v1);
    auto l2 = s.new_element<LineSegment>(v1, v2);
    auto l3 = s.new_element<LineSegment>(v2, vc);
    auto l4 = s.new_element<LineSegment>(vc, v3);
    auto l5 = s.new_element<LineSegment>(v3, v0);

    std::vector<std::shared_ptr<Curve const>> c{l0,l1,l2,l3,l4,l5};

    EXPECT_ANY_THROW(Contour cont{c}); // Construction should fail since contour is self intersecting
}