Posts

Showing posts from August, 2011

Getting Started with Python

Reading and analyzing data from a text file: My gentle introduction to writing a Python script.  A script I needed to read in a log file and extract all inkjet printhead temperature values in Celcius recorded for the duration of the print run. Very simple.

Using FLTK in Visual Studio

Image
Note: for configuring FLTK in Linux environments, please refer to this post . 1. Download and extract FLTK For this particular install I used an old Visual Studio 2003 .NET version. Get the zipped file from the download section of Bill Spitzak's FLTK site. Unzip the file and place it somewhere suitable such as C:\fltk-1.1.10-source.

Getting Started with OpenGL for Windows

Image
For setting up OpenGL in Ubuntu Linux, please see this post , otherwise for Windows / Visual Studio environments, please use these following instructions: 1. Download the latest Windows drivers As stated on the OpenGLs wiki page , OpenGL more or less comes with the Windows operating system. You will need to ensure your PC has the latest drivers for your graphics hardware, however. Without these drivers, you will probably default to the software version of OpenGL 1.1 which is considerably slower.

The Big Three in C++

Constructor, destructors and assignment operators There is a rule of thumb in C++ that if a class defines a destructor, constructor and copy assignment operator - then it should explicitly define these and not rely on their default implementation. Why do we need them? In the example below, the absence of an explicit copy constructor will simply make an exact copy of the class and you end up with two classes pointing to the same memory address - not what you want.  When you delete the array pointer in one class for example, the other class will be pointing to memory to which you have no idea as what it contains. Consider the following example class which is used to house an array of integers plus its size. It is written in such a way as to guarantee a crash: [code language="cpp"] #include ,algorithm> class Array { private: int size; int* vals; public: ~Array(); Array( int s, int* v ); }; Array::~Array() { delete vals; vals = NULL; ...

Converting IEEE 754 Floating Point into Binary

This post explains how to convert floating point numbers to binary numbers in the IEEE 754 format.  A good link on the subject of IEEE 754 conversion exists at Thomas Finleys website . For this post I will stick with the IEEE 754 single precision binary floating-point format: binary32. See this other posting for C++, Java and Python implementations for converting between the binary and decimal formats.

A First Stab at boost::bind

Boost::bind is “able to bind any argument to a specific value or route input arguments into arbitrary positions.” It's a means of converting a function into an object that can be copied around and called at a later point, deferred callbacks for example.

Avoiding Memory Leaks using Boost Libraries

Using boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library :

Dynamically Created MFC Controls and their Notifications

Image
I was required to create a dialog application with dynamically created checkboxes (CButtons).  Given that selection of a certain hardware types in the software would result in completely different checkbox layouts.  In these situations, it is the software that has to automatically do the work normally done by using the Dialog Editor toolbox to insert static controls. This involves the following steps:

Getting Started with Boost Threads in Visual Studio

Image
Introduction This post aims to be an accessible introduction to getting set up with the Boost threads in Visual Studio environments for the first time.  Like with many technical subjects, there is a great deal of information out on the internet, that tells you a lot without actually showing you anything!

Integrating the FlyCapture SDK for use with OpenCV

Image
Introduction A recent stab at grabbing images from the Flea2 camera using APIs from the FlyCapture2 SDK by Point Gray Research (PGR).  Additionally, the camera was to be used in  "Format 7 mode", so that we may grab partial regions of the complete image.

Using Template Classes to Handle Generic Data Types in STL Containers

Image
A code snippet with which utilizes both template classes and STL to handle generic data types. In keeping with the spirit of this blog, I have kept the explanation to a minimum and hopefully the code posted below should be self-explanatory.  The idea is for readers to get the gist of what I am saying so that they can go off and make up more relevant examples of their own.

Getting Started with the CU Decision Diagram (CUDD) Package for Windows

Image
I have managed to download, uncompress and compile this library in a Visual Studio environment. At last.  Though a very powerful package for the implementation of binary decision diagrams, the documentation for its actual set-up for non-Unix environments seems a little sketchy and somewhat intimidating for the beginner. 

OpenCV Detection of Dark Objects Against Light Backgrounds

Image
The results of some experimentation with and comparison between raw OpenCV functions and the cvBlobsLib library to detect darker coloured spots against lighter backgrounds.

How To Re-Direct Old URLs to New URLs

Image
When transferring your old website to a new one, you will probably want to re-direct your visitors so that links pointing to outdated URLs are sent to the correct new ones.

Getting Started with OpenCV in Visual Studio

