Commit 4b594a40601be8cacbce578ad000d549e4403b43

Authored by Pavel Govyadinov
2 parents c230039b c0f3e9f6

Merge remote-tracking branch 'origin/Branch_Detection'

stim/cuda/cost.h
... ... @@ -3,17 +3,16 @@
3 3 #include <cuda_runtime.h>
4 4 #include <cublas_v2.h>
5 5 #include <stdio.h>
6   -#include <stim/visualization/colormap.h>
  6 +#include "../visualization/colormap.h"
7 7 #include <sstream>
8   -#include <stim/math/vector.h>
9   -#include <stim/cuda/devices.h>
10   -#include <stim/cuda/threads.h>
  8 +#include "../math/vector.h"
  9 +#include "../cuda/devices.h"
  10 +#include "../cuda/threads.h"
11 11  
12 12 ///Cost function that works with the gl-spider class to find index of the item with min-cost.
13 13 typedef unsigned char uchar;
14 14 texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn;
15 15 float *result;
16   -float* v_dif;
17 16 cudaArray* srcArray;
18 17 bool testing = false;
19 18  
... ... @@ -26,30 +25,12 @@ inline void checkCUDAerrors(const char *msg)
26 25 }
27 26 }
28 27  
29   -///Finds the sum of all the pixes in a gives template element.
30   -///Returns the abosolute value.
31   -///@param *diff, a pointer to the memory block that holds the pixel-differences.
32   -float get_sum(float *diff)
33   -{
34   -
35   - cublasStatus_t ret;
36   - cublasHandle_t handle;
37   - ret = cublasCreate(&handle);
38   -
39   - ret = cublasSetVector(20*10, sizeof(*diff), diff, 1, v_dif, 1);
40   - float out;
41   - ret = cublasSasum(handle, 20*10, v_dif, 1, &out);
42   -// cublasDestroy(ret);
43   - cublasDestroy(handle);
44   - return out;
45   -}
46   -
47 28 ///A virtual representation of a uniform template.
48 29 ///Returns the value of the template pixel.
49 30 ///@param x, location of a pixel.
50 31 __device__ float Template(int x)
51 32 {
52   - if(x < 20/6 || x > 20*5/6 || (x > 20*2/6 && x < 20*4/6)){
  33 + if(x < 16/6 || x > 16*5/6 || (x > 16*2/6 && x < 16*4/6)){
53 34 return 1.0;
54 35 }else{
55 36 return 0.0;
... ... @@ -63,15 +44,47 @@ __device__ float Template(int x)
63 44 __global__
64 45 void get_diff (float *result)
65 46 {
66   - //cuPrintf("Hello");
  47 + //float* shared = SharedMemory();
  48 + __shared__ float shared[16][8];
67 49 int x = threadIdx.x + blockIdx.x * blockDim.x;
68 50 int y = threadIdx.y + blockIdx.y * blockDim.y;
69   - int idx = y*20+x;
  51 + int x_t = threadIdx.x;
  52 + int y_t = threadIdx.y;
  53 + //int idx = y*16+x;
  54 + int g_idx = blockIdx.y;
70 55  
71 56 float valIn = tex2D(texIn, x, y)/255.0;
72 57 float valTemp = Template(x);
73   - result[idx] = abs(valIn-valTemp);
74   - //result[idx] = abs(valIn);
  58 + shared[x_t][y_t] = abs(valIn-valTemp);
  59 +
  60 + __syncthreads();
  61 +
  62 + for(unsigned int step = blockDim.x/2; step >= 1; step >>= 1)
  63 + {
  64 + __syncthreads();
  65 + if (x_t < step)
  66 + {
  67 + shared[x_t][y_t] += shared[x_t + step][y_t];
  68 + }
  69 + __syncthreads();
  70 + }
  71 + __syncthreads();
  72 +
  73 + for(unsigned int step = blockDim.y/2; step >= 1; step >>= 1)
  74 + {
  75 + __syncthreads();
  76 + if(y_t < step)
  77 + {
  78 + shared[x_t][y_t] += shared[x_t][y_t + step];
  79 + }
  80 + __syncthreads();
  81 + }
  82 + __syncthreads();
  83 + if(x_t == 0 && y_t == 0)
  84 + result[g_idx] = shared[0][0];
  85 +
  86 +
  87 +// //result[idx] = abs(valIn);
75 88 }
76 89  
77 90  
... ... @@ -82,12 +95,6 @@ void get_diff (float *result)
82 95 ///@param DIM_Y, integer controlling how much memory to allocate.
83 96 void initArray(cudaGraphicsResource_t src, int DIM_Y)
84 97 {
85   - //cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar> ();
86   - //cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0);
87   - //HANDLE_ERROR(
88   - // cudaGraphicsGLRegisterImage(&src,
89   - // fboID,
90   - // GL_TEXTURE_2D,
91 98 HANDLE_ERROR(
92 99 cudaGraphicsMapResources(1, &src)
93 100 );
... ... @@ -97,10 +104,8 @@ void initArray(cudaGraphicsResource_t src, int DIM_Y)
97 104 HANDLE_ERROR(
98 105 cudaBindTextureToArray(texIn, srcArray)
99 106 );
100   - cudaMalloc( (void**) &result, 20*DIM_Y*sizeof(float));
  107 + cudaMalloc( (void**) &result, DIM_Y*sizeof(float));
101 108 checkCUDAerrors("Memory Allocation Issue 1");
102   - cudaMalloc((void **) &v_dif, 20*10*sizeof(float));
103   - checkCUDAerrors("Memory Allocation Issue 2");
104 109 //HANDLE_ERROR(
105 110 // cudaBindTextureToArray(texIn, ptr, &channelDesc)
106 111 // );
... ... @@ -117,9 +122,6 @@ void cleanUP(cudaGraphicsResource_t src)
117 122 cudaGraphicsUnmapResources(1,&src)
118 123 );
119 124 HANDLE_ERROR(
120   - cudaFree(v_dif)
121   - );
122   - HANDLE_ERROR(
123 125 cudaUnbindTexture(texIn)
124 126 );
125 127 }
... ... @@ -151,25 +153,33 @@ stim::vec&lt;int&gt; get_cost(cudaGraphicsResource_t src, int DIM_Y)
151 153 // name << "sample_" << inter << "_" << idx << ".bmp";
152 154 // stim::gpu2image<float>(v_dif, name.str(), 20,10,0,1);
153 155  
154   - float output[DIM_Y];
  156 + //float output[DIM_Y];
  157 + float *output;
  158 + output = (float* ) malloc(DIM_Y*sizeof(float));
155 159 stim::vec<int> ret(0, 0);
156 160 float mini = 10000000000000000.0;
157   - int idx;
158   - initArray(src, DIM_Y*10);
159   - dim3 grid(20/2, DIM_Y*10/2);
160   - dim3 block(2, 2);
161   -
162   - get_diff <<< grid, block >>> (result);
163   - for (int i = 0; i < DIM_Y; i++){
164   - output[i] = get_sum(result+(20*10*i));
165   - if(output[i] <= mini){
  161 + int idx = 0;
  162 + initArray(src, DIM_Y*8);
  163 + dim3 numBlocks(1, DIM_Y);
  164 + dim3 threadsPerBlock(16, 8);
  165 +
  166 +
  167 + get_diff <<< numBlocks, threadsPerBlock >>> (result);
  168 + cudaMemcpy(output, result, DIM_Y*sizeof(float), cudaMemcpyDeviceToHost);
  169 +
  170 + for( int i = 0; i<DIM_Y; i++){
  171 +// std::cout << output[i] << std::endl;
  172 + if(output[i] < mini){
166 173 mini = output[i];
167 174 idx = i;
168 175 }
169   - }
170   -
171   - output[idx] = get_sum(result+(20*10*idx));
  176 + }
  177 +
  178 +// std::cout << "hello" << std::endl;
  179 + //output[idx] = get_sum(result+(16*8*idx));
172 180 cleanUP(src);
173 181 ret[0] = idx; ret[1] = (int) output[idx];
  182 + std::cout << output[idx] << std::endl;
  183 + free(output);
174 184 return ret;
175 185 }
... ...
stim/gl/gl_spider.h
... ... @@ -121,7 +121,7 @@ class gl_spider
121 121 setMatrix();
122 122 glCallList(dList+3);
123 123  
124   -// int best = getCost();
  124 + // int best = getCost();
125 125  
126 126 }
127 127  
... ... @@ -185,7 +185,7 @@ class gl_spider
185 185 ver = stim::rect<float>(mag,
186 186 pos, temp,
187 187 hor.n());
188   - UpdateBuffer(0.0, 0.0+idx*10.0);
  188 + UpdateBuffer(0.0, 0.0+idx*8.0);
189 189 CHECK_OPENGL_ERROR
190 190 }
191 191 }
... ... @@ -233,7 +233,7 @@ class gl_spider
233 233 ver = stim::rect<float>(mag,
234 234 temp, dir,
235 235 hor.n());
236   - UpdateBuffer(0.0, 0.0+idx*10.0);
  236 + UpdateBuffer(0.0, 0.0+idx*8.0);
