Commit f31bf86d09e4c045e7b8a6df5755874accc1d1e9

Authored by Pavel Govyadinov
1 parent db3c28c9

Added skeleton functionality for branch detection. A Diplay list is not wrapped …

…around the center of the spider with the same radius as the spider.
Showing 3 changed files with 57 additions and 415 deletions   Show diff stats
gl/gl_spider.h deleted
1   -#ifndef STIM_GL_SPIDER_H
2   -#define STIM_GL_SPIDER_H
3   -
4   -#include <GL/glew.h>
5   -#include <GL/glut.h>
6   -#include <cuda.h>
7   -#include <cuda_gl_interop.h>
8   -#include <cudaGL.h>
9   -#include "gl_texture.h"
10   -#include "../visualization/camera.h"
11   -#include "./error.h"
12   -#include "../math/vector.h"
13   -#include "../math/rect.h"
14   -#include "../cuda/cost.h"
15   -#include "../cuda/glbind.h"
16   -
17   -namespace stim
18   -{
19   -
20   -template<typename T>
21   -class gl_spider : public virtual gl_texture<T>
22   -{
23   - //doen't use gl_texture really, just needs the GLuint id.
24   - //doesn't even need the texture iD really.
25   - private:
26   - stim::camera rotator;
27   - stim::vec<float> position; //vector designating the position of the spider.
28   - stim::vec<float> direction; //vector designating the orientation of the spider
29   - //always a unit vector.
30   - stim::vec<float> magnitude; //magnitude of the direction vector.
31   - //mag[0] = length.
32   - //mag[1] = width.
33   - using gl_texture<T>::texID;
34   - //using image_stack<T>::S;
35   - cudaArray* c_Array;
36   - //void** devPtr;
37   - //size_t size;
38   - cudaGraphicsResource_t resource;
39   - GLuint fboID;
40   - GLuint texbufferID;
41   -
42   - void
43   - findOptimalDirection()
44   - {
45   - /* Method for finding the best direction for the spider.
46   - Uses the camera to rotate. Then Calls Evaluate to find new cost.
47   - */
48   - }
49   -
50   - void
51   - findOptimalPosition()
52   - {
53   - /* Method for finding the best direction for the spider.
54   - Not sure if necessary since the next position for the spider
55   - will be at direction * magnitude. */
56   - }
57   -
58   - void
59   - findOptimalScale()
60   - {
61   - /* Method for finding the best scale for the spider.
62   - changes the x, y, z size of the spider to minimize the cost
63   - function. */
64   - }
65   -
66   - void
67   - Evaluate()
68   - {
69   - /* Uses uniform sampler2D in order to take a difference between
70   - the colors of two textures. 1st texture is the spider template,
71   - the 2nd is the location of the spider's overlap with the
72   - gl_template
73   -
74   - does the spider need to track it's location? Prob not since
75   - position can be set with gl_texture coordinates */
76   -
77   - }
78   -
79   - void
80   - Optimize()
81   - {
82   - /*find the optimum direction and scale */
83   - }
84   - /*
85   - void
86   - Step()
87   - {
88   - // move to the new position
89   - }
90   - */
91   - public:
92   -
93   - stim::rect<float> hor;
94   - stim::rect<float> ver;
95   -
96   -
97   -
98   - gl_spider
99   - ()
100   - {
101   - setPosition(0.0,0.0,0.0);
102   - setDirection(1.0,1.0,1.0);
103   - setMagnitude(0.1,0.1);
104   - //GenerateFBO(400,200);
105   - //Update();
106   - }
107   -
108   - gl_spider
109   - (vec<float> pos, vec<float> dir, vec<float> mag)
110   - {
111   - position = pos;
112   - direction = dir;
113   - magnitude = mag;
114   - //GenerateFBO(400,200);
115   - //Update();
116   - }
117   - //temporary cost for convenience.
118   - gl_spider
119   - (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
120   - float mag_x, float mag_y)
121   - {
122   - setPosition(pos_x, pos_y, pos_z);
123   - setDirection(dir_x, dir_y, dir_z);
124   - setMagnitude(mag_x, mag_y);
125   - //GenerateFBO(400,200);
126   - //Update();
127   - }
128   -
129   - void
130   - attachSpider(GLuint id)
131   - {
132   - texID = id;
133   - GenerateFBO(400,200);
134   - Update();
135   - }
136   -
137   - void
138   - Update()
139   - {
140   - vec<float> Y(1.0,0.0,0.0);
141   - if(cos(Y.dot(direction))< 0.087){
142   - Y[0] = 0.0; Y[1] = 1.0;}
143   - hor = stim::rect<float>(magnitude, position, direction.norm(),
144   - ((Y.cross(direction)).cross(direction)).norm());
145   - ver = stim::rect<float>(magnitude, position, direction.norm(),
146   - hor.n());
147   - UpdateBuffer();
148   - }
149   -
150   - vec<float>
151   - getPosition()
152   - {
153   - return position;
154   - }
155   -
156   - vec<float>
157   - getDirection()
158   - {
159   - return direction;
160   - }
161   -
162   - vec<float>
163   - getMagnitude()
164   - {
165   - return magnitude;
166   - }
167   -
168   - void
169   - setPosition(vec<float> pos)
170   - {
171   - position = pos;
172   - }
173   -
174   - void
175   - setPosition(float x, float y, float z)
176   - {
177   - position[0] = x;
178   - position[1] = y;
179   - position[2] = z;
180   - }
181   -
182   - void
183   - setDirection(vec<float> dir)
184   - {
185   - direction = dir;
186   - }
187   -
188   - void
189   - setDirection(float x, float y, float z)
190   - {
191   - direction[0] = x;
192   - direction[1] = y;
193   - direction[2] = z;
194   - }
195   -
196   - void
197   - setMagnitude(vec<float> mag)
198   - {
199   - magnitude = mag;
200   - }
201   -
202   - void
203   - setMagnitude(float x, float y)
204   - {
205   - magnitude[0] = x;
206   - magnitude[1] = y;
207   - }
208   -
209   - GLuint
210   - getFB()
211   - {
212   - return fboID;
213   - }
214   -
215   - void
216   - Step()
217   - {
218   - std::cout << position[0] << "," << position[1] << "," << position[1]
219   - << std::endl;
220   - setPosition(direction*magnitude[1]/2+position);
221   - Update();
222   - std::cout << position[0] << "," << position[1] << "," << position[1]
223   - << std::endl;
224   -
225   - }
226   -
227   - void
228   - UpdateBuffer()
229   - {
230   - stim::vec<float>p1;
231   - stim::vec<float>p2;
232   - stim::vec<float>p3;
233   - stim::vec<float>p4;
234   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
235   - glFramebufferTexture2D(
236   - GL_FRAMEBUFFER,
237   - GL_COLOR_ATTACHMENT0,
238   - GL_TEXTURE_2D,
239   - texbufferID,
240   - 0);
241   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
242   - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
243   - glDrawBuffers(1, DrawBuffers);
244   - glBindTexture(GL_TEXTURE_2D, texbufferID);
245   - glClearColor(0,0,0,0);
246   - glClear(GL_COLOR_BUFFER_BIT);
247   - glMatrixMode(GL_PROJECTION);
248   - glLoadIdentity();
249   - glMatrixMode(GL_MODELVIEW);
250   - glLoadIdentity();
251   - glViewport(0,0,400,200);
252   - gluOrtho2D(0.0,2.0,0.0,2.0);
253   - glEnable(GL_TEXTURE_3D);
254   - glBindTexture(GL_TEXTURE_3D, texID);
255   - p1 = hor.p(1,1);
256   - p2 = hor.p(1,0);
257   - p3 = hor.p(0,0);
258   - p4 = hor.p(0,1);
259   - glBegin(GL_QUADS);
260   - glTexCoord3f(
261   - p1[0],
262   - p1[1],
263   - p1[2]
264   - );
265   - glVertex2f(0.0,0.0);
266   - glTexCoord3f(
267   - p2[0],
268   - p2[1],
269   - p2[2]
270   - );
271   - glVertex2f(1.0, 0.0);
272   - glTexCoord3f(
273   - p3[0],
274   - p3[1],
275   - p3[2]
276   - );
277   - glVertex2f(1.0, 2.0);
278   - glTexCoord3f(
279   - p4[0],
280   - p4[1],
281   - p4[2]
282   - );
283   - glVertex2f(0.0, 2.0);
284   - glEnd();
285   - p1 = ver.p(1,1);
286   - p2 = ver.p(1,0);
287   - p3 = ver.p(0,0);
288   - p4 = ver.p(0,1);
289   - glBegin(GL_QUADS);
290   - glTexCoord3f(
291   - p1[0],
292   - p1[1],
293   - p1[2]
294   - );
295   - glVertex2f(1.0, 0.0);
296   - glTexCoord3f(
297   - p2[0],
298   - p2[1],
299   - p2[2]
300   - );
301   - glVertex2f(2.0, 0.0);
302   - glTexCoord3f(
303   - p3[0],
304   - p3[1],
305   - p3[2]
306   - );
307   - glVertex2f(2.0, 2.0);
308   - glTexCoord3f(
309   - p4[0],
310   - p4[1],
311   - p4[2]
312   - );
313   - glVertex2f(1.0, 2.0);
314   - glEnd();
315   - glBindTexture(GL_TEXTURE_3D, 0);
316   - glDisable(GL_TEXTURE_3D);
317   - glBindFramebuffer(GL_FRAMEBUFFER,0);
318   - glBindTexture(GL_TEXTURE_2D, 0);
319   - }
320   -
321   -
322   - void
323   - GenerateFBO(unsigned int width, unsigned int height)
324   - {
325   - glGenFramebuffers(1, &fboID);
326   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
327   - int numChannels = 1;
328   - unsigned char* texels = new unsigned char[width * height * numChannels];
329   - glGenTextures(1, &texbufferID);
330   - glBindTexture(GL_TEXTURE_2D, texbufferID);
331   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
332   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
333   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
334   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
335   - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
336   - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels);
337   - delete[] texels;
338   - glBindFramebuffer(GL_FRAMEBUFFER, 0);
339   - }
340   -
341   -
342   - void
343   - initCuda()
344   - {
345   - /* cudaDeviceProp prop;
346   - int device;
347   - memset( &prop, 0, sizeof(cudaDeviceProp) );
348   - prop.major = 1;
349   - prop.minor = 0;
350   - HANDLE_ERROR( cudaChooseDevice (&device, &prop ) );
351   - HANDLE_ERROR( cudaGetDeviceProperties(&prop, device));
352   - printf(" device number: %d\n", device);
353   - printf(" device name: %s\n", prop.name);
354   - printf(" device maxTexture3D: %d %d %d\n", prop.maxTexture3D[0] ,prop.maxTexture3D[0], prop.maxTexture3D[2]) ;
355   - HANDLE_ERROR( cudaGLSetGLDevice (device)); */
356   - stim::cudaSetDevice();
357   -/* HANDLE_ERROR(
358   - cudaGraphicsMapResources(1, &resource, 0)
359   - );
360   - HANDLE_ERROR(
361   - cudaGraphicsResourceGetMappedPointer(
362   - &devPtr,
363   - size,
364   - resource));
365   - HANDLE_ERROR(
366   - cudaGraphicsSubResourceGetMappedArray(
367   - &c_Array,
368   - resource,
369   - 0,0)
370   - );
371   - HANDLE_ERROR(
372   - cudaBindTextureToArray(fboID, c_Array)
373   - );
374   - HANDLE_ERROR(
375   - cudaGraphicsUnmapResources(1, &resource, 0)
376   - );
377   - //need to move the constants to video memory.
378   -*/
379   - }
380   -
381   - void
382   - createResource()
383   - {
384   - HANDLE_ERROR(
385   - cudaGraphicsGLRegisterImage(
386   - &resource,
387   - fboID,
388   - GL_TEXTURE_2D,
389   - CU_GRAPHICS_REGISTER_FLAGS_NONE)
390   - );
391   - }
392   -
393   - void
394   - destroyResource()
395   - {
396   - HANDLE_ERROR(
397   - cudaGraphicsUnregisterResource(resource)
398   - );
399   - }
400   -
401   - float
402   - getCost()
403   - {
404   - createResource();
405   - float cost = get_cost(resource);
406   - destroyResource();
407   - return cost;
408   - }
409   -};
410   -}
411   -#endif
stim/cuda/cost.h
... ... @@ -134,7 +134,8 @@ int get_cost(cudaGraphicsResource_t src, int inter, int DIM_Y)
134 134 dim3 grid(20, DIM_Y*10);
135 135 dim3 block(1, 1);
136 136 get_diff <<< grid, block >>> (result);
137   - stim::gpu2image<float>(result, "test.bmp", 20,DIM_Y*10,0,1);
  137 + name << "temp_diff_" << inter << ".bmp";
  138 + stim::gpu2image<float>(result, name.str(), 20,DIM_Y*10,0,1);
