Merge pull request #6523 from dabingnn/v3_refactor_normalize

V3 refactor normalize
This commit is contained in:
Ricardo Quesada 2014-04-30 12:58:04 -07:00
commit 528cdf461c
17 changed files with 92 additions and 137 deletions

View File

@ -379,7 +379,7 @@ void Lens3D::update(float time)
if (vect.getLength() > 0)
{
vect = vect.normalize();
vect.normalize();
Vector2 new_vect = vect * new_r;
v.z += (_concave ? -1.0f : 1.0f) * new_vect.getLength() * _lensEffect;
}

View File

@ -81,7 +81,7 @@ static inline Vector2 v2fforangle(float _a_)
static inline Vector2 v2fnormalize(const Vector2 &p)
{
Vector2 r = Vector2(p.x, p.y).normalize();
Vector2 r = Vector2(p.x, p.y).getNormalized();
return v2f(r.x, r.y);
}

View File

@ -710,7 +710,7 @@ void ParticleSystem::update(float dt)
// radial acceleration
if (p->pos.x || p->pos.y)
{
radial = p->pos.normalize();
radial = p->pos.getNormalized();
}
tangential = radial;
radial = radial * p->modeA.radialAccel;

View File

@ -46,26 +46,26 @@ void ccVertexLineToPolygon(Vector2 *points, float stroke, Vector2 *vertices, uns
Vector2 perpVector;
if(i == 0)
perpVector = (p1 - points[i+1]).normalize().getPerp();
perpVector = (p1 - points[i+1]).getNormalized().getPerp();
else if(i == nuPointsMinus)
perpVector = (points[i-1] - p1).normalize().getPerp();
perpVector = (points[i-1] - p1).getNormalized().getPerp();
else
{
Vector2 p2 = points[i+1];
Vector2 p0 = points[i-1];
Vector2 p2p1 = (p2 - p1).normalize();
Vector2 p0p1 = (p0 - p1).normalize();
Vector2 p2p1 = (p2 - p1).getNormalized();
Vector2 p0p1 = (p0 - p1).getNormalized();
// Calculate angle between vectors
float angle = acosf(p2p1.dot(p0p1));
if(angle < CC_DEGREES_TO_RADIANS(70))
perpVector = p2p1.getMidpoint(p0p1).normalize().getPerp();
perpVector = p2p1.getMidpoint(p0p1).getNormalized().getPerp();
else if(angle < CC_DEGREES_TO_RADIANS(170))
perpVector = p2p1.getMidpoint(p0p1).normalize();
perpVector = p2p1.getMidpoint(p0p1).getNormalized();
else
perpVector = (p2 - p0).normalize().getPerp();
perpVector = (p2 - p0).getNormalized().getPerp();
}
perpVector = perpVector * stroke;

View File

@ -314,7 +314,7 @@ Matrix* kmMat4RotationZ(Matrix* pOut, const float radians)
return pOut;
}
Matrix* kmMat4RotationAxisAngle(Matrix* pOut, const struct Vector3* axis, float radians)
Matrix* kmMat4RotationAxisAngle(Matrix* pOut, const Vector3* axis, float radians)
{
Matrix::createRotation(*axis, radians, pOut);
return pOut;
@ -338,7 +338,7 @@ Matrix* kmMat4OrthographicProjection(Matrix* pOut, float left, float right, floa
return pOut;
}
Matrix* kmMat4LookAt(Matrix* pOut, const struct Vector3* pEye, const struct Vector3* pCenter, const struct Vector3* pUp)
Matrix* kmMat4LookAt(Matrix* pOut, const Vector3* pEye, const Vector3* pCenter, const Vector3* pUp)
{
Matrix::createLookAt(*pEye, *pCenter, *pUp, pOut);
return pOut;
@ -372,7 +372,7 @@ CC_DLL Vector3* kmVec3Lerp(Vector3* pOut, const Vector3* pV1, const Vector3* pV2
Vector3* kmVec3Normalize(Vector3* pOut, const Vector3* pIn)
{
pIn->normalize(pOut);
*pOut = pIn->getNormalized();
return pOut;
}
@ -456,7 +456,7 @@ float kmVec2LengthSq(const Vector2* pIn)
Vector2* kmVec2Normalize(Vector2* pOut, const Vector2* pIn)
{
pIn->normalize(pOut);
*pOut = pIn->getNormalized();
return pOut;
}
@ -534,7 +534,7 @@ Vector4* kmVec4Lerp(Vector4* pOut, const Vector4* pV1, const Vector4* pV2, float
Vector4* kmVec4Normalize(Vector4* pOut, const Vector4* pIn)
{
pIn->normalize(pOut);
*pOut = pIn->getNormalized();
return pOut;
}

View File

@ -220,7 +220,7 @@ CC_DEPRECATED_ATTRIBUTE static inline float ccpDistance(const Vector2& v1, const
*/
CC_DEPRECATED_ATTRIBUTE static inline Vector2 ccpNormalize(const Vector2& v)
{
return v.normalize();
return v.getNormalized();
}
/** Converts radians to a normalized vector.
@ -1082,12 +1082,12 @@ CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4Translation(Matrix* pOut, const flo
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4RotationX(Matrix* pOut, const float radians);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4RotationY(Matrix* pOut, const float radians);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4RotationZ(Matrix* pOut, const float radians);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4RotationAxisAngle(Matrix* pOut, const struct Vector3* axis, float radians);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4RotationAxisAngle(Matrix* pOut, const Vector3* axis, float radians);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4Scaling(Matrix* pOut, const float x, const float y, const float z);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4PerspectiveProjection(Matrix* pOut, float fovY, float aspect, float zNear, float zFar);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4OrthographicProjection(Matrix* pOut, float left, float right, float bottom, float top, float nearVal, float farVal);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4LookAt(Matrix* pOut, const struct Vector3* pEye, const struct Vector3* pCenter, const struct Vector3* pUp);
CC_DEPRECATED_ATTRIBUTE CC_DLL Matrix* kmMat4LookAt(Matrix* pOut, const Vector3* pEye, const Vector3* pCenter, const Vector3* pUp);
//kmVec3
CC_DEPRECATED_ATTRIBUTE typedef Vector3 kmVec3;

View File

@ -149,37 +149,29 @@ void Quaternion::multiply(const Quaternion& q1, const Quaternion& q2, Quaternion
void Quaternion::normalize()
{
normalize(this);
}
void Quaternion::normalize(Quaternion* dst) const
{
GP_ASSERT(dst);
if (this != dst)
{
dst->x = x;
dst->y = y;
dst->z = z;
dst->w = w;
}
float n = x * x + y * y + z * z + w * w;
// Already normalized.
if (n == 1.0f)
return;
n = sqrt(n);
// Too close to zero.
if (n < 0.000001f)
return;
n = 1.0f / n;
dst->x *= n;
dst->y *= n;
dst->z *= n;
dst->w *= n;
x *= n;
y *= n;
z *= n;
w *= n;
}
Quaternion Quaternion::getNormalized() const
{
Quaternion q(*this);
q.normalize();
return q;
}
void Quaternion::set(float xx, float yy, float zz, float ww)

View File

@ -228,7 +228,7 @@ public:
*
* @param dst A quaternion to store the result in.
*/
void normalize(Quaternion* dst) const;
Quaternion getNormalized() const;
/**
* Sets the elements of the quaternion to the specified values.

View File

@ -208,35 +208,28 @@ void Vector2::negate()
y = -y;
}
Vector2& Vector2::normalize()
void Vector2::normalize()
{
normalize(this);
return *this;
}
void Vector2::normalize(Vector2* dst) const
{
GP_ASSERT(dst);
if (dst != this)
{
dst->x = x;
dst->y = y;
}
float n = x * x + y * y;
// Already normalized.
if (n == 1.0f)
return;
n = sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
return;
n = 1.0f / n;
dst->x *= n;
dst->y *= n;
x *= n;
y *= n;
}
Vector2 Vector2::getNormalized() const
{
Vector2 v(*this);
v.normalize();
return v;
}
void Vector2::scale(float scalar)
@ -342,8 +335,8 @@ bool Vector2::fuzzyEquals(const Vector2& b, float var) const
float Vector2::getAngle(const Vector2& other) const
{
Vector2 a2 = normalize();
Vector2 b2 = other.normalize();
Vector2 a2 = getNormalized();
Vector2 b2 = other.getNormalized();
float angle = atan2f(a2.cross(b2), a2.dot(b2));
if( fabs(angle) < FLT_EPSILON ) return 0.f;
return angle;

View File

@ -248,7 +248,7 @@ public:
*
* @return This vector, after the normalization occurs.
*/
Vector2& normalize();
void normalize();
/**
* Normalizes this vector and stores the result in dst.
@ -259,7 +259,7 @@ public:
*
* @param dst The destination vector.
*/
void normalize(Vector2* dst) const;
Vector2 getNormalized() const;
/**
* Scales all elements of this vector by the specified value.
@ -616,19 +616,6 @@ public:
return Vector2(x*other.x + y*other.y, y*other.x - x*other.y);
};
/** Returns point multiplied to a length of 1.
* If the point is 0, it returns (1, 0)
@return Vector2
@since v2.1.4
* @js NA
* @lua NA
*/
inline Vector2 normalize() const {
float length = getLength();
if(length == 0.) return Vector2(1.f, 0);
return *this / getLength();
};
/** Linear Interpolation between two points a and b
@returns
alpha == 0 ? a

View File

@ -223,37 +223,29 @@ void Vector3::negate()
z = -z;
}
Vector3& Vector3::normalize()
void Vector3::normalize()
{
normalize(this);
return *this;
}
void Vector3::normalize(Vector3* dst) const
{
GP_ASSERT(dst);
if (dst != this)
{
dst->x = x;
dst->y = y;
dst->z = z;
}
float n = x * x + y * y + z * z;
// Already normalized.
if (n == 1.0f)
return;
n = sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
return;
n = 1.0f / n;
dst->x *= n;
dst->y *= n;
dst->z *= n;
x *= n;
y *= n;
z *= n;
}
Vector3 Vector3::getNormalized() const
{
Vector3 v(*this);
v.normalize();
return v;
}
void Vector3::scale(float scalar)

View File

@ -282,7 +282,7 @@ public:
*
* @return This vector, after the normalization occurs.
*/
Vector3& normalize();
void normalize();
/**
* Normalizes this vector and stores the result in dst.
@ -293,7 +293,7 @@ public:
*
* @param dst The destination vector.
*/
void normalize(Vector3* dst) const;
Vector3 getNormalized() const;
/**
* Scales all elements of this vector by the specified value.

View File

@ -233,39 +233,30 @@ void Vector4::negate()
w = -w;
}
Vector4& Vector4::normalize()
void Vector4::normalize()
{
normalize(this);
return *this;
}
void Vector4::normalize(Vector4* dst) const
{
GP_ASSERT(dst);
if (dst != this)
{
dst->x = x;
dst->y = y;
dst->z = z;
dst->w = w;
}
float n = x * x + y * y + z * z + w * w;
// Already normalized.
if (n == 1.0f)
return;
n = sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
return;
n = 1.0f / n;
dst->x *= n;
dst->y *= n;
dst->z *= n;
dst->w *= n;
x *= n;
y *= n;
z *= n;
w *= n;
}
Vector4 Vector4::getNormalized() const
{
Vector4 v(*this);
v.normalize();
return v;
}
void Vector4::scale(float scalar)

View File

@ -273,7 +273,7 @@ public:
*
* @return This vector, after the normalization occurs.
*/
Vector4& normalize();
void normalize();
/**
* Normalizes this vector and stores the result in dst.
@ -284,7 +284,7 @@ public:
*
* @param dst The destination vector.
*/
void normalize(Vector4* dst) const;
Vector4 getNormalized() const;
/**
* Scales all elements of this vector by the specified value.

View File

@ -392,56 +392,56 @@ bool ScrollView::checkNeedBounce()
{
Vector2 scrollVector = Vector2(0.0f, _size.height) - Vector2(_innerContainer->getLeftInParent(), _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded && _rightBounceNeeded)
{
Vector2 scrollVector = Vector2(_size.width, _size.height) - Vector2(_innerContainer->getRightInParent(), _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _leftBounceNeeded)
{
Vector2 scrollVector = Vector2::ZERO - Vector2(_innerContainer->getLeftInParent(), _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded && _rightBounceNeeded)
{
Vector2 scrollVector = Vector2(_size.width, 0.0f) - Vector2(_innerContainer->getRightInParent(), _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_topBounceNeeded)
{
Vector2 scrollVector = Vector2(0.0f, _size.height) - Vector2(0.0f, _innerContainer->getTopInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_bottomBounceNeeded)
{
Vector2 scrollVector = Vector2::ZERO - Vector2(0.0f, _innerContainer->getBottomInParent());
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_leftBounceNeeded)
{
Vector2 scrollVector = Vector2::ZERO - Vector2(_innerContainer->getLeftInParent(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
else if (_rightBounceNeeded)
{
Vector2 scrollVector = Vector2(_size.width, 0.0f) - Vector2(_innerContainer->getRightInParent(), 0.0f);
float orSpeed = scrollVector.getLength()/(0.2f);
_bounceDir = scrollVector.normalize();
_bounceDir = scrollVector.getNormalized();
startBounceChildren(orSpeed);
}
return true;
@ -524,7 +524,7 @@ void ScrollView::startAutoScrollChildrenWithDestination(const Vector2& des, floa
_needCheckAutoScrollDestination = false;
_autoScrollDestination = des;
Vector2 dis = des - _innerContainer->getPosition();
Vector2 dir = dis.normalize();
Vector2 dir = dis.getNormalized();
float orSpeed = 0.0f;
float acceleration = -1000.0f;
if (attenuated)
@ -1412,7 +1412,7 @@ void ScrollView::endRecordSlidAction()
{
Vector2 subVector = _touchEndedPoint - _touchBeganPoint;
totalDis = subVector.getLength();
dir = subVector.normalize();
dir = subVector.getNormalized();
break;
}
default:

View File

@ -614,7 +614,7 @@ void LayerGradientTest::onTouchesMoved(const std::vector<Touch*>& touches, Event
auto start = touch->getLocation();
auto diff = Vector2(s.width/2,s.height/2) - start;
diff = diff.normalize();
diff = diff.getNormalized();
auto gradient = static_cast<LayerGradient*>( getChildByTag(1) );
gradient->setVector(diff);

View File

@ -1182,7 +1182,7 @@ bool PhysicsDemoSlice::slice(PhysicsWorld &world, const PhysicsRayCastInfo& info
if (!info.shape->containsPoint(info.start) && !info.shape->containsPoint(info.end))
{
Vector2 normal = info.end - info.start;
normal = normal.getPerp().normalize();
normal = normal.getPerp().getNormalized();
float dist = info.start.dot(normal);
clipPoly(dynamic_cast<PhysicsShapePolygon*>(info.shape), normal, dist);