ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
constraint_solver.h
浏览该文件的文档.
1 #pragma once
2 #include "vde/core/point.h"
3 #include <vector>
4 #include <string>
5 #include <unordered_map>
6 
7 namespace vde::sketch {
8 using core::Point2D;
9 using core::Vector2D;
10 
15 struct SketchPoint {
16  int id;
18  bool fixed = false;
19 };
20 
25 struct SketchLine {
26  int id;
27  int p0, p1;
28 };
29 
34 struct SketchCircle {
35  int id;
36  int center;
37  double radius;
38 };
39 
49 enum class ConstraintType {
50  Horizontal,
51  Vertical,
52  Parallel,
54  Coincident,
55  EqualLength,
56  Distance,
57  Radius,
58  Tangent,
59  Concentric,
60  FixedPoint,
61  Fixed,
62  Angle,
63 };
64 
69 struct Constraint {
71  std::vector<int> elements;
72  double value = 0.0;
73 };
74 
79 struct SolverResult {
80  bool converged = false;
81  int iterations = 0;
82  double residual = 0.0;
83  std::vector<Point2D> points;
84  std::string message;
85 };
86 
125 public:
133  int add_point(double x, double y, bool fixed = false);
134 
141  int add_line(int p0, int p1);
142 
149  int add_circle(int center, double radius);
150 
166  void add_constraint(ConstraintType type, const std::vector<int>& elements, double val = 0.0);
167 
173  void fix_point(int pid) {
174  if (pid >= 0 && pid < static_cast<int>(points_.size()))
175  points_[pid].fixed = true;
176  }
177 
187  [[nodiscard]] SolverResult solve(int max_iter = 100, double tol = 1e-8);
188 
195  [[nodiscard]] int degrees_of_freedom() const;
196 
201  [[nodiscard]] const std::vector<SketchPoint>& points() const { return points_; }
202 
208  [[nodiscard]] Point2D get_point(int id) const;
209 
210 private:
211  std::vector<SketchPoint> points_;
212  std::vector<SketchLine> lines_;
213  std::vector<SketchCircle> circles_;
214  std::vector<Constraint> constraints_;
215 
221  void sync_from_vars(const double* vars, int nv);
222 
228  double eval_constraint(const Constraint& c) const;
229 
235  int count_equations(const Constraint& c) const;
236 
246  void fill_jacobian_rows(const Constraint& c, int ci,
247  const std::vector<int>& row_map,
248  double* J_data, int nv, int stride) const;
249 };
250 
251 } // namespace vde::sketch
二维几何约束求解器
Point2D get_point(int id) const
查询点坐标
int add_point(double x, double y, bool fixed=false)
添加自由点
void fix_point(int pid)
标记点为固定(硬约束)
SolverResult solve(int max_iter=100, double tol=1e-8)
执行约束求解
int degrees_of_freedom() const
计算自由度
const std::vector< SketchPoint > & points() const
当前点集访问
void add_constraint(ConstraintType type, const std::vector< int > &elements, double val=0.0)
添加约束
int add_line(int p0, int p1)
添加线段
int add_circle(int center, double radius)
添加圆
ConstraintType
约束类型枚举
@ Vertical
垂直线: x1 - x0 = 0 [1 eq]
@ Distance
距离约束: |p1-p0| - d = 0 [1 eq]
@ FixedPoint
固定点(硬约束,排除变量)[0 eq — 系统预处理]
@ Fixed
固定点位置: x-x0=0, y-y0=0 [2 eqs]
@ Angle
角度约束: atan2(cross(d0,d1), dot(d0,d1)) - θ = 0 [1 eq]
@ EqualLength
等长度: |d0| - |d1| = 0 [1 eq]
@ Coincident
两点重合: |p1-p0| = 0 [2 eqs: Δx=0, Δy=0]
@ Parallel
两线平行: cross(d0, d1) = 0 [1 eq]
@ Horizontal
水平线: y1 - y0 = 0 [1 eq]
@ Perpendicular
两线垂直: dot(d0, d1) = 0 [1 eq]
@ Radius
半径约束 [1 eq]
@ Concentric
同心 [2 eqs]
foundation::Point2D Point2D
双精度二维点(重新导出)
Definition: point.h:29
foundation::Vector2D Vector2D
双精度二维向量(重新导出)
Definition: point.h:47
double value
距离/角度值
ConstraintType type
约束类型
std::vector< int > elements
引用的元素索引(点/线/圆 ID)
草图线段元素
bool fixed
是否固定(排除自由度)
求解器返回结果
double residual
最终残差(均方根)
std::string message
诊断信息(成功或失败原因)
int iterations
实际迭代次数
std::vector< Point2D > points
求解后的点坐标