Thursday 31 July 2014

Python binding in Visual Studio(without bjam)

I am hoping that you have already gone through my previous post Python binding with IDE.
In that post, I would have demonstrated it for Visual Studio IDE, but using Boost's own build tool bjam.

In this post, I am gonna explain how to configure Visual Studio to build out Python binding C++ code without bjam and the additional files it depends on(boost_build.jam and Jamroot. Its a relief that you dont have to change the Jamroot file accordingly, if your projects are in different folders. Helped me to resolve a lot of dependency issues without using bjam). But I would recommend bjam, if all your project files are in the same source directory. Its a great build tool by Boost!

Okay, lets get started.
  1. Start with an Empty C++ Project as mentioned earlier
    (File->New->Project Select Under 'Visual C++', select 'Empty Project')
  2. Create a new C++ file, say hello.cpp Copy the contents of the already existing hello.cpp from .../examples/tutorial.
  3. Open Project properties page. Right-click Project, select Properties. Make the following changes in that window 
  • Under 'General' :
           Target Extension : .pyd
           Configuration Type : Change to Static Library(.lib) ( Really have no idea why we have to do                  this, but the other Configuration types may cause unnecessary link errors , Dll worked for me                        though :) )
  • Under 'VC++ Directories' :
    Include Directories, add C:\Boost\1.55.0;C:\Python27\include;
    Library Directories, add C:\Boost\1.55.0\stage\lib;C:\Python27\libs;
    ( Note : Add appropriate file location in your system )

  • Under 'Linker' , you will have to add boost-python library and python library as shown below. (Copy the lib name, available under ..\Python27\libs\ and ..\Boost\1.55.0\stage\lib\ and press OK.

  • Go ahead and build the file. You will find a *.pyd file either in your Project source directory or under Debug folder.
  • Dont forget the most important step : Make sure to copy the boost_python-vc110-mt-gd-1_55.dll file available under ..\Boost\1.55.0\stage\lib\. Copy it to wherever your *.pyd file is.

    Then run your code using Python interpreter as mentioned in the previous post(Boost.Python setup )
    Hope you got the desired output.
          Feel free to provide your comments below!!!

Thursday 17 July 2014

Wrapping Functions using MACRO INVOCATION

When you want to wrap functions (or member functions) that either :
  • have default arguments (or)
  • are overloaded with a common sequence of initial arguments
we can either do a Manual wrapping or we can go for MACRO INVOCATION introduced by Boost.Python which makes wrapping simpler and easier.

Manual Wrapping 


Overloaded Functions 

We have here our C++ example class :



Class hello has 3 overloaded functions. To expose these in Python, we have to wrap them manually as shown below, else Python wouldn't understand which function to call when.
This is achieved by introducing some member function pointer variables :


Default Arguments

To have an idea about how Boost.Python manual wrapping works with Default arguments, have a look at it here. Boost website provides a clear explanation for this. So, am not gonna explain it here.

Lets move onto see how MACROS makes things easier.

MACROS

The macros used for this are as below.

For global functions and static methods :



For Class Methods :



Using MACROS our above program, can be written as,


When built and run using Python interpreter, we get the following desired output.


Reference :






Wednesday 16 July 2014

Exposing C++ std::vectors to Python

Boost.Python by default includes functionality for wrapping Vectors. This makes it quite simple for exposing C++ std::vectors to Python. The following sample program will provide you a detailed understanding on how to achieve it!

Program to add the sum of integers in a vector.

C++ program (SumOfVector.h)



When you build and run the above in the command Prompt using Python, you get the following output :


Tuesday 15 July 2014

Python binding with IDE

How brilliant would it be, if Python binding can be achieved using IDE that aims to simplify software development process.
Am sure you would love it when you are able to configure your IDE to use bjam for its custom building process.
The IDE am gonna use here is Microsoft Visual Studio.Net 2012 ( you can use any IDE and configure it appropriately with the below steps).

So lets start!

Configure Visual C++ Project to use Boost.Build

  1. As Boost uses it own Build system - bjam, you will have to start with an Empty C++ Project
    (File->New->Project Select Under 'Visual C++', select 'Empty Project')
  2. And new text document and rename it to Jamroot (Note :  not Jamroot.txt)  Copy the entire Jamroot file contents from the .../example/tutorial folder here. Make sure the appropriate changes are made according to your project as mentioned in my previous post)
  3. Add another text document and rename it into boost-build.jam It just needs to contain this line,
    boost-build "C:/Boost/1.55.0/tools/build/v2";
  4. Create a new C++ file, say hello.cpp Copy the contents of the already existing hello.cpp from .../examples/tutorial.
  5. Open Project properties page. Right-click Project, select Properties. Make the following changes in that window 
  • Under 'General' :
    Configuration Type : Change to Static Library(.lib) ( Really have no idea why we have to do this, but the other Configuration types cause unnecessary link errors , Dll worled for me though :) )
  • Under 'VC++ Directories' :
    Include Directories, add C:\Boost\1.55.0;C:\Python27\include;
    Library Directories, add C:\Boost\1.55.0\stage\lib;C:\Python27\libs;
    ( Note : Add appropriate file location in your system )

  • Under 'Custom Build Step' :
    Command Line : bjam
    Outputs : .pyd
