Skip to content

Commit 3998797

Browse files
committed
Adding source for SDL+OpenCV
1 parent 10e3d2c commit 3998797

File tree

9 files changed

+298
-2
lines changed

9 files changed

+298
-2
lines changed

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ config.main.mk: configure
1717

1818
configure:src/configure.ac
1919
@echo " AUTOCONF $@"
20-
@autoconf --warnings=error -o $@ $< && rm -r autom4te.cache 2>/dev/null
20+
@autoconf -I/usr/share/aclocal/ --warnings=error -o $@ $< && rm -r autom4te.cache 2>/dev/null
2121

src/Solver.h.Rt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#ifdef GRAPHICS
3030
const int desired_fps = 10;
3131
class GPUAnimBitmap;
32+
#elif WITH_SDL_WINDOW
33+
const int desired_fps = 15;
3234
#else
3335
const int desired_fps = 1;
3436
#endif

src/config.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
/* Making a GUI version */
2424
#undef GRAPHICS
2525

26+
/* Making a SDL2 version */
27+
#undef WITH_SDL_WINDOW
28+
29+
/* Making a OpenCV version */
30+
#undef WITH_OPENCV
31+
2632
/* Making a double precision version */
2733
#undef CALC_DOUBLE_PRECISION
2834

src/config.mk.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ NV_OPT = -D_FORCE_INLINES @NVFLAGS@
55
CPU = @CROSS_CPU@
66
HIP = @CROSS_HIP@
77

8+
CPP_OPT += $(pkg-config sdl2 --cflags)
9+
CPP_OPT += $(pkg-config sdl2 --cflags)
10+
811
PV_SOURCE = @PV_SOURCE@
912
PV_BUILD = @PV_BUILD@
1013
PV_BUILD_INC = @PV_BUILD_INC@
@@ -17,7 +20,7 @@ SOURCE_CU=Global.cu Lattice.cu main.cu vtkLattice.cu vtkOutput.cu cross.cu cuda.
1720
SOURCE=$(SOURCE_CU)
1821
HEADERS=Global.h gpu_anim.h LatticeContainer.h Lattice.h Region.h vtkLattice.h vtkOutput.h cross.h gl_helper.h Dynamics.h types.h pugixml.hpp pugiconfig.hpp
1922

20-
OBJ = vtkOutput.o cuda.o Global.o Lattice.o vtkLattice.o cross.o pugixml.o Geometry.o def.o unit.o Solver.o SyntheticTurbulence.o Sampler.o ZoneSettings.o RemoteForceInterface.o hdf5Lattice.o xpath_modification.o GetThreads.o Lists.o
23+
OBJ = vtkOutput.o cuda.o Global.o Lattice.o vtkLattice.o cross.o pugixml.o Geometry.o def.o unit.o Solver.o SyntheticTurbulence.o Sampler.o ZoneSettings.o RemoteForceInterface.o hdf5Lattice.o xpath_modification.o GetThreads.o Lists.o gui.o
2124

2225
AOUT = main empty compare simplepart
2326

src/configure.ac

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ AC_ARG_ENABLE([graphics],
4949
AS_HELP_STRING([--enable-graphics],
5050
[make a GUI version]))
5151

52+
AC_ARG_ENABLE([sdl],
53+
AS_HELP_STRING([--enable-sdl],
54+
[make a GUI version with SDL2]))
55+
56+
AC_ARG_ENABLE([opencv],
57+
AS_HELP_STRING([--enable-opencv],
58+
[make a version with opencv interation]))
59+
5260
AC_ARG_ENABLE([double],
5361
AS_HELP_STRING([--enable-double],
5462
[make a double precision version]))
@@ -413,6 +421,25 @@ else
413421
AC_DEFINE([GRID3D], [1], [Using 3D block grid in HIP])
414422
fi
415423

