All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
37  m_normalize(normalize),
38  m_a(a),
39  m_factor(factor)
40 {
42 }
43 
45 {
46 }
47 
49 {
50  //create weight property
51  int weightIdx = createWeightAttribute(gpm);
52 
53  int nEdgeAttrs = gpm->getGraph()->getMetadata()->getEdgePropertySize();
54 
55  //check if the gpm has the distance attribute
56  int distanceIdx;
57  bool hasDistance = getDistanceAttributeIndex(gpm->getGraph()->getMetadata(), distanceIdx);
58 
59  //iterate over all vertex
63 
64  while(v)
65  {
66  std::set<int> neighbours = v->getSuccessors();
67  std::set<int>::iterator itNeighbours = neighbours.begin();
68 
69  //calculate weight
70  double tot = 0.;
71  std::vector<double> weightVec;
72 
73  while(itNeighbours != neighbours.end())
74  {
75  double weight = 0.;
76 
77  te::graph::Edge* e = g->getEdge(*itNeighbours);
78 
79  if(e)
80  {
81  //get distance attribute
82  if(hasDistance)
83  {
84  double distance = getDistanceAttributeValue(e, distanceIdx);
85 
86  if(distance != 0.)
87  weight += m_a * (1. / (distance * distance));
88  }
89 
90  //extra information calculate in TerraLib 4
91  //if (attr.WasNetworkObjectsDistanceComputed())
92  //if ((d_net = attr.NetworkObjectsDistance()) != 0.0)
93  //w += (params_.b_)*1/(d_net*d_net);
94 
95  //if (attr.WasNetworkMinimumPathComputed())
96  //if ((d_conn = attr.NetworkMinimumPath()) != 0.0)
97  //w += (params_.c_)*1/(d_conn*d_conn);
98  }
99 
100  if(weight == 0.)
101  weight = 1.;
102 
103  weightVec.push_back(weight * m_factor);
104 
105  tot += weight;
106 
107  ++itNeighbours;
108  }
109 
110  itNeighbours = neighbours.begin();
111  std::vector<double>::iterator itWeights = weightVec.begin();
112 
113  while(itNeighbours != neighbours.end())
114  {
115  te::graph::Edge* e = g->getEdge(*itNeighbours);
116 
117  if(e)
118  {
119  double weight = *itWeights;
120 
121  if(m_normalize && tot != 0.)
122  {
123  weight = weight / tot;
124  }
125 
126  e->setAttributeVecSize(nEdgeAttrs);
127 
129  }
130 
131  ++itWeights;
132  ++itNeighbours;
133  }
134 
135  v = it->getNextVertex();
136  }
137 }
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.
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.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
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
This class defines a an Abstract class to calculates a weight for a GPM.
virtual int getEdgePropertySize()
Used to verify the number of properties associated to edge elements.
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.