From 6719a8e3d5d457a52b8cfe9ba392ecbf5ae89a7d Mon Sep 17 00:00:00 2001 From: Erik Hofman Date: Mon, 25 Apr 2022 10:48:07 +0200 Subject: [PATCH] Add functions to return the squared length of a vector: Comparing two squared values prevents two computionally heavy sqrt calls. --- simgear/math/SGVec2.hxx | 8 ++++++++ simgear/math/SGVec3.hxx | 9 +++++++++ simgear/math/SGVec4.hxx | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/simgear/math/SGVec2.hxx b/simgear/math/SGVec2.hxx index d9e4439f..87c6fe2f 100644 --- a/simgear/math/SGVec2.hxx +++ b/simgear/math/SGVec2.hxx @@ -231,6 +231,14 @@ T norm(const SGVec2& v) { return simd4::magnitude(v.simd2()); } +/// The squared euclidean norm of the vector +/// Comparing two squared values prevents two computionally heavy sqrt calls. +template +inline +T +norm2(const SGVec2& v) +{ return simd4::magnitude2(v.simd2()); } + /// The euclidean norm of the vector, that is what most people call length template inline diff --git a/simgear/math/SGVec3.hxx b/simgear/math/SGVec3.hxx index 514f9575..aa1061b8 100644 --- a/simgear/math/SGVec3.hxx +++ b/simgear/math/SGVec3.hxx @@ -301,6 +301,15 @@ T norm(const SGVec3& v) { return simd4::magnitude(v.simd3()); } +/// The squared euclidean norm of the vector +/// Comparing two squared values prevents two computionally heavy sqrt calls. +template +inline +T +norm2(const SGVec3& v) +{ return simd4::magnitude2(v.simd3()); } + + /// The euclidean norm of the vector, that is what most people call length template inline diff --git a/simgear/math/SGVec4.hxx b/simgear/math/SGVec4.hxx index b62561ab..bd66389f 100644 --- a/simgear/math/SGVec4.hxx +++ b/simgear/math/SGVec4.hxx @@ -249,6 +249,14 @@ T norm(const SGVec4& v) { return simd4::magnitude(v.simd4()); } +/// The squared euclidean norm of the vector +/// Comparing two squared values prevents two computionally heavy sqrt calls. +template +inline +T +norm2(const SGVec4& v) +{ return simd4::magnitude2(v.simd4()); } + /// The euclidean norm of the vector, that is what most people call length template inline