-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewPathClusterFinder.cpp
More file actions
247 lines (204 loc) · 8.59 KB
/
ViewPathClusterFinder.cpp
File metadata and controls
247 lines (204 loc) · 8.59 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* File: ViewPathClusterFinder.cpp
* Author: zoizoi
*
* Created on 18 June 2011, 10:15
*/
#include "ViewPathClusterFinder.h"
#include "GeoSphere.h"
ViewPathClusterFinder::ViewPathClusterFinder(int plen, OVASControl* o) {
viewPaths = new vector<int*>();
pathLength = plen;
oc = o;
}
ViewPathClusterFinder::ViewPathClusterFinder(const ViewPathClusterFinder& orig) {
}
ViewPathClusterFinder::~ViewPathClusterFinder() {
}
void ViewPathClusterFinder::addPath(int* path) {
viewPaths->push_back(path);
}
void ViewPathClusterFinder::outputPathsToFile(){
string filename=string("./lastestTempData/viewPathData");
cout << "outputing file " << filename << "\n";
ofstream of;
of.open(filename.c_str());
int numPaths=viewPaths->size();
int numSteps=pathLength;
of << numSteps << " " << " "<<numPaths<< endl;
int* path;
for (int i = 0; i < numPaths; i++) {
path=viewPaths->at(i);
for (int j = 0; j < numSteps; j++) {
of << path[j] << " ";
}
of << endl;
}
of.close();
ifstream infile;
}
void ViewPathClusterFinder::loadPathsFromFile(){
string filename=string("./lastestTempData/viewPathData");
cout << "inputing file " << filename << "\n";
ifstream inf;
inf.open(filename.c_str());
int numPaths;
int numSteps;
inf >> numSteps >>numPaths;
if(numSteps!=oc->numSteps){
cout<<"error: step count conflict. "<<endl;
oc->numSteps=numSteps;
}
int* path;
for (int i = 0; i < numPaths; i++) {
path=new int[numSteps]();
for (int j = 0; j < numSteps; j++) {
inf>>path[j];
}
viewPaths->push_back(path);
}
inf.close();
cout<<"vp size "<<viewPaths->size()<<endl;
}
vector<int*>* ViewPathClusterFinder::getPathClusterMeans() {
vector<int*>* meanPaths = new vector<int*>();
int numberOfClusters = 2;
vtkSmartPointer<vtkTable> inputData =
vtkSmartPointer<vtkTable>::New();
for (int c = 0; c < pathLength; ++c) {
std::stringstream colName;
colName << "coord " << c*3;
vtkSmartPointer<vtkDoubleArray> doubleArrayX =
vtkSmartPointer<vtkDoubleArray>::New();
doubleArrayX->SetNumberOfComponents(1);
doubleArrayX->SetName(colName.str().c_str());
doubleArrayX->SetNumberOfTuples(viewPaths->size());
colName << "coord " << c*3+1;
vtkSmartPointer<vtkDoubleArray> doubleArrayY =
vtkSmartPointer<vtkDoubleArray>::New();
doubleArrayY->SetNumberOfComponents(1);
doubleArrayY->SetName(colName.str().c_str());
doubleArrayY->SetNumberOfTuples(viewPaths->size());
colName << "coord " << c*3+2;
vtkSmartPointer<vtkDoubleArray> doubleArrayZ =
vtkSmartPointer<vtkDoubleArray>::New();
doubleArrayZ->SetNumberOfComponents(1);
doubleArrayZ->SetName(colName.str().c_str());
doubleArrayZ->SetNumberOfTuples(viewPaths->size());
for (int r = 0; r < viewPaths->size(); ++r) {
vector<int*>::iterator it;
int* path = viewPaths->at(r); // int* path=viewPaths->
//double p[3];;
// cout<<" test GS c "<<c<<" "<<" path val"<<path[c]<<" r "<<r<<endl;
// cout<<" test GS c"<<c<<" "<<(float)oc->geoSphere->getView(path[c])->getx()<<" path val"<<path[c]<<endl;
//
float vx = oc->geoSphere->getView(path[c])->getx();
//cout<<setprecision(16)<<" using x "<<vx<<endl;
float vy = oc->geoSphere->getView(path[c])->gety();
float vz = oc->geoSphere->getView(path[c])->getz();
//cout<<r<<" adding "<<val<<" to data"<<endl;
//cout<<"rt "<<randtest<<endl;
doubleArrayX->SetValue(r, vx);
doubleArrayY->SetValue(r, vy);
doubleArrayZ->SetValue(r, vz);
}
inputData->AddColumn(doubleArrayX);
inputData->AddColumn(doubleArrayY);
inputData->AddColumn(doubleArrayZ);
}
vtkSmartPointer<vtkKMeansStatistics> kMeansStatistics =
vtkSmartPointer<vtkKMeansStatistics>::New();
kMeansStatistics->SetInput(vtkStatisticsAlgorithm::INPUT_DATA, inputData);
for (int c = 0; c < pathLength*3; ++c) {
kMeansStatistics->SetColumnStatus(inputData->GetColumnName(c), 1);
}
//
// kMeansStatistics->SetLearnOption( 1 ); // This is on by default.
// kMeansStatistics->SetMaxNumIterations( 1 );
kMeansStatistics->RequestSelectedColumns();
kMeansStatistics->SetAssessOption(true);
kMeansStatistics->SetDefaultNumberOfClusters(numberOfClusters);
kMeansStatistics->Update();
//kMeansStatistics->GetOutput()->Dump();
float* minDistances=new float[numberOfClusters];
int* closestToMin=new int[numberOfClusters];
pathClusters=new int[viewPaths->size()]();
for (int i = 0; i < numberOfClusters; i++) {
minDistances[i]=100;
int* pathPoints = new int[pathLength*3];
int clusterCount = 0;
for (unsigned int r = 0; r < kMeansStatistics->GetOutput()->GetNumberOfRows(); r++) {
vtkVariant cluster = kMeansStatistics->GetOutput()->GetValue(r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 1);
vtkVariant distance = kMeansStatistics->GetOutput()->GetValue(r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 2);
pathClusters[r]=cluster.ToInt();
if (pathClusters[r]==1){
cout<<" got 1"<<i<<endl;
}
//cout<<" cluster is "<<cluster.ToInt()<<" i:"<<i<<endl;
if (cluster.ToInt() == i) {
if(distance.ToFloat()<minDistances[i]){
minDistances[i]=distance.ToFloat();
closestToMin[i]=r;
}
}
}
//cout<<"closest r was "<<closestToMin[i]<<" dist "<<minDistances[i]<<"viewpaths size:"<<viewPaths->size();
int* path = viewPaths->at(closestToMin[i]);
meanPaths->push_back(path);
}
// vtkMultiBlockDataSet* outputData = vtkMultiBlockDataSet::SafeDownCast(kMeansStatistics->GetOutputDataObject(vtkStatisticsAlgorithm::OUTPUT_MODEL));
// vtkTable* outputMeta = vtkTable::SafeDownCast(outputData->GetBlock(0));
// outputMeta->Dump();
// cout << " num tabs " << outputData->GetNumberOfBlocks() << endl;
return meanPaths;
}
void ViewPathClusterFinder::testCluserDetection() {
int numDims = 40;
int numPoints = 10;
int numberOfClusters = 2;
// Get the points into the format needed for KMeans
vtkSmartPointer<vtkTable> inputData =
vtkSmartPointer<vtkTable>::New();
for (int c = 0; c < numDims; ++c) {
std::stringstream colName;
colName << "coord " << c;
vtkSmartPointer<vtkDoubleArray> doubleArray =
vtkSmartPointer<vtkDoubleArray>::New();
doubleArray->SetNumberOfComponents(1);
doubleArray->SetName(colName.str().c_str());
doubleArray->SetNumberOfTuples(numPoints);
for (int r = 0; r < numPoints; ++r) {
//double p[3];;
double val;
if (r < 5) val = (rand() % 10);
else {
val = (rand() % 10) + 4;
}
//cout<<"rt "<<randtest<<endl;
doubleArray->SetValue(r, val);
}
inputData->AddColumn(doubleArray);
}
vtkSmartPointer<vtkKMeansStatistics> kMeansStatistics =
vtkSmartPointer<vtkKMeansStatistics>::New();
kMeansStatistics->SetInput(vtkStatisticsAlgorithm::INPUT_DATA, inputData);
for (int c = 0; c < numDims; ++c) {
kMeansStatistics->SetColumnStatus(inputData->GetColumnName(c), 1);
}
//kMeansStatistics->SetColumnStatus( "Testing", 1 );
kMeansStatistics->RequestSelectedColumns();
kMeansStatistics->SetAssessOption(true);
kMeansStatistics->SetDefaultNumberOfClusters(numberOfClusters);
kMeansStatistics->Update();
// Display the results
kMeansStatistics->GetOutput()->Dump();
vtkSmartPointer<vtkIntArray> clusterArray =
vtkSmartPointer<vtkIntArray>::New();
clusterArray->SetNumberOfComponents(1);
clusterArray->SetName("ClusterId");
for (unsigned int r = 0; r < kMeansStatistics->GetOutput()->GetNumberOfRows(); r++) {
vtkVariant v = kMeansStatistics->GetOutput()->GetValue(r, kMeansStatistics->GetOutput()->GetNumberOfColumns() - 1);
std::cout << "Point " << r << " is in cluster " << v.ToInt() << std::endl;
clusterArray->InsertNextValue(v.ToInt());
}
}