essay on programming languages, computer science, information techonlogies and all.

Monday, December 15, 2014

An attempt on parallel execution in OpenCL

To achieve heterogeneous computing in full strength, it is needed to use every bit of computing power of a system and also every bandwidth possible. One important thing for that road is parallel execution. And one of parallel execution is parallelism between kernel and bus. In other word, when a GPU is running a kernel, the bus should prepare the next data to process.

First of all, the hardware of GPU should be constructed in such a way that when a kernel is running and accessing the memory in it, the bus e.g. PCIe connecting GPU and PC main memory should be able to transfer the next data to run the kernel. I found that it is not so easy to figure out in datasheets of GPU boards.

I tried with my laptop - HP Pavillion e119wm with AMD A10 and AMD Radeon HD 8650G. And installed AMD APP 2.9. I made up the kernel and data size so that time for kernel and data transfer are equal. Refer below the screen shot using AMD Profiler.


Then I tried using out-of-order queue and putting events to run data transfer for next image start with kernel execution. Though it is pushed out to the last somehow.


Then I tried making two in-order queue. Still it is serialized somehow as below screenshot.

Going through internet, found that OpenCL of AMD APP does not support the out-of-order queue, but not easy to find out about report of multiple queue parallel execution. I will try to update when I have more news.

Thursday, June 26, 2014

SketchUp motion script uploaded on github

Here sketchup-motion.git is the git that contains the ruby script that can make animation in the SketchUp.

And here is a PCB inspection equipment animation.



Have you noticed that the cable duct is also moving around as the stage moves ? I had to spent some times to make this works with Ruby API. It is crude mimic but it should tells how the cable duct works.

Tuesday, April 15, 2014

Heartbeat of SketchUp animation pumping Ruby script

The heartbeat is a timer that ticks regularly. This tick runs Ruby script block, which increments position or angle of SketchUp's instance toward target. Refer below code snippet for a timer usage and block execution by the timer tick.

def run( model, period )
timer_id = UI.start_timer( period, true) do # for every beat of lub dub lub dub, run below block
@actions.delete_if do |a|
a.tick( period ) # move around instance
a.arrived? # remove if instance arrived at target
end
model.active_view.invalidate # update scene
if @actions.empty? then
UI.stop_timer timer_id # rest in peace if nothing left
end
end
end

In the code, I said the heart beats at precise interval of 'period' and you can say that I should somehow replace the timer with atom clock to get that behaviour. Well, we may meet in the middle by measuring duration; We can't make consistent period but we can get elapsed time between consequent timer calls. Refer below code.