Finally, build your project. If you have done everything correctly, you should be able to Build it successfully without errors and find a *.pyd file in your project root folder.


Congratulations! You have built your project successfully with Visual Studio. The steps are quite similar for any IDE.


Reference :


[1]   http://pythonthusiast.pythonblogs.com/230_pythonthusiast/archive/1335_pycocos2d-x__how_to_properly_setup_boostpython_in_your_visual_c_2012_project.html


Monday 14 July 2014

Boost.Python setup

Now, for the tough part! Are you ready to test your patience?
What are we waiting for, lets start!

Please note, the setup process mentioned here is for Windows environment!

Software Requirements :

  1. Microsoft Visual Studio (Version I am using 2012) including Microsoft Visual C++
  2. Python ( I have used version 2.7 - Boost.Python requires version 2.2 or newer)
  3. Boost ( I have used version 1.55.0)

Build Boost.Python

  1. Download Boost and extract it to a path in your system, lets say C:\Boost\1.55.0
  2. Put this into your PATH variable, so that bjam can be run anywhere within the system. (bjam is one part of Boost's own build system, called Boost.Build).
  3. Download Python and extract it to a path in your system say C:\Python27 Add this to the PATH variable.(To check if python is installed correctly, type python in the Command Prompt and you should be able to see the python interpreter with >>> )
  4. Open 'Developer Command Prompt for VS2012' (Start->Microsoft Visual Studio 2012-> Visual Studio Tools)
  5. Navigate to the Boost_root location(C:\Boost\1.55.0)
  6. Enter bootstrap to prepare Boost in building it for our system.
  7. Then, run b2  --toolset=msvc-11.0  --build-type=complete  stage (Time for a cup of tea! :) )
    (If you ever come across link.jam : No such file or directory during the build process. Dont panic and waste your time trying to resolve it like I did, its quite harmless!)
    As a result you should be able to see two new directories in your Boost_root location 
    bin.v2 (Build directory that contains generated library files, object files and other auxillary files) and  stage (contains library files alone, ready to be deployed)
    Note :  --toolset=msvc-11.0 tells bjam to use Microsoft Visual Studio 2012 toolkit(varies depending on your visual studio version number, for 2012, its 11.0)
               --build-type=complete tells Bjam to build all possible variations of the libraries.
  8. One unfortuante thing is, for our Python project to be successfully integrated, you have to check if C:\Boost\1.55.0\stage\lib\libboost_python-vc110-mt-gd-1_55.lib is renamed to boost_python-vc110-mt-gd-1_55.lib, else you will have to copy this file into root folder of your project.
    (This annoying problem left me hours wondering what the error was!!!)

Checking correctness of our Boost.Build system installation!


Inorder to test the correctness of our installation, lets use the already existing Boost.Python example. Still in Command Prompt environment, go to C:\Boost\1.55.0\libs\python\example\tutorial You will see three files
  1. Jamroot : Boost.build system will use this file to build your application. It is similar to a Makefile
  2. hello.cpp : Main C++ class whose members are ready to be exposed to Python
  3. hello.py : A python code that uses the exposed information from the above class (Optional)
Note : Dont forget to copy the aforementioned  C:\Boost\1.55.0\stage\lib\boost_python-vc110-mt-gd-1_55.lib file into C:\Boost\1.55.0\libs\python\example\tutorial. Otherwise your build process will not be successful.

