-
Notifications
You must be signed in to change notification settings - Fork 0
/
tissue.hpp
1425 lines (1306 loc) · 52.3 KB
/
tissue.hpp
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
#ifndef TISSUE_HPP
#define TISSUE_HPP
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <Process.hpp>
#include <Attributes.hpp>
#include <Function.hpp>
#include <MDXProcessTissue.hpp>
#include <MeshProcessSystem.hpp>
#include <Contour.hpp>
#include <CCTopology.hpp>
#include <Mesh.hpp>
#include <MeshBuilder.hpp>
#include <Process.hpp>
#include <MDXProcessCellDivide.hpp>
#include <MeshProcessStructure.hpp>
#include <ToolsProcessSystem.hpp>
#include <CellMakerUtils.hpp>
#include <CCVerify.hpp>
#include <CCIndex.hpp>
#include <CellMakerMesh2D.hpp>
#include <GeomMathUtils.hpp>
#include <Geometry.hpp>
#include <Matrix.hpp>
#include <Triangulate.hpp>
using namespace mdx;
const double EPS = 1e-6;
const double BIG_VAL = 100000;
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() ;
void SVDDecompX(const Matrix3d &M, Matrix3d &U, Matrix3d &S, Matrix3d &V);
void PolarDecompX(Matrix3d &M,Matrix3d &S, Matrix3d &U);
Point3d Rotate(Point3d v, double angle);
bool PointInPolygon(Point3d point, std::vector<Point3d> points) ;
Point3d shortenLength(Point3d A, float reductionLength) ;
float DistancePtLine(Point3d a, Point3d b, Point3d p) ;
Point3d findClosestLineToLine(Point3d targetLine,
Point3d line1, Point3d line2) ;
std::vector<double> softmax(std::vector<double> v) ;
void MBR(std::vector<Point3d> points, std::vector<Point3d>& rect) ;
// extended version of the official one
void neighborhood2D(Mesh& mesh,
const CCStructure& cs,
const CCIndexDataAttr& indexAttr,
std::map<IntIntPair, double>& wallAreas,
std::map<IntIntPair, std::set<CCIndex>>& wallEdges);
class Tissue : public Process, public virtual TissueParms {
public:
Tissue(const Process& process)
: Process(process) {
setName("Model/Root/03 Cell Tissue");
addParm("Set Global Attr Process",
"Name of Set Global Attributes Process",
"Model/Root/23 Set Global Attr");
setIcon(QIcon(":/images/CellDivide.png"));
addParm("Draw Velocity Rescale", "Draw Velocity Rescale", "0.0");
addParm("Draw Strain Tensors Rescale", "Draw Strain Tensors Rescale", "0");
addParm("Draw MF",
"Draw MF",
"True",
QStringList() << "False"
<< "True");
addParm("Draw Bounding Box",
"Draw Bounding Box",
"False",
QStringList() << "False"
<< "True");
addParm("Draw Auxin-Flux Rescale",
"Draw Auxin-Flux Rescale",
"1");
addParm("Draw PGD Rescale",
"Draw PGD Rescale",
"1");
addParm("Draw PINs",
"Draw PINs",
"0.1");
addParm("Verbose", "Verbose", "False",
QStringList() << "False"
<< "True");
}
struct CellData;
struct FaceData;
struct EdgeData;
struct VertexData;
typedef AttrMap<int, CellData> CellDataAttr;
typedef AttrMap<CCIndex, FaceData> FaceDataAttr;
typedef AttrMap<CCIndex, EdgeData> EdgeDataAttr;
typedef AttrMap<CCIndex, VertexData> VertexDataAttr;
struct Data {
int dim = -1; // for the chemical solver
};
enum VertexType { NoVertexType, Inside, Border, Visual };
static const char* ToString(VertexType v) {
switch(v) {
case NoVertexType:
return "NoVertexType";
case Inside:
return "Inside";
case Border:
return "Border";
case Visual:
return "Visual";
default:
throw(QString("Bad vertex type %1").arg(v));
}
}
// Vertex Data
struct VertexData : Data {
// attributes worth saving
VertexType type = NoVertexType;
double invmass = -1;
Point3d restPos, prevPos, lastPos;
Point3d velocity, prevVelocity;
Data* dualCell = 0; // in case this is a vertex of the dual graph
bool divisionPoint = false;
std::map<std::pair<CCIndex, CCIndex>, double> angle;
// other dynamically loaded attributes
Point3u dirichlet = Point3u(0, 0, 0); // fixed Dirichlet boundary in x, y, z
int clusters = 0;
std::map<const char*, Point3d> corrections;
Point3d dampedVelocity;
bool substrate = false, source = false;
Point3d force;
// just for debug, these variable have no effect
std::vector<std::tuple<int, const char*, Point3d>> forces;
CCIndex V_v0, V_v1, V_e;
VertexData() {
dim = 0;
}
void resetChems() {}
void resetMechanics() {
restPos = prevPos = lastPos; // ?
}
void restore(CCIndex v, const CCStructure& cs,
const CCIndexDataAttr &indexAttr,const CellDataAttr &cellAttr,
const EdgeDataAttr &edgeAttr) {
type = Tissue::Inside;
for(CCIndex e : cs.incidentCells(v, 1))
if(edgeAttr[e].type == Wall)
type = Border;
substrate = false, source = false;
std::set<int> labels;
for(CCIndex f : cs.incidentCells(v, 2)) {
labels.insert(indexAttr[f].label);
if(cellAttr[indexAttr[f].label].type==Substrate)
substrate = true;
if(cellAttr[indexAttr[f].label].type==Source)
source = true;
}
clusters = labels.size();
if(norm(restPos) == 0)
restPos = indexAttr[v].pos;
}
// careful when you change these
bool read(const QByteArray& ba, size_t& pos) {
readPOD<VertexType>(type, ba, pos);
readPOD<Data*>(dualCell, ba, pos);
readPOD<double>(invmass, ba, pos);
readPOD<Point3d>(restPos, ba, pos);
readPOD<Point3d>(prevPos, ba, pos);
readPOD<Point3d>(lastPos, ba, pos);
readPOD<Point3d>(velocity, ba, pos);
readPOD<Point3d>(prevVelocity, ba, pos);
readPOD<bool>(divisionPoint, ba, pos);
return true;
}
// attribute must be write and read in the same order
bool write(QByteArray& ba) {
writePOD<VertexType>(type, ba);
writePOD<Data*>(dualCell, ba);
writePOD<double>(invmass, ba);
writePOD<Point3d>(restPos, ba);
writePOD<Point3d>(prevPos, ba);
writePOD<Point3d>(lastPos, ba);
writePOD<Point3d>(velocity, ba);
writePOD<Point3d>(prevVelocity, ba);
writePOD<bool>(divisionPoint, ba);
return true;
}
bool operator==(const VertexData& other) const {
if(dualCell == other.dualCell)
return true;
return false;
}
};
// Cell Data
enum CellType {
Undefined,
QC,
Columella,
ColumellaInitial,
CEI,
CEID,
Cortex,
Endodermis,
Pericycle,
Vascular,
VascularInitial,
EpLrcInitial,
Epidermis,
LRC,
Substrate,
Source
};
static CellType stringToCellType(const QString& str) {
if(str == "Undefined")
return (Undefined);
else if(str == "QC")
return (QC);
else if(str == "Columella")
return (Columella);
else if(str == "ColumellaInitial")
return (ColumellaInitial);
else if(str == "CEI")
return (CEI);
else if(str == "CEID")
return (CEID);
else if(str == "Cortex")
return (Cortex);
else if(str == "Endodermis")
return (Endodermis);
else if(str == "Vascular")
return (Vascular);
else if(str == "VascularInitial")
return (VascularInitial);
else if(str == "Pericycle")
return (Pericycle);
else if(str == "EpLrcInitial")
return (EpLrcInitial);
else if(str == "Epidermis")
return (Epidermis);
else if(str == "LRC")
return (LRC);
else if(str == "Substrate")
return (Substrate);
else if(str == "Source")
return (Source);
else
throw(QString("Bad cell type %1").arg(str));
}
static const char* ToString(CellType v) {
switch(v) {
case Undefined:
return "Undefined";
case QC:
return "QC";
case Columella:
return "Columella";
case ColumellaInitial:
return "ColumellaInitial";
case CEI:
return "CEI";
case CEID:
return "CEID";
case Cortex:
return "Cortex";
case Endodermis:
return "Endodermis";
case VascularInitial:
return "VascularInitial";
case Vascular:
return "Vascular";
case Pericycle:
return "Pericycle";
case EpLrcInitial:
return "EpLrcInitial";
case Epidermis:
return "Epidermis";
case LRC:
return "LRC";
case Substrate:
return "Substrate";
case Source:
return "Source";
default:
throw(QString("Bad cell type %1").arg(v));
}
}
struct CellData : Data {
// attributes worth saving
Tissue *tissue = 0;
int label = -1;
CellType type = Undefined;
int lineage = 0;
CCIndex dualVertex;
std::set<CCIndex>* cellFaces = 0;
std::vector<CCIndex> cellVertices;
std::vector<CCIndex> perimeterFaces, perimeterVertices, perimeterEdges;
double area = 0, prevArea = 0, restArea = 0;
double invmassVertices = 1;
Point3d a1 = Point3d(0, EPS, 0), a2 = Point3d(EPS, 0, 0);
double mfRORate = -1;
double wallsMaxGR = -1;
double lifeTime = 0;
double remeshTime = 0;
double lastDivision = 0;
double divisionCount = 0;
int stage = 0; // Meristem, elongation, differentiation
Point3d axisMin, axisMax, prev_axisMin, prev_axisMax;
bool periclinalDivision = false;
int divAlg = -1;
bool shapeInit = false;
double cellMaxArea = 1000;
Point3d restCm;
Matrix3d invRestMat;
std::vector<Point3d> restX0;
double growthSignal = 1;
double auxin = 0, prevAuxin = 0;
double Pin1 = 0;
double Aux1 = 0;
double PINOID = 0;
double PP2A = 0;
double divInhibitor = 0, divPromoter = 0;
double quasimodo = 0;
double wox5 = 0; bool QCwox5Flag = false;
double pressure = 1, pressureMax = -1;
double auxinProdRate = -1;
double auxinDecayRate = 0;
double pinProdRate = -1;
double pinInducedRate = -1;
double aux1ProdRate = -1;
double aux1InducedRate = -1;
double aux1MaxEdge = -1;
double brassinosteroids = 0;
double brassinosteroidProd = 0;
int brassinosteroidTarget = 0;
double brassinosteroidSignal = 1;
int brassinosteroidTop = -1;
int brassinosteroidMother = 0;
bool is_ccvTIR1tissue = false;
// other dinamically loaded attribute
bool selected = false;
std::pair<int, int> daughters;
Point3d divVector = Point3d(1., 0., 0.);
bool mfDelete = false;
double perimeter = 0;
std::vector<Point3d> box;
Point3d centroid;
std::map<CCIndex, double> perimeterAngles;
double growthRate = 0, axisMin_growthRate = 0, axisMax_growthRate = 0;
std::vector<double> growthRates, axisMin_grs, axisMax_grs;
Matrix3d G, E, U = Matrix3d().identity();
double gMax = 0, gMin = 0, gAngle = 0;
bool divisionAllowed = true;
double divProb = 0;
std::map<int, Point3d> auxinFluxes;
Point3d auxinFluxVector;
Point3d auxinGradientVector;
double totalStiffness = 0;
// visuals
CCIndex a1_v1, a1_v2, a1_e, a2_v1, a2_v2, a2_e,
auxinFlux_v1, auxinFlux_v2, auxinFlux_v3, auxinFlux_v4, auxinFlux_e, auxinFlux_el, auxinFlux_er, auxinFlux_eb, auxinFlux_f;
CCIndex axisMin_v, axisMax_v, axisCenter_v, axisMin_e, axisMax_e;
CCIndex PDGmax_e, PDGmax_v1, PDGmax_v2, PDGmin_e, PDGmin_v1, PDGmin_v2;
CCIndex box_v[4], box_e[4];
CellData()
: cellFaces(new std::set<CCIndex>()) {
dim = 5;
box.reserve(4);
if(type == CEID)
periclinalDivision = true;
}
~CellData() {
}
// BE CAREFUL, every time you change these two functions, you will have to manually reset the attibutes
bool read(const QByteArray& ba, size_t& pos) {
readPOD<Tissue*>(tissue, ba, pos);
readPOD<int>(label, ba, pos);
readPOD<CellType>(type, ba, pos);
readPOD<CCIndex>(dualVertex, ba, pos);
readPOD<double>(area, ba, pos);
readPOD<double>(prevArea, ba, pos);
readPOD<double>(restArea, ba, pos);
readPOD<double>(invmassVertices, ba, pos);
readPOD<Point3d>(a1, ba, pos);
readPOD<Point3d>(a2, ba, pos);
readPOD<double>(mfRORate, ba, pos);
readPOD<double>(auxin, ba, pos);
readPOD<double>(Aux1, ba, pos);
readPOD<double>(Pin1, ba, pos);
readPOD<double>(auxinProdRate, ba, pos);
readPOD<double>(auxinDecayRate, ba, pos);
// cellFaces
std::vector<CCIndex> v;
size_t sz;
readPOD<size_t>(sz, ba, pos);
v.resize(sz);
readChar(v.data(), sz * sizeof(CCIndex), ba, pos);
cellFaces->clear();
for(CCIndex i : v)
cellFaces->insert(i);
readPOD<double>(lastDivision, ba, pos);
readPOD<bool>(periclinalDivision, ba, pos);
readPOD<Point3d>(divVector, ba, pos);
readPOD<double>(cellMaxArea, ba, pos);
readPOD<Point3d>(axisMax, ba, pos);
readPOD<Point3d>(axisMin, ba, pos);
// cellVertices
std::vector<CCIndex> cv;
size_t cv_sz;
readPOD<size_t>(cv_sz, ba, pos);
cv.resize(cv_sz);
readChar(cv.data(), cv_sz * sizeof(CCIndex), ba, pos);
cellVertices.clear();
for(CCIndex i : cv)
cellVertices.push_back(i);
// perimeterFaces
std::vector<CCIndex> pf;
size_t pf_sz;
readPOD<size_t>(pf_sz, ba, pos);
pf.resize(pf_sz);
readChar(pf.data(), pf_sz * sizeof(CCIndex), ba, pos);
perimeterFaces.clear();
for(CCIndex i : pf)
perimeterFaces.push_back(i);
// perimeterEdges
std::vector<CCIndex> pe;
size_t pe_sz;
readPOD<size_t>(pe_sz, ba, pos);
pe.resize(pe_sz);
readChar(pe.data(), pe_sz * sizeof(CCIndex), ba, pos);
perimeterEdges.clear();
for(CCIndex i : pe)
perimeterEdges.push_back(i);
// perimeterVertices
std::vector<CCIndex> pv;
size_t pv_sz;
readPOD<size_t>(pv_sz, ba, pos);
pv.resize(pv_sz);
readChar(pv.data(), pv_sz * sizeof(CCIndex), ba, pos);
perimeterVertices.clear();
for(CCIndex i : pv)
perimeterVertices.push_back(i);
// invRestMatrix
readPOD<Point3d>(invRestMat[0], ba, pos);
readPOD<Point3d>(invRestMat[1], ba, pos);
readPOD<Point3d>(invRestMat[2], ba, pos);
// restX0
std::vector<Point3d> rX0;
size_t rX0_sz;
readPOD<size_t>(rX0_sz, ba, pos);
rX0.resize(rX0_sz);
readChar(rX0.data(), rX0_sz * sizeof(Point3d), ba, pos);
restX0.clear();
for(Point3d i : rX0)
restX0.push_back(i);
readPOD<Point3d>(restCm, ba, pos);
//
readPOD<double>(lifeTime, ba, pos);
readPOD<double>(pressure, ba, pos);
readPOD<double>(pressureMax, ba, pos);
readPOD<bool>(shapeInit, ba, pos);
readPOD<int>(divAlg, ba, pos);
return true;
}
// attribute must be write and read in the same order
bool write(QByteArray& ba) {
writePOD<Tissue*>(tissue, ba);
writePOD<int>(label, ba);
writePOD<CellType>(type, ba);
writePOD<CCIndex>(dualVertex, ba);
writePOD<double>(area, ba);
writePOD<double>(prevArea, ba);
writePOD<double>(restArea, ba);
writePOD<double>(invmassVertices, ba);
writePOD<Point3d>(a1, ba);
writePOD<Point3d>(a2, ba);
writePOD<double>(mfRORate, ba);
writePOD<double>(auxin, ba);
writePOD<double>(Aux1, ba);
writePOD<double>(Pin1, ba);
writePOD<double>(auxinProdRate, ba);
writePOD<double>(auxinDecayRate, ba);
// cellFaces
std::vector<CCIndex> v(cellFaces->begin(), cellFaces->end());
size_t sz = v.size();
writePOD<size_t>(sz, ba);
writeChar(v.data(), sz * sizeof(CCIndex), ba);
writePOD<double>(lastDivision, ba);
writePOD<bool>(periclinalDivision, ba);
writePOD<Point3d>(divVector, ba);
writePOD<double>(cellMaxArea, ba);
writePOD<Point3d>(axisMax, ba);
writePOD<Point3d>(axisMin, ba);
// cellVertices
std::vector<CCIndex> cv(cellVertices.begin(), cellVertices.end());
size_t cv_sz = cv.size();
writePOD<size_t>(cv_sz, ba);
writeChar(cv.data(), cv_sz * sizeof(CCIndex), ba);
// perimeterFaces
std::vector<CCIndex> pf(perimeterFaces.begin(), perimeterFaces.end());
size_t pf_sz = pf.size();
writePOD<size_t>(pf_sz, ba);
writeChar(pf.data(), pf_sz * sizeof(CCIndex), ba);
// perimeterEdges
std::vector<CCIndex> pe(perimeterEdges.begin(), perimeterEdges.end());
size_t pe_sz = pe.size();
writePOD<size_t>(pe_sz, ba);
writeChar(pe.data(), pe_sz * sizeof(CCIndex), ba);
// perimeterVertices
std::vector<CCIndex> pv(perimeterVertices.begin(), perimeterVertices.end());
size_t pv_sz = pv.size();
writePOD<size_t>(pv_sz, ba);
writeChar(pv.data(), pv_sz * sizeof(CCIndex), ba);
// invRestMatrix
writePOD<Point3d>(invRestMat[0], ba);
writePOD<Point3d>(invRestMat[1], ba);
writePOD<Point3d>(invRestMat[2], ba);
// restX0
std::vector<Point3d> rx0(restX0.begin(), restX0.end());
size_t rx0_sz = rx0.size();
writePOD<size_t>(rx0_sz, ba);
writeChar(rx0.data(), rx0_sz * sizeof(Point3d), ba);
writePOD<Point3d>(restCm, ba);
//
writePOD<double>(lifeTime, ba);
writePOD<double>(pressure, ba);
writePOD<double>(pressureMax, ba);
writePOD<bool>(shapeInit, ba);
writePOD<int>(divAlg, ba);
return true;
}
bool operator==(const CellData& other) const {
if(area == other.area)
return true;
return false;
}
// reset chemicals
void resetChems() {
auxin = 0;
Pin1 = 0;
Aux1 = 0;
PP2A = 0;
PINOID = 0;
divPromoter = 0;
divInhibitor = 0;
quasimodo = 0;
wox5 = 0;
brassinosteroids = 0;
auxinFluxes.clear();
auxinFluxVector = Point3d(0, 0, 0);
}
void resetMechanics(FaceDataAttr& faceAttr) {
a1 = Point3d(1, 1, 0) * EPS, a2 = Point3d(1, 1, 0) * EPS;
restArea = area;
restCm = centroid;
pressure = 0;
pressureMax = -1;
shapeInit = false;
invmassVertices = 1;
mfRORate = -1;
for(CCIndex f : *cellFaces){
FaceData &fD = faceAttr[f];
fD.resetMechanics();
}
growthRates.clear();
}
void updateGeom(const CCIndexDataAttr& indexAttr, FaceDataAttr& faceAttr, EdgeDataAttr& edgeAttr, double Dt) {
prevArea = area;
area = 0;
centroid = Point3d(0, 0, 0);
// deformation gradient and green strain tensor
G = 0, E = 0;
int i = 0;
for(CCIndex f : *cellFaces) {
FaceData fD = faceAttr[f];
area += indexAttr[f].measure;
centroid += indexAttr[f].pos;
if(fD.type == Membrane) {
E += fD.E;
G += fD.G;
i++;
}
}
centroid /= cellFaces->size();
E /= i;
G /= i;
// perimeter length and total stiffness
perimeter = 0; totalStiffness = 0;
for(CCIndex e : perimeterEdges) {
perimeter += edgeAttr[e].length;
totalStiffness += edgeAttr[e].eStiffness;
}
totalStiffness /= perimeterEdges.size();
// cell growth rate
if(Dt > 0)
growthRates.push_back((area - prevArea) / (prevArea * Dt));
if(growthRates.size() > 100)
growthRates.erase(growthRates.begin());
growthRate = 0;
int grs = 1;
for(double gr : growthRates)
if(gr > 0) {
growthRate += gr;
grs++;
}
growthRate /= grs;
// principal components of the green strain tensor
gAngle = gMax = gMin = 0;
if((G[0][0] - G[1][1]) != 0) {
gAngle = (0.5 * atan(2 * G[0][1] / (G[0][0] - G[1][1]) )) ;
double left = (G[0][0] + G[1][1]) / 2;
double right = sqrt(pow((G[0][0] - G[1][1]) / 2, 2) + pow(G[0][1]/2, 2));
gMax = left + right;
gMin = left - right;
}
if(G[0][0] < G[1][1])
if(gAngle > -(M_PI/4) && gAngle < (M_PI/4))
gAngle += (M_PI/2);
}
void restore(Mesh* mesh, Tissue* tissue,
const CCStructure& cs,
const CCStructure& csDual,
CCIndexDataAttr& indexAttr,
FaceDataAttr& faceAttr,
EdgeDataAttr& edgeAttr,
VertexDataAttr& vMAttr) {
this->tissue = tissue;
if(lineage == 0)
lineage = label;
// find my faces
std::set<CCIndex> old_cellFaces = *cellFaces;
cellFaces->clear();
for(CCIndex f : cs.faces())
if(indexAttr[f].label == label) {
cellFaces->insert(f);
faceAttr[f].owner = this;
}
if(old_cellFaces != *cellFaces)
shapeInit = false;
// update the geometry
updateGeom(indexAttr, faceAttr, edgeAttr, 0);
// find my dual vertex
if(csDual.vertices().size() > 0) {
//throw(QString("CellData::restore empty dual graph"));
CCIndex closest = csDual.vertices()[0];
for(CCIndex v : csDual.vertices())
if(norm(indexAttr[v].pos - centroid) < norm(indexAttr[closest].pos - centroid))
closest = v;
indexAttr[closest].label = label;
vMAttr[closest].dualCell = this;
dualVertex = closest;
}
// set perimeter edges and faces, and set average restLength for edges
cellVertices.clear();
perimeterFaces.clear();
perimeterEdges.clear();
std::vector<CCIndex> tmpEdges, tmpVertices;
perimeterVertices.clear();
for(CCIndex f : *cellFaces)
for(CCIndex e : cs.incidentCells(f, 1)) {
if(std::find(cellVertices.begin(), cellVertices.end(), cs.edgeBounds(e).first) == cellVertices.end())
cellVertices.push_back(cs.edgeBounds(e).first);
if(std::find(cellVertices.begin(), cellVertices.end(), cs.edgeBounds(e).second) == cellVertices.end())
cellVertices.push_back(cs.edgeBounds(e).second);
if(vMAttr[cs.edgeBounds(e).first].invmass == -1)
vMAttr[cs.edgeBounds(e).first].invmass = invmassVertices;
if(vMAttr[cs.edgeBounds(e).second].invmass == -1)
vMAttr[cs.edgeBounds(e).second].invmass = invmassVertices;
std::set<CCIndex> edgeFaces = cs.incidentCells(e, 2);
CCIndex f1 = *(edgeFaces.begin());
CCIndex f2;
if(edgeFaces.size() > 1)
f2 = *(++edgeFaces.begin());
if(mesh->getLabel(indexAttr[f1].label) == mesh->getLabel(indexAttr[f2].label))
continue;
if(std::find(perimeterFaces.begin(), perimeterFaces.end(), f) == perimeterFaces.end())
perimeterFaces.push_back(f);
if(std::find(tmpEdges.begin(), tmpEdges.end(), e) == tmpEdges.end())
tmpEdges.push_back(e);
if(std::find(tmpVertices.begin(), tmpVertices.end(), cs.edgeBounds(e).first) == tmpVertices.end())
tmpVertices.push_back(cs.edgeBounds(e).first);
if(std::find(tmpVertices.begin(), tmpVertices.end(), cs.edgeBounds(e).second) == tmpVertices.end())
tmpVertices.push_back(cs.edgeBounds(e).second);
}
// sort the perimeter edges and vertices
std::vector<std::pair<Point3d,Point3d> > polygonSegs;
std::vector<Point3d> polygonPoints;
for(CCIndex e : tmpEdges) {
auto eb = cs.edgeBounds(e);
polygonSegs.push_back(make_pair(indexAttr[eb.first].pos, indexAttr[eb.second].pos));
}
for(CCIndex v : tmpVertices)
polygonPoints.push_back(indexAttr[v].pos);
std::vector<std::pair<Point3d,Point3d> > polygonSegsOrig = polygonSegs;
std::vector<Point3d> polygonPointsOrdered = mdx::orderPolygonSegs(polygonSegs, true);
for(auto p : polygonSegs) {
auto it = std::find(polygonSegsOrig.begin(), polygonSegsOrig.end(), p);
if(it == polygonSegsOrig.end()) {
Point3d tmp = p.first;
p.first = p.second;
p.second = tmp;
it = std::find(polygonSegsOrig.begin(), polygonSegsOrig.end(), p);
}
perimeterEdges.push_back(tmpEdges[std::distance(polygonSegsOrig.begin(), std::find(polygonSegsOrig.begin(), polygonSegsOrig.end(), p))]);
}
for(auto p : polygonPointsOrdered)
perimeterVertices.push_back(tmpVertices[std::distance(polygonPoints.begin(), std::find(polygonPoints.begin(), polygonPoints.end(), p))]);
// set the perimeter vertices' angles
int n = perimeterVertices.size();
for(int i = 0; i < n; i++) {
CCIndex v = perimeterVertices[i];
if(perimeterAngles.find(v) == perimeterAngles.end()){
int prev = i-1;
if(prev == -1)
prev = n-1;
int next = i+1;
if(next == n)
next = 0;
Point3d p0 = indexAttr[perimeterVertices[prev]].pos - indexAttr[perimeterVertices[i]].pos;
Point3d p1 = indexAttr[perimeterVertices[next]].pos - indexAttr[perimeterVertices[i]].pos;
perimeterAngles[v] = mdx::angle(p0, p1);
}
CCIndex e1, e2;
for(CCIndex e : cs.incidentCells(v, 1))
if(find(perimeterEdges.begin(), perimeterEdges.end(), e) != perimeterEdges.end()){
if(e1.isPseudocell())
e1 = e;
else
e2 = e;
}
if(e1.isPseudocell() || e2.isPseudocell())
throw(QString("Something wrong here"));
int prev = i-1;
if(prev == -1) prev = n-1;
int next = i+1;
if(next == n) next = 0;
Point3d p0 = indexAttr[perimeterVertices[prev]].pos - indexAttr[perimeterVertices[i]].pos;
Point3d p1 = indexAttr[perimeterVertices[next]].pos - indexAttr[perimeterVertices[i]].pos;
if(vMAttr[v].angle[make_pair(e1, e2)] == 0)
vMAttr[v].angle[make_pair(e1, e2)] = mdx::angle(p0, p1);
}
}
void
update(const CCStructure& cs, const CCIndexDataAttr& indexAttr, FaceDataAttr& faceAttr, EdgeDataAttr &edgeAttr, VertexDataAttr& vMAttr, double Dt) {
// Geometry
updateGeom(indexAttr, faceAttr, edgeAttr, Dt);
// Misc updates
lastDivision += Dt;
if(perimeterVertices.size() > 0) {
// bounding box
std::vector<Point3d> points;
for(auto v : perimeterVertices)
points.push_back(indexAttr[v].pos);
MBR(points, box);
prev_axisMax = axisMax;
prev_axisMin = axisMin;
axisMax = box[0] - box[1];
axisMin = box[1] - box[2];
if(norm(axisMax) < norm(axisMin)) {
Point3d tmp = axisMin;
axisMin = axisMax;
axisMax = tmp;
}
// Axis growth rates, needed for PGD visualization
if(Dt > EPS) {
double axisMax_gr = (norm(axisMax) - norm(prev_axisMax)) / Dt;
double axisMin_gr = (norm(axisMin) - norm(prev_axisMin)) / Dt;
if(axisMax_gr > 0 && axisMin_gr > 0) {
axisMax_grs.insert(axisMax_grs.begin(), axisMax_gr);
axisMin_grs.insert(axisMin_grs.begin(), axisMin_gr);
}
uint max_values = 30;
if(axisMax_grs.size() > max_values || axisMin_grs.size() > max_values) {
axisMax_grs.resize(max_values);
axisMin_grs.resize(max_values);
}
axisMax_growthRate = 0, axisMin_growthRate = 0;
for(uint i = 0; i < axisMax_grs.size(); i++) {
axisMax_growthRate += axisMax_grs[i];
axisMin_growthRate += axisMin_grs[i] ;
}
if(axisMax_grs.size() > 0)
axisMax_growthRate /= axisMax_grs.size();
if(axisMin_grs.size() > 0)
axisMin_growthRate /= axisMin_grs.size();
}
} else
mdxInfo << "Cell " << label << " has no vertices!?!?" << endl;
}
void setType(CellType newType, VertexDataAttr& vMAttr) {
type = newType;
for(CCIndex v : cellVertices) {
vMAttr[v].substrate = newType == Substrate;
vMAttr[v].source = newType == Source;
}
}
void division(const CCStructure &cs, const CCIndexDataAttr& indexAttr, CellDataAttr& cellAttr,
FaceDataAttr& faceAttr, EdgeDataAttr &edgeAttr, VertexDataAttr& vMAttr,
CellData& cD1, CellData& cD2, std::map<CellType, int> maxAreas, bool ignoreCellType=false) ;
};
// Edge Data
enum EdgeType { NoEdgeType, Shear, Wall };
static const char* ToString(EdgeType v) {
switch(v) {
case Shear:
return "Shear";
case Wall:
return "Wall";
case NoEdgeType:
return "NoEdgeType";
default:
throw(QString("Bad edge type %1").arg(v));
}
}
struct EdgeData : Data {
// attributes worth saving
EdgeType type = NoEdgeType;
double restLength = 0;
double prevLength = 0, length = 0;
double prev_strain = 0, strain = 0;
double strainRate = 0;
double updateRate = 0;
double eStiffness = -1;
double cStiffness = -1;
double intercellularAuxin = 0;
std::map<int, double> Aux1;
std::map<int, double> Pin1;
std::map<int, double> PINOID;
std::map<int, double> PP2A;
// other dynamical attrs
std::map<int, double> auxinRatio;
std::map<int, double> auxinGrad;
Point3d midPoint;
std::map<int, double> angle;
std::map<CCIndex, Point3d> outwardNormal;
std::map<int, double> auxinFluxImpact, MFImpact, geomImpact;
std::map<int, double> pin1Sensitivity, pin1SensitivityRaw;
Point3d pressureForce = Point3d(0, 0, 0);
CCIndex pin_v1_1, pin_v2_1, pin_v3_1, pin_v4_1, pin_e1_1, pin_e2_1, pin_e3_1, pin_e4_1, pin_f_1;
CCIndex pin_v1_2, pin_v2_2, pin_v3_2, pin_v4_2, pin_e1_2, pin_e2_2, pin_e3_2, pin_e4_2, pin_f_2;
EdgeData() {
dim = 1;
} // for the chemical solver
// reset chemicals
void resetChems() {
intercellularAuxin = 0;
Aux1.clear();
Pin1.clear();
PINOID.clear();
PP2A.clear();
auxinFluxImpact.clear();
pin1Sensitivity.clear();
}
void resetMechanics() {
restLength = length;
prevLength = length;
}
void restore(CCIndex e,
const CCStructure& cs,
const CCIndexDataAttr& indexAttr,
const CellDataAttr& cellAttr) {
auto eb = cs.edgeBounds(e);
length = norm(indexAttr[eb.first].pos - indexAttr[eb.second].pos);
if(restLength <= 0)
restLength = length;
// determine whether we are a wall or internal edge
std::vector<int> incident_labels;
for(CCIndex f : cs.incidentCells(e, 2)) {
int label = indexAttr[f].label;
incident_labels.push_back(label);
}
if((incident_labels.size() == 2 && incident_labels[0] != incident_labels[1]) ||
cs.onBorder(e))
this->type = Tissue::Wall;
else
this->type = Tissue::Shear;
}
// to be called at each step
void update(CCIndex e,
const CCStructure& cs,
const CCIndexDataAttr& indexAttr,
VertexDataAttr& vMAttr,
FaceDataAttr& faceAttr) {
auto eb = cs.edgeBounds(e);
Point3d vPos = indexAttr[eb.first].pos;
Point3d nPos = indexAttr[eb.second].pos;
prevLength = length;
length = norm(vPos - nPos);
prev_strain = strain;
strain = (length - restLength) / restLength;
Point3d versor = vPos - nPos;
Point3d dv = vMAttr[eb.first].velocity - vMAttr[eb.second].velocity;
strainRate = (versor * dv) / prevLength;
midPoint = (nPos + (vPos - nPos) / 2.);
outwardNormal.clear();
std::vector<int> incident_labels;
for(CCIndex f : cs.incidentCells(e, 2)) {
incident_labels.push_back(indexAttr[f].label);
Point3d versor = (midPoint - indexAttr[f].pos);
outwardNormal[f] = Point3d(0., 0., 1.) ^ (vPos - nPos);
outwardNormal[f] *= 1. / norm(outwardNormal[f]);
if(outwardNormal[f] * versor < 0.)
outwardNormal[f] *= -1.;
Point3d eVector = vPos - nPos;
// angle is relative to MF a1 orientation
/*
* 90º
* ^
* |
* |
* |
* |
* 0º <-----------o---------------> 180º
* |
* |
* |
* |
* v
* 90º
*/
FaceData& fD = faceAttr[f];
if(!fD.owner)
throw(QString("Tissue::EdgeData::update: orphan face, something wrong with tissue restore or cell division?"));
// angle[fD.owner->label] = fabs(atan2(eVector[1], eVector[0])) *
// 180./M_PI;