237 237 CHECK_OPENGL_ERROR
238 238 }
239 239 }
... ... @@ -244,7 +244,8 @@ class gl_spider
244 244 ///Method for populating the buffer with the sampled texture.
245 245 ///uses the default m <1,1,0>
246 246 void
247   - genMagnitudeVectors(float delta = 0.5)
  247 + genMagnitudeVectors(float delta = 0.70)
  248 +// genMagnitudeVectors(float delta = 0.50)
248 249 {
249 250  
250 251 //Set up the vectors necessary for Rectangle creation.
... ... @@ -274,7 +275,7 @@ class gl_spider
274 275 ver = stim::rect<float>(temp,
275 276 pos, dir,
276 277 hor.n());
277   - UpdateBuffer(0.0, 0.0+i*10.0);
  278 + UpdateBuffer(0.0, 0.0+i*8.0);
278 279 CHECK_OPENGL_ERROR
279 280 }
280 281 glEndList();
... ... @@ -286,7 +287,7 @@ class gl_spider
286 287 void
287 288 UpdateBuffer(float v_x, float v_y)
288 289 {
289   - float len = 10.0;
  290 + float len = 8.0;
290 291 stim::vec<float>p1;
291 292 stim::vec<float>p2;
292 293 stim::vec<float>p3;
... ... @@ -338,13 +339,13 @@ class gl_spider
338 339 p2[1],
339 340 p2[2]
340 341 );
341   - glVertex2f(v_x+2*len, v_y);
  342 + glVertex2f(v_x+2.0*len, v_y);