424+
if test "x${enable_sdl}" == "xyes"
425+
then
426+
CPPFLAGS="${CPPFLAGS} $(pkg-config sdl2 --cflags)"
427+
LDFLAGS="${LDFLAGS} $(pkg-config sdl2 --libs)"
428+
AC_DEFINE([WITH_SDL_WINDOW], [1], [Making a sdl version])
429+
fi
430+
431+
if test "x${enable_opencv}" == "xyes"
432+
then
433+
CPPFLAGS="${CPPFLAGS} $(pkg-config opencv4 --cflags)"
434+
LDFLAGS="${LDFLAGS} $(pkg-config opencv4 --libs)"
435+
AC_DEFINE([WITH_OPENCV], [1], [Making a opencv version])
436+
fi
437+
438+
439+
440+
441+
442+
416443
if test "x${with_x_mod}" != "x"
417444
then
418445
X_MOD="${with_x_mod}"

src/gui.cpp

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#include <opencv2/opencv.hpp>
2+
#include "gui.h"
3+
4+
using namespace cv;
5+
6+
void check_pointer(void* ptr) {
7+
if (ptr == NULL) {
8+
ERROR("Fatal error: %s\n", SDL_GetError());
9+
exit(5);
10+
}
11+
}
12+
13+
int gui_window::calibrate() {
14+
int board_width = 7;
15+
int board_height = 5;
16+
double mar = 0.5;
17+
namedWindow("Video Player");//Declaring the video to show the video//
18+
VideoCapture cap(0);//Declaring an object to capture stream of frames from default camera//
19+
if (!cap.isOpened()){ //This section prompt an error message if no video stream is found//
20+
cout << "No video stream detected" << endl;
21+
system("pause");
22+
return-1;
23+
}
24+
cap.set(CAP_PROP_FRAME_WIDTH,1920);
25+
cap.set(CAP_PROP_FRAME_HEIGHT,1080);
26+
27+
// SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
28+
SDL_SetRenderDrawColor(sdl_renderer, 128, 128, 128, 255);
29+
SDL_RenderClear(sdl_renderer);
30+
std::vector<double> check_x, check_y;
31+
for (int ix = 0; ix<=board_width+1; ix++) check_x.push_back((ix + mar) * window_width / (board_width+1+2*mar));
32+
for (int iy = 0; iy<=board_height+1; iy++) check_y.push_back((iy + mar) * window_height / (board_height+1+2*mar));
33+
for (int ix = 0; ix<=board_width; ix++) {
34+
for (int iy = 0; iy<=board_height; iy++) {
35+
if ((ix+iy) % 2 == 0) {
36+
SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
37+
} else {
38+
SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, 255);
39+
}
40+
SDL_Rect rect;
41+
rect.x = check_x[ix];
42+
rect.y = check_y[iy];
43+
rect.w = check_x[ix+1] - check_x[ix];
44+
rect.h = check_y[iy+1] - check_y[iy];
45+
SDL_RenderFillRect(sdl_renderer, &rect);
46+
}
47+
}
48+
SDL_RenderPresent( sdl_renderer );
49+
50+
Mat corners;
51+
int state = 0;
52+
while (true) {
53+
Mat myImage, gray;
54+
Mat corn;
55+
cap >> myImage;
56+
cvtColor(myImage, gray, COLOR_BGR2GRAY);
57+
bool ret = findChessboardCorners(gray, Size(board_width,board_height), corn);
58+
if (ret) {
59+
cornerSubPix(gray, corn, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 30, 0.1));
60+
drawChessboardCorners(myImage, Size(board_width,board_height), corn, ret);
61+
corners = corn;
62+
state = 2;
63+
}
64+
{
65+
if (state == 1) {
66+
SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 0, 255);
67+
} else if (state == 2) {
68+
SDL_SetRenderDrawColor(sdl_renderer, 0, 255, 0, 255);
69+
state = 1;
70+
} else {
71+
SDL_SetRenderDrawColor(sdl_renderer, 255, 0, 0, 255);
72+
}
73+
SDL_Rect rect;
74+
rect.x = 10;
75+
rect.y = 10;
76+
rect.w = 20;
77+
rect.h = 20;
78+
SDL_RenderFillRect(sdl_renderer, &rect);
79+
SDL_RenderPresent( sdl_renderer );
80+
}
81+
82+
imshow("Video Player", myImage);//Showing the video//
83+
char c = (char)waitKey(1);//Allowing 25 milliseconds frame processing time and initiating break condition//
84+
if (c == 27){ //If 'Esc' is entered break the loop//
85+
break;
86+
}
87+
}
88+
printf("%d %d\n",(int) corners.size().width,(int) corners.size().height);
89+
90+
91+
Size target_size(window_width,window_height);
92+
std::vector<cv::Point3f> object_points;
93+
double square_size = target_size.width/(board_width+1);
94+
for (int i = 0; i < board_height; ++i) {
95+
for (int j = 0; j < board_width; ++j) {
96+
object_points.push_back(cv::Point3f(check_x[j+1], check_y[i+1], 0));
97+
}
98+
}
99+
100+
Mat H = findHomography(object_points, corners);
101+
while (true) {
102+
Mat myImage, warped_image;
103+
cap >> myImage;
104+
warpPerspective(myImage, warped_image, H, target_size,WARP_INVERSE_MAP);
105+
imshow("Video Player", warped_image);//Showing the video//
106+
char c = (char)waitKey(1);//Allowing 25 milliseconds frame processing time and initiating break condition//
107+
if (c == 27){ //If 'Esc' is entered break the loop//
108+
break;
109+
}
110+
}
111+
112+
}
113+
114+
115+
gui_window::gui_window(int window_width_, int window_height_, Solver * solver_) : solver(solver_), window_width(window_width_), window_height(window_height_) {
116+
output("Initializing SDL window\n");
117+
// sdl_window = SDL_CreateWindow("Graphical Window", SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), sx, sy, SDL_WINDOW_FULLSCREEN);
118+
sdl_window = SDL_CreateWindow("Graphical Window", 0, 0, window_width, window_height, 0);
119+
check_pointer(sdl_window);
120+
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
121+
check_pointer(sdl_renderer);
122+
sdl_display = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, window_width, window_height);
123+
check_pointer(sdl_display);
124+
//SDL_SetRenderTarget(sdl_renderer, sdl_display);
125+
126+
sdl_surface = SDL_CreateRGBSurface( 0, solver->region.nx, solver->region.ny, 32,
127+
0xFF000000,
128+
0x00FF0000,
129+
0x0000FF00,
130+
0x000000FF
131+
);
132+
check_pointer(sdl_surface);
133+
sdl_texture = SDL_CreateTexture(sdl_renderer,SDL_PIXELFORMAT_ABGR8888,
134+
SDL_TEXTUREACCESS_STREAMING | SDL_TEXTUREACCESS_TARGET,
135+
solver->region.nx, solver->region.ny);
136+
check_pointer(sdl_texture);
137+
CudaMalloc( &outputBitmap, sizeof(uchar4)*solver->region.sizeL());
138+
srcrect.w = solver->region.nx;
139+
srcrect.h = solver->region.ny;
140+
srcrect.x = 0;
141+
srcrect.y = 0;
142+
143+
double sx = (double) window_width / solver->region.nx;
144+
double sy = (double) window_height / solver->region.ny;
145+
double s = sx;
146+
if (sy > s) s = sy;
147+
dstrect.w = s * solver->region.nx;
148+
dstrect.h = s * solver->region.ny;
149+
dstrect.x = 0;
150+
dstrect.x = (window_width - dstrect.w)/2;
151+
dstrect.y = (window_height - dstrect.h)/2;
152+
153+
calibrate();
154+
155+
}
156+
157+
int gui_window::eventloop() {
158+
SDL_Event event;
159+
int ret = 0;
160+
solver->lattice->Color(outputBitmap); // Updating graphics
161+
SDL_LockSurface(sdl_surface);
162+
CudaMemcpy(sdl_surface->pixels, outputBitmap, sizeof(uchar4)*solver->region.sizeL(), cudaMemcpyDeviceToHost);
163+
SDL_UnlockSurface(sdl_surface);
164+
165+
SDL_UpdateTexture(sdl_texture, NULL, sdl_surface->pixels, sdl_surface->pitch);
166+
167+
SDL_SetRenderDrawColor(sdl_renderer, 0, 0, 0, 255);
168+
SDL_RenderClear(sdl_renderer);
169+
SDL_RenderCopy(sdl_renderer, sdl_texture, &srcrect, &dstrect);
170+
SDL_SetRenderDrawColor(sdl_renderer, 255, 255, 255, 255);
171+
SDL_RenderDrawLine(sdl_renderer, 0, 0, 200, 200);
172+
SDL_RenderPresent( sdl_renderer );
173+
174+
while( SDL_PollEvent(&event) )
175+
{
176+
if (event.type == SDL_QUIT) {
177+
exit(0);
178+
ret = 1;
179+
}
180+
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) {
181+
exit(0);
182+
ret = 1;
183+
}
184+
}
185+
return 0;
186+
}
187+
188+
gui_window::~gui_window() {
189+
CudaFree( outputBitmap );
190+
output("Killing SDL window\n");
191+
}
192+

