本文整理汇总了C++中eigen::SparseMatrix::setIdentity方法的典型用法代码示例。如果您正苦于以下问题:C++ SparseMatrix::setIdentity方法的具体用法?C++ SparseMatrix::setIdentity怎么用?C++ SparseMatrix::setIdentity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eigen::SparseMatrix
的用法示例。
在下文中一共展示了SparseMatrix::setIdentity方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: solve
//=============================================================================
void ImplicitSolver::solve (float dt, float mass,
std::vector<Particle> &particles)
{
int num_particles = particles.size ();
/// Build the Jacobian matrix from the sparse set of elements
Eigen::SparseMatrix<float> J (2 * num_particles, 2 * num_particles);
J.setFromTriplets (triplets_.begin (), triplets_.end ());
/// Build up the position, velocity and force vectors
Eigen::VectorXf pos_vec (Eigen::VectorXf::Zero (2 * num_particles)),
velocity_vec (Eigen::VectorXf::Zero (2 * num_particles)),
force_vec (Eigen::VectorXf::Zero (2 * num_particles));
for (size_t p_i = 0; p_i < num_particles; ++p_i)
{
pos_vec (2 * p_i + 0) = particles[p_i].position[0];
pos_vec (2 * p_i + 1) = particles[p_i].position[1];
velocity_vec (2 * p_i + 0) = particles[p_i].velocity[0];
velocity_vec (2 * p_i + 1) = particles[p_i].velocity[1];
force_vec (2 * p_i + 0) = particles[p_i].force[0];
force_vec (2 * p_i + 1) = particles[p_i].force[1];
}
/// Kick out the fixed particles by creating a sparse selection matrix
std::vector<Eigen::Triplet<float> > triplets_selection;
int valid_particle_index = 0;
for (size_t p_i = 0; p_i < num_particles; ++p_i)
if (!particles[p_i].locked)
{
triplets_selection.push_back (Eigen::Triplet<float> (2 * p_i + 0, 2 * valid_particle_index + 0, 1.f));
triplets_selection.push_back (Eigen::Triplet<float> (2 * p_i + 1, 2 * valid_particle_index + 1, 1.f));
valid_particle_index ++;
}
Eigen::SparseMatrix<float> mat_selection (2 * num_particles, 2 * valid_particle_index);
mat_selection.setFromTriplets (triplets_selection.begin (), triplets_selection.end ());
/// Sparse identity matrix
Eigen::SparseMatrix<float> Id (2 * valid_particle_index, 2 * valid_particle_index);
Id.setIdentity ();
/// Apply the selection matrix on each vector and the Jacobian matrix
pos_vec = mat_selection.transpose () * pos_vec;
velocity_vec = mat_selection.transpose () * velocity_vec;
force_vec = mat_selection.transpose () * force_vec;
J = mat_selection.transpose () * J * mat_selection;
/// Build the right and left hand sides of the linear system
Eigen::SparseMatrix<float> A = Id - dt * dt / mass * J;
Eigen::VectorXf b;
b = dt * velocity_vec + dt * dt / mass * force_vec + (Id - dt * dt / mass * J) * pos_vec;
/// Solve the system and use the selection matrix again to arrange the new positions in a vector
linear_solver_.analyzePattern (A);
linear_solver_.compute (A);
Eigen::VectorXf new_pos = mat_selection * linear_solver_.solve (b);
/// Extract the positions from the solution vector and set the new positions and velocities inside the particle structures
for (size_t p_i = 0; p_i < num_particles; ++p_i)
{
if (!particles[p_i].locked)
{
vec2 pos_update (new_pos (2 * p_i + 0), new_pos (2 * p_i + 1));
particles[p_i].velocity = (pos_update - particles[p_i].position) / dt;
particles[p_i].position = pos_update;
}
}
}