-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLagrangeTensorWorker.cpp
288 lines (265 loc) · 11.8 KB
/
LagrangeTensorWorker.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Software License Agreement (BSD License)
*
* Xin Wang
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author : Xin Wang
* Email : ericrussell@zju.edu.cn
*
*/
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <math.h>
#include <Eigen/Core>
#include "LagrangeTensorWorker.h"
#include "globaldef.h"
#include "dataLibrary.h"
using namespace std;
bool LagrangeTensorWorker::is_para_satisfying(QString &message){
if(dataLibrary::Fracture_Triangles.size() > 0){
this->setParaSize(7);
if(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters.size()>=this->getParaSize()){
this->setStriationsFileName(QString::fromUtf8(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[0].c_str()));
this->setStepsFileName(QString::fromUtf8(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[1].c_str()));
this->setSizeFileName(QString::fromUtf8(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[2].c_str()));
this->setTensorFileName(QString::fromUtf8(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[3].c_str()));
string size_th_string = dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[4];
transform(size_th_string.begin(), size_th_string.end(), size_th_string.begin(), ::tolower);
if(size_th_string == "allsize"){
this->setSizeThMode(false);
this->setSizeTh(0.0);
}
else{
double SizeTh;
stringstream ss_SizeTh(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[4]);
ss_SizeTh >> SizeTh;
this->setSizeThMode(true);
this->setSizeTh(SizeTh);
}
double AngleTh;
stringstream ss_AngleTh(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[5]);
ss_AngleTh >> AngleTh;
this->setAngleTh(AngleTh);
string K_string = dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[6];
transform(K_string.begin(), K_string.end(), K_string.begin(), ::tolower);
if(K_string == "realk"){
this->setKfixedMode(false);
}
else{
double K;
stringstream ss_K(dataLibrary::Workflow[dataLibrary::current_workline_index].parameters[6]);
ss_K >> K;
this->setKfixedMode(true);
this->setK(K);
}
this->setStriationsMTh(0.05);
this->setStepsMTh(0.0);
this->setParaIndex(this->getParaSize());
return true;
}
else{
message = QString("lagrangetensor: No (or not enough) Parameter Given.");
return false;
}
}
else{
message = QString("lagrangetensor: Please Perform Fracture Triangulation or Open Triangulation Data First.");
return false;
}
}
void LagrangeTensorWorker::prepare(){
this->setUnmute();
this->setWriteLog();
this->check_mute_nolog();
}
bool readStriationsOrSteps(const string &filename, vector<Eigen::Vector3f> &dir, vector<float> &M, string err_message){
ifstream fs;
fs.open (filename.c_str());
if (!fs.is_open() || fs.fail()){
err_message = "Could not open file " + filename + " !";
fs.close();
return false;
}
string line;
vector<string> st;
while (!fs.eof()){
getline(fs, line);
// Ignore empty lines
if (line == "")
continue;
// Tokenize the line
boost::trim(line);
boost::split(st, line, boost::is_any_of(",\t\r "), boost::token_compress_on);
if (st.size() < 5)
continue;
Eigen::Vector3f temp_dir;
temp_dir<<stof(st[0]),stof(st[1]),stof(st[2]);
dir.push_back(temp_dir);
M.push_back(stof(st[4]));
}
fs.close();
return true;
}
bool readSize(const string &filename, vector<int> &f_size, string err_message){
ifstream fs;
fs.open (filename.c_str());
if (!fs.is_open() || fs.fail()){
err_message = "Could not open file " + filename + " !";
fs.close();
return false;
}
string line;
while (!fs.eof()){
getline(fs, line);
// Ignore empty lines
if (line == "")
continue;
boost::trim(line);
f_size.push_back(stoi(line));
}
fs.close();
return true;
}
void LagrangeTensorWorker::doWork(){
bool is_success(false);
QByteArray baStriations = this->getStriationsFileName().toLocal8Bit();
string* strStriationsfilename = new string(baStriations.data());
QByteArray baSteps = this->getStepsFileName().toLocal8Bit();
string* strStepsfilename = new string(baSteps.data());
QByteArray baSize = this->getSizeFileName().toLocal8Bit();
string* strSizefilename = new string(baSize.data());
QByteArray baTensor = this->getTensorFileName().toLocal8Bit();
string* strTensorfilename = new string(baTensor.data());
dataLibrary::Status = STATUS_LAGRANGETENSOR;
this->timer_start();
//begin of processing
vector<Eigen::Vector3f> striations_dir;
vector<float> striations_M;
vector<Eigen::Vector3f> steps_dir;
vector<float> steps_M;
vector<int> f_size;
string error_msg = "";
if(!readStriationsOrSteps(*strStriationsfilename, striations_dir, striations_M, error_msg)){
emit showErrors(QString(error_msg.c_str()));
}
else{
if(!readStriationsOrSteps(*strStepsfilename, steps_dir, steps_M, error_msg)){
emit showErrors(QString(error_msg.c_str()));
}
else{
if(!readSize(*strSizefilename, f_size, error_msg)){
emit showErrors(QString(error_msg.c_str()));
}
else{
ofstream fout(strTensorfilename->c_str());
for(int i=0; i<dataLibrary::Fracture_Triangles.size(); i++){
if((this->getSizeThMode() == false)||((this->getSizeThMode() == true)&&(f_size[i] >= this->getSizeTh()))){
if((striations_dir[i].norm() != 0)&&(steps_dir[i].norm() != 0)){
if((striations_M[i] >= this->getStriationsMTh())&&(steps_M[i] >= this->getStepsMTh())){
Eigen::Vector3f shear_dir;
double angle = acos(striations_dir[i].dot(steps_dir[i])/(striations_dir[i].norm()*steps_dir[i].norm()));
double angle_th = this->getAngleTh()*TWOPI/360.0;
if((angle<=angle_th)||((TWOPI/2-angle)<=angle_th)){
if(angle < TWOPI/4){
shear_dir = striations_dir[i];
}
else if(angle > TWOPI/4){
shear_dir = -striations_dir[i];
}
else{
continue;
}
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromROSMsg(dataLibrary::Fracture_Triangles[i]->cloud, *cloud_ptr);
Eigen::Vector4f plane_normal_param = dataLibrary::fitPlaneManually(*cloud_ptr);
Eigen::Vector3f fracture_normal;
fracture_normal << plane_normal_param(0), plane_normal_param(1), plane_normal_param(2);
Eigen::Vector3f e1 = shear_dir/shear_dir.norm();
Eigen::Vector3f e2 = fracture_normal/fracture_normal.norm();
Eigen::Vector3f e3 = e1.cross(e2);
Eigen::Vector3f e_prime1, e_prime2, e_prime3;
e_prime1<<1,0,0;
e_prime2<<0,1,0;
e_prime3<<0,0,1;
double K;
double M2Kpara = 0.2;
if(this->getKfixedMode()){
K = this->getK();
}
else{
K = (striations_M[i] + steps_M[i])*M2Kpara;
}
Eigen::Matrix3f E_star;
E_star << 0, K/2, 0,
K/2, K*K/2, 0,
0, 0, 0;
Eigen::Matrix3f T;
T << e1.dot(e_prime1), e1.dot(e_prime2), e1.dot(e_prime3),
e2.dot(e_prime1), e2.dot(e_prime2), e2.dot(e_prime3),
e3.dot(e_prime1), e3.dot(e_prime2), e3.dot(e_prime3);
Eigen::Matrix3f E_star_prime = T.transpose()*E_star*T;
fout<<i<<'\t'
<<E_star_prime(0,0)<<'\t'<<E_star_prime(0,1)<<'\t'<<E_star_prime(0,2)<<'\t'
<<E_star_prime(1,0)<<'\t'<<E_star_prime(1,1)<<'\t'<<E_star_prime(1,2)<<'\t'
<<E_star_prime(2,0)<<'\t'<<E_star_prime(2,1)<<'\t'<<E_star_prime(2,2)<<'\n';
}
}
}
}
}
fout.close();
is_success = true;
}
}
}
//end of processing
this->timer_stop();
if(this->getWriteLogMode()&&is_success){
string log_text = "\tLagrange Tensor costs: ";
ostringstream strs;
strs << this->getTimer_sec();
log_text += (strs.str() +" seconds.");
dataLibrary::write_text_to_log_file(log_text);
}
dataLibrary::Status = STATUS_READY;
emit showReadyStatus();
delete strStriationsfilename;
delete strStepsfilename;
delete strSizefilename;
delete strTensorfilename;
if(this->getWorkFlowMode()&&is_success){
this->Sleep(1000);
emit GoWorkFlow();
}
}