-
Notifications
You must be signed in to change notification settings - Fork 54
/
PolygonalCohesion.cpp
2519 lines (2048 loc) · 92.9 KB
/
PolygonalCohesion.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of the libWetHair open source project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright 2017 Yun (Raymond) Fei, Henrique Teles Maia, Christopher Batty,
// Changxi Zheng, and Eitan Grinspun
//
#include "PolygonalCohesion.h"
#include "TwoDScene.h"
#include "MathUtilities.h"
#include "HairFlow.h"
#include "CohesionTableGen.h"
#include "fluidsim2D.h"
#include "fluidsim3D.h"
#include "ThreadUtils.h"
#include "CTCD.h"
#include <stack>
#include <deque>
#include <set>
#include <numeric>
#include <algorithm>
//#define DEBUG_COHESION_TABLE
//#define DEBUG_PLANAR_COHESION_TABLE
template<int DIM>
PolygonalCohesion<DIM>::PolygonalCohesion(TwoDScene<DIM>* scene) :
m_parent(scene), m_sorter(NULL), m_use_decoupled_force(false), m_compute_particle_poe_mapping(true), m_min_cohesion_table(NULL), m_max_cohesion_table(NULL)
{
m_sorter = new Sorter(0, 0, 0);
scalar gnorm = scene->getSimpleGravity().norm();
gnorm = (gnorm == 0.0) ? 1000.0 : gnorm;
const scalar max_eta = sqrt(scene->getLiquidTension() / (scene->getLiquidDensity() * gnorm)) * 0.5;
const scalar sigma = scene->getLiquidTension();
const scalar theta = scene->getLiquidTheta();
const int disc = 256;
if(m_parent->getNumParticles() > 0) {
const WetHairParameter& parameter = scene->getParameter();
const scalar max_rad = scene->getRadii().maxCoeff();
const scalar min_rad = scene->getRadii().minCoeff();
m_min_cohesion_table = new CohesionTable( parameter.radius_multiplier, parameter.collision_stiffness, parameter.radius_multiplier_planar, parameter.collision_stiffness_planar );
m_max_cohesion_table = new CohesionTable( parameter.radius_multiplier, parameter.collision_stiffness, parameter.radius_multiplier_planar, parameter.collision_stiffness_planar );
m_min_cohesion_table->setParameter(sigma, theta, min_rad, max_eta * 2.0, disc);
m_max_cohesion_table->setParameter(sigma, theta, max_rad, max_eta * 2.0, disc);
std::cout << "[construct adhesive/repulsive table]" << std::endl;
m_min_cohesion_table->construct_alpha_table();
m_max_cohesion_table->construct_alpha_table();
m_min_cohesion_table->construct_planar_alpha_table();
m_max_cohesion_table->construct_planar_alpha_table();
#ifdef DEBUG_COHESION_TABLE
auto debug_cohesion_table = [&] (scalar rad, CohesionTable* cohesion_table) {
scalar max_vol = M_PI * ((max_eta + rad) * (max_eta + rad) - rad * rad);
const scalar max_dist = max_eta * 2.0;
std::cout << "[sweep adhesive/repulsive table]" << std::endl;
MatrixXs stiffness_table(disc, disc);
MatrixXs force_table(disc, disc);
MatrixXs dEdd_table(disc, disc);
const scalar d_star = cohesion_table->getDStar();
for(int i = 0; i < disc; ++i)
{
for(int j = 0; j < disc; ++j)
{
scalar d = max_dist / (scalar) disc * (scalar) i;
scalar A_L = max_vol / (scalar) disc * (scalar) j;
scalar stiffness = cohesion_table->getStiffness(d, A_L, 1.0);
scalar dEdd = cohesion_table->interpolate_dEdd(A_L, d);
dEdd_table(j, i) = dEdd;
stiffness_table(j, i) = stiffness;
force_table(j, i) = stiffness * (d - d_star);
}
}
std::cout << "[adhesive table]" << std::endl;
std::cout << dEdd_table << std::endl;
std::cout << "[stiffness table]" << std::endl;
std::cout << stiffness_table << std::endl;
std::cout << "[force table]" << std::endl;
std::cout << force_table << std::endl;
};
debug_cohesion_table(min_rad, m_min_cohesion_table);
debug_cohesion_table(max_rad, m_max_cohesion_table);
#endif
#ifdef DEBUG_PLANAR_COHESION_TABLE
auto debug_planar_cohesion_table = [&] (scalar rad, CohesionTable* cohesion_table) {
scalar max_vol = M_PI * ((max_eta + rad) * (max_eta + rad) - rad * rad);
const scalar max_dist = max_eta * 2.0;
std::cout << "[sweep planar adhesive/repulsive table]" << std::endl;
MatrixXs stiffness_table(disc, disc);
MatrixXs force_table(disc, disc);
MatrixXs dEdd_table(disc, disc);
const scalar d_star = cohesion_table->getDStarPlanar();
for(int i = 0; i < disc; ++i)
{
for(int j = 0; j < disc; ++j)
{
scalar d = max_dist / (scalar) disc * (scalar) i;
scalar A_L = max_vol / (scalar) disc * (scalar) j;
scalar stiffness = cohesion_table->getStiffnessPlanar(d, A_L, 1.0);
scalar dEdd = cohesion_table->interpolate_dEdd_planar(A_L, d);
dEdd_table(j, i) = dEdd;
stiffness_table(j, i) = stiffness;
force_table(j, i) = stiffness * (d - d_star);
}
}
std::cout << "[planar adhesive table]" << std::endl;
std::cout << dEdd_table << std::endl;
std::cout << "[planar stiffness table]" << std::endl;
std::cout << stiffness_table << std::endl;
std::cout << "[planar force table]" << std::endl;
std::cout << force_table << std::endl;
};
debug_planar_cohesion_table(min_rad, m_min_cohesion_table);
debug_planar_cohesion_table(max_rad, m_max_cohesion_table);
#endif
}
}
template<int DIM>
PolygonalCohesion<DIM>::~PolygonalCohesion()
{
if(m_sorter) delete m_sorter;
if(m_max_cohesion_table) delete m_max_cohesion_table;
if(m_min_cohesion_table) delete m_min_cohesion_table;
for(auto& adj : m_adjacency_categorized)
{
for(auto& hp : adj) {
if(hp.second) delete hp.second;
}
}
}
template<int DIM>
scalar PolygonalCohesion<DIM>::computeEdgeThickness(const scalar& vol_p, const int_Vectors_scalar<DIM>& ep)
{
const scalar d0 = ep.v.norm();
const scalar A = vol_p + M_PI * ep.eta * ep.eta;
const scalar alpha_0 = m_min_cohesion_table->interpolate_alpha(A, d0);
const scalar R_0 = m_min_cohesion_table->computeR(alpha_0, d0);
const scalar H_0 = m_min_cohesion_table->computeH(R_0, alpha_0);
const scalar alpha_1 = m_max_cohesion_table->interpolate_alpha(A, d0);
const scalar R_1 = m_max_cohesion_table->computeR(alpha_1, d0);
const scalar H_1 = m_max_cohesion_table->computeH(R_1, alpha_1);
return (H_0 + H_1) * 0.5;
}
template<int DIM>
void PolygonalCohesion<DIM>::findEdgeEdgeContact( const VectorXs x, const VectorXs v, const scalar& dt, const int& base_eidx )
{
if( DIM == 2 ) return;
const std::vector<int>& particle_to_hairs = m_parent->getParticleToHairs();
const std::vector<int>& particle_local_indices = m_parent->getParticleToHairLocalIndices();
const std::vector< std::pair<int, int> >& scene_edges = m_parent->getEdges();
const std::pair<int, int>& base_edge = scene_edges[ base_eidx ];
const VectorXs& radii = m_parent->getRadii();
const scalar& radius_0 = radii(base_edge.first);
const scalar& radius_1 = radii(base_edge.second);
auto& flows = m_parent->getFilmFlows();
const scalar& theta = m_parent->getLiquidTheta();
int pidx = base_edge.first;
int hidx = particle_to_hairs[pidx];
if(hidx < 0) return;
int local_idx_0 = particle_local_indices[base_edge.first];
int local_idx_1 = particle_local_indices[base_edge.second];
const VectorXs& eta = flows[hidx]->getEta();
const scalar peta_0 = eta(local_idx_0);
const scalar peta_1 = eta(local_idx_1);
const scalar H_0 = peta_0 + radius_0;
const scalar H_1 = peta_1 + radius_1;
const int pepair_neighbor_0 = std::max(1, (int) m_num_edge_connections[base_edge.first] );
const int pepair_neighbor_1 = std::max(1, (int) m_num_edge_connections[base_edge.second] );
const scalar vol_p_each = M_PI * ((H_0 * H_0 - radius_0 * radius_0) / (scalar) pepair_neighbor_0 + (H_1 * H_1 - radius_1 * radius_1) / (scalar) pepair_neighbor_1) * 0.5;
const scalar radii_p = sqrt((radius_0 * radius_0 + radius_1 * radius_1) * 0.5);
// Edge AB
// end positions of base edge
Vector3s xAe = x.segment<3>( m_parent->getDof( base_edge.first ) );
Vector3s xBe = x.segment<3>( m_parent->getDof( base_edge.second ) );
// start positions of edge
Vector3s xAs = xAe - v.segment<3>( m_parent->getDof( base_edge.first ) ) * dt;
Vector3s xBs = xBe - v.segment<3>( m_parent->getDof( base_edge.second) ) * dt;
// grab center of Edge
const Vector3s& pos = ( xAs + xBs ) * 0.5;
const scalar& cellsize = m_parent->getSearchRadius();
const Vectors<DIM>& bbx_min = m_parent->getBoundingBoxMin();
int pindex[3] = {0, 0, 0};
int range[3] = {0, 0, 0};
const int nindex[3] = {m_sorter->ni, m_sorter->nj, m_sorter->nk};
for(int r = 0; r < DIM; ++r)
{
pindex[r] = std::max(0, std::min(nindex[r] - 1, (int)((pos(r) - bbx_min(r)) / cellsize)));
range[r] = 1;
}
auto& edge_pairs = m_edge_connections[base_eidx];
for(auto it = std::begin(edge_pairs); it != std::end(edge_pairs); ++it)
{
it->second->updated = false;
}
const scalar H_each = sqrt(vol_p_each / M_PI + radii_p * radii_p);
// get closest edges to each hair
m_sorter->getNeigboringParticles_cell(pindex[0], pindex[1], pindex[2], -range[0], range[0], -range[1], range[1], -range[2], range[2], [&] (int eidx) {
// ignore myself
const std::pair<int, int>& e = scene_edges[eidx];
if( e == base_edge ) return;
// ignore edges on my strand
int nhidx = particle_to_hairs[e.first];
if(nhidx < 0 || nhidx == hidx) return;
//if( eidx < base_eidx ) return; // avoid duplicates
// Grab edge CD
// end of timestep positions
Vector3s xCe = x.segment<3>( m_parent->getDof( e.first ) );
Vector3s xDe = x.segment<3>( m_parent->getDof( e.second ) );
// start of timestep positions
Vector3s xCs = xCe - v.segment<3>( m_parent->getDof( e.first ) ) * dt;
Vector3s xDs = xDe - v.segment<3>( m_parent->getDof( e.second) ) * dt;
// averaging radii_q since we don't have an alpha yet...
const scalar radii_q = sqrt(mathutils::lerp(radii(e.first) * radii(e.first), radii(e.second) * radii(e.second), 0.5));
HairFlow<DIM>* neighbor_flow = flows[nhidx];
const VectorXs& neta = neighbor_flow->getEta();
int nlidx_0 = particle_local_indices[e.first];
int nlidx_1 = particle_local_indices[e.second];
const int npepair_neighbor_0 = std::max(1, (int) m_num_edge_connections[e.first] );
const int npepair_neighbor_1 = std::max(1, (int) m_num_edge_connections[e.second] );
const scalar npeta_0 = neta(nlidx_0);
const scalar npeta_1 = neta(nlidx_1);
const scalar nH_0 = npeta_0 + radii(e.first);
const scalar nH_1 = npeta_1 + radii(e.second);
const scalar vol_q_each = M_PI * ((nH_0 * nH_0 - radii(e.first) * radii(e.first)) / (scalar) npepair_neighbor_0 + (nH_1 * nH_1 - radii(e.second) * radii(e.second)) / (scalar) npepair_neighbor_1) * 0.5;
scalar nH_each = sqrt(vol_q_each / M_PI + radii_q * radii_q);
auto itr = edge_pairs.find( eidx );
EdgeEdgePairEEC* ref_pair = NULL;
bool previous_linked = false;
if( itr != edge_pairs.end() ){ // previously found
ref_pair = itr->second;
previous_linked = ref_pair->valid;
}
scalar max_dist;
if(previous_linked) {
max_dist = std::max(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radii_p + radii_q);
} else {
max_dist = std::min(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radii_p + radii_q);
}
double colTime = -1.0;
if( CTCD::checkEEContact( xAs, xBs, xCs, xDs, xAe, xBe, xCe, xDe, max_dist, colTime ) ){
if( !ref_pair ){
ref_pair = new EdgeEdgePairEEC();
edge_pairs[ eidx ] = ref_pair;
m_num_edge_connections[ base_edge.first ] += 1;
m_num_edge_connections[ base_edge.second ] += 1;
m_num_edge_connections[ e.first ] += 1;
m_num_edge_connections[ e.second ] += 1;
}
ref_pair->valid = true;
ref_pair->base_eidx = base_eidx;
ref_pair->neighbor_eidx = eidx;
// Time of collision
const Vector3s xAc = (1.0 - colTime) * xAs + colTime * xAe;
const Vector3s xBc = (1.0 - colTime) * xBs + colTime * xBe;
const Vector3s xCc = (1.0 - colTime) * xCs + colTime * xCe;
const Vector3s xDc = (1.0 - colTime) * xDs + colTime * xDe;
// points of contact on edges
Vector3s xAB, xCD;
scalar alpha, beta;
double dist_squared = CTCD::ClosestPtSegmentSegment( xAc, xBc, xCc, xDc, alpha, beta, xAB, xCD );
double d_contact = sqrt( dist_squared );
EdgeEdgePairEEC* eep = ref_pair;
eep->time = colTime;
eep->alpha_contact = alpha;
eep->neighbor_local_coord_contact = beta;
// compute quadruture region from contact points
// Edge d1 quad_region,
scalar AC = (xAc - xCc).norm();
scalar AD = (xAc - xDc).norm();
scalar max_alpha = m_parent->isTip( base_edge.first ) ? 0.9 : 1.0;
if( AC < AD ){
if( AC == d_contact ){
eep->alpha_0 = 0.0;
eep->neighbor_local_coord_0 = 0.0;
} else {
eep->alpha_0 = mathutils::clamp( alpha * ( ( max_dist - AC) / (d_contact - AC) ) , 0.0, max_alpha );
max_alpha = m_parent->isTip( e.first ) ? 0.9 : 1.0;
eep->neighbor_local_coord_0 = mathutils::clamp( beta * ( ( max_dist - AC) / (d_contact - AC) ) , 0.0, max_alpha );
}
}
else{
if( AD == d_contact ){
eep->alpha_0 = 0.0;
eep->neighbor_local_coord_1 = 0.0;
} else {
eep->alpha_0 = mathutils::clamp( alpha * ( ( max_dist - AD) / (d_contact - AD) ) , 0.0, max_alpha );
max_alpha = m_parent->isTip( e.second ) ? 0.9 : 1.0;
eep->neighbor_local_coord_1 = mathutils::clamp( beta * ( ( max_dist - AD) / (d_contact - AD) ) , 0.0, max_alpha );
}
}
// Edge d2 quad_region,
scalar BC = (xBc - xCc).norm();
scalar BD = (xBc - xDc).norm();
max_alpha = m_parent->isTip( base_edge.second ) ? 0.9 : 1.0;
if( BC < BD ){
if( BC == d_contact ){
eep->alpha_1 = 0.0;
eep->neighbor_local_coord_0 = 0.0;
} else {
eep->alpha_1 = mathutils::clamp( alpha * ( ( max_dist - BC) / (d_contact - BC) ) , 0.0, max_alpha );
max_alpha = m_parent->isTip( e.first ) ? 0.9 : 1.0;
eep->neighbor_local_coord_0 = mathutils::clamp( beta * ( ( max_dist - BC) / (d_contact - BC) ) , 0.0, max_alpha );
}
}
else{
if( BD == d_contact ){
eep->alpha_1 = 0.0;
eep->neighbor_local_coord_1 = 0.0;
} else {
eep->alpha_1 = mathutils::clamp( alpha * ( ( max_dist - BD) / (d_contact - BD) ) , 0.0, max_alpha );
max_alpha = m_parent->isTip( e.second ) ? 0.9 : 1.0;
eep->neighbor_local_coord_1 = mathutils::clamp( beta * ( ( max_dist - BD) / (d_contact - BD) ) , 0.0, max_alpha );
}
}
eep->avgpos = ( xAc + xBc + xCc + xDc ) * 0.25;
} // CCD check
else if( ref_pair ){
ref_pair->valid = false;
}
if(ref_pair) ref_pair->updated = true;
}); // edge neighbors
//remove EdgeEdgePairs that are no longer valid/updated
for(auto it = std::begin(edge_pairs); it != std::end(edge_pairs);)
{
if( !it->second->valid || !it->second->updated ){
delete it->second;
const std::pair<int, int>& e = scene_edges[it->first];
m_num_edge_connections[ e.first ] = std::max( 0, m_num_edge_connections[ e.first ] - 1 );
m_num_edge_connections[ e.second ] = std::max( 0, m_num_edge_connections[ e.second ] - 1 );
m_num_edge_connections[ base_edge.first ] = std::max( 0, m_num_edge_connections[ base_edge.first ] - 1 );
m_num_edge_connections[ base_edge.second ] = std::max( 0, m_num_edge_connections[ base_edge.second ] - 1 );
it = edge_pairs.erase(it);
}
else ++it;
}
}
template<int DIM>
void PolygonalCohesion<DIM>::findParticleEdgePairs(const VectorXs& x, int pidx)
{
const std::vector<int>& particle_to_hairs = m_parent->getParticleToHairs();
const std::vector<int>& particle_local_indices = m_parent->getParticleToHairLocalIndices();
const std::vector< std::pair<int, int> >& scene_edges = m_parent->getEdges();
const VectorXs& radii = m_parent->getRadii();
const scalar& radius_i = radii(pidx);
auto& flows = m_parent->getFilmFlows();
const scalar& theta = m_parent->getLiquidTheta();
const std::vector< std::vector<int> >& particle_edges = m_parent->getParticleToEdge();
int hidx = particle_to_hairs[pidx];
if(hidx < 0) return;
int local_idx = particle_local_indices[pidx];
const VectorXs& eta = flows[hidx]->getEta();
const scalar peta = eta(local_idx);
const scalar H_p = peta + radius_i;
const scalar vol_p = M_PI * (H_p * H_p - radius_i * radius_i);
const Vectors<DIM>& pos = x.segment<DIM>( m_parent->getDof( pidx ) );
const scalar& cellsize = m_parent->getSearchRadius();
const Vectors<DIM>& bbx_min = m_parent->getBoundingBoxMin();
int pindex[3] = {0, 0, 0};
int range[3] = {0, 0, 0};
const int nindex[3] = {m_sorter->ni, m_sorter->nj, m_sorter->nk};
for(int r = 0; r < DIM; ++r)
{
pindex[r] = std::max(0, std::min(nindex[r] - 1, (int)((pos(r) - bbx_min(r)) / cellsize)));
range[r] = 1;
}
auto& hair_pairs = m_adjacency_categorized[pidx];
const int npepair_local = std::max(1, m_num_adjacency_categorized[pidx]);
const scalar vol_p_each = vol_p / (scalar) npepair_local;
const scalar H_each = sqrt(vol_p / M_PI + radius_i * radius_i);
for(auto it = std::begin(hair_pairs); it != std::end(hair_pairs);)
{
// delete all original pairs that is marked as should-be-deleted and not marked as a part of valid double-linked-edge
// see the computation of point-edge pair for details
if(!it->second->updated && it->second->should_be_deleted) {
delete it->second;
it = hair_pairs.erase(it);
}
else ++it;
}
// mark all the original pairs as not-updated & original
for(auto hp : hair_pairs)
{
ParticleEdgePair* p = hp.second;
p->updated = false;
p->latest = false;
}
// get closest edge to each hair
m_sorter->getNeigboringParticles_cell(pindex[0], pindex[1], pindex[2], -range[0], range[0], -range[1], range[1], -range[2], range[2], [&] (int eidx) {
const std::pair<int, int>& e = scene_edges[eidx];
if(e.first == pidx || e.second == pidx)
return;
int nhidx = particle_to_hairs[e.first];
if(nhidx < 0 || nhidx == hidx) return;
Vectors<DIM> gap_vec;
scalar alpha;
mathutils::pointedgevec<scalar, DIM>(pos, x.segment<DIM>( m_parent->getDof( e.first ) ), x.segment<DIM>( m_parent->getDof( e.second ) ), gap_vec, alpha);
scalar dist = gap_vec.norm();
auto itr = hair_pairs.find(nhidx);
ParticleEdgePair* ref_pair = NULL;
bool previous_linked = false;
if(itr != hair_pairs.end()) {
ref_pair = itr->second;
if(ref_pair->updated && dist > ref_pair->dist) return;
if(!ref_pair->latest) previous_linked = true;
}
const scalar radius_j = sqrt(mathutils::lerp(radii(e.first) * radii(e.first), radii(e.second) * radii(e.second), alpha));
if(!ref_pair) {
ref_pair = new ParticleEdgePair;
ref_pair->latest = true;
hair_pairs[nhidx] = ref_pair;
}
ref_pair->pidx = pidx;
ref_pair->eidx = eidx;
ref_pair->alpha = alpha;
ref_pair->dist = dist;
ref_pair->radii_j = radius_j;
ref_pair->updated = true;
HairFlow<DIM>* neighbor_flow = flows[nhidx];
const VectorXs& neta = neighbor_flow->getEta();
int nlidx_0 = particle_local_indices[e.first];
int nlidx_1 = particle_local_indices[e.second];
const int npepair_neighbor_0 = std::max(1, (int) m_num_adjacency_categorized[e.first]);
const int npepair_neighbor_1 = std::max(1, (int) m_num_adjacency_categorized[e.second]);
const scalar npeta_0 = neta(nlidx_0);
const scalar npeta_1 = neta(nlidx_1);
const scalar nH_0 = npeta_0 + radii(e.first);
const scalar nH_1 = npeta_1 + radii(e.second);
const scalar vol_q_each = M_PI * mathutils::lerp((nH_0 * nH_0 - radii(e.first) * radii(e.first)) / (scalar) npepair_neighbor_0
, (nH_1 * nH_1 - radii(e.second) * radii(e.second)) / (scalar) npepair_neighbor_1, alpha);
scalar nH_each = sqrt(vol_q_each / M_PI + radius_j * radius_j);
scalar max_dist;
if(previous_linked) {
max_dist = std::max(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radius_i + radius_j);
} else {
max_dist = std::min(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radius_i + radius_j);
}
ref_pair->valid = (dist <= max_dist);
ref_pair->max_dist = max_dist;
});
// update all pairs that are not updated
for(auto hp : hair_pairs)
{
ParticleEdgePair* pepair = hp.second;
if(pepair->updated) continue;
bool previous_linked = pepair->valid;
const std::pair<int, int>& e = scene_edges[pepair->eidx];
Vectors<DIM> gap_vec;
scalar alpha;
mathutils::pointedgevec<scalar, DIM>(pos, x.segment<DIM>( m_parent->getDof( e.first ) ), x.segment<DIM>( m_parent->getDof( e.second ) ), gap_vec, alpha);
scalar dist = gap_vec.norm();
pepair->alpha = alpha;
pepair->dist = dist;
int nhidx = particle_to_hairs[e.first];
HairFlow<DIM>* neighbor_flow = flows[nhidx];
const VectorXs& neta = neighbor_flow->getEta();
int nlidx_0 = particle_local_indices[e.first];
int nlidx_1 = particle_local_indices[e.second];
const int npepair_neighbor_0 = std::max(1, (int) m_num_adjacency_categorized[e.first]);
const int npepair_neighbor_1 = std::max(1, (int) m_num_adjacency_categorized[e.second]);
const scalar npeta_0 = neta(nlidx_0);
const scalar npeta_1 = neta(nlidx_1);
const scalar nH_0 = npeta_0 + radii(e.first);
const scalar nH_1 = npeta_1 + radii(e.second);
const scalar vol_q_each = M_PI * mathutils::lerp((nH_0 * nH_0 - radii(e.first) * radii(e.first)) / (scalar) npepair_neighbor_0
, (nH_1 * nH_1 - radii(e.second) * radii(e.second)) / (scalar) npepair_neighbor_1, alpha);
scalar nH_each = sqrt(vol_q_each / M_PI + pepair->radii_j * pepair->radii_j);
scalar max_dist;
if(previous_linked) {
max_dist = std::max(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radius_i + pepair->radii_j);
} else {
max_dist = std::min(H_each + nH_each, (1.0 + 0.5 * theta) * sqrt(vol_p_each + vol_q_each) + radius_i + pepair->radii_j);
}
pepair->valid = (dist <= max_dist);
pepair->max_dist = max_dist;
}
// mark all the as not-updated again for later use, also all as not to be deleted
for(auto hp : hair_pairs)
{
ParticleEdgePair* p = hp.second;
p->updated = false;
p->should_be_deleted = false;
}
}
template<int DIM>
void PolygonalCohesion<DIM>::findParticleParticlePairsEEC(const VectorXs& x)
{
auto& edges = m_parent->getEdges();
auto& radii = m_parent->getRadii();
const int np = m_parent->getNumParticles();
const int ne = m_parent->getNumEdges();
if(!np) return;
if(!ne) return;
if(m_particle_to_pppairs.size() != np) m_particle_to_pppairs.resize(np);
if(m_num_valid_edge_connections.size() != ne) m_num_valid_edge_connections.resize(ne);
// foreach edge count valid eep
threadutils::thread_pool::ParallelFor(0, ne, [&] (int eidx){
int count_valid = 0;
auto& adjs = m_edge_connections[eidx];
for(auto itr = adjs.begin(); itr != adjs.end(); ++itr)
{
const EdgeEdgePairEEC* eep = itr->second;
if(!eep->valid) continue;
++count_valid;
}
m_num_valid_edge_connections[eidx] = count_valid;
});
m_counting_valid_adjacency = m_num_valid_edge_connections;
// prefix sum to get the locations of pp-pair
std::partial_sum(m_counting_valid_adjacency.begin(), m_counting_valid_adjacency.end(), m_counting_valid_adjacency.begin());
m_counting_pp_pairs.resize(m_counting_valid_adjacency[m_counting_valid_adjacency.size() - 1] * 2);
if(m_counting_pp_pairs.size() == 0) {
m_particle_particle_pairs.resize(0);
return;
}
// foreach edge, store the hash into locations
threadutils::thread_pool::ParallelFor(0, ne, [&] (int base_eidx){
const std::pair<int, int>& base_e = edges[base_eidx];
auto& adjs = m_edge_connections[base_eidx];
int base_loc = (base_eidx == 0) ? 0 : m_counting_valid_adjacency[base_eidx - 1];
int k = 0;
for(auto itr = adjs.begin(); itr != adjs.end(); ++itr)
{
const EdgeEdgePairEEC* eep = itr->second;
if(!eep->valid) continue;
const int loc = base_loc + k;
auto& neighbor_e = edges[itr->first];
// check distance
scalar d00 = (x.segment<DIM>( m_parent->getDof(base_e.first) ) - x.segment<DIM>( m_parent->getDof(neighbor_e.first) )).norm();
scalar d01 = (x.segment<DIM>( m_parent->getDof(base_e.first) ) - x.segment<DIM>( m_parent->getDof(neighbor_e.second) )).norm();
scalar d10 = (x.segment<DIM>( m_parent->getDof(base_e.second) ) - x.segment<DIM>( m_parent->getDof(neighbor_e.first) )).norm();
scalar d11 = (x.segment<DIM>( m_parent->getDof(base_e.second) ) - x.segment<DIM>( m_parent->getDof(neighbor_e.second) )).norm();
if(d00 < d01) {
m_counting_pp_pairs[loc * 2 + 0] = makePairwisePPHash(base_e.first, neighbor_e.first);
} else {
m_counting_pp_pairs[loc * 2 + 0] = makePairwisePPHash(base_e.first, neighbor_e.second);
}
if(d10 < d11) {
m_counting_pp_pairs[loc * 2 + 1] = makePairwisePPHash(base_e.second, neighbor_e.first);
} else {
m_counting_pp_pairs[loc * 2 + 1] = makePairwisePPHash(base_e.second, neighbor_e.second);
}
++k;
}
});
// sort the hash
tbb::parallel_sort(m_counting_pp_pairs.begin(), m_counting_pp_pairs.end());
const int n_pred_pairs = m_counting_pp_pairs.size();
m_counting_pp_pair_location.resize(n_pred_pairs);
// marking for the one where hash is different than previous
threadutils::thread_pool::ParallelFor(0, n_pred_pairs, [&] (int i) {
m_counting_pp_pair_location[i] = (i != 0 && m_counting_pp_pairs[i] != m_counting_pp_pairs[i - 1]);
});
// prefix sum to get relocated locations
std::partial_sum(m_counting_pp_pair_location.begin(), m_counting_pp_pair_location.end(), m_counting_pp_pair_location.begin());
// actually record the PP-pairs
m_particle_particle_pairs.resize(m_counting_pp_pair_location[n_pred_pairs - 1] + 1);
threadutils::thread_pool::ParallelFor(0, n_pred_pairs, [&] (int i) {
if(i == 0 || m_counting_pp_pairs[i] != m_counting_pp_pairs[i - 1]) {
int storing_loc = m_counting_pp_pair_location[i];
uint64_t hash_code = m_counting_pp_pairs[i];
ParticleParticlePair& ppp = m_particle_particle_pairs[storing_loc];
ppp.pidx[0] = (int)((hash_code >> 32UL) & 0xFFFFFFFFUL);
ppp.pidx[1] = (int)(hash_code & 0xFFFFFFFFUL);
scalar r0 = radii(ppp.pidx[0]);
scalar r1 = radii(ppp.pidx[1]);
ppp.d = std::max(r0 + r1, (x.segment<DIM>( m_parent->getDof(ppp.pidx[0]) ) - x.segment<DIM>( m_parent->getDof(ppp.pidx[1]) )).norm());
ppp.r = sqrt(mathutils::lerp(r0 * r0, r1 * r1, 0.5));
}
});
// accumulate pidx -> other particles
for(int i = 0; i < np; ++i)
{
m_particle_to_pppairs[i].resize(0);
}
const int npppairs = (int) m_particle_particle_pairs.size();
for(int i = 0; i < npppairs; ++i)
{
ParticleParticlePair& ppp = m_particle_particle_pairs[i];
m_particle_to_pppairs[ppp.pidx[0]].push_back(ppp.pidx[1]);
m_particle_to_pppairs[ppp.pidx[1]].push_back(ppp.pidx[0]);
}
}
template<int DIM>
void PolygonalCohesion<DIM>::findParticleParticlePairs(const VectorXs& x)
{
auto& edges = m_parent->getEdges();
auto& radii = m_parent->getRadii();
const int np = m_parent->getNumParticles();
auto& global_to_local = m_parent->getParticleToHairLocalIndices();
auto& particle_hair = m_parent->getParticleToHairs();
auto& flows = m_parent->getFilmFlows();
if(!np) return;
m_counting_valid_adjacency.resize(np);
// for each particle, record num of valid PE-pairs
threadutils::thread_pool::ParallelFor(0, np, [&] (int pidx) {
auto& adjs = m_adjacency_categorized[pidx];
int num_valid = 0;
auto& flow = flows[particle_hair[pidx]];
int local_idx = global_to_local[pidx];
if(local_idx != 0 && local_idx != flow->getParticleIndices().size() - 1) {
for(auto itr = adjs.begin(); itr != adjs.end(); ++itr)
{
if(itr->second->valid && itr->second->dist >= radii(pidx) * 4.0) ++num_valid;
}
}
m_counting_valid_adjacency[pidx] = num_valid;
});
// prefix sum to get the locations of pp-pair
std::partial_sum(m_counting_valid_adjacency.begin(), m_counting_valid_adjacency.end(), m_counting_valid_adjacency.begin());
m_counting_pp_pairs.resize(m_counting_valid_adjacency[m_counting_valid_adjacency.size() - 1]);
if(m_counting_pp_pairs.size() == 0) {
m_particle_particle_pairs.resize(0);
return;
}
// store the hash into locations
threadutils::thread_pool::ParallelFor(0, np, [&] (int pidx) {
auto& flow = flows[particle_hair[pidx]];
int local_idx = global_to_local[pidx];
if(local_idx != 0 && local_idx != flow->getParticleIndices().size() - 1) {
int base_loc = (pidx == 0) ? 0 : m_counting_valid_adjacency[pidx - 1];
int k = 0;
auto& adjs = m_adjacency_categorized[pidx];
for(auto itr = adjs.begin(); itr != adjs.end(); ++itr)
{
const ParticleEdgePair* pep = itr->second;
if(!pep->valid || itr->second->dist < radii(pidx) * 4.0) continue;
auto& e = edges[pep->eidx];
scalar d0 = (x.segment<DIM>(m_parent->getDof(pep->pidx)) - x.segment<DIM>(m_parent->getDof(e.first))).norm();
scalar d1 = (x.segment<DIM>(m_parent->getDof(pep->pidx)) - x.segment<DIM>(m_parent->getDof(e.second))).norm();
if(d0 < d1) m_counting_pp_pairs[base_loc + k] = makePairwisePPHash(pep->pidx, e.first);
else m_counting_pp_pairs[base_loc + k] = makePairwisePPHash(pep->pidx, e.second);
++k;
}
}
});
// sort the hash
tbb::parallel_sort(m_counting_pp_pairs.begin(), m_counting_pp_pairs.end());
const int n_pred_pairs = m_counting_pp_pairs.size();
m_counting_pp_pair_location.resize(n_pred_pairs);
// marking for the one where hash is different than previous
threadutils::thread_pool::ParallelFor(0, n_pred_pairs, [&] (int i) {
m_counting_pp_pair_location[i] = (i != 0 && m_counting_pp_pairs[i] != m_counting_pp_pairs[i - 1]);
});
// prefix sum to get relocated locations
std::partial_sum(m_counting_pp_pair_location.begin(), m_counting_pp_pair_location.end(), m_counting_pp_pair_location.begin());
// actually record the PP-pairs
m_particle_particle_pairs.resize(m_counting_pp_pair_location[n_pred_pairs - 1] + 1);
threadutils::thread_pool::ParallelFor(0, n_pred_pairs, [&] (int i) {
if(i == 0 || m_counting_pp_pairs[i] != m_counting_pp_pairs[i - 1]) {
int storing_loc = m_counting_pp_pair_location[i];
uint64_t hash_code = m_counting_pp_pairs[i];
ParticleParticlePair& ppp = m_particle_particle_pairs[storing_loc];
ppp.pidx[0] = (int)((hash_code >> 32UL) & 0xFFFFFFFFUL);
ppp.pidx[1] = (int)(hash_code & 0xFFFFFFFFUL);
scalar r0 = radii(ppp.pidx[0]);
scalar r1 = radii(ppp.pidx[1]);
ppp.d = std::max(r0 + r1, (x.segment<DIM>( m_parent->getDof(ppp.pidx[0]) ) - x.segment<DIM>( m_parent->getDof(ppp.pidx[1]) )).norm());
ppp.r = sqrt(mathutils::lerp(r0 * r0, r1 * r1, 0.5));
}
});
}
template<int DIM>
void PolygonalCohesion<DIM>::findPointEdgePairsEEC(const VectorXs& x, int base_eidx, std::vector<PointEdgePair>& poepairs)
{
if( DIM == 2 ) return;
auto& edges = m_parent->getEdges();
auto& e = edges[base_eidx];
auto& global_to_local = m_parent->getParticleToHairLocalIndices();
auto& particle_hair = m_parent->getParticleToHairs();
auto& flows = m_parent->getFilmFlows();
for( auto& adjPair : m_edge_connections[base_eidx] )
{
const EdgeEdgePairEEC& eepair = *(adjPair.second);
int num_linked_hairs_e0 = std::max(1, (int) m_num_edge_connections[e.first] );
int num_linked_hairs_e1 = std::max(1, (int) m_num_edge_connections[e.second] );
int local_base_e0 = global_to_local[e.first];
int local_base_e1 = global_to_local[e.second];
int hidx = particle_hair[e.first];
HairFlow<DIM>* flow = flows[hidx];
const VectorXs& eta = flow->getEta();
const VectorXs& radii_v = flow->getRadiiV();
int nhidx = particle_hair[ edges[eepair.neighbor_eidx].first ];
HairFlow<DIM>* neighbor_flow = flows[nhidx];
const VectorXs& neighbor_eta = neighbor_flow->getEta();
const VectorXs& neighbor_radii_v = neighbor_flow->getRadiiV();
const scalar peta_0 = eta(local_base_e0);
const scalar peta_1 = eta(local_base_e1);
const scalar radius_0 = radii_v(local_base_e0);
const scalar radius_1 = radii_v(local_base_e1);
const scalar H_0 = peta_0 + radius_0;
const scalar H_1 = peta_1 + radius_1;
auto& neighbor_e = edges[eepair.neighbor_eidx];
int local_neighbor_e0 = global_to_local[neighbor_e.first];
int local_neighbor_e1 = global_to_local[neighbor_e.second];
const scalar vol_p_each = M_PI * mathutils::lerp((H_0 * H_0 - radius_0 * radius_0) / (scalar) num_linked_hairs_e0
, (H_1 * H_1 - radius_1 * radius_1) / (scalar) num_linked_hairs_e1, eepair.alpha_contact);
// compute actual liquid area shared
int neighbor_num_linked_hairs_e0 = std::max(1, (int) m_num_edge_connections[neighbor_e.first] );
int neighbor_num_linked_hairs_e1 = std::max(1, (int) m_num_edge_connections[neighbor_e.second] );
const scalar npeta_0 = neighbor_eta(local_neighbor_e0);
const scalar npeta_1 = neighbor_eta(local_neighbor_e1);
const scalar nradius_0 = neighbor_radii_v(local_neighbor_e0);
const scalar nradius_1 = neighbor_radii_v(local_neighbor_e1);
const scalar nH_0 = npeta_0 + nradius_0;
const scalar nH_1 = npeta_1 + nradius_1;
const scalar vol_q_each = M_PI * mathutils::lerp((nH_0 * nH_0 - nradius_0 * nradius_0) / (scalar) neighbor_num_linked_hairs_e0
, (nH_1 * nH_1 - nradius_1 * nradius_1) / (scalar) neighbor_num_linked_hairs_e1, eepair.neighbor_local_coord_contact);
const scalar V = vol_p_each + vol_q_each;
if(V < 1e-7) continue;
const Vectors<DIM>& x0 = x.segment<DIM>( m_parent->getDof(e.first) );
const Vectors<DIM>& x1 = x.segment<DIM>( m_parent->getDof(e.second) );
const Vectors<DIM>& x2 = x.segment<DIM>( m_parent->getDof(neighbor_e.first) );
const Vectors<DIM>& x3 = x.segment<DIM>( m_parent->getDof(neighbor_e.second) );
const scalar area_base_e = (x1 - x0).norm();
const scalar neighbor_area_base_e = (x2 - x3).norm();
Vector3s xcc = eepair.avgpos;
FluidSim3D* fluid3d = (FluidSim3D*) m_parent->getFluidSim();
scalar cohesion_weight = 1.0 - fluid3d->get_clamped_particle_weight(xcc);
scalar criterion = m_parent->getBulkThresholdMultiplier() * fluid3d->cellsize();
PointEdgePair pep;
pep.alpha_point = eepair.alpha_contact;
pep.base_eidx = base_eidx;
pep.quadrature_weight = ( mathutils::clamp(eepair.alpha_1 - eepair.alpha_0, 0.0, 1.0) * area_base_e / mathutils::lerp(num_linked_hairs_e0, num_linked_hairs_e1, pep.alpha_point) + mathutils::clamp(eepair.neighbor_local_coord_1 - eepair.neighbor_local_coord_0, 0.0, 1.0) * neighbor_area_base_e / mathutils::lerp(neighbor_num_linked_hairs_e0, neighbor_num_linked_hairs_e1, pep.neighbor_alpha_point) ) * 0.25;
// FIX ME: another 0.5 is necessary since we duplicate the detection
pep.neighbor_alpha_point = eepair.neighbor_local_coord_contact;
pep.neighbor_eidx = eepair.neighbor_eidx;
pep.V = V;
pep.pressure_weight = cohesion_weight;
pep.hash_code = ((uint64) pep.base_eidx << 32UL) | ((uint64) (pep.neighbor_eidx ) & 32UL);
pep.time = eepair.time;
poepairs.push_back(pep);
}
}
template<int DIM>
void PolygonalCohesion<DIM>::findPointEdgePairs(const VectorXs& x, int base_eidx, std::vector<PointEdgePair>& poepairs)
{
auto& edges = m_parent->getEdges();
auto& e = edges[base_eidx];
auto& radii = m_parent->getRadii();
auto& global_to_local = m_parent->getParticleToHairLocalIndices();
auto& particle_hair = m_parent->getParticleToHairs();
auto& flows = m_parent->getFilmFlows();
// get all the hairs linked with the edge
std::unordered_map<int, std::pair<ParticleEdgePair*, ParticleEdgePair*> > hairs; // neighbor hair index -> Particle-Edge pair connected with that hair
for(auto& hair_pair : m_adjacency_categorized[e.first])
{
std::pair<ParticleEdgePair*, ParticleEdgePair*>& pair = hairs[hair_pair.first];
pair.first = NULL;
pair.second = NULL;
}
for(auto& hair_pair : m_adjacency_categorized[e.second])
{
std::pair<ParticleEdgePair*, ParticleEdgePair*>& pair = hairs[hair_pair.first];
pair.first = NULL;
pair.second = NULL;
}
for(auto& hair_pair : m_adjacency_categorized[e.first])
{
hairs[hair_pair.first].first = hair_pair.second;
}
for(auto& hair_pair : m_adjacency_categorized[e.second])
{
hairs[hair_pair.first].second = hair_pair.second;
}
// for each hair, check the state for length
for(auto& hair_pp : hairs)
{
const int nhidx = hair_pp.first;
std::pair<ParticleEdgePair*, ParticleEdgePair*>& pairs = hair_pp.second;
EdgeEdgePair eepair;
eepair.base_eidx = base_eidx;
eepair.neighbor_hair_idx = nhidx;
eepair.alpha_0 = 0.0;
eepair.alpha_1 = 1.0;
if(pairs.first && pairs.second) // both defined, full length
{
// check if both pairs valid
ParticleEdgePair* pair_0 = pairs.first;
ParticleEdgePair* pair_1 = pairs.second;
scalar raw_neighbor_coord_0 = computeLocalCoord(pair_0->eidx, pair_0->alpha);
scalar raw_neighbor_coord_1 = computeLocalCoord(pair_1->eidx, pair_1->alpha);
eepair.count_0 = pair_0->count;
eepair.count_1 = pair_1->count;
if(pair_0->valid && pair_1->valid) {
// both valid, do nothing to the alphas, mark them as a part of the double-linked-edges
pair_0->updated = true;
pair_1->updated = true;
} else if(!pair_0->valid && !pair_1->valid) {
// both invalid, ignore this edge-edge pair, and mark both particle-edge pairs as "should-be-deleted"
pair_0->should_be_deleted = true;
pair_1->should_be_deleted = true;
continue;
} else if(pair_0->valid) {
// alpha1 < 1.0
// mark them as a part of the double-linked-edges
pair_0->updated = true;
pair_1->updated = true;
const scalar& r0i = radii(pair_0->pidx);
const scalar& r0j = pair_0->radii_j;
const scalar& r1i = radii(pair_1->pidx);
const scalar& r1j = pair_1->radii_j;
scalar ddiv = (pair_0->max_dist - (pair_0->dist - (r0i + r0j))) - (pair_1->max_dist - (pair_1->dist - (r1i + r1j)));