-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhoughtransform.cu
283 lines (241 loc) · 9.1 KB
/
houghtransform.cu
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <fstream>
#include <algorithm> // to find minimum --> min_element
#include "stdio.h"
#include <vector>
#include <cuda.h>
#include "math.h"
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/functional.h>
#include <thrust/transform_reduce.h>
#include <thrust/transform.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/sequence.h>
#include <thrust/extrema.h>
#include "TH2D.h"
#include "TROOT.h"
#include "TApplication.h"
#include "TMatrixD.h"
#include "TCanvas.h"
#include "TStopwatch.h"
#include <cusp/coo_matrix.h>
#include <cusp/print.h>
#include "AhTwoArraysToMatrix.h"
#include "AhHoughTransformation.h"
/**
* @mainpage Conformal Mapping and Hough Transformation in CUDA Thrust
*
* <h1>HoughTrust</h1>
* <h2>A toy project during my PhD time</h2>
*
* Using CUDA Thrust to conformal map and hough transform points of a tracker. Two dimensionally.
*
* @author Andreas Herten
*/
/**
* @file houghtransform.cu
*
* @brief Everything is in that one file
*
* Sorry for that, but if/when I'm going to need any special functions, I will extract them
**/
/**
* @brief Reads data from specific formated file into three given vectors
* @param filename Filename (include path if necessary)
* @param x x-value vector (1st row of file)
* @param y y-value vector (2nd row of file)
* @param r r-value vector (4th row of file)
* @param upToLineNumber Up to with line (including that line) the file will be read. Starts at line 1.
*
* Uses just x, y and r at the moment, because that's all I need.
*/
void readPoints(std::string filename, std::vector<double> &x, std::vector<double> &y, std::vector<double> &r, int upToLineNumber = 2) {
std::ifstream file(filename.c_str());
float tempX, tempY, tempZ, tempR, tempI;
char tempChar[10];
int i = 1;
if (!file.good() || file.fail()) std::cout << "Failed opening file!" << std::endl;
while(i <= upToLineNumber && file >> tempX >> tempY >> tempZ >> tempR >> tempI >> tempChar) {
x.push_back(tempX);
y.push_back(tempY);
r.push_back(tempR);
i++;
}
file.close();
}
/**
* @brief Helper function which simply prints out the contents of a tuple
* @param thatTuple A tuple of doubles to be printed
* @return Nothing, it's a void.
*/
void printTuple (thrust::tuple<double, double> thatTuple) {
std::cout << thrust::get<0>(thatTuple) << " - " << thrust::get<1>(thatTuple);
}
/**
* @brief Prints out every element of a vector
* @param Vector - and a type (this method is a template)
*/
template <class T>
void printVector (const T & v) {
for (int i = 0; i < v.size(); ++i) {
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
/**
* @brief Adds up TH2D histograms
* @param vHistograms A std vector with pointers to TH2D histograms
* @return A pointer to a TH2D histogram
*/
TH2D * addVectorOfPToHistograms (std::vector<TH2D* > vHistograms) {
TH2D * tempHist = new TH2D(*(vHistograms[0]));
for (int i = 1; i < vHistograms.size(); i++) {
tempHist->Add(vHistograms[i]);
}
return tempHist;
}
int main (int argc, char** argv) {
int verbose = 1;
//! fill original data
std::vector<double> x;
std::vector<double> y;
std::vector<double> r;
// readPoints("data.dat", x, y, r, 18);
readPoints("real_data.txt", x, y, r, 18);
//! Change container from std::vector to thrust::host_vector
thrust::host_vector<double> h_x = x;
thrust::host_vector<double> h_y = y;
thrust::host_vector<double> h_r = r;
TStopwatch myWatch;
//! Setting parameters
double maxAngle = 180; //!< Hough transform ranges from 0 deg to 180 deg
double everyXDegrees = 30; //!< make a point every X degrees of alpha; default = 30
if (argc > 1) everyXDegrees = (double)atof(argv[1]); //!< overwrite default value to what was given by command line
//! Simple (x,y) coordinates
AhHoughTransformation * houghTrans = new AhHoughTransformation(h_x, h_y, maxAngle, everyXDegrees);
//! Use isochrones - (x,y,r) coordinates
// maxAngle *= 2; //!< for isochrones, hough transformation goes from 0 to 360
// AhHoughTransformation * houghTrans = new AhHoughTransformation(h_x, h_y, h_r, maxAngle, everyXDegrees);
thrust::device_vector<double> alphas = houghTrans->GetAngles();
std::vector<thrust::device_vector<double> > transformedPoints = houghTrans->GetVectorOfTransformedPoints();
/*
* ### Make CUSP Matrix ###
*/
//! Find upper and lower borders of histograms
int nBinsX = (int) maxAngle/everyXDegrees;
double minValueX = 0;
double maxValueX = maxAngle;
if (verbose > 0) std::cout << "nBinsX = " << nBinsX << ", minValueX = " << minValueX << ", maxValueX = " << maxValueX << std::endl;
int nBinsY = maxAngle/everyXDegrees;
// double minValueY = -0.4;
// double maxValueY = 0.7;
// if (verbose > 0) std::cout << "nBinsY = " << nBinsY << ", minValueY = " << minValueY << ", maxValueY " << maxValueY << std::endl;
//! Automatically get y borders
std::cout << transformedPoints.size() << std::endl;
double minValueY = transformedPoints[0][0];
double maxValueY = transformedPoints[0][0];
std::cout << "minValueY = " << minValueY << ", maxValueY = " << maxValueY << std::endl;
for (int i = 0; i < transformedPoints.size(); i++) {
thrust::device_vector<double> tempD(transformedPoints[i]);
double minimum = *(thrust::min_element(tempD.begin(), tempD.end()));
double maximum = *(thrust::max_element(tempD.begin(), tempD.end()));
if (minimum < minValueY) minValueY = minimum;
if (maximum > maxValueY) maxValueY = maximum;
std::cout << "minimum = " << minimum << ", maximum = " << maximum << std::endl;
}
minValueY *= 1.1; //!< make edges a little smoother
maxValueY *= 1.1; //!< make edges a little smoother
std::cout << "minValueY = " << minValueY << ", maxValueY = " << maxValueY << std::endl;
//! Create matrices
std::vector<TH2D*> theHistograms;
std::vector< cusp::coo_matrix<int, double, cusp::device_memory> > theMatrices;
std::vector<AhTwoArraysToMatrix> theObjects;
for (int i = 0; i < transformedPoints.size(); i++) {
AhTwoArraysToMatrix tempObject(
thrust::device_vector<double> (alphas), thrust::device_vector<double> (transformedPoints[i]),
nBinsX,
minValueX,
maxValueX,
nBinsY,
minValueY,
maxValueY,
true,
true
);
if (verbose > 0) std::cout << "Some matrix parameters: " << tempObject.GetNBinsX() << " " << tempObject.GetXlow() << " " << tempObject.GetXup() << " "<< tempObject.GetNBinsY() << " " << tempObject.GetYlow() << " " << tempObject.GetYup() << std::endl;
theHistograms.push_back(tempObject.GetHistogram());
theMatrices.push_back(tempObject.GetCUSPMatrix());
theObjects.push_back(tempObject);
char tempchar[5];
sprintf(tempchar, "%d", i);
theHistograms[i]->SetName(tempchar);
}
/*
* ### DEBUG Output ###
*/
if (verbose > 5) cusp::print(theMatrices[0]);
if (verbose > 0) { // Timings
for (int i = 0; i < theObjects.size(); i++) {
std::cout << "Timings for histogram " << i << std::endl;
std::cout << " T for translating values: " << theObjects[i].GetTimeTranslateValues() << std::endl;
std::cout << " T for sorting histogram vectors: " << theObjects[i].GetTimeHistSort() << std::endl;
std::cout << " T for summing histogram vectors: " << theObjects[i].GetTimeHistSum() << std::endl;
std::cout << " T for generating Matrix: " << theObjects[i].GetTimeCreateTMatrixD() << std::endl;
std::cout << " T for generating TH2D: " << theObjects[i].GetTimeCreateTH2D() << std::endl;
}
}
if (verbose > 1) {
for (int i = 0; i < transformedPoints.size(); i++) {
std::cout << "transformedPoints[" << i << "].size() = " << transformedPoints[i].size() << std::endl;
for (int j = 0; j < transformedPoints[i].size(); j++) {
std::cout << " transformedPoints[" << i << "][" << j << "] = " << transformedPoints[i][j] << std::endl;
}
}
}
myWatch.Stop();
std::cout << "For operations, it took me ";
myWatch.Print();
std::cout << std::endl;
/*
* ### ROOT VISUALIZATION ###
*/
bool doRoot = true;
if (argc > 2) doRoot = (bool)atof(argv[2]);
if (doRoot) {
TH2D * thatHist = addVectorOfPToHistograms(theHistograms);
thatHist->GetXaxis()->SetTitle("Angle / #circ");
thatHist->GetYaxis()->SetTitle("Hough transformed");
// for (int i = 0; i < transformedPoints.size(); i++) {
// for (int j = 0; j < transformedPoints[i].size(); j++) {
// thatHist->Fill(alphas[j], transformedPoints[i][j]);
// }
// }
TApplication *theApp = new TApplication("app", &argc, argv, 0, -1);
TCanvas * c1 = new TCanvas("c1", "default", 100, 10, 800, 600);
thatHist->Draw("COLZ");
c1->Update();
theApp->Run();
}
/*
*
* ### TODO ###
*
* make peak finder
* outline:
* create rough grid
* find max of trasnformedPoints via thrust lib to determine borders
* divide grid
* find max
* create fine grid
* find peaks
LINKS FOR MATRIX STUFF:
https://www.google.com/search?q=thrust+matrix
https://www.google.com/search?q=thrust+matrix+multiplication&sugexp=chrome,mod=10&sourceid=chrome&ie=UTF-8
http://stackoverflow.com/questions/618511/a-proper-way-to-create-a-matrix-in-c
http://www.velocityreviews.com/forums/t281152-is-there-any-matrix-in-the-stl.html
BLAS
Blitz++
*/
}