From 24bec379d5f1d59dfac2ff304b8ccbc64f5ae5b2 Mon Sep 17 00:00:00 2001 From: David Fioramonti Date: Sat, 19 Aug 2017 07:00:39 -0700 Subject: TITANIC: Pull assert out of dvector/fvector normalization Before the normalization function was asserting if it couldn't normalize now the caller can determine what to do with a failed normalization. --- engines/titanic/star_control/camera_auto_mover.cpp | 16 ++++++-- engines/titanic/star_control/dvector.cpp | 16 +++++--- engines/titanic/star_control/dvector.h | 11 +++++- engines/titanic/star_control/fmatrix.cpp | 12 +++++- engines/titanic/star_control/fvector.cpp | 17 ++++++--- engines/titanic/star_control/fvector.h | 10 ++++- engines/titanic/star_control/star_camera.cpp | 43 ++++++++++++++++------ .../titanic/star_control/unmarked_auto_mover.cpp | 7 +++- 8 files changed, 100 insertions(+), 32 deletions(-) (limited to 'engines/titanic') diff --git a/engines/titanic/star_control/camera_auto_mover.cpp b/engines/titanic/star_control/camera_auto_mover.cpp index 861248fc1f..bcb94d27c2 100644 --- a/engines/titanic/star_control/camera_auto_mover.cpp +++ b/engines/titanic/star_control/camera_auto_mover.cpp @@ -49,8 +49,12 @@ void CCameraAutoMover::proc2(const FVector &oldPos, const FVector &newPos, _srcPos = oldPos; _destPos = newPos; _posDelta = _destPos - _srcPos; - _distance = _posDelta.normalize(); - + float temp = 0.0; + if (!_posDelta.normalize(temp)) { // Do the normalization, put the scale amount in temp, + // but if it is unsuccessful, crash + assert(temp); + } + _distance = temp; _active = false; _field34 = false; _transitionPercent = 1.0; @@ -73,8 +77,12 @@ void CCameraAutoMover::setPath(const FVector &srcV, const FVector &destV, const _srcPos = srcV; _destPos = destV; _posDelta = _destPos - _srcPos; - _distance = _posDelta.normalize(); - + float temp = 0.0; + if (!_posDelta.normalize(temp)) { // Do the normalization, put the scale amount in temp, + // but if it is unsuccessful, crash + assert(temp); + } + _distance = temp; _active = false; _field34 = false; _field40 = -1; diff --git a/engines/titanic/star_control/dvector.cpp b/engines/titanic/star_control/dvector.cpp index c7481a2b49..405af4a0d4 100644 --- a/engines/titanic/star_control/dvector.cpp +++ b/engines/titanic/star_control/dvector.cpp @@ -26,14 +26,16 @@ namespace Titanic { -double DVector::normalize() { - double hyp = sqrt(_x * _x + _y * _y + _z * _z); - assert(hyp); +bool DVector::normalize(double & hyp) { + hyp = sqrt(_x * _x + _y * _y + _z * _z); + if (hyp==0) { + return false; + } _x *= 1.0 / hyp; _y *= 1.0 / hyp; _z *= 1.0 / hyp; - return hyp; + return true; } double DVector::getDistance(const DVector &src) { @@ -73,7 +75,11 @@ void DVector::rotVectAxisY(double angleDeg) { DVector DVector::getAnglesAsVect() const { DVector vector = *this; DVector dest; - dest._x = vector.normalize(); // scale that makes this vector have magnitude=1, also does the scaling + + if (!vector.normalize(dest._x)) { // Makes this vector have magnitude=1, put the scale amount in dest._x, + // but if it is unsuccessful, crash + assert(dest._x); + } dest._y = acos(vector._y); // radian distance/angle that this vector's y component is from the +y axis, // result is restricted to [0,pi] dest._z = atan2(vector._x,vector._z); // result is restricted to [-pi,pi] diff --git a/engines/titanic/star_control/dvector.h b/engines/titanic/star_control/dvector.h index bff271dd6f..f115abdaae 100644 --- a/engines/titanic/star_control/dvector.h +++ b/engines/titanic/star_control/dvector.h @@ -44,7 +44,16 @@ public: DVector(double x, double y, double z) : _x(x), _y(y), _z(z) {} DVector(const FVector &v) : _x(v._x), _y(v._y), _z(v._z) {} - double normalize(); + /** + * Attempts to normalizes the vector so the length from origin equals 1.0 + * Return value is whether or not it was successful in normalizing + * First argument is scale value that normalizes the vector + * TODO: split this function into 2. One that calculates the normalization + * and another that does the normalization. The 2nd would assert if a + * normalization of one was requested. This is cleaner than the current + * implementation. + */ + bool normalize(double &); /** * Returns the distance between this vector and the passed one diff --git a/engines/titanic/star_control/fmatrix.cpp b/engines/titanic/star_control/fmatrix.cpp index 050d89bc67..470569c88d 100644 --- a/engines/titanic/star_control/fmatrix.cpp +++ b/engines/titanic/star_control/fmatrix.cpp @@ -127,10 +127,18 @@ void FMatrix::set(const FVector &v) { _row2 = _row3.fn1(); _row1 = _row3.crossProduct(_row2); - _row1.normalize(); + + float unused_scale=0.0; + if (!_row1.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } _row2 = _row3.crossProduct(_row1); - _row2.normalize(); + if (!_row2.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } } void FMatrix::matRProd(const FMatrix &m) { diff --git a/engines/titanic/star_control/fvector.cpp b/engines/titanic/star_control/fvector.cpp index a107ad1ed2..75d30748d2 100644 --- a/engines/titanic/star_control/fvector.cpp +++ b/engines/titanic/star_control/fvector.cpp @@ -48,20 +48,25 @@ FVector FVector::crossProduct(const FVector &src) const { ); } -float FVector::normalize() { - float hyp = sqrt(_x * _x + _y * _y + _z * _z); - assert(hyp); +bool FVector::normalize(float & hyp) { + hyp = sqrt(_x * _x + _y * _y + _z * _z); + if (hyp==0) { + return false; + } _x *= 1.0 / hyp; _y *= 1.0 / hyp; _z *= 1.0 / hyp; - return hyp; + return true; } FVector FVector::addAndNormalize(const FVector &v) const { FVector tempV(_x + v._x, _y + v._y, _z + v._z); - tempV.normalize(); - + float unused_scale=0.0; + if (!tempV.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } return tempV; } diff --git a/engines/titanic/star_control/fvector.h b/engines/titanic/star_control/fvector.h index fa24fe58c1..f93ac60c26 100644 --- a/engines/titanic/star_control/fvector.h +++ b/engines/titanic/star_control/fvector.h @@ -59,9 +59,15 @@ public: FVector crossProduct(const FVector &src) const; /** - * Normalizes the vector so the length from origin equals 1.0 + * Attempts to normalizes the vector so the length from origin equals 1.0 + * Return value is whether or not it was successful in normalizing + * First argument is scale value that normalizes the vector + * TODO: split this function into 2. One that calculates the normalization + * and another that does the normalization. The 2nd would assert if a + * normalization of one was requested. This is cleaner than the current + * implementation. */ - float normalize(); + bool normalize(float &); /** * Adds the current vector and a passed one together, normalizes them, diff --git a/engines/titanic/star_control/star_camera.cpp b/engines/titanic/star_control/star_camera.cpp index c74a1d946d..1d543b376d 100644 --- a/engines/titanic/star_control/star_camera.cpp +++ b/engines/titanic/star_control/star_camera.cpp @@ -276,9 +276,14 @@ void CStarCamera::setViewportAngle(const FPoint &angles) { tempV4 -= tempV1; tempV5 -= tempV1; tempV6 -= tempV1; - tempV4.normalize(); - tempV5.normalize(); - tempV6.normalize(); + + float unused_scale=0.0; + if (!tempV4.normalize(unused_scale) || + !tempV5.normalize(unused_scale) || + !tempV6.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } tempV1 += row1; m1.set(tempV4, tempV5, tempV6); @@ -345,9 +350,15 @@ void CStarCamera::setViewportAngle(const FPoint &angles) { mrow1 -= tempV3; mrow2 -= tempV3; mrow3 -= tempV3; - mrow1.normalize(); - mrow2.normalize(); - mrow3.normalize(); + + double unused_scale=0.0; + if (!mrow1.normalize(unused_scale) || + !mrow2.normalize(unused_scale) || + !mrow3.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } + tempV16 = tempV3; m3.set(mrow1, mrow2, mrow3); @@ -459,8 +470,12 @@ void CStarCamera::lockMarker1(FVector v1, FVector v2, FVector v3) { tempV._x = val9 - _viewport._valArray[2]; tempV._y = val8; - v3.normalize(); - tempV.normalize(); + float unused_scale=0.0; + if (!v3.normalize(unused_scale) || + !tempV.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } FMatrix matrix = _viewport.getOrientation(); const FVector &pos = _viewport._position; @@ -548,9 +563,15 @@ void CStarCamera::lockMarker2(CViewport *viewport, const FVector &v) { m4._col2 -= m4._col1; m4._col4 -= m4._col1; - m4._col3.normalize(); - m4._col2.normalize(); - m4._col4.normalize(); + double unused_scale=0.0; + if (!m4._col2.normalize(unused_scale) || + !m4._col3.normalize(unused_scale) || + !m4._col4.normalize(unused_scale) ) { // Do the normalizations, put the scale amount in unused_scale, + // but if any of the normalizations are unsuccessful, + // crash + assert(unused_scale); + } + m5.set(m4._col3, m4._col2, m4._col4); FVector newPos = m4._col1; diff --git a/engines/titanic/star_control/unmarked_auto_mover.cpp b/engines/titanic/star_control/unmarked_auto_mover.cpp index 5e74692ed5..71063fa615 100644 --- a/engines/titanic/star_control/unmarked_auto_mover.cpp +++ b/engines/titanic/star_control/unmarked_auto_mover.cpp @@ -98,7 +98,12 @@ int CUnmarkedAutoMover::proc5(CErrorCode &errorCode, FVector &pos, FMatrix &orie v2 = orientation._row3; v3 = _destPos - pos; - v3.normalize(); + + float unused_scale=0.0; + if (!v3.normalize(unused_scale)) { // Do the normalization, put the scale amount in unused_scale, + // but if it is unsuccessful, crash + assert(unused_scale); + } double val = orientation._row3._x * v3._x + orientation._row3._y * v3._y + orientation._row3._z * v3._z; bool flag = false; -- cgit v1.2.3