Add BBox class for calculating entity bounding boxes.

pull/4/head
EvilSpirit 2016-01-23 14:05:02 +06:00 committed by whitequark
parent 89eb208660
commit fc68804f65
4 changed files with 82 additions and 0 deletions

View File

@ -493,4 +493,19 @@ public:
}
};
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

View File

@ -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;
}

View File

@ -701,6 +701,8 @@ public:
// Styles are handled a bit differently.
void Clear(void);
BBox CalculateEntityBBox(bool includingInvisible);
};
#undef ENTITY
#undef CONSTRAINT

View File

@ -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;
}