Run bjam (It will not work if you haven't set your PATH variable as mentioned in step 2)
If everything works fine, you will see a newly created hello_ext.pyd in the tutorial folder as a result of the build process. This is very much similar to a regular Windows DLL file but renamed into *.pyd file.(Python will not import a DLL file instead it will import only PYD file)
Test this by running python and import this new PYD file as below :


(or) since we already have an existing hello.py with the python code. You can simply run python hello.py to see the same output.
Let's take a look at what is inside hello.cpp file. I have added comments for better understanding.


Hope its quite starightforward!
So what are the corresponding changes that need to be made in the Jamroot file. Let's take a look at what's inside Jamroot file.
In the above file, I have changed the default BOOST_PYTHON_MODULE(hello_ext) to BOOST_PYTHON_MODULE(sample) for better understanding on what it does!


Take a look at the lines the corresponding changes ought to be made! If you are planning to change the name in your project, make sure to make the necessary changes in your Jamroot file as well.
Also, if your project is in some other directory and not within Boost directory, make sure you give the Boost path under use-project boost as shown above(see the first blue dot!)

Note : If the file path contains spaces, make sure you enclose the path within double quotes. This is applicable to any path you use in this project.
Eg : C:/Boost 1.55.0/tools/build/v2 then change it to "C:/Boost 1.55.0/tools/build/v2"
Also note for Windows, single backward slash or two forward slashes only are acceptable between filenames in a path.

This is it! We are almost done!
As a final reminder, make sure your project folder always has these three important files along with your C++ files :
  1. Jamroot (with the corresponding changes made as mentioned above)
  2. boost.build (Mainly used to locate the installation directory of Boost distribution. Its contents would be : boost-build C:\\Boost\\1.55.0\\tools\\build\\v2 )
  3. Boost library file boost_python-vc110-mt-gd-1_55.lib
Okay! Thats brilliant! We have successfully achieved python binding with this simple module.
Now, you are good to start with your own Python Binding project. 

Happy coding!

Important Note : 
If suppose you encounter an error while building Boost, it may be due to the following reason.
A file called user-config.jam may be found either in your home directory or most probably in C:\Boost\1.55.0\tools\build\v2. This file specifies where the tools and libraries are available to the Build system. If build fails, one of the many reasons might be that Boost.Build is unable to find Python and link to its corresponding libraries. You may need to edit this file to help Boost.Build find the necessary.
Add these two lines in user-config.jam and try building Boost again.

using msvc : 11.0 : "C:\\Program Files\\Microsoft Visual Studio 11.0\\VC\\bin\\cl.exe" ;
#msvc:11.0 is Microsoft Visual C++ version number, for 12 its 11. You need to change it accordingly).
using python 
    : 2.7                                    # Version
    : C:\\Python27\\python.exe  # Python Path
    : C:\\Python27\\include        # include path
    : C:\\Python27\\libs             # lib path(s)
    : <define>BOOST_ALL_NO_LIB=1
    ;


Reference :



Python Binding - Introduction

In this post am gonna give a quick overview on what Python binding actually is.

Python binding is nothing but, exposing C/C++ classes, data members/functions to Python. What does 'exposing' mean? "Member functions and data members written in C/C++ can be accessed by(or will be available to) Python by this method".
You may ask, why C++ - Python interface? Parts of program that require more flexibility than speed/efficiency can be written in Python and critical portions can be coded in C++. 

According to PythonWiki, Python has more than one way to make C/C++ functionality accessible to Python.

I am choosing Boost.Python, mainly because of its stable support.

According to QuickStart, the Boost Python Library is a framework for interfacing Python and C++. It allows quick and seamless exposure of C++ classes,functions and objects to Python, and vice-versa using no special tools - just C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that the C++ code need not be modified/altered at all inorder to wrap it, making Boost.Python ideal for exposing 3rd part libraries to Python.
The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative Interface Definition Language(IDL).

Advantages of Boost.Python
  • Comprehensive lifetime management facilities for low-level C++ pointers and references (Call Policies).
  • Support for C++ virtual functions that can be overridden in Python.
  • Wrapping of overloaded operators, STL container classes (like vector and map).
  • Support for organizing extensions as Python packages, with a central registry for inter-language type conversions.
  • As mentioned earlier, it allows exposing C++ classes and functions to Python, without an additional wrapping language, but simply using C++ compiler.

Compiling the library initially and getting it to work in your environment or IDE can be quite frustrating and time consuming.

In the next post I will guide you through the setup of Boost.Python that will ease your burden!

Happy learning!

Tuesday 11 March 2014

Masters Project

Delaunay Tetrahedralization and its dual Voronoi Diagrams

The Delaunay tetrahedralization is one of the most popular and common method used for solving problems related to meshes. It is either used for generating a mesh or for breaking it up, as Voronoi diagrams, dual of the DT is a commonly used process for that. The main task of this project is to implement a robust Delaunay Tetrahedralization structure, with a set of points generated from sampling a given 3D Mesh.

Points within the volume of the mesh can be obtained by several methods. We present two such methods for Mesh Sampling : Ray Intersection and SDF(Signed Distance Field).


Mesh Sampling from Maria Vineeta on Vimeo.


These points serve as vertices for the tetrahedrons that are a part of the combinatorial structure DT.
In this project we have used Incremental insertion algorithm, has a complexity which is worst case-optimal. With this algorithm, every point is inserted into the DT one at a time. The tetrahedra containing the newlyadded point is partitioned and new tetrahedrons are created thus updating the DT structure.
By inserting each point one at a time, we observe that only the Delaunay tetrahedra local to the point is altered and not the entire structure. Whereas in the other two paradigm, divide-and-conquer and sweep plane, the entire structure is built in a single operation. Inserting a new vertex would result in computing the whole structure again. Hence, incremental insertion is mandatory when a dynamic spatial model is built. The insertion of a single point can be achieved with two incremental insertion algorithms
1) the Bowyer-Watson algorithm and
2) flip-based algorithm.
The idea behind Bowyer-Watsons very simple. Tetrahedrons that does not satisfy the Delaunay criterion when a point is inserted, are deleted. However, this method is prone to errors as it forms a ‘gap’ or ‘hole’ when the tetrahedrons are deleted and affects the overall topological structure as well. Hence, flip based algorithm is preferred over the former.

