ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Particle.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "Particle.h"
9 /* This is one of the important methods, where the time is progressed a single
10 step size (TIME_STEPSIZE) The method is called by Cloth.time_step() Given the
11 equation "force = mass * acceleration" the next position is found through verlet
12 integration*/
14  if (movable) {
15  Vec3 temp = pos;
16  pos = pos + (pos - old_pos) * (1.0 - DAMPING) +
17  acceleration * time_step2;
18  old_pos = temp;
19  // acceleration = Vec3(0, 0, 0); // acceleration is reset since it HAS
20  // been translated into a change in position (and implicitely into
21  // velocity)
22  }
23 }
24 
25 // we precompute the overall displacement of a particle accroding to the
26 // rigidness const double singleMove1[15] = {0, 0.4, 0.64, 0.784, 0.8704,
27 // 0.92224, 0.95334, 0.97201, 0.9832, 0.98992, 0.99395, 0.99637, 0.99782,
28 // 0.99869, 0.99922 };
29 const double singleMove1[15] = {0, 0.3, 0.51, 0.657, 0.7599,
30  0.83193, 0.88235, 0.91765, 0.94235, 0.95965,
31  0.97175, 0.98023, 0.98616, 0.99031, 0.99322};
32 // 当有两端移动时
33 // const double doubleMove1[15] = {0, 0.4, 0.48, 0.496, 0.4992, 0.49984,
34 // 0.49997, 0.49999, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
35 const double doubleMove1[15] = {0, 0.3, 0.42, 0.468, 0.4872,
36  0.4949, 0.498, 0.4992, 0.4997, 0.4999,
37  0.4999, 0.5, 0.5, 0.5, 0.5};
38 
39 void Particle::satisfyConstraintSelf(int constraintTimes) {
40  Particle *p1 = this;
41  for (int i = 0; i < neighborsList.size(); i++) {
42  Particle *p2 = neighborsList[i];
43  Vec3 correctionVector(0, p2->pos.y - p1->pos.y, 0);
44  if (p1->isMovable() && p2->isMovable()) {
45  Vec3 correctionVectorHalf =
46  correctionVector *
47  (constraintTimes > 14
48  ? 0.5
49  : doubleMove1
50  [constraintTimes]); // Lets make it half
51  // that length, so
52  // that we can move
53  // BOTH p1 and p2.
54  p1->offsetPos(correctionVectorHalf);
55  p2->offsetPos(-correctionVectorHalf);
56  } else if (p1->isMovable() && !p2->isMovable()) {
57  Vec3 correctionVectorHalf =
58  correctionVector *
59  (constraintTimes > 14
60  ? 1
61  : singleMove1
62  [constraintTimes]); // Lets make it half
63  // that length, so
64  // that we can move
65  // BOTH p1 and p2.
66  p1->offsetPos(correctionVectorHalf);
67  } else if (!p1->isMovable() && p2->isMovable()) {
68  Vec3 correctionVectorHalf =
69  correctionVector *
70  (constraintTimes > 14
71  ? 1
72  : singleMove1
73  [constraintTimes]); // Lets make it half
74  // that length, so
75  // that we can move
76  // BOTH p1 and p2.
77  p2->offsetPos(-correctionVectorHalf);
78  }
79  }
80 }
const double doubleMove1[15]
Definition: Particle.cpp:35
const double singleMove1[15]
Definition: Particle.cpp:29
#define DAMPING
Definition: Particle.h:14
bool isMovable() const
Definition: Particle.h:88
void timeStep()
Definition: Particle.cpp:13
Vec3 old_pos
Definition: Particle.h:39
void satisfyConstraintSelf(int constraintTimes)
Definition: Particle.cpp:39
void offsetPos(const Vec3 v)
Definition: Particle.h:94
Vec3 pos
Definition: Particle.h:38
std::vector< Particle * > neighborsList
Definition: Particle.h:44
Definition: Vec3.h:14
double y
Definition: Vec3.h:18