Image
OpenCV is a free, open source library that enables your computer application to "see" and make decisions from the image data it acquires.  Here are some guides for setting up OpenCV for use in Microsoft Visual Studio Environments:

General HowTos

Image
Collapse / Expand code blocks: Go into Tools -> Options -> Text Editor -> C/C++ -> Formatting Check everything or a selection as required.

Getting Started with UnitTest++ in Visual Studio

Image
For some time I wanted to incorporate unit testing into our development process. Though many think unit testing is largely a waste of time with not all code bases lending themselves easily to testing, if done in the right spirit I think it (like version control and bug tracking) makes life easier.

Simple Multithreading in C++ / MFC

Right now I am just starting with a very simple example - no fancy stuff - just a single thread to demonstrate a sequence that updates a counter 20 times per second.

Using TortoiseSVN / Subversion.

Image
Creating a new repository from scratch - In Windows Explorer, create the folder to contain your repository folder. - Right-click on this folder and select "Create Repository Here..."

Some neat examples of C++ String Formatting

Image
Some neat string formatting A pretty neat snippet found on stackoverflow some time ago (kudos David Rodriguez), that is worth repeating. Some time ago, a 'moderator' at StackOverflow removed this thread "for reasons of moderation". Fair enough, but the result is that code that many would find pretty darn useful is no longer searchable on that site. How very constructive. Here is a snapshot of that particular StackOverflow page as preserved by the WayBack Machine: http://web.archive.org/web/20090420233428/http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-snippet/470999 I'm gonna start using this. Essentially it is a bit of in-house stream formatting. This class make_string produces an instance of an object derived from ostream, which has an implicit conversion to a std::string:

Sorting Objects Using STL

Image
Here's an example of how using objects (hat-tip: Paul Wolfensberger )

Using Smart Pointers to Avoid Memory Leaks

Using boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library :

The IDLE Editor for Python

Image
Assuming you have downloaded and installed Python, whichever latest version it is, using the IDLE editor is very simple. For a proverbial "Hello World!" example follow the instructions below:

Using Function Objects (Functors) in STL / C++

