ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Cloth.h
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 // This source code is about a ground filtering algorithm for airborn LiDAR data
9 // based on physical process simulations, specifically cloth simulation.
10 //
11 // This code is based on a Cloth Simulation Tutorial at the cg.alexandra.dk
12 // blog. Thanks to Jesper Mosegaard (clothTutorial@jespermosegaard.dk)
13 //
14 // When applying the cloth simulation to LIDAR point filtering. A lot of
15 // features have been added to the original source code, including
16 //* configuration file management
17 //* point cloud data read/write
18 //* point-to-point collsion detection
19 //* nearest point search structure from CGAL
20 //* addding a terrain class
21 
22 // using discrete steps (drop and pull) to approximate the physical process
23 // Finding the max height value in nearest N points aroud every particles, as
24 // the lowest position where the particles can get.
25 
26 #pragma once
27 
28 // local
29 #include "Particle.h"
30 #include "Vec3.h"
31 
32 // system
33 #include <string>
34 #include <vector>
35 
36 class ccMesh;
37 
38 struct XY {
39  XY(int x1, int y1) : x(x1), y(y1) {}
40  int x;
41  int y;
42 };
43 
44 class Cloth {
45 private:
46  // total number of particles is num_particles_width*num_particles_height
47  int constraint_iterations;
48 
49  double time_step;
50 
51  std::vector<Particle>
52  particles; // all particles that are part of this cloth
53  // std::vector<Constraint> constraints; // alle constraints between
54  // particles as part of this cloth
55 
56  // parameters of slope postpocessing
57  double smoothThreshold;
58  double heightThreshold;
59 
60  // heightvalues
61  std::vector<double> heightvals;
62 
63  // movable particle index
64  std::vector<int> movableIndex;
65  std::vector<std::vector<int>> particle_edges;
66 
67  // record
68  inline void addConstraint(
69  Particle* p1,
70  Particle* p2) { /*constraints.push_back(Constraint(p1, p2));*/
71  p1->neighborsList.push_back(
72  p2); // record the neighbor for each particle
73  p2->neighborsList.push_back(p1);
74  }
75 
76 public:
77  inline Particle& getParticle(int x, int y) {
78  return particles[y * num_particles_width + x];
79  }
80  inline const Particle& getParticle(int x, int y) const {
81  return particles[y * num_particles_width + x];
82  }
83  inline Particle& getParticleByIndex(int index) { return particles[index]; }
84  inline const Particle getParticleByIndex(int index) const {
85  return particles[index];
86  }
87 
88  int num_particles_width; // number of particles in "width" direction
89  int num_particles_height; // number of particles in "height" direction
91  double step_x, step_y;
92 
93  inline int getSize() const {
95  }
96 
97  inline std::vector<double>& getHeightvals() { return heightvals; }
98 
99 public:
100  /* This is a important constructor for the entire system of particles and
101  * constraints */
102  Cloth(const Vec3& origin_pos,
105  double step_x,
106  double step_y,
107  double smoothThreshold,
108  double heightThreshold,
109  int rigidness,
110  double time_step);
111 
112  void setheightvals(const std::vector<double>& heightvals) {
113  this->heightvals = heightvals;
114  }
115 
120  double timeStep();
121 
122  /* used to add gravity (or any other arbitrary vector) to all particles */
123  void addForce(const Vec3& direction);
124 
125  // detecting collision of cloth and terrain
126  void terrainCollision();
127 
128  // implementing postpocessing to movable particles
129  void movableFilter();
130  // 找到每组可移动点,这个连通分量周围的不可移动点。从四周向中间逼近
131  void findUnmovablePoint(const std::vector<XY>& connected,
132  const std::vector<double>& heightvals,
133  std::vector<int>& edgePoints);
134 
135  // 直接对联通分量进行边坡处理
136  void handle_slop_connected(const std::vector<int>& edgePoints,
137  const std::vector<XY>& connected,
138  const std::vector<std::vector<int>>& neighbors,
139  const std::vector<double>& heightvals);
140 
141  // saving the cloth to file
142  void saveToFile(std::string path = "");
143  // saving the movable particles to file
144  void saveMovableToFile(std::string path = "");
145 
147  ccMesh* toMesh() const;
148 };
Definition: Cloth.h:44
void saveToFile(std::string path="")
Definition: Cloth.cpp:434
const Particle & getParticle(int x, int y) const
Definition: Cloth.h:80
Particle & getParticleByIndex(int index)
Definition: Cloth.h:83
int getSize() const
Definition: Cloth.h:93
void setheightvals(const std::vector< double > &heightvals)
Definition: Cloth.h:112
int num_particles_width
Definition: Cloth.h:88
void movableFilter()
Definition: Cloth.cpp:214
void addForce(const Vec3 &direction)
Definition: Cloth.cpp:187
double step_y
Definition: Cloth.h:91
void findUnmovablePoint(const std::vector< XY > &connected, const std::vector< double > &heightvals, std::vector< int > &edgePoints)
Definition: Cloth.cpp:318
std::vector< double > & getHeightvals()
Definition: Cloth.h:97
void saveMovableToFile(std::string path="")
Definition: Cloth.cpp:453
int num_particles_height
Definition: Cloth.h:89
Cloth(const Vec3 &origin_pos, int num_particles_width, int num_particles_height, double step_x, double step_y, double smoothThreshold, double heightThreshold, int rigidness, double time_step)
Definition: Cloth.cpp:24
void terrainCollision()
Definition: Cloth.cpp:198
Vec3 origin_pos
Definition: Cloth.h:90
double timeStep()
Definition: Cloth.cpp:146
void handle_slop_connected(const std::vector< int > &edgePoints, const std::vector< XY > &connected, const std::vector< std::vector< int >> &neighbors, const std::vector< double > &heightvals)
Definition: Cloth.cpp:393
Particle & getParticle(int x, int y)
Definition: Cloth.h:77
ccMesh * toMesh() const
Converts the cloth to a CC mesh structure.
Definition: Cloth.cpp:104
double step_x
Definition: Cloth.h:91
const Particle getParticleByIndex(int index) const
Definition: Cloth.h:84
std::vector< Particle * > neighborsList
Definition: Particle.h:44
Definition: Vec3.h:14
Triangular mesh.
Definition: ecvMesh.h:35
static const std::string path
Definition: PointCloud.cpp:59
Definition: Cloth.h:38
int x
Definition: Cloth.h:40
int y
Definition: Cloth.h:41
XY(int x1, int y1)
Definition: Cloth.h:39