Uncategorized

C++ Properties – C# like getter and setter

Hello there, back with a new snippet of code.

Since i have seen the C# getters and setters and i wanted to write such things in c++ so bad, the main reason is that they are too elegant in code and part of me being lazy so i can write expression like this “point.X *= point.Y;” instead of “point.setX(point.getX()*point.getY());”.

now let’s have a look now on the Property Class:

First thing that you will notice is the enum Permission it’s just there to control the level of permission on the values, the class itself has 3 template arguments, the first one is the value that you will store in it, the second is the class that holds this value, and the third and last one is the permission.

The Class constructor is fairly simple it just takes the class instance that holds the value and two pointers the first one is the get function pointer, and the second one is the set function pointer.

As you can see there’re two main functions the operator=, and the operator Prop(), simply the first function is for writing value to the property, it will handle the equal operator, and the second function is used to cast this class the property class into the value class and returning the resulting value from the get function.

Now, I think i’ll leave you alone with the code, happy Coding

Property.h
[gist https://gist.github.com/7bacb88a72ccc2885a2e file=”Property.h” /]

Source.cpp
[gist https://gist.github.com/7bacb88a72ccc2885a2e file=”Source.cpp” /]

Uncategorized

Hand Recognition & Finger Counter – OpenCV and C++

Greetings, Fellow Programmers out there in the wild, today i will discuss a little project that i have been working on it’s a finger counter project through this project we will go through isolating the hand and then counting it’s raised fingers using OpenCV.

efm

First things first, Let’s see the unfinished version in action

I have to say that OpenCV is a cool library to have and work with, it may seem hard to grasp but it’s a useful experience you’ll have.

Now to the project itself, well i was working with this fellow “Mahmoud Arafa“, we divided this little project into two pieces the first one is the Processor, the second is the Detector, now let’s get into the meat and potatoes.

The First Thought:
When we started this project we imagined the following, we will have a manager class this manager class will take care of getting input from the camera and supply the input and output between the two parts the processing and the detector, but also in order to provide the ability to do operations that may cost a lot we decided that the processor and the detector shall work in parallel, so the workflow of the application goes like this, the manager gets a frame from the camera at a constant rate, let’s say it’s 20 frame per second and pushes that stream into a queue that will be fed to the processor which will do it’s magic and then stores it’s output in a queue for the detector to perform the last step which is extracting the information from the image so, yeah it may seems naive but consider that this is the first time of us to deal with OpenCV

The Processor:
So, this chunk of code is all about managing the whole project and getting the image ready for the detector, so we had a “WindowManager” which task is to handle the output thing windows and stuff like that it’s truly a naive chunk of code but we will get through it anyway.

WindowManager.h

Now, the second one is the “TrafficOfficer“, and i will explain first why we needed that, as i mentioned above we needed the two main parts of the application to work in parallel so we had to find a way to manage the working memory and object between the two threads actually it’s three including the main thread but here we go,

TrafficOfficer.h
As you can see it’s full of queues and map to send variables through threads, with a few functions to retrieve these objects from the queues or the map.

I guess i must mention the container class of our project, this class contains the data that will be going here and there between threads and functions, it’s the “ImgToken“, this class contains the main data that out processing will need, the three main images the original, binary and the contour image, the other objects are just vectors of contours and defects and finger points that will be filled later.

ImgToken.h

Now the Processor for now it’s just a function that takes the original image and produce a binary one and it does so through 3 steps
– Blurring the image to eliminate the small noise
– Threshold the values of the image to the colors of the hand and produce binary image
– Using Erode and Dilate functions to fill holes that may be produced

FrameProcessor.h

Now the detector part, there’s about 5 steps for counting the hand that i used in this app
– Find Contours of the Binary image and store it in a vector
– Getting the biggest contour on the screen which should be the hand you can use other method that’s smarter than this.
– Using OpenCV convexHull Algorithm to get the points of the convex of the biggest contour.
– Now as we have generated the convex hull we should get the convexity defects, and those are the points that make the shape of the contour not a convex which should be by this point the inner part of the hand that’s between the fingers
– The last thing is to process this convexity defects points and get the points of Finger

Now without further talking the detector code

HandDetector


and finally, here is the repository.

Happy Coding, bye.

FalloutEngineV2.0

Fallout V2.0 – Input and other stuff– 12 of n

Hello, I’m ALIVE and back after the exams, and it’s was aweful, pray for me 😦

Now Let’s pick up where we left last time, the game loop, some cleaning has been done and some function has been added “input”, “update”, “render” … etc

FalloutEngine.cpp
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”gameloop.cpp” /]

now let’s see another class that’s has been added the “timeStep” this class holds the data of the current frame the delta time from the last frame and the whole elapsed time of the application and passes through the update function to every object.

timeStep.h
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”timeStep.h” /]

Now for the IKeyboard and IMouse, they are just an interfaces that the engine will deal to the input through them, they are fairly simple

IKeyboard.h
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”IKeyboard.h” /]

IMouse.h
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”IMouse.h” /]

GLKeyboard.h
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”GLKeyboard.h” /]

GLMouse.h
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”GLMouse.h” /]

