GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
math_line_segment2_test.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_MATH_LINE_SEGMENT2_TEST_H_
9#define DESIGNLAB_MATH_LINE_SEGMENT2_TEST_H_
10
11#include <doctest.h>
12
13#include "math_line_segment2.h"
14
15
16TEST_SUITE("LineSegment2")
17{
20
21
22 TEST_CASE("ConstructorTest_ShouldZeroForStartAndEnd")
23 {
24 // デフォルトコンストラクタで初期化した場合,start と end が 0 で初期化される.
25 const LineSegment2 line;
26
27 CHECK_EQ(line.start, Vector2(0.0f, 0.0f));
28 CHECK_EQ(line.end, Vector2(0.0f, 0.0f));
29 }
30
31 TEST_CASE("ConstructorTest_ShouldSetStartAndEnd")
32 {
33 SUBCASE("Vector2")
34 {
35 const LineSegment2 line(Vector2(1.0f, 2.0f), Vector2(3.0f, 4.0f));
36
37 CHECK_EQ(line.start, Vector2(1.0f, 2.0f));
38 CHECK_EQ(line.end, Vector2(3.0f, 4.0f));
39 }
40
41 SUBCASE("float")
42 {
43 const LineSegment2 line(1.0f, 2.0f, 3.0f, 4.0f);
44
45 CHECK_EQ(line.start, Vector2(1.0f, 2.0f));
46 CHECK_EQ(line.end, Vector2(3.0f, 4.0f));
47 }
48 }
49
50 TEST_CASE("IsParallelTest_WhenParallel_ReturnTrue")
51 {
52 struct { LineSegment2 line1; LineSegment2 line2; }
53 test_table[] = {
54 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 1.0f, 1.0f, 2.0f) },
55 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(1.0f, 1.0f, 2.0f, 2.0f) },
56 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(-1.0f, -1.0f, 0.0f, 0.0f) },
57 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(-2.0f, -2.0f, -1.0f, -1.0f) },
58 };
59
60 for (const auto& t : test_table)
61 {
62 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
63 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
64 CHECK(t.line1.IsParallel(t.line2));
65 }
66 }
67
68 TEST_CASE("IsParallelTest_WhenNotParallel_ReturnFalse")
69 {
70 struct { LineSegment2 line1; LineSegment2 line2; }
71 test_table[] = {
72 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 1.0f, 1.0f, 0.0f) },
73 { LineSegment2(0.0f, 0.0f, 1.0f, 0.0f), LineSegment2(0.0f, 0.0f, 0.0f, 1.0f) },
74 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 0.0f, 1.0f, 2.0f) },
75 };
76
77 for (const auto& t : test_table)
78 {
79 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
80 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
81 CHECK_FALSE(t.line1.IsParallel(t.line2));
82 }
83 }
84
85 TEST_CASE("GetLengthTest")
86 {
87 struct { LineSegment2 line; float expected; }
88 test_table[] = {
89 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), std::sqrt(2.0f) },
90 { LineSegment2(0.0f, 0.0f, 2.0f, -2.0f), std::sqrt(8.0f) },
91 { LineSegment2(0.0f, 0.0f, -4.0f, 4.0f), std::sqrt(32.0f) },
92 { LineSegment2(2.0f, -3.0f, 7.0f, 2.0f), std::sqrt(50.0f) },
93 { LineSegment2(10.0f, 10.0f, 13.0f, 13.0f), std::sqrt(18.0f) },
94 };
95
96 for (const auto& t : test_table)
97 {
98 const auto length = t.line.GetLength();
99
100 INFO("line: " << t.line.start << ", " << t.line.end);
101
102 CHECK_EQ(length, t.expected);
103 }
104 }
105
106 TEST_CASE("GetIntersectionTest_WhenHasIntersection_ReturnIntersection")
107 {
108 struct { LineSegment2 line1; LineSegment2 line2; Vector2 expected; }
109 test_table[] = {
110 { LineSegment2(0.f, 0.f, 1.f, 1.f), LineSegment2(0.f, 1.f, 1.f, 0.f), Vector2(0.5f, 0.5f) },
111 { LineSegment2(1.f, 0.f, 1.f, 1.f), LineSegment2(0.f, 1.f, 1.f, 1.f), Vector2(1.0f, 1.f) },
112 { LineSegment2(0.f, 0.f, 1.f, 3.f), LineSegment2(0.f, 3.f, 1.f, 0.f), Vector2(0.5f, 1.5f) },
113 { LineSegment2(0.f, 0.f, 2.f, 2.f), LineSegment2(0.f, 2.f, 2.f, 0.f), Vector2(1.0f, 1.0f) },
114 };
115
116 for (const auto& t : test_table)
117 {
118 const auto intersection = t.line1.GetIntersection(t.line2);
119
120 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
121 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
122
123 CHECK_EQ(intersection, t.expected);
124 }
125 }
126
127 TEST_CASE("GetIntersectionTest_WhenNotHasIntersection_ReturnZeroVector")
128 {
129 struct { LineSegment2 line1; LineSegment2 line2; }
130 test_table[] = {
131 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(1.0f, 0.0f, 2.0f, 1.0f) },
132 { LineSegment2(1.0f, 0.0f, 1.0f, 1.0f), LineSegment2(2.0f, 0.0f, 2.0f, 1.0f) },
133 { LineSegment2(1.0f, 0.0f, 0.0f, 1.0f), LineSegment2(0.0f, 0.0f, 0.4f, 0.4f) },
134 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(-2.0f, -2.0f, -3.0f, -3.0f) },
135 };
136
137 for (const auto& t : test_table)
138 {
139 const auto intersection = t.line1.GetIntersection(t.line2);
140
141 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
142 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
143
144 CHECK_EQ(intersection, Vector2(0.0f, 0.0f));
145 }
146 }
147
148 TEST_CASE("GetIntersectionTest_WhenParallel_ReturnZeroVector")
149 {
150 struct { LineSegment2 line1; LineSegment2 line2; }
151 test_table[] = {
152 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 1.0f, 1.0f, 2.0f) },
153 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(1.0f, 1.0f, 2.0f, 2.0f) },
154 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(-1.0f, -1.0f, 0.0f, 0.0f) },
155 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(-2.0f, -2.0f, -1.0f, -1.0f) },
156 };
157
158 for (const auto& t : test_table)
159 {
160 const auto intersection = t.line1.GetIntersection(t.line2);
161
162 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
163 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
164
165 CHECK_EQ(intersection, Vector2(0.0f, 0.0f));
166 }
167 }
168
169 TEST_CASE("HasIntersectionTest_WhenHasIntersection_ReturnTrue")
170 {
171 struct { LineSegment2 line1; LineSegment2 line2; }
172 test_table[] = {
173 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 1.0f, 1.0f, 0.0f) },
174 { LineSegment2(0.0f, 0.0f, 1.0f, 0.0f), LineSegment2(0.0f, 0.0f, 0.0f, 1.0f) },
175 { LineSegment2(0.0f, 0.0f, 1.0f, 1.0f), LineSegment2(0.0f, 0.0f, 1.0f, 2.0f) },
176 { LineSegment2(0.0f, 0.0f, 2.0f, 2.0f), LineSegment2(0.0f, 2.0f, 2.0f, 0.0f) },
177 };
178
179 for (const auto& t : test_table)
180 {
181 INFO("line1 start : " << t.line1.start << ", end : " << t.line1.end);
182 INFO("line2 start : " << t.line2.start << ", end : " << t.line2.end);
183
184 CHECK(t.line1.HasIntersection(t.line2));
185 }
186 }
187}
188
189
190#endif // DESIGNLAB_MATH_LINE_SEGMENT2_TEST_H_
TEST_SUITE("LineSegment2")
2次元の線分を表す構造体.
2次元の位置ベクトルを表す構造体.