src/gui.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
This software contains source code provided by NVIDIA Corporation
3+
taken from materials for the book "CUDA by example"
4+
Modified by L Laniewski-Wollk for CLB project
5+
*/
6+
7+
8+
#ifndef GUI_H
9+
#define GUI_H
10+
11+
#include "Consts.h"
12+
#include "Global.h"
13+
#include "Solver.h"
14+
#include <cuda.h>
15+
#include <cuda_gl_interop.h>
16+
17+
#include <SDL2/SDL.h>
18+
#include <SDL2/SDL_ttf.h>
19+
#include <SDL2/SDL_image.h>
20+
21+
22+
struct gui_window {
23+
Solver * solver;
24+
int window_width;
25+
int window_height;
26+
SDL_Window* sdl_window;
27+
SDL_Renderer* sdl_renderer;
28+
SDL_Texture* sdl_display;
29+
SDL_Texture *sdl_texture;
30+
SDL_Surface* sdl_surface;
31+
uchar4* outputBitmap;
32+
SDL_Rect srcrect, dstrect;
33+
int calibrate();
34+
public:
35+
gui_window(int window_width_, int window_height_, Solver * solver_);
36+
~gui_window();
37+
int eventloop();
38+
};
39+
40+
#endif // __GPU_ANIM_H__
41+

