Valgrind is a popular tool for debugging memory leak in C++. By using this tool you will be able to see objects that were forgotten to be freed/deleted, and potential bug from code that Valgrind doesn't like.

Installing Valgrind on older Mac OS is very simple and can be done from brew. However the most recent Mac OS 10.10 (Yosemite), has not been officially supported by Valgrind. Hence to be able to use this, installation must be done manually from their latest svn trunk which is also very simple.

## Setting up Valgrind on Mac OS Yosemite

svn checkout svn://svn.valgrind.org/valgrind/trunk

Followed by going into the directory trunk and:

./autogen.sh

make

make install

To start using valgrind:

valgrind --leak-check=full --show-reachable=yes --log-info=leak.log "[your program and args]"

For less detailed information and log to stdout:

valgrind --leak-check=yes "[your program and args]"

## How to Setup GDB in Mac OS

The last time I wrote something in C++ was 10 years ago during the lab session in the university, and recently I just decided to write my thesis project in C++ because of many reasons. First time writing after 10 years wasn't so easy and I got this error message on the screen when running the code:

Segmentation fault: 11

Not so helpful, is it? Meaning some errors relating to pointer happened somewhere in the program.

In Java (and probably many other modern programming languages too) usually a complete stack trace will be printed at least on the stdout, even if it's the programmer's fault for not catching the exception. Apparently in C to debug something like this there is a tool called GDB that can be easily installed in linux. On Mac it can be installed from brew, followed by a simple step of adding keychain access for gdb.

So to install:

- brew tap homebrow/dupes
- brew install gdb
- Open /Applications/Utilities/Keychain Access and
**create a certificate**in**"System"**. Things to highlight here are (chronologically): - Identity type: Self signed root
- Certificate type: code signing
- Let me override defaults
- Store in "System"
- Get info of the just created certificate and set everything to
**"Always trust"** - codesign -s [name of certificate]
$(which gdb)

Start using GDB:

- gdb [name of executable]
- run
- up to this point the program will run and print out line number where the error happened and if need more information use backtrace.
- backtrace
- help catch exception followed by catch throw and re-run debug to help catch exceptions that were not caught in code.
- quit

## C2 Cubic Piecewise Interpolation with Different Parameterization Methods

On the previous geometric modelling class assignment, we were given some points in dimension 2, and then asked to implement a C2 cubic interpolation with 3 different parameterization methods.

Here I will try to explain briefly everything related to the image above as much as I can in layman terms.

#### Interpolation

Remember in junior high when mathematics was very basic, where we were given 2 points $(x_1, y_1); (x_2,y_2)$ and then we were asked to find a line passing these points? This is what interpolation means. Only that we would want a spline instead of a line, which means that it will involve polynomial equations. A line $f(x)=ax+b$ itself is a degree one polynomial.

#### Piecewise

Piecewise means that we want the interpolation to be done separately by considering only 2 points at one time. And then we connect those pieces into one complete spline.

#### C2 Continuity

Above we also see the terms C2 cubic interpolation. What C2 means is that we want the spline segments to be connected in the way such that their second derivative are the same. Here I call a spline between 2 points as a spline segment.

#### Cubic

For this term we may refer to Bézier curve where it indicates that the curve will have 4 control points. Here is the equation for Bézier representation:

$p(u)=\sum_{i=0}^n c_i B^n_i(u)$ where $B^n_i(x)=\binom{n}{i}t^n (1-x)^{n-i}$

#### Parameterization

The image above was drawn using 3 different parameterization methods:

- uniform for $\mu=0$
- chordal for $\mu=1$
- centripetal for $\mu=0.5$

To make it easier to imagine how these $\mu$ values will affect the shape of the spline, I made up a little story by looking the points as the racing game where the driver were required to pass these points in order to complete the race. Here we have 3 different drivers with different driving style.

**Racer #1**drives with the same speed all the way, this is why he made sharp turn when he has to pass points that are closed to each other. This causes the ^ section which is called as cusp.

And then moving on to

**racer #2**who adjusts his speed extremely based from time to time by observing how far away the next point is. If the next point is near then he will slow down, otherwise if it is still far away then he will increase speed. This causes the last turn to be a late apex.

In racing, a driver will often use a "late apex," turning into the corner a little later than normal in order to straighten out the last part of the corner. This allows the driver to accelerate earlier and harder, gaining maximum speed down the next straight.Lastly

**racer #3**is also the type who adjusts speed based on how far away the next point is. But he does not make too much extreme changes of speed along the way. So here we can see that the driving path he made is the one passing those points elegantly.

The question now is which racers complete the trip the earliest?

They completed it in the same amount of time!

## Cubic Spline Interpolation in Matlab

This was a homework in my geometric modeling class. Later I modified it to receive input with mouse click and a button to clear and redraw, and also a drop down to choose parametrization method.

Take a look at the spiral I made!!

Bezier representation:

$$

p(t)=\sum_{i=0}^{n} c_i B^n_i(t)

$$

