2 //######################################################################################################################
4 #include "../include/lattice.h"
7 using namespace LatticeGraph;
9 //######################################################################################################################
10 //#################################### Ceiling Cat & Basement Cat ###############################################
11 //######################################################################################################################
17 this->getVortices().clear();
18 this->getEdges().clear();
21 //######################################################################################################################
22 //#################################### Get stuff ###############################################
23 //######################################################################################################################
25 std::vector< std::shared_ptr<Node> >& Lattice::getVortices(){
26 return this->vortices;
29 std::shared_ptr<Node> Lattice::getVortexIdx(unsigned int idx){
30 return getVortices().at(idx);
34 * Gets the location of the Node with UID uid.
36 unsigned int Lattice::getVortexIdxUid(unsigned int uid){
37 for (size_t ii=0; ii< getVortices().size(); ++ii){
38 if(this->Lattice::getVortexIdx(ii)->getUid()== uid){
46 * Gets the the Node with UID uid. Assumes Node exists.
48 std::shared_ptr<Node> Lattice::getVortexUid(unsigned int uid){
49 for (std::shared_ptr<Node> n : this->Lattice::getVortices()){
50 if(n->getUid()== uid){
54 return std::shared_ptr<Node>();
57 double Lattice::getVortexDistance(std::shared_ptr<Node> n1, std::shared_ptr<Node> n2){
58 return sqrt(pow(n1->getData().getCoords().x - n2->getData().getCoords().x,2)
59 + pow(n1->getData().getCoords().y - n2->getData().getCoords().y,2));
62 double Lattice::getVortexDistanceD(std::shared_ptr<Node> n1, std::shared_ptr<Node> n2){
63 return sqrt(pow(n1->getData().getCoordsD().x - n2->getData().getCoordsD().x,2)
64 + pow(n1->getData().getCoordsD().y - n2->getData().getCoordsD().y,2));
67 std::shared_ptr<Edge> Lattice::getEdgeIdx(unsigned int idx){
68 return getEdges().at(idx);
72 * Gets the location of the Edge with UID uid.
74 unsigned int Lattice::getEdgeIdxUid(unsigned int uid){
75 for (size_t ii=0; ii< getEdges().size(); ++ii){
76 if(this->Lattice::getEdgeIdx(ii)->getUid()== uid){
84 * Gets the the Edge with UID uid. Assumes Node exists.
86 std::shared_ptr<Edge> Lattice::getEdgeUid(unsigned int uid){
87 for (std::shared_ptr<Edge> e : this->Lattice::getEdges()){
88 if(e->getUid()== uid){
95 std::vector< std::shared_ptr<Edge> >& Lattice::getEdges(){
99 //######################################################################################################################
100 //#################################### Set stuff ###############################################
101 //######################################################################################################################
103 void Lattice::setVortex(unsigned int idx, std::shared_ptr<Node> n){
104 this->Lattice::getVortices().at(idx)=(n);
107 void Lattice::setEdge(unsigned int idx, std::shared_ptr<Edge> e){
108 this->Lattice::getEdges().at(idx)=(e);
111 //######################################################################################################################
112 //#################################### + stuff ###############################################
113 //######################################################################################################################
116 void Lattice::createEdges(unsigned int radius){
117 std::shared_ptr<Edge> e;
119 for(size_t ii=0; ii< this->Lattice::getVortices().size(); ++ii){
120 //std::cout << "Got here ii " << ii << std::endl;
121 for(size_t jj=ii+1; jj < this->Lattice::getVortices().size(); ++jj){
122 dist = Lattice::getVortexDistance(this->getVortexIdx(ii),this->getVortexIdx(jj));
124 //std::cout << "Got here jj " << jj << std::endl;
125 e.reset(new Edge ( this->getVortexIdx(ii), this->getVortexIdx(jj) ));
127 this->Lattice::addEdge(e,this->getVortexIdx(ii),this->getVortexIdx(jj));
132 void Lattice::createEdges(double radius){
133 std::shared_ptr<Edge> e;
135 for(size_t ii=0; ii< this->Lattice::getVortices().size(); ++ii){
136 //std::cout << "Got here ii " << ii << std::endl;
137 for(size_t jj=ii+1; jj < this->Lattice::getVortices().size(); ++jj){
138 dist = Lattice::getVortexDistance(this->getVortexIdx(ii),this->getVortexIdx(jj));
139 if( dist < radius ) {
140 //std::cout << "Got here jj " << jj << std::endl;
141 e.reset(new Edge ( this->getVortexIdx(ii), this->getVortexIdx(jj) ));
143 this->Lattice::addEdge(e,this->getVortexIdx(ii),this->getVortexIdx(jj));
149 void Lattice::addVortex(std::shared_ptr<Node> n){
150 this->Lattice::getVortices().push_back((n));
153 void Lattice::addEdge(std::shared_ptr<Edge> e){
154 this->addEdge(e, e->getVortex(0).lock(), e->getVortex(1).lock());
157 void Lattice::addEdge(std::shared_ptr<Edge> e, std::shared_ptr<Node> n1, std::shared_ptr<Node> n2){
158 this->Lattice::getEdges().push_back(e);
159 std::weak_ptr<Edge> e1 = e;
160 std::weak_ptr<Edge> e2 = e;
165 //######################################################################################################################
166 //#################################### - stuff ###############################################
167 //######################################################################################################################
169 void Lattice::removeVortex(std::shared_ptr<Node> n){
170 for(std::weak_ptr<Edge> e : n->getEdges()){
172 std::cout << "UID: Removing Vortex{" << n->getUid() <<"}." << std::endl;
173 this->removeEdge(e.lock());
174 this->Lattice::getVortices().erase(this->Lattice::getVortices().begin() + this->getVortexIdxUid(n->getUid()));
177 std::cout << "Cannot remove UID:Edge{"<< e.lock()->getUid() << "}, does not exist." << std::endl;
182 void Lattice::removeVortexUid(unsigned int uid){
183 auto vtx = this->getVortexUid(uid);
185 this->Lattice::removeVortex(vtx);
188 std::cout << "Cannot remove UID:Vortex{"<< uid << "}, does not exist." << std::endl;
192 void Lattice::removeVortexIdx(unsigned int idx){
193 auto vtx = this->getVortexIdx(idx);
195 this->Lattice::removeVortex(vtx);
198 std::cout << "Cannot remove IDX:Vortex["<< idx << "], does not exist." << std::endl;
202 void Lattice::removeEdge(std::shared_ptr<Edge> e){
203 std::cout << "Removing Edge{" << e->getUid() <<"} connecting Node{" << e->getVortex(0).lock()->getUid() << "} and Node{" << e->getVortex(1).lock()->getUid() << "}." << std::endl;
204 e->getVortex(0).lock()->removeEdgeUid(e->getUid());
205 e->getVortex(1).lock()->removeEdgeUid(e->getUid());
206 this->Lattice::getEdges().erase(this->Lattice::getEdges().begin() + this->Lattice::getEdgeIdxUid(e->getUid()));
209 void Lattice::removeEdgeIdx(unsigned int idx){
210 std::weak_ptr<Edge> e = this->getEdgeIdx(idx);
211 if (auto el = e.lock()) {
212 this->Lattice::removeEdge(el);
215 std::cout << "Cannot remove IDX:Edge[" << idx << "], does not exist." << std::endl;
219 void Lattice::removeEdgeUid(unsigned int uid) {
220 std::weak_ptr<Edge> e = this->getEdgeUid(uid);
221 if (auto el = e.lock()) {
222 this->Lattice::removeEdge(el);
225 std::cout << "Cannot remove UID:Edge{" << uid << "}, does not exist." << std::endl;
229 void Lattice::removeEdge(std::shared_ptr<Node> n1, std::shared_ptr<Node> n2){
230 std::weak_ptr<Edge> e = this->Lattice::isConnected(n1,n2);
232 this->Lattice::removeEdge(e.lock());
235 std::cout << "Node{" << n1->getUid() << "} and Node{" << n2->getUid() << "} were unconnected." << std::endl;
240 void Lattice::removeEdges(std::shared_ptr<Node> n1){
245 void Lattice::createVortex(double posx, double posy, int winding){
249 void Lattice::destroyVortex(unsigned int uid){
250 this->Lattice::getVortexUid(uid);
255 //######################################################################################################################
256 //#################################### Generate stuff ###############################################
257 //######################################################################################################################
260 * Create adjacency matrix
262 void Lattice::genAdjMat(unsigned int *mat){
264 idx1 = 0; idx2 = 0; idx=0;
265 for(std::shared_ptr<Node> n : this->Lattice::getVortices()){
266 idx1=this->getVortexIdxUid(n->getUid());
267 for(std::weak_ptr<Edge> e : n->getEdges()){
268 idx2 = this->getVortexIdxUid(n->getConnectedNode(e.lock())->getUid());
269 //std::cout << "this=" << n->getUid() << " connected=" << n->getConnectedNode(e.lock())->getUid() << std::endl;
270 idx = idx1*this->Lattice::getVortices().size() + idx2;
271 //std::cout << "idx1=" << idx1 << " idx2=" << idx2 << " idx=" << idx << "\n" << std::endl;
277 void Lattice::genAdjMat(double *mat){
279 idx1 = 0; idx2 = 0; idx=0;
280 for(std::shared_ptr<Node> n : this->Lattice::getVortices()){
281 idx1=this->getVortexIdxUid(n->getUid());
282 for(std::weak_ptr<Edge> e : n->getEdges()){
283 idx2 = this->getVortexIdxUid(n->getConnectedNode(e.lock())->getUid());
284 //std::cout << "this=" << n->getUid() << " connected=" << n->getConnectedNode(e.lock())->getUid() << std::endl;
285 idx = idx1*this->Lattice::getVortices().size() + idx2;
286 //std::cout << "idx1=" << idx1 << " idx2=" << idx2 << " idx=" << idx << "\n" << std::endl;
287 mat[idx] = this->Lattice::getVortexDistance(n, this->getVortexIdx(idx2));
294 * Outputs adjacency matrix in format for copy/paste into Mathematica.
296 void Lattice::adjMatMtca(unsigned int *mat){
297 unsigned int size = this->Lattice::getVortices().size();
299 for(size_t ii = 0; ii < size; ++ii){
301 for(size_t jj = 0; jj < size; ++jj){
302 std::cout << mat[ii*size + jj];
310 std::cout << std::endl;
312 std::cout << "}" << std::endl;
314 void Lattice::adjMatMtca(double *mat){
315 unsigned int size = this->Lattice::getVortices().size();
317 for(size_t ii = 0; ii < size; ++ii){
319 for(size_t jj = 0; jj < size; ++jj){
320 std::cout << mat[ii*size + jj];
328 std::cout << std::endl;
330 std::cout << "}" << std::endl;
333 //######################################################################################################################
334 //#################################### Check stuff ###############################################
335 //######################################################################################################################
337 std::weak_ptr<Edge> Lattice::isConnected(std::shared_ptr<Node> n1, std::shared_ptr<Node> n2){
339 if(n1->getUid() != n2->getUid()){
340 for(std::weak_ptr<Edge> e1 : n1->getEdges()){
341 if(e1.lock()->isMember(n2)){
346 return std::weak_ptr<Edge> ();
349 //######################################################################################################################
350 //#################################### Modify stuff ###############################################
351 //######################################################################################################################
353 void Lattice::swapIdxUid(unsigned int uid1, unsigned int uid2) {
354 Lattice::swapIdx(this->getVortexIdxUid(uid1),this->getVortexIdxUid(uid2));
356 void Lattice::swapIdx(unsigned int idx1, unsigned int idx2) {
357 std::swap(this->getVortices().at(idx1),this->getVortices().at(idx2));
359 //void Lattice::swapVort(std::shared_ptr<Node> v1, std::shared_ptr<Node> v2) {
363 //######################################################################################################################