TTK
Loading...
Searching...
No Matches
Graph.cpp
Go to the documentation of this file.
1#include "Graph.h"
2
3#include <iostream>
4#include <unordered_map>
5
6using namespace std;
7using namespace ttk;
8using namespace ftr;
9
10Graph::Graph() = default;
11
12Graph::~Graph() = default;
13
14std::string Graph::print(const int verbosity) const {
15 stringstream res;
16 if(verbosity >= 1) {
17 res << "Graph:" << endl;
18 res << "leaves: " << leaves_.size() << endl;
19 res << "nodes: " << nodes_.size() << endl;
20 res << "arcs: " << arcs_.size() << endl;
21 }
22
23 if(verbosity >= 2) {
24 res << "visible arcs: " << getNumberOfVisibleArcs() << endl;
25 }
26
27 if(verbosity >= 3) {
28 res << "Leaves: " << endl;
29 for(const auto &v : leaves_) {
30 res << get<0>(v) << " ";
31 }
32 res << endl;
33 }
34
35 if(verbosity >= 4) {
36 res << "Nodes:" << endl;
37 const idNode nbn = nodes_.size();
38 for(idNode i = 0; i < nbn; ++i) {
39 res << printNode(i) << endl;
40 }
41 res << "Arcs:" << endl;
42 const idSuperArc nba = arcs_.size();
43 for(idSuperArc i = 0; i < nba; ++i) {
44 res << printArc(i) << endl;
45 }
46 }
47
48 res << "visible " << getNumberOfVisibleArcs() << " / " << getNumberOfArcs()
49 << std::endl;
50
51 return res.str();
52}
53
54std::string Graph::printArc(const idSuperArc arcId) const {
55 std::stringstream res;
56 res << "a" << arcId << ":";
57 if(!arcs_[arcId].isVisible()) {
58 res << " hidden ";
59 }
60 if(arcs_[arcId].merged()) {
61 res << " merged in " << arcs_[arcId].mergedIn();
62 }
63 res << "(";
64 const idNode did = arcs_[arcId].getDownNodeId();
65 const idNode uid = arcs_[arcId].getUpNodeId();
66 if(did != nullNode) {
67 res << nodes_[did].getVertexIdentifier();
68 } else {
69 res << "X";
70 }
71 res << " - ";
72 if(uid != nullNode) {
73 res << nodes_[uid].getVertexIdentifier();
74 } else {
75 res << "X";
76 }
77 res << " ";
78 if(arcs_[arcId].isEmpty()) {
79 res << "--";
80 } else {
81 res << arcs_[arcId].getFirstReg() << ".." << arcs_[arcId].getLastReg();
82 }
83 if(arcs_[arcId].getEnd() != nullVertex) {
84 res << " > " << arcs_[arcId].getEnd();
85 }
86 res << ")";
87 return res.str();
88}
89
90std::string Graph::printNode(const idNode nodeId) const {
91 std::stringstream res;
92 res << "n" << nodeId << "v" << nodes_[nodeId].getVertexIdentifier() << ":[v";
93 const idSuperArc nbd = nodes_[nodeId].getNbDownArcs();
94 for(idSuperArc i = 0; i < nbd; ++i) {
95 res << " " << nodes_[nodeId].getDownArc(i);
96 }
97 res << " - ^";
98 const idSuperArc nbu = nodes_[nodeId].getNbUpArcs();
99 for(idSuperArc i = 0; i < nbu; ++i) {
100 res << " " << nodes_[nodeId].getUpArc(i);
101 }
102 res << "]";
103 return res.str();
104}
105
106std::string Graph::printVisit(const idVertex v) const {
107 std::stringstream res;
108 if(isArc(v))
109 res << " a: " << getArcId(v);
110 if(isNode(v))
111 res << " n: " << getNodeId(v);
112 return res.str();
113}
114
115// DEBUG function
116
117std::string Graph::printVisit() const {
118 std::stringstream res;
119 res << "Segmentation: " << std::endl;
120 for(idVertex s = 0; s < nbElmt_; ++s) {
121 res << s << " : ";
122 res << printVisit(s);
123 res << std::endl;
124 }
125 return res.str();
126}
127
128// initialization
129
131#ifndef TTK_ENABLE_KAMIKAZE
132 if(nbElmt_ == nullVertex) {
133 this->printErr("setNumberOfElmt not called before alloc in Graph");
134 }
135#endif
136 leaves_.reserve(nbElmt_);
137 nodes_.reserve(nbElmt_ * 2);
138 arcs_.reserve(nbElmt_ * 2);
139 segmentation_.resize(nbElmt_);
140 valUp_.resize(nbElmt_);
141 valDown_.resize(nbElmt_);
142
143#ifdef TTK_ENABLE_FTR_VERT_STATS
144 nbTouch_.resize(nbElmt_);
145 nbArcActif_.resize(nbElmt_);
146 avoided_ = 0;
147#endif
148}
149
151 // Consider max valence is 10
152 fillVector<SegmInfo>(segmentation_, SegmInfo{});
153 fillVector<valence>(valUp_, -1);
154 fillVector<valence>(valDown_, -1);
155#ifdef TTK_ENABLE_FTR_VERT_STATS
156 fillVector<idVertex>(nbTouch_, 0);
157 fillVector<idSuperArc>(nbArcActif_, 0);
158#endif
159}
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
std::size_t size() const
void reserve(const std::size_t &newSize, const bool fromOther=false)
idVertex nbElmt_
Allocation may depends on the number of vertices.
Definition FTRCommon.h:50
std::string printNode(const idNode nodeId) const
Definition Graph.cpp:90
idSuperArc getNumberOfArcs() const
Definition Graph.h:88
bool isArc(const idVertex v) const
Definition Graph.h:194
std::string printArc(const idSuperArc arcId) const
Definition Graph.cpp:54
std::string printVisit() const
Definition Graph.cpp:117
std::vector< valence > valUp_
Definition Graph.h:55
bool isNode(const idVertex v) const
Definition Graph.h:202
idSuperArc getArcId(const idVertex v) const
Definition Graph.h:214
~Graph() override
std::vector< valence > valDown_
Definition Graph.h:55
void init() override
Definition Graph.cpp:150
idNode getNodeId(const idVertex v) const
Definition Graph.h:210
void alloc() override
Definition Graph.cpp:130
std::string print(const int verbosity) const
Definition Graph.cpp:14
idSuperArc getNumberOfVisibleArcs() const
Definition Graph.h:92
long unsigned int idSuperArc
SuperArc index in vect_superArcs_.
SimplexId idVertex
Vertex index in scalars_.
unsigned int idNode
Node index in vect_nodes_.
The Topology ToolKit.