Interactive video showing DT construction using Flip based Algorithm 



Construction of a DT structure(A Step-by-step implementation) from Maria Vineeta on Vimeo.

Final DT construction using several Meshes


Delaunay Tetrahedralization(DT) and its dual Voronoi Diagram(VD) from Maria Vineeta on Vimeo.

Renderman Shader

Potato Shader

Primitive Shape of the  Potato 


BRDF Model

     
Potato without BRDF
Potato with diffuse implemented

   
Potato with specular highlights


Displacement shader with varying Layers of noise.



 Final Rendered Image Without Shadows



  Final Rendered Image With Shadows







Dynamism using MAYA


Car Model




To obtain a realistic crash sequence between two cars, I employed Maya's nDynamics technique. The car to be crashed acted as an nCloth while the other car was made the Passive collider. A new cache was then created to obtain the desired crash effect between the cars.I tried several collision objects to the car to make it look more realistic. The collission object had to be animated to create the shatter effect and it had a good effect when Motion blur was added to it during rendering. It took a lot of time to render, but once it had finished it had a pretty convincing shatter effect.

The exterior of the Car was modeled using the blueprints of a Nissan Car.The reference images obtained were then edited in Photoshop, to obtain the appropriate image proportions on all the images. Polygon box modeling was used to design various parts of the car separately and were combined together at a later stage.
The car's tyre was then modeled separately and animated, to achieve the motion blur effect and then imported into the original maya file.
The second red car was obtained by duplicating the first car and by changing its color to red.
For the fire explosion between the cars, the blast explosion from Visor is used.

Textures :


The car's texture is mia_car_paint under mentalray.
The image used on the car headlight obtained from car headlight image posted by Blinkenberg(2004),in which the color was edited in Photoshop to match the car's colour. 
The car's glasses has the chrome mia_material, and its attributes were altered to achieve the desired reflective effect.
The Bournemouth University background image (Universities News 2012) was used as a texture on a plane and altered to match the car explosion.

Lighting :


I used Image based Lighting to illuminate the scene so that the environments reflection would be visible on the cars windows.
I have used five area Lights.
One to illuminate the University nameboard and the other to illuminate the explosion of the cars.
Three more are used to reflect the explosion in the car's glasses of the two cars.
The output was finally rendered using mentalray.
I have done colour correction and added some blur to the image obtained from maya, using Photoshop CS6 to obtain a more realistic look to the image.


Final Dynamism Image created using MAYA




IOS Game

Unreal Development Kit

Developed this game using Unreal Engine, with a group of eight other members. 

I was responsible for developing the User Interface and HUD for the game.

UI DESIGN FOR GAME SHAFT 37_x25 :


This was achieved with the help of Unrealscript and Kismet.
The HUD was designed in Adobe flash. I integrated it with UDK using Unrealscript to make it interact with a custom trigger class named TriggerHealth.uc.
Inorder to make unrealscript communicate with the Flash file, ActionsScipt 2.0, the scripting language of Flash was used. FlashPlayer 8 and ActionScript 2.0 were the compatible settings for ios. The swf file was generated using Scaleform plugin that uses GFxMediaPlayer to generate that file.
Function calls were made from UnrealScript to Actionscript. Function activateTNT calls actionscript from Unrealscript whenever a trigger is triggered by the player. Similarly, ActionScript communicates with UnrealScript. Actionscript calls a function setOptionsInfoUScript using ExternalInterface.call.

