新建 vde::sketch 草图几何引擎 src/sketch/sketch_geometry.h/.cpp 并编入 vde_sketch: - detect_loops:半边界(half-edge)推进枚举闭合环 + 开放链,回填 nested/parent_loop - detect_self_intersections:折线求交;相邻段共享端点处的接触忽略 - analyze_nesting:射线法点在多边形内,取最小面积包含环为直接父环 - infer_constraints:水平/垂直/平行/垂直/等长/等半径/同心/相切/重合推断 - analyze_dof:Line/Spline=4、Arc=5,按约束类型累计方程数,输出恰定/过约束 (SketchProfileTools 为 ViewDesign 应用层名,非引擎 API) test_sketch_geometry 16/16、test_constraint_solver 30/30、test_sketch_ops 11/11 全绿
368 lines
14 KiB
C++
368 lines
14 KiB
C++
#include "sketch/sketch_geometry.h"
|
||
#include <gtest/gtest.h>
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <set>
|
||
|
||
using namespace vde::sketch;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 辅助:构造段 / 独立复算工具
|
||
// ═══════════════════════════════════════════════════════════
|
||
static SketchSegment make_line(int id, double x0, double y0, double x1, double y1) {
|
||
SketchSegment s;
|
||
s.id = id;
|
||
s.type = SketchSegment::Line;
|
||
s.endpoints[0] = Point2D(x0, y0);
|
||
s.endpoints[1] = Point2D(x1, y1);
|
||
return s;
|
||
}
|
||
|
||
static SketchSegment make_arc(int id, double cx, double cy, double r, double a0,
|
||
double a1) {
|
||
SketchSegment s;
|
||
s.id = id;
|
||
s.type = SketchSegment::Arc;
|
||
s.center = Point2D(cx, cy);
|
||
s.radius = r;
|
||
s.endpoints[0] = Point2D(cx + r * std::cos(a0), cy + r * std::sin(a0));
|
||
s.endpoints[1] = Point2D(cx + r * std::cos(a1), cy + r * std::sin(a1));
|
||
return s;
|
||
}
|
||
|
||
/// 测试侧独立鞋带面积(与实现无关,用于交叉验证 signed_area)
|
||
static double area_of(const std::vector<Point2D>& p) {
|
||
const size_t n = p.size();
|
||
if (n < 3) return 0.0;
|
||
double a = 0.0;
|
||
for (size_t i = 0; i < n; ++i) {
|
||
const Point2D& q = p[i];
|
||
const Point2D& r = p[(i + 1) % n];
|
||
a += q.x() * r.y() - r.x() * q.y();
|
||
}
|
||
return 0.5 * a;
|
||
}
|
||
|
||
/// 取某类型约束条数
|
||
static int count_type(const std::vector<Constraint>& cs, ConstraintType t) {
|
||
int n = 0;
|
||
for (const auto& c : cs)
|
||
if (c.type == t) ++n;
|
||
return n;
|
||
}
|
||
|
||
/// 是否存在指定类型的约束且元素完全匹配(忽略顺序)
|
||
static bool has_constraint(const std::vector<Constraint>& cs, ConstraintType t,
|
||
std::vector<int> elems) {
|
||
std::sort(elems.begin(), elems.end());
|
||
for (const auto& c : cs) {
|
||
if (c.type != t) continue;
|
||
std::vector<int> e = c.elements;
|
||
std::sort(e.begin(), e.end());
|
||
if (e == elems) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 环检测
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(SketchGeometryTest, DetectLoops_ClosedSquare) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 10, 0, 10, 10),
|
||
make_line(2, 10, 10, 0, 10),
|
||
make_line(3, 0, 10, 0, 0),
|
||
};
|
||
auto loops = detect_loops(segs);
|
||
ASSERT_EQ(loops.size(), 1u);
|
||
EXPECT_TRUE(loops[0].closed);
|
||
EXPECT_FALSE(loops[0].nested);
|
||
EXPECT_EQ(loops[0].parent_loop, -1);
|
||
|
||
// 段集合独立校验
|
||
std::set<int> ids(loops[0].segment_ids.begin(), loops[0].segment_ids.end());
|
||
EXPECT_EQ(ids, (std::set<int>{0, 1, 2, 3}));
|
||
EXPECT_EQ(loops[0].segment_ids.size(), 4u);
|
||
|
||
// 边界为 4 个角点,逐点校验
|
||
ASSERT_EQ(loops[0].boundary.size(), 4u);
|
||
std::set<std::pair<double, double>> corners;
|
||
for (const auto& p : loops[0].boundary)
|
||
corners.insert({std::round(p.x()), std::round(p.y())});
|
||
EXPECT_EQ(corners, (std::set<std::pair<double, double>>{
|
||
{0, 0}, {10, 0}, {10, 10}, {0, 10}}));
|
||
|
||
// 有向面积 = +100(CCW),与测试侧独立鞋带复算一致
|
||
EXPECT_NEAR(loops[0].signed_area, 100.0, 1e-9);
|
||
EXPECT_NEAR(loops[0].signed_area, area_of(loops[0].boundary), 1e-9);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, DetectLoops_OpenChain) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 10, 0, 10, 10),
|
||
};
|
||
auto loops = detect_loops(segs);
|
||
ASSERT_EQ(loops.size(), 1u);
|
||
EXPECT_FALSE(loops[0].closed);
|
||
EXPECT_DOUBLE_EQ(loops[0].signed_area, 0.0);
|
||
std::set<int> ids(loops[0].segment_ids.begin(), loops[0].segment_ids.end());
|
||
EXPECT_EQ(ids, (std::set<int>{0, 1}));
|
||
EXPECT_EQ(loops[0].boundary.size(), 3u);
|
||
EXPECT_NEAR(loops[0].boundary.front().x(), 0.0, 1e-12);
|
||
EXPECT_NEAR(loops[0].boundary.back().y(), 10.0, 1e-12);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, DetectLoops_TwoArcsFormCircle) {
|
||
// 两个半圆弧首尾相接 → 一个闭合圆
|
||
std::vector<SketchSegment> segs = {
|
||
make_arc(0, 0, 0, 2.0, 0.0, M_PI),
|
||
make_arc(1, 0, 0, 2.0, M_PI, 2.0 * M_PI),
|
||
};
|
||
auto loops = detect_loops(segs);
|
||
ASSERT_EQ(loops.size(), 1u);
|
||
EXPECT_TRUE(loops[0].closed);
|
||
EXPECT_EQ(loops[0].segment_ids.size(), 2u);
|
||
const double expect = M_PI * 2.0 * 2.0;
|
||
EXPECT_NEAR(loops[0].signed_area, expect, 0.05); // 采样折线近似
|
||
EXPECT_NEAR(loops[0].signed_area, area_of(loops[0].boundary), 1e-9);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, DetectLoops_SelfClosedArc) {
|
||
// 单条整圆(端点重合)→ 自闭合环
|
||
SketchSegment c;
|
||
c.id = 0;
|
||
c.type = SketchSegment::Arc;
|
||
c.center = Point2D(1.0, 1.0);
|
||
c.radius = 3.0;
|
||
c.endpoints[0] = Point2D(4.0, 1.0);
|
||
c.endpoints[1] = Point2D(4.0, 1.0);
|
||
auto loops = detect_loops({c});
|
||
ASSERT_EQ(loops.size(), 1u);
|
||
EXPECT_TRUE(loops[0].closed);
|
||
EXPECT_EQ(loops[0].segment_ids, (std::vector<int>{0}));
|
||
EXPECT_NEAR(loops[0].signed_area, M_PI * 9.0, 0.1);
|
||
EXPECT_NEAR(loops[0].signed_area, area_of(loops[0].boundary), 1e-9);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, DetectLoops_NestedSquares) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 20, 0), make_line(1, 20, 0, 20, 20),
|
||
make_line(2, 20, 20, 0, 20), make_line(3, 0, 20, 0, 0),
|
||
make_line(4, 5, 5, 10, 5), make_line(5, 10, 5, 10, 10),
|
||
make_line(6, 10, 10, 5, 10), make_line(7, 5, 10, 5, 5),
|
||
};
|
||
auto loops = detect_loops(segs);
|
||
ASSERT_EQ(loops.size(), 2u);
|
||
|
||
int outer = -1, inner = -1;
|
||
for (int i = 0; i < 2; ++i) {
|
||
if (std::fabs(loops[i].signed_area) > 100.0) outer = i;
|
||
else inner = i;
|
||
}
|
||
ASSERT_GE(outer, 0);
|
||
ASSERT_GE(inner, 0);
|
||
EXPECT_NEAR(loops[outer].signed_area, 400.0, 1e-9);
|
||
EXPECT_NEAR(loops[inner].signed_area, 25.0, 1e-9);
|
||
EXPECT_FALSE(loops[outer].nested);
|
||
EXPECT_TRUE(loops[inner].nested);
|
||
EXPECT_EQ(loops[inner].parent_loop, outer);
|
||
|
||
// 直接调用嵌套分析:父 -> 子
|
||
auto nesting = analyze_nesting(loops);
|
||
ASSERT_EQ(nesting.size(), 1u);
|
||
ASSERT_TRUE(nesting.count(outer));
|
||
EXPECT_EQ(nesting[outer], (std::vector<int>{inner}));
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 自交检测
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(SketchGeometryTest, SelfIntersection_CrossingLines) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, -5, 0, 5, 0),
|
||
make_line(1, 0, -5, 0, 5),
|
||
};
|
||
auto pairs = detect_self_intersections(segs);
|
||
ASSERT_EQ(pairs.size(), 1u);
|
||
EXPECT_EQ(pairs[0], std::make_pair(0, 1));
|
||
}
|
||
|
||
TEST(SketchGeometryTest, SelfIntersection_SharedEndpointNotReported) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 0, 0, 5, 5), // 与 seg0 共享 (0,0)
|
||
};
|
||
EXPECT_TRUE(detect_self_intersections(segs).empty());
|
||
}
|
||
|
||
TEST(SketchGeometryTest, SelfIntersection_ClosedSquareClean) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 10, 0, 10, 10),
|
||
make_line(2, 10, 10, 0, 10),
|
||
make_line(3, 0, 10, 0, 0),
|
||
};
|
||
EXPECT_TRUE(detect_self_intersections(segs).empty());
|
||
}
|
||
|
||
TEST(SketchGeometryTest, SelfIntersection_TJunctionReported) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 5, 0, 5, 5), // 端点 (5,0) 落在 seg0 内部
|
||
};
|
||
auto pairs = detect_self_intersections(segs);
|
||
ASSERT_EQ(pairs.size(), 1u);
|
||
EXPECT_EQ(pairs[0], std::make_pair(0, 1));
|
||
}
|
||
|
||
TEST(SketchGeometryTest, SelfIntersection_LineCrossesArc) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 3, -10, 3, 10),
|
||
make_arc(1, 0, 0, 5.0, 0.0, M_PI), // 上半圆
|
||
};
|
||
auto pairs = detect_self_intersections(segs);
|
||
ASSERT_EQ(pairs.size(), 1u);
|
||
EXPECT_EQ(pairs[0], std::make_pair(0, 1));
|
||
|
||
// 不接触的情形
|
||
std::vector<SketchSegment> miss = {
|
||
make_line(0, 10, -10, 10, 10),
|
||
make_arc(1, 0, 0, 5.0, 0.0, M_PI),
|
||
};
|
||
EXPECT_TRUE(detect_self_intersections(miss).empty());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 自动约束推断
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(SketchGeometryTest, InferConstraints_Rectangle) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, 0, 0, 10, 0),
|
||
make_line(1, 10, 0, 10, 5),
|
||
make_line(2, 10, 5, 0, 5),
|
||
make_line(3, 0, 5, 0, 0),
|
||
};
|
||
auto cs = infer_constraints(segs);
|
||
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Horizontal), 2);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Vertical), 2);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Parallel), 2);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Perpendicular), 4);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::EqualLength), 2);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Coincident), 4);
|
||
|
||
// 具体成员独立校验
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Horizontal, {0}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Horizontal, {2}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Vertical, {1}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Vertical, {3}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Parallel, {0, 2}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Parallel, {1, 3}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Perpendicular, {0, 1}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::EqualLength, {0, 2}));
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::EqualLength, {1, 3}));
|
||
// 角点 (0,0): seg0.end0 与 seg3.end1
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Coincident, {0, 7}));
|
||
|
||
// 总条数(矩形 = 4 单段 + 8 线对 + 4 重合)
|
||
EXPECT_EQ(static_cast<int>(cs.size()), 16);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, InferConstraints_AngleTolerance) {
|
||
// 倾斜 0.005 rad(<1°)仍判水平
|
||
std::vector<SketchSegment> near = {
|
||
make_line(0, 0, 0, 10, 10 * std::tan(0.005)),
|
||
};
|
||
EXPECT_EQ(count_type(infer_constraints(near), ConstraintType::Horizontal), 1);
|
||
|
||
// 倾斜 0.1 rad(>1°)不判水平
|
||
std::vector<SketchSegment> far = {
|
||
make_line(0, 0, 0, 10, 10 * std::tan(0.1)),
|
||
};
|
||
auto cs = infer_constraints(far);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Horizontal), 0);
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Vertical), 0);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, InferConstraints_ArcTangentAndConcentric) {
|
||
std::vector<SketchSegment> segs = {
|
||
make_line(0, -10, 0, 10, 0),
|
||
make_arc(1, 0, 5, 5.0, 0.0, 2.0 * M_PI), // 与 y=0 相切于 (0,0)
|
||
make_arc(2, 0, 5, 3.0, 0.0, M_PI), // 与 arc1 同心
|
||
};
|
||
auto cs = infer_constraints(segs);
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Tangent, {0, 1}));
|
||
EXPECT_EQ(count_type(cs, ConstraintType::Concentric), 1);
|
||
EXPECT_TRUE(has_constraint(cs, ConstraintType::Concentric, {1, 2}));
|
||
// 半径不同 → 无相等半径
|
||
EXPECT_EQ(count_type(cs, ConstraintType::EqualRadius), 0);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 自由度分析
|
||
// ═══════════════════════════════════════════════════════════
|
||
TEST(SketchGeometryTest, AnalyzeDof_LineUnderConstrained) {
|
||
std::vector<SketchSegment> segs = {make_line(0, 0, 0, 10, 0)};
|
||
auto r = analyze_dof(segs, {});
|
||
EXPECT_EQ(r.total_dof, 4);
|
||
EXPECT_EQ(r.constraint_equations, 0);
|
||
EXPECT_EQ(r.remaining_dof, 4);
|
||
EXPECT_FALSE(r.fully_constrained);
|
||
EXPECT_FALSE(r.over_constrained);
|
||
ASSERT_EQ(r.free_segment_ids.size(), 1u);
|
||
EXPECT_EQ(r.free_segment_ids[0], 0);
|
||
EXPECT_EQ(static_cast<int>(r.recommendations.size()), 2); // 总建议 + 单段提示
|
||
}
|
||
|
||
TEST(SketchGeometryTest, AnalyzeDof_LineFullyAndOver) {
|
||
std::vector<SketchSegment> segs = {make_line(0, 0, 0, 10, 0)};
|
||
auto mk = [](ConstraintType t, std::vector<int> e) {
|
||
Constraint c;
|
||
c.type = t;
|
||
c.elements = std::move(e);
|
||
return c;
|
||
};
|
||
// Fixed(2) + Horizontal(1) + Distance(1) = 4 → 恰定
|
||
std::vector<Constraint> cs = {
|
||
mk(ConstraintType::Fixed, {0}),
|
||
mk(ConstraintType::Horizontal, {0}),
|
||
mk(ConstraintType::Distance, {0}),
|
||
};
|
||
auto r = analyze_dof(segs, cs);
|
||
EXPECT_EQ(r.constraint_equations, 4);
|
||
EXPECT_EQ(r.remaining_dof, 0);
|
||
EXPECT_TRUE(r.fully_constrained);
|
||
EXPECT_FALSE(r.over_constrained);
|
||
EXPECT_TRUE(r.free_segment_ids.empty());
|
||
|
||
cs.push_back(mk(ConstraintType::Fixed, {0})); // 再 +2 → 过约束
|
||
auto r2 = analyze_dof(segs, cs);
|
||
EXPECT_EQ(r2.constraint_equations, 6);
|
||
EXPECT_EQ(r2.remaining_dof, -2);
|
||
EXPECT_TRUE(r2.over_constrained);
|
||
EXPECT_FALSE(r2.fully_constrained);
|
||
}
|
||
|
||
TEST(SketchGeometryTest, AnalyzeDof_ArcDof) {
|
||
std::vector<SketchSegment> segs = {make_arc(0, 0, 0, 5.0, 0.0, M_PI)};
|
||
auto r = analyze_dof(segs, {});
|
||
EXPECT_EQ(r.total_dof, 5);
|
||
EXPECT_EQ(r.remaining_dof, 5);
|
||
// 通过合成点 id(2*id)引用段,也识别为已约束
|
||
Constraint c;
|
||
c.type = ConstraintType::Radius;
|
||
c.elements = {0};
|
||
auto r2 = analyze_dof(segs, {c});
|
||
EXPECT_EQ(r2.constraint_equations, 1);
|
||
EXPECT_TRUE(r2.free_segment_ids.empty());
|
||
|
||
Constraint coinc;
|
||
coinc.type = ConstraintType::Coincident;
|
||
coinc.elements = {0, 1}; // 点 0 = 段0 的端点0
|
||
auto r3 = analyze_dof(segs, {coinc});
|
||
EXPECT_EQ(r3.constraint_equations, 2);
|
||
EXPECT_TRUE(r3.free_segment_ids.empty());
|
||
}
|