ExactDGPMethod.cpp
Go to the documentation of this file.
1 // ExactMethodForDGP.cpp: implementation of the CExactDGPMethod class.
2 //
4 #include "ExactDGPMethod.h"
5 //#include <windows.h> // dsy: comment out.
6 //#include <gl/GL.h> // dsy: comment out.
7 //#include <gl/GLU.h> // dsy: comment out.
8 //#pragma comment(lib, "opengl32.lib") // dsy: comment out.
9 //#pragma comment(lib, "glu32.lib") // dsy: comment out.
10 #include <fstream>
11 #include <iterator>
12 #include <cassert>
13 #include "Parameters.h"
14 using namespace std;
15 
16 // dsy - - -
17 #ifndef max
18 #define max(a,b) (((a) > (b)) ? (a) : (b))
19 #endif
20 #ifndef min
21 #define min(a,b) (((a) < (b)) ? (a) : (b))
22 #endif
23 /*template<class _BidIt> inline
24 void _Reverse(_BidIt _First, _BidIt _Last, bidirectional_iterator_tag)
25 {
26  // reverse elements in [_First, _Last), bidirectional iterators
27  for (; _First != _Last && _First != --_Last; ++_First)
28  ::std:: iter_swap(_First, _Last);
29 }
30 template<class _BidIt> inline
31 void reverse(_BidIt _First, _BidIt _Last)
32 {
33  // reverse elements in [_First, _Last)
34  //_DEBUG_RANGE(_First, _Last);
35  _Reverse(_First._Unchecked(), _Unchecked(_Last), _Iter_cat(_First));
36 }*/
37 template<class _BidIt> inline
38 void reverse(_BidIt _First, _BidIt _Last)
39 {
40  // reverse elements in [_First, _Last), bidirectional iterators
41  for (; _First != _Last && _First != --_Last; ++_First)
42  ::std:: iter_swap(_First, _Last);
43 }
44 // dsy - - -
45 
47 // Construction/Destruction
49 
51 {
53  m_InfoAtVertices.resize(model.GetNumOfVerts());
54 }
55 
56 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, int source) : CDistanceApproach(inputModel, source)
57 {
58  m_nameOfAlgorithm = "Exact";
59 }
60 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, int source, int destination) : CDistanceApproach(inputModel, source, destination)
61 {
62  m_nameOfAlgorithm = "Exact";
63 }
64 
65 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, int source, double R) : CDistanceApproach(inputModel, source, R)
66 {
67  m_nameOfAlgorithm = "Exact";
68 }
69 
70 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, const set<int> &indexOfSourceVerts) : CDistanceApproach(inputModel, indexOfSourceVerts)
71 {
72  m_nameOfAlgorithm = "Exact";
73 }
74 
75 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, const set<int> &indexOfSourceVerts, double R) : CDistanceApproach(inputModel, indexOfSourceVerts, R)
76 {
77  m_nameOfAlgorithm = "Exact";
78 }
79 
80 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, const set<int> &indexOfSourceVerts, const set<int> &destinations) : CDistanceApproach(inputModel, indexOfSourceVerts, destinations)
81 {
82  m_nameOfAlgorithm = "Exact";
83 }
84 
85 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, const map<int, double> &indexOfSourceVerts) : CDistanceApproach(inputModel, indexOfSourceVerts)
86 {
87  m_nameOfAlgorithm = "Exact";
88 }
89 
90 CExactDGPMethod::CExactDGPMethod(const CRichModel& inputModel, const map<int, double> &indexOfSourceVerts, const set<int> &destinations) : CDistanceApproach(inputModel, indexOfSourceVerts, destinations)
91 {
92  m_nameOfAlgorithm = "Exact";
93 }
94 
95 vector<EdgePoint> CExactDGPMethod::BacktraceShortestPath(int end) const
96 {
97  if (m_InfoAtVertices[end].birthTimeForCheckingValidity == -1
98  || m_InfoAtVertices[end].indexOfAncestor == -1)
99  {
100  assert(model.GetNumOfComponents() != 1 || model.Neigh(end).empty());
101  return vector<EdgePoint>();
102  }
103  vector<EdgePoint> path;
104  vector<int> vertexNodes;
105  int index = end;
106  vertexNodes.push_back(index);
107  while (m_InfoAtVertices[index].indexOfDirectParent != -1)
108  {
109  int indexOfParent = m_InfoAtVertices[index].indexOfDirectParent;
110  if (m_InfoAtVertices[index].fParentIsPseudoSource)
111  {
112  index = indexOfParent;
113  }
114  else
115  {
116  index = m_InfoAtVertices[index].indexOfRootVertOfDirectParent;
117  }
118  vertexNodes.push_back(index);
119  };
120  int indexOfSourceVert = index;
121 
122  for (int i = 0; i < (int)vertexNodes.size() - 1; ++i)
123  {
124  int lastVert = vertexNodes[i];
125  path.push_back(EdgePoint(lastVert));
126  if (m_InfoAtVertices[lastVert].fParentIsPseudoSource)
127  {
128  continue;
129  }
130  int parentEdgeIndex = m_InfoAtVertices[lastVert].indexOfDirectParent;
131  int edgeIndex = model.Edge(parentEdgeIndex).indexOfReverseEdge;
132  pair<double, double> coord(model.GetNew2DCoordinatesByReversingCurrentEdge(parentEdgeIndex, model.Edge(parentEdgeIndex).coordOfOppositeVert));
133 
134  double proportion = 1 - m_InfoAtVertices[lastVert].entryProp;
135  while (true)
136  {
137  path.push_back(EdgePoint(edgeIndex, proportion));
138  if (model.Edge(edgeIndex).indexOfOppositeVert == vertexNodes[i + 1])
139  break;
140  double oldProprotion = proportion;
141  proportion = model.ProportionOnLeftEdgeByImage(edgeIndex, coord, oldProprotion);
142  if (abs(proportion - 1) < 1e-2)
143  {
144  vector<EdgePoint> path2 = BacktraceShortestPath(model.Edge(edgeIndex).indexOfOppositeVert);
145  reverse(path.begin(), path.end());
146  copy(path.begin(), path.end(), back_inserter(path2));
147  return path2;
148  }
149  else if (proportion >= 0 && proportion <= 1)
150  {
151  proportion = max(proportion, 0);
153  edgeIndex = model.Edge(edgeIndex).indexOfLeftEdge;
154  //rightLen = disToAngle;
155  }
156  else
157  {
158  if (model.IsExtremeEdge(edgeIndex))
159  {
160  int left = model.Edge(edgeIndex).indexOfLeftVert;
161  int right = model.Edge(edgeIndex).indexOfRightVert;
162  if (GetDistanceField()[left] < GetDistanceField()[right])
163  {
164  vector<EdgePoint> path2 = BacktraceShortestPath(left);
165  reverse(path.begin(), path.end());
166  copy(path.begin(), path.end(), back_inserter(path2));
167  return path2;
168  }
169  else
170  {
171  vector<EdgePoint> path2 = BacktraceShortestPath(right);
172  reverse(path.begin(), path.end());
173  copy(path.begin(), path.end(), back_inserter(path2));
174  return path2;
175  }
176  }
177  else
178  {
179  proportion = model.ProportionOnRightEdgeByImage(edgeIndex, coord, oldProprotion);
180  proportion = max(proportion, 0);
181  proportion = min(proportion, 1);
183  edgeIndex = model.Edge(edgeIndex).indexOfRightEdge;
184  }
185  }
186  };
187  }
188  path.push_back(EdgePoint(indexOfSourceVert));
189  reverse(path.begin(), path.end());
190  return path;
191 }
192 
193 int CExactDGPMethod::GetAncestor(int vertex) const
194 {
195  return m_InfoAtVertices[vertex].indexOfAncestor;
196 }
197 
199 {
200  m_memory = ((double)model.GetNumOfVerts() * sizeof (InfoAtVertex)) / 1024 / 1024;
201  for (int i = 0; i < m_scalarField.size(); ++i)
202  {
203  m_scalarField[i] = m_InfoAtVertices[i].disUptodate;
204  }
206 }
207 
209 {
210  //Do nothing...
211 }
CExactDGPMethod(const CRichModel &inputModel, int source)
virtual void Initialize()
pair< double, double > GetNew2DCoordinatesByRotatingAroundLeftChildEdge(int edgeIndex, const pair< double, double > &input2DCoordinates) const
Definition: RichModel.cpp:706
vector< double > m_scalarField
#define max(a, b)
virtual void CollectExperimentalResults()
double ProportionOnLeftEdgeByImage(int edgeIndex, const pair< double, double > &coord, double proportion) const
Definition: RichModel.cpp:691
const vector< double > & GetDistanceField() const
void reverse(_BidIt _First, _BidIt _Last)
pair< double, double > coordOfOppositeVert
Definition: RichModel.h:30
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
const CEdge & Edge(int edgeIndex) const
Definition: RichModel.cpp:669
double ProportionOnRightEdgeByImage(int edgeIndex, const pair< double, double > &coord, double proportion) const
Definition: RichModel.cpp:698
virtual void CollectExperimentalResults()=0
pair< double, double > GetNew2DCoordinatesByRotatingAroundRightChildEdge(int edgeIndex, const pair< double, double > &input2DCoordinates) const
Definition: RichModel.cpp:712
int indexOfReverseEdge
Definition: RichModel.h:26
int GetNumOfComponents() const
Definition: RichModel.cpp:619
virtual vector< EdgePoint > BacktraceShortestPath(int end) const
bool IsExtremeEdge(int edgeIndex) const
Definition: RichModel.cpp:659
const vector< pair< int, double > > & Neigh(int root) const
Definition: RichModel.cpp:674
int GetNumOfVerts() const
Definition: BaseModel.h:80
virtual void Initialize()=0
int indexOfOppositeVert
Definition: RichModel.h:23
pair< double, double > GetNew2DCoordinatesByReversingCurrentEdge(int edgeIndex, const pair< double, double > &input2DCoordinates) const
Definition: RichModel.cpp:720
virtual void Dispose()
vector< InfoAtVertex > m_InfoAtVertices
#define min(a, b)
const CRichModel & model
virtual int GetAncestor(int vIndex) const


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:41