### ADR:VDCS/GCS 与 VDE 的微内核集成决策 - 新增 docs/design/vdcs-gcs-integration.md:VDCS 保持独立、不并入内核(延续 VDE-1197/1199/1201); - 三条路径:A 宿主直连(现状保持)、B 进程内 GCS 插件(ABI 1.3 VdeGcsSolverPlugin/solve_gcs)、 C 进程外 vde-service gcs:solve(推荐 VDCS 独立发布/隔离);VDE 不链接 VDCS,VDCS 不依赖 VDE C++ 内部库; - 契约:schema_version=1 sketch request / positions+DOF+constraint_status response JSON;约束词表归 VDCS; - 模块分类:Kernel/Official Modules/Plugins/Host/External 五层;GCS 属文档/参数化层而非几何内核; ### P3-8 插件 ABI 1.3:GCS 求解器扩展点 - VDE_PLUGIN_ABI_MINOR 2→3;VdeGcsSolverPlugin(solver_id/capabilities_json/solve/describe)+ Registration.gcs_solver; VdePluginKind GCS_SOLVER=7;VdePluginHost=168 不变、Registration=48(+8)、GcsSolverPlugin=40; - PluginManager gcs_solvers()/solve_gcs()(加载校验/失败隔离/卸载清理);sample_gcs mock 2D 投影求解(capability gcs:solve); - test_plugin_gcs 8/8、插件子集 109/109;ABI 矩阵 accepted=4(1.0/1.1/1.2/1.3)、rejected=1(2.0); 9 个既有 manifest abi.minor 2→3(abi11_compat 保持 1);docs design §5.3 + plugin-development §13; ### P3-9 vde-service GCS 外部服务(gcs:solve) - vde-service-protocol §9.1:能力/operation、request/response schema、错误码、宿主映射(schema_version=1); - plugins/external/sample_gcs_solver(独立可执行、不链接 VDE、确定性 mock + 测试钩子); - test_plugin_external_gcs 9/9(发现/端到端/驱动参数/超时/坏响应/未知 operation/非法 sketch/schema/冲突/能力过滤); - docs plugin-development §12.6;真实 VDCS 只需替换可执行文件; ### P3-10 微内核模块边界审计 / kernel SDK 瘦身 - 审计确认 vde::kernel 聚合/SDK/链接依赖干净;修复 kernel SDK 头宽拷贝夹带 cae/fluid/feature/param + fea_mesh.h 缺陷; - 新增 cmake/check_kernel_boundary.cmake + CTest VDE_KernelAggregateBoundary(聚合/SDK/依赖/include/头排除/app 分类); - 默认+kernel-only 构建 0 error、守卫 2/2 PASS;SDK install 实测仅 12 内核头目录/12 内核库; - docs/guides/architecture.md §7.9:三层分类/聚合与 SDK 边界/守卫/拆仓候选清单;2 处业务头耦合登记技术债; ### 工程 - 版本 1.0.69(CMake/version.h/test_version/README/docs index/module-manual);design §14 ADR 摘要; - 全量构建 0 error(385 targets);全量 ctest 7490 注册、7395/7395 PASS(0 failed,25 skipped,240.00s)。
695 lines
27 KiB
C++
695 lines
27 KiB
C++
// 示例 GCS(几何约束求解)插件(VDE 微内核 P3-8 / 插件 ABI 1.3;纯 C ABI,不链接内核)。
|
||
//
|
||
// 背景:VDE-1201 决策「VDE 不内嵌求解器」;本插件是 GCS 扩展点的 mock/演示实现,
|
||
// 用于验证 ABI 1.3 的 VdeGcsSolverPlugin 契约(VDCS 类真实求解器可按同一接口接入,
|
||
// 不必合并进 VDE 内核)。
|
||
//
|
||
// 输入 sketch JSON(UTF-8):
|
||
// {
|
||
// "points": [{"id":0,"x":0,"y":0,"fixed":true}, {"id":1,"x":10,"y":0}, ...],
|
||
// "constraints": [
|
||
// {"type":"distance","a":0,"b":1,"value":20.0},
|
||
// {"type":"horizontal","a":0,"b":1},
|
||
// {"type":"vertical","a":0,"b":1},
|
||
// {"type":"coincident","a":0,"b":1},
|
||
// {"type":"fix","point":2,"x":10.0,"y":5.0}
|
||
// ]
|
||
// }
|
||
// 输出 result JSON:
|
||
// {"solver_id":"gcs.2d.mock","converged":true,"iterations":N,"residual":R,"dof":D,
|
||
// "points":[{"id":0,"x":..,"y":..},...],
|
||
// "constraint_status":[{"index":0,"type":"distance","residual":0.0},...],
|
||
// "diagnostics":[...]}
|
||
// 求解方式:约束投影(Gauss-Seidel 迭代,逐步把每个约束的违反量按可动自由度均摊),
|
||
// 仅用于契约/接口验证,不保证工业级收敛性/数值稳定性。
|
||
#include <algorithm>
|
||
#include <cctype>
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <string>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#include "../official/common/format_plugin_common.h" // 官方插件公共工具(宿主校验/错误)
|
||
|
||
namespace {
|
||
|
||
using vde_plugin_common::fail;
|
||
using vde_plugin_common::validate_common_host;
|
||
|
||
// ─────────────────────────────────────────────────────────
|
||
// 极简 JSON(仅本插件 sketch/诊断需要;对象/数组/数字/布尔/字符串)
|
||
// ─────────────────────────────────────────────────────────
|
||
|
||
struct JsonValue {
|
||
enum class Type { Null, Bool, Number, String, Array, Object };
|
||
Type type = Type::Null;
|
||
bool boolean = false;
|
||
double number = 0.0;
|
||
std::string text;
|
||
std::vector<JsonValue> array;
|
||
std::vector<std::pair<std::string, JsonValue>> object;
|
||
|
||
const JsonValue* find(const std::string& key) const {
|
||
if (type != Type::Object) return nullptr;
|
||
for (const auto& entry : object) {
|
||
if (entry.first == key) return &entry.second;
|
||
}
|
||
return nullptr;
|
||
}
|
||
bool as_number(double* out) const {
|
||
if (type != Type::Number) return false;
|
||
*out = number;
|
||
return true;
|
||
}
|
||
bool as_bool(bool* out) const {
|
||
if (type != Type::Bool) return false;
|
||
*out = boolean;
|
||
return true;
|
||
}
|
||
};
|
||
|
||
class JsonParser {
|
||
public:
|
||
JsonParser(const char* data, std::size_t size) : cursor_(data), end_(data + size) {}
|
||
|
||
bool parse(JsonValue* out) {
|
||
skip_ws();
|
||
if (!parse_value(out, 0)) return false;
|
||
skip_ws();
|
||
if (cursor_ != end_) {
|
||
error_ = "trailing characters after JSON value";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
const std::string& error() const { return error_; }
|
||
|
||
private:
|
||
void skip_ws() {
|
||
while (cursor_ < end_ &&
|
||
(*cursor_ == ' ' || *cursor_ == '\t' || *cursor_ == '\r' || *cursor_ == '\n'))
|
||
++cursor_;
|
||
}
|
||
bool parse_value(JsonValue* out, int depth) {
|
||
if (!out || depth > 64) return fail_("JSON nesting too deep");
|
||
skip_ws();
|
||
if (cursor_ >= end_) return fail_("unexpected end of JSON");
|
||
const char c = *cursor_;
|
||
if (c == '{') return parse_object(out, depth);
|
||
if (c == '[') return parse_array(out, depth);
|
||
if (c == '"') {
|
||
out->type = JsonValue::Type::String;
|
||
return parse_string(&out->text);
|
||
}
|
||
if (c == 't' || c == 'f') return parse_bool(out);
|
||
if (c == 'n') return parse_null(out);
|
||
return parse_number(out);
|
||
}
|
||
bool fail_(const std::string& message) {
|
||
if (error_.empty()) error_ = message;
|
||
return false;
|
||
}
|
||
bool parse_object(JsonValue* out, int depth) {
|
||
out->type = JsonValue::Type::Object;
|
||
++cursor_; // '{'
|
||
skip_ws();
|
||
if (cursor_ < end_ && *cursor_ == '}') {
|
||
++cursor_;
|
||
return true;
|
||
}
|
||
while (cursor_ < end_) {
|
||
skip_ws();
|
||
if (cursor_ >= end_ || *cursor_ != '"') return fail_("expected object key");
|
||
std::string key;
|
||
if (!parse_string(&key)) return false;
|
||
skip_ws();
|
||
if (cursor_ >= end_ || *cursor_ != ':') return fail_("expected ':'");
|
||
++cursor_;
|
||
JsonValue value;
|
||
if (!parse_value(&value, depth + 1)) return false;
|
||
out->object.emplace_back(std::move(key), std::move(value));
|
||
skip_ws();
|
||
if (cursor_ < end_ && *cursor_ == ',') {
|
||
++cursor_;
|
||
continue;
|
||
}
|
||
if (cursor_ < end_ && *cursor_ == '}') {
|
||
++cursor_;
|
||
return true;
|
||
}
|
||
return fail_("expected ',' or '}'");
|
||
}
|
||
return fail_("unterminated object");
|
||
}
|
||
bool parse_array(JsonValue* out, int depth) {
|
||
out->type = JsonValue::Type::Array;
|
||
++cursor_; // '['
|
||
skip_ws();
|
||
if (cursor_ < end_ && *cursor_ == ']') {
|
||
++cursor_;
|
||
return true;
|
||
}
|
||
while (cursor_ < end_) {
|
||
JsonValue value;
|
||
if (!parse_value(&value, depth + 1)) return false;
|
||
out->array.push_back(std::move(value));
|
||
skip_ws();
|
||
if (cursor_ < end_ && *cursor_ == ',') {
|
||
++cursor_;
|
||
continue;
|
||
}
|
||
if (cursor_ < end_ && *cursor_ == ']') {
|
||
++cursor_;
|
||
return true;
|
||
}
|
||
return fail_("expected ',' or ']'");
|
||
}
|
||
return fail_("unterminated array");
|
||
}
|
||
bool parse_string(std::string* out) {
|
||
out->clear();
|
||
++cursor_; // '"'
|
||
while (cursor_ < end_) {
|
||
const char c = *cursor_++;
|
||
if (c == '"') return true;
|
||
if (c != '\\') {
|
||
out->push_back(c);
|
||
continue;
|
||
}
|
||
if (cursor_ >= end_) break;
|
||
const char escape = *cursor_++;
|
||
switch (escape) {
|
||
case '"': out->push_back('"'); break;
|
||
case '\\': out->push_back('\\'); break;
|
||
case '/': out->push_back('/'); break;
|
||
case 'b': out->push_back('\b'); break;
|
||
case 'f': out->push_back('\f'); break;
|
||
case 'n': out->push_back('\n'); break;
|
||
case 'r': out->push_back('\r'); break;
|
||
case 't': out->push_back('\t'); break;
|
||
case 'u': {
|
||
// 仅支持 BMP 内转 ASCII(非 ASCII 码点以 '?' 代替;本插件不依赖)
|
||
if (end_ - cursor_ < 4) return fail_("bad \\u escape");
|
||
char hex[5] = {cursor_[0], cursor_[1], cursor_[2], cursor_[3], '\0'};
|
||
cursor_ += 4;
|
||
const long code = std::strtol(hex, nullptr, 16);
|
||
out->push_back(code >= 0x20 && code < 0x7f ? static_cast<char>(code) : '?');
|
||
break;
|
||
}
|
||
default: return fail_("bad escape sequence");
|
||
}
|
||
}
|
||
return fail_("unterminated string");
|
||
}
|
||
bool parse_bool(JsonValue* out) {
|
||
if (end_ - cursor_ >= 4 && std::strncmp(cursor_, "true", 4) == 0) {
|
||
out->type = JsonValue::Type::Bool;
|
||
out->boolean = true;
|
||
cursor_ += 4;
|
||
return true;
|
||
}
|
||
if (end_ - cursor_ >= 5 && std::strncmp(cursor_, "false", 5) == 0) {
|
||
out->type = JsonValue::Type::Bool;
|
||
out->boolean = false;
|
||
cursor_ += 5;
|
||
return true;
|
||
}
|
||
return fail_("expected true/false");
|
||
}
|
||
bool parse_null(JsonValue* out) {
|
||
if (end_ - cursor_ >= 4 && std::strncmp(cursor_, "null", 4) == 0) {
|
||
out->type = JsonValue::Type::Null;
|
||
cursor_ += 4;
|
||
return true;
|
||
}
|
||
return fail_("expected null");
|
||
}
|
||
bool parse_number(JsonValue* out) {
|
||
char* parse_end = nullptr;
|
||
const double value = std::strtod(cursor_, &parse_end);
|
||
if (parse_end == cursor_) return fail_("invalid number");
|
||
out->type = JsonValue::Type::Number;
|
||
out->number = value;
|
||
cursor_ = parse_end;
|
||
return true;
|
||
}
|
||
|
||
const char* cursor_ = nullptr;
|
||
const char* end_ = nullptr;
|
||
std::string error_;
|
||
};
|
||
|
||
// ─────────────────────────────────────────────────────────
|
||
// Mock 2D 约束求解
|
||
// ─────────────────────────────────────────────────────────
|
||
|
||
struct SketchPoint {
|
||
double x = 0.0;
|
||
double y = 0.0;
|
||
bool fixed = false;
|
||
double anchor_x = 0.0;
|
||
double anchor_y = 0.0;
|
||
};
|
||
|
||
enum class ConstraintType { Distance, Horizontal, Vertical, Coincident, Fix };
|
||
|
||
struct SketchConstraint {
|
||
ConstraintType type = ConstraintType::Distance;
|
||
int a = -1;
|
||
int b = -1;
|
||
int point = -1;
|
||
double value = 0.0;
|
||
double target_x = 0.0;
|
||
double target_y = 0.0;
|
||
};
|
||
|
||
const char* constraint_name(ConstraintType type) {
|
||
switch (type) {
|
||
case ConstraintType::Distance: return "distance";
|
||
case ConstraintType::Horizontal: return "horizontal";
|
||
case ConstraintType::Vertical: return "vertical";
|
||
case ConstraintType::Coincident: return "coincident";
|
||
case ConstraintType::Fix: return "fix";
|
||
}
|
||
return "unknown";
|
||
}
|
||
|
||
bool parse_sketch(const JsonValue& root, std::vector<SketchPoint>* points,
|
||
std::vector<SketchConstraint>* constraints, std::string* error) {
|
||
const JsonValue* points_value = root.find("points");
|
||
if (!points_value || points_value->type != JsonValue::Type::Array ||
|
||
points_value->array.empty()) {
|
||
if (error) *error = "gcs: sketch.points 缺失或为空";
|
||
return false;
|
||
}
|
||
for (const JsonValue& point_value : points_value->array) {
|
||
if (point_value.type != JsonValue::Type::Object) {
|
||
if (error) *error = "gcs: sketch.points 元素必须是对象";
|
||
return false;
|
||
}
|
||
SketchPoint point;
|
||
double x = 0.0, y = 0.0;
|
||
const JsonValue* x_value = point_value.find("x");
|
||
const JsonValue* y_value = point_value.find("y");
|
||
if (!x_value || !y_value || !x_value->as_number(&x) || !y_value->as_number(&y)) {
|
||
if (error) *error = "gcs: point 缺少数值 x/y";
|
||
return false;
|
||
}
|
||
point.x = x;
|
||
point.y = y;
|
||
point.anchor_x = x;
|
||
point.anchor_y = y;
|
||
const JsonValue* fixed_value = point_value.find("fixed");
|
||
if (fixed_value) {
|
||
bool fixed = false;
|
||
if (!fixed_value->as_bool(&fixed)) {
|
||
if (error) *error = "gcs: point.fixed 必须是布尔";
|
||
return false;
|
||
}
|
||
point.fixed = fixed;
|
||
}
|
||
points->push_back(point);
|
||
}
|
||
const JsonValue* constraint_value = root.find("constraints");
|
||
if (!constraint_value) return true;
|
||
if (constraint_value->type != JsonValue::Type::Array) {
|
||
if (error) *error = "gcs: constraints 必须是数组";
|
||
return false;
|
||
}
|
||
for (const JsonValue& entry : constraint_value->array) {
|
||
if (entry.type != JsonValue::Type::Object) {
|
||
if (error) *error = "gcs: constraints 元素必须是对象";
|
||
return false;
|
||
}
|
||
const JsonValue* type_value = entry.find("type");
|
||
if (!type_value || type_value->type != JsonValue::Type::String) {
|
||
if (error) *error = "gcs: constraint.type 缺失";
|
||
return false;
|
||
}
|
||
SketchConstraint constraint;
|
||
const std::string& type_name = type_value->text;
|
||
if (type_name == "distance") {
|
||
constraint.type = ConstraintType::Distance;
|
||
const JsonValue* value_value = entry.find("value");
|
||
if (!value_value || !value_value->as_number(&constraint.value)) {
|
||
if (error) *error = "gcs: distance constraint 缺少数值 value";
|
||
return false;
|
||
}
|
||
} else if (type_name == "horizontal") {
|
||
constraint.type = ConstraintType::Horizontal;
|
||
} else if (type_name == "vertical") {
|
||
constraint.type = ConstraintType::Vertical;
|
||
} else if (type_name == "coincident") {
|
||
constraint.type = ConstraintType::Coincident;
|
||
} else if (type_name == "fix") {
|
||
constraint.type = ConstraintType::Fix;
|
||
double x = 0.0, y = 0.0;
|
||
const JsonValue* px = entry.find("x");
|
||
const JsonValue* py = entry.find("y");
|
||
if (!px || !py || !px->as_number(&x) || !py->as_number(&y)) {
|
||
if (error) *error = "gcs: fix constraint 缺少数值 x/y";
|
||
return false;
|
||
}
|
||
constraint.target_x = x;
|
||
constraint.target_y = y;
|
||
} else {
|
||
if (error) *error = "gcs: 不支持的约束类型 '" + type_name + "'";
|
||
return false;
|
||
}
|
||
const auto read_index = [&](const char* key, int* out) -> bool {
|
||
const JsonValue* value = entry.find(key);
|
||
double number = 0.0;
|
||
if (!value || !value->as_number(&number)) return false;
|
||
const double rounded = std::floor(number);
|
||
if (std::abs(number - rounded) > 1e-9 || number < 0.0) return false;
|
||
*out = static_cast<int>(rounded);
|
||
return true;
|
||
};
|
||
if (constraint.type == ConstraintType::Fix) {
|
||
if (!read_index("point", &constraint.point)) {
|
||
if (error) *error = "gcs: fix constraint 缺少整数 point";
|
||
return false;
|
||
}
|
||
} else {
|
||
if (!read_index("a", &constraint.a) || !read_index("b", &constraint.b)) {
|
||
if (error) *error = "gcs: constraint 缺少整数 a/b";
|
||
return false;
|
||
}
|
||
}
|
||
const bool index_ok =
|
||
constraint.type == ConstraintType::Fix
|
||
? (constraint.point >= 0 &&
|
||
static_cast<std::size_t>(constraint.point) < points->size())
|
||
: (constraint.a >= 0 &&
|
||
static_cast<std::size_t>(constraint.a) < points->size() &&
|
||
constraint.b >= 0 &&
|
||
static_cast<std::size_t>(constraint.b) < points->size());
|
||
if (!index_ok) {
|
||
if (error) *error = "gcs: constraint 引用的 point 索引越界";
|
||
return false;
|
||
}
|
||
constraints->push_back(constraint);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
double constraint_residual(const std::vector<SketchPoint>& points,
|
||
const SketchConstraint& constraint) {
|
||
switch (constraint.type) {
|
||
case ConstraintType::Distance: {
|
||
const SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
const SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
const double dx = b.x - a.x;
|
||
const double dy = b.y - a.y;
|
||
return std::abs(std::sqrt(dx * dx + dy * dy) - constraint.value);
|
||
}
|
||
case ConstraintType::Horizontal: {
|
||
const SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
const SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
return std::abs(b.y - a.y);
|
||
}
|
||
case ConstraintType::Vertical: {
|
||
const SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
const SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
return std::abs(b.x - a.x);
|
||
}
|
||
case ConstraintType::Coincident: {
|
||
const SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
const SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
return std::max(std::abs(b.x - a.x), std::abs(b.y - a.y));
|
||
}
|
||
case ConstraintType::Fix: {
|
||
const SketchPoint& p = points[static_cast<std::size_t>(constraint.point)];
|
||
return std::max(std::abs(p.x - constraint.target_x),
|
||
std::abs(p.y - constraint.target_y));
|
||
}
|
||
}
|
||
return 0.0;
|
||
}
|
||
|
||
void move_towards(SketchPoint* from, SketchPoint* to, double dx, double dy) {
|
||
if (from->fixed && to->fixed) return;
|
||
if (from->fixed) {
|
||
to->x += dx;
|
||
to->y += dy;
|
||
return;
|
||
}
|
||
if (to->fixed) {
|
||
from->x -= dx;
|
||
from->y -= dy;
|
||
return;
|
||
}
|
||
from->x -= dx * 0.5;
|
||
from->y -= dy * 0.5;
|
||
to->x += dx * 0.5;
|
||
to->y += dy * 0.5;
|
||
}
|
||
|
||
bool solve_projections(std::vector<SketchPoint>& points,
|
||
const std::vector<SketchConstraint>& constraints, double tolerance,
|
||
int max_iterations, int* iterations_used, double* residual_out) {
|
||
double residual = 0.0;
|
||
for (int iteration = 0; iteration < max_iterations; ++iteration) {
|
||
residual = 0.0;
|
||
for (const SketchConstraint& constraint : constraints) {
|
||
const double violation = constraint_residual(points, constraint);
|
||
residual = std::max(residual, violation);
|
||
if (violation <= tolerance) continue;
|
||
switch (constraint.type) {
|
||
case ConstraintType::Fix: {
|
||
SketchPoint& p = points[static_cast<std::size_t>(constraint.point)];
|
||
if (!p.fixed) {
|
||
p.x = constraint.target_x;
|
||
p.y = constraint.target_y;
|
||
}
|
||
break;
|
||
}
|
||
case ConstraintType::Distance: {
|
||
SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
const double dx = b.x - a.x;
|
||
const double dy = b.y - a.y;
|
||
const double length = std::sqrt(dx * dx + dy * dy);
|
||
if (length < 1e-12) {
|
||
move_towards(&a, &b, constraint.value, 0.0);
|
||
break;
|
||
}
|
||
const double desired = constraint.value;
|
||
// 沿 a→b 方向移动 (desired - length):距离不足时把 b 推远(或 a 拉远)
|
||
const double error = desired - length;
|
||
move_towards(&a, &b, dx / length * error, dy / length * error);
|
||
break;
|
||
}
|
||
case ConstraintType::Horizontal: {
|
||
SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
const double mean = (a.y + b.y) * 0.5;
|
||
if (!a.fixed) a.y = mean;
|
||
if (!b.fixed) b.y = mean;
|
||
break;
|
||
}
|
||
case ConstraintType::Vertical: {
|
||
SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
const double mean = (a.x + b.x) * 0.5;
|
||
if (!a.fixed) a.x = mean;
|
||
if (!b.fixed) b.x = mean;
|
||
break;
|
||
}
|
||
case ConstraintType::Coincident: {
|
||
SketchPoint& a = points[static_cast<std::size_t>(constraint.a)];
|
||
SketchPoint& b = points[static_cast<std::size_t>(constraint.b)];
|
||
const double mean_x = (a.x + b.x) * 0.5;
|
||
const double mean_y = (a.y + b.y) * 0.5;
|
||
if (!a.fixed) {
|
||
a.x = mean_x;
|
||
a.y = mean_y;
|
||
}
|
||
if (!b.fixed) {
|
||
b.x = mean_x;
|
||
b.y = mean_y;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
residual = 0.0;
|
||
for (const SketchConstraint& constraint : constraints)
|
||
residual = std::max(residual, constraint_residual(points, constraint));
|
||
if (iterations_used) *iterations_used = iteration + 1;
|
||
if (residual <= tolerance) {
|
||
if (residual_out) *residual_out = residual;
|
||
return true;
|
||
}
|
||
}
|
||
if (residual_out) *residual_out = residual;
|
||
return false;
|
||
}
|
||
|
||
int compute_dof(std::size_t point_count, const std::vector<SketchPoint>& points,
|
||
const std::vector<SketchConstraint>& constraints) {
|
||
// fixed 点的 x/y 各被钉住(-2 自由度);其余约束按方程数扣减
|
||
long equations = 0;
|
||
for (const SketchPoint& point : points) {
|
||
if (point.fixed) equations += 2;
|
||
}
|
||
for (const SketchConstraint& constraint : constraints) {
|
||
switch (constraint.type) {
|
||
case ConstraintType::Fix: equations += 2; break;
|
||
case ConstraintType::Coincident: equations += 2; break;
|
||
default: equations += 1; break;
|
||
}
|
||
}
|
||
long dof = static_cast<long>(2 * point_count) - equations;
|
||
return dof > 0 ? static_cast<int>(dof) : 0;
|
||
}
|
||
|
||
// ─────────────────────────────────────────────────────────
|
||
// 宿主服务入口
|
||
// ─────────────────────────────────────────────────────────
|
||
|
||
int gcs_solve(const VdePluginHost* host, const char* sketch_json, size_t sketch_size,
|
||
const char* params_json, VdeBuffer* result_json) {
|
||
if (result_json) {
|
||
result_json->data = nullptr;
|
||
result_json->size = 0;
|
||
}
|
||
if (!result_json) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "gcs: result 为空");
|
||
const int host_rc = validate_common_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
if (!sketch_json || sketch_size == 0)
|
||
return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "gcs: sketch JSON 为空");
|
||
|
||
double tolerance = 1e-9;
|
||
int max_iterations = 500;
|
||
if (params_json && *params_json) {
|
||
// 允许 {"tolerance":1e-9,"max_iterations":500};极简解析(失败回退默认)
|
||
const char* tol_pos = std::strstr(params_json, "\"tolerance\"");
|
||
if (tol_pos) {
|
||
const char* value = std::strchr(tol_pos, ':');
|
||
if (value) {
|
||
const double parsed = std::strtod(value + 1, nullptr);
|
||
if (parsed > 0.0) tolerance = parsed;
|
||
}
|
||
}
|
||
const char* iter_pos = std::strstr(params_json, "\"max_iterations\"");
|
||
if (iter_pos) {
|
||
const char* value = std::strchr(iter_pos, ':');
|
||
if (value) {
|
||
const long parsed = std::strtol(value + 1, nullptr, 10);
|
||
if (parsed > 0 && parsed <= 100000) max_iterations = static_cast<int>(parsed);
|
||
}
|
||
}
|
||
}
|
||
|
||
JsonValue root;
|
||
JsonParser parser(sketch_json, sketch_size);
|
||
if (!parser.parse(&root))
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE,
|
||
("gcs: sketch JSON 解析失败: " + parser.error()).c_str());
|
||
if (root.type != JsonValue::Type::Object)
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, "gcs: sketch JSON 根必须是对象");
|
||
|
||
std::vector<SketchPoint> points;
|
||
std::vector<SketchConstraint> constraints;
|
||
std::string error;
|
||
if (!parse_sketch(root, &points, &constraints, &error))
|
||
return fail(host, VDE_PLUGIN_ERR_PARSE, error.c_str());
|
||
|
||
int iterations = 0;
|
||
double residual = 0.0;
|
||
const bool converged = solve_projections(points, constraints, tolerance, max_iterations,
|
||
&iterations, &residual);
|
||
const int dof = compute_dof(points.size(), points, constraints);
|
||
|
||
std::string output;
|
||
output.reserve(256 + points.size() * 48 + constraints.size() * 64);
|
||
char buffer[256];
|
||
output += "{\"solver_id\":\"gcs.2d.mock\",\"converged\":";
|
||
output += converged ? "true" : "false";
|
||
std::snprintf(buffer, sizeof(buffer), ",\"iterations\":%d,\"dof\":%d,\"residual\":%.12g",
|
||
iterations, dof, residual);
|
||
output += buffer;
|
||
output += ",\"points\":[";
|
||
for (std::size_t i = 0; i < points.size(); ++i) {
|
||
if (i != 0) output += ",";
|
||
std::snprintf(buffer, sizeof(buffer),
|
||
"{\"id\":%zu,\"x\":%.12g,\"y\":%.12g,\"fixed\":%s}", i, points[i].x,
|
||
points[i].y, points[i].fixed ? "true" : "false");
|
||
output += buffer;
|
||
}
|
||
output += "],\"constraint_status\":[";
|
||
for (std::size_t i = 0; i < constraints.size(); ++i) {
|
||
if (i != 0) output += ",";
|
||
std::snprintf(buffer, sizeof(buffer),
|
||
"{\"index\":%zu,\"type\":\"%s\",\"residual\":%.12g}", i,
|
||
constraint_name(constraints[i].type),
|
||
constraint_residual(points, constraints[i]));
|
||
output += buffer;
|
||
}
|
||
output += "],\"diagnostics\":[\"mock GCS solver (projection iterations); not for "
|
||
"production use\"]}";
|
||
|
||
void* out_buffer = host->alloc(output.size() + 1);
|
||
if (!out_buffer) return fail(host, VDE_PLUGIN_ERR_BUFFER, "gcs: 结果缓冲分配失败");
|
||
std::memcpy(out_buffer, output.c_str(), output.size() + 1);
|
||
result_json->data = out_buffer;
|
||
result_json->size = output.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
const char* gcs_describe() {
|
||
return "VDE sample GCS 2D constraint solver (mock projection solver; ABI 1.3)";
|
||
}
|
||
|
||
const VdeGcsSolverPlugin kSolver = [] {
|
||
VdeGcsSolverPlugin solver{};
|
||
solver.struct_size = static_cast<uint32_t>(sizeof(VdeGcsSolverPlugin));
|
||
solver.solver_id = "gcs.2d.mock";
|
||
solver.capabilities_json =
|
||
"{\"dimensions\":2,\"constraint_types\":[\"distance\",\"horizontal\","
|
||
"\"vertical\",\"coincident\",\"fix\"],\"demo\":true}";
|
||
solver.solve = &gcs_solve;
|
||
solver.describe = &gcs_describe;
|
||
return solver;
|
||
}();
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_query(uint32_t host_abi_major, VdePluginInfo* info) {
|
||
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_ABI;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
||
info->id = "com.vde.sample.gcs";
|
||
info->name = "VDE Sample GCS 2D Constraint Solver";
|
||
info->version = "0.1.0";
|
||
info->kind = VDE_PLUGIN_KIND_GCS_SOLVER;
|
||
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
info->capabilities = "gcs:solve";
|
||
info->license = "Apache-2.0";
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host,
|
||
VdePluginRegistration* reg) {
|
||
if (!reg || reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI;
|
||
const int host_rc = validate_common_host(host);
|
||
if (host_rc != VDE_PLUGIN_OK) return host_rc;
|
||
reg->kind = VDE_PLUGIN_KIND_GCS_SOLVER;
|
||
reg->format = nullptr;
|
||
reg->cae_solver = nullptr;
|
||
reg->cam_strategy = nullptr;
|
||
reg->gcs_solver = &kSolver;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|