Normalize normals to norm 1

This commit is contained in:
Théophile Bastian 2018-02-13 16:14:28 +01:00
parent 4d100731a2
commit aaaebdc042
3 changed files with 18 additions and 5 deletions

View file

@ -8,11 +8,13 @@ Point ImplicitSurface::normal_at(const Point& pt) const {
// this is simply the gradient. // this is simply the gradient.
static const double d_dist = 1e-8; static const double d_dist = 1e-8;
return Point( Point normal(
(operator()(pt.x + d_dist, pt.y, pt.z) - (operator()(pt.x + d_dist, pt.y, pt.z) -
operator()(pt.x - d_dist, pt.y, pt.z)) / (2. * d_dist), operator()(pt.x - d_dist, pt.y, pt.z)) / (2. * d_dist),
(operator()(pt.x, pt.y + d_dist, pt.z) - (operator()(pt.x, pt.y + d_dist, pt.z) -
operator()(pt.x, pt.y - d_dist, pt.z)) / (2. * d_dist), operator()(pt.x, pt.y - d_dist, pt.z)) / (2. * d_dist),
(operator()(pt.x, pt.y, pt.z + d_dist) - (operator()(pt.x, pt.y, pt.z + d_dist) -
operator()(pt.x, pt.y, pt.z - d_dist)) / (2. * d_dist)); operator()(pt.x, pt.y, pt.z - d_dist)) / (2. * d_dist));
normal.normalize();
return normal;
} }

View file

@ -91,7 +91,7 @@ void Mesh::compute_normal(size_t vert) {
normal += e1.cross_prod(e2); normal += e1.cross_prod(e2);
} }
normal = (1. / ((double) faces_with_vert[vert].size())) * normal; normal.normalize();
normals[vert].dirty = false; normals[vert].dirty = false;
normals[vert].vect = normal; normals[vert].vect = normal;
} }

View file

@ -13,6 +13,17 @@ struct Point {
Point(double x, double y, double z) : x(x), y(y), z(z) {} Point(double x, double y, double z) : x(x), y(y), z(z) {}
double x, y, z; double x, y, z;
double norm() const {
auto sq = [](double x) { return x * x; };
return sqrt(sq(x) + sq(y) + sq(z));
}
void normalize() {
double _norm = norm();
x /= _norm;
y /= _norm;
z /= _norm;
}
double operator[](unsigned i) const { double operator[](unsigned i) const {
assert(i < 3); assert(i < 3);
switch(i) { switch(i) {