From 42c2ccd11ba20a1ae396b6d64caaef68d8506ca0 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Tue, 13 Sep 2022 23:43:54 +0200 Subject: Some code simplification. --- rbmp.cpp | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/rbmp.cpp b/rbmp.cpp index 989509f..1d0c47e 100644 --- a/rbmp.cpp +++ b/rbmp.cpp @@ -29,31 +29,28 @@ public: class HalfEdge { private: - unsigned index; Vertex* tail; Vertex* head; public: double oldX; double X; - double w; - void setIndex(unsigned i) {index = i;} - unsigned getIndex() const {return index;} + double weight; void setVertices(Vertex& vt, Vertex& vh) { tail = &vt; head = &vh; } - Vertex* getHead() { - return head; + Vertex& getHead() { + return *head; } - Vertex* getTail() { - return tail; + Vertex& getTail() { + return *tail; } - const Vertex* getHead() const { - return head; + const Vertex& getHead() const { + return *head; } - const Vertex* getTail() const { - return tail; + const Vertex& getTail() const { + return *tail; } }; @@ -70,8 +67,6 @@ public: blueVertices[i].setIndex(i); } for (unsigned i = 0; i < redEdges.size(); i++) { - redEdges[i].setIndex(i); - blueEdges[i].setIndex(i); Vertex& blueVertex = blueVertices[(i % (2 * n)) / 2 + n * (((i + 2*n) / 4) / n)]; Vertex& redVertex = redVertices[(1+(i % (2 * n))) / 2 + (n + 1) * ((i / 4) / (n))]; redEdges[i].setVertices(redVertex, blueVertex); @@ -83,28 +78,26 @@ public: redEdges[i].oldX = 0; blueEdges[i].oldX = 0; double w = r.variate(1); - redEdges[i].w = w; - blueEdges[i].w = w; + redEdges[i].weight = w; + blueEdges[i].weight = w; } } void propagateBeliefs() { for (HalfEdge& e : redEdges) { double Xt = std::numeric_limits::infinity(); - const Vertex* head = e.getHead(); - for (const HalfEdge* en : head->neighbors) { - if (e.getTail()->getIndex() != en->getHead()->getIndex()) { - Xt = std::min(en->w - en->oldX, Xt); + for (const HalfEdge* en : e.getHead().neighbors) { + if (e.getTail().getIndex() != en->getHead().getIndex()) { + Xt = std::min(en->weight - en->oldX, Xt); } } e.X = Xt; } for (HalfEdge& e : blueEdges) { double Xt = std::numeric_limits::infinity(); - const Vertex* head = e.getHead(); - for (const HalfEdge* en : head->neighbors) { - if (e.getTail()->getIndex() != en->getHead()->getIndex()) { - Xt = std::min(en->w - en->oldX, Xt); + for (const HalfEdge* en : e.getHead().neighbors) { + if (e.getTail().getIndex() != en->getHead().getIndex()) { + Xt = std::min(en->weight - en->oldX, Xt); } } e.X = Xt; @@ -128,11 +121,11 @@ int main() { } for (unsigned i = 0; i < G.blueEdges.size(); i++) { - std::cout << G.blueEdges[i].getTail()->getIndex() << " " << G.blueEdges[i].getHead()->getIndex() + G.blueVertices.size() << std::endl; + std::cout << G.blueEdges[i].getTail().getIndex() << " " << G.blueEdges[i].getHead().getIndex() + G.blueVertices.size() << std::endl; } for (unsigned i = 0; i < G.blueEdges.size(); i++) { if (G.blueEdges[i].X + G.redEdges[i].X >= 0) { - std::cout << G.blueEdges[i].getTail()->getIndex() << " " << G.blueEdges[i].getHead()->getIndex() + G.blueVertices.size() << std::endl; + std::cout << G.blueEdges[i].getTail().getIndex() << " " << G.blueEdges[i].getHead().getIndex() + G.blueVertices.size() << std::endl; } } -- cgit v1.2.3-70-g09d2