Files
ViewDesignEngine/include/vde/sketch/constraint_solver.h
T

51 lines
1.5 KiB
C++
Raw Normal View History

#pragma once
#include "vde/core/point.h"
#include <vector>
#include <string>
namespace vde::sketch {
using core::Point2D;
using core::Vector2D;
struct SketchPoint { int id; Point2D pos; bool fixed = false; };
struct SketchLine { int id, p0, p1; };
struct SketchCircle { int id, center; double radius; };
enum class ConstraintType {
Horizontal, Vertical, Parallel, Perpendicular, Coincident,
EqualLength, Distance, Radius, Tangent, Concentric, FixedPoint
};
struct Constraint {
ConstraintType type;
std::vector<int> elements;
double value = 0.0;
};
struct SolverResult {
bool converged = false; int iterations = 0; double residual = 0.0;
std::vector<Point2D> points; std::string message;
};
class ConstraintSolver {
public:
int add_point(double x, double y, bool fixed = false);
int add_line(int p0, int p1);
int add_circle(int center, double radius);
void add_constraint(ConstraintType type, const std::vector<int>& elements, double val = 0.0);
void fix_point(int pid) { if(pid>=0&&pid<(int)points_.size()) points_[pid].fixed=true; }
[[nodiscard]] SolverResult solve(int max_iter = 100, double tol = 1e-8);
[[nodiscard]] int degrees_of_freedom() const;
[[nodiscard]] const std::vector<SketchPoint>& points() const { return points_; }
private:
std::vector<SketchPoint> points_;
std::vector<SketchLine> lines_;
std::vector<SketchCircle> circles_;
std::vector<Constraint> constraints_;
double eval_constraint(const Constraint& c) const;
};
} // namespace vde::sketch