as you can see there’re a keyboard for OpenGL that implements the interface of freeglut and there’s also the DirectX one that implements Windows API for getting input, they are actually the same code so i’ll put only the OpenGL one

the way input works is fairly simple, in every frame it gets keys state and mouse state and store it in the “_data” map for later user retrieval, more advanced implementation is to line these input events in a queue with time stamp to each one and process them in turn, but for now we don’t need this fancy implementation, maybe later.

now let’s see the input function of the engine

FalloutEngine.cpp
[gist https://gist.github.com/755d92e2dc010eeeb893 file=”input.cpp” /]

BYE.

FalloutEngineV2.0

Fallout V2.0 – Time – 11 of n

Well, it was a long week, Assignments and Quizzes everywhere, but we are back and today we’ll talk about time.

Time is a static class it’s main domain is to have some counters and functions to get time from the OS to control the fps rate of the application, it has about 7 static member variables you can find them in the code below

Time.h
[gist https://gist.github.com/20fc87c940efb686f7fb file=”Time.h” /]

Time.cpp
[gist https://gist.github.com/20fc87c940efb686f7fb file=”Time.cpp” /]

now using this class we can easily control the frame rate of any application just we need to control the variable _frameLimit

now we must go to IGXManager to handle some functions, as you can see the IGXManager had just two functions, it’s time to add more, we’ll add two functions “clearBuffers” and “swapBuffers” these two functions do clear and swap the buffers, and of course they are virtual functions

IGXManager.h
[gist https://gist.github.com/20fc87c940efb686f7fb file=”IGXManager.h” /]

GLManager.cpp
here’s the implementation of these functions in GL and the edit we did in the display and idle functions
[gist https://gist.github.com/20fc87c940efb686f7fb file=”GLManager.cpp” /]

DXManager.cpp
here’s the implementation of these functions in DX and the edit we did in the display function
[gist https://gist.github.com/20fc87c940efb686f7fb file=”DXManager.cpp” /]

FalloutEngine.cpp
Now, for the grand finale the gameloop function, this function will control the logic of the engine and manage the Time component and everything else.
[gist https://gist.github.com/20fc87c940efb686f7fb file=”FalloutEngine.cpp”/]

now if you set the frame limit to be 60 the engine will render just 60 frame every second using OpenGL or DirectX.
Happy Coding, bye.

FalloutEngineV2.0

Fallout V2.0 – Application – 10 of n

Well, this was a long break, sorry for that college and stuff, now let’s get into our engine, today we’ll be coding the Application class, this class will represent the main game or application to our engine, simply you put your code there and engine will run it.

It’s a simple class with lots of virtual functions, actually they’re 6 virtual functions

initialization and loading function these 3 function will run in the first run of application, and then there’s our three main functions that the engine will loop through input, update, render.

Appliction.h
[gist https://gist.github.com/d1edb87d71715ebaa03f file=”Application.h” /]

Application.cpp
[gist https://gist.github.com/d1edb87d71715ebaa03f file=”Application.cpp” /]

FalloutEngine.h
[gist https://gist.github.com/d1edb87d71715ebaa03f file=”FalloutEngine.h” /]

FalloutEngine.cpp
[gist https://gist.github.com/d1edb87d71715ebaa03f file=”FalloutEngine.cpp” /]

main.cpp
[gist https://gist.github.com/d1edb87d71715ebaa03f file=”main.cpp” /]

bye.

Uncategorized

Fallout V2.0 – Up and Running – 9 of n

Hey, finally today we’ll get a window with frames on it, isn’t that awesome!, let’s talk about that.

First, we need to setup function pointers in windows window class to handle DirectX functions.

WinWindow.h
Two function pointer that has the same signature as the static functions
[gist https://gist.github.com/9c9b01dc9e1d98d7f782 file=”WinWindow.h” /]

WinWindow.cpp
Calling the function pointers in the loop
[gist https://gist.github.com/9c9b01dc9e1d98d7f782 file=”WinWindow.cpp” /]

DXManager.cpp
Assigning the function pointers and in display we clear the buffer and swap the swap chain.
[gist https://gist.github.com/9c9b01dc9e1d98d7f782 file=”DXManager.cpp” /]

GLManager.cpp
Assigning function pointers and in display we clear the buffer and swap the swap chain
[gist https://gist.github.com/9c9b01dc9e1d98d7f782 file=”GLManager.cpp” /]

and that’s it now we have window with frames, using fraps you can see the frames per second for GL and DX

OpenGL
GLDirectX

DX

Happy Coding, bye

FalloutEngineV2.0

Fallout V2.0 – DirectX Init – 8 of n

Hello, today we’ll init DirectX, let’s talk about that.

today’s post is simple, we’ll just init the DirectX, so we’ll start adding some pointers in the DirectX Manager

DXManager.h
[gist https://gist.github.com/9dae80584c8691d06096 file=”DXManager.h” /]

DXManager.cpp
[gist https://gist.github.com/9dae80584c8691d06096 file=”DXManager.cpp” /]

and that’s it, the same components of OpenGL just a different code.

Happy coding, bye