-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparadis.h
More file actions
1825 lines (1527 loc) · 50.9 KB
/
paradis.h
File metadata and controls
1825 lines (1527 loc) · 50.9 KB
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
/* Written by Richard David Cook
at Lawrence Livermore National Laboratory
Contact: wealthychef@gmail.com
See license.txt for information about usage.
Spoiler alert: it's GNU opensource.
*/
/*!
\file paradis.h
\brief data structures and API for libparadis
Definition, needed to find a node in a std::set<Node>: node A > node B iff ( domain A > domain B ) || ( domain A == domain B && node ID A > node ID B ). segment A > segment B iff segA.nodes[0] > segB.nodes[0].
------------------------------------------------------------------
ALGORITHM OVERVIEW
THIS IS A SERIAL ALGORITHM and parallelizing it would require multi-pass out of core techniques.
-- create all relevant IB, OOB, and IOOB nodes and related arm segments
-- create and classify arms
-- decompose higher-energy arms into lower-energy arms until there is nothing but loops and type 111 arms in the data set.
-- wrap any segments that cross periodic boundaries
-- create meta-arms
------------------------------------------------------------------
*/
#ifndef PARADIS_H
#define PARADIS_H
// set this to 1 to re-enable linked loops code
#define LINKED_LOOPS 0
/* now for the API */
#include "boost/cstdint.hpp"
using boost::int16_t;
using boost::int16_t;
using boost::int32_t;
using boost::uint32_t;
#include "boost/shared_ptr.hpp"
#include "boost/format.hpp"
#include <stdio.h>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <ostream>
#include <math.h>
#include "Point.h"
#include "pathutil.h"
#include "stringutil.h" /* from RC_c_lib, is this a good idea? */
#include "debugutil.h" /* from RC_c_lib, now we're committed. */
#include "BurgersTypes.h"
#ifdef RC_CPP_VISIT_BUILD
#define dbprintf dbstream_printf
void dbstream_printf(int level, const char *fmt, ...);
#endif
std::string GetLibraryVersionString(const char *progname);
std::string GetLibraryVersionNumberString(void);
#define dbecho(level, msg...) fprintf(stderr, msg); dbprintf(level, msg)
/* ======================================== */
std::string INDENT(int i);
//=============================================================================
namespace paraDIS {
extern std::string doctext;
/*!
A Node is a segment endpoint.
*/
class Node {
// NOTE PROTECTED CONSTRUCTORS
protected:
/*!
===========================================
constructor
*/
Node(int16_t domain, int32_t nodeID, vector<float>location) {
init();
mDomainID = domain;
mNodeID = nodeID;
mLocation = location;
}
/*!
===========================================
constructor
*/
Node(int16_t domain, int32_t nodeID) {
init();
mDomainID = domain;
mNodeID = nodeID;
}
/*!
===========================================
Copy constructor, for decomposing/detaching operations
Removes neighbors, but otherwise duplicates the node.
When wrapping, use this but then change the location.
*/
Node(const Node &other) {
init();
uint32_t indexSave = mNodeIndex;
*this = other;
mNodeIndex = indexSave;
mNeighborSegments.clear();
mNeighborArms.clear();
mOriginal = const_cast<Node*>(&other);
mIsDuplicate = true;
return;
}
public:
/*!
===========================================
destructor
*/
virtual ~Node() {
printf ("Need to remove this node from the global list before destroying. Not implemented yet.\n");
abort();
return;
}
/*
Utility function for finding node in map
*/
static uint32_t Hash(uint32_t domain, uint32_t nodeID) {
return 10*1000*domain + nodeID;
}
/*
GetOrCreateNode: used during file parsing when we have the location.
This happens in the first line for each node.
If the node is already in the global map, then set its location.
Else, create it with the given location.
When first seen, nodes are arbitrarily assigned location <0,0,0>.
*/
static Node *GetOrCreateNode(uint32_t domainID, uint32_t nodeID, vector<float> location) {
Node *node = NULL;
if (mNodes[Hash(domainID,nodeID)].size()) {
node = mNodes[Hash(domainID,nodeID)][0];
node->mLocation = location;
} else {
node = new Node(domainID,nodeID,location);
mNodes[Hash(domainID,nodeID)].push_back(node);
mNodeVector.push_back(node);
if (mNodeVector.size()-1 != node->mNodeIndex) {
dbprintf(0, "Error: node vector side does not match node index...\n");
abort();
}
}
return node;
}
/*
GetOrCreateNode: used during file parsing when we do not have location.
This happens in the neighbor lines for each node.
If the node is already in the global map, then just return it.
Else, create it without setting location.
*/
static Node *GetOrCreateNode(uint32_t domainID, uint32_t nodeID) {
Node *node = NULL;
if (mNodes[Hash(domainID,nodeID)].size()) {
node = mNodes[Hash(domainID,nodeID)][0];
}
else {
node = new Node(domainID,nodeID);
mNodes[Hash(domainID,nodeID)].push_back(node);
mNodeVector.push_back(node);
}
return node;
}
/* For wrapping, detach/fuse, and other duplications: */
static Node *DuplicateNode(Node *other) {
Node *node = new Node(*other);
mNodes[Hash(other->mDomainID, other->mNodeID)].push_back(node);
return node;
}
/*!
===========================================
Clear all nodes
*/
static void Clear(void) {
for (map<uint32_t, vector<Node *> >::iterator nodepos = mNodes.begin(); nodepos != mNodes.end(); nodepos++) {
for (int i = nodepos->second.size(); i; i--) {
delete nodepos->second[i];
}
}
mNodes.clear();
mTraceFileBasename = "";
mTraceNodes.clear();
mNextNodeIndex = 0;
}
/*!
===========================================
initializer
*/
void init(void) {
mNodeIndex = mNextNodeIndex++; // "serial number"
// mLocation.resize(3,0); // do not do this! Empty is meaningful
mNodeType = 0;
mIsLoopNode = false;
mWrapped = false;
mIsDuplicate = false; // if true and location changed, we're wrapped
mOriginal = NULL; // the node we are a copy of
mSeen = false;
}
/*!
Accessor function for C API
*/
void GetLocation(float loc[3]) {
for (int i=0; i<3; i++)
loc[i] = mLocation[i];
return;
}
/*!
Accessor function for C API
*/
int GetNumNeighborSegments(void) { return mNeighborSegments.size();}
/*!
Accessor function for C API
*/
int32_t GetNodeSimulationDomain(void) { return mDomainID; }
/*!
Accessor function for C API
*/
int32_t GetNodeSimulationID(void) { return mNodeID; }
/*!
===========================================
Add the given arm to the list of arms to trace.
See WriteTraceFiles()
*/
static void TraceNode(int32_t nodeID){
mTraceNodes.push_back(nodeID);
}
/*!
===========================================
Give a name to distinguish tracefiles from files from other runs.
*/
static void SetTraceFileBasename(string basename) {
mTraceFileBasename = basename;
}
/*!
Compute the distance to another node
*/
double Distance(const Node &other, bool wrap=false) {
double dist[3] = {0}, sum=0;
int i=3; while (i--) {
dist[i] = mLocation[i] - other.mLocation[i];
if (wrap && fabs(dist[i]) > mBoundsSize[i]/2.0) {
dist[i] = mBoundsSize[i] - fabs(dist[i]);
}
sum += dist[i]*dist[i];
}
return sqrt(sum);
}
/*!
Accessor function.
The node will attempt to determine its own type.
*/
void ComputeNodeType(bool BCC);
/*!
Accessor function set the node type.
*/
void SetNodeType(int16_t itype) {
mNodeType = itype;
return;
}
/*!
Accessor function
*/
int16_t GetNodeType(void) { return mNodeType; }
/*!
Query
*/
bool IsTypeM(void) {
return mNodeType < 0;
}
bool IsTypeN(void) {
return mNodeType > 2;
}
bool IsLoopNode(void) {
return mIsLoopNode;
}
void SetLoopNode(bool tf = true) {
mIsLoopNode = tf;
}
/*!
Convert Node to string
*/
virtual std::string Stringify(int indent=0, bool shortform=true) const;
/*!
Handy wrapper
*/
static void PrintAllNodeTraces(string stepname);
/*
For each node that we want to trace, we write one set of files out before any decompositions, then one for decomposition step of each arm containing this node.
*/
void WriteTraceFiles(string stepname);
/*!
Assuming we have two neighbors or less, return the neighbor not passed to us
*/
class ArmSegment *GetOtherNeighbor (const class ArmSegment* n) {
if (mNeighborSegments.size() != 2) {
dbprintf(6, "Node::GetOtherNeighbor called but mNeighborSegments.size() != 2\n");
return NULL;
}
if (mNeighborSegments[0] == n) return mNeighborSegments[1];
if (mNeighborSegments[1] == n) return mNeighborSegments[0];
return NULL;
}
/*!
Accessor -- set the global bounds for every point
*/
static void SetBounds(rclib::Point<float> &min, rclib::Point<float> &max) {
mBoundsMin = min;
mBoundsMax = max;
mBoundsSize = max-min;
}
/*!
Remove one instance of the neighbor segment if present, leaving duplicates.
*/
void RemoveNeighborSegment(class ArmSegment *oldseg, bool doall = false) {
if (doall) {
mNeighborSegments.erase(remove(mNeighborSegments.begin(), mNeighborSegments.end(), oldseg), mNeighborSegments.end());
} else {
mNeighborSegments.erase(find(mNeighborSegments.begin(), mNeighborSegments.end(), oldseg));
}
//ComputeNodeType();
}
// used to remove an arm which has been decomposed
void RemoveNeighborArm(struct Arm *neighbor, bool doall = false) {
if ( find(mNeighborArms.begin(), mNeighborArms.end(), neighbor) == mNeighborArms.end()) {
return;
}
if (doall) {
mNeighborArms.erase(remove(mNeighborArms.begin(), mNeighborArms.end(), neighbor), mNeighborArms.end());
} else {
// assumes that the arm exists in mNeighborArms!
mNeighborArms.erase(find(mNeighborArms.begin(), mNeighborArms.end(), neighbor));
}
}
bool HasNeighbor(struct Arm *neighbor) {
return (find(mNeighborArms.begin(), mNeighborArms.end(), neighbor) != mNeighborArms.end());
}
/*!
Accessor function
*/
const std::vector< int> GetNeighborArmIDs(void) const;
/*!
Identify arms which cross over this node for DetachCrossArms();
Broken out separately to enable query and debug output
*/
vector<vector<Arm *> >IdentifyCrossArms(void);
/*!
Identify arms which cross over this node and glue them together.
Simplifies decomposition of arms.
*/
void DetachCrossArms(void);
string GetNodeIDString(void) const {
return str(boost::format("(%1%, %2%)")%mDomainID%mNodeID);
}
/*!
for debugging
*/
bool mSeen;
/*!
Connectivity to our neighboring arms.
Only used for terminal nodes.
*/
std::vector<struct Arm *> mNeighborArms;
/*!
all fullnodes in the data set. The map key is a hash of the domain and node ID for the node. Wrapped nodes are found by looking at mWrappedDouble in the found node.
*/
static std::map<uint32_t, vector<Node *> > mNodes;
static std::vector<Node *> mNodeVector; // purely for the C interface
/*!
Static member to keep track of subspace bounds for checking if we are in bounds or not
*/
static rclib::Point<float> mBoundsMin, mBoundsMax, mBoundsSize;
/* From NodeID: */
uint32_t mDomainID;
uint64_t mNodeID;
/* ------------ */
/* NEW to handle wrapping, we are going to have a "wrapped double" */
Node *mOriginal;
bool mIsDuplicate, mWrapped;
uint32_t mNodeIndex;
static uint32_t mNextNodeIndex ;
/*!
Absolute location in global space
*/
vector<float> mLocation;
/*!
Node Type is whether we are a butterfly, monster, or normal node (or a placeholder in a segment)
*/
int16_t mNodeType;
bool mIsLoopNode;
/*!
Connectivity to our neighboring nodes encapsulated in ArmSegments
*/
std::vector< class ArmSegment *> mNeighborSegments;
static string mTraceFileBasename;
static vector<uint32_t> mTraceNodes;
// statistics:
std::vector<uint32_t> mNumMonsterNodes;
}; /* end Node */
// ==================================================================
// ==================================================================
/*!
Arm segments contain neighbor relationships, encoded as pointers to nodes.
They also contain burgers and arm-type information for later analysis.
*/
class ArmSegment
{
public:
ArmSegment(const ArmSegment &other){
init();
uint32_t saved = mSegmentIndex;
*this = other;
mSegmentIndex = saved;
mIsDuplicate = true;
}
/* This is called by ReadNodeFromFile() and WrapBoundarySegments() */
ArmSegment(Node *ep0, Node *ep1, int burgersType) {
init();
// It turns out that *ep0 < *ep1 in terms of file order
mEndpoints[0] = ep0;
ep0->mNeighborSegments.push_back(this);
mEndpoints[1] = ep1;
ep1->mNeighborSegments.push_back(this);
mOriginalBurgersType = mBurgersType = burgersType;
return;
}
void init(void) {
mScrewType = SCREW_UNDEFINED;
mSegmentIndex = mNextSegmentIndex;
mNextSegmentIndex++;
mParentArm = NULL;
mLightTheFuseDistance = 0;
mOriginalBurgersType = mBurgersType = BCC_BURGERS_UNKNOWN;
mSeen = false;
int i=2; while (i--) {
mEndpoints[i] = NULL;
}
mIsDuplicate = false;
mWrapped = false;
}
/*!
Destructor
*/
~ArmSegment() {
mArmSegments.erase(mSegmentIndex); // mArmSegments[mSegmentIndex] = NULL;
//mNumArmSegments--;
int i=2; while (i--) {
if (mEndpoints[i]) {
delete mEndpoints[i]; // memory leak fix
}
}
return;
}
static void Clear(void) {
for (map<uint32_t, ArmSegment *>::iterator pos = mArmSegments.begin(); pos != mArmSegments.end(); ++pos) {
delete pos->second;
}
mArmSegments.clear();
mSegLen = 0;
mNumClassified = 0;
mNumDecomposed = 0;
mNumArmSegmentsMeasured = 0;
mNextSegmentIndex = 0;
}
/*!
Accessor function for C API
*/
int32_t GetNodeIndex(int num) {
return mEndpoints[num]->mNodeIndex;
}
int16_t GetBurgersType(void) const { return mBurgersType; }
int16_t GetOriginalBurgersType(void) const { return mOriginalBurgersType; }
/*!
Return the distance between the endpoints
*/
double GetLength(bool wrap=false) const {
return mEndpoints[0]->Distance(*mEndpoints[1], wrap);
}
/*!
Set the segment id to the next available global ID
*/
void SetIndex(uint32_t id) {
mSegmentIndex = id;
return;
}
/*!
accessor -- noop if not debug mode
*/
uint32_t GetID(void) {
return mSegmentIndex;
}
/*!
Accessor for MN type
*/
int16_t GetMNType(void) const;
/* Get the arm ID for the parent of this segment */
uint32_t GetArmID(void);
/* Get the metaarm ID for the parent of this segment */
uint32_t GetMetaArmID(void);
/* Get the metaarm Type for the parent of this segment */
uint16_t GetMetaArmType(void);
/*!
Replace one segment endpoint with the given new one.
Remove self from oldEP neighbors, and add to newEP neighbors.
*/
void ReplaceEndpoint(Node *oldEP, Node *newEP) {
if (mEndpoints[0] == oldEP) {
mEndpoints[0] = newEP;
} else if (mEndpoints[1] == oldEP) {
mEndpoints[1] = newEP;
} else {
throw string("Error in ReplaceEndpoint -- there is no such endpoint");
}
oldEP->RemoveNeighborSegment(this);
newEP->mNeighborSegments.push_back(this);
return;
}
/*!
Accessor function
*/
void GetNodeIndices(uint32_t indices[2]) {
indices[0] = mEndpoints[0]->mNodeIndex;
indices[1] = mEndpoints[1]->mNodeIndex;
}
/*!
convert ArmSegment to string
*/
std::string Stringify(int indent=0) const;
// specific to JSON files for blender ingestion
std::string BlenderRotationString(void) const;
/*!
return a vector ep1 - ep0
*/
vector<float> SegmentDirection(void) const;
/*!
Compute the mScrewType value
*/
int16_t ComputeScrewType(void);
int16_t GetScrewType(void) {
return ComputeScrewType();
}
static void SetScrewToleranceAngle(double angle) {
mScrewToleranceAngle = angle;
mScrewToleranceCosine = fabs(cos(angle)); // this is more useful computationally
}
// ========================
bool HasEndpoint(Node *ep) {
return (mEndpoints[0] == ep || mEndpoints[1] == ep);
}
/* Some segments connect a node at one edge of the subspace to a node outside the subspace. I think these are ok to draw as-is. But some segments connect a node to a node on the other side due to periodic boundary conditions. This segment needs to be "wrapped". For a "wrapped" segment, create a new "wrapped" node inside the subspace boundary by adding an entire boundary dimension as needed such that the distance to its neighbor is less than half the size of the bounds of the dataset. Create a new "wrapper segment" connecting the original node to the new "wrapped" node. This new wrapper segment will not contribute to any distance calculations; it's like a hyperwarp to the wrapped double node. Then change the original segment to connect from the wrapped node to its neighbor.
*/
// bool Wrap(const rclib::Point<float> &dataSize);
/*!
Accessor function.
\param epnum Must be 0 or 1
*/
Node *GetEndpoint(int epnum) const { return mEndpoints[epnum]; }
/*!
Common Accessor operation: we have one node ID, but we're looking to see what the other end of the segment is.
*/
Node *GetOtherEndpoint(const Node *node) const {
int idx = 0;
while (idx < 2) {
if (mEndpoints[idx] == node ) {
break;
}
++idx;
}
if (idx == 2) {
throw string("Error in GetOtherEndpoint: cannot find node corresponding to ")+node->Stringify(0);
}
return mEndpoints[1-idx];
}
/*!
purely for debugging
*/
int32_t mSegmentIndex;
/*!
If this segment is a duplicate, that's potentially useful to know for visualization as there will be two or more superposed segments.
*/
bool mWrapped, mIsDuplicate;
static double mSegLen;
static uint32_t mNumClassified, mNumBeforeDecomposition,
mNumDecomposed, mNumArmSegmentsMeasured;
static uint32_t mNextSegmentIndex;
static ArmSegment *mInitialLightTheFuseArmSegment;
static uint32_t mMaxLightTheFuseDistance;
uint32_t mLightTheFuseDistance;
/*!
The burgers-type is defined above.
*/
int16_t mBurgersType, mOriginalBurgersType;
/*!
The screw type is defined above and is derived from the burgers type.
*/
int16_t mScrewType;
static double mScrewToleranceAngle, // Meaning: angle deviation inside which a segment is 'screw' or 'edge'
mScrewToleranceCosine;// the computational twin derived from mScrewToleranceAngle and used in computations many times
static double SQRT3; // sqrt(3), used many times
/*!
The MN_type of the segment is set by its parent arm. See Arm struct for definitions, but it describes whether the segment is 200 or 111 and whether its parent arm has any monsters at either end.
*/
//int16_t mMNType;
/* The metaArm type that it belongs to. */
int16_t mMetaArmType;
/*!
Marker used for "once-through" operations like building arms that must look at every segment, but which will usually discover echo particular segment more than once.
*/
bool mSeen;
/* every segment has two endpoints */
Node * mEndpoints[2];
/*!
The global list of valid arm segments
*/
static std::map<uint32_t, ArmSegment *> mArmSegments;
struct Arm *mParentArm;
}; /* end ArmSegment */
//==============================================
/*!
Arms are conceptually a list of segments, but all we need to store is the two (or one, for a cycle) terminal ArmSegments for the arm, and the two (or one) terminal Nodes. This allows us to recreate the arm for drawing or analysis, but saves lots of memory. (Tradeoff of speed to get memory). The assumption is that traversing the arm will be fast enough and not done very often. If it starts using lots of time, we can always store more information if it makes it faster later.
Arms are used just for classifying nodes and segments and are not expected to be useful to the user of this library;
*/
struct Arm {
Arm() {
this->init();
}
~Arm() {
mArms[mArmID] = NULL;
}
void init(void) {
mArmType = ARM_UNKNOWN;
mArmLength=0;
mSeen=false;
mSeenInMeta=false;
mParentMetaArm=NULL;
mNumNormalSegments = 0;
mNumWrappedSegments = 0;
mTerminalSegments.clear();
mTerminalNodes.clear();
mDecomposing = false;
mExtendOrDetach = false;
mArmID = mArms.size();
mArms.push_back(this);
#if LINKED_LOOPS
mPartOfLinkedLoop=false;
mCheckedForLinkedLoop=false;
#endif
//mTraceArms.clear();
//mTraceDepth = 2;
}
// bookkeeping for e.g. timestep changes.
static void Clear(void) {
for (vector<Arm*>::iterator arm = mArms.begin(); arm != mArms.end(); arm++) {
if (*arm) delete *arm;
}
mArms.clear();
mTraceArms.clear();
mTraceDepth = 2;
mTraceFileBasename = "";
mLongestLength = 0.0;
mDecomposedLength = 0.0;
mNumDecomposed.clear();
mNumDestroyedInDetachment = 0;
mTotalArmLengthBeforeDecomposition = 0.0;
mTotalArmLengthAfterDecomposition = 0.0;
mThreshold = -1;
}
/*!
When one arm is gobbled up by another, the gobblee becomes ancestor to the gobbler
*/
void MakeAncestor(Arm *sourceArm) {
mAncestorArms.push_back(sourceArm->mArmID);
for (uint32_t a = 0; a < sourceArm->mAncestorArms.size(); a++) {
mAncestorArms.push_back(sourceArm->mAncestorArms[a]);
}
return;
}
/*!
Helper for DetachLoopFromNode and DetachAndFuse, does the detach part
*/
void ReplaceTerminalNode(Node *node, Node *replacement);
/*!
During decomposition, cross arms are removed from terminal nodes.
If the cross arm is a loop, then this is called to detach it.
*/
void DetachLoopFromNode(Node *node);
/*!
During decomposition, cross arms are removed from terminal nodes.
This actually detaches one such cross arm from a node, reusing the contents of other arm except the detached node.
*/
void DetachAndFuse(Node *node, Arm *other);
/*!
A helper for ExtendByArm() function.
*/
void ExtendBySegments(Arm *sourceArm, Node *sharedNode, bool reuseSegments);
/*!
A helper for Decompose() function.
*/
void ExtendByArm(Arm *sourceArm, Node *sharedNode, bool reuseSegments);
/* temporary helper to check mNodes and mSegments vs. old method */
void printNodes(void) const;
/* temporary helper to check mNodes and mSegments vs. old method */
void printSegments(void) const;
/* temporary helper to check mNodes and mSegments vs. old method */
void CheckNodesAndSegments(void) const;
/*!
Merge with neighbor arms.
*/
bool Decompose(int energyLevel);
/*!
create a string out of the arm
*/
std::string Stringify(int indent, bool shortform=true, bool allnodes=false) const ;
/*!
Give the exact Burgers type of its segments.
Return 0 if no terminal segments.
*/
int16_t GetBurgersType(void) const {
if (!mTerminalSegments.size() || !mTerminalSegments[0]) {
return BCC_BURGERS_UNKNOWN;
}
return mTerminalSegments[0]->GetBurgersType();
}
/*!
Give the original Burgers type of its segments.
All segment original burgers type will be the same for any given arm.
Return 0 if no terminal segments.
*/
int16_t GetOriginalBurgersType(void) const {
if (!mTerminalSegments.size() || !mTerminalSegments[0]) {
return BCC_BURGERS_UNKNOWN;
}
return mTerminalSegments[0]->GetOriginalBurgersType();
}
/* Get the metaarm ID for the parent of this arm */
uint32_t GetMetaArmID(void);
/* Get the metaarm Type for the parent of this arm */
uint16_t GetMetaArmType(void);
#if LINKED_LOOPS
/*!
A linked loop is defined as:
A) Two arms which have the same four-armed terminal nodes
OR
B) Three arms which all have the same three-armed terminal nodes.
*/
void CheckForLinkedLoops(void);
#endif
// =========================================================
/*!
Return vectors with pointers to all nodes and segments in arm,
computed in order from end to end. In a loop, the startNode is
repeated as first and last node.
wrapping is signified by a NULL node
*/
void GetNodesAndSegments(Node *startNode, vector<Node*>*outnodes, vector<ArmSegment*>*outsegs) const;
// =========================================================
// Returns number of nodes, not counting "NULL" nodes for wrapping
uint32_t GetNumNodes(void) {
return mNumNormalSegments ? mNumNormalSegments + mNumWrappedSegments + 1: 0;
}
// =========================================================
string GetSegmentsAsString(Node *startNode = NULL) {
string s;
vector<ArmSegment *> segments;
GetNodesAndSegments(startNode, NULL, &segments);
int seg = 0, numsegs = segments.size();
s += intToString(numsegs) + string(" segments: \n");
while (seg < numsegs) {
s += (string(" ") + segments[seg]->Stringify(0) + "\n");
++ seg;
}
return s;
}
double ComputeLength(void);
/*!
Classify the arm as one of NN, MN or MM, combined with 200 or 111...
*/
void Classify(void) ;
bool isTypeMM(void) const {
return mArmType == ARM_BCC_MM_111;
}
bool isTypeUnknown(void) const {
return mArmType == ARM_UNKNOWN;
}
bool isType111(void) {
return mTerminalSegments.size() && BurgTypeToBurgInfo(mTerminalSegments[0]->GetBurgersType()).energy == 1;
}
bool isHighEnergy(void) {
return mTerminalSegments.size() && BurgTypeToBurgInfo(mTerminalSegments[0]->GetBurgersType()).energy > 1;
}
Node *GetCommonNode(Arm *other) {
uint32_t myNode = mTerminalNodes.size();
while (myNode--) {
uint32_t otherNode = other->mTerminalNodes.size();
while (otherNode--) {
if (mTerminalNodes[myNode] == other->mTerminalNodes[otherNode]) {
return mTerminalNodes[myNode];
}
}
}
return NULL;
}
/*!
Return number of neighbor arms, not including this arm, but including duplicates
*/
uint32_t GetNumNeighborArms(void) {
uint32_t num = 0;
for (uint32_t node = 0; node < mTerminalNodes.size(); node++) {
for (uint32_t arm=0; arm < mTerminalNodes[node]->mNeighborArms.size(); arm++) {
if (mTerminalNodes[node]->mNeighborArms[arm] != this) {
num++;
}
}
}
return num;
}
/*!
Return nth neighbor arm, not including this but including duplicates
*/
Arm *GetNeighborArm (int num) {
for (uint32_t node = 0; node < mTerminalNodes.size(); node++) {
for (uint32_t arm=0; arm < mTerminalNodes[node]->mNeighborArms.size(); arm++) {
if (mTerminalNodes[node]->mNeighborArms[arm] != this) {
if (!num)
return mTerminalNodes[node]->mNeighborArms[arm];
else
num--;
}
}
}
return NULL;
}
/*!