Files
ViewDesignEngine/tests/core/test_polygon.cpp
T
hm 136e154183
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
test(core): VDE-893 复核 core 模块未测试清单并补判别性测试
真实缺口:transform rotate_x/y/z·rotate·mirror·mirror_about_plane(+6);Polygon2D perimeter/is_convex/simplify(+9);exact_point_in_polyhedron(+3);simd_vector dot3/intersect4/intersect4_count/aabb_intersect_batch*(新增 test_simd_vector 4 例)。逐点/逐字段对手写公式或独立标量复算。

假阳性:add_task/cache_line_count/prefetch_array/set_aos/invalidate_soa(vde_infra)、base32_encode(vde_license)、make_temp_file/delete_file/run_command(vde_platform)非 core;decompress_brep/edgebreaker_decompress/serialize/project 已有专测。
2026-10-06 09:49:35 +08:00

232 lines
7.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <gtest/gtest.h>
#include "vde/core/polygon.h"
using namespace vde::core;
// ── signed_area tests ──
TEST(PolygonTest, SignedArea_CCWSquare_Positive) {
// Counter-clockwise unit square
Polygon2D poly({{0, 0}, {1, 0}, {1, 1}, {0, 1}});
EXPECT_GT(poly.signed_area(), 0.0);
EXPECT_NEAR(poly.signed_area(), 1.0, 1e-9);
}
TEST(PolygonTest, SignedArea_CWSquare_Negative) {
// Clockwise unit square
Polygon2D poly({{0, 0}, {0, 1}, {1, 1}, {1, 0}});
EXPECT_LT(poly.signed_area(), 0.0);
EXPECT_NEAR(poly.signed_area(), -1.0, 1e-9);
}
TEST(PolygonTest, SignedArea_Triangle) {
Polygon2D poly({{0, 0}, {3, 0}, {0, 4}});
EXPECT_NEAR(std::abs(poly.signed_area()), 6.0, 1e-9);
}
TEST(PolygonTest, SignedArea_Collinear_Degenerate) {
// All points on a line → zero area
Polygon2D poly({{0, 0}, {1, 1}, {2, 2}});
EXPECT_NEAR(poly.signed_area(), 0.0, 1e-9);
}
TEST(PolygonTest, SignedArea_Empty) {
Polygon2D poly;
EXPECT_DOUBLE_EQ(poly.signed_area(), 0.0);
}
TEST(PolygonTest, Area_AbsoluteValue) {
// area() should be absolute value of signed_area()
Polygon2D ccw({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
Polygon2D cw({{0, 0}, {0, 2}, {2, 2}, {2, 0}});
EXPECT_NEAR(ccw.area(), 4.0, 1e-9);
EXPECT_NEAR(cw.area(), 4.0, 1e-9);
}
// ── contains (point-in-polygon via ray casting) ──
TEST(PolygonTest, Contains_InteriorPoint) {
Polygon2D square({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
// Center of the square
EXPECT_TRUE(square.contains(Point2D(1.0, 1.0)));
}
TEST(PolygonTest, Contains_ExteriorPoint) {
Polygon2D square({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
// Far outside
EXPECT_FALSE(square.contains(Point2D(10.0, 10.0)));
// Outside but near
EXPECT_FALSE(square.contains(Point2D(-0.1, 1.0)));
}
TEST(PolygonTest, Contains_BoundaryPoint) {
Polygon2D square({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
// On edge — behavior may vary; ray casting treats as intersection
// Not strictly "interior" in most implementations
Point2D on_edge(1.0, 0.0);
// Just verify it doesn't crash; boundary is valid input
bool result = square.contains(on_edge);
(void)result; // implementation-defined for boundary
SUCCEED();
}
TEST(PolygonTest, Contains_PointOnVertex) {
Polygon2D tri({{0, 0}, {2, 0}, {0, 2}});
// Exact vertex — should not crash
bool result = tri.contains(Point2D(0.0, 0.0));
(void)result;
SUCCEED();
}
TEST(PolygonTest, Contains_ConcavePolygon) {
// L-shaped polygon (concave)
Polygon2D L({{0, 0}, {3, 0}, {3, 1}, {2, 1}, {2, 3}, {0, 3}});
// Point in the "notch" area (1.5, 2) should be inside
EXPECT_TRUE(L.contains(Point2D(1.0, 2.0)));
// Point in the concave void
EXPECT_FALSE(L.contains(Point2D(2.5, 2.0)));
}
// ── is_ccw ──
TEST(PolygonTest, IsCCW) {
Polygon2D ccw({{0, 0}, {1, 0}, {1, 1}, {0, 1}});
Polygon2D cw({{0, 0}, {0, 1}, {1, 1}, {1, 0}});
EXPECT_TRUE(ccw.is_ccw());
EXPECT_FALSE(cw.is_ccw());
}
// ── Basic accessors ──
TEST(PolygonTest, SizeAndEmpty) {
Polygon2D empty;
EXPECT_TRUE(empty.empty());
EXPECT_EQ(empty.size(), 0u);
Polygon2D tri({{0, 0}, {1, 0}, {0, 1}});
EXPECT_FALSE(tri.empty());
EXPECT_EQ(tri.size(), 3u);
}
TEST(PolygonTest, VerticesAccessor) {
std::vector<Point2D> v = {{0, 0}, {1, 0}, {0, 1}};
Polygon2D poly(v);
EXPECT_EQ(poly.vertices().size(), 3u);
EXPECT_DOUBLE_EQ(poly.vertices()[0].x(), 0.0);
EXPECT_DOUBLE_EQ(poly.vertices()[1].y(), 0.0);
}
TEST(PolygonTest, DefaultConstructor_Empty) {
Polygon2D poly;
EXPECT_TRUE(poly.vertices().empty());
EXPECT_TRUE(poly.empty());
}
// ── Larger polygon area (regular hexagon) ──
TEST(PolygonTest, SignedArea_RegularHexagon) {
// Regular hexagon centered at origin, radius 1
// Vertices CCW: (1,0), (0.5,0.866), (-0.5,0.866), (-1,0), (-0.5,-0.866), (0.5,-0.866)
Polygon2D hex({
{1.0, 0.0}, {0.5, std::sqrt(3.0) / 2.0},
{-0.5, std::sqrt(3.0) / 2.0}, {-1.0, 0.0},
{-0.5, -std::sqrt(3.0) / 2.0}, {0.5, -std::sqrt(3.0) / 2.0}
});
// Area of regular hexagon = (3√3 / 2) * r² ≈ 2.598
EXPECT_NEAR(hex.signed_area(), 3.0 * std::sqrt(3.0) / 2.0, 1e-9);
}
TEST(PolygonTest, Contains_StarShapedHexagon) {
Polygon2D hex({
{1.0, 0.0}, {0.5, 0.866}, {-0.5, 0.866},
{-1.0, 0.0}, {-0.5, -0.866}, {0.5, -0.866}
});
EXPECT_TRUE(hex.contains(Point2D(0.0, 0.0)));
EXPECT_FALSE(hex.contains(Point2D(2.0, 0.0)));
}
// ═══════════════════════════════════════════════════════════
// VDE-893: perimeter / is_convex / simplify 未测试公开函数补测
// ═══════════════════════════════════════════════════════════
TEST(PolygonTest, Perimeter_Square) {
// 独立复算:4 × 边长 2 = 8
Polygon2D sq({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
EXPECT_DOUBLE_EQ(sq.perimeter(), 8.0);
}
TEST(PolygonTest, Perimeter_Triangle) {
// 3-4-5 直角三角:3 + 5 + 4 = 12
Polygon2D tri({{0, 0}, {3, 0}, {0, 4}});
EXPECT_NEAR(tri.perimeter(), 12.0, 1e-12);
}
TEST(PolygonTest, Perimeter_Degenerate) {
// < 2 顶点 → 0
EXPECT_DOUBLE_EQ(Polygon2D{}.perimeter(), 0.0);
EXPECT_DOUBLE_EQ(Polygon2D({{5, 5}}).perimeter(), 0.0);
// 顶点顺序不影响周长(闭合性)
Polygon2D cw({{0, 0}, {0, 2}, {2, 2}, {2, 0}});
EXPECT_DOUBLE_EQ(cw.perimeter(), 8.0);
}
TEST(PolygonTest, IsConvex_CCWAndCW_True) {
// 凸性检测对朝向不敏感(叉积符号一致即可)
Polygon2D ccw({{0, 0}, {2, 0}, {2, 2}, {0, 2}});
Polygon2D cw({{0, 0}, {0, 2}, {2, 2}, {2, 0}});
EXPECT_TRUE(ccw.is_convex());
EXPECT_TRUE(cw.is_convex());
// 正六边形(CCW)
Polygon2D hex({
{1.0, 0.0}, {0.5, 0.8660254037844386}, {-0.5, 0.8660254037844386},
{-1.0, 0.0}, {-0.5, -0.8660254037844386}, {0.5, -0.8660254037844386}
});
EXPECT_TRUE(hex.is_convex());
}
TEST(PolygonTest, IsConvex_Concave_False) {
// L 形凹多边形(i=2 顶点处转向相反)
Polygon2D L({{0, 0}, {3, 0}, {3, 1}, {2, 1}, {2, 3}, {0, 3}});
EXPECT_FALSE(L.is_convex());
}
TEST(PolygonTest, IsConvex_Degenerate_False) {
EXPECT_FALSE(Polygon2D{}.is_convex());
EXPECT_FALSE(Polygon2D({{0, 0}}).is_convex());
EXPECT_FALSE(Polygon2D({{0, 0}, {1, 0}}).is_convex());
}
TEST(PolygonTest, Simplify_RemovesCollinearVertex) {
// 底边中点 (1,0) 共线,应被 Douglas-Peucker 删除;保留 (0,0) 与末端 (0,2)
Polygon2D poly({{0, 0}, {1, 0}, {2, 0}, {2, 2}, {0, 2}});
Polygon2D s = poly.simplify(0.1);
ASSERT_EQ(s.size(), 4u);
EXPECT_DOUBLE_EQ(s.vertices()[0].x(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices()[0].y(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices()[1].x(), 2.0);
EXPECT_DOUBLE_EQ(s.vertices()[1].y(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices()[2].x(), 2.0);
EXPECT_DOUBLE_EQ(s.vertices()[2].y(), 2.0);
EXPECT_DOUBLE_EQ(s.vertices()[3].x(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices()[3].y(), 2.0);
}
TEST(PolygonTest, Simplify_LargeToleranceKeepsEndpointsOnly) {
Polygon2D poly({{0, 0}, {1, 0}, {2, 0}, {2, 2}, {0, 2}});
Polygon2D s = poly.simplify(100.0);
ASSERT_EQ(s.size(), 2u);
EXPECT_DOUBLE_EQ(s.vertices().front().x(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices().front().y(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices().back().x(), 0.0);
EXPECT_DOUBLE_EQ(s.vertices().back().y(), 2.0);
}
TEST(PolygonTest, Simplify_SmallPolygonUnchanged) {
// < 3 顶点:原样返回
Polygon2D line({{0, 0}, {5, 5}});
EXPECT_EQ(line.simplify(1e-9).size(), 2u);
// 三角形:三点均非共线,tolerance 很小时全部保留
Polygon2D tri({{0, 0}, {1, 0}, {0, 1}});
EXPECT_EQ(tri.simplify(1e-9).size(), 3u);
}