342 343 glTexCoord3f(
343 344 p3[0],
344 345 p3[1],
345 346 p3[2]
346 347 );
347   - glVertex2f(v_x+2*len, v_y+len);
  348 + glVertex2f(v_x+2.0*len, v_y+len);
348 349 glTexCoord3f(
349 350 p4[0],
350 351 p4[1],
... ... @@ -383,47 +384,6 @@ class gl_spider
383 384 glBindTexture(GL_TEXTURE_2D, 0);
384 385 }
385 386  
386   - ///Method for controling the buffer and texture binding in order to properly
387   - ///do the render to texture.
388   - void
389   - Bind()
390   - {
391   - float len = 10.0;
392   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer
393   - glFramebufferTexture2D(
394   - GL_FRAMEBUFFER,
395   - GL_COLOR_ATTACHMENT0,
396   - GL_TEXTURE_2D,
397   - texbufferID,
398   - 0);
399   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
400   - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
401   - glDrawBuffers(1, DrawBuffers);
402   - glBindTexture(GL_TEXTURE_2D, texbufferID);
403   - glClearColor(1,1,1,1);
404   - glClear(GL_COLOR_BUFFER_BIT);
405   - glMatrixMode(GL_PROJECTION);
406   - glLoadIdentity();
407   - glMatrixMode(GL_MODELVIEW);
408   - glLoadIdentity();
409   - glViewport(0,0,2.0*len, numSamples*len);
410   - gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
411   - glEnable(GL_TEXTURE_3D);
412   - glBindTexture(GL_TEXTURE_3D, texID);
413   -
414   - CHECK_OPENGL_ERROR
415   - }
416   -
417   - ///Method for Unbinding all of the texture resources
418   - void
419   - Unbind()
420   - {
421   - //Finalize GL_buffer
422   - glBindTexture(GL_TEXTURE_3D, 0);
423   - glDisable(GL_TEXTURE_3D);
424   - glBindFramebuffer(GL_FRAMEBUFFER,0);
425   - glBindTexture(GL_TEXTURE_2D, 0);
426   - }
427 387  
428 388 ///Method for using the gl manipulation to alighn templates from
429 389 ///Template space (-0.5 0.5) to Texture space (0.0, 1.0),
... ... @@ -558,7 +518,7 @@ class gl_spider
558 518 attachSpider(GLuint id)
559 519 {
560 520 texID = id;
561   - GenerateFBO(20, numSamples*10);
  521 + GenerateFBO(16, numSamples*8);
562 522 setDims(0.6, 0.6, 1.0);
563 523 setSize(512.0, 512.0, 426.0);
564 524 setMatrix();
... ... @@ -704,6 +664,47 @@ class gl_spider
704 664 return fboID;
705 665 }
706 666  
  667 + ///Method for controling the buffer and texture binding in order to properly
  668 + ///do the render to texture.
  669 + void
  670 + Bind()
  671 + {
  672 + float len = 8.0;
  673 + glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer
  674 + glFramebufferTexture2D(
  675 + GL_FRAMEBUFFER,
  676 + GL_COLOR_ATTACHMENT0,
  677 + GL_TEXTURE_2D,
  678 + texbufferID,
  679 + 0);
  680 + glBindFramebuffer(GL_FRAMEBUFFER, fboID);
  681 + GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
  682 + glDrawBuffers(1, DrawBuffers);
  683 + glBindTexture(GL_TEXTURE_2D, texbufferID);
  684 + glClearColor(1,1,1,1);
  685 + glClear(GL_COLOR_BUFFER_BIT);
  686 + glMatrixMode(GL_PROJECTION);
  687 + glLoadIdentity();
  688 + glMatrixMode(GL_MODELVIEW);
  689 + glLoadIdentity();
  690 + glViewport(0,0,2.0*len, numSamples*len);
  691 + gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
  692 + glEnable(GL_TEXTURE_3D);
  693 + glBindTexture(GL_TEXTURE_3D, texID);
  694 +
  695 + CHECK_OPENGL_ERROR
  696 + }
  697 +
  698 + ///Method for Unbinding all of the texture resources
  699 + void
  700 + Unbind()
  701 + {
  702 + //Finalize GL_buffer
  703 + glBindTexture(GL_TEXTURE_3D, 0);
  704 + glDisable(GL_TEXTURE_3D);
  705 + glBindFramebuffer(GL_FRAMEBUFFER,0);
  706 + glBindTexture(GL_TEXTURE_2D, 0);
  707 + }
