GPMWeightsSquaredInverseDistanceStrategy.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file GPMWeightsSquaredInverseDistanceStrategy.h
22 
23  \brief This class defines a class to calculates a weight for a GPM using Squared Inverse Distance strategy.
24 */
25 
26 // Terralib Includes
27 #include "../../datatype/SimpleData.h"
28 #include "../../graph/core/AbstractGraph.h"
29 #include "../../graph/core/Edge.h"
30 #include "../../graph/core/GraphMetadata.h"
31 #include "../../graph/core/Vertex.h"
32 #include "../../graph/iterator/MemoryIterator.h"
35 
38  double factor)
39  : m_normalize(normalize), m_a(a), m_factor(factor)
40 {
42 }
43 
46 
48 {
49  //create weight property
50  int weightIdx = createWeightAttribute(gpm);
51 
52  int nEdgeAttrs = gpm->getGraph()->getMetadata()->getEdgePropertySize();
53 
54  //check if the gpm has the distance attribute
55  int distanceIdx;
56  bool hasDistance = getDistanceAttributeIndex(gpm->getGraph()->getMetadata(), distanceIdx);
57 
58  //iterate over all vertex
62 
63  while(v)
64  {
65  std::set<int> neighbours = v->getSuccessors();
66  std::set<int>::iterator itNeighbours = neighbours.begin();
67 
68  //calculate weight
69  double tot = 0.;
70  std::vector<double> weightVec;
71 
72  while(itNeighbours != neighbours.end())
73  {
74  double weight = 0.;
75 
76  te::graph::Edge* e = g->getEdge(*itNeighbours);
77 
78  if(e)
79  {
80  //get distance attribute
81  if(hasDistance)
82  {
83  double distance = getDistanceAttributeValue(e, distanceIdx);
84 
85  if(distance != 0.)
86  weight += m_a * (1. / (distance * distance));
87  }
88 
89  //extra information calculate in TerraLib 4
90  //if (attr.WasNetworkObjectsDistanceComputed())
91  //if ((d_net = attr.NetworkObjectsDistance()) != 0.0)
92  //w += (params_.b_)*1/(d_net*d_net);
93 
94  //if (attr.WasNetworkMinimumPathComputed())
95  //if ((d_conn = attr.NetworkMinimumPath()) != 0.0)
96  //w += (params_.c_)*1/(d_conn*d_conn);
97  }
98 
99  if(weight == 0.)
100  weight = 1.;
101 
102  weightVec.push_back(weight * m_factor);
103 
104  tot += weight;
105 
106  ++itNeighbours;
107  }
108 
109  itNeighbours = neighbours.begin();
110  std::vector<double>::iterator itWeights = weightVec.begin();
111 
112  while(itNeighbours != neighbours.end())
113  {
114  te::graph::Edge* e = g->getEdge(*itNeighbours);
115 
116  if(e)
117  {
118  double weight = *itWeights;
119 
120  if(m_normalize && tot != 0.)
121  {
122  weight = weight / tot;
123  }
124 
125  e->setAttributeVecSize(nEdgeAttrs);
126 
128  }
129 
130  ++itWeights;
131  ++itNeighbours;
132  }
133 
134  v = it->getNextVertex();
135  }
136 }
virtual te::graph::Vertex * getFirstVertex()
It returns a pointer to the first vertex element of a graph.
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the edge elements.
Definition: Edge.cpp:86
virtual te::graph::Vertex * getNextVertex()
It returns a pointer to the next vertex element of a graph.
This class defines a Generalized Proximity Matrix.
bool getDistanceAttributeIndex(te::graph::GraphMetadata *gm, int &index)
Function used to get the distance attribute index.
GPMWeightsStrategyType m_type
Weight Type.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
int createWeightAttribute(GeneralizedProximityMatrix *gpm)
Added to the edge a new attribute for weight information and return the attr index.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
double getDistanceAttributeValue(te::graph::Edge *e, const int index)
Function used to get the distance value from a edge element.
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
GPMWeightsSquaredInverseDistanceStrategy(bool normalize, double a=1.0, double factor=1.0)
Default constructor.
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
virtual ~GPMWeightsSquaredInverseDistanceStrategy()
Virtual destructor.
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
This class defines a class to calculates a weight for a GPM using Squared Inverse Distance strategy...
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Edge.cpp:91
This class defines the GPM class.