138 139 for (int i = 0; i < DIM_Y; i++){
139 140 output[i] = get_sum(result+(20*10*i));
140 141 if(output[i] <= mini){
... ... @@ -142,6 +143,7 @@ int get_cost(cudaGraphicsResource_t src, int inter, int DIM_Y)
142 143 idx = i;
143 144 }
144 145 }
  146 + name.clear();
145 147 name << "sample_" << inter << "_" << idx << ".bmp";
146 148 output[idx] = get_sum(result+(20*10*idx));
147 149 stim::gpu2image<float>(v_dif, name.str(), 20,10,0,1);
... ...
stim/gl/gl_spider.h
... ... @@ -50,7 +50,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
50 50 cudaGraphicsResource_t resource;
51 51  
52 52 GLuint dList;
53   - GLubyte list[3];
  53 + GLubyte list[4];
54 54 GLuint fboID;
55 55 GLuint texbufferID;
56 56 int iter; //temporary for testing
... ... @@ -124,6 +124,17 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
124 124 Unbind();
125 125 }
126 126  
  127 + void
  128 + branchDetection()
  129 + {
  130 + Bind();
  131 + positionTemplate();
  132 + glCallList(dList+3);
  133 +
  134 + int best = getCost();
  135 +
  136 + }
  137 +