These functions were used for Options menu and Pause menu, where the option set has to be communicated with UDK as shown in Figure . This is handled through Kismet Event OpenGFxMovie which gives UDK the control to access the Flash file.
Similarly, button clicks are communicated to Kismet using the FsCommand node.
The integration with UDK was done by using a demo game. Integration consumed a whole lot of our time and was not easy as we thought it would be. Finally, the design of all the pages was done and integrated into UDK. 





Monday 10 March 2014

Physics Simulation

Rigid Body Dynamics


Rigid Body Dynamics deals with the linear and angular motion of non-deformable objects in 3D space. Detecting a collision when two or more rigid bodies come in contact and the way they respond to the collision are crucial to simulate a realistic motion.
A very basic rigid body simulation is implemented in this project. Though, a very simple collision detection method is used, the impulse method used to resolve collisions is quite powerful as it handles response to collisions quite effectively. But, there are several areas in this project where improvement can be made to achieve better results. Though lot of work is to be done to improve this project, it was a really good experience working on rigid body simulation and helped gain an understanding to the way physics works.



COLLISION DETECTION


Collision detection is an important aspect in physical simulation. Rigid objects freely moving and rotating in our rigid world, will have to collide with each other at some point in time. There are several collision detection algorithms used for accurately calculating the collision between different objects. The complexity of the algorithm depends upon the shape of the rigid bodies.
For computational efficiency, bounding volumes are most commonly used to detect collisions. The whole object or mesh is enclosed within a closed volume. A simple shape like sphere or cube is chosen to enclose complex objects to improve efficiency of geometric operations. In collision detection, when two bounding volumes do not intersect, then it is concluded that the objects do not intersect as well.
For our simulation, since we are dealing with simple objects and to keep the collision detection simple, we are enclosing all our rigid bodies within a sphere.

Bounding Sphere


Bounding sphere is one in which the objects are enclosed within a sphere. It is represented by a centre and a radius. Collision detection between bounding spheres is very quick and easy to implement. Hence, this method is chosen for our simulation due to time constraint.
A collision is detected if two bounding spheres intersect each other. The intersection is detected if the distance between their centres does not exceed the sum of their radii.

The flip side in using bounding spheres is that it has more wasted space than a bounding box. Using them can give less accurate results during collision. The two objects detects collision when the bounding spheres touch each other, but when in reality the objects actually doesn’t touch each other.

During collision, contact information is calculated and stored in ContactData class (Figure 4). The data calculated consists of :
Contact normal    –  the normal direction along which the colliding objects has to move after resolution.
Penetration depth – the distance along contact normal, to move the objects so that they don’t intersect each                                 other.
Contact point        – the point where the objects intersects.

COLLISION RESOLUTION


Once a collision is detected and contact data calculated, the collision has to be resolved to obtain appropriate simulation. According to Millington, when two objects encounter a collision, their direction of movement after collision can be obtained from their movement before collision. He terms this as Collision Resolution.
When a collision occurs between two rigid bodies, a collision impulse is calculated. This impulse prevents interpenetration by changing the velocity of the colliding objects instantly. Impulse can be thought if as a huge force integrated over a really short period of time. As force changes the velocity over time, impulse changes the velocity instantaneously. Impulse can be calculated and applied at the point and instant of collision. Since rigid bodies have rotation to them after colliding angular velocity too has to be calculated along with linear velocity.

To calculate the impulse magnitude and direction we use the collision model called as “Newton’s Law of Restitution for Instantaneous Collisions with No Friction.”. This model assumes that the collision process takes no or very small amount of time. At this time, non-collision forces like gravity are not taken into account though they exist before and after collision.
Newton’s Law of Restitution introduces another new quantity, the coefficient of restitution(e). The speed at which the objects separate after collision is controlled by this parameter. Its usually between 0 to 1, differs according to different materials involved in collision. A material with high bouncing effect like ball will have high coefficient of restitution, whereas clay or snowball will be almost zero.
This collision model assumes there is no friction, hence there is no tangential impulse and collision is entirely in the normal direction.

Few problems occurred when the above collision resolution equations were implemented. The main problem was interpenetration between colliding objects. It was quite complex to find the exact time at which two objects touch each other without penetration. Hence, to solve this easily a small amount of penetration is added to impulse to push the objects apart. This handled the resting contact as well, that is when objects come to rest.
Another complication is multiple contacts. To resolve this, separate Contact data is stored for every colliding object, thus handling it.


Houdini Digital Asset

City Generation


Screenshot 1 : Image based map using dummy buildings



Screenshot 2 : Voronoi based map using actual buildings




Video