| 21 | |
| 22 | |
| 23 | Cloth::Cloth(const Vec3& _origin_pos, |
| 24 | int _num_particles_width, |
| 25 | int _num_particles_height, |
| 26 | double _step_x, |
| 27 | double _step_y, |
| 28 | double _smoothThreshold, |
| 29 | double _heightThreshold, |
| 30 | int rigidness, |
| 31 | double time_step, |
| 32 | bool debug, |
| 33 | string output_dir) |
| 34 | : constraint_iterations(rigidness), |
| 35 | smoothThreshold(_smoothThreshold), |
| 36 | heightThreshold(_heightThreshold), |
| 37 | m_outputDir(output_dir), |
| 38 | debug(debug), |
| 39 | origin_pos(_origin_pos), |
| 40 | step_x(_step_x), |
| 41 | step_y(_step_y), |
| 42 | num_particles_width(_num_particles_width), |
| 43 | num_particles_height(_num_particles_height) { |
| 44 | // I am essentially using this vector as an array with room for |
| 45 | // num_particles_width*num_particles_height particles |
| 46 | particles.resize(num_particles_width * num_particles_height); |
| 47 | |
| 48 | double time_step2 = time_step * time_step; |
| 49 | |
| 50 | // creating particles in a grid of particles from (0,0,0) to |
| 51 | // (width,-height,0) creating particles in a grid |
| 52 | for (int i = 0; i < num_particles_width; i++) { |
| 53 | for (int j = 0; j < num_particles_height; j++) { |
| 54 | Vec3 pos(origin_pos.f[0] + i *step_x, |
| 55 | origin_pos.f[1], |
| 56 | origin_pos.f[2] + j *step_y); |
| 57 | |
| 58 | // insert particle in column i at j'th row |
| 59 | particles[j * num_particles_width + i] = Particle(pos, time_step2); |
| 60 | particles[j * num_particles_width + i].pos_x = i; |
| 61 | particles[j * num_particles_width + i].pos_y = j; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | saveToFile("initial-nodes.txt"); |
| 66 | |
| 67 | // Connecting immediate neighbor particles with constraints |
| 68 | // (distance 1 and sqrt(2) in the grid) |
| 69 | for (int x = 0; x < num_particles_width; x++) { |
| 70 | for (int y = 0; y < num_particles_height; y++) { |
| 71 | if (x < num_particles_width - 1) |
| 72 | makeConstraint(getParticle(x, y), getParticle(x + 1, y)); |
| 73 | |
| 74 | if (y < num_particles_height - 1) |
| 75 | makeConstraint(getParticle(x, y), getParticle(x, y + 1)); |
| 76 | |
| 77 | if ((x < num_particles_width - 1) && (y < num_particles_height - 1)) |
| 78 | makeConstraint(getParticle(x, y), getParticle(x + 1, y + 1)); |
| 79 | |
| 80 | if ((x < num_particles_width - 1) && (y < num_particles_height - 1)) |