Add center to a mesh to allow easy translation
This commit is contained in:
parent
c179b1049f
commit
8655e39fba
3 changed files with 64 additions and 0 deletions
27
Mesh.cpp
27
Mesh.cpp
|
@ -1,6 +1,7 @@
|
|||
#include "Mesh.hpp"
|
||||
|
||||
Mesh::Mesh()
|
||||
: center(Point(.0, .0, .0))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -16,6 +17,32 @@ void Mesh::add_face(size_t f1, size_t f2, size_t f3) {
|
|||
add_face(Face(f1, f2, f3));
|
||||
}
|
||||
|
||||
const Point& Mesh::get_center() const {
|
||||
return center;
|
||||
}
|
||||
|
||||
void Mesh::set_center(const Point& pt) {
|
||||
center = pt;
|
||||
}
|
||||
void Mesh::translate(const Point& tr) {
|
||||
center += tr;
|
||||
}
|
||||
|
||||
void Mesh::normalize_center(bool keep_position) {
|
||||
Point bary(0., 0., 0.);
|
||||
for(const Point& vert: vertices)
|
||||
bary += vert;
|
||||
bary = (1. / ((double)vertices.size())) * bary;
|
||||
|
||||
for(Point& vert: vertices)
|
||||
vert -= bary;
|
||||
|
||||
if(keep_position)
|
||||
translate(bary);
|
||||
else
|
||||
set_center(Point(0, 0, 0));
|
||||
}
|
||||
|
||||
const std::vector<Point>& Mesh::get_vertices() const {
|
||||
return vertices;
|
||||
}
|
||||
|
|
13
Mesh.hpp
13
Mesh.hpp
|
@ -20,6 +20,17 @@ class Mesh {
|
|||
void add_face(const Face& face);
|
||||
void add_face(size_t f1, size_t f2, size_t f3);
|
||||
|
||||
/// Center manipulation
|
||||
const Point& get_center() const;
|
||||
void set_center(const Point& pt);
|
||||
void translate(const Point& tr); ///< Translate by the vector `tr`
|
||||
|
||||
/** Translates the vertices to make (0, 0, 0) the mesh's barycenter.
|
||||
* If `keep_position == true`, this also sets the center to the
|
||||
* previous barycenter.
|
||||
*/
|
||||
void normalize_center(bool keep_position=false);
|
||||
|
||||
/// Gets the various vertices
|
||||
const std::vector<Point>& get_vertices() const;
|
||||
|
||||
|
@ -29,4 +40,6 @@ class Mesh {
|
|||
private:
|
||||
std::vector<Point> vertices;
|
||||
std::vector<Face> faces;
|
||||
|
||||
Point center;
|
||||
};
|
||||
|
|
|
@ -21,6 +21,30 @@ struct Point {
|
|||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Point operator+(const Point& pt) const {
|
||||
return Point(
|
||||
x + pt.x,
|
||||
y + pt.y,
|
||||
z + pt.z);
|
||||
}
|
||||
Point& operator+=(const Point& pt) {
|
||||
x += pt.x;
|
||||
y += pt.y;
|
||||
z += pt.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Point& operator-=(const Point& pt) {
|
||||
return (*this += Point(-pt.x, -pt.y, -pt.z));
|
||||
}
|
||||
|
||||
friend Point operator*(double scalar, const Point& pt) {
|
||||
return Point(
|
||||
scalar * pt.x,
|
||||
scalar * pt.y,
|
||||
scalar * pt.z);
|
||||
}
|
||||
};
|
||||
|
||||
struct Face {
|
||||
|
|
Loading…
Reference in a new issue