I always had an interest in electronics, but other interests meant I did not take it very far as far as learning it formally is concerned. I’ve always tried to learning from books, but always wished I had a more formal education to give me a strong base in the subject. So when I saw this post on lifehacker.com I jumped in to take MIT’s offering of a free web course. Could not resist learning from one of the best (if not the best) universities in the world. After 12-13 hectic weeks of existing work, schedules, completing assignments and tests I am proud to say that I have completed the course successfully with 98% (Grade A) result. Yaay. Take a look at the shiny new virtual certificate here. The real aim though was not the grade but getting more confident with working and designing circuits. I feel I’ve earned a lot more than just a certificate. I will now slowly take this forward and be able to realise some of my other hardware projects that I have so far only dreamt of achieving. But I know there is still a long way to go.
When trying to solve problems in geometry, I tend to get stuck trying to solve these problems from a geometric perspective. While this may be easy to visualise and understand (at least for me), it may not always be the best way to approach the problem. One such problem is the distance between two lines ion 3D space. I recently found that by solving the same problem using calculus can lead to a much more elegant and efficient solution. Below we take a look at the problem and compare the two methods.
First lets define the problem.
We are given four points
Points lie on line and points lie on line
For the remaining part of this article we assume that and are not parallel.
We need to find two points such that
that lies on and
that lies on and
where (the squared distance function) is minimised
The old approach:
Here is a summary of the old approach. I find this method more intuitive.
- Calculate a plane
- Calculate the line direction vectors
- Define a plane from a direction and point, where
- Calculate the line direction vectors
- Project the points onto the plane to get two lines in 2D
- Define the two projected lines in the parametric form
- Solve for and by calculating the point where the two lines meet i.e. solve the following equation
- Find the closest points on and in 3D using the values of and we just calculated
And finally some pseudocode. Beside each line, the number of floating point operations are listed, so we can compare the two methods later on.
Vector3 d1 = p1-p2; // 3 sub Vector3 d2 = p4-p3; // 3 sub // Crate the plane and project the points Plane m(d1.cross(d2), p1); // 6 mul, 6 add, 3 div, 1 sqrt Vector3 pj1 = m.project(p1); // (6 mul, 6 add, 1 sub) X 4 Vector3 pj2 = m.project(p2); Vector3 pj3 = m.project(p3); Vector3 pj4 = m.project(p4); // Calculate line direction in 2D Vector2 d34 = (Vector2)pj4 - (Vector2)pj3; // 2 sub x3 Vector2 d31 = (Vector2)pj1 - (Vector2)pj3; Vector2 d12 = (Vector2)pj2 - (Vector2)pj1; // Calculate u1, u2 in 2D float t = 1.0 / (d34.y * d12.x - d34.x * d12.y); // 2 mul, 1 sub, 1 div u = (d12.x * d31.y - d12.y * d31.x) * t ; // 3 mul, 1 sub u = (d34.x * d31.y - d34.y * d31.x) * t;// 3 mul, 1 sub // Total # of FP operations // 49 additions & subtractions, // 42 multiplications and divisions // 1 expensive sqrt
- Define and in the parametric form as parameters of and respectively
- The distance between the two lines are the points where the two lines are closest to each other.
We minimise the squared distance between the two lines
- To minimise we differentiate with respect to and and set both equation = 0
- We end up with two equations with two unknowns and . Solve for and .
- Find the closest points on and in 3D using the values of and we just calculated
This may sound a lot more complicated, but after working out and simplifying the equations we end up with a much more efficient solution. This will shown below.
Replacing the value of and rewriting the two equations above we get
We can write this in the matrix form
Solving for and to get
Note that if the lines are parallel and then any point on one line can be projected onto the other to get the distance between the two lines. Finally here is some C++ style pseudocode and a count of all operations.
Vector3 d21 = p1-p2; // 3 sub x 3 Vector3 d34 = p4-p3; Vector3 d13 = p3-p1; // m * u = x float a = d21.dot(d21); // (3 mul + 3 add ) x 5 float b = d21.dot(d34); float c = d34.dot(d34); float d = -d13.dot(d21); float e = -d13.dot(d34); // Solve for u1 &amp;amp;amp;amp;amp;amp;amp; u2 u = (d*c-e*b)/(c*a-b*b); // 4 mul, 2 sub, 1 div u = (e - b * u) / c; // 1 mul, 1 sub, 1 div // Total # of FP operations // 22 multiplications and division // 27 additions and subtractions
I was recently given a task where I needed to assign quotas to folders on a Windows system. Preferably without creating multiple users. This could be a very useful feature in budgeting disk space to various users or applications. Now if you do some quick searches on Google you will quickly realise that this specific feature is not supported by any version of Windows. Well that’s a shame! But that does not mean that there is no work around. The solution in one line is to create a virtual disk (vdisk) and mount this vdisk onto a path on the system. All applications will see this path as an ordinary folder but will be restricted by the virtual drive’s size.
On Windows 7 and Windows Server 2008
For those who are not sure how this can be done, the answer is a less well known command line utility called diskpart.exe. Diskpart is able to run scripts using the /s switch. Scripts are nothing but text files with a series of diskpart commands. So the script we need to create will need to do the following…
- Create a virtual disk
- Create a partition on this virtual disk
- Format the virtual disk
- Map the virtual disk on to a path
The script to do this is as follows. If you prefer, you can start diskpart and type these command in line by line.
create vdisk file=c:\myDisk.vhd maximum=2048 type=expandable select vdisk file=c:\myDisk.vhd attach vdisk create partition primary format fs=ntfs quick assign mount="c:\my folder"
A line by line explanation of the script is doing is this
Line 1: Creates a vdisk which will be saved as a file at c:\myDisk.vhd with a size of 2048Mb. The type=expandable part makes sure the size of file depends on the amount of data saved in your vdisk. If you skip this parameter the file will take up the entire 2048Mb of space, even if you have not saved a single byte.
Line 2: makes the vdisk current. This way all future operations will target this vdisk.
Line 3: So far we have only created a vdisk. Think of this command as connecting the vdisk to your computer (virtually) just as you would connect a USB storage device to a USB port.
Line 4: Creates a partition on the vdisk
Line 5: Format the new partition using NTFS
Line 6: Map the vdisk on to a folder. In this case it is c:\my folder, but you can change this to whatever you like.
So that’s it. I have made a small batch to automate the process for Windows 7. Get it from the link below.
On older versions of Windows
If you are using something older than XP, then I’m afraid you need to WAKE UP and realise we are in 2011. The stuff mentioned in this section will work only on Windows XP and newer versions.
The problem on older versions of Windows is that diskpart does not support virtual disks. We just need to use different tools to do the same thing, so if you skipped the previous section I suggest you go back and read it again. The bad news is that you will need to download some additional software. The good news is that all these tools are free. These are the tools you will need…
- vhdtool.exe (From here)
- vhdmount by installing Microsoft Virtual Server 2005. Note: When installing use the custom install option and only select vhdmount from the list. The other components are not needed unless you want to play with them.
As mentioned previously, the process is the same one as we did for Windows 7. The step by step process is as follows.
Step 1: Create a vdisk using vhdtool.exe using the following command line.
VhdTool.exe /create c:\myDisk.vhd 2147483648
There are two differences here. 1 – The size we enetered here is in bytes, so we need to multiply the number of bytes we want with 1048576 and 2- We don’t have the option of creating an expandable drive. So the vdisk file will take up all the space requested regardless of how much is actually stored in it.
Step 2: Connect the vdisk (only virtually of course) using vhdmount.exe
vhdmount.exe /p /f c:\myDisk.vhd
Step 3: Create a partition on the drive using diskpart. But before we do this, we must first start diskpart
1. Identify the disk number using the diskpart LIST command
Make a note of the disk number. In my case it is 1.
2. Select the disk using the command
SELECT DISK 1
3. Create the partition like we did before using the CREATE command
CREATE PARTITION PRIMARY
Step 4: Unfortunately old versions of diskpart don’t have a format command, so we must do this using the good old format.com. But format.exe only works on drive letters, so that is what we need to do using the ASSIGN command
Now, you will need to exit diskpart temporarily using the EXIT command and format the drive using good old format.com. If you have never done this or have gotn’ used to the GUI, this is how it is done from the command line…
format.com x: /Q /FS:NTFS
Step 5: Now if you don’t mind using the drive, you can stop here. Otherwise head back into diskpart to un-mount the volume. Unfortunately since we exited diskpart, we need to reselect the right volume. Get a list of volume using the LIST command as follows
Make a note of the volume number. It will have the drive letter mentioned under the Ltr column. In this case it is x. Then select it using…
SELECT volume 2
Then remove the drive letter mapping using the REMOVE command as follows
And finally map it on to your folder using…
ASSIGN mount=”c:\my folder”
And so that is it. Although you may be able to automate the process using a combination of batch files and grep, I successfully implemented a command line PHP script to automate the process on Windows XP as well. You can also probably use a VB script, but that is not something I am very familiar with. The problem is identifying the disk number in step 3 and volume number in step 5. I used the following process to determine the disk number automatically.
- Make a list of disks before plugging in the vdisk in step 2.
- Use a regex pattern match and extract a list of disk numbers.
- Mount the vdisk.
- Make another list of disks. This time, the list will have the vdisk that you just mounted.
- Identify the disk that is in the second list but not in the first one.
Identifying the volume number in step 5 if much simpler. Just use a regex pattern to identify the line which has the drive letter x. The number to the left of it will be the volume number.
There are a lot of other cool things you can do using diskpart that is not available via a GUI. If you don’t mind getting your hands dirty using the command line, then this is a good place to get started.
Looks like this blog is now being updated bi-annually. The last year or so has been quite a busy time for me. I’ve been updating my core skills. One of my main targets was to get my head round to understanding a global illumination technique that everyone seems to love so much these days – spherical harmonic lighting. It took some time for me to get my head around the math, and even more time for me to find the spare time to make an implementation. Click here to go to the page with details on the implementation and a link to the full source code. As with a lot of things, once I understood what was being done, it all looked quite simple and straight forward. With that being said, I must say I still can’t claim to be an expert on the subject, but as I time goes by I hope to continue to improve upon it as the need arises.
As a graphics programmer, I often need to find a point on a hyperplane (line or plane) closest to a given point. So far my understanding of the problem has been based on trigonometric concepts that is probably the simplest way of approaching the problem. I’m not going to explain the boring trigonometric approach to the problem here. You can find that on some other website using Google.
A few weeks back I came across a new concept in maths called Lagrange multipliers. In addition to the problem that I was addressing at the time, I was interested in seeing if it could be applied to find a point on a hyperplane closest to a given point.
If you are not familiar with Lagrange multipliers then just go to the Wikipedia page that explains it all. I think they give a better explanation of the problem than I ever will. Ok, so if you haven’t already figured out how we are going to approach this problem, read on.
Lets work in 2 dimensions with a line of the form
Lets try and visualize this…
The vector represents the normal to the line
The constant is the distance to closest point on the line to the origin.
If you are used to only seeing the line in the slope intercept form as
then we can simply assume that
, and .
Now lets take our point in space. Now we would like to do is minimise the Euclidean distance to this point. The Euclidean distance is calculated using the equation
Minimizing this problem is dead simple. The closest point to a point is the point it self. But now we would like to add a constraint such that the closest point must lie of the line, whose equation is
We approach this problem using a simple form of the Lagrange multiplier where we minimize the function subject to the constraint . The best way to show that this works is with an example so…
Lets try this with an example
Lets assume we have a line of the form
and we would like to find a point on this line that is closest to a given point
We know the general form of the Lagrange multiplier which states that
In English this simply means the that we need to find a point where the functions and give us vectors that point in the same direction. By multiplying one of the vectors with a constant we make both vectors equal in length and direction. We could also write the equation above as
Before we start we change our distance function to a squared distance function. So in this case would become
We can do this because is just a function that needs to be minimised and for us it does not make a difference if we minimise the distance or the squared distance. We make this change as differentiating the squared distance function is much simpler and results in a system of linear equations. The function remains the same.
So now we need to find
In this case
We now need to find
After doing all the hairy math, we get
We now have a system of three linear equations with three unknowns. This should be fairly easy to solve using a number of methods. If we had used the distance function instead of the squared distance, we would have ended up with a system of non-linear equations which would not been so easy to solve.
Ok so after solving for we get
We are not really interested in anymore, so we shall leave that out. So the point on the line is
We can use several different methods to verify if this answer is correct. We can solve for the same point using the trigonometric method or we can check if the vectors and are orthonormal to each other. Here is just another point on our line. We shall use the latter method to verify our results. We already have
, . We can find by just setting in line equation. So we get
So we get
…or in plain English: IT WORKS!!
A while back I was looking for a good way to create a very strong password for my KeePass Password Safe. If I was going to put all my passwords in to one database, it better be protected by a good password – a very good one, one which I would not be able to remember. Of course this does not mean I do something stupid like write it on a piece of paper. Duh!
- A USB stick? Ah too simple.
- YubiKey? Sounded very cool initially, but only can work with one password at a time and another device to take care of. And it was USB – too much friction if I had to enter the password 5-10 times a day.
- My phone: Ah perfect, it’s very personal. I never leave anywhere without it. WHich means it won’t be forgotten in my USB port when I’m out. And yeah, it’s got bluetooth (no shoving stuff in to usb ports).
So I set out to make a bluetooth app that could exchange keys with KeePass. Head on to the KeyXchanger page to get more info and download it.
The stand alone password exchanger
Even if you do not use KeePass, you can still use Key Xchanger. The download contains a stand alone application that allows your computer to exchange passwords with your phone. Some features of Key Xchanger are…
- All passwords are stored on your phone encrypted.
- Passwords on the phone are encrypted and can only be decrypted by an authorized computer
- Your password is not compromised even if you loose your phone, since one needs to attack both your phone and computer. This is extremely unlikely to happen.
- Passwords are never stored on the PC and are discarded immediately after being used.
- You can enter passwords in to almost any application on windows
Due to the large number of phones out there I would appreciate it if you could tell me if the application worked for you.
The problem: For a long time I have tried various methods to disable the worst key on the keyboard – the CAPS lock. In my opinion, it provides no real function and yet is placed in a position where it can be easily be pressed.
Attempt 1: Initially I disabled this key using an AutoHotKey script. But for some reason, the script would stop working and I would still find CAPS LOCK ON just when I was entering that password or something. Besides every new computer I went to needed to have AutoHotKey or a compiled .exe just for this purpose. Since I am fanatical about keeping 24×7 background apps to a minimal, this was a big no no. And finally, even though I think AutoHotKey is a very useful app, I doubt it can be used as a full scale keyboard remapper.
Attempt 2: After being pushed over the edge with AutoHotKey’s incompetence in this area, my hunt for a new application continued. After some modified google search terms and fresh hope for luck, I came across KeyTweak, among others which included plain old registry hacking and several incarnations or AutoHotKey. KeyTweak did just what I wanted. Once initial the configuration was made, it would do all the dirty registry hacing for me and I did not need any software to running in the background.
But now I was faced with a new problem, and this time it was caused due to personal circumstances. I had recently moved to a new country and this meant getting used to a whole new keyboard layout just for one new charecter – £. I think you know which country I am talking about. Initially I resisted by opening ‘Character Map’ and copying the £ symbol from the table. But soon I realized that this was happening more often than not and I needed another solution.
Secondly even if I did give in and decide to get used to a new keyboard layout, I’d still be stuck with a US keyboard on my laptop. Changing that would be a fairly expensive affair and if I didn’t it would mean different keyboard layouts on different computers which can get very very irritating. I say this out of experience.
AutoHotKey – Although this could do the job, I was still against having an app running in the background just so that it could type the £ for me. But until I found a better solution, this was going to be the answer to the problem.
KeyTweak – Although this could remap keys to others, somehow it was not able to find the £ character for me.
Attempt 3: So once again it was back to google. That is when I found ‘Microsoft Keyboard Layout Creator‘. From the depths of Microsoft’s lair lies this little known app that can do pretty much anything and everything you wanted. Basically you create a whole new keyboard layout, just like the professionals do.
A quick guide to using Microsoft Keyboard Layout Creator
- So once you have done the obvious download and install, run the application.
- If you are like me and want to remap change a few keys, then load your current keyboard layout by going to
File > Load existing keyboard
- If you are not sure which keyboard layout you are using, you can find this by going to
Control Panel > Regional and Language Options > Languages (tab) > Details (button)
- Reconfigure the keys to the way you like it
- Create a new installation package by going to
Project > Build DLL and Setup Package
Follow the instructions and create a installation package. Remember to give it a name that you can identify later.
- Install the new layout using the installation package
- Go to
Control Panel > Regional and Language Options > Languages (tab) > Details (button)
Beside the tree view, click Add… and select the new keyboard layout that you just created.
- Activate it by selecting the new layout from the drop down menu where is says
‘Default input language‘
Alternatively, you can switch between this layout and your old layout by enabling the language bar.
I hope this guide helps you get the most out of your keyboard.
All links are verified to be valid at the time of writing this post. If any of the links in this article or anywhere else on my blog are broken, let me know via the comments and I shall do my best to fix it.
Recently when automating certain tasks, I came across the need for a command line app that would close windows applications in a clean way. After a lot of searching I was surprised not to find something to do this simple task. Yes there a lot of tools to kill an application, but that was not what I was looking for.
Well, a lot of applications perform a lot of important tasks before they close. These range from saving settings, asking you to save files, releasing hooks. Whatever the task may be, if you killing an application stop it dead in it’s tracks and the app does not have a chance to perform these tasks.
So TerminateApp is the result of this necessity.
You can specify the process by it’s name or by it’s process ID. If you really are impatient, you can even kill the application if the application refuses to terminate within a given amount of time.
How does it work?
TerminateApp sends a WM_QUIT message to all window handles within a specified process. So it will only work for applications that process the WM_QUIT message. This worked fine for the several applications I tested it on, but I cannot guarantee it works on all the apps out there. This would depend on the way the app has been written.
It’s been some time since anything technical has been posted here. Been spending my time building up a stronger base since the top seemed a little wobbly.
Got a new demo out today. It’s something that some people (including me) call vector textures. A very interesting technique that uses the existing texture resizing methods implemented on the GPU. These days it’s hard to find graphics techniques that manage really makes use of everything the GPU provides us with. What’s even nicer is that this technique could improve existing methods of rendering things like text and could even be extended to rendering blades of grass or leaves on a tree where the possibility of zooming in to a leaf is very real.
Hey, What you doing child…
Loafing around the internet like a college boy outside a girl’s college.
For those of you who don’t know who Chamrajpet Charles is, I’ll forgive you. If you have not heard of him, I seriously doubt you will be able to fully appreciate this post
For the rest, you will get some upper cuts on your kidneys if you lie only. So for all CC fans out there, here are some rips that will make you feel at home, where ever you are
He makes me laugh no matter what my mood. Feel free to download them to your computer. And now for my track collection…