This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontact_target_mesh.cpp
686 lines (644 loc) · 21.5 KB
/
contact_target_mesh.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* contact_target_mesh.cpp
* sensitive couture
*
* Created by Nobuyuki Umetani on 7/26/10.
* Copyright 2010 The University of Tokyo and Columbia University. All rights reserved.
*
*/
#include <fstream>
#include <iostream>
#include <assert.h>
#include <math.h>
#include <vector>
#if defined(__APPLE__) && defined(__MACH__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "delfem/vector3d.h"
#include "contact_target.h"
CContactTarget3D_Mesh::CContactTarget3D_Mesh(){
nnode_ = 0; pXYZs_ = 0;
ntri_ = 0; aTri_ = 0;
pBoxel_ = 0;
is_hole = false;
}
CContactTarget3D_Mesh::~CContactTarget3D_Mesh(){
if( pXYZs_ != 0 ){ delete pXYZs_; }
if( aTri_ != 0 ){ delete aTri_; }
if( pBoxel_ != 0 ){ delete pBoxel_; }
}
void CContactTarget3D_Mesh::GetCenterWidth(double& cx, double& cy, double& cz,
double& wx, double& wy, double& wz)
{
double x_min = pXYZs_[0], x_max = pXYZs_[0];
double y_min = pXYZs_[1], y_max = pXYZs_[1];
double z_min = pXYZs_[2], z_max = pXYZs_[2];
for(unsigned int ino=1;ino<nnode_;ino++){
x_min = ( x_min < pXYZs_[ino*3+0] ) ? x_min : pXYZs_[ino*3+0];
x_max = ( x_max > pXYZs_[ino*3+0] ) ? x_max : pXYZs_[ino*3+0];
y_min = ( y_min < pXYZs_[ino*3+1] ) ? y_min : pXYZs_[ino*3+1];
y_max = ( y_max > pXYZs_[ino*3+1] ) ? y_max : pXYZs_[ino*3+1];
z_min = ( z_min < pXYZs_[ino*3+2] ) ? z_min : pXYZs_[ino*3+2];
z_max = ( z_max > pXYZs_[ino*3+2] ) ? z_max : pXYZs_[ino*3+2];
}
cx = (x_min+x_max)*0.5;
cy = (y_min+y_max)*0.5;
cz = (z_min+z_max)*0.5;
wx = x_max-x_min;
wy = y_max-y_min;
wz = z_max-z_min;
}
/*
void CContactTarget3D_Mesh::Load_Off(const std::string& fname)
{
std::ifstream fin;
fin.open(fname.c_str());
if( fin.fail() ){
std::cout << "Fail Read Fail" << std::endl;
return;
}
std::string str;
fin >> str;
fin >> nnode_ >> ntri_ >> str;
std::cout << "Load Off Nnode: ntri :" << nnode_ << " " << ntri_ << std::endl;
pXYZs_ = new double [nnode_*3];
aTri_ = new unsigned int [ntri_*3];
for(unsigned int ino=0;ino<nnode_;ino++){
double x,y,z;
fin >> x >> y >> z;
// std::cout << ino << " " << x << " " << y << " " << z << std::endl;
pXYZs_[ino*3+0] = x;
pXYZs_[ino*3+1] = y;
pXYZs_[ino*3+2] = z;
}
for(unsigned int itri=0;itri<ntri_;itri++){
int itmp, i1, i2, i3;
fin >> itmp >> i1 >> i2 >> i3;
aTri_[itri*3+0] = i1;
aTri_[itri*3+1] = i2;
aTri_[itri*3+2] = i3;
// std::cout << itri << " " << itmp << " " << i1 << " " << i2 << " " << i3 << std::endl;
}
}
void CContactTarget3D_Mesh::Load_Gmv(const std::string& fname)
{
std::cout << "File load " << fname << std::endl;
std::ifstream fin;
fin.open(fname.c_str());
if( fin.fail() ){
std::cout << "Fail Read Fail" << std::endl;
return;
}
std::string str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> str;
fin >> nnode_;
std::cout << "Nnode " << nnode_ << std::endl;
pXYZs_ = new double [nnode_*3];
for(unsigned int ino=0;ino<nnode_;ino++){
double x;
fin >> x;
pXYZs_[ino*3+0] = x;
}
for(unsigned int ino=0;ino<nnode_;ino++){
double y;
fin >> y;
pXYZs_[ino*3+1] = y;
}
for(unsigned int ino=0;ino<nnode_;ino++){
double z;
fin >> z;
pXYZs_[ino*3+2] = z;
}
fin >> str;
fin >> ntri_;
std::cout << "Ntri " << ntri_ << std::endl;
aTri_ = new unsigned int [ntri_*3];
for(unsigned int itri=0;itri<ntri_;itri++){
int itmp, i1, i2, i3;
fin >> str >> itmp >> i1 >> i2 >> i3;
aTri_[itri*3+0] = i1-1;
aTri_[itri*3+1] = i2-1;
aTri_[itri*3+2] = i3-1;
// std::cout << itri << " " << itmp << " " << i1 << " " << i2 << " " << i3 << std::endl;
}
}
void CContactTarget3D_Mesh::Load_Ply(const std::string& fname)
{
std::cout << "File load " << fname << std::endl;
std::ifstream fin;
fin.open(fname.c_str());
if( fin.fail() ){
std::cout << "Fail Read Fail" << std::endl;
return;
}
const unsigned int nbuff = 256;
char buff[nbuff], buff1[nbuff], buff2[nbuff];
std::string str1,str2;
fin.getline(buff,nbuff); // ply
fin.getline(buff,nbuff); // format asi 1.0
for(;;){
fin.getline(buff,nbuff);
if( strncmp(buff, "comment ", 8) != 0 ){ break; }
}
/////
sscanf(buff,"%s %s %d",buff1,buff2,&nnode_);
std::cout << "Nnode " << nnode_ << std::endl;
////
for(;;){
fin.getline(buff,nbuff);
if( strncmp(buff, "property ", 9) != 0 ){ break; }
}
sscanf(buff,"%s %s %d",buff1,buff2,&ntri_);
std::cout << "NTri " << ntri_ << std::endl;
/////
fin.getline(buff,nbuff); // property list int int vertex_indices
fin.getline(buff,nbuff); // end header
////
pXYZs_ = new double [nnode_*3];
for(unsigned int ino=0;ino<nnode_;ino++){
double x,y,z;
fin >> x >> y >> z;
// std::cout << ino << " " << x << " " << y << " " << z << std::endl;
pXYZs_[ino*3+0] = x;
pXYZs_[ino*3+1] = y;
pXYZs_[ino*3+2] = z;
}
aTri_ = new unsigned int [ntri_*3];
for(unsigned int itri=0;itri<ntri_;itri++){
int itmp, i1, i2, i3;
fin >> itmp >> i1 >> i2 >> i3;
aTri_[itri*3+0] = i1;
aTri_[itri*3+1] = i2;
aTri_[itri*3+2] = i3;
// std::cout << itri << " " << itmp << " " << i1 << " " << i2 << " " << i3 << std::endl;
}
}
*/
void CContactTarget3D_Mesh::SetMesh(const std::vector<unsigned int>& aTri, const std::vector<double>& aXYZ)
{
ntri_ = aTri.size()/3;
aTri_ = new unsigned int [ntri_*3];
for(unsigned int i=0;i<ntri_*3;i++){ this->aTri_[i] = aTri[i]; }
nnode_ = aXYZ.size()/3;
pXYZs_ = new double [nnode_*3];
for(unsigned int i=0;i<nnode_*3;i++){ this->pXYZs_[i] = aXYZ[i]; }
}
void CContactTarget3D_Mesh::Draw() const
{
::glColor3d(1,0,0);
::glBegin(GL_LINES);
for(unsigned int itri=0;itri<ntri_;itri++){
unsigned int i1 = aTri_[itri*3+0];
unsigned int i2 = aTri_[itri*3+1];
unsigned int i3 = aTri_[itri*3+2];
::glVertex3dv(pXYZs_+i1*3);
::glVertex3dv(pXYZs_+i2*3);
::glVertex3dv(pXYZs_+i2*3);
::glVertex3dv(pXYZs_+i3*3);
::glVertex3dv(pXYZs_+i3*3);
::glVertex3dv(pXYZs_+i1*3);
}
::glEnd();
}
// return penetration depth (inside is positive)
double CContactTarget3D_Mesh::Projection
(double px, double py, double pz,
double n[3]) const // normal outward
{
unsigned int inout;
double dist;
if( pBoxel_ != 0 ){
inout = this->FindInOut_Boxel(px,py,pz);
dist = this->Distance_Mesh_Boxel(px,py,pz, n);
}
else {
inout = this->FindInOut(px,py,pz);
dist = this->Distance_Mesh(px,py,pz, n);
}
if( inout == 0 ){ return dist; }
else if( inout == 1 ){ return -dist; }
return -dist; // if not sure assume out
}
void CContactTarget3D_Mesh::GetMesh
(std::vector<unsigned int>& aTri, std::vector<double>& aXYZ, double elen) const
{
aTri.resize(ntri_*3);
for(unsigned int i=0;i<ntri_*3;i++){ aTri[i] = this->aTri_[i]; }
aXYZ.resize(nnode_*3);
for(unsigned int i=0;i<nnode_*3;i++){ aXYZ[i] = this->pXYZs_[i]; }
}
// 0:in 1:out 2:not sure
double CContactTarget3D_Mesh::Distance_Mesh
(double px, double py, double pz,
double n[3]) const
{
double p0[3] = {px,py,pz};
n[0] = pXYZs_[0]-px;
n[1] = pXYZs_[1]-py;
n[2] = pXYZs_[2]-pz;
double dist = Com::Length3D(n);
for(unsigned int ino=1;ino<nnode_;ino++){
const double d0 = Com::Distance3D(p0, pXYZs_+ino*3);
if( d0 >= dist ){ continue; }
dist = d0;
n[0] = pXYZs_[ino*3+0]-px;
n[1] = pXYZs_[ino*3+1]-py;
n[2] = pXYZs_[ino*3+2]-pz;
}
for(unsigned int itri=0;itri<ntri_;itri++){
const unsigned int i1 = aTri_[itri*3+0];
const unsigned int i2 = aTri_[itri*3+1];
const unsigned int i3 = aTri_[itri*3+2];
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
double normal[3];
Com::NormalTri3D(normal,pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
{
const double invlen = 1.0/Com::Length3D(normal);
normal[0] *= invlen;
normal[1] *= invlen;
normal[2] *= invlen;
}
const double p1[3] = { px+normal[0], py+normal[1], pz+normal[2] };
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
if( fabs(v1+v2+v3) < 1.0e-10 ){ continue; } // p0 and p1 is on the triangle
double inv_v4 = 1.0/(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-4;
if( r1 > -tol && r2 > -tol && r3 > -tol )
{
const double dir2[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3 - px,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3 - py,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 - pz};
// const double dotdir = Dot3D(normal,dir2);
const double d1 = Com::Length3D(dir2);
if( d1 >= dist ) continue;
dist = d1;
n[0] = dir2[0];
n[1] = dir2[1];
n[2] = dir2[2];
}
}
{
const double invlen = 1.0/Com::Length3D(n);
n[0] *= invlen;
n[1] *= invlen;
n[2] *= invlen;
}
return dist;
}
// 0:in 1:out 2:not sure
double CContactTarget3D_Mesh::Distance_Mesh_Boxel
(double px, double py, double pz,
double n[3]) const
{
assert( this->pBoxel_ != 0 );
double p0[3] = {px,py,pz};
double dist = pBoxel_->GetWidth()*3;
pBoxel_->Find_NearestTriCand(p0,aIndTriCand);
// std::cout << aIndTriCand.size() << std::endl;
for(unsigned int iitri=0;iitri<aIndTriCand.size();iitri++){
unsigned int itri = aIndTriCand[iitri];
assert( itri < ntri_ );
const unsigned int i1 = aTri_[itri*3+0];
const unsigned int i2 = aTri_[itri*3+1];
const unsigned int i3 = aTri_[itri*3+2];
{
const double d1 = Com::Distance3D(p0, pXYZs_+i1*3);
if( d1 < dist ){
dist = d1;
n[0] = pXYZs_[i1*3+0]-px;
n[1] = pXYZs_[i1*3+1]-py;
n[2] = pXYZs_[i1*3+2]-pz;
}
const double d2 = Com::Distance3D(p0, pXYZs_+i2*3);
if( d2 < dist ){
dist = d2;
n[0] = pXYZs_[i2*3+0]-px;
n[1] = pXYZs_[i2*3+1]-py;
n[2] = pXYZs_[i2*3+2]-pz;
}
const double d3 = Com::Distance3D(p0, pXYZs_+i3*3);
if( d3 < dist ){
dist = d3;
n[0] = pXYZs_[i3*3+0]-px;
n[1] = pXYZs_[i3*3+1]-py;
n[2] = pXYZs_[i3*3+2]-pz;
}
}
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
double normal[3];
Com::NormalTri3D(normal,pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
{
const double invlen = 1.0/Com::Length3D(normal);
normal[0] *= invlen;
normal[1] *= invlen;
normal[2] *= invlen;
}
const double p1[3] = { px+normal[0], py+normal[1], pz+normal[2] };
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
if( fabs(v1+v2+v3) < 1.0e-10 ){ continue; } // p0 and p1 is on the triangle
double inv_v4 = 1.0/(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-4;
if( r1 > -tol && r2 > -tol && r3 > -tol )
{
const double dir2[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3 - px,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3 - py,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 - pz};
// const double dotdir = Dot3D(normal,dir2);
const double d1 = Com::Length3D(dir2);
if( d1 >= dist ) continue;
dist = d1;
n[0] = dir2[0];
n[1] = dir2[1];
n[2] = dir2[2];
}
}
{
const double invlen = 1.0/Com::Length3D(n);
n[0] *= invlen;
n[1] *= invlen;
n[2] *= invlen;
}
return dist;
}
// 0:in 1:out 2:not sure
unsigned int CContactTarget3D_Mesh::FindInOut_IntersectionRay
(double px, double py, double pz,
const double dir[3]) const
{
double p0[3] = {px,py,pz};
double p1[3] = {px+dir[0],py+dir[1],pz+dir[2]};
unsigned int icnt = 0;
for(unsigned int itri=0;itri<ntri_;itri++){
unsigned int i1 = aTri_[itri*3+0];
unsigned int i2 = aTri_[itri*3+1];
unsigned int i3 = aTri_[itri*3+2];
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
if( fabs(v1+v2+v3) < 1.0e-10 ) return 2; // p0 and p1 is on the triangle
double inv_v4 = 1.0/fabs(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-2;
if( r1 < -tol || r2 < -tol || r3 < -tol ) continue; // need tol ( compare with fabs(v1+v2+v3)? )
if( r1 < tol || r2 < tol || r3 < tol ) return 2; // on the edge
double dir2[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3 - px,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3 - py,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 - pz};
double dotdir = Com::Dot3D(dir,dir2);
if( dotdir > 0 ) icnt++;
}
if( icnt % 2 == 0 ) return 1;
return 0;
}
// 0:in 1:out 2:not sure
unsigned int CContactTarget3D_Mesh::FindInOut_IntersectionRay_Boxel
(double px, double py, double pz,
const double dir[3]) const
{
assert( pBoxel_ != 0 );
double p0[3] = {px,py,pz};
pBoxel_->Find_IntersecTriCand(p0,dir,aIndTriCand);
// std::cout << aIndTriCand_.size() << std::endl;
double p1[3] = {px+dir[0],py+dir[1],pz+dir[2]};
unsigned int icnt = 0;
for(unsigned int iitri=0;iitri<aIndTriCand.size();iitri++){
unsigned int itri = aIndTriCand[iitri];
if( aFlgTriUsed[itri] == 1 ) continue;
aFlgTriUsed[itri] = 1;
unsigned int i1 = aTri_[itri*3+0];
unsigned int i2 = aTri_[itri*3+1];
unsigned int i3 = aTri_[itri*3+2];
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
if( fabs(v1+v2+v3) < 1.0e-10 ) goto AMBIGUOUS; // p0 and p1 is on the triangle
double inv_v4 = 1.0/fabs(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-2;
if( r1 < -tol || r2 < -tol || r3 < -tol ) continue; // need tol ( compare with fabs(v1+v2+v3)? )
if( r1 < tol || r2 < tol || r3 < tol ) goto AMBIGUOUS; // on the edge
double dir2[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3 - px,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3 - py,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 - pz};
double dotdir = Com::Dot3D(dir,dir2);
if( dotdir > 0 ) icnt++;
}
for(unsigned int iitri=0;iitri<aIndTriCand.size();iitri++){
unsigned int itri = aIndTriCand[iitri];
aFlgTriUsed[itri] = 0;
}
// std::cout << "Cunt" << icnt << std::endl;
if( icnt % 2 == 0 ) return 1;
return 0;
AMBIGUOUS:
for(unsigned int iitri=0;iitri<aIndTriCand.size();iitri++){
unsigned int itri = aIndTriCand[iitri];
aFlgTriUsed[itri] = 0;
}
return 2;
}
void CContactTarget3D_Mesh::BuildBoxel()
{
// double c[3] = {0.0542,-0.04374532,0.06234};
double c[3] = {0,0,0};
double w[3] = {0,0,0};
this->GetCenterWidth(c[0],c[1],c[2], w[0],w[1],w[2]);
double width = w[0];
width = ( w[1] > width ) ? w[1] : width;
width = ( w[2] > width ) ? w[2] : width;
if( pBoxel_ != 0 ){ delete pBoxel_; }
pBoxel_ = new CSpatialHash_Grid3D(32,c,width*0.5*1.13454325);
for(unsigned int itri=0;itri<ntri_;itri++){
unsigned int i0 = aTri_[itri*3+0];
unsigned int i1 = aTri_[itri*3+1];
unsigned int i2 = aTri_[itri*3+2];
pBoxel_->AddTri(itri, pXYZs_+i0*3, pXYZs_+i1*3, pXYZs_+i2*3);
}
if( !is_hole ){ pBoxel_->BuildOutFlg(); }
aFlgTriUsed.clear();
aFlgTriUsed.resize(ntri_,0);
aIndTriCand.reserve(2048);
}
unsigned int CContactTarget3D_Mesh::FindInOut(double px, double py, double pz) const
{
unsigned int icnt_in = 0;
unsigned int icnt_out = 0;
for(unsigned int i=0;i<10;i++){
const double theta = i*6.28/10+0.1431432154;
// const double dir[3] = { sin(theta)*cos(theta*2), sin(theta)*sin(theta*2), cos(theta) };
const double dir[3] = { 1, 0, 0 };
unsigned int ires = FindInOut_IntersectionRay(px,py,pz, dir);
if( ires != 2 && !is_hole ){ return ires; }
if( ires == 0 ) icnt_in++;
if( ires == 1 ) icnt_out++;
}
if( icnt_in > 5 ) return 0;
if( icnt_out > 5 ) return 1;
/* for(unsigned int i=1;i<20;i++){
const double theta = i*6.28*543260;
const double dir[3] = { sin(theta)*cos(theta*3), cos(theta), sin(theta)*sin(theta*3) };
unsigned int ires = FindInOut_IntersectionRay(px,py,pz, dir);
if( ires == 0 ) icnt_in++;
if( ires == 1 ) icnt_out++;
if( icnt_in - icnt_out > 5 ) return 0;
if( icnt_out - icnt_in > 5 ) return 1;
}*/
return 2;
}
unsigned int CContactTarget3D_Mesh::FindInOut_Boxel
(double px, double py, double pz) const
{
assert( pBoxel_ != 0 );
double p[3] = { px, py, pz };
if( pBoxel_->IsOut(p) && !is_hole ){ return 1; }
unsigned int icnt_in = 0;
unsigned int icnt_out = 0;
for(unsigned int i=0;i<10;i++){
const double theta = i*6.28/10+0.15432452356673;
const double dir[3] = { sin(theta)*cos(theta*2), sin(theta)*sin(theta*2), cos(theta) };
// const double dir[3] = { -0.35, 0.1342, 0.3 };
unsigned int ires = FindInOut_IntersectionRay_Boxel(px,py,pz, dir);
// unsigned int ires1 = FindInOut_IntersectionRay(px,py,pz, dir);
/* if( ires != ires1 ){
std::cout << "hoge " << px << " " << py << " " << pz << std::endl;
}*/
if( ires != 2 && !is_hole ){ return ires; }
if( ires == 0 ) icnt_in++;
if( ires == 1 ) icnt_out++;
}
if( icnt_in > 5 ) return 0;
if( icnt_out > 5 ) return 1;
return 2;
}
void CContactTarget3D_Mesh::Translate(double x, double y, double z)
{
for(unsigned int ino=0;ino<nnode_;ino++){
pXYZs_[ino*3+0] += x;
pXYZs_[ino*3+1] += y;
pXYZs_[ino*3+2] += z;
}
}
bool CContactTarget3D_Mesh::IntersectionPoint
(double p[3],
const double org[3], const double dir[3]) const
{
if( pBoxel_ != 0 ){
std::vector<unsigned int> aIndTriCand;
pBoxel_->Find_IntersecTriCand(org,dir, aIndTriCand);
if( aIndTriCand.empty() ) return false;
bool iflg = false;
double min_dist;
const double p0[3] = {org[0],org[1],org[2]};
const double p1[3] = {org[0]+dir[0],org[1]+dir[1],org[2]+dir[2]};
for(unsigned int iitri=0;iitri<aIndTriCand.size();iitri++){
unsigned int itri = aIndTriCand[iitri];
unsigned int i1 = aTri_[itri*3+0];
unsigned int i2 = aTri_[itri*3+1];
unsigned int i3 = aTri_[itri*3+2];
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
double inv_v4 = 1.0/fabs(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-2;
if( r1 < -tol || r2 < -tol || r3 < -tol ) continue; // need tol ( compare with fabs(v1+v2+v3)? )
double end[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 };
double dir2[3] = { end[0]-org[0], end[1]-org[1], end[2]-org[2] };
double dotdir = Com::Dot3D(dir,dir2);
if( dotdir < 0 ) continue;
if( !iflg ){
min_dist = dotdir;
p[0]=end[0]; p[1]=end[1]; p[2]=end[2];
}
else if( dotdir < min_dist ){
min_dist = dotdir;
p[0]=end[0]; p[1]=end[1]; p[2]=end[2];
}
iflg = true;
}
if( iflg ) return true;
}
//////////////////////////////////////
else {
bool iflg = false;
double min_dist;
const double p0[3] = {org[0],org[1],org[2]};
const double p1[3] = {org[0]+dir[0],org[1]+dir[1],org[2]+dir[2]};
for(unsigned int itri=0;itri<ntri_;itri++){
unsigned int i1 = aTri_[itri*3+0];
unsigned int i2 = aTri_[itri*3+1];
unsigned int i3 = aTri_[itri*3+2];
const double v0 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, pXYZs_+i3*3);
const double sign = ( v0 > 0 ) ? 1 : -1;
const double v1 = Com::TetVolume3D(p0, pXYZs_+i2*3, pXYZs_+i3*3, p1)*sign;
const double v2 = Com::TetVolume3D(p0, pXYZs_+i3*3, pXYZs_+i1*3, p1)*sign;
const double v3 = Com::TetVolume3D(p0, pXYZs_+i1*3, pXYZs_+i2*3, p1)*sign;
double inv_v4 = 1.0/fabs(v1+v2+v3);
const double r1 = v1*inv_v4;
const double r2 = v2*inv_v4;
const double r3 = v3*inv_v4;
const double tol = 1.0e-2;
if( r1 < -tol || r2 < -tol || r3 < -tol ) continue; // need tol ( compare with fabs(v1+v2+v3)? )
double end[3] = {
pXYZs_[i1*3+0]*r1 + pXYZs_[i2*3+0]*r2 + pXYZs_[i3*3+0]*r3,
pXYZs_[i1*3+1]*r1 + pXYZs_[i2*3+1]*r2 + pXYZs_[i3*3+1]*r3,
pXYZs_[i1*3+2]*r1 + pXYZs_[i2*3+2]*r2 + pXYZs_[i3*3+2]*r3 };
double dir2[3] = { end[0]-org[0], end[1]-org[1], end[2]-org[2] };
double dotdir = Com::Dot3D(dir,dir2);
if( dotdir < 0 ) continue;
if( !iflg ){
min_dist = dotdir;
p[0]=end[0]; p[1]=end[1]; p[2]=end[2];
}
else if( dotdir < min_dist ){
min_dist = dotdir;
p[0]=end[0]; p[1]=end[1]; p[2]=end[2];
}
iflg = true;
}
if( iflg ) return true;
}
return false;
}
////////////////