Skip to content

Commit

Permalink
Add BBox class for calculating entity bounding boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evil-Spirit authored and whitequark committed Feb 13, 2016
1 parent 89eb208 commit fc68804
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/dsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions src/solvespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/solvespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ class Sketch {
// Styles are handled a bit differently.

void Clear(void);

BBox CalculateEntityBBox(bool includingInvisible);
};
#undef ENTITY
#undef CONSTRAINT
Expand Down
31 changes: 31 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit fc68804

Please sign in to comment.