Something went wrong. Try again.
Builds CAD models from 3d scan data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351#include "align.hpp"
#include <algorithm>#include <array>#include <cmath>#include <cstdio>#include <string>#include <vector>
namespace {
using align::Feature;using align::FeatureKind;using align::Result;using align::Slot;using geom::dot;using geom::norm;using geom::normalize;using geom::Vec3;
constexpr double PI = 3.14159265358979323846;int failures = 0;
void check(const std::string& name, bool passed, const std::string& detail = {}) { std::printf("%-38s %s%s%s\n", name.c_str(), passed ? "PASS" : "FAIL", detail.empty() ? "" : " ", detail.c_str()); if (!passed) ++failures;}
bool close(double a, double b, double eps = 1e-9) { return std::fabs(a - b) <= eps;}
bool close(const Vec3& a, const Vec3& b, double eps = 1e-9) { return norm(a - b) <= eps;}
double determinant(const align::Mat3& R) { return R[0] * (R[4] * R[8] - R[5] * R[7]) - R[1] * (R[3] * R[8] - R[5] * R[6]) + R[2] * (R[3] * R[7] - R[4] * R[6]);}
bool properRotation(const align::Mat3& R) { double maxError = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { double value = 0; for (int k = 0; k < 3; ++k) value += R[3 * k + i] * R[3 * k + j]; maxError = std::max(maxError, std::fabs(value - (i == j ? 1.0 : 0.0))); } return maxError <= 1e-12 && std::fabs(determinant(R) - 1.0) <= 1e-12;}
bool locks(const std::array<bool, 3>& actual, bool x, bool y, bool z) { return actual == std::array<bool, 3>{x, y, z};}
struct Fixture { Vec3 planeNormal = normalize(Vec3{std::sin(PI / 6.0), 0, std::cos(PI / 6.0)}); Vec3 planeOrigin{4, -3, 8}; Vec3 planeU; Vec3 planeV; Vec3 cylinderAxis; Vec3 cylinderAnchor; std::vector<Feature> features;
Fixture() { geom::basisFromNormal(planeNormal, planeU, planeV); cylinderAxis = normalize(planeU * std::cos(40.0 * PI / 180.0) + planeV * std::sin(40.0 * PI / 180.0)); cylinderAnchor = planeOrigin + planeU * 7.0 + planeV * -2.0; features = { {1, FeatureKind::Plane, planeOrigin, planeNormal}, {2, FeatureKind::LineLike, cylinderAnchor, cylinderAxis}, {3, FeatureKind::PointLike, cylinderAnchor, {}}, }; }};
void planePrimaryCase() { Fixture f; Result r = align::buildFrame(f.features, {{1, "Z", false}}); Vec3 normal = align::transformDirection(r.R, f.planeNormal); Vec3 origin = align::transformPoint(r.R, r.t, f.planeOrigin); bool passed = r.ok && close(normal, {0, 0, 1}) && close(origin.z, 0) && locks(r.rotLocked, true, true, false) && locks(r.transLocked, false, false, true) && !r.fullyDefined && properRotation(r.R); check("tilted plane primary -> Z", passed, r.error);}
void sequentialCase() { Fixture f; Result two = align::buildFrame(f.features, {{1, "Z", false}, {2, "X", false}}); Vec3 axis = align::transformDirection(two.R, f.cylinderAxis); Vec3 axisPoint = align::transformPoint(two.R, two.t, f.cylinderAnchor); bool twoPassed = two.ok && close(axis, {1, 0, 0}) && close(axisPoint.y, 0) && close(axisPoint.z, 0) && locks(two.rotLocked, true, true, true) && locks(two.transLocked, false, true, true) && !two.fullyDefined && two.overconstrained.empty() && properRotation(two.R); check("plane + cylinder sequential", twoPassed, two.error);
Result three = align::buildFrame( f.features, {{1, "Z", false}, {2, "X", false}, {3, "ignored", false}}); Vec3 point = align::transformPoint(three.R, three.t, f.cylinderAnchor); bool threePassed = three.ok && close(point, {0, 0, 0}) && three.fullyDefined && locks(three.transLocked, true, true, true) && three.overconstrained.empty() && properRotation(three.R); check("tertiary point locks free translation", threePassed, three.error);}
void targetAndParallelCases() { const Vec3 cylinderAxis = normalize({0.2, -0.1, 0.97}); Vec3 planeX, planeOther; geom::basisFromNormal(cylinderAxis, planeX, planeOther); std::vector<Feature> features = { {10, FeatureKind::LineLike, {1, 2, 3}, cylinderAxis}, {11, FeatureKind::Plane, {0, 0, 0}, planeX}, {12, FeatureKind::Plane, {0, 0, 0}, normalize(cylinderAxis + planeX * 0.01)}, }; Result same = align::buildFrame(features, {{10, "Z", false}, {11, "Z", false}}); check("same direction target rejected", !same.ok && same.error == "secondary target conflicts with primary", same.error);
Result different = align::buildFrame(features, {{10, "Z", false}, {11, "X", false}}); check("cylinder Z + plane X", different.ok && close(align::transformDirection(different.R, cylinderAxis), {0, 0, 1}) && close(align::transformDirection(different.R, planeX), {1, 0, 0}) && properRotation(different.R), different.error);
Result parallel = align::buildFrame(features, {{10, "Z", false}, {12, "X", false}}); check("secondary parallel rejected", !parallel.ok && parallel.error == "secondary is parallel to primary", parallel.error);}
void flipAndTranslateCases() { Fixture f; Result flipped = align::buildFrame(f.features, {{1, "Z", true}}); check("flip reverses mapped direction", flipped.ok && close(align::transformDirection(flipped.R, f.planeNormal), {0, 0, -1}) && properRotation(flipped.R), flipped.error);
std::vector<Feature> point = { {20, FeatureKind::PointLike, {1, 2, 3}, {}}}; Result translated = align::buildFrame(point, {{20, "", false}}, {4, -5, 6}); check("numeric offset is in new frame", translated.ok && close(align::transformPoint(translated.R, translated.t, point[0].anchor), {4, -5, 6}) && locks(translated.transLocked, true, true, true) && properRotation(translated.R), translated.error);}
void overconstraintAndRoundTripCases() { Fixture f; Feature conflictingPoint{4, FeatureKind::PointLike, f.cylinderAnchor + f.planeV * 3.0, {}}; f.features.push_back(conflictingPoint); Result r = align::buildFrame( f.features, {{1, "Z", false}, {2, "X", false}, {4, "", false}}); Vec3 planeOrigin = align::transformPoint(r.R, r.t, f.planeOrigin); Vec3 cylinderPoint = align::transformPoint(r.R, r.t, f.cylinderAnchor); Vec3 point = align::transformPoint(r.R, r.t, conflictingPoint.anchor); bool passed = r.ok && close(planeOrigin.z, 0) && close(cylinderPoint.y, 0) && close(cylinderPoint.z, 0) && close(point.x, 0) && !r.overconstrained.empty() && properRotation(r.R); check("first-writer translation round trip", passed, r.error);
std::vector<Feature> points = { {30, FeatureKind::PointLike, {1, 2, 3}, {}}, {31, FeatureKind::PointLike, {4, 5, 6}, {}}}; Result pointConflict = align::buildFrame(points, {{30, "", false}, {31, "", false}}); check("two point origins report conflict", pointConflict.ok && pointConflict.overconstrained.size() == 3, pointConflict.error);}
void meshApplyCase() { mesh::Mesh mesh; mesh.verts = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}; mesh.tris = {{0, 1, 2}}; mesh.adj = {{1, 2}, {0, 2}, {0, 1}}; mesh.curv = {0, 0, 0}; align::Mat3 R{0, -1, 0, 1, 0, 0, 0, 0, 1}; align::applyTo(mesh, R, {2, 3, 4}); check("mesh apply refreshes derived data", close(mesh.verts[1], {2, 4, 4}) && mesh.vnormals.size() == 3 && close(mesh.vnormals[0], {0, 0, 1}) && mesh.adj.empty() && mesh.curv.empty() && close(mesh.bbMin, {1, 3, 4}) && close(mesh.bbMax, {2, 4, 4}));}
// A block whose bottom face is z=0, back face y=0, left face x=0, posed// arbitrarily in scan space. 3-2-1 picks on those faces must recover the// block's own frame exactly: plane -> Z, line -> X, point -> X origin.void pointsCase() { align::Mat3 pose{0.36, 0.48, -0.8, -0.8, 0.6, 0, 0.48, 0.64, 0.6}; Vec3 shift{120, -35, 410}; auto toScan = [&](const Vec3& q) { return align::transformPoint(pose, shift, q); }; align::PointsSpec spec; spec.points = {toScan({10, 10, 0}), toScan({50, 10, 0}), toScan({30, 40, 0}), // bottom toScan({10, 0, 5}), toScan({60, 0, 9}), // back toScan({0, 20, 7})}; // left spec.planeHint = align::transformDirection(pose, {0, 0, 1}); Result r = align::buildFrameFromPoints(spec); Vec3 probe{17, 23, 31}; bool passed = r.ok && r.fullyDefined && r.overconstrained.empty() && properRotation(r.R) && close(align::transformPoint(r.R, r.t, toScan(probe)), probe, 1e-9); check("3-2-1 points recover block frame", passed, r.error); check("axes are the new frame in scan coords", close(r.axes[0], align::transformDirection(pose, {1, 0, 0})) && close(r.axes[1], align::transformDirection(pose, {0, 1, 0})) && close(r.axes[2], align::transformDirection(pose, {0, 0, 1})) && close(r.origin, shift));
spec.planeHint = {}; spec.flipPlane = false; std::swap(spec.points[1], spec.points[2]); // left-handed pick order Result hintless = align::buildFrameFromPoints(spec); check("pick order sets normal w/o hint", hintless.ok && close(hintless.axes[2], align::transformDirection(pose, {0, 0, -1})), hintless.error);
align::PointsSpec three; three.points = {spec.points.begin(), spec.points.begin() + 3}; Result partial = align::buildFrameFromPoints(three); check("3 points lock plane only", partial.ok && !partial.fullyDefined && locks(partial.rotLocked, true, true, false) && locks(partial.transLocked, false, false, true), partial.error);
align::PointsSpec collinear; collinear.points = {{0, 0, 0}, {1, 1, 1}, {2, 2, 2}}; check("collinear primary rejected", !align::buildFrameFromPoints(collinear).ok);
align::PointsSpec vertical = spec; vertical.points.resize(5); vertical.points[3] = toScan({10, 0, 0}); vertical.points[4] = toScan({10, 0, 9}); // along the plane normal check("secondary along normal rejected", !align::buildFrameFromPoints(vertical).ok);
align::PointsSpec four = spec; four.points.resize(4); check("4 points rejected", !align::buildFrameFromPoints(four).ok);
// Per-point nominal values, picks NOT on the datum faces: every pick gets // its true block coordinate as its value, so the exact solve must recover // the pose regardless of the plane tilt the unequal values imply. struct RuleCase { const char* rule; int a, b, c; }; for (RuleCase rc : {RuleCase{"ZZZ-YY-X", 2, 1, 0}, RuleCase{"XXX-YY-Z", 0, 1, 2}, RuleCase{"YYY-ZZ-X", 1, 2, 0}}) { std::array<Vec3, 6> q = {Vec3{12, 7, 3}, Vec3{55, 14, 9}, Vec3{30, 48, 6}, Vec3{8, 2, 11}, Vec3{70, 9, 31}, Vec3{4, 25, 18}}; auto comp = [](const Vec3& p, int k) { return k == 0 ? p.x : k == 1 ? p.y : p.z; }; align::PointsSpec vs; vs.rule = rc.rule; for (int k = 0; k < 6; ++k) { vs.points.push_back(toScan(q[k])); vs.values[k] = comp(q[k], k < 3 ? rc.a : k < 5 ? rc.b : rc.c); } Vec3 up{0, 0, 0}; if (rc.a == 0) up.x = 1; else if (rc.a == 1) up.y = 1; else up.z = 1; vs.planeHint = align::transformDirection(pose, up); // "line positive": +C must run from line point 1 to 2 — flip if not. vs.flipLine = comp(q[4], rc.c) < comp(q[3], rc.c); Result rv = align::buildFrameFromPoints(vs); check(std::string("values solve exactly ") + rc.rule, rv.ok && rv.fullyDefined && properRotation(rv.R) && close(align::transformPoint(rv.R, rv.t, toScan(probe)), probe, 1e-7), rv.error); }
align::PointsSpec far = spec; far.values = {0, 500, 0, 0, 0, 0}; check("impossible plane values rejected", !align::buildFrameFromPoints(far).ok); align::PointsSpec badRule = spec; badRule.rule = "ZZZ-ZZ-X"; check("bad rule rejected", !align::buildFrameFromPoints(badRule).ok);}
} // namespace
void rigidCase() { bool rotsOk = true; for (const char* a : {"X", "Y", "Z"}) rotsOk = rotsOk && properRotation(align::rot90(a)); Result r = align::fromRigid(align::rot90("Z"), {0, 0, -5}); check("rot90 + shift rigid move", rotsOk && r.ok && r.fullyDefined && close(align::transformPoint(r.R, r.t, {1, 0, 0}), {0, 1, -5}) && close(align::transformPoint(r.R, r.t, {0, 0, 5}), {0, 0, 0}));}
int main() { pointsCase(); rigidCase(); planePrimaryCase(); sequentialCase(); targetAndParallelCases(); flipAndTranslateCases(); overconstraintAndRoundTripCases(); meshApplyCase(); std::printf("\n%d failure(s)\n", failures); return failures == 0 ? 0 : 1;}