function [ ] = draw( ) figure hold on; box on; grid; uicontrol('Style', 'pushbutton', 'String', 'Try me again!',... 'Position', [20 20 50 20],... 'Callback', @draw_new); uicontrol('Style', 'popup',... 'String', 'uniform|centripetal|chordal',... 'Position', [20 340 100 50],... 'Callback', @setmethod); draw_new ('',''); end function [] = setmethod (hObj,event) axis([0 10 0 10]); global PARAMETER; PARAMETER=get(hObj,'Value'); draw_spline(); end function[]=draw_spline() cla; global XY; global PARAMETER; if PARAMETER ==1 interpolate(XY, 0, 'r'); elseif PARAMETER == 2 interpolate(XY, 0.5, 'g'); elseif PARAMETER == 3 interpolate(XY, 1, 'b'); else interpolate(XY, 0, 'r'); end end function [] = draw_new (hObj,event) %#okglobal XY; cla; axis([0 10 0 10]); % Initially, the list of points is empty. XY = []; n = 0; % Loop, picking up the points. disp('Left mouse button picks points.') disp('Right mouse button picks last point.') but = 1; while but == 1 [xi,yi,but] = ginput(1); plot(xi,yi,'ro') n = n+1; XY(:,n) = [xi;yi]; if (length(XY(1,:)) > 1) draw_spline; end end draw_spline(); end function [] = interpolate(x, mu, color) n=length(x); t=parameterize(x,mu); h(1:n-1) = t(2:n)-t(1:n-1); mi=get_cubic_slope(x,t); for i=1:n-1 a=x(1,i); b=x(1,i+1); p(1,:)=x(:,i); p(2,:)=x(:,i)+(h(i)).*mi(:,i)/3; p(3,:)=x(:,i+1)-(h(i)).*mi(:,i+1)/3; p(4,:)=x(:,i+1); [q_bez] = deCasteljau(0,1,p,linspace(0,1,100)); plot(q_bez(:,1),q_bez(:,2),[color,'-'],'LineWidth',3); plot(p(:,1),p(:,2),[color,'--o'], 'MarkerSize',3); end plot(x(1,:),x(2,:),'ro', 'MarkerSize',5,'MarkerFaceColor','r'); xlabel('x'), ylabel('y'),title(['Cubic Spline Interpolation']); end function[m] = get_cubic_slope(x,t) %cublic spline interpolation n=length(x); h(2:n) = t(2:n)-t(1:n-1); A=zeros(n-1,n-1); b=zeros(2,n-1); b(:,1)= 3*((h(1)/h(2)*(x(:,2)-x(:,1)))); for i=1:n-1 A(i,i)=2*(h(i)+h(i+1)); end for i=2:n-1 b(:,i)=3*((h(i+1)/h(i)*(x(:,i)-x(:,i-1)))+(h(i)/h(i+1)*(x(:,i+1)-x(:,i)))); A(i,i-1)=h(i+1); A(i-1,i)=h(i); end m=A\b'; m=m'; m(:,n)=0; end function[m] = get_hermite_slope(x,t) n=length(x); %piecewise hermite interpolation m(:,1)=(x(:,2)-x(:,1))./(t(2)-t(1)); m(:,n)=(x(:,n)-x(:,n-1))./(t(n)-t(n-1)); m(1,2:n-1)=(x(1,3:n)-x(1,1:n-2))./(t(3:n)-t(1:n-2)); m(2,2:n-1)=(x(2,3:n)-x(2,1:n-2))./(t(3:n)-t(1:n-2)); end function [t] = parameterize(x, mu) t=zeros(1,length(x)); for i=2:length(x) t(i)=sqrt((x(1,i) - x(1,i-1))^2 + (x(2,i) - x(2,i-1))^2)^mu + t(i-1); end end function [p_t] = deCasteljau(a,b,p,t) n = size(p,1); m = length(t); p_t = zeros(m,2); X(:,1) = p(:,1); Y(:,1) = p(:,2); for j = 1:m for i = 2:n X(i:n,i) = (t(j)-a)/(b-a)*X(i-1:n-1,i-1) + (b-t(j)-a)/(b-a)*X(i:n,i-1); Y(i:n,i) = (t(j)-a)/(b-a)*Y(i-1:n-1,i-1) + (b-t(j)-a)/(b-a)*Y(i:n,i-1); end p_t(j,1) = X(n,n); p_t(j,2) = Y(n,n); end end

## Browsers Add-ons

## WOT

WOT gives warning whenever we are about to enter websites with bad reputations, rated by the community. WOT categorises those bad websites as either: scam, phishing, adult content, illegal, Malwares/viruses.

## Adblock

I totally understand that ads is a way to gain traffic and customers. But I sincerely prefer to not have flashy adult site ads, or promotions that actually requires you to do sacrifice for them before they will give you a piece of cake. So go ahead and install this Adblock add-on.

## Google Translate

This is especially useful for someone who is living in a foreign country. You might be thinking that English is an international language and most websites must have the option for English. This is partially true because some websites are not multi-lingual and even if they do, some of the times it sucks. Here it is: for Firefox, and for Chrome.

## XPath Helper

It might be out of topic, but I found this useful for web application development. XPath Helper also gives a console where we can test Xpath command, and it also displays HTML element inspect and another couple of useful things.

Subscribe to:
Posts (Atom)