127 138  
128 139  
129 140 void
... ... @@ -576,12 +587,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
576 587 gl_texture<T>::setDims(0.6, 0.6, 1.0);
577 588 setSize(512, 512, 426);
578 589  
579   - dList = glGenLists(3);
580   - list[0] = 0; list[1] = 1; list[2] = 2;
  590 + dList = glGenLists(4);
  591 + list[0] = 0; list[1] = 1; list[2] = 2; list[3] = 3;
581 592 glListBase(dList);
582 593 genTemplate(dirVectors, 0);
583 594 genTemplate(posVectors, 1);
584 595 genTemplate(magVectors, 2);
  596 + DrawCylinder();
585 597 }
586 598  
587 599 //--------------------------------------------------------------------------//
... ... @@ -742,6 +754,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
742 754 findOptimalDirection();
743 755 findOptimalPosition();
744 756 findOptimalScale();
  757 + branchDetection();
745 758 }
746 759  
747 760  
... ... @@ -761,6 +774,44 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
761 774 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
762 775 //std::cout << max << std::endl;
763 776 }
  777 +
  778 +//--------------------------------------------------------------------------//
  779 +//-----------------------------EXPERIMENTAL METHODS-------------------------//
  780 +//--------------------------------------------------------------------------//
  781 +
  782 + void
  783 + DrawCylinder()
  784 + {
  785 + Bind();
  786 + glNewList(dList+3, GL_COMPILE);
  787 + float z0 = -0.5; float z1 = 0.5; float r0 = 0.5;
  788 + float x,y;
  789 + float xold = 0.5; float yold = 0.5;
  790 + float step = 360.0/numSamples;
  791 + int j = 0;
  792 + glEnable(GL_TEXTURE_3D);
  793 + glBindTexture(GL_TEXTURE_3D, texID);
  794 + glBegin(GL_QUAD_STRIP);
  795 + for(float i = step; i <= 360.0; i += step)
  796 + {
  797 + x=r0*cos(i*2.0*M_PI/360.0);
  798 + y=r0*sin(i*2.0*M_PI/360.0);
  799 + glTexCoord3f(x,y,z0);
  800 + glVertex2f(0.0, j*10.0+10.0);
  801 + glTexCoord3f(x,y,z1);
  802 + glVertex2f(20.0, j*10.0+10.0);
  803 + glTexCoord3f(xold,yold,z1);
  804 + glVertex2f(20.0, j*10.0);
  805 + glTexCoord3f(xold,yold,z0);
  806 + glVertex2f(0.0, j*10.0);
  807 + xold=x;
  808 + yold=y;
  809 + j++;
  810 + }
  811 + glEnd();
  812 + glEndList();
  813 + Unbind();
  814 + }
764 815 };
765 816 }
766 817 #endif
... ...