-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
177 lines (137 loc) · 3.84 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Mesh.h"
#include "Painter.h"
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <Inventor/Win/SoWin.h>
#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCone.h>
using namespace std;
int getRandVertexPoint(Mesh* mesh);
float getDistance(float* refCoord, float* targetCoord);
float* Dijkstra(float** M, int src, int targ, int N, int* prev);
int minDistance(float* dist, bool* marked, int N);
int main(int, char** argv)
{
srand(time(NULL)); //to get random two vertices
//Coin3D Initializations
HWND window = SoWin::init(argv[0]);
SoWinExaminerViewer* viewer = new SoWinExaminerViewer(window);
SoSeparator* root = new SoSeparator;
root->ref();
Mesh* mesh = new Mesh();
Painter* painter = new Painter();
mesh->loadOff("man0.off");
int N = mesh->verts.size();
float **M;
float** M_print;
int* prev;
prev = new int[N];
M = new float* [N];
M_print = new float* [N]; //we will save this matrix after filling it
//memory allocation
for (int i = 0; i < N; i++) {
M[i] = new float[N];
M_print[i] = new float[N];
}
//initialization of matrices
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
M[i][j] = 0.0f;
M_print[i][j] = 0.0f;
}
}
//adjacency matrix creation
for (int i = 0; i < N; i++) {
for (int j = 0; j < mesh->verts[i]->vertList.size(); j++) {
float dist = getDistance(mesh->verts[i]->coords, mesh->verts[mesh->verts[i]->vertList[j]]->coords);
M[i][mesh->verts[i]->vertList[j]] = dist;
M_print[i][mesh->verts[i]->vertList[j]] = dist;
}
}
//These are the vertices which will geodesic distance calculated on
int V_source = getRandVertexPoint(mesh);
int V_target = getRandVertexPoint(mesh);
//Prev will be used for painting shortest path
Dijkstra(M, V_source,V_target, N, prev);
root->addChild(painter->getShapeSep(mesh));
root->addChild(painter->get1PointSep(mesh, V_source, 2, 0, 0, 1));
root->addChild(painter->get1PointSep(mesh, V_target, 2, 0, 0, 1));
root->addChild(painter->DrawLines(mesh,V_source,V_target,N, prev));
viewer->setSize(SbVec2s(640, 480));
viewer->setSceneGraph(root);
viewer->show();
SoWin::show(window);
SoWin::mainLoop();
delete viewer;
root->unref();
for (int i = 0; i < N; i++) {
M_print[i] = Dijkstra(M, i, 0, N, prev);
}
FILE* matrix_text;
matrix_text = fopen("geodesic_distance_matrix.txt", "w+");
for (int y = 0; y < N; y++) {
fprintf(matrix_text, "\n");
for (int x = 0; x < N; x++)
{
fprintf(matrix_text, "%0.7g ",M_print[y][x]);
}
}
fclose(matrix_text);
//Memory leakage prevention
for (int i = 0; i < N; i++) {
delete[] M_print[i];
delete[] M[i];
}
delete[] prev;
return 0;
}
int minDistance(float* dist, bool* marked, int N)
{
// Initialize min value
float min = INT_MAX, min_index;
for (int v = 0; v < N; v++)
if (marked[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
//Dijkstra algorithm
float* Dijkstra(float** M, int src,int targ, int N, int* prev )
{
float* dist = new float[N];
bool* marked = new bool[N];
for (int i = 0; i < N; i++)
{
prev[i] = -1; //source node
dist[i] = INT_MAX;
marked[i] = false;
}
dist[src] = 0;
for (int count = 0; count < N - 1; count++)
{
int u = minDistance(dist, marked, N);
marked[u] = true;
for (int v = 0; v < N; v++) {
if (!marked[v] && M[u][v] && (dist[u] + M[u][v] < dist[v]))
{
prev[v] = u;
dist[v] = dist[u] + M[u][v];
}
}
}
delete marked;
return dist;
}
int getRandVertexPoint(Mesh* mesh) {
int randV1 = rand() % (mesh->verts.size());
return randV1;
}
//returns distance between two 3D points
float getDistance(float* refCoord, float* targetCoord) {
float distance = (
pow(refCoord[0] - targetCoord[0], 2) +
pow(refCoord[1] - targetCoord[1], 2) +
pow(refCoord[2] - targetCoord[2], 2));
return sqrt(distance);
}