From 799fe056a8291a0430590545632558adb0815acd Mon Sep 17 00:00:00 2001 From: Shyotl Date: Sat, 28 May 2011 01:49:32 -0500 Subject: [PATCH] Some nice dist^2 vector length checks to save from squarerooting. --- indra/newview/llhudeffectlookat.cpp | 4 ++-- indra/newview/llhudeffectpointat.cpp | 4 ++-- indra/newview/llmanipscale.cpp | 12 +++++------- indra/newview/llselectmgr.cpp | 13 +++++++------ 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/indra/newview/llhudeffectlookat.cpp b/indra/newview/llhudeffectlookat.cpp index 98814e882..bde58540f 100644 --- a/indra/newview/llhudeffectlookat.cpp +++ b/indra/newview/llhudeffectlookat.cpp @@ -67,7 +67,7 @@ const S32 PKT_SIZE = 57; // throttle const F32 MAX_SENDS_PER_SEC = 4.f; -const F32 MIN_DELTAPOS_FOR_UPDATE = 0.05f; +const F32 MIN_DELTAPOS_FOR_UPDATE_SQUARED = 0.05f * 0.05f; const F32 MIN_TARGET_OFFSET_SQUARED = 0.0001f; @@ -427,7 +427,7 @@ BOOL LLHUDEffectLookAt::setLookAt(ELookAtType target_type, LLViewerObject *objec BOOL lookAtChanged = (target_type != mTargetType) || (object != mTargetObject); // lookat position has moved a certain amount and we haven't just sent an update - lookAtChanged = lookAtChanged || ((dist_vec(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE) && + lookAtChanged = lookAtChanged || ((dist_vec_squared(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE_SQUARED) && ((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC))); if (lookAtChanged) diff --git a/indra/newview/llhudeffectpointat.cpp b/indra/newview/llhudeffectpointat.cpp index 10409a95b..c566bd768 100644 --- a/indra/newview/llhudeffectpointat.cpp +++ b/indra/newview/llhudeffectpointat.cpp @@ -53,7 +53,7 @@ const S32 PKT_SIZE = 57; // throttle const F32 MAX_SENDS_PER_SEC = 4.f; -const F32 MIN_DELTAPOS_FOR_UPDATE = 0.05f; +const F32 MIN_DELTAPOS_FOR_UPDATE_SQUARED = 0.05f * 0.05f; // timeouts // can't use actual F32_MAX, because we add this to the current frametime @@ -251,7 +251,7 @@ BOOL LLHUDEffectPointAt::setPointAt(EPointAtType target_type, LLViewerObject *ob BOOL targetTypeChanged = (target_type != mTargetType) || (object != mTargetObject); - BOOL targetPosChanged = (dist_vec(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE) && + BOOL targetPosChanged = (dist_vec_squared(position, mLastSentOffsetGlobal) > MIN_DELTAPOS_FOR_UPDATE_SQUARED) && ((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC)); if (targetTypeChanged || targetPosChanged) diff --git a/indra/newview/llmanipscale.cpp b/indra/newview/llmanipscale.cpp index eb467f921..5da5ba0aa 100644 --- a/indra/newview/llmanipscale.cpp +++ b/indra/newview/llmanipscale.cpp @@ -222,8 +222,6 @@ void LLManipScale::render() LLVector3 center_agent = gAgent.getPosAgentFromGlobal(LLSelectMgr::getInstance()->getSelectionCenterGlobal()); - F32 range; - F32 range_from_agent; if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD) { mBoxHandleSize = BOX_HANDLE_BASE_SIZE * BOX_HANDLE_BASE_FACTOR / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels(); @@ -231,25 +229,25 @@ void LLManipScale::render() } else { - range = dist_vec(gAgent.getCameraPositionAgent(), center_agent); - range_from_agent = dist_vec(gAgent.getPositionAgent(), center_agent); + F32 range_squared = dist_vec_squared(gAgent.getCameraPositionAgent(), center_agent); + F32 range_from_agent_squared = dist_vec_squared(gAgent.getPositionAgent(), center_agent); // Don't draw manip if object too far away if (gSavedSettings.getBOOL("LimitSelectDistance")) { F32 max_select_distance = gSavedSettings.getF32("MaxSelectDistance"); - if (range_from_agent > max_select_distance) + if (range_from_agent_squared > max_select_distance * max_select_distance) { return; } } - if (range > 0.001f) + if (range_squared > 0.001f * 0.001f) { // range != zero F32 fraction_of_fov = BOX_HANDLE_BASE_SIZE / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels(); F32 apparent_angle = fraction_of_fov * LLViewerCamera::getInstance()->getView(); // radians - mBoxHandleSize = range * tan(apparent_angle) * BOX_HANDLE_BASE_FACTOR; + mBoxHandleSize = (F32) sqrtf(range_squared) * tan(apparent_angle) * BOX_HANDLE_BASE_FACTOR; } else { diff --git a/indra/newview/llselectmgr.cpp b/indra/newview/llselectmgr.cpp index 0ca376345..f6fd26f41 100644 --- a/indra/newview/llselectmgr.cpp +++ b/indra/newview/llselectmgr.cpp @@ -6404,23 +6404,24 @@ bool LLSelectMgr::selectionMove(const LLVector3& displ, if (update_position) { // calculate the distance of the object closest to the camera origin - F32 min_dist = 1e+30f; + F32 min_dist_squared = F32_MAX; // value will be overridden in the loop + LLVector3 obj_pos; for (LLObjectSelection::root_iterator it = getSelection()->root_begin(); it != getSelection()->root_end(); ++it) { obj_pos = (*it)->getObject()->getPositionEdit(); - F32 obj_dist = dist_vec(obj_pos, LLViewerCamera::getInstance()->getOrigin()); - if (obj_dist < min_dist) + F32 obj_dist_squared = dist_vec_squared(obj_pos, LLViewerCamera::getInstance()->getOrigin()); + if (obj_dist_squared < min_dist_squared) { - min_dist = obj_dist; + min_dist_squared = obj_dist_squared; } } - // factor the distance inside the displacement vector. This will get us + // factor the distance into the displacement vector. This will get us // equally visible movements for both close and far away selections. - min_dist = sqrt(min_dist) / 2; + F32 min_dist = sqrt((F32) sqrtf(min_dist_squared)) / 2; displ_global.setVec(displ.mV[0]*min_dist, displ.mV[1]*min_dist, displ.mV[2]*min_dist);