707 708 //--------------------------------------------------------------------------//
708 709 //-----------------------------TEMPORARY METHODS----------------------------//
709 710 //--------------------------------------------------------------------------//
... ... @@ -729,7 +730,7 @@ class gl_spider
729 730 findOptimalDirection();
730 731 findOptimalPosition();
731 732 findOptimalScale();
732   - // branchDetection();
  733 + branchDetection();
733 734 Unbind();
734 735 return current_cost;
735 736 }
... ... @@ -776,9 +777,9 @@ class gl_spider
776 777 glTexCoord3f(x,y,z0);
777 778 glVertex2f(0.0, j*0.1+0.1);
778 779 glTexCoord3f(x,y,z1);
779   - glVertex2f(20.0, j*0.1+0.1);
  780 + glVertex2f(16.0, j*0.1+0.1);
780 781 glTexCoord3f(xold,yold,z1);
781   - glVertex2f(20.0, j*0.1);
  782 + glVertex2f(16.0, j*0.1);
782 783 glTexCoord3f(xold,yold,z0);
783 784 glVertex2f(0.0, j*0.1);
784 785 xold=x;
... ...
stim/visualization/glObj.h
1 1 #ifndef STIM_GLOBJ_H
2 2 #define STIM_GLOBJ_H
3 3  
4   -#include <stim/visualization/obj.h>
5 4 #include <GL/glew.h>
6 5 #include <GL/glut.h>
7 6 #include <stim/math/vector.h>
  7 +#include <stim/visualization/obj.h>
