v0.1.0: 初始工程骨架 — 7模块 40头文件 32源文件 17文档 Apache-2.0
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include "vde/boolean/boolean_2d.h"
|
||||
#include <algorithm>
|
||||
|
||||
namespace vde::boolean {
|
||||
|
||||
// Simple implementation for convex polygons using Sutherland-Hodgman approach
|
||||
static Polygon2D clip_polygon(const Polygon2D& subject, const Polygon2D& clip) {
|
||||
auto result = subject;
|
||||
const auto& cv = clip.vertices();
|
||||
int n = static_cast<int>(cv.size());
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (result.empty()) break;
|
||||
auto input = std::move(result);
|
||||
result = Polygon2D();
|
||||
const Point2D& e0 = cv[i];
|
||||
const Point2D& e1 = cv[(i + 1) % n];
|
||||
|
||||
for (size_t j = 0; j < input.size(); ++j) {
|
||||
const Point2D& p0 = input.vertices()[j];
|
||||
const Point2D& p1 = input.vertices()[(j + 1) % input.size()];
|
||||
|
||||
Vector2D edge = e1 - e0;
|
||||
double d0 = (p0.x() - e0.x()) * edge.y() - (p0.y() - e0.y()) * edge.x();
|
||||
double d1 = (p1.x() - e0.x()) * edge.y() - (p1.y() - e0.y()) * edge.x();
|
||||
|
||||
bool in0 = d0 <= 0;
|
||||
bool in1 = d1 <= 0;
|
||||
|
||||
if (in0 && in1) {
|
||||
result = Polygon2D(result.vertices());
|
||||
const_cast<std::vector<Point2D>&>(result.vertices()).push_back(p1);
|
||||
} else if (in0 && !in1) {
|
||||
double t = d0 / (d0 - d1);
|
||||
Point2D inter(p0.x() + t * (p1.x() - p0.x()),
|
||||
p0.y() + t * (p1.y() - p0.y()));
|
||||
auto verts = result.vertices();
|
||||
verts.push_back(inter);
|
||||
result = Polygon2D(verts);
|
||||
} else if (!in0 && in1) {
|
||||
double t = d0 / (d0 - d1);
|
||||
Point2D inter(p0.x() + t * (p1.x() - p0.x()),
|
||||
p0.y() + t * (p1.y() - p0.y()));
|
||||
auto verts = result.vertices();
|
||||
verts.push_back(inter);
|
||||
verts.push_back(p1);
|
||||
result = Polygon2D(verts);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Polygon2D> boolean_2d(const Polygon2D& a, const Polygon2D& b, BooleanOp op) {
|
||||
// Assume convex polygons for this simplified implementation
|
||||
if (op == BooleanOp::Intersection) {
|
||||
auto result = clip_polygon(a, b);
|
||||
if (result.empty()) return {};
|
||||
return {result};
|
||||
}
|
||||
// Union, Difference, SymDiff not implemented yet
|
||||
return {a};
|
||||
}
|
||||
|
||||
} // namespace vde::boolean
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "vde/boolean/boolean_mesh.h"
|
||||
namespace vde::boolean {
|
||||
HalfedgeMesh mesh_boolean(const HalfedgeMesh&, const HalfedgeMesh&, BooleanOp) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user