STORM-1920: Math cleanup from Richard Linden.

master
Ricky Curtice 2014-02-18 23:03:13 -08:00
parent d8a81b240e
commit a060dcfff7
3 changed files with 100 additions and 109 deletions

View File

@ -101,25 +101,25 @@ class LLVector3d
F64 operator[](int idx) const { return mdV[idx]; }
F64 &operator[](int idx) { return mdV[idx]; }
friend LLVector3d operator+(const LLVector3d &a, const LLVector3d &b); // Return vector a + b
friend LLVector3d operator-(const LLVector3d &a, const LLVector3d &b); // Return vector a minus b
friend F64 operator*(const LLVector3d &a, const LLVector3d &b); // Return a dot b
friend LLVector3d operator%(const LLVector3d &a, const LLVector3d &b); // Return a cross b
friend LLVector3d operator*(const LLVector3d &a, const F64 k); // Return a times scaler k
friend LLVector3d operator/(const LLVector3d &a, const F64 k); // Return a divided by scaler k
friend LLVector3d operator*(const F64 k, const LLVector3d &a); // Return a times scaler k
friend bool operator==(const LLVector3d &a, const LLVector3d &b); // Return a == b
friend bool operator!=(const LLVector3d &a, const LLVector3d &b); // Return a != b
friend LLVector3d operator+(const LLVector3d& a, const LLVector3d& b); // Return vector a + b
friend LLVector3d operator-(const LLVector3d& a, const LLVector3d& b); // Return vector a minus b
friend F64 operator*(const LLVector3d& a, const LLVector3d& b); // Return a dot b
friend LLVector3d operator%(const LLVector3d& a, const LLVector3d& b); // Return a cross b
friend LLVector3d operator*(const LLVector3d& a, const F64 k); // Return a times scaler k
friend LLVector3d operator/(const LLVector3d& a, const F64 k); // Return a divided by scaler k
friend LLVector3d operator*(const F64 k, const LLVector3d& a); // Return a times scaler k
friend bool operator==(const LLVector3d& a, const LLVector3d& b); // Return a == b
friend bool operator!=(const LLVector3d& a, const LLVector3d& b); // Return a != b
friend const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b); // Return vector a + b
friend const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b); // Return vector a minus b
friend const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b); // Return a cross b
friend const LLVector3d& operator*=(LLVector3d &a, const F64 k); // Return a times scaler k
friend const LLVector3d& operator/=(LLVector3d &a, const F64 k); // Return a divided by scaler k
friend const LLVector3d& operator+=(LLVector3d& a, const LLVector3d& b); // Return vector a + b
friend const LLVector3d& operator-=(LLVector3d& a, const LLVector3d& b); // Return vector a minus b
friend const LLVector3d& operator%=(LLVector3d& a, const LLVector3d& b); // Return a cross b
friend const LLVector3d& operator*=(LLVector3d& a, const F64 k); // Return a times scaler k
friend const LLVector3d& operator/=(LLVector3d& a, const F64 k); // Return a divided by scaler k
friend LLVector3d operator-(const LLVector3d &a); // Return vector -a
friend LLVector3d operator-(const LLVector3d& a); // Return vector -a
friend std::ostream& operator<<(std::ostream& s, const LLVector3d &a); // Stream a
friend std::ostream& operator<<(std::ostream& s, const LLVector3d& a); // Stream a
static BOOL parseVector3d(const std::string& buf, LLVector3d* value);
@ -298,59 +298,59 @@ inline F64 LLVector3d::lengthSquared(void) const
return mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2];
}
inline LLVector3d operator+(const LLVector3d &a, const LLVector3d &b)
inline LLVector3d operator+(const LLVector3d& a, const LLVector3d& b)
{
LLVector3d c(a);
return c += b;
}
inline LLVector3d operator-(const LLVector3d &a, const LLVector3d &b)
inline LLVector3d operator-(const LLVector3d& a, const LLVector3d& b)
{
LLVector3d c(a);
return c -= b;
}
inline F64 operator*(const LLVector3d &a, const LLVector3d &b)
inline F64 operator*(const LLVector3d& a, const LLVector3d& b)
{
return (a.mdV[0]*b.mdV[0] + a.mdV[1]*b.mdV[1] + a.mdV[2]*b.mdV[2]);
}
inline LLVector3d operator%(const LLVector3d &a, const LLVector3d &b)
inline LLVector3d operator%(const LLVector3d& a, const LLVector3d& b)
{
return LLVector3d( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1] );
}
inline LLVector3d operator/(const LLVector3d &a, const F64 k)
inline LLVector3d operator/(const LLVector3d& a, const F64 k)
{
F64 t = 1.f / k;
return LLVector3d( a.mdV[0] * t, a.mdV[1] * t, a.mdV[2] * t );
}
inline LLVector3d operator*(const LLVector3d &a, const F64 k)
inline LLVector3d operator*(const LLVector3d& a, const F64 k)
{
return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
}
inline LLVector3d operator*(F64 k, const LLVector3d &a)
inline LLVector3d operator*(F64 k, const LLVector3d& a)
{
return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
}
inline bool operator==(const LLVector3d &a, const LLVector3d &b)
inline bool operator==(const LLVector3d& a, const LLVector3d& b)
{
return ( (a.mdV[0] == b.mdV[0])
&&(a.mdV[1] == b.mdV[1])
&&(a.mdV[2] == b.mdV[2]));
}
inline bool operator!=(const LLVector3d &a, const LLVector3d &b)
inline bool operator!=(const LLVector3d& a, const LLVector3d& b)
{
return ( (a.mdV[0] != b.mdV[0])
||(a.mdV[1] != b.mdV[1])
||(a.mdV[2] != b.mdV[2]));
}
inline const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b)
inline const LLVector3d& operator+=(LLVector3d& a, const LLVector3d& b)
{
a.mdV[0] += b.mdV[0];
a.mdV[1] += b.mdV[1];
@ -358,7 +358,7 @@ inline const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b)
return a;
}
inline const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b)
inline const LLVector3d& operator-=(LLVector3d& a, const LLVector3d& b)
{
a.mdV[0] -= b.mdV[0];
a.mdV[1] -= b.mdV[1];
@ -366,14 +366,14 @@ inline const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b)
return a;
}
inline const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b)
inline const LLVector3d& operator%=(LLVector3d& a, const LLVector3d& b)
{
LLVector3d ret( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1]);
a = ret;
return a;
}
inline const LLVector3d& operator*=(LLVector3d &a, const F64 k)
inline const LLVector3d& operator*=(LLVector3d& a, const F64 k)
{
a.mdV[0] *= k;
a.mdV[1] *= k;
@ -381,7 +381,7 @@ inline const LLVector3d& operator*=(LLVector3d &a, const F64 k)
return a;
}
inline const LLVector3d& operator/=(LLVector3d &a, const F64 k)
inline const LLVector3d& operator/=(LLVector3d& a, const F64 k)
{
F64 t = 1.f / k;
a.mdV[0] *= t;
@ -390,12 +390,12 @@ inline const LLVector3d& operator/=(LLVector3d &a, const F64 k)
return a;
}
inline LLVector3d operator-(const LLVector3d &a)
inline LLVector3d operator-(const LLVector3d& a)
{
return LLVector3d( -a.mdV[0], -a.mdV[1], -a.mdV[2] );
}
inline F64 dist_vec(const LLVector3d &a, const LLVector3d &b)
inline F64 dist_vec(const LLVector3d& a, const LLVector3d& b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
@ -403,7 +403,7 @@ inline F64 dist_vec(const LLVector3d &a, const LLVector3d &b)
return (F32) sqrt( x*x + y*y + z*z );
}
inline F64 dist_vec_squared(const LLVector3d &a, const LLVector3d &b)
inline F64 dist_vec_squared(const LLVector3d& a, const LLVector3d& b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
@ -411,14 +411,14 @@ inline F64 dist_vec_squared(const LLVector3d &a, const LLVector3d &b)
return x*x + y*y + z*z;
}
inline F64 dist_vec_squared2D(const LLVector3d &a, const LLVector3d &b)
inline F64 dist_vec_squared2D(const LLVector3d& a, const LLVector3d& b)
{
F64 x = a.mdV[0] - b.mdV[0];
F64 y = a.mdV[1] - b.mdV[1];
return x*x + y*y;
}
inline LLVector3d lerp(const LLVector3d &a, const LLVector3d &b, const F64 u)
inline LLVector3d lerp(const LLVector3d& a, const LLVector3d& b, const F64 u)
{
return LLVector3d(
a.mdV[VX] + (b.mdV[VX] - a.mdV[VX]) * u,
@ -450,7 +450,7 @@ inline F64 angle_between(const LLVector3d& a, const LLVector3d& b)
return angle;
}
inline BOOL are_parallel(const LLVector3d &a, const LLVector3d &b, const F64 epsilon)
inline BOOL are_parallel(const LLVector3d& a, const LLVector3d& b, const F64 epsilon)
{
LLVector3d an = a;
LLVector3d bn = b;
@ -465,11 +465,22 @@ inline BOOL are_parallel(const LLVector3d &a, const LLVector3d &b, const F64 eps
}
inline LLVector3d projected_vec(const LLVector3d &a, const LLVector3d &b)
inline LLVector3d projected_vec(const LLVector3d& a, const LLVector3d& b)
{
LLVector3d project_axis = b;
project_axis.normalize();
return project_axis * (a * project_axis);
}
inline LLVector3d inverse_projected_vec(const LLVector3d& a, const LLVector3d& b)
{
LLVector3d normalized_a = a;
normalized_a.normalize();
LLVector3d normalized_b = b;
F64 b_length = normalized_b.normalize();
F64 dot_product = normalized_a * normalized_b;
return normalized_a * (b_length / dot_product);
}
#endif // LL_V3DMATH_H

View File

@ -159,6 +159,7 @@ F32 dist_vec(const LLVector3 &a, const LLVector3 &b); // Returns distance betwe
F32 dist_vec_squared(const LLVector3 &a, const LLVector3 &b);// Returns distance squared between a and b
F32 dist_vec_squared2D(const LLVector3 &a, const LLVector3 &b);// Returns distance squared between a and b ignoring Z component
LLVector3 projected_vec(const LLVector3 &a, const LLVector3 &b); // Returns vector a projected on vector b
LLVector3 inverse_projected_vec(const LLVector3 &a, const LLVector3 &b); // Returns vector a scaled such that projected_vec(inverse_projected_vec(a, b), b) == b;
LLVector3 parallel_component(const LLVector3 &a, const LLVector3 &b); // Returns vector a projected on vector b (same as projected_vec)
LLVector3 orthogonal_component(const LLVector3 &a, const LLVector3 &b); // Returns component of vector a not parallel to vector b (same as projected_vec)
LLVector3 lerp(const LLVector3 &a, const LLVector3 &b, F32 u); // Returns a vector that is a linear interpolation between a and b
@ -495,6 +496,18 @@ inline LLVector3 projected_vec(const LLVector3 &a, const LLVector3 &b)
return project_axis * (a * project_axis);
}
inline LLVector3 inverse_projected_vec(const LLVector3& a, const LLVector3& b)
{
LLVector3 normalized_a = a;
normalized_a.normalize();
LLVector3 normalized_b = b;
F32 b_length = normalized_b.normalize();
F32 dot_product = normalized_a * normalized_b;
//NB: if a _|_ b, then returns an infinite vector
return normalized_a * (b_length / dot_product);
}
inline LLVector3 parallel_component(const LLVector3 &a, const LLVector3 &b)
{
return projected_vec(a, b);

View File

@ -843,121 +843,85 @@ void LLManipScale::drag( S32 x, S32 y )
// Scale around the
void LLManipScale::dragCorner( S32 x, S32 y )
{
LLBBox bbox = LLSelectMgr::getInstance()->getBBoxOfSelection();
// Suppress scale if mouse hasn't moved.
if (x == mLastMouseX && y == mLastMouseY)
{
// sendUpdates(TRUE,TRUE,TRUE);
return;
}
mLastMouseX = x;
mLastMouseY = y;
LLVector3d drag_start_point_global = mDragStartPointGlobal;
LLVector3d drag_start_center_global = mDragStartCenterGlobal;
LLVector3 drag_start_point_agent = gAgent.getPosAgentFromGlobal(drag_start_point_global);
LLVector3 drag_start_center_agent = gAgent.getPosAgentFromGlobal(drag_start_center_global);
LLVector3 drag_start_point_agent = gAgent.getPosAgentFromGlobal(mDragStartPointGlobal);
LLVector3 drag_start_center_agent = gAgent.getPosAgentFromGlobal(mDragStartCenterGlobal);
LLVector3d drag_start_dir_d;
drag_start_dir_d.setVec(drag_start_point_global - drag_start_center_global);
LLVector3 drag_start_dir_f;
drag_start_dir_f.setVec(drag_start_dir_d);
drag_start_dir_d.setVec(mDragStartPointGlobal - mDragStartCenterGlobal);
F32 s = 0;
F32 t = 0;
nearestPointOnLineFromMouse(x, y,
drag_start_center_agent,
drag_start_point_agent,
s, t );
F32 drag_start_dist = dist_vec(drag_start_point_agent, drag_start_center_agent);
drag_start_center_agent,
drag_start_point_agent,
s, t );
if( s <= 0 ) // we only care about intersections in front of the camera
{
return;
}
mDragPointGlobal = lerp(mDragStartCenterGlobal, mDragStartPointGlobal, t);
LLVector3d drag_point_global = drag_start_center_global + t * drag_start_dir_d;
F32 scale_factor = t;
BOOL uniform = LLManipScale::getUniform();
if( !uniform )
{
scale_factor = 0.5f + (scale_factor * 0.5f);
}
LLBBox bbox = LLSelectMgr::getInstance()->getBBoxOfSelection();
F32 scale_factor = 1.f;
F32 max_scale = partToMaxScale(mManipPart, bbox);
F32 min_scale = partToMinScale(mManipPart, bbox);
BOOL uniform = LLManipScale::getUniform();
// check for snapping
LLVector3 drag_center_agent = gAgent.getPosAgentFromGlobal(drag_point_global);
LLVector3 mouse_on_plane1;
getMousePointOnPlaneAgent(mouse_on_plane1, x, y, drag_center_agent, mScalePlaneNormal1);
getMousePointOnPlaneAgent(mouse_on_plane1, x, y, mScaleCenter, mScalePlaneNormal1);
mouse_on_plane1 -= mScaleCenter;
LLVector3 mouse_on_plane2;
getMousePointOnPlaneAgent(mouse_on_plane2, x, y, drag_center_agent, mScalePlaneNormal2);
LLVector3 mouse_dir_1 = mouse_on_plane1 - mScaleCenter;
LLVector3 mouse_dir_2 = mouse_on_plane2 - mScaleCenter;
LLVector3 mouse_to_scale_line_1 = mouse_dir_1 - projected_vec(mouse_dir_1, mScaleDir);
LLVector3 mouse_to_scale_line_2 = mouse_dir_2 - projected_vec(mouse_dir_2, mScaleDir);
LLVector3 mouse_to_scale_line_dir_1 = mouse_to_scale_line_1;
mouse_to_scale_line_dir_1.normVec();
if (mouse_to_scale_line_dir_1 * mSnapGuideDir1 < 0.f)
{
// need to keep sign of mouse offset wrt to snap guide direction
mouse_to_scale_line_dir_1 *= -1.f;
}
LLVector3 mouse_to_scale_line_dir_2 = mouse_to_scale_line_2;
mouse_to_scale_line_dir_2.normVec();
if (mouse_to_scale_line_dir_2 * mSnapGuideDir2 < 0.f)
{
// need to keep sign of mouse offset wrt to snap guide direction
mouse_to_scale_line_dir_2 *= -1.f;
}
getMousePointOnPlaneAgent(mouse_on_plane2, x, y, mScaleCenter, mScalePlaneNormal2);
mouse_on_plane2 -= mScaleCenter;
F32 snap_dir_dot_mouse_offset1 = mSnapGuideDir1 * mouse_to_scale_line_dir_1;
F32 snap_dir_dot_mouse_offset2 = mSnapGuideDir2 * mouse_to_scale_line_dir_2;
LLVector3 projected_drag_pos1 = inverse_projected_vec(mScaleDir, orthogonal_component(mouse_on_plane1, mSnapGuideDir1));
LLVector3 projected_drag_pos2 = inverse_projected_vec(mScaleDir, orthogonal_component(mouse_on_plane2, mSnapGuideDir2));
F32 dist_from_scale_line_1 = mouse_to_scale_line_1 * mouse_to_scale_line_dir_1;
F32 dist_from_scale_line_2 = mouse_to_scale_line_2 * mouse_to_scale_line_dir_2;
F32 max_scale = partToMaxScale(mManipPart, bbox);
F32 min_scale = partToMinScale(mManipPart, bbox);
LLVector3 mouse_offset_from_scale_line_1 = orthogonal_component(mouse_on_plane1, mScaleDir);
LLVector3 mouse_offset_from_scale_line_2 = orthogonal_component(mouse_on_plane2, mScaleDir);
BOOL snap_enabled = gSavedSettings.getBOOL("SnapEnabled");
if (snap_enabled && dist_from_scale_line_1 > mSnapRegimeOffset * snap_dir_dot_mouse_offset1)
if (snap_enabled && (mouse_on_plane1 - projected_drag_pos1) * mSnapGuideDir1 > mSnapRegimeOffset)
{
mInSnapRegime = TRUE;
LLVector3 projected_drag_pos = mouse_on_plane1 - (dist_from_scale_line_1 / snap_dir_dot_mouse_offset1) * mSnapGuideDir1;
F32 drag_dist = (projected_drag_pos - mScaleCenter) * mScaleDir;
F32 drag_dist = projected_drag_pos1.length();
F32 cur_subdivisions = llclamp(getSubdivisionLevel(projected_drag_pos, mScaleDir, mScaleSnapUnit1), sGridMinSubdivisionLevel, sGridMaxSubdivisionLevel);
F32 cur_subdivisions = llclamp(getSubdivisionLevel(projected_drag_pos1, mScaleDir, mScaleSnapUnit1), sGridMinSubdivisionLevel, sGridMaxSubdivisionLevel);
F32 snap_dist = mScaleSnapUnit1 / (2.f * cur_subdivisions);
F32 relative_snap_dist = fmodf(drag_dist + snap_dist, mScaleSnapUnit1 / cur_subdivisions);
mScaleSnapValue = llclamp((drag_dist - (relative_snap_dist - snap_dist)), min_scale, max_scale);
scale_factor = mScaleSnapValue / drag_start_dist;
if( !uniform )
mInSnapRegime = TRUE;
scale_factor = mScaleSnapValue / dist_vec(drag_start_point_agent, drag_start_center_agent);
if (!uniform)
{
scale_factor *= 0.5f;
}
}
else if (snap_enabled && dist_from_scale_line_2 > mSnapRegimeOffset * snap_dir_dot_mouse_offset2)
else if (snap_enabled && (mouse_on_plane2 - projected_drag_pos2) * mSnapGuideDir2 > mSnapRegimeOffset )
{
mInSnapRegime = TRUE;
LLVector3 projected_drag_pos = mouse_on_plane2 - (dist_from_scale_line_2 / snap_dir_dot_mouse_offset2) * mSnapGuideDir2;
F32 drag_dist = (projected_drag_pos - mScaleCenter) * mScaleDir;
F32 drag_dist = projected_drag_pos2.length();
F32 cur_subdivisions = llclamp(getSubdivisionLevel(projected_drag_pos, mScaleDir, mScaleSnapUnit2), sGridMinSubdivisionLevel, sGridMaxSubdivisionLevel);
F32 cur_subdivisions = llclamp(getSubdivisionLevel(projected_drag_pos2, mScaleDir, mScaleSnapUnit2), sGridMinSubdivisionLevel, sGridMaxSubdivisionLevel);
F32 snap_dist = mScaleSnapUnit2 / (2.f * cur_subdivisions);
F32 relative_snap_dist = fmodf(drag_dist + snap_dist, mScaleSnapUnit2 / cur_subdivisions);
mScaleSnapValue = llclamp((drag_dist - (relative_snap_dist - snap_dist)), min_scale, max_scale);
scale_factor = mScaleSnapValue / drag_start_dist;
if( !uniform )
mInSnapRegime = TRUE;
scale_factor = mScaleSnapValue / dist_vec(drag_start_point_agent, drag_start_center_agent);
if (!uniform)
{
scale_factor *= 0.5f;
}
@ -965,8 +929,14 @@ void LLManipScale::dragCorner( S32 x, S32 y )
else
{
mInSnapRegime = FALSE;
scale_factor = t;
if (!uniform)
{
scale_factor = 0.5f + (scale_factor * 0.5f);
}
}
F32 max_scale_factor = get_default_max_prim_scale() / MIN_PRIM_SCALE;
F32 min_scale_factor = MIN_PRIM_SCALE / get_default_max_prim_scale();
@ -1069,9 +1039,6 @@ void LLManipScale::dragCorner( S32 x, S32 y )
}
}
mDragPointGlobal = drag_point_global;
}