Files

712 lines
29 KiB
C++
Raw Permalink 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.
// 官方 VRML 2.0(IndexedFaceSet)插件(VDE 微内核 P4 / 插件 ABI 1.5;纯 C ABI,不链接内核)。
//
// 读:VRML 2.0 ASCII(#VRML V2.0 utf8):
// - Shape { geometry IndexedFaceSet { coord Coordinate { point [...] } + coordIndex } };
// - Transform { translation/rotation/scale/center/scaleOrientation children [...] } 层级变换;
// - DEF name <node> / USE name 节点/几何复用(未定义/循环上限明确报错);
// - Appearance { material Material {...} }、Color、Normal、texCoord 等解析/忽略(颜色不写入实体);
// - 多 Shape/Transform 应用世界变换后合并为单网格;多边形扇形三角化。
// 写:实体经 host->body_tessellate 三角化后按 body 输出 Shape/IndexedFaceSet。
// 限制:仅 ASCII VRML 2.0(不支持 VRML 1.0/二进制/gzip);显式顶点/三角面上限。
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include "../common/format_plugin_common.h"
namespace {
using vde_plugin_common::MeshAccumulator;
using vde_plugin_common::fail;
using vde_plugin_common::read_file_bytes;
using vde_plugin_common::validate_common_host;
using vde_plugin_common::validate_read_host;
using vde_plugin_common::validate_write_host;
using vde_plugin_common::write_file_bytes;
constexpr std::size_t kMaxVertices = 5000000u;
constexpr std::size_t kMaxTriangles = 20000000u;
constexpr std::size_t kMaxTextBytes = 1024u * 1024u * 1024u;
constexpr std::size_t kMaxFaceVertices = 4096u;
constexpr std::size_t kMaxShapes = 10000u;
constexpr double kWriteDeflection = 0.01;
bool is_punct(char c) {
return c == '[' || c == ']' || c == '{' || c == '}' || c == ',';
}
/// VRML 标记化:跳过空白/逗号/# 注释(行尾);标点单独成 token。
std::vector<std::string> tokenize(const char* data, std::size_t size) {
std::vector<std::string> tokens;
std::size_t i = 0;
while (i < size) {
const char c = data[i];
if (c == '#') {
while (i < size && data[i] != '\n') ++i;
continue;
}
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' || c == ',') {
++i;
continue;
}
if (is_punct(c)) {
tokens.emplace_back(1, c);
++i;
continue;
}
const std::size_t begin = i;
while (i < size && !is_punct(data[i]) && data[i] != ' ' && data[i] != '\t' &&
data[i] != '\r' && data[i] != '\n' && data[i] != '\f' && data[i] != ',')
++i;
tokens.emplace_back(data + begin, i - begin);
}
return tokens;
}
bool parse_double(const std::string& token, double* out) {
if (!out) return false;
char* end = nullptr;
const double value = std::strtod(token.c_str(), &end);
if (end == token.c_str() || *end != '\0') return false;
if (!std::isfinite(value)) return false;
*out = value;
return true;
}
bool parse_long(const std::string& token, long long* out) {
if (!out) return false;
char* end = nullptr;
const long long value = std::strtoll(token.c_str(), &end, 10);
if (end == token.c_str() || *end != '\0') return false;
*out = value;
return true;
}
/// 解析一个数值数组(tokens[pos] 必须是 "[";返回 "]" 之后的位置)。
bool parse_array(const std::vector<std::string>& tokens, std::size_t* pos,
std::vector<std::string>* out) {
if (!pos || !out || *pos >= tokens.size() || tokens[*pos] != "[") return false;
++(*pos);
while (*pos < tokens.size() && tokens[*pos] != "]") {
out->push_back(tokens[*pos]);
++(*pos);
}
if (*pos >= tokens.size()) return false;
++(*pos); // skip "]"
return true;
}
struct VrmlAffine {
double m[16];
static VrmlAffine identity() {
VrmlAffine a{};
a.m[0] = a.m[5] = a.m[10] = a.m[15] = 1.0;
return a;
}
static VrmlAffine translation(double x, double y, double z) {
VrmlAffine a = identity();
a.m[3] = x; a.m[7] = y; a.m[11] = z;
return a;
}
static VrmlAffine scaling(double x, double y, double z) {
VrmlAffine a = identity();
a.m[0] = x; a.m[5] = y; a.m[10] = z;
return a;
}
static VrmlAffine rotation(double x, double y, double z, double angle) {
const double n = std::sqrt(x * x + y * y + z * z);
if (n <= 1e-12 || !std::isfinite(angle)) return identity();
x /= n; y /= n; z /= n;
const double c = std::cos(angle), s = std::sin(angle), t = 1.0 - c;
VrmlAffine a = identity();
a.m[0] = t*x*x + c; a.m[1] = t*x*y - s*z; a.m[2] = t*x*z + s*y;
a.m[4] = t*x*y + s*z; a.m[5] = t*y*y + c; a.m[6] = t*y*z - s*x;
a.m[8] = t*x*z - s*y; a.m[9] = t*y*z + s*x; a.m[10] = t*z*z + c;
return a;
}
VrmlAffine multiply(const VrmlAffine& b) const {
VrmlAffine out{};
for (int r = 0; r < 4; ++r)
for (int c = 0; c < 4; ++c) {
double v = 0.0;
for (int k = 0; k < 4; ++k) v += m[r*4+k] * b.m[k*4+c];
out.m[r*4+c] = v;
}
return out;
}
void apply(double x, double y, double z, double out[3]) const {
out[0] = m[0]*x + m[1]*y + m[2]*z + m[3];
out[1] = m[4]*x + m[5]*y + m[6]*z + m[7];
out[2] = m[8]*x + m[9]*y + m[10]*z + m[11];
}
};
class VrmlParser {
public:
VrmlParser(const std::vector<std::string>& tokens, MeshAccumulator* mesh,
std::string* error)
: t_(tokens), mesh_(mesh), error_(error) {}
bool parse() {
while (!eof()) {
if (!node(VrmlAffine::identity(), 0)) return false;
}
if (mesh_->indices.empty()) return err("未找到 IndexedFaceSet 几何");
return true;
}
private:
bool err(const std::string& message) {
if (error_) *error_ = "vrml: " + message;
return false;
}
bool eof() const { return i_ >= t_.size(); }
const std::string& at() const { return t_[i_]; }
bool accept(const std::string& token) {
if (eof() || at() != token) return false;
++i_;
return true;
}
bool number(double* out) {
if (eof()) return false;
char* end = nullptr;
const double v = std::strtod(at().c_str(), &end);
if (end == at().c_str() || *end != '\0' || !std::isfinite(v)) return false;
*out = v;
++i_;
return true;
}
bool number_array(std::vector<double>* out) {
out->clear();
if (!accept("[")) return err("数值数组缺少 '['");
while (!eof() && at() != "]") {
double v = 0.0;
if (!number(&v)) return err("数值数组含非法数字");
out->push_back(v);
}
if (!accept("]")) return err("数值数组缺少 ']'");
return true;
}
bool int_array(std::vector<long long>* out) {
out->clear();
if (!accept("[")) return err("索引数组缺少 '['");
while (!eof() && at() != "]") {
char* end = nullptr;
const long long v = std::strtoll(at().c_str(), &end, 10);
if (end == at().c_str() || *end != '\0') return err("索引数组含非整数");
out->push_back(v);
++i_;
}
if (!accept("]")) return err("索引数组缺少 ']'");
return true;
}
bool skip_value() {
if (eof()) return false;
// 字段值可能是节点(如 Appearance/Material/Color):identifier + { ... }。
if (i_ + 1 < t_.size() && (t_[i_ + 1] == "{" || t_[i_ + 1] == "[")) {
++i_;
return skip_value();
}
if (at() == "[" || at() == "{") {
const char open = at()[0];
const char close = (open == '[') ? ']' : '}';
int depth = 0;
while (!eof()) {
if (at() == std::string(1, open)) ++depth;
else if (at() == std::string(1, close)) {
--depth;
++i_;
if (depth == 0) return true;
continue;
}
++i_;
}
return err("括号不匹配");
}
++i_;
return true;
}
bool skip_braces() {
if (!accept("{")) return err("缺少 '{'");
int depth = 1;
while (!eof() && depth > 0) {
if (at() == "{") ++depth;
else if (at() == "}") --depth;
++i_;
}
if (depth != 0) return err("花括号不匹配");
return true;
}
bool node(const VrmlAffine& world, int depth) {
if (depth > 64) return err("场景图嵌套超过 64 层");
if (++nodes_ > 200000) return err("场景图节点数超过上限");
if (eof()) return err("节点缺失");
if (at() == "DEF") {
++i_;
if (eof()) return err("DEF 缺少名称");
const std::string name = at();
++i_;
const std::size_t begin = i_;
if (!body(world, depth)) return false;
defs_[name] = {begin, i_};
return true;
}
if (at() == "USE") {
++i_;
if (eof()) return err("USE 缺少名称");
const std::string name = at();
++i_;
const auto it = defs_.find(name);
if (it == defs_.end()) return err("USE 未定义的节点: " + name);
if (++expansions_ > 10000) return err("USE 展开次数超过上限(疑似循环)");
const std::size_t saved = i_;
i_ = it->second.first;
if (!body(world, depth + 1)) return false;
i_ = saved;
return true;
}
return body(world, depth);
}
bool body(const VrmlAffine& world, int depth) {
if (eof()) return err("节点类型缺失");
const std::string type = at();
++i_;
if (type == "Transform") return transform(world, depth);
if (type == "Shape") return shape(world, depth);
if (type == "Group" || type == "Anchor" || type == "Collision" ||
type == "Switch" || type == "Billboard")
return group(world, depth);
if (type == "IndexedFaceSet") return indexed_face_set(world, depth);
if (type == "Coordinate" || type == "Appearance" || type == "Material" ||
type == "Color" || type == "Normal" || type == "TextureCoordinate") {
if (!eof() && at() == "{") return skip_braces();
return true;
}
if (!eof() && at() == "{") return skip_braces();
return true;
}
bool children(const VrmlAffine& world, int depth) {
if (eof()) return err("children 缺少值");
if (at() == "[") {
++i_;
while (!eof() && at() != "]") {
if (!node(world, depth + 1)) return false;
}
return accept("]") ? true : err("children 缺少 ']'");
}
return node(world, depth + 1);
}
bool group(const VrmlAffine& world, int depth) {
if (!accept("{")) return err("Group 缺少 '{'");
while (!eof() && at() != "}") {
const std::string field = at();
++i_;
if (field == "children") {
if (!children(world, depth)) return false;
} else if (!skip_value()) {
return err("Group 字段值不完整: " + field);
}
}
return accept("}") ? true : err("Group 花括号不匹配");
}
bool transform(const VrmlAffine& parent, int depth) {
if (!accept("{")) return err("Transform 缺少 '{'");
double tx=0, ty=0, tz=0, sx=1, sy=1, sz=1, cx=0, cy=0, cz=0;
double rx=0, ry=0, rz=1, angle=0, sox=0, soy=0, soz=1, so_angle=0;
bool children_done = false;
while (!eof() && at() != "}") {
const std::string field = at();
++i_;
if (field == "translation" || field == "scale" || field == "center" ||
field == "rotation" || field == "scaleOrientation") {
if (eof() || at() != "[") return err(field + " 需要数值数组");
std::vector<double> v;
if (!number_array(&v)) return false;
if (field == "translation") {
if (v.size() != 3) return err("translation 需要 3 个数");
tx=v[0]; ty=v[1]; tz=v[2];
} else if (field == "scale") {
if (v.size() != 3) return err("scale 需要 3 个数");
sx=v[0]; sy=v[1]; sz=v[2];
} else if (field == "center") {
if (v.size() != 3) return err("center 需要 3 个数");
cx=v[0]; cy=v[1]; cz=v[2];
} else if (field == "rotation") {
if (v.size() != 4) return err("rotation 需要 4 个数");
rx=v[0]; ry=v[1]; rz=v[2]; angle=v[3];
} else {
if (v.size() != 4) return err("scaleOrientation 需要 4 个数");
sox=v[0]; soy=v[1]; soz=v[2]; so_angle=v[3];
}
} else if (field == "children") {
if (children_done) return err("Transform 重复 children");
const VrmlAffine local = VrmlAffine::translation(tx,ty,tz)
.multiply(VrmlAffine::translation(cx,cy,cz))
.multiply(VrmlAffine::rotation(rx,ry,rz,angle))
.multiply(VrmlAffine::rotation(sox,soy,soz,so_angle))
.multiply(VrmlAffine::scaling(sx,sy,sz))
.multiply(VrmlAffine::rotation(sox,soy,soz,-so_angle))
.multiply(VrmlAffine::translation(-cx,-cy,-cz));
if (!children(parent.multiply(local), depth)) return false;
children_done = true;
} else if (!skip_value()) {
return err("Transform 字段值不完整: " + field);
}
}
if (!accept("}")) return err("Transform 花括号不匹配");
return true;
}
bool shape(const VrmlAffine& world, int depth) {
if (!accept("{")) return err("Shape 缺少 '{'");
bool geometry_seen = false;
while (!eof() && at() != "}") {
const std::string field = at();
++i_;
if (field == "geometry") {
if (!node(world, depth + 1)) return false;
geometry_seen = true;
} else if (field == "appearance") {
if (!skip_value() && !skip_braces()) return err("Appearance 结构不完整");
} else if (!skip_value()) {
return err("Shape 字段值不完整: " + field);
}
}
if (!accept("}")) return err("Shape 花括号不匹配");
if (!geometry_seen) return err("Shape 缺少 geometry");
return true;
}
bool indexed_face_set(const VrmlAffine& world, int depth) {
if (!accept("{")) return err("IndexedFaceSet 缺少 '{'");
std::vector<double> points;
std::vector<long long> indices;
bool have_points = false, have_indices = false;
while (!eof() && at() != "}") {
const std::string field = at();
++i_;
if (field == "coord") {
if (!coordinate(&points)) return false;
have_points = true;
} else if (field == "coordIndex") {
if (!int_array(&indices)) return false;
have_indices = true;
} else if (!skip_value()) {
return err("IndexedFaceSet 字段值不完整: " + field);
}
}
if (!accept("}")) return err("IndexedFaceSet 花括号不匹配");
if (!have_points || !have_indices)
return err("IndexedFaceSet 缺少 Coordinate.point 或 coordIndex");
if (points.size() % 3 != 0) return err("Coordinate.point 数量不是 3 的倍数");
const std::size_t vertex_count = points.size() / 3;
if (vertex_count == 0) return err("IndexedFaceSet 没有顶点");
if (mesh_->vertex_count() + vertex_count > kMaxVertices)
return err("VRML 顶点数超过上限");
const std::uint32_t base = static_cast<std::uint32_t>(mesh_->vertex_count());
for (std::size_t v = 0; v < vertex_count; ++v) {
double out[3] = {0, 0, 0};
world.apply(points[v*3], points[v*3+1], points[v*3+2], out);
mesh_->positions.push_back(out[0]);
mesh_->positions.push_back(out[1]);
mesh_->positions.push_back(out[2]);
}
std::vector<long long> face;
const auto flush = [&]() -> bool {
if (face.empty()) return true;
if (face.size() < 3 || face.size() > kMaxFaceVertices)
return err("面顶点数非法(需 3..4096)");
for (long long v : face)
if (v < 0 || static_cast<std::size_t>(v) >= vertex_count)
return err("coordIndex 索引越界");
for (std::size_t k = 1; k + 1 < face.size(); ++k) {
if (face[0] == face[k] || face[k] == face[k+1] || face[0] == face[k+1])
continue;
mesh_->indices.push_back(base + static_cast<std::uint32_t>(face[0]));
mesh_->indices.push_back(base + static_cast<std::uint32_t>(face[k]));
mesh_->indices.push_back(base + static_cast<std::uint32_t>(face[k+1]));
if (mesh_->triangle_count() > kMaxTriangles)
return err("VRML 三角面数超过上限");
}
face.clear();
return true;
};
for (long long v : indices) {
if (v == -1) { if (!flush()) return false; }
else face.push_back(v);
}
if (!flush()) return false;
if (mesh_->indices.empty()) return err("文件不含可用三角面");
return true;
}
bool coordinate(std::vector<double>* points) {
points->clear();
if (eof()) return err("Coordinate 缺失");
if (at() == "DEF") {
++i_;
if (eof()) return err("DEF 缺少名称");
const std::string name = at(); ++i_;
const std::size_t begin = i_;
if (!coordinate(points)) return false;
defs_[name] = {begin, i_};
return true;
}
if (at() == "USE") {
++i_;
if (eof()) return err("USE 缺少名称");
const std::string name = at(); ++i_;
const auto it = defs_.find(name);
if (it == defs_.end()) return err("USE 未定义的节点: " + name);
const std::size_t saved = i_;
i_ = it->second.first;
if (!coordinate(points)) return false;
i_ = saved;
return true;
}
if (!accept("Coordinate")) return err("coord 必须是 Coordinate 节点");
if (!accept("{")) return err("Coordinate 缺少 '{'");
bool seen = false;
while (!eof() && at() != "}") {
const std::string field = at(); ++i_;
if (field == "point") {
if (!number_array(points)) return false;
seen = true;
} else if (!skip_value()) {
return err("Coordinate 字段值不完整: " + field);
}
}
if (!accept("}")) return err("Coordinate 花括号不匹配");
return seen ? true : err("Coordinate 缺少 point");
}
const std::vector<std::string>& t_;
std::size_t i_ = 0;
MeshAccumulator* mesh_ = nullptr;
std::string* error_ = nullptr;
std::map<std::string, std::pair<std::size_t, std::size_t>> defs_;
std::size_t nodes_ = 0;
std::size_t expansions_ = 0;
};
/// 解析 VRML 2.0(Transform/DEF/USE/Material/Color;多 Shape 变换后合并为单网格)。
bool parse_vrml(const char* data, std::size_t size, MeshAccumulator* mesh,
std::string* error) {
if (!data || size == 0) {
if (error) *error = "vrml: 文件为空";
return false;
}
if (size > kMaxTextBytes) {
if (error) *error = "vrml: 文件过大(>1GiB)";
return false;
}
std::size_t begin = 0;
if (size >= 3 && static_cast<unsigned char>(data[0]) == 0xEF &&
static_cast<unsigned char>(data[1]) == 0xBB &&
static_cast<unsigned char>(data[2]) == 0xBF)
begin = 3;
while (begin < size && (data[begin] == ' ' || data[begin] == '\t' ||
data[begin] == '\r' || data[begin] == '\n'))
++begin;
const std::string header_text(data + begin, size - begin);
if (header_text.rfind("#VRML V2.0", 0) != 0) {
if (error) *error = "vrml: 缺少 #VRML V2.0 utf8 头(仅支持 VRML 2.0 ASCII)";
return false;
}
const std::vector<std::string> tokens = tokenize(data, size);
VrmlParser parser(tokens, mesh, error);
return parser.parse();
}
/// 实体三角化后生成 VRML 2.0 文本(每个 body 一个 Shape)。
int build_vrml_text(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
std::string* out) {
if (!out) return VDE_PLUGIN_ERR_INVALID_ARG;
out->clear();
std::string text = "#VRML V2.0 utf8\n";
char line[256];
bool wrote_geometry = false;
for (std::size_t i = 0; i < count; ++i) {
VdeMeshData mesh{};
mesh.struct_size = static_cast<std::uint32_t>(sizeof(VdeMeshData));
const int rc = host->body_tessellate(bodies[i], kWriteDeflection, &mesh);
const auto release = [&]() {
if (mesh.positions) host->free_buffer(mesh.positions);
if (mesh.indices) host->free_buffer(mesh.indices);
if (mesh.normals) host->free_buffer(mesh.normals);
};
if (rc != VDE_PLUGIN_OK) {
release();
return fail(host, rc, "vrml: 实体三角化失败");
}
if (!mesh.positions || !mesh.indices || mesh.num_vertices == 0 ||
mesh.num_triangles == 0) {
release();
continue;
}
text += "Shape {\n geometry IndexedFaceSet {\n solid FALSE\n"
" coord Coordinate { point [ ";
for (std::size_t v = 0; v < mesh.num_vertices; ++v) {
std::snprintf(line, sizeof(line), "%.17g %.17g %.17g%s",
mesh.positions[v * 3], mesh.positions[v * 3 + 1],
mesh.positions[v * 3 + 2], (v + 1 < mesh.num_vertices) ? ", " : " ");
text += line;
}
text += " ] }\n coordIndex [ ";
for (std::size_t t = 0; t < mesh.num_triangles; ++t) {
std::snprintf(line, sizeof(line), "%u, %u, %u, -1, ", mesh.indices[t * 3],
mesh.indices[t * 3 + 1], mesh.indices[t * 3 + 2]);
text += line;
}
text += " ]\n }\n}\n";
wrote_geometry = true;
release();
}
if (!wrote_geometry) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: 没有可导出的三角面");
*out = std::move(text);
return VDE_PLUGIN_OK;
}
int vrml_read_file(const VdePluginHost* host, const char* path, VdeBody** bodies,
std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: bodies/count 为空");
const int host_rc = validate_read_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
std::string bytes;
if (!path || !read_file_bytes(path, &bytes))
return fail(host, VDE_PLUGIN_ERR_IO, "vrml: 无法读取输入文件");
MeshAccumulator mesh;
std::string error;
if (!parse_vrml(bytes.data(), bytes.size(), &mesh, &error))
return fail(host, VDE_PLUGIN_ERR_PARSE, error.c_str());
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "vrml");
}
int vrml_read_memory(const VdePluginHost* host, const void* data, std::size_t size,
VdeBody** bodies, std::size_t* count) {
if (bodies) *bodies = nullptr;
if (count) *count = 0;
if (!bodies || !count) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: bodies/count 为空");
const int host_rc = validate_read_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (!data || size == 0) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: 内存数据为空");
MeshAccumulator mesh;
std::string error;
if (!parse_vrml(static_cast<const char*>(data), size, &mesh, &error))
return fail(host, VDE_PLUGIN_ERR_PARSE, error.c_str());
return vde_plugin_common::publish_single_body(host, mesh, bodies, count, "vrml");
}
int vrml_write_file(const VdePluginHost* host, const char* path, const VdeBody* bodies,
std::size_t count) {
if (!path) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: 输出路径为空");
const int host_rc = validate_write_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (count > 0 && !bodies) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: bodies 为空");
std::string text;
const int rc = build_vrml_text(host, bodies, count, &text);
if (rc != VDE_PLUGIN_OK) return rc;
if (!write_file_bytes(path, text.data(), text.size()))
return fail(host, VDE_PLUGIN_ERR_IO, "vrml: 无法写入输出文件");
return VDE_PLUGIN_OK;
}
int vrml_write_memory(const VdePluginHost* host, const VdeBody* bodies, std::size_t count,
VdeBuffer* out) {
if (out) {
out->data = nullptr;
out->size = 0;
}
if (!out) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: 输出缓冲为空");
const int host_rc = validate_write_host(host);
if (host_rc != VDE_PLUGIN_OK) return host_rc;
if (count > 0 && !bodies) return fail(host, VDE_PLUGIN_ERR_INVALID_ARG, "vrml: bodies 为空");
std::string text;
const int rc = build_vrml_text(host, bodies, count, &text);
if (rc != VDE_PLUGIN_OK) return rc;
void* data = host->alloc(text.size() + 1);
if (!data) return fail(host, VDE_PLUGIN_ERR_BUFFER, "vrml: 输出缓冲分配失败");
std::memcpy(data, text.c_str(), text.size() + 1);
out->data = data;
out->size = text.size();
return VDE_PLUGIN_OK;
}
int vrml_probe(const void* head, std::size_t head_size) {
if (!head || head_size == 0) return 0;
const char* data = static_cast<const char*>(head);
std::size_t begin = 0;
if (head_size >= 3 && static_cast<unsigned char>(data[0]) == 0xEF &&
static_cast<unsigned char>(data[1]) == 0xBB &&
static_cast<unsigned char>(data[2]) == 0xBF)
begin = 3;
while (begin < head_size && (data[begin] == ' ' || data[begin] == '\t' ||
data[begin] == '\r' || data[begin] == '\n'))
++begin;
if (head_size - begin >= 10 && std::strncmp(data + begin, "#VRML V2.0", 10) == 0)
return 95;
return 0;
}
const char* vrml_describe() {
return "VDE official VRML 2.0 IndexedFaceSet format (ASCII; read/write; polygons triangulated)";
}
const VdeFormatPlugin kFormat = [] {
VdeFormatPlugin format{};
format.struct_size = static_cast<std::uint32_t>(sizeof(VdeFormatPlugin));
format.format_id = "vrml";
format.extensions = ".wrl;.vrml";
format.probe = &vrml_probe;
format.read_file = &vrml_read_file;
format.read_memory = &vrml_read_memory;
format.write_file = &vrml_write_file;
format.write_memory = &vrml_write_memory;
format.describe = &vrml_describe;
return format;
}();
} // namespace
extern "C" {
VDE_PLUGIN_EXPORT int vde_plugin_query(std::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.official.vrml";
info->name = "VDE Official VRML 2.0 Format";
info->version = "1.0.0";
info->kind = VDE_PLUGIN_KIND_FORMAT;
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
info->capabilities = "format:vrml.read;format:vrml.write";
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_FORMAT;
reg->format = &kFormat;
reg->generic = nullptr;
reg->cae_solver = nullptr;
reg->cam_strategy = nullptr;
reg->gcs_solver = nullptr;
reg->mesher = nullptr;
reg->optimizer = nullptr;
reg->io_batch = nullptr;
return VDE_PLUGIN_OK;
}
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
} // extern "C"