Some nice dist^2 vector length checks to save from squarerooting.

This commit is contained in:
Shyotl
2011-05-28 01:49:32 -05:00
parent 71eec1caed
commit 799fe056a8
4 changed files with 16 additions and 17 deletions

View File

@@ -67,7 +67,7 @@ const S32 PKT_SIZE = 57;
// throttle // throttle
const F32 MAX_SENDS_PER_SEC = 4.f; 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; 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); BOOL lookAtChanged = (target_type != mTargetType) || (object != mTargetObject);
// lookat position has moved a certain amount and we haven't just sent an update // 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))); ((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC)));
if (lookAtChanged) if (lookAtChanged)

View File

@@ -53,7 +53,7 @@ const S32 PKT_SIZE = 57;
// throttle // throttle
const F32 MAX_SENDS_PER_SEC = 4.f; 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 // timeouts
// can't use actual F32_MAX, because we add this to the current frametime // 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) || BOOL targetTypeChanged = (target_type != mTargetType) ||
(object != mTargetObject); (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)); ((current_time - mLastSendTime) > (1.f / MAX_SENDS_PER_SEC));
if (targetTypeChanged || targetPosChanged) if (targetTypeChanged || targetPosChanged)

View File

@@ -222,8 +222,6 @@ void LLManipScale::render()
LLVector3 center_agent = gAgent.getPosAgentFromGlobal(LLSelectMgr::getInstance()->getSelectionCenterGlobal()); LLVector3 center_agent = gAgent.getPosAgentFromGlobal(LLSelectMgr::getInstance()->getSelectionCenterGlobal());
F32 range;
F32 range_from_agent;
if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD) if (mObjectSelection->getSelectType() == SELECT_TYPE_HUD)
{ {
mBoxHandleSize = BOX_HANDLE_BASE_SIZE * BOX_HANDLE_BASE_FACTOR / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels(); mBoxHandleSize = BOX_HANDLE_BASE_SIZE * BOX_HANDLE_BASE_FACTOR / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels();
@@ -231,25 +229,25 @@ void LLManipScale::render()
} }
else else
{ {
range = dist_vec(gAgent.getCameraPositionAgent(), center_agent); F32 range_squared = dist_vec_squared(gAgent.getCameraPositionAgent(), center_agent);
range_from_agent = dist_vec(gAgent.getPositionAgent(), center_agent); F32 range_from_agent_squared = dist_vec_squared(gAgent.getPositionAgent(), center_agent);
// Don't draw manip if object too far away // Don't draw manip if object too far away
if (gSavedSettings.getBOOL("LimitSelectDistance")) if (gSavedSettings.getBOOL("LimitSelectDistance"))
{ {
F32 max_select_distance = gSavedSettings.getF32("MaxSelectDistance"); 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; return;
} }
} }
if (range > 0.001f) if (range_squared > 0.001f * 0.001f)
{ {
// range != zero // range != zero
F32 fraction_of_fov = BOX_HANDLE_BASE_SIZE / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels(); F32 fraction_of_fov = BOX_HANDLE_BASE_SIZE / (F32) LLViewerCamera::getInstance()->getViewHeightInPixels();
F32 apparent_angle = fraction_of_fov * LLViewerCamera::getInstance()->getView(); // radians 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 else
{ {

View File

@@ -6404,23 +6404,24 @@ bool LLSelectMgr::selectionMove(const LLVector3& displ,
if (update_position) if (update_position)
{ {
// calculate the distance of the object closest to the camera origin // 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; LLVector3 obj_pos;
for (LLObjectSelection::root_iterator it = getSelection()->root_begin(); for (LLObjectSelection::root_iterator it = getSelection()->root_begin();
it != getSelection()->root_end(); ++it) it != getSelection()->root_end(); ++it)
{ {
obj_pos = (*it)->getObject()->getPositionEdit(); obj_pos = (*it)->getObject()->getPositionEdit();
F32 obj_dist = dist_vec(obj_pos, LLViewerCamera::getInstance()->getOrigin()); F32 obj_dist_squared = dist_vec_squared(obj_pos, LLViewerCamera::getInstance()->getOrigin());
if (obj_dist < min_dist) 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. // 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_global.setVec(displ.mV[0]*min_dist,
displ.mV[1]*min_dist, displ.mV[1]*min_dist,
displ.mV[2]*min_dist); displ.mV[2]*min_dist);