Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// geom.hpp — lightweight geometry + tiny linear algebra (no OpenCASCADE dependency).// Used by the mesh loader and the primitive fitters so those stay kernel-independent.#pragma once#include <array>#include <cmath>#include <vector>
namespace geom {
struct Vec3 { double x = 0, y = 0, z = 0; Vec3() = default; Vec3(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {}
Vec3 operator+(const Vec3& o) const { return {x + o.x, y + o.y, z + o.z}; } Vec3 operator-(const Vec3& o) const { return {x - o.x, y - o.y, z - o.z}; } Vec3 operator*(double s) const { return {x * s, y * s, z * s}; } Vec3 operator/(double s) const { return {x / s, y / s, z / s}; }};
inline double dot(const Vec3& a, const Vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }inline Vec3 cross(const Vec3& a, const Vec3& b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};}inline double norm(const Vec3& a) { return std::sqrt(dot(a, a)); }inline Vec3 normalize(const Vec3& a) { double n = norm(a); return n > 1e-12 ? a / n : Vec3{0, 0, 1};}
// Build an orthonormal basis (u, v) spanning the plane whose normal is n.inline void basisFromNormal(const Vec3& n_in, Vec3& u, Vec3& v) { Vec3 n = normalize(n_in); Vec3 ref = (std::fabs(n.x) < 0.9) ? Vec3{1, 0, 0} : Vec3{0, 1, 0}; u = normalize(cross(n, ref)); v = normalize(cross(n, u));}
// Symmetric 3x3 eigen-decomposition via cyclic Jacobi rotations.// Returns eigenvalues (ascending) and corresponding eigenvectors as columns of `vecs`.inline void jacobiEigen3(const double A_in[3][3], double evals[3], double vecs[3][3]) { double a[3][3]; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) a[i][j] = A_in[i][j]; double V[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
for (int sweep = 0; sweep < 50; ++sweep) { double off = std::fabs(a[0][1]) + std::fabs(a[0][2]) + std::fabs(a[1][2]); if (off < 1e-18) break; for (int p = 0; p < 2; ++p) { for (int q = p + 1; q < 3; ++q) { if (std::fabs(a[p][q]) < 1e-300) continue; double theta = (a[q][q] - a[p][p]) / (2.0 * a[p][q]); double t = (theta >= 0 ? 1.0 : -1.0) / (std::fabs(theta) + std::sqrt(theta * theta + 1.0)); double c = 1.0 / std::sqrt(t * t + 1.0); double s = t * c; // Rotate a for (int i = 0; i < 3; ++i) { double aip = a[i][p], aiq = a[i][q]; a[i][p] = c * aip - s * aiq; a[i][q] = s * aip + c * aiq; } for (int i = 0; i < 3; ++i) { double api = a[p][i], aqi = a[q][i]; a[p][i] = c * api - s * aqi; a[q][i] = s * api + c * aqi; } // Accumulate eigenvectors for (int i = 0; i < 3; ++i) { double vip = V[i][p], viq = V[i][q]; V[i][p] = c * vip - s * viq; V[i][q] = s * vip + c * viq; } } } } // Sort ascending by eigenvalue int idx[3] = {0, 1, 2}; double d[3] = {a[0][0], a[1][1], a[2][2]}; for (int i = 0; i < 3; ++i) for (int j = i + 1; j < 3; ++j) if (d[idx[j]] < d[idx[i]]) std::swap(idx[i], idx[j]); for (int k = 0; k < 3; ++k) { evals[k] = d[idx[k]]; for (int i = 0; i < 3; ++i) vecs[i][k] = V[i][idx[k]]; }}
} // namespace geom