You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
inlinevec3_t& normalize() {
constvalue_t ls = lengthSquared();
if(ls==0) {
std::cerr<<"Cannot normalize a vector with zero length."<<std::endl;
return *this;
}elseif( std::abs(ls-static_cast<value_t>(1.0)) <= epsilon ){
return *this;
}else{
constvalue_t l = 1/std::sqrt(ls);
vec[0] *= l;
vec[1] *= l;
vec[2] *= l;
return *this;
}
}
Calculate sqrt only when necessary.
Don't normalize a vector that is almost 1 (due to floating point errors). Dividing those numbers won't result in a vector that has exactly a length of 1.0 anyway. The question is: what is a good choice for epsilon? It should be chosen in a way, that a normalized vector is not touched when normalized again.
The text was updated successfully, but these errors were encountered:
Initially reported by @ClaudiusJ as #645:
The text was updated successfully, but these errors were encountered: