From d4d5ab578de59768f8a6f2b6179ec46c9968ca11 Mon Sep 17 00:00:00 2001 From: Jonathan Westhues Date: Wed, 2 Jul 2008 01:52:32 -0800 Subject: [PATCH] Add a special case when raising quaternions almost equal to (+/-1, 0, 0, 0) to some power; that was failing if the magnitude of w was slightly greater than 1, so that acos() returned NaN. [git-p4: depot-paths = "//depot/solvespace/": change = 1820] --- util.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/util.cpp b/util.cpp index ea9c5040..0fcaa1c4 100644 --- a/util.cpp +++ b/util.cpp @@ -225,6 +225,13 @@ Quaternion Quaternion::Inverse(void) { } Quaternion Quaternion::ToThe(double p) { + // Avoid division by zero, or arccos of something not in its domain + if(w >= (1 - 1e-6)) { + return From(1, 0, 0, 0); + } else if(w <= (-1 + 1e-6)) { + return From(-1, 0, 0, 0); + } + Quaternion r; Vector axis = Vector::From(vx, vy, vz); double theta = acos(w); // okay, since magnitude is 1, so -1 <= w <= 1