src/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727

2828
#include "GetThreads.h"
2929

30+
#ifdef WITH_SDL_WINDOW
31+
#include "gui.h"
32+
33+
gui_window* window = NULL;
34+
#endif
35+
36+
3037
// Reads units from configure file and applies them to the solver
3138
int readUnits(pugi::xml_node config, Solver* solver) {
3239
pugi::xml_node set = config.child("Units");
@@ -150,6 +157,14 @@ int MainCallback(int seg, int tot, Solver* solver) {
150157
}
151158
MPI_Bcast(&steps, 1, MPI_INT, 0, MPMD.local);
152159
solver->EventLoop();
160+
#ifdef WITH_SDL_WINDOW
161+
if (window != NULL) {
162+
int ret = window->eventloop();
163+
if (ret) {
164+
ERROR("Should close\n");
165+
}
166+
}
167+
#endif
153168
CudaEventRecord( start, 0 );
154169
CudaEventSynchronize( start );
155170
iter = 0;
@@ -400,6 +415,11 @@ int main ( int argc, char * argv[] )
400415
CudaEventRecord( start, 0 );
401416
solver->lattice->Callback((int(*)(int, int, void*)) MainCallback, (void*) solver);
402417

418+
#ifdef WITH_SDL_WINDOW
419+
window = new gui_window(800,600,solver);
420+
#endif
421+
422+
403423
// Running main handler (it makes all the magic)
404424
{
405425
Handler hand(config, solver);
@@ -409,6 +429,10 @@ int main ( int argc, char * argv[] )
409429
}
410430
}
411431

432+
#ifdef WITH_SDL_WINDOW
433+
if (window != NULL) delete window;
434+
#endif
435+
412436
// Finish and clean up
413437
debug2("CudaFree ...\n");
414438
CudaEventDestroy( start );

src/makefile.main.Rt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SOURCE_PLAN+=simplepart.cpp
3838
SOURCE_PLAN+=GetThreads.h GetThreads.cpp
3939
SOURCE_PLAN+=range_int.hpp
4040
SOURCE_PLAN+=Lists.h Lists.cpp Things.h
41+
SOURCE_PLAN+=gui.cpp gui.h
4142
<?R
4243
h = dir("src/Handlers","[.](h|cpp)(|.Rt)$")
4344
h = sub(".Rt","",h)

0 commit comments

Comments
 (0)