-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.cpp
170 lines (136 loc) · 5.65 KB
/
writer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Created by egor on 1/23/24.
//
#include "writer.h"
using namespace std;
static size_t write_counter = 1;
static size_t spring_connector_counter = 1;
static size_t neck_counter = 1;
void write_spring_connectors(std::string const & dir, double r_part, vector<array<Eigen::Vector3d, 5>> const & spring_connectors, vector<bool> const & enabled_contacts) {
stringstream path;
path << dir << "/spring_connectors_" << spring_connector_counter << ".vtk";
spring_connector_counter ++;
ofstream ofs(path.str());
if (!ofs.good()) {
cerr << "ERROR: Unable to create dump file" << "\n";
exit(EXIT_FAILURE);
}
size_t point_count = 0;
for (auto val : enabled_contacts) {
if (!val) point_count += 5;
}
ofs << "# vtk DataFile Version 4.0" << "\n";
ofs << "Generated by libFractalCommon" << "\n";
ofs << "ASCII" << "\n";
ofs << "DATASET POLYDATA" << "\n";
ofs << "POINTS " << point_count << " FLOAT" << "\n";
// Coordinates are normalized to avoid issues with ParaView and small particles
for (size_t i = 0; i < enabled_contacts.size(); i ++) {
if (!enabled_contacts[i]) {
// This is a necked contact
for (size_t j = 0; j < 5; j ++) {
Eigen::Vector3d p = spring_connectors[i][j];
ofs << p[0] / r_part << " " << p[1] / r_part << " " << p[2] / r_part << " ";
}
}
}
// Flush the buffer
ofs << std::endl;
}
void write_particles(const std::string & dir, std::vector<Eigen::Vector3d> const & x, std::vector<Eigen::Vector3d> const & theta, double r_part) {
stringstream path;
path << dir << "/particles_" << write_counter << ".vtk";
write_counter ++;
ofstream ofs(path.str());
if (!ofs.good()) {
cerr << "ERROR: Unable to create dump file" << "\n";
exit(EXIT_FAILURE);
}
ofs << "# vtk DataFile Version 4.0" << "\n";
ofs << "Generated by libFractalCommon" << "\n";
ofs << "ASCII" << "\n";
ofs << "DATASET POLYDATA" << "\n";
ofs << "POINTS " << x.size() << " FLOAT" << "\n";
// Coordinates are normalized to avoid issues with ParaView and small particles
for (auto const & p : x) {
ofs << p[0] / r_part << " " << p[1] / r_part << " " << p[2] / r_part << " ";
}
ofs << "\n" << "\n";
ofs << "POINT_DATA " << x.size() << "\n";
ofs << "FIELD FieldData 3" << "\n";
ofs << "thetax 3 " << x.size() << " double" << "\n";
for (auto const & t : theta) {
Eigen::Quaternion<double> q;
q = Eigen::AngleAxis(t[0], Eigen::Vector3d::UnitX())
* Eigen::AngleAxis(t[1], Eigen::Vector3d::UnitY())
* Eigen::AngleAxis(t[2], Eigen::Vector3d::UnitZ());
auto unit = Eigen::Vector3d::UnitX();
Eigen::Matrix3d m = q.matrix();
auto orient = m*unit;
ofs << orient[0] << " " << orient[1] << " " << orient[2] << " ";
}
ofs << "\n" << "thetay 3 " << x.size() << " double" << "\n";
for (auto const & t : theta) {
Eigen::Quaternion<double> q;
q = Eigen::AngleAxis(t[0], Eigen::Vector3d::UnitX())
* Eigen::AngleAxis(t[1], Eigen::Vector3d::UnitY())
* Eigen::AngleAxis(t[2], Eigen::Vector3d::UnitZ());
auto unit = Eigen::Vector3d::UnitY();
Eigen::Matrix3d m = q.matrix();
auto orient = m*unit;
ofs << orient[0] << " " << orient[1] << " " << orient[2] << " ";
}
ofs << "\n" << "thetaz 3 " << x.size() << " double" << "\n";
for (auto const & t : theta) {
Eigen::Quaternion<double> q;
q = Eigen::AngleAxis(t[0], Eigen::Vector3d::UnitX())
* Eigen::AngleAxis(t[1], Eigen::Vector3d::UnitY())
* Eigen::AngleAxis(t[2], Eigen::Vector3d::UnitZ());
auto unit = Eigen::Vector3d::UnitZ();
Eigen::Matrix3d m = q.matrix();
auto orient = m*unit;
ofs << orient[0] << " " << orient[1] << " " << orient[2] << " ";
}
// Flush the buffer
ofs << std::endl;
}
void write_necks(std::string const & dir, std::vector<Eigen::Vector3d> const & x, double r_part,
std::vector<bool> const & enabled_contacts) {
stringstream path;
path << dir << "/necks_" << neck_counter << ".vtk";
neck_counter ++;
ofstream ofs(path.str());
if (!ofs.good()) {
cerr << "ERROR: Unable to create dump file" << endl;
exit(EXIT_FAILURE);
}
std::vector<std::pair<size_t, size_t>> neck_indices;
size_t n_part = x.size();
for (size_t i = 0; i < n_part - 1; i ++) {
for (size_t j = i + 1; j < n_part; j ++) {
if (!enabled_contacts[i * n_part + j]) neck_indices.emplace_back(i, j);
}
}
ofs << "# vtk DataFile Version 4.0" << endl;
ofs << "Generated by libFractalCommon" << endl;
ofs << "ASCII" << endl;
ofs << "DATASET POLYDATA" << endl;
ofs << "POINTS " << neck_indices.size() << " FLOAT" << endl;
// Coordinates are normalized to avoid issues with ParaView and small particles
for (auto [i, j] : neck_indices) {
auto const & particleA = x[i];
auto const & particleB = x[j];
Eigen::Vector3d pos = (particleA + particleB) / 2.0;
ofs << pos[0] /r_part << " " << pos[1] / r_part << " " << pos[2] / r_part << " ";
}
ofs << endl << endl;
ofs << "POINT_DATA " << neck_indices.size() << endl;
ofs << "FIELD FieldData 1" << endl;
ofs << "normals 3 " << neck_indices.size() << " double" << endl;
for (auto [i, j] : neck_indices) {
auto const & particleA = x[i];
auto const & particleB = x[j];
Eigen::Vector3d normal = (particleA - particleB).normalized();
ofs << normal[0] << " " << normal[1] << " " << normal[2] << " ";
}
}