diff --git a/src/dsc.h b/src/dsc.h index c2c294149..5995dcb08 100644 --- a/src/dsc.h +++ b/src/dsc.h @@ -493,4 +493,19 @@ class RgbaColor { } }; +class BBox { +public: + Vector minp; + Vector maxp; + + BBox(); + BBox(const Vector &p0, const Vector &p1); + + Vector GetOrigin(); + Vector GetExtents(); + + void Include(const Vector &v, double r = 0.0); + bool Overlaps(BBox &b1); +}; + #endif diff --git a/src/solvespace.cpp b/src/solvespace.cpp index d9c343fba..fdc3d45f9 100644 --- a/src/solvespace.cpp +++ b/src/solvespace.cpp @@ -817,3 +817,37 @@ void Sketch::Clear(void) { entity.Clear(); param.Clear(); } + +BBox Sketch::CalculateEntityBBox(bool includingInvisible) { + BBox box; + bool first = true; + for(int i = 0; i < entity.n; i++) { + Entity *e = (Entity *)&entity.elem[i]; + if(!(e->IsVisible() || includingInvisible)) continue; + + Vector point; + double r = 0.0; + if(e->IsPoint()) { + point = e->PointGetNum(); + } else { + switch(e->type) { + case Entity::ARC_OF_CIRCLE: + case Entity::CIRCLE: + r = e->CircleGetRadiusNum(); + point = GetEntity(e->point[0])->PointGetNum(); + break; + default: continue; + } + } + + if(first) { + box.minp = point; + box.maxp = point; + box.Include(point, r); + first = false; + } else { + box.Include(point, r); + } + } + return box; +} diff --git a/src/solvespace.h b/src/solvespace.h index 1be244ac5..4ee1775f6 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -701,6 +701,8 @@ class Sketch { // Styles are handled a bit differently. void Clear(void); + + BBox CalculateEntityBBox(bool includingInvisible); }; #undef ENTITY #undef CONSTRAINT diff --git a/src/util.cpp b/src/util.cpp index 959026cb2..4cf1eca6b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1047,3 +1047,34 @@ bool Point2d::Equals(Point2d v, double tol) { return (this->Minus(v)).MagSquared() < tol*tol; } +BBox::BBox() { } +BBox::BBox(const Vector &p0, const Vector &p1) { + minp.x = min(p0.x, p1.x); + minp.y = min(p0.y, p1.y); + minp.z = min(p0.z, p1.z); + + maxp.x = max(p0.x, p1.x); + maxp.y = max(p0.y, p1.y); + maxp.z = max(p0.z, p1.z); +} + +Vector BBox::GetOrigin() { return minp.Plus(maxp.Minus(minp)).ScaledBy(0.5); } +Vector BBox::GetExtents() { return maxp.Minus(minp).ScaledBy(0.5); } + +void BBox::Include(const Vector &v, double r) { + minp.x = min(minp.x, v.x - r); + minp.y = min(minp.y, v.y - r); + minp.z = min(minp.z, v.z - r); + + maxp.x = max(maxp.x, v.x + r); + maxp.y = max(maxp.y, v.y + r); + maxp.z = max(maxp.z, v.z + r); +} + +bool BBox::Overlaps(BBox &b1) { + + Vector t = b1.GetOrigin().Minus(GetOrigin()); + Vector e = b1.GetExtents().Plus(GetExtents()); + + return fabs(t.x) < e.x && fabs(t.y) < e.y && fabs(t.z) < e.z; +}