forked from sascha/godot
bullet: Sync with upstream 3.17
Stop include Bullet headers using `-isystem` for GCC/Clang as it misleads SCons into not properly rebuilding all files when headers change. This means we also need to make sure Bullet builds without warning, and current version fares fairly well, there were just a couple to fix (patch included). Increase minimum version for distro packages to 2.90 (this was never released as the "next" version after 2.89 was 3.05... but that covers it too).3.4
parent
9b4e62d78f
commit
b7901c773c
@ -1,34 +0,0 @@
|
||||
diff --git a/thirdparty/bullet/BulletDynamics/Dynamics/btRigidBody.cpp b/thirdparty/bullet/BulletDynamics/Dynamics/btRigidBody.cpp
|
||||
index 9e8705b001..f1b50b39c8 100644
|
||||
--- a/thirdparty/bullet/BulletDynamics/Dynamics/btRigidBody.cpp
|
||||
+++ b/thirdparty/bullet/BulletDynamics/Dynamics/btRigidBody.cpp
|
||||
@@ -136,8 +136,13 @@ void btRigidBody::setGravity(const btVector3& acceleration)
|
||||
|
||||
void btRigidBody::setDamping(btScalar lin_damping, btScalar ang_damping)
|
||||
{
|
||||
- m_linearDamping = btClamped(lin_damping, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
|
||||
- m_angularDamping = btClamped(ang_damping, (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
|
||||
+#ifdef BT_USE_OLD_DAMPING_METHOD
|
||||
+ m_linearDamping = btMax(lin_damping, btScalar(0.0));
|
||||
+ m_angularDamping = btMax(ang_damping, btScalar(0.0));
|
||||
+#else
|
||||
+ m_linearDamping = btClamped(lin_damping, btScalar(0.0), btScalar(1.0));
|
||||
+ m_angularDamping = btClamped(ang_damping, btScalar(0.0), btScalar(1.0));
|
||||
+#endif
|
||||
}
|
||||
|
||||
///applyDamping damps the velocity, using the given m_linearDamping and m_angularDamping
|
||||
@@ -146,10 +151,9 @@ void btRigidBody::applyDamping(btScalar timeStep)
|
||||
//On new damping: see discussion/issue report here: http://code.google.com/p/bullet/issues/detail?id=74
|
||||
//todo: do some performance comparisons (but other parts of the engine are probably bottleneck anyway
|
||||
|
||||
-//#define USE_OLD_DAMPING_METHOD 1
|
||||
-#ifdef USE_OLD_DAMPING_METHOD
|
||||
- m_linearVelocity *= GEN_clamped((btScalar(1.) - timeStep * m_linearDamping), (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
|
||||
- m_angularVelocity *= GEN_clamped((btScalar(1.) - timeStep * m_angularDamping), (btScalar)btScalar(0.0), (btScalar)btScalar(1.0));
|
||||
+#ifdef BT_USE_OLD_DAMPING_METHOD
|
||||
+ m_linearVelocity *= btMax((btScalar(1.0) - timeStep * m_linearDamping), btScalar(0.0));
|
||||
+ m_angularVelocity *= btMax((btScalar(1.0) - timeStep * m_angularDamping), btScalar(0.0));
|
||||
#else
|
||||
m_linearVelocity *= btPow(btScalar(1) - m_linearDamping, timeStep);
|
||||
m_angularVelocity *= btPow(btScalar(1) - m_angularDamping, timeStep);
|
||||
@ -0,0 +1,112 @@
|
||||
/*
|
||||
Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
|
||||
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2019 Google Inc. http://bulletphysics.org
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef BT_CONJUGATE_RESIDUAL_H
|
||||
#define BT_CONJUGATE_RESIDUAL_H
|
||||
#include "btKrylovSolver.h"
|
||||
|
||||
template <class MatrixX>
|
||||
class btConjugateResidual : public btKrylovSolver<MatrixX>
|
||||
{
|
||||
typedef btAlignedObjectArray<btVector3> TVStack;
|
||||
typedef btKrylovSolver<MatrixX> Base;
|
||||
TVStack r, p, z, temp_p, temp_r, best_x;
|
||||
// temp_r = A*r
|
||||
// temp_p = A*p
|
||||
// z = M^(-1) * temp_p = M^(-1) * A * p
|
||||
btScalar best_r;
|
||||
|
||||
public:
|
||||
btConjugateResidual(const int max_it_in)
|
||||
: Base(max_it_in, 1e-8)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~btConjugateResidual() {}
|
||||
|
||||
// return the number of iterations taken
|
||||
int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false)
|
||||
{
|
||||
BT_PROFILE("CRSolve");
|
||||
btAssert(x.size() == b.size());
|
||||
reinitialize(b);
|
||||
// r = b - A * x --with assigned dof zeroed out
|
||||
A.multiply(x, temp_r); // borrow temp_r here to store A*x
|
||||
r = this->sub(b, temp_r);
|
||||
// z = M^(-1) * r
|
||||
A.precondition(r, z); // borrow z to store preconditioned r
|
||||
r = z;
|
||||
btScalar residual_norm = this->norm(r);
|
||||
if (residual_norm <= Base::m_tolerance)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
p = r;
|
||||
btScalar r_dot_Ar, r_dot_Ar_new;
|
||||
// temp_p = A*p
|
||||
A.multiply(p, temp_p);
|
||||
// temp_r = A*r
|
||||
temp_r = temp_p;
|
||||
r_dot_Ar = this->dot(r, temp_r);
|
||||
for (int k = 1; k <= Base::m_maxIterations; k++)
|
||||
{
|
||||
// z = M^(-1) * Ap
|
||||
A.precondition(temp_p, z);
|
||||
// alpha = r^T * A * r / (Ap)^T * M^-1 * Ap)
|
||||
btScalar alpha = r_dot_Ar / this->dot(temp_p, z);
|
||||
// x += alpha * p;
|
||||
this->multAndAddTo(alpha, p, x);
|
||||
// r -= alpha * z;
|
||||
this->multAndAddTo(-alpha, z, r);
|
||||
btScalar norm_r = this->norm(r);
|
||||
if (norm_r < best_r)
|
||||
{
|
||||
best_x = x;
|
||||
best_r = norm_r;
|
||||
if (norm_r < Base::m_tolerance)
|
||||
{
|
||||
return k;
|
||||
}
|
||||
}
|
||||
// temp_r = A * r;
|
||||
A.multiply(r, temp_r);
|
||||
r_dot_Ar_new = this->dot(r, temp_r);
|
||||
btScalar beta = r_dot_Ar_new / r_dot_Ar;
|
||||
r_dot_Ar = r_dot_Ar_new;
|
||||
// p = beta*p + r;
|
||||
p = this->multAndAdd(beta, p, r);
|
||||
// temp_p = beta*temp_p + temp_r;
|
||||
temp_p = this->multAndAdd(beta, temp_p, temp_r);
|
||||
}
|
||||
if (verbose)
|
||||
{
|
||||
std::cout << "ConjugateResidual max iterations reached, residual = " << best_r << std::endl;
|
||||
}
|
||||
x = best_x;
|
||||
return Base::m_maxIterations;
|
||||
}
|
||||
|
||||
void reinitialize(const TVStack& b)
|
||||
{
|
||||
r.resize(b.size());
|
||||
p.resize(b.size());
|
||||
z.resize(b.size());
|
||||
temp_p.resize(b.size());
|
||||
temp_r.resize(b.size());
|
||||
best_x.resize(b.size());
|
||||
best_r = SIMD_INFINITY;
|
||||
}
|
||||
};
|
||||
#endif /* btConjugateResidual_h */
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,162 @@
|
||||
/*
|
||||
Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
|
||||
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2019 Google Inc. http://bulletphysics.org
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef BT_MOUSE_PICKING_FORCE_H
|
||||
#define BT_MOUSE_PICKING_FORCE_H
|
||||
|
||||
#include "btDeformableLagrangianForce.h"
|
||||
|
||||
class btDeformableMousePickingForce : public btDeformableLagrangianForce
|
||||
{
|
||||
// If true, the damping force will be in the direction of the spring
|
||||
// If false, the damping force will be in the direction of the velocity
|
||||
btScalar m_elasticStiffness, m_dampingStiffness;
|
||||
const btSoftBody::Face& m_face;
|
||||
btVector3 m_mouse_pos;
|
||||
btScalar m_maxForce;
|
||||
|
||||
public:
|
||||
typedef btAlignedObjectArray<btVector3> TVStack;
|
||||
btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face& face, const btVector3& mouse_pos, btScalar maxForce = 0.3) : m_elasticStiffness(k), m_dampingStiffness(d), m_face(face), m_mouse_pos(mouse_pos), m_maxForce(maxForce)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void addScaledForces(btScalar scale, TVStack& force)
|
||||
{
|
||||
addScaledDampingForce(scale, force);
|
||||
addScaledElasticForce(scale, force);
|
||||
}
|
||||
|
||||
virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
|
||||
{
|
||||
addScaledElasticForce(scale, force);
|
||||
}
|
||||
|
||||
virtual void addScaledDampingForce(btScalar scale, TVStack& force)
|
||||
{
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 v_diff = m_face.m_n[i]->m_v;
|
||||
btVector3 scaled_force = scale * m_dampingStiffness * v_diff;
|
||||
if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
|
||||
scaled_force = scale * m_dampingStiffness * v_diff.dot(dir) * dir;
|
||||
}
|
||||
force[m_face.m_n[i]->index] -= scaled_force;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void addScaledElasticForce(btScalar scale, TVStack& force)
|
||||
{
|
||||
btScalar scaled_stiffness = scale * m_elasticStiffness;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
|
||||
btVector3 scaled_force = scaled_stiffness * dir;
|
||||
if (scaled_force.safeNorm() > m_maxForce)
|
||||
{
|
||||
scaled_force.safeNormalize();
|
||||
scaled_force *= m_maxForce;
|
||||
}
|
||||
force[m_face.m_n[i]->index] -= scaled_force;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
|
||||
{
|
||||
btScalar scaled_k_damp = m_dampingStiffness * scale;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index];
|
||||
if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
|
||||
local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index].dot(dir) * dir;
|
||||
}
|
||||
df[m_face.m_n[i]->index] -= local_scaled_df;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
|
||||
|
||||
virtual double totalElasticEnergy(btScalar dt)
|
||||
{
|
||||
double energy = 0;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
|
||||
btVector3 scaled_force = m_elasticStiffness * dir;
|
||||
if (scaled_force.safeNorm() > m_maxForce)
|
||||
{
|
||||
scaled_force.safeNormalize();
|
||||
scaled_force *= m_maxForce;
|
||||
}
|
||||
energy += 0.5 * scaled_force.dot(dir);
|
||||
}
|
||||
return energy;
|
||||
}
|
||||
|
||||
virtual double totalDampingEnergy(btScalar dt)
|
||||
{
|
||||
double energy = 0;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 v_diff = m_face.m_n[i]->m_v;
|
||||
btVector3 scaled_force = m_dampingStiffness * v_diff;
|
||||
if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
|
||||
scaled_force = m_dampingStiffness * v_diff.dot(dir) * dir;
|
||||
}
|
||||
energy -= scaled_force.dot(m_face.m_n[i]->m_v) / dt;
|
||||
}
|
||||
return energy;
|
||||
}
|
||||
|
||||
virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
|
||||
{
|
||||
btScalar scaled_stiffness = scale * m_elasticStiffness;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
|
||||
btScalar dir_norm = dir.norm();
|
||||
btVector3 dir_normalized = (dir_norm > SIMD_EPSILON) ? dir.normalized() : btVector3(0, 0, 0);
|
||||
int id = m_face.m_n[i]->index;
|
||||
btVector3 dx_diff = dx[id];
|
||||
btScalar r = 0; // rest length is 0 for picking spring
|
||||
btVector3 scaled_df = btVector3(0, 0, 0);
|
||||
if (dir_norm > SIMD_EPSILON)
|
||||
{
|
||||
scaled_df -= scaled_stiffness * dir_normalized.dot(dx_diff) * dir_normalized;
|
||||
scaled_df += scaled_stiffness * dir_normalized.dot(dx_diff) * ((dir_norm - r) / dir_norm) * dir_normalized;
|
||||
scaled_df -= scaled_stiffness * ((dir_norm - r) / dir_norm) * dx_diff;
|
||||
}
|
||||
df[id] += scaled_df;
|
||||
}
|
||||
}
|
||||
|
||||
void setMousePos(const btVector3& p)
|
||||
{
|
||||
m_mouse_pos = p;
|
||||
}
|
||||
|
||||
virtual btDeformableLagrangianForceType getForceType()
|
||||
{
|
||||
return BT_MOUSE_PICKING_FORCE;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* btMassSpring_h */
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,107 @@
|
||||
/*
|
||||
Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
|
||||
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2019 Google Inc. http://bulletphysics.org
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef BT_KRYLOV_SOLVER_H
|
||||
#define BT_KRYLOV_SOLVER_H
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <LinearMath/btAlignedObjectArray.h>
|
||||
#include <LinearMath/btVector3.h>
|
||||
#include <LinearMath/btScalar.h>
|
||||
#include "LinearMath/btQuickprof.h"
|
||||
|
||||
template <class MatrixX>
|
||||
class btKrylovSolver
|
||||
{
|
||||
typedef btAlignedObjectArray<btVector3> TVStack;
|
||||
|
||||
public:
|
||||
int m_maxIterations;
|
||||
btScalar m_tolerance;
|
||||
btKrylovSolver(int maxIterations, btScalar tolerance)
|
||||
: m_maxIterations(maxIterations), m_tolerance(tolerance)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~btKrylovSolver() {}
|
||||
|
||||
virtual int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false) = 0;
|
||||
|
||||
virtual void reinitialize(const TVStack& b) = 0;
|
||||
|
||||
virtual SIMD_FORCE_INLINE TVStack sub(const TVStack& a, const TVStack& b)
|
||||
{
|
||||
// c = a-b
|
||||
btAssert(a.size() == b.size());
|
||||
TVStack c;
|
||||
c.resize(a.size());
|
||||
for (int i = 0; i < a.size(); ++i)
|
||||
{
|
||||
c[i] = a[i] - b[i];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE btScalar squaredNorm(const TVStack& a)
|
||||
{
|
||||
return dot(a, a);
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE btScalar norm(const TVStack& a)
|
||||
{
|
||||
btScalar ret = 0;
|
||||
for (int i = 0; i < a.size(); ++i)
|
||||
{
|
||||
for (int d = 0; d < 3; ++d)
|
||||
{
|
||||
ret = btMax(ret, btFabs(a[i][d]));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE btScalar dot(const TVStack& a, const TVStack& b)
|
||||
{
|
||||
btScalar ans(0);
|
||||
for (int i = 0; i < a.size(); ++i)
|
||||
ans += a[i].dot(b[i]);
|
||||
return ans;
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE void multAndAddTo(btScalar s, const TVStack& a, TVStack& result)
|
||||
{
|
||||
// result += s*a
|
||||
btAssert(a.size() == result.size());
|
||||
for (int i = 0; i < a.size(); ++i)
|
||||
result[i] += s * a[i];
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE TVStack multAndAdd(btScalar s, const TVStack& a, const TVStack& b)
|
||||
{
|
||||
// result = a*s + b
|
||||
TVStack result;
|
||||
result.resize(a.size());
|
||||
for (int i = 0; i < a.size(); ++i)
|
||||
result[i] = s * a[i] + b[i];
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual SIMD_FORCE_INLINE void setTolerance(btScalar tolerance)
|
||||
{
|
||||
m_tolerance = tolerance;
|
||||
}
|
||||
};
|
||||
#endif /* BT_KRYLOV_SOLVER_H */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,447 @@
|
||||
// poly34.cpp : solution of cubic and quartic equation
|
||||
// (c) Khashin S.I. http://math.ivanovo.ac.ru/dalgebra/Khashin/index.html
|
||||
// khash2 (at) gmail.com
|
||||
// Thanks to Alexandr Rakhmanin <rakhmanin (at) gmail.com>
|
||||
// public domain
|
||||
//
|
||||
#include <math.h>
|
||||
|
||||
#include "poly34.h" // solution of cubic and quartic equation
|
||||
#define TwoPi 6.28318530717958648
|
||||
const btScalar eps = SIMD_EPSILON;
|
||||
|
||||
//=============================================================================
|
||||
// _root3, root3 from http://prografix.narod.ru
|
||||
//=============================================================================
|
||||
static SIMD_FORCE_INLINE btScalar _root3(btScalar x)
|
||||
{
|
||||
btScalar s = 1.;
|
||||
while (x < 1.)
|
||||
{
|
||||
x *= 8.;
|
||||
s *= 0.5;
|
||||
}
|
||||
while (x > 8.)
|
||||
{
|
||||
x *= 0.125;
|
||||
s *= 2.;
|
||||
}
|
||||
btScalar r = 1.5;
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
r -= 1. / 3. * (r - x / (r * r));
|
||||
return r * s;
|
||||
}
|
||||
|
||||
btScalar SIMD_FORCE_INLINE root3(btScalar x)
|
||||
{
|
||||
if (x > 0)
|
||||
return _root3(x);
|
||||
else if (x < 0)
|
||||
return -_root3(-x);
|
||||
else
|
||||
return 0.;
|
||||
}
|
||||
|
||||
// x - array of size 2
|
||||
// return 2: 2 real roots x[0], x[1]
|
||||
// return 0: pair of complex roots: x[0]i*x[1]
|
||||
int SolveP2(btScalar* x, btScalar a, btScalar b)
|
||||
{ // solve equation x^2 + a*x + b = 0
|
||||
btScalar D = 0.25 * a * a - b;
|
||||
if (D >= 0)
|
||||
{
|
||||
D = sqrt(D);
|
||||
x[0] = -0.5 * a + D;
|
||||
x[1] = -0.5 * a - D;
|
||||
return 2;
|
||||
}
|
||||
x[0] = -0.5 * a;
|
||||
x[1] = sqrt(-D);
|
||||
return 0;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
// x - array of size 3
|
||||
// In case 3 real roots: => x[0], x[1], x[2], return 3
|
||||
// 2 real roots: x[0], x[1], return 2
|
||||
// 1 real root : x[0], x[1] i*x[2], return 1
|
||||
int SolveP3(btScalar* x, btScalar a, btScalar b, btScalar c)
|
||||
{ // solve cubic equation x^3 + a*x^2 + b*x + c = 0
|
||||
btScalar a2 = a * a;
|
||||
btScalar q = (a2 - 3 * b) / 9;
|
||||
if (q < 0)
|
||||
q = eps;
|
||||
btScalar r = (a * (2 * a2 - 9 * b) + 27 * c) / 54;
|
||||
// equation x^3 + q*x + r = 0
|
||||
btScalar r2 = r * r;
|
||||
btScalar q3 = q * q * q;
|
||||
btScalar A, B;
|
||||
if (r2 <= (q3 + eps))
|
||||
{ //<<-- FIXED!
|
||||
btScalar t = r / sqrt(q3);
|
||||
if (t < -1)
|
||||
t = -1;
|
||||
if (t > 1)
|
||||
t = 1;
|
||||
t = acos(t);
|
||||
a /= 3;
|
||||
q = -2 * sqrt(q);
|
||||
x[0] = q * cos(t / 3) - a;
|
||||
x[1] = q * cos((t + TwoPi) / 3) - a;
|
||||
x[2] = q * cos((t - TwoPi) / 3) - a;
|
||||
return (3);
|
||||
}
|
||||
else
|
||||
{
|
||||
//A =-pow(fabs(r)+sqrt(r2-q3),1./3);
|
||||
A = -root3(fabs(r) + sqrt(r2 - q3));
|
||||
if (r < 0)
|
||||
A = -A;
|
||||
B = (A == 0 ? 0 : q / A);
|
||||
|
||||
a /= 3;
|
||||
x[0] = (A + B) - a;
|
||||
x[1] = -0.5 * (A + B) - a;
|
||||
x[2] = 0.5 * sqrt(3.) * (A - B);
|
||||
if (fabs(x[2]) < eps)
|
||||
{
|
||||
x[2] = x[1];
|
||||
return (2);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
} // SolveP3(btScalar *x,btScalar a,btScalar b,btScalar c) {
|
||||
//---------------------------------------------------------------------------
|
||||
// a>=0!
|
||||
void CSqrt(btScalar x, btScalar y, btScalar& a, btScalar& b) // returns: a+i*s = sqrt(x+i*y)
|
||||
{
|
||||
btScalar r = sqrt(x * x + y * y);
|
||||
if (y == 0)
|
||||
{
|
||||
r = sqrt(r);
|
||||
if (x >= 0)
|
||||
{
|
||||
a = r;
|
||||
b = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
a = 0;
|
||||
b = r;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // y != 0
|
||||
a = sqrt(0.5 * (x + r));
|
||||
b = 0.5 * y / a;
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
int SolveP4Bi(btScalar* x, btScalar b, btScalar d) // solve equation x^4 + b*x^2 + d = 0
|
||||
{
|
||||
btScalar D = b * b - 4 * d;
|
||||
if (D >= 0)
|
||||
{
|
||||
btScalar sD = sqrt(D);
|
||||
btScalar x1 = (-b + sD) / 2;
|
||||
btScalar x2 = (-b - sD) / 2; // x2 <= x1
|
||||
if (x2 >= 0) // 0 <= x2 <= x1, 4 real roots
|
||||
{
|
||||
btScalar sx1 = sqrt(x1);
|
||||
btScalar sx2 = sqrt(x2);
|
||||
x[0] = -sx1;
|
||||
x[1] = sx1;
|
||||
x[2] = -sx2;
|
||||
x[3] = sx2;
|
||||
return 4;
|
||||
}
|
||||
if (x1 < 0) // x2 <= x1 < 0, two pair of imaginary roots
|
||||
{
|
||||
btScalar sx1 = sqrt(-x1);
|
||||
btScalar sx2 = sqrt(-x2);
|
||||
x[0] = 0;
|
||||
x[1] = sx1;
|
||||
x[2] = 0;
|
||||
x[3] = sx2;
|
||||
return 0;
|
||||
}
|
||||
// now x2 < 0 <= x1 , two real roots and one pair of imginary root
|
||||
btScalar sx1 = sqrt(x1);
|
||||
btScalar sx2 = sqrt(-x2);
|
||||
x[0] = -sx1;
|
||||
x[1] = sx1;
|
||||
x[2] = 0;
|
||||
x[3] = sx2;
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{ // if( D < 0 ), two pair of compex roots
|
||||
btScalar sD2 = 0.5 * sqrt(-D);
|
||||
CSqrt(-0.5 * b, sD2, x[0], x[1]);
|
||||
CSqrt(-0.5 * b, -sD2, x[2], x[3]);
|
||||
return 0;
|
||||
} // if( D>=0 )
|
||||
} // SolveP4Bi(btScalar *x, btScalar b, btScalar d) // solve equation x^4 + b*x^2 d
|
||||
//---------------------------------------------------------------------------
|
||||
#define SWAP(a, b) \
|
||||
{ \
|
||||
t = b; \
|
||||
b = a; \
|
||||
a = t; \
|
||||
}
|
||||
static void dblSort3(btScalar& a, btScalar& b, btScalar& c) // make: a <= b <= c
|
||||
{
|
||||
btScalar t;
|
||||
if (a > b)
|
||||
SWAP(a, b); // now a<=b
|
||||
if (c < b)
|
||||
{
|
||||
SWAP(b, c); // now a<=b, b<=c
|
||||
if (a > b)
|
||||
SWAP(a, b); // now a<=b
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
int SolveP4De(btScalar* x, btScalar b, btScalar c, btScalar d) // solve equation x^4 + b*x^2 + c*x + d
|
||||
{
|
||||
//if( c==0 ) return SolveP4Bi(x,b,d); // After that, c!=0
|
||||
if (fabs(c) < 1e-14 * (fabs(b) + fabs(d)))
|
||||
return SolveP4Bi(x, b, d); // After that, c!=0
|
||||
|
||||
int res3 = SolveP3(x, 2 * b, b * b - 4 * d, -c * c); // solve resolvent
|
||||
// by Viet theorem: x1*x2*x3=-c*c not equals to 0, so x1!=0, x2!=0, x3!=0
|
||||
if (res3 > 1) // 3 real roots,
|
||||
{
|
||||
dblSort3(x[0], x[1], x[2]); // sort roots to x[0] <= x[1] <= x[2]
|
||||
// Note: x[0]*x[1]*x[2]= c*c > 0
|
||||
if (x[0] > 0) // all roots are positive
|
||||
{
|
||||
btScalar sz1 = sqrt(x[0]);
|
||||
btScalar sz2 = sqrt(x[1]);
|
||||
btScalar sz3 = sqrt(x[2]);
|
||||
// Note: sz1*sz2*sz3= -c (and not equal to 0)
|
||||
if (c > 0)
|
||||
{
|
||||
x[0] = (-sz1 - sz2 - sz3) / 2;
|
||||
x[1] = (-sz1 + sz2 + sz3) / 2;
|
||||
x[2] = (+sz1 - sz2 + sz3) / 2;
|
||||
x[3] = (+sz1 + sz2 - sz3) / 2;
|
||||
return 4;
|
||||
}
|
||||
// now: c<0
|
||||
x[0] = (-sz1 - sz2 + sz3) / 2;
|
||||
x[1] = (-sz1 + sz2 - sz3) / 2;
|
||||
x[2] = (+sz1 - sz2 - sz3) / 2;
|
||||
x[3] = (+sz1 + sz2 + sz3) / 2;
|
||||
return 4;
|
||||
} // if( x[0] > 0) // all roots are positive
|
||||
// now x[0] <= x[1] < 0, x[2] > 0
|
||||
// two pair of comlex roots
|
||||
btScalar sz1 = sqrt(-x[0]);
|
||||
btScalar sz2 = sqrt(-x[1]);
|
||||
btScalar sz3 = sqrt(x[2]);
|
||||
|
||||
if (c > 0) // sign = -1
|
||||
{
|
||||
x[0] = -sz3 / 2;
|
||||
x[1] = (sz1 - sz2) / 2; // x[0]i*x[1]
|
||||
x[2] = sz3 / 2;
|
||||
x[3] = (-sz1 - sz2) / 2; // x[2]i*x[3]
|
||||
return 0;
|
||||
}
|
||||
// now: c<0 , sign = +1
|
||||
x[0] = sz3 / 2;
|
||||
x[1] = (-sz1 + sz2) / 2;
|
||||
x[2] = -sz3 / 2;
|
||||
x[3] = (sz1 + sz2) / 2;
|
||||
return 0;
|
||||
} // if( res3>1 ) // 3 real roots,
|
||||
// now resoventa have 1 real and pair of compex roots
|
||||
// x[0] - real root, and x[0]>0,
|
||||
// x[1]i*x[2] - complex roots,
|
||||
// x[0] must be >=0. But one times x[0]=~ 1e-17, so:
|
||||
if (x[0] < 0)
|
||||
x[0] = 0;
|
||||
btScalar sz1 = sqrt(x[0]);
|
||||
btScalar szr, szi;
|
||||
CSqrt(x[1], x[2], szr, szi); // (szr+i*szi)^2 = x[1]+i*x[2]
|
||||
if (c > 0) // sign = -1
|
||||
{
|
||||
x[0] = -sz1 / 2 - szr; // 1st real root
|
||||
x[1] = -sz1 / 2 + szr; // 2nd real root
|
||||
x[2] = sz1 / 2;
|
||||
x[3] = szi;
|
||||
return 2;
|
||||
}
|
||||
// now: c<0 , sign = +1
|
||||
x[0] = sz1 / 2 - szr; // 1st real root
|
||||
x[1] = sz1 / 2 + szr; // 2nd real root
|
||||
x[2] = -sz1 / 2;
|
||||
x[3] = szi;
|
||||
return 2;
|
||||
} // SolveP4De(btScalar *x, btScalar b, btScalar c, btScalar d) // solve equation x^4 + b*x^2 + c*x + d
|
||||
//-----------------------------------------------------------------------------
|
||||
btScalar N4Step(btScalar x, btScalar a, btScalar b, btScalar c, btScalar d) // one Newton step for x^4 + a*x^3 + b*x^2 + c*x + d
|
||||
{
|
||||
btScalar fxs = ((4 * x + 3 * a) * x + 2 * b) * x + c; // f'(x)
|
||||
if (fxs == 0)
|
||||
return x; //return 1e99; <<-- FIXED!
|
||||
btScalar fx = (((x + a) * x + b) * x + c) * x + d; // f(x)
|
||||
return x - fx / fxs;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
// x - array of size 4
|
||||
// return 4: 4 real roots x[0], x[1], x[2], x[3], possible multiple roots
|
||||
// return 2: 2 real roots x[0], x[1] and complex x[2]i*x[3],
|
||||
// return 0: two pair of complex roots: x[0]i*x[1], x[2]i*x[3],
|
||||
int SolveP4(btScalar* x, btScalar a, btScalar b, btScalar c, btScalar d)
|
||||
{ // solve equation x^4 + a*x^3 + b*x^2 + c*x + d by Dekart-Euler method
|
||||
// move to a=0:
|
||||
btScalar d1 = d + 0.25 * a * (0.25 * b * a - 3. / 64 * a * a * a - c);
|
||||
btScalar c1 = c + 0.5 * a * (0.25 * a * a - b);
|
||||
btScalar b1 = b - 0.375 * a * a;
|
||||
int res = SolveP4De(x, b1, c1, d1);
|
||||
if (res == 4)
|
||||
{
|
||||
x[0] -= a / 4;
|
||||
x[1] -= a / 4;
|
||||
x[2] -= a / 4;
|
||||
x[3] -= a / 4;
|
||||
}
|
||||
else if (res == 2)
|
||||
{
|
||||
x[0] -= a / 4;
|
||||
x[1] -= a / 4;
|
||||
x[2] -= a / 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
x[0] -= a / 4;
|
||||
x[2] -= a / 4;
|
||||
}
|
||||
// one Newton step for each real root:
|
||||
if (res > 0)
|
||||
{
|
||||
x[0] = N4Step(x[0], a, b, c, d);
|
||||
x[1] = N4Step(x[1], a, b, c, d);
|
||||
}
|
||||
if (res > 2)
|
||||
{
|
||||
x[2] = N4Step(x[2], a, b, c, d);
|
||||
x[3] = N4Step(x[3], a, b, c, d);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
#define F5(t) (((((t + a) * t + b) * t + c) * t + d) * t + e)
|
||||
//-----------------------------------------------------------------------------
|
||||
btScalar SolveP5_1(btScalar a, btScalar b, btScalar c, btScalar d, btScalar e) // return real root of x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
{
|
||||
int cnt;
|
||||
if (fabs(e) < eps)
|
||||
return 0;
|
||||
|
||||
btScalar brd = fabs(a); // brd - border of real roots
|
||||
if (fabs(b) > brd)
|
||||
brd = fabs(b);
|
||||
if (fabs(c) > brd)
|
||||
brd = fabs(c);
|
||||
if (fabs(d) > brd)
|
||||
brd = fabs(d);
|
||||
if (fabs(e) > brd)
|
||||
brd = fabs(e);
|
||||
brd++; // brd - border of real roots
|
||||
|
||||
btScalar x0, f0; // less than root
|
||||
btScalar x1, f1; // greater than root
|
||||
btScalar x2, f2, f2s; // next values, f(x2), f'(x2)
|
||||
btScalar dx = 0;
|
||||
|
||||
if (e < 0)
|
||||
{
|
||||
x0 = 0;
|
||||
x1 = brd;
|
||||
f0 = e;
|
||||
f1 = F5(x1);
|
||||
x2 = 0.01 * brd;
|
||||
} // positive root
|
||||
else
|
||||
{
|
||||
x0 = -brd;
|
||||
x1 = 0;
|
||||
f0 = F5(x0);
|
||||
f1 = e;
|
||||
x2 = -0.01 * brd;
|
||||
} // negative root
|
||||
|
||||
if (fabs(f0) < eps)
|
||||
return x0;
|
||||
if (fabs(f1) < eps)
|
||||
return x1;
|
||||
|
||||
// now x0<x1, f(x0)<0, f(x1)>0
|
||||
// Firstly 10 bisections
|
||||
for (cnt = 0; cnt < 10; cnt++)
|
||||
{
|
||||
x2 = (x0 + x1) / 2; // next point
|
||||
//x2 = x0 - f0*(x1 - x0) / (f1 - f0); // next point
|
||||
f2 = F5(x2); // f(x2)
|
||||
if (fabs(f2) < eps)
|
||||
return x2;
|
||||
if (f2 > 0)
|
||||
{
|
||||
x1 = x2;
|
||||
f1 = f2;
|
||||
}
|
||||
else
|
||||
{
|
||||
x0 = x2;
|
||||
f0 = f2;
|
||||
}
|
||||
}
|
||||
|
||||
// At each step:
|
||||
// x0<x1, f(x0)<0, f(x1)>0.
|
||||
// x2 - next value
|
||||
// we hope that x0 < x2 < x1, but not necessarily
|
||||
do
|
||||
{
|
||||
if (cnt++ > 50)
|
||||
break;
|
||||
if (x2 <= x0 || x2 >= x1)
|
||||
x2 = (x0 + x1) / 2; // now x0 < x2 < x1
|
||||
f2 = F5(x2); // f(x2)
|
||||
if (fabs(f2) < eps)
|
||||
return x2;
|
||||
if (f2 > 0)
|
||||
{
|
||||
x1 = x2;
|
||||
f1 = f2;
|
||||
}
|
||||
else
|
||||
{
|
||||
x0 = x2;
|
||||
f0 = f2;
|
||||
}
|
||||
f2s = (((5 * x2 + 4 * a) * x2 + 3 * b) * x2 + 2 * c) * x2 + d; // f'(x2)
|
||||
if (fabs(f2s) < eps)
|
||||
{
|
||||
x2 = 1e99;
|
||||
continue;
|
||||
}
|
||||
dx = f2 / f2s;
|
||||
x2 -= dx;
|
||||
} while (fabs(dx) > eps);
|
||||
return x2;
|
||||
} // SolveP5_1(btScalar a,btScalar b,btScalar c,btScalar d,btScalar e) // return real root of x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
//-----------------------------------------------------------------------------
|
||||
int SolveP5(btScalar* x, btScalar a, btScalar b, btScalar c, btScalar d, btScalar e) // solve equation x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
{
|
||||
btScalar r = x[0] = SolveP5_1(a, b, c, d, e);
|
||||
btScalar a1 = a + r, b1 = b + r * a1, c1 = c + r * b1, d1 = d + r * c1;
|
||||
return 1 + SolveP4(x + 1, a1, b1, c1, d1);
|
||||
} // SolveP5(btScalar *x,btScalar a,btScalar b,btScalar c,btScalar d,btScalar e) // solve equation x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
//-----------------------------------------------------------------------------
|
||||
@ -0,0 +1,38 @@
|
||||
// poly34.h : solution of cubic and quartic equation
|
||||
// (c) Khashin S.I. http://math.ivanovo.ac.ru/dalgebra/Khashin/index.html
|
||||
// khash2 (at) gmail.com
|
||||
|
||||
#ifndef POLY_34
|
||||
#define POLY_34
|
||||
#include "LinearMath/btScalar.h"
|
||||
// x - array of size 2
|
||||
// return 2: 2 real roots x[0], x[1]
|
||||
// return 0: pair of complex roots: x[0]i*x[1]
|
||||
int SolveP2(btScalar* x, btScalar a, btScalar b); // solve equation x^2 + a*x + b = 0
|
||||
|
||||
// x - array of size 3
|
||||
// return 3: 3 real roots x[0], x[1], x[2]
|
||||
// return 1: 1 real root x[0] and pair of complex roots: x[1]i*x[2]
|
||||
int SolveP3(btScalar* x, btScalar a, btScalar b, btScalar c); // solve cubic equation x^3 + a*x^2 + b*x + c = 0
|
||||
|
||||
// x - array of size 4
|
||||
// return 4: 4 real roots x[0], x[1], x[2], x[3], possible multiple roots
|
||||
// return 2: 2 real roots x[0], x[1] and complex x[2]i*x[3],
|
||||
// return 0: two pair of complex roots: x[0]i*x[1], x[2]i*x[3],
|
||||
int SolveP4(btScalar* x, btScalar a, btScalar b, btScalar c, btScalar d); // solve equation x^4 + a*x^3 + b*x^2 + c*x + d = 0 by Dekart-Euler method
|
||||
|
||||
// x - array of size 5
|
||||
// return 5: 5 real roots x[0], x[1], x[2], x[3], x[4], possible multiple roots
|
||||
// return 3: 3 real roots x[0], x[1], x[2] and complex x[3]i*x[4],
|
||||
// return 1: 1 real root x[0] and two pair of complex roots: x[1]i*x[2], x[3]i*x[4],
|
||||
int SolveP5(btScalar* x, btScalar a, btScalar b, btScalar c, btScalar d, btScalar e); // solve equation x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// And some additional functions for internal use.
|
||||
// Your may remove this definitions from here
|
||||
int SolveP4Bi(btScalar* x, btScalar b, btScalar d); // solve equation x^4 + b*x^2 + d = 0
|
||||
int SolveP4De(btScalar* x, btScalar b, btScalar c, btScalar d); // solve equation x^4 + b*x^2 + c*x + d = 0
|
||||
void CSqrt(btScalar x, btScalar y, btScalar& a, btScalar& b); // returns as a+i*s, sqrt(x+i*y)
|
||||
btScalar N4Step(btScalar x, btScalar a, btScalar b, btScalar c, btScalar d); // one Newton step for x^4 + a*x^3 + b*x^2 + c*x + d
|
||||
btScalar SolveP5_1(btScalar a, btScalar b, btScalar c, btScalar d, btScalar e); // return real root of x^5 + a*x^4 + b*x^3 + c*x^2 + d*x + e = 0
|
||||
#endif
|
||||
@ -0,0 +1,83 @@
|
||||
//
|
||||
// btModifiedGramSchmidt.h
|
||||
// LinearMath
|
||||
//
|
||||
// Created by Xuchen Han on 4/4/20.
|
||||
//
|
||||
|
||||
#ifndef btModifiedGramSchmidt_h
|
||||
#define btModifiedGramSchmidt_h
|
||||
|
||||
#include "btReducedVector.h"
|
||||
#include "btAlignedObjectArray.h"
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
template<class TV>
|
||||
class btModifiedGramSchmidt
|
||||
{
|
||||
public:
|
||||
btAlignedObjectArray<TV> m_in;
|
||||
btAlignedObjectArray<TV> m_out;
|
||||
|
||||
btModifiedGramSchmidt(const btAlignedObjectArray<TV>& vecs): m_in(vecs)
|
||||
{
|
||||
m_out.resize(0);
|
||||
}
|
||||
|
||||
void solve()
|
||||
{
|
||||
m_out.resize(m_in.size());
|
||||
for (int i = 0; i < m_in.size(); ++i)
|
||||
{
|
||||
// printf("========= starting %d ==========\n", i);
|
||||
TV v(m_in[i]);
|
||||
// v.print();
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
v = v - v.proj(m_out[j]);
|
||||
// v.print();
|
||||
}
|
||||
v.normalize();
|
||||
m_out[i] = v;
|
||||
// v.print();
|
||||
}
|
||||
}
|
||||
|
||||
void test()
|
||||
{
|
||||
std::cout << SIMD_EPSILON << std::endl;
|
||||
printf("=======inputs=========\n");
|
||||
for (int i = 0; i < m_out.size(); ++i)
|
||||
{
|
||||
m_in[i].print();
|
||||
}
|
||||
printf("=======output=========\n");
|
||||
for (int i = 0; i < m_out.size(); ++i)
|
||||
{
|
||||
m_out[i].print();
|
||||
}
|
||||
btScalar eps = SIMD_EPSILON;
|
||||
for (int i = 0; i < m_out.size(); ++i)
|
||||
{
|
||||
for (int j = 0; j < m_out.size(); ++j)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
if (std::abs(1.0-m_out[i].dot(m_out[j])) > eps)// && std::abs(m_out[i].dot(m_out[j])) > eps)
|
||||
{
|
||||
printf("vec[%d] is not unit, norm squared = %f\n", i,m_out[i].dot(m_out[j]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (std::abs(m_out[i].dot(m_out[j])) > eps)
|
||||
{
|
||||
printf("vec[%d] and vec[%d] is not orthogonal, dot product = %f\n", i, j, m_out[i].dot(m_out[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
template class btModifiedGramSchmidt<btReducedVector>;
|
||||
#endif /* btModifiedGramSchmidt_h */
|
||||
@ -0,0 +1,170 @@
|
||||
//
|
||||
// btReducedVector.cpp
|
||||
// LinearMath
|
||||
//
|
||||
// Created by Xuchen Han on 4/4/20.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include "btReducedVector.h"
|
||||
#include <cmath>
|
||||
|
||||
// returns the projection of this onto other
|
||||
btReducedVector btReducedVector::proj(const btReducedVector& other) const
|
||||
{
|
||||
btReducedVector ret(m_sz);
|
||||
btScalar other_length2 = other.length2();
|
||||
if (other_length2 < SIMD_EPSILON)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
return other*(this->dot(other))/other_length2;
|
||||
}
|
||||
|
||||
void btReducedVector::normalize()
|
||||
{
|
||||
if (this->length2() < SIMD_EPSILON)
|
||||
{
|
||||
m_indices.clear();
|
||||
m_vecs.clear();
|
||||
return;
|
||||
}
|
||||
*this /= std::sqrt(this->length2());
|
||||
}
|
||||
|
||||
bool btReducedVector::testAdd() const
|
||||
{
|
||||
int sz = 5;
|
||||
btAlignedObjectArray<int> id1;
|
||||
id1.push_back(1);
|
||||
id1.push_back(3);
|
||||
btAlignedObjectArray<btVector3> v1;
|
||||
v1.push_back(btVector3(1,0,1));
|
||||
v1.push_back(btVector3(3,1,5));
|
||||
btAlignedObjectArray<int> id2;
|
||||
id2.push_back(2);
|
||||
id2.push_back(3);
|
||||
id2.push_back(5);
|
||||
btAlignedObjectArray<btVector3> v2;
|
||||
v2.push_back(btVector3(2,3,1));
|
||||
v2.push_back(btVector3(3,4,9));
|
||||
v2.push_back(btVector3(0,4,0));
|
||||
btAlignedObjectArray<int> id3;
|
||||
id3.push_back(1);
|
||||
id3.push_back(2);
|
||||
id3.push_back(3);
|
||||
id3.push_back(5);
|
||||
btAlignedObjectArray<btVector3> v3;
|
||||
v3.push_back(btVector3(1,0,1));
|
||||
v3.push_back(btVector3(2,3,1));
|
||||
v3.push_back(btVector3(6,5,14));
|
||||
v3.push_back(btVector3(0,4,0));
|
||||
btReducedVector rv1(sz, id1, v1);
|
||||
btReducedVector rv2(sz, id2, v2);
|
||||
btReducedVector ans(sz, id3, v3);
|
||||
bool ret = ((ans == rv1+rv2) && (ans == rv2+rv1));
|
||||
if (!ret)
|
||||
printf("btReducedVector testAdd failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool btReducedVector::testMinus() const
|
||||
{
|
||||
int sz = 5;
|
||||
btAlignedObjectArray<int> id1;
|
||||
id1.push_back(1);
|
||||
id1.push_back(3);
|
||||
btAlignedObjectArray<btVector3> v1;
|
||||
v1.push_back(btVector3(1,0,1));
|
||||
v1.push_back(btVector3(3,1,5));
|
||||
btAlignedObjectArray<int> id2;
|
||||
id2.push_back(2);
|
||||
id2.push_back(3);
|
||||
id2.push_back(5);
|
||||
btAlignedObjectArray<btVector3> v2;
|
||||
v2.push_back(btVector3(2,3,1));
|
||||
v2.push_back(btVector3(3,4,9));
|
||||
v2.push_back(btVector3(0,4,0));
|
||||
btAlignedObjectArray<int> id3;
|
||||
id3.push_back(1);
|
||||
id3.push_back(2);
|
||||
id3.push_back(3);
|
||||
id3.push_back(5);
|
||||
btAlignedObjectArray<btVector3> v3;
|
||||
v3.push_back(btVector3(-1,-0,-1));
|
||||
v3.push_back(btVector3(2,3,1));
|
||||
v3.push_back(btVector3(0,3,4));
|
||||
v3.push_back(btVector3(0,4,0));
|
||||
btReducedVector rv1(sz, id1, v1);
|
||||
btReducedVector rv2(sz, id2, v2);
|
||||
btReducedVector ans(sz, id3, v3);
|
||||
bool ret = (ans == rv2-rv1);
|
||||
if (!ret)
|
||||
printf("btReducedVector testMinus failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool btReducedVector::testDot() const
|
||||
{
|
||||
int sz = 5;
|
||||
btAlignedObjectArray<int> id1;
|
||||
id1.push_back(1);
|
||||
id1.push_back(3);
|
||||
btAlignedObjectArray<btVector3> v1;
|
||||
v1.push_back(btVector3(1,0,1));
|
||||
v1.push_back(btVector3(3,1,5));
|
||||
btAlignedObjectArray<int> id2;
|
||||
id2.push_back(2);
|
||||
id2.push_back(3);
|
||||
id2.push_back(5);
|
||||
btAlignedObjectArray<btVector3> v2;
|
||||
v2.push_back(btVector3(2,3,1));
|
||||
v2.push_back(btVector3(3,4,9));
|
||||
v2.push_back(btVector3(0,4,0));
|
||||
btReducedVector rv1(sz, id1, v1);
|
||||
btReducedVector rv2(sz, id2, v2);
|
||||
btScalar ans = 58;
|
||||
bool ret = (ans == rv2.dot(rv1) && ans == rv1.dot(rv2));
|
||||
ans = 14+16+9+16+81;
|
||||
ret &= (ans==rv2.dot(rv2));
|
||||
|
||||
if (!ret)
|
||||
printf("btReducedVector testDot failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool btReducedVector::testMultiply() const
|
||||
{
|
||||
int sz = 5;
|
||||
btAlignedObjectArray<int> id1;
|
||||
id1.push_back(1);
|
||||
id1.push_back(3);
|
||||
btAlignedObjectArray<btVector3> v1;
|
||||
v1.push_back(btVector3(1,0,1));
|
||||
v1.push_back(btVector3(3,1,5));
|
||||
btScalar s = 2;
|
||||
btReducedVector rv1(sz, id1, v1);
|
||||
btAlignedObjectArray<int> id2;
|
||||
id2.push_back(1);
|
||||
id2.push_back(3);
|
||||
btAlignedObjectArray<btVector3> v2;
|
||||
v2.push_back(btVector3(2,0,2));
|
||||
v2.push_back(btVector3(6,2,10));
|
||||
btReducedVector ans(sz, id2, v2);
|
||||
bool ret = (ans == rv1*s);
|
||||
if (!ret)
|
||||
printf("btReducedVector testMultiply failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
void btReducedVector::test() const
|
||||
{
|
||||
bool ans = testAdd() && testMinus() && testDot() && testMultiply();
|
||||
if (ans)
|
||||
{
|
||||
printf("All tests passed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Tests failed\n");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue