Optimization: Cache boundingbox, added append_poly with Polygon parameter

master
Marius Kintel 2015-03-08 19:32:10 -04:00
parent 485edd57be
commit 30f5343834
2 changed files with 30 additions and 11 deletions

View File

@ -47,11 +47,11 @@
*/ */
PolySet::PolySet(unsigned int dim, boost::tribool convex) : dim(dim), convex(convex) PolySet::PolySet(unsigned int dim, boost::tribool convex) : dim(dim), convex(convex), dirty(false)
{ {
} }
PolySet::PolySet(const Polygon2d &origin) : polygon(origin), dim(2), convex(unknown) PolySet::PolySet(const Polygon2d &origin) : polygon(origin), dim(2), convex(unknown), dirty(false)
{ {
} }
@ -87,6 +87,11 @@ void PolySet::append_poly()
polygons.push_back(Polygon()); polygons.push_back(Polygon());
} }
void PolySet::append_poly(const Polygon &poly)
{
polygons.push_back(poly);
}
void PolySet::append_vertex(double x, double y, double z) void PolySet::append_vertex(double x, double y, double z)
{ {
append_vertex(Vector3d(x, y, z)); append_vertex(Vector3d(x, y, z));
@ -95,11 +100,12 @@ void PolySet::append_vertex(double x, double y, double z)
void PolySet::append_vertex(const Vector3d &v) void PolySet::append_vertex(const Vector3d &v)
{ {
polygons.back().push_back(v); polygons.back().push_back(v);
this->dirty = true;
} }
void PolySet::append_vertex(const Vector3f &v) void PolySet::append_vertex(const Vector3f &v)
{ {
polygons.back().push_back(v.cast<double>()); append_vertex((const Vector3d &)v.cast<double>());
} }
void PolySet::insert_vertex(double x, double y, double z) void PolySet::insert_vertex(double x, double y, double z)
@ -110,24 +116,26 @@ void PolySet::insert_vertex(double x, double y, double z)
void PolySet::insert_vertex(const Vector3d &v) void PolySet::insert_vertex(const Vector3d &v)
{ {
polygons.back().insert(polygons.back().begin(), v); polygons.back().insert(polygons.back().begin(), v);
this->dirty = true;
} }
void PolySet::insert_vertex(const Vector3f &v) void PolySet::insert_vertex(const Vector3f &v)
{ {
polygons.back().insert(polygons.back().begin(), v.cast<double>()); insert_vertex((const Vector3d &)v.cast<double>());
} }
BoundingBox PolySet::getBoundingBox() const BoundingBox PolySet::getBoundingBox() const
{ {
BoundingBox bbox; if (this->dirty) {
for (size_t i = 0; i < polygons.size(); i++) { this->bbox.setNull();
const Polygon &poly = polygons[i]; BOOST_FOREACH(const Polygon &poly, polygons) {
for (size_t j = 0; j < poly.size(); j++) { BOOST_FOREACH(const Vector3d &p, poly) {
const Vector3d &p = poly[j]; this->bbox.extend(p);
bbox.extend(p); }
} }
this->dirty = false;
} }
return bbox; return this->bbox;
} }
size_t PolySet::memsize() const size_t PolySet::memsize() const
@ -142,6 +150,9 @@ size_t PolySet::memsize() const
void PolySet::append(const PolySet &ps) void PolySet::append(const PolySet &ps)
{ {
this->polygons.insert(this->polygons.end(), ps.polygons.begin(), ps.polygons.end()); this->polygons.insert(this->polygons.end(), ps.polygons.begin(), ps.polygons.end());
if (!dirty && !this->bbox.isNull()) {
this->bbox.extend(ps.getBoundingBox());
}
} }
void PolySet::transform(const Transform3d &mat) void PolySet::transform(const Transform3d &mat)
@ -155,6 +166,11 @@ void PolySet::transform(const Transform3d &mat)
} }
if (mirrored) std::reverse(p.begin(), p.end()); if (mirrored) std::reverse(p.begin(), p.end());
} }
if (!dirty && !this->bbox.isNull()) {
this->bbox.min() = mat * this->bbox.min();
this->bbox.max() = mat * this->bbox.max();
}
} }
bool PolySet::is_convex() const { bool PolySet::is_convex() const {

View File

@ -31,6 +31,7 @@ public:
void quantizeVertices(); void quantizeVertices();
size_t numPolygons() const { return polygons.size(); } size_t numPolygons() const { return polygons.size(); }
void append_poly(); void append_poly();
void append_poly(const Polygon &poly);
void append_vertex(double x, double y, double z = 0.0); void append_vertex(double x, double y, double z = 0.0);
void append_vertex(const Vector3d &v); void append_vertex(const Vector3d &v);
void append_vertex(const Vector3f &v); void append_vertex(const Vector3f &v);
@ -52,4 +53,6 @@ private:
Polygon2d polygon; Polygon2d polygon;
unsigned int dim; unsigned int dim;
mutable boost::tribool convex; mutable boost::tribool convex;
mutable BoundingBox bbox;
mutable bool dirty;
}; };