attr_reader :last_time
def initialize
@last_time = 0.0
end
def run( model, period )
timer_id = UI.start_timer( period, true) {
if @last_time == 0.0
@last_time = Time.now.to_f
elapsed_time = period
else
current_time = Time.now.to_f
elapsed_time = current_time - @last_time
@last_time = current_time
end
@actions.delete_if do |a|
a.tick( elapsed_time )
a.arrived?
end
# snip snip
end


Below is code for object translation. 'entity.transform!' updates the position. This change will be shown by model.active_view.update - search up above code.

Updating position in the middle is easy as it is moving in constant speed. Difficulty lies at the boundaries - reminding me of studying differential equations, asking for great care on boundary condition at all times. In here I have to figure out where and when to stop. I am just checking the remaining distance to the target position in here.



class Translation < Action
attr_accessor :target, :velocity, :speed
def tick( duration )
return if arrived?
ds = @speed * duration # distance to cover in this tick
d = (@target - @entity.transformation.origin).normalize # direction vector in unit distance
d.x, d.y, d.z = d.x*ds, d.y*ds, d.z*ds # distance vector toward target
s = @entity.transformation.origin # start position
e = s + d # end position
dt = s.distance @target # distance between start and end
if ds >= dt # if reached the target
d = @target - s; # update distance vector with remaining distance
@is_arrived = true
end
@entity.transform! Geom::Transformation.new( d ) # update position of SketchUp instance
...
end
end

This simple approach - constant speed - makes unrealistic movements at the boundaries. It starts and stops in infinite acceleration. Better simulation should involve smooth change at ends.

Reference : Animate Yo' Cheese

Tuesday, April 8, 2014

One step further with Sketchup and RubyAPI

Trying to impress audience, I have gone further on this simulation and created this glass handling robot interacting with AOI and cassette.



Apparent complex movement are all come down to either rotation or translation in a certain axis. And then these simple motions are chained to each other or set to run concurrently. In the end, it is all about designing class to easily create, link and launch motions.

Wednesday, March 26, 2014

Animation in SketchUp using RubyAPI

Having tried to animate objects using dynamic component in SketchUp, I feel that it isn't adequate to make a complicate movement. But before giving up, found that there is Ruby API which can allow to move around object at a timer call. As timer call will be periodically ticking which can increment position of object to desired position, it will definitely the right way to animate objects.

It takes time to be fluent on Ruby as it is a script language which has lots of unfamiliar concepts and syntactic sugar. But it is worth investment as I can make below animation



The outliner at the above video shows the hierarchy of the objects. For example, 'tractor_base' has 'tractor's and 'grip_base' which are moved along as the 'tractor_base' moves. Also note that 'glass' is rebased to 'tractor_base' when it is loaded on the stage and then rebased to 'bed' after it is released from the grip.

And the Ruby console shows internal debugging prints that must means nothing to outsider but invaluable information for the programmer.

I will describe what Ruby code is running behind from next blogs. But for those who can't wait, below shows what is driving in high level.

require "sketchup.rb"
load "e:/aux_depots/sketchups/sandbox/Motion.rb"
include Motion
hierarchy = Hierarchy.new( Sketchup.active_model )
glass = hierarchy.walk_definition( "glass" ).first
tractor_base = hierarchy.walk_definition( "tractor_base" ).first
grip_base = hierarchy.walk_definition( "grip_base" ).first
camera_base = hierarchy.walk_definition( "camerabase" ).first
bed = hierarchy.walk_definition( "bed" ).first
program = Movement.new
program.return_home_when_finished( tractor_base, camera_base, grip_base )
program.when_finished= lambda { |m| puts 'done!' }
program.chain(
Chain.MoveDir( glass, :AXIS_X, 1.0.m, 600.0.mm ),
step_glass_insert = Chain.MoveDir( glass, :AXIS_Z, -30.0.mm, 100.0.mm ),
Chain.MoveDir( grip_base, :AXIS_Z, -40.0.mm, 400.0.mm ),
Chain.MoveDir( tractor_base, :AXIS_X, 1.0.m, 400.0.mm ),
Chain.MoveDir( camera_base, :AXIS_Y, 25.0.mm, 50.0.mm ),
Chain.MoveDir( tractor_base, :AXIS_X, -1.0.m, 400.0.mm ),
Chain.MoveDir( camera_base, :AXIS_Y, 25.0.mm, 50.0.mm ),
Chain.MoveDir( tractor_base, :AXIS_X, 1.0.m, 400.0.mm ),
Chain.MoveDir( camera_base, :AXIS_Y, 25.0.mm, 50.0.mm ),
step_last_scan = Chain.MoveDir( tractor_base, :AXIS_X, -1.0.m, 400.0.mm ),
Chain.MoveDir( camera_base, :AXIS_Y, -75.0.mm, 50.0.mm ) )
step_glass_insert.when_arrived = lambda { |e|
glass = Hierarchy.rebase( glass, tractor_base, Geom::Point3d.new(0,0,303.0.mm) )
}
step_last_scan.when_arrived = lambda { |e|
glass = Hierarchy.rebase( glass, bed, Geom::Point3d.new(0,600.0.mm,603.0.mm) )
program.chain(
Chain.MoveDir( grip_base, :AXIS_Z, 40.0.mm, 400.0.mm ),
Chain.MoveDir( glass, :AXIS_Z, 30.0.mm, 100.0.mm ),
Chain.MoveDir( glass, :AXIS_X, -1.0.m, 600.0.mm ) )
}
program.run( Sketchup.active_model, 0.1 )
view raw stage02.rb hosted with ❤ by GitHub


Saturday, February 1, 2014

Embedding Lua in C++

Lua is a script language. It is light weight and easily embedded in C. Below is a code snippet that embeds Lua and retrieves result back from the script.
// copy lua5.1.dll to output path
// link lua5.1.lib
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
typedef boost::shared_ptr<lua_State> LuaStatePtr;
{
LuaStatePtr L( luaL_newstate(), lua_close ); // lua_close is called automatically when the lua_State is not reference in any place.
int width = 0, height = 0;
Foo( L.get(), "dimension.lua", &width, &height );
}
void Foo( lua_State* L, const char *filename, int *w, int *h )
{
if( luaL_loadfile( L, filename ) ||
lua_pcall( L, 0, 0, 0 ) )
{
throw Exception( StringFormat::As(
_T("cannot run config file : %s"), CA2CT( lua_tostring(L,-1) )));
}
lua_getglobal(L, "width"); // the 'width' value is pushed to the stack
lua_getglobal(L, "height"); // then 'height' is pused
if( !lua_isnumber(L,-2))
{
throw Exception( _T("'width' should be a number"));
}
if( !lua_isnumber(L,-1))
{
throw Exception( _T("'height' should be a number"));
}
*w = lua_tointeger( L, -2 ); // stack -2 is where the 'width' was pused
*h = lua_tointeger( L, -1 ); // stack -1 is where the 'height' was pushed
}
view raw LuaSample.cpp hosted with ❤ by GitHub
And the script used from above C++ code can be like below.
-- dimension.lua
width = 200
height = 300
view raw dimension.lua hosted with ❤ by GitHub


To further assist the embedding and interacting with C/C++, there is Luabind. You can make method and class binded to Lua script. And it also provides utility class that hide the details of stack.

Refer below code snippet to include Luabind in the C++ code.
// luabind 0.9.1
// copy luabind.dll ( or luabindd.dll ) to output path
// link luabind.lib ( or luabindd.lib )
#pragma warning( push )
// need preprocessor : LUABIND_DYNAMIC_LINK
#pragma warning(disable:4251)
#include "luabind\luabind.hpp"
#include "luabind\object.hpp"
#pragma warning( pop )
void foo()
{
LuaStatePtr LPtr( luaL_newstate(), lua_close );
lua_State* L = LPtr.get();
luabind::open(L);
...
}
Lua syntax is quite simple and intuitive. It can be applied to a configuration file and can be a good alternative to the INI. Refer below. Each name value pair can be string, real number, boolean or table. A name represents a table if it contains another sets of name-value pair like variable 'b' at below.

config =
{
a = 123.4, -- real number
b = { r = 0.3, g = 0.1, b = 0 }, -- table
c = true, -- boolean
d = 'Hello World!' -- string
}
It is not necessary to enclose configuration items with 'config={ }'. Any variable can be defined at the highest level - global level. But there are other global variables used in Lua - e.g. string, preload, package, os, setmetable and so on - which cause problem when we want to iterate through config items. If we choose to use a fixed name, then it will be straight forward on iteration. Just find the 'config' variable and iterate through all variables inside of it. It will be shown in code later.

void foo()
{
using namespace luabind;
try
{
LuaStatePtr LPtr( luaL_newstate(), lua_close );
lua_State* L = LPtr.get();
luabind::open(L);
if( luaL_loadfile( L, "test.lua" ) ||
lua_pcall( L, 0, 0, 0 ) )
{
throw Exception( StringFormat::As(
_T("cannot run config file : %s"), CA2CT( lua_tostring(L,-1) )));
}
object G( globals(L) );
object config( G[ "config" ] );
double a = object_cast<double>( config["a"] ); // a <- 123.4
object b( config[ "b" ] );
double br = object_cast<double>( b["r"] ); // b <- 0.3
bool c = object_cast<bool>( config["c"] ); // c <- true
std::string d = object_cast<std::string>( config["d"] ); // d <- "Hello World!"
}
catch( ... )
{
}
}
With above code, you can simply access each variable presumed that you know the variable names and hierachy.

But what if you want to access configuration like SAX does ? There should be a way to iterate through configuration items. Lua can iterate through items in the table, and we can make the script calls you with predefined interface. Here is code snippet that can do it.
using namespace std;
using namespace luabind;
class Visitor // this is the interface class that will be binded to Lua using LuaBind.
{
public:
virtual ~Visitor() {}
virtual void Begin( const string& arg ) = 0; // when an iteration starts
virtual bool BeginTable( const string& name ) = 0; // when a table starts
virtual void VisitItem( const string& key, const string& value ) = 0; // string
virtual void VisitItem( const string& key, double value ) = 0; // real number
virtual void VisitItem( const string& key, bool value ) = 0; // boolean
virtual void EndTable() = 0; // when the table ends
virtual void End() = 0; // when the iteration ends
};
class ConfigWriter : public Visitor
{
public:
ConfigWriter() { ... }
~ConfigWriter() { ... }
void Begin( const string& arg ) { ... }
...
};
typedef boost::shared_ptr<Visitor> VisitorPtr; // Lua will hold the created instance using shared_ptr which can be destroyed when it goes out of scope.
VisitorPtr CreateVisitor( const string& arg )
{
if( arg == "ConfigWriter" )
return VisitorPtr( new ConfigWriter() );
else
...
}
void Config::Open( cont _TCHAR* filename )
{
m_LuaState = LuaStatePtr( luaL_newstate(), lua_close );
lua_State* L = m_LuaState.get();
luaopen_base(L);
luaL_openlibs(L);
luabind::open( L );
// need typedef to help luabind to figure out right overloaded method.
typedef void(Visitor::*VisitItem1)( const string&, const string& );
typedef void(Visitor::*VisitItem2)( const string&, double );
typedef void(Visitor::*VisitItem3)( const string&, bool );
// Here goes a beautiful code snippet that binds Lua with C++ thanks to LuaBind
module(L) [
class_<Visitor>("Visitor")
.def("Begin", &Visitor::Begin)
.def("BeginTable", &Visitor::BeginTable)
.def("VisitItem", (VisitItem1)&Visitor::VisitItem)
.def("VisitItem", (VisitItem2)&Visitor::VisitItem)
.def("VisitItem", (VisitItem3)&Visitor::VisitItem)
.def("EndTable", &Visitor::EndTable)
.def("End", &Visitor::End)
,
def("CreateVisitor", CreateVisitor)
];
}
int pcall_handler(lua_State* L)
{
return 1;
}
void dostring(lua_State* state, char const* str)
{
lua_pushcclosure(state, &pcall_handler, 0);
if (luaL_loadbuffer(state, str, std::strlen(str), str))
{
string err(lua_tostring(state, -1));
lua_pop(state, 2);
throw err;
}
if (lua_pcall(state, 0, 0, -2))
{
std::string err(lua_tostring(state, -1));
lua_pop(state, 2);
throw err;
}
lua_pop(state, 1);
}
void Config::Save( const _TCHAR* filename )
{
stringstream script;
script <<
"function IterateTable( visitor, table, tablename ) \n"
" if visitor:BeginTable( tablename ) == false then \n " // call ConfigWriter::BeginTable( tablename )
" return \n"
" end \n"
" for k,v in pairs(table) do \n"
" local typev = type(v) \n"
" if typev == 'table' then \n"
" IterateTable( visitor, v, k ) \n" // recursive call to itself
" elseif typev == 'string' or typev == 'boolean' or typev == 'number' then \n"
" visitor:VisitItem( k, v ) \n" // call ConfigWriter::VisitItem( k, v )
" end \n"
" end \n"
" visitor:EndTable() \n" // call ConfigWriter::EndTable()
"end \n"
"visitor = CreateVisitor( 'ConfigWriter' ) \n" // this create a C++ class ConfigWriter
"visitor:Begin('" << string(filename) << "') \n" // call ConfigWriter::Begin( filename )
" IterateTable( visitor, Config, 'Config') \n" // iterate through 'Config'
"visitor:End() \n" // call ConfigWriter::End()
"visitor = nil \n"
"collectgarbage() \n" // ensure ConfigWriter's desturctor call.
;
dostring( m_LuaState.get(), script.str().c_str() );
}

Monday, January 20, 2014

Automatic optical inspection equipment running in SketchUp by dynamic component

It will be great if you can see that the equipment you designed running even in a computer. You can find out lots of thing that can go wrong before hand. Actually, I guess that is the motto of the SketchUp. Design and see whether it works. To that extent, using component and ensuring edges of different component meet exactly is a big step forward. Next step can be seeing those components running around in times and see how each components interact with each other.

But adding another dimension - in here, it is time - is always a great challenge. Sketchup answers it with dynamic component. Here is an example that use dynamic component to run automatic optical inspection system. You can see that a glass coming and sitting on the stage and gripped by tractor and being scanned by cameras that are also moving to cover whole glass area.



To make this kind of simulation, you need to slice the whole time and put which component should move in what direction in each time slice. Refer below screen shot for this.

When user click the bed component, the "ANIMATECUSTOM( "t", 10, 0, 0, 0, 10 )" is executed which increments variable 't' from 0 to 10 in 10 seconds. Refer SketchUp OnClick Functions for detail. Of course, 't' is the time in unit of seconds.

Now we can move parts in various ways in this 10 seconds. In above screen, you can see that the red marks that 't' updates variable 'servox1' to 'servox5' and when time is 2 seconds to 4 seconds, the servox2 is incremented from 0 to 120 cm with 60cm/seconds. Then it is added up to servox and servox is fed into X position of 'tractor' which makes SketchUp draws the tractor component moves along the x-axis at that time slice.

Well, it works but you can see that it is awkward. How ugly it looks to see those variables. It will makes you crazy if you are asked to change any movement in this code. Just think about how much changes should be there if you decided to put 1 second slice after second 2. Maybe there is a better way to organize code. But still you have to write everything in a line. And also look at all those methods - even there is 'AND' method. Of course, there is OR method. Certainly this will make dynamic component in less favorable option.

Reference : Google SektchUp Workshop. Edited by Laurent Brixius, Appendix : Dynamic Components by Matt Chambers

Monday, January 13, 2014

Employ matrix operation in C++ using MTL4

Developing algorithm using matrix asks for number of tools. First a straight forward and proven tool is needed like Matlab. It is better to minimize unknowns when a new algorithm is tried. You have to be sure that the result is solely come from one source - mostly from maturing algorithm. And that is the reason why you sticks to a tool that you are familiar with.

After making up an algorithm with matrix, it is time to make it a working code. It is not desirable to stick to the algorithm development tool as it comes together with a big library and license. It is MTL4 's turn to play.

MTL stands for Matrix Template Library. It is C++ template based and make no dependencies on a library file nor dll. Hence it goes in a small foot print and can be deployed in any platform.

But as template library goes, the most of the work comes down to a successful compilation of the source code. There are number of things to note. First, download appropriate zip files and copies files to an appropriate folders. In Windows, you can download from here and copies whole usr folder and name it MTL4. Second there is conflict in variable names between MTL4 and WinDefs. Refer below code snippet to fix the compilation error in Windows.
// Variable name IN below conflicts with WinDefs.h. Change it to something else like InMatrix
// inlcude/boost/numeric/mtl/operaiton/eigenvalue.hpp
eigenvalue_solver(const Matrix& IN) : ncols(num_cols(IN)), nrows(num_rows(IN)) {
zero= math::zero(IN[0][0]);
one= math::one(IN[0][0]);
R = hessenberg(IN);
// inlcude/boost/numeric/mtl/operaiton/qr_givens.hpp
qr_givens_solver(const Matrix& IN) : R(IN), G(2,2), Q(num_cols(IN), num_rows(IN)) {
view raw gistfile1.txt hosted with ❤ by GitHub
There are number of warning that can be ignored - I know I am lazy on it and will pay the debt at someday but until then. The code to include the MTL4 will be like below.
#ifndef _SCL_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS // remove warning 4996
#endif
#pragma warning( push )
#pragma warning( disable: 4522 4996 4146 4355)
#include <boost\numeric\mtl\mtl.hpp>
#pragma warning( pop )
view raw include_mtl.cpp hosted with ❤ by GitHub
Now time to port the algorithm code to C++ code using MTL4. Refer below code snippet.
typedef mtl::dense2D<double> Matrix;
typedef mtl::dense_vector<double> Vector;
// matrix creation using array
double ct = cos(theta), st = sin(theta);
double m[][3]=
{ {ct, -st, x },
{st, ct, y },
{0, 0, 1 } };
Matrix R_c2p( m );
// inverse
Matrix R_p2c( inv( R_c2p ) );
// fill up matrix
Matrix M_p(3,1);
M_p[0][0] = MP_p.GetX(); M_p[1][0] = MP_p.GetY(); M_p[2][0] = 1;
// multiplication
Matrix u(3,1);
u = R_p2c * M_p;
// solve
Matrix Ut( trans(U) );
Vector UtV( Ut*V );
Matrix UtU( Ut*U );
Vector X( lu_solve( UtU, UtV ) );
As above code shows, the C++ code can be very similar to a code in the Matlab thanks to operator overloading in MTL4. In here, very limited features of MTL4 is shown and you can find more in the MTL4.

Friday, January 3, 2014

Installer with Wix

WiX is XML based installer builder.

As WiX is based on the XML, it can be version controlled and you can track the changes made. This is important as tool like InstallShield isn't saved to a text file. If you don't see a difference, you won't be able to debug some bug that comes up some day. If you know there is no difference in source code, you can be sure that there will be no difference in behaviour. If there is , then you can be sure that it isn't come from the installer. That will save a good time of debugging in installer.

Another advantage of WiX is that it is based on the Window Installer. If you write installer for Windows, you can be sure that this tool can fully utilize all the feature the OS provides - like rollback. How can you make rollback without OS supports ? I have no doubt that you can invent a wheel but who will credit you on a new wheel ?

Though unlike other script based installer e.g. NSIS and InnoSetup, WiX is declarative language as it comes from XML. You may find it hard to understand how one element behave. You won't be able to step through the XML. You will spend quite some time scanning through document to figure out which element should be used in what parent and still you need to figure out appropriate attributes. There is no assignment operator. There is no type. No control statement. All these makes quite unpleasant to read XML code. Probably you won't be able to remember why you use one XML element in a place after a month later.

So you have pros and cons. Simply speaking, it is a great tool to fine control the installer. But will take some dedicated mind to develop and maintain.

I am going to show you a simple WiX installer which install number of file and shortcuts. Also prerequisites will be installed also in here. Below is a xml file defines Product.
<?xml version="1.0" encoding="UTF-8"?>
<?include VersionInfo.wxi ?>
<Wix
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >
<Product
Id="07C001CE-A3E6-4E91-A063-25D20F293E2D"
Name="$(var.Company).$(var.Product)"
Language="1033"
Version="$(var.Version)"
Manufacturer="$(var.Company)"
UpgradeCode="B7F1057E-1DE0-4B9A-8AD3-C56EF5E63291"
>
<Package
InstallerVersion="200"
Compressed="yes"
InstallScope="perMachine"
/>
<MajorUpgrade DowngradeErrorMessage="A newer version of $(var.Product) is already installed." />
<MediaTemplate />
<Feature Id="ProductFeature" Title="$(var.Product)" Level="1">
<ComponentGroupRef Id="Files" />
<ComponentGroupRef Id="Shortcuts" />
</Feature>
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="$(var.Company)" />
<Directory Id="ProgramMenuFolder">
<Directory Id="MyShortcutsDir" Name="$(var.Product)" />
</Directory>
</Directory>
</Directory>
</Fragment>
<Fragment>
<ComponentGroup Id="Shortcuts" Directory="MyShortcutsDir">
<Component Guid="1DC95A5B-C1DA-4CC5-9742-23CA84DD9F79">
<Shortcut
Id="Shortcuts.$(var.Product)"
Name="$(var.Product)"
Description="Start the main application"
Target="[INSTALLFOLDER]YourApplication.exe"
/>
<Shortcut
Id="Shortcuts.Uninstall"
Name="Uninstall"
Description="Uninstall $(var.Product)"
Target="[System64Folder]msiexec.exe"
Arguments="/x [ProductCode]"
/>
<RemoveFolder Id="RemoveMyShortcutsDir" On="uninstall"/>
<RegistryValue
Root="HKCU"
Key="Software\$(var.Company)\$(var.Product)"
Name="installed"
Type="integer"
Value="1"
KeyPath="yes"
/>
</Component>
</ComponentGroup>
</Fragment>
</Wix>
view raw Product.wxs hosted with ❤ by GitHub
$(var.Product) and others are preprocessor variables which can be defined in the command line argument of WiX compiler or it can be defined by another xml file. Below is a xml file that defines these variables. You might want to update this file automatically to have a increased build number on every build.
<?xml version="1.0" encoding="utf-8"?>
<Include>
<?define Company = "YourCompanyName" ?>
<?define Product = "YourProductName" ?>
<?define Version = "YourVersionNumber e.g. 1.0.0.1" ?>
</Include>
view raw VersionInfo.wxi hosted with ❤ by GitHub
Product has GUID at Id and UpgradeCode. You want to change Id when you make a major version change but will keep the UpgradeCode throughout the product.

When a product is installed, it is customary to put a registry value of 'Installed' at the HKCU/Software/Company/Product. This can be used by other installer or program to check existence of a product.

Files feature is defined at a separate file. It contains all the files that will be copied to the target PC. But as there can be quite number of files to copy and manually define all these files can be error prone and tedious, 'Heat.exe' is provided by Wix as harvest tool. To use the heat, we can put relevant files in a directory to simplify the process. Here is a sample batch file that copies product files to a directory and run heat.
@SET TARGET_DIR=%1
@SET SOURCE_DIR=%2
echo Copying install files from %SOURCE_DIR% to %TARGET_DIR%
rmdir /S /Q %TARGET_DIR%
mkdir %TARGET_DIR%
xcopy %SOURCE_DIR%\YourApplication.exe %TARGET_DIR% /Y
xcopy %SOURCE_DIR%\*.dll %TARGET_DIR% /Y
xcopy %SOURCE_DIR%\*.ocx %TARGET_DIR% /Y
xcopy %SOURCE_DIR%\*.lua %TARGET_DIR% /Y
mkdir %TARGET_DIR%\AnotherDirectory
xcopy %SOURCE_DIR%\AnotherDirectory\*.cfg %TARGET_DIR%\AnotherDirectory /Y
rem %WIX% is defined by Wix installation to the bin path of Wix
"%WIX%bin\heat.exe" dir %TARGET_DIR% -dr INSTALLFOLDER -cg Files -gg -g1 -sf -srd -var "var.DIR.Main" -out "%TARGET_DIR%HarvetsedFiles.wxs"

Sole reason of copying files from one place to another is the heat.exe. It doesn't allow to run multiple times to merge harvest files. You run heat once and then you can't update the result xml file to feed on the next heat run. Maybe this is just one way to collect all the files to install. Anyway, you can find people using different technique like using XSLT or others to achieve their own goal.

The above batch can be executed by pre-build event of the project with below command line.
$(ProjectDir)PrepareInstallImages.bat $(SolutionDir)\InstallerPickupFilesInHere $(SolutionDir)\YourProductFilesAreHere

First argument is the target path at where the generated installer pick up files. This argument is evaluated at $(var.DIR.Main) at HarvestFiles.wx like below snippet.
<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="INSTALLFOLDER">
<Directory Id="dir9CA0ED38F3ACC979D4C8ACB87F284C88" Name="AnotherDirectory" />
</DirectoryRef>
</Fragment>
<Fragment>
<ComponentGroup Id="Files">
<Component Id="cmp796784FA6BB90C62DCA64FA754CA9F05" Directory="INSTALLFOLDER" Guid="4F477F99-2508-4416-8856-865D39E75510">
<File Id="fil8F050F2F12393A6A3865323CAF8F3E42" KeyPath="yes" Source="$(var.DIR.Main)\YourApplication.exe" />
</Component>


But this file will be updated with new id and GUID whenever you run the build. It may not desirable. If you want to keep the change minimized between each version of installer, you'd better copy the content to a new file say Files.wxs and use it to build installer. And later when you need to add a new file or directory, you will pick up those changes from the generated HarvestFiles.wxs and put it to the Files.wxs.

With these files in place in the VS project - Product.wxs, VersionInfo.wxs and Files.wxs - you can build the msi file that can install your product.

Though it is not the end yet. You have your files installed with the msi file but it may not run properly as it may need dependencies installed. You may need to install prerequisites like VC2010 x86 redistribution or .NET Framework 4.0 or others. Time to delve into Bundle.
<?xml version="1.0" encoding="UTF-8"?>
<?include VersionInfo.wxi ?>
<Wix
xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension"
xmlns:bal="http://schemas.microsoft.com/wix/BalExtension"
>
<Bundle
Name="$(var.Company).$(var.Product).Setup"
Version="$(var.Version)"
Manufacturer="$(var.Company)"
Copyright="Copyright (c) 2014 $(var.Company). All rights reserved."
UpgradeCode="cac171e6-3bfc-4c2f-acc2-97a8c3e87d60"
>
<util:RegistrySearchRef Id='SearchVC2010X86Installed' />
<util:RegistrySearchRef Id='SearchVC2010X64Installed' />
<BootstrapperApplicationRef
Id="WixStandardBootstrapperApplication.RtfLicense"
>
<bal:WixStandardBootstrapperApplication
LicenseFile=".\license.rtf"
/>
</BootstrapperApplicationRef>
<Chain>
<PackageGroupRef
Id="VC2010SP1Redist" />
<MsiPackage
Id="YourProductId"
SourceFile="$(var.OutDir)\YourProduct.msi"
/>
</Chain>
</Bundle>
<Fragment>
<util:RegistrySearch
Id ="SearchVC2010X86Installed"
Variable="IsVC2010X86Installed"
Root="HKLM"
Key="SOFTWARE\Microsoft\VisualStudio\10.0\VC\VCRedist\x86"
Value="Installed"
Result="value"
/>
<util:RegistrySearch
Id ="SearchVC2010X64Installed"
Variable="IsVC2010X64Installed"
Root="HKLM"
Key="SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\VC\VCRedist\x64"
Value="Installed"
Result="value"
/>
<PackageGroup Id="VC2010SP1Redist">
<ExePackage
Id="VC2010SP1Redistx86"
Cache="no" Compressed="yes"
PerMachine="yes" Permanent="yes" Vital="yes"
SourceFile=".\prerequisites\vc2010\vcredist_x86.exe"
InstallCommand="/quiet /norestart"
InstallCondition="(NOT VersionNT64 AND NOT IsVC2010X86Installed)"
/>
<ExePackage
Id="VC2010SP1Redistx64"
Cache="no" Compressed="yes"
PerMachine="yes" Permanent="yes" Vital="yes"
SourceFile=".\prerequisites\vc2010\vcredist_x64.exe"
InstallCommand="/quiet /norestart"
InstallCondition="(VersionNT64 AND NOT IsVC2010X64Installed)"
/>
</PackageGroup>
</Fragment>
</Wix>
view raw Bundle.wxs hosted with ❤ by GitHub
There is "xmlns:util" which is same as in Product project. But in here, as it is a Bundle, you need to put "-ext WixUtilExtension.dll" at Tool Settings / Additional parameters / Compiler. This is to let the Wix compiler to figure out external dll. I found it weird though.

Note that the RegistrySearch is from util which is WixUtilExtension. The basic Wix RegistrySearch isn't working at the Bundle - how come ! - and you should use util:RegistrySearch instead.

For the VC2010 x86 and x64 redist, you can check registry whether those are installed before or not. Refer How to detect the presence of the Visual C++ 2010 redistributable package

The result of this bundle project is an exe file with those prerequisite files contained in addition to the msi file of your product.

Reference : WiX : A Developer's Guide to Windows Installer XML, Nick Ramirez.