8 8  
9 9  
10 10 namespace stim
... ... @@ -13,23 +13,25 @@ namespace stim
13 13 * Assist with the visualization the segmented vessels.
14 14 * Uses
15 15 */
16   -
17   -class objGl : public virtual stim::obj<T>
  16 +template <typename T>
  17 +class glObj : public virtual stim::obj<T>
18 18 {
19 19 private:
20 20 using stim::obj<T>::L;
21 21 using stim::obj<T>::P;
22 22 using stim::obj<T>::F;
23   - using vec<T>::size;
24   - using vec<T>::at;
  23 +// using stim::obj<T>::numL();
  24 +// using std::vector<T>::size;
  25 +// using std::vector<T>::at;
25 26 GLuint dList;
26 27  
27 28  
28 29 void
29   - init
  30 + init()
30 31 {
31   - dList = glGenList(1);
  32 + dList = glGenLists(1);
32 33 glListBase(dList);
  34 +
33 35 glMatrixMode(GL_PROJECTION);
34 36 glLoadIdentity;
35 37 glMatrixMode(GL_MODELVIEW);
... ... @@ -40,15 +42,17 @@ private:
40 42 void
41 43 Create()
42 44 {
43   - int len = (int) numL();
  45 + int len = (int) stim::obj<T>::numL();
44 46 std::vector< stim::vec<float> > line;
45 47 glNewList(dList, GL_COMPILE);
46   - glColor3f(0.5, 1.0, 0.5);
  48 + // glColor3f(0.0, 1.0, 0.0);
  49 + glLineWidth(2.5);
47 50 for(int i = 0; i < len; i++){
48   - line = getL_V(i);
49   - glBegin(GL_LINES);
  51 + line = stim::obj<T>::getL_V(i);
  52 + glColor3ub(rand()%255, rand()%255, rand()%255);
  53 + glBegin(GL_LINE_STRIP);
50 54 for(int j = 0; j < line.size(); j++){
51   - glVectex3f(
  55 + glVertex3f(
52 56 line[j][0],
53 57 line[j][1],
54 58 line[j][2]
... ... @@ -57,16 +61,33 @@ private:
57 61 glEnd();
58 62 }
59 63 glEndList();
  64 + CHECK_OPENGL_ERROR
60 65 }
61 66  
62 67 public:
63   - objGl(std::string filename)
  68 + glObj()
64 69 {
65   - stim::obj::load(filename);
66   - glPopMatrix(); //Safety Operation to avoid changing the current matrix.
  70 +
  71 + }
  72 +
  73 + void
  74 + createFromSelf()
  75 + {
  76 + // glPopMatrix();
67 77 init();
68 78 Create();
69   - glPushMatrix();
  79 + // glPushMatrix();
  80 + }
  81 +
  82 + void
  83 + createFromFile(std::string filename)
  84 + {
  85 + stim::obj<T>::load(filename);
  86 + glPushMatrix(); //Safety Operation to avoid changing the current matrix.
  87 + init();
  88 + Create();
  89 + glPopMatrix();
  90 + CHECK_OPENGL_ERROR
70 91 }
71 92  
72 93  
... ... @@ -74,7 +95,44 @@ public:
74 95 Render()
75 96 {
76 97 glCallList(dList);
  98 + CHECK_OPENGL_ERROR
77 99 }
78 100  
  101 + void
  102 + RenderLine(int i)
  103 + {
  104 + std::vector < stim::vec<T> > line;
  105 + int len = (int) stim::obj<T>::numL();
  106 + line = stim::obj<T>::getL_V(i);
  107 + glColor3f(0.5, 1.0, 0.5);
  108 + glLineWidth(3.0);
  109 + glBegin(GL_LINE_STRIP);
  110 + for(int j = 0; j < line.size(); j++){
  111 + glVertex3f(
  112 + line[j][0],
  113 + line[j][1],
  114 + line[j][2]
  115 + );
  116 + }
  117 + glEnd();
  118 + }
  119 +
  120 + void
  121 + RenderLine(std::vector< stim::vec<T> > l)
  122 + {
  123 + glColor3f(0.5, 1.0, 0.5);
  124 + glLineWidth(3.0);
  125 + glBegin(GL_LINE_STRIP);
  126 + for(int j = 0; j < l.size(); j++){
  127 + glVertex3f(
  128 + l[j][0],
  129 + l[j][1],
  130 + l[j][2]
  131 + );
  132 + }
  133 + glEnd();
  134 + }
  135 +
  136 +};
79 137 }
80   -}
  138 +#endif
... ...
stim/visualization/obj.h
... ... @@ -352,26 +352,27 @@ public:
352 352  
353 353 /// This function terminates drawing of a primitive object, such as a line, face, or point set
354 354 void End(){
355   -
356 355 //copy the current object to the appropriate list
357   - switch(current_type){
  356 + if(current_geo.size() != 0)
  357 + {
  358 + switch(current_type){
358 359  
359   - case OBJ_NONE:
360   - std::cout<<"STIM::OBJ error, objEnd() called before objBegin()."<<std::endl;
361   - break;
  360 + case OBJ_NONE:
  361 + std::cout<<"STIM::OBJ error, objEnd() called before objBegin()."<<std::endl;
  362 + break;
362 363  
363   - case OBJ_POINTS:
364   - P.push_back(current_geo);
365   - break;
  364 + case OBJ_POINTS:
  365 + P.push_back(current_geo);
  366 + break;
366 367  
367   - case OBJ_LINE:
368   - L.push_back(current_geo);
369   - break;
  368 + case OBJ_LINE:
  369 + L.push_back(current_geo);
  370 + break;
370 371  
371   - case OBJ_FACE:
372   - F.push_back(current_geo);
  372 + case OBJ_FACE:
  373 + F.push_back(current_geo);
  374 + }
373 375 }
374   -
375 376 //clear everything
376 377 current_type = OBJ_NONE;
377 378 current_geo.clear();
... ... @@ -380,6 +381,12 @@ public:
380 381 geo_flag_vt = false;
381 382 geo_flag_vn = false;
382 383 }
  384 +//temporary convenience method
  385 + void rev(){
  386 + if(current_geo.size() != 0)
  387 + // current_geo.reverse(current_geo.begin(), current_geo.end());
  388 + std::reverse(current_geo.begin(), current_geo.end());
  389 + }
383 390  
384 391 //output the OBJ structure as a string
385 392 std::string str(){
... ... @@ -642,7 +649,6 @@ public:
642 649  
643 650 //get the number of points in the specified line
644 651 unsigned int nP = L[i].size();
645   -
646 652 //create a vector of points
647 653 std::vector< stim::vec<T> > l;
648 654  
... ... @@ -688,6 +694,7 @@ public:
688 694 return l;
689 695 }
690 696  
  697 +
691 698 /// Returns a vector containing the list of texture coordinates associated with each point of a line.
692 699  
693 700 /// @param i is the index of the desired line
... ...