Image
Generically, function objects (or functors) are class instances whose member function operator() has been defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore its type can be used as template parameter when a generic function type is expected. So why not just use straight function calls? 1. Function objects (functors) can contain states 2. A function object is a type, so it can be used as a template parameter. Unary Function Objects (single argument) to read a std::vector of integers and output them as a string separated by whitespaces Suppose you wanted to find the sum total of all the values contained in a vector.  You could loop through them sequentially in a for loop and increment them, or you could use a functor.  A suitable functor might look like this: [code language="cpp"] struct adder : public unary_function<double, void> { adder() : sum(0) {} double sum; void oper...

User Defined Predicates

Just some simple examples posted here as a means of easy lookup...

Getting Started with the FlyCapture SDK

Image
I have recently been working on a knotty problem involving control and image acquisition using the Flea ® 2 camera, using FlyCapture ® SDK by Point Grey Research, Inc.

'Blitting' a Bitmap Image to the Screen.

I recently needed to revisit this to create simple monochrome bitmaps representing the sets of nozzles turned off/on on a Xaar microarrayer printhead. Sample code here .  Essential steps are outlined in the following code [code language="cpp"] // 1. Create the uninitialized bitmap, that is compatible // with the the specified device context CPaintDC dc( this ); CBitmap bitmap; bitmap.CreateCompatibleBitmap( &dc, 20, 20 ); // 2. Create memory device context, that is compatible // with the specified device (dc in this case) CDC dcMem; dcMem.CreateCompatibleDC( &dc ); // 3. Obtain/create the bitmap and select into // the memory device context. CBrush brushblue( RGB( 0, 0, 255 ) ); CBrush brush( RGB( 0, 255, 255 ) ); CBitmap* pOldBitmap = dcMem.SelectObject( &bitmap ); dcMem.FillRect( CRect( 0, 0, 10, 10 ), &brushblue ); dcMem.FillRect( CRect( 10, 0, 20, 10 ), &brush ); dcMem.FillRect( CRect( 0, 10, 10, 20 ), &brush ); dcMem.FillRect( CRect(...

Creating Bitmap Files from Raw Pixel Data in C++

Image
Creating the byte array This post describes a means of taking data in the form of raw pixels containing RGB values as well as the image height, width and the number of bits per pixel (24 in this case) and converting this into a bitmap (BMP) file. Example Visual Studio 2010 project is downloadable from here: www.technical-recipes.com/Downloads/BitmapRawPixels.zip In this this post I originally described how I needed a means of representing binary array values (inkjet printhead nozzles turned ON/OFF) as a set of colored 'squares' or pixels, so that nozzles switched ON/OFF could be represented by two different colours - black or white. The bitmap width was always going to be 128, while the bitmap length ("SizeValue") was one of a set of discrete set of values in the range { 1000, 1250, 1251, 1350 }. For this example I did not have to worry about padding additional values

Multidimensional Arrays in C++

Declaring and initializing multi-dimensional arrays in C++ can be done not just by way of traditional pointer arithmetic, but using the STL / Boost libraries as well.  Here are some examples:

How to use the Boost Libraries in Visual Studio

Image
In this guide we will use boost::format , a Boost library requiring no separate compilation. If you do not already have Boost installed, the first task is to download and extract the Boost zip file to a destination of your choice. Official releases of Boost may be obtained from here: http://www.boost.org/users/download/. Download and extract the zip file to your destination of choice:

Installing ActiveX Controls For Communicating With Mitsubishi Microcontrollers

Image
Another recent re-revisit was necessary, this time to enable USB-based as well as COM-based communication between PC and Mitsubishi PLC microcontrollers.   Registering the ActiveX Control To register the ActiveX component so that is shows up on your toolbox, choose Tools and select Add/Remove Toolbox Items: Click the Com Components tab and choose the DLL which includes the ActiveX control you want to use: The Mitsubishi ActiveX control is now added to your toolbox: Set the include file Start Visual Studio, choose the Tools menu and select Options. In the Projects folder select VC++ Directories, choose Show directories for include files and select the location for the include file: Using the ActiveX control in your project Right-click on your form and choose "Insert ActiveX Control": Select the one you need, which will then paste itself onto your form. Generate the new ActiveX class Navigate to the Project menu and select Add Class. Select MFC c...

Object Detection Using the OpenCV / cvBlobsLib Libraries

Image
A short example of how to utilize various open source library functions that can be used to identify and analyse strongly connected components for a given input image. In the example I have given here, the image represents microarray sample spots printed to a slide using a Xaar inket printer.  Using our robotic equipment, a camera is mounted to the printhead, so that images are taken of the spots, as they are being printed on-the-fly, usually in linear groups of 12 or 32 at a time:

A Genetic Algorithm Based Routing Optimization Tool

Image
(For source code see this updated post .) Introduction This post describes the use of a tool written in C++ that could be used to assist a network designer in establishing an optimal set of virtual paths in ATM networks. In broadband ATM networks, the cellD based switching capacity is frequently built over digital cross connect system (DCS) networks.  The DCS network can be considered the backbone for connecting ATM switches and for reconfiguration.  As a consequence, the problem confronted by the system designer is how to configure the optimal topology and capacities within the DCS network.

Maintaining Recent File Lists in MFC Applications

A simple application in Visual Studio 2003 that builds on the "MyPad" application provided by Jeff Prosise in Programming Windows with MFC. Alternatively you could probably create the same by running the AppWizard to create a standard view-based application, but making sure to uncheck the Document/View architecture box, as well as the ActiveX Controls box to avoid unnecessary code.

How to Create Resizable Dialogs in MFC

Image
Example 1: Using Paulo Messina's Code Project sample A minimalist use of the CResizeableDialog class as described by Paulo Messina at CodeProject .   Hope you find the following recipe useful. Visual Studio 2003 source code is downloadable from here . 1. Create a new MFC Project 2.  In the resource editor, add a simple edit control that will be used to demonstrate how this resizing works: 3. Download the CodeProject library to a location of your choice. 4. Add this downloaded project to the same dialog project you are working on.  To do this, right-click the topmost solution folder and select Add Existing Project: So that the extra project is added: 5. Right-click on the Dialog (or whatever you called your project)  folder and select "Set Project Dependencies", making sure ResizableLib is checked: 6. In the Dialog code make sure CDialog is replaced with CResizeableDialog at the following appropriate points. (i) In the Dialog class (header file) be su...

Finding Minimal Spanning Trees using Kruskal's Algorithm in MFC / C++ / Boost libraries

Image
Kruskal's algorithm is used to find the minimal spanning tree for a network with a set of weighted links. This might be a telecoms network, or the layout for planning pipes and cables, or one of many other applications.

Avoiding Circular Dependencies using Observer Patterns in C++

Image
The basic concept is when you want information stored in one object, called the Model (or subject) to be watched by other objects, called Views (or observers). When information changes in the Model, all views attached to the Model should be notified and their statuses updated accordingly. In simpler terms, class A needs to know about class B, and vice versa, but while avoiding circular dependencies while doing so.