Based on the following matlab: 
 
 % Based on Eqn 11.9 in H&Z ("First order geometric error (Sampson distance)")
 syms f1 f2 f3 f4 f5 f6 real
 syms r s real
 syms x y X Y real
 % row 3 is parameterised
 f7 = r*f1 + s*f4;
 f8 = r*f2 + s*f5;
 f9 = r*f3 + s*f6;
 % build F
 F = [f1 f2 f3; f4 f5 f6; f7 f8 f9];
 % the sampson distance and its analytic jacobian
 Fx = F*[x y 1]';
 FtX = F'*[X Y 1]';
 XFx = [X Y 1] * F * [x y 1]';
 d = XFx^2 / (Fx(1)^2 + Fx(2)^2 + FtX(1)^2 + FtX(2)^2);
 J = jacobian(d, [f1 f2 f3 f4 f5 f6 r s]);
 % generate code
 ccode(d, 'file', 'ccode/f12_sampson_value.c')
 ccode(J, 'file', 'ccode/f12_sampson_jac.c')