# HG changeset patch
# User Martin Janda <janda.martin1@gmail.com>
# Date 1542935406 -3600
#      Fri Nov 23 02:10:06 2018 +0100
# Node ID 8e5274044ddb6562e044e14da8717e45e3366572
# Parent  085c40a0e9795a43ebd45af038f64620834c08a0
affine: fix shared functions and add BIST tests

* inst/affine.m: fix functions invert, isRigid, isTranslation, isSimilarity,
  add disp function
* inst/affine2d.m, inst/affine3d.m: add BIST tests (includes two xtests)

diff -r 085c40a0e979 -r 8e5274044ddb inst/affine.m
--- a/inst/affine.m	Sat May 20 22:49:14 2017 +0100
+++ b/inst/affine.m	Fri Nov 23 02:10:06 2018 +0100
@@ -69,28 +69,30 @@
     endfunction
 
     function this = invert (this)
-      this.tform = inv (this.T);
+      this.T = inv (this.T);
+      this.T(1:end-1, end) = 0;
+      this.T(end) = 1;
     endfunction
 
     ## Check if transform is only rotation or translation.
     function check = isRigid (this)
       submatrix = this.T(1:end-1,1:end-1);
-      check = abs (abs (det (submatrix)) -1) < e^-10;
+      check = abs (det (submatrix) - 1) < eps;
     endfunction
 
-    ## Check if transform is only homogineous scaling, rotation, or
+    ## Check if transform is only homogeneous scaling, rotation, reflection or
     ## translation.
     function check = isSimilarity (this)
       submatrix = this.T(1:end-1,1:end-1);
-      check = all ((abs (abs (submatrix*submatrix^-1)
-                         - eye (this.Dimensionality)) < e^-10)(:));
+      s2 = submatrix*submatrix';
+      check = all ((abs (s2 - s2(1,1) * eye (this.Dimensionality)) ...
+      < (eps * ones (this.Dimensionality)))(:));
     endfunction
 
     ## Check if transform is only a translation.
     function check = isTranslation (this)
       submatrix = this.T(1:end-1,1:end-1);
-      check = all ((abs (abs (submatrix)
-                         - eye (this.Dimensionality)) < e^-10)(:));
+      check = !any ((abs (submatrix - eye (this.Dimensionality)) > 0)(:));
     endfunction
 
     ## given a bounding cube corner coordinates in xlims, ylims and
@@ -127,5 +129,11 @@
       [varargout{1:nargout}] = transformPoints (this, inv (this.T),
                                                 varargin{:});
     endfunction
+    
+    function disp (this)
+      printf ("  %s with properties:\n\n", class (this));
+      printf ("               T: [%dx%d %s]\n", size (this.T), class (this.T));
+      printf ("  Dimensionality: %d\n", this.Dimensionality);
+    endfunction
   endmethods
 endclassdef
diff -r 085c40a0e979 -r 8e5274044ddb inst/affine2d.m
--- a/inst/affine2d.m	Sat May 20 22:49:14 2017 +0100
+++ b/inst/affine2d.m	Fri Nov 23 02:10:06 2018 +0100
@@ -63,6 +63,22 @@
 %! assert (isSimilarity (tform))
 
 %!test
+%! theta = 30;
+%! tform = affine2d([ cosd(theta)  sind(theta) 0
+%!                   -sind(theta)  cosd(theta) 0
+%!                             0            0  1]);
+%! assert (tform.T, [ 0.86603 0.5     0
+%!                   -0.5     0.86603 0
+%!                    0       0       1], e^-5);
+%! invtform = invert(tform);
+%! assert (invtform.T, [ 0.86603 -0.5     0
+%!                    0.5      0.86603 0
+%!                    0        0       1], e^-5);
+%! assert (isRigid (tform))
+%! assert (! isTranslation (tform))
+%! assert (isSimilarity (tform))
+
+%!test
 %! tform = affine2d ([1 0 0; 0 1 0; 5 10 1]);
 %! [X, Y] = transformPointsForward (tform, [1 2; 3 4; 5 6; 7 8]);
 %! assert (round (X), [6; 8; 10; 12])
@@ -91,7 +107,31 @@
 %! assert (xlim, [1.1585 286.8855], e^-4)
 %! assert (ylim, [-40.6908  286.4054], e^-4)
 
+%!test
+%! A = [1  0  0
+%!      0  1  0
+%!      40 40 1];
+%! tform = affine2d (A);
+%! assert (isRigid (tform));
+%! assert (isSimilarity (tform));
+%! assert (isTranslation (tform));
+
+%!test
+%! a = invert (affine2d ([1 2 0; 3 4 0; 10 20 1]));
+%! b = affine2d(a.T);
+%! assert (b.T, [-2, 1, 0; 1.5, -0.5, 0; -10, 0, 1], e^-10)
+
+%!assert (isTranslation (affine2d ([1, 0, 0; 0, 1, 0; 40, 40, 1])))
+%!assert (! isTranslation (affine2d ([1 0 0; 0 -1 0; 0 0 1])))
+%!assert (! isRigid (affine2d ([1 0 0; 0 -1 0; 0 0 1])))
+
 %!error <must be an affine transform matrix> affine2d ([0 0 0; 0 0 0])
 %!error <must be an affine transform matrix> affine2d ([0 0 0  0 0 0  0 0 1])
 %!error <must be an affine transform matrix> affine2d ([0 0 0; 0 0 0; 0 0 0])
+%!error <must be an affine transform matrix> affine2d ([1 0 0; 0 1 1; 0 0 1])
 %!error <must not be a singular matrix> affine2d ([0 0 0; 0 0 0; 0 0 1])
+
+%!xtest
+%! tform = affine2d
+%! assert (tform.T, eye (3))
+%! assert (tform.Dimensionality, 2)
\ No newline at end of file
diff -r 085c40a0e979 -r 8e5274044ddb inst/affine3d.m
--- a/inst/affine3d.m	Sat May 20 22:49:14 2017 +0100
+++ b/inst/affine3d.m	Fri Nov 23 02:10:06 2018 +0100
@@ -60,22 +60,34 @@
 %! assert ([U V W], [5 10 3], e^-10)
 %! assert (! isRigid (tform))
 %! assert (! isTranslation (tform))
-%! assert (isSimilarity (tform))
+%! assert (! isSimilarity (tform))
+
+%!test
+%! A = [3 1 2 0; 4 5 8 0; 6 2 1 0; 0 0 0 1];
+%! tform = affine3d (A);
+%! [X, Y, Z] = transformPointsForward (tform, 2, 3, 5);
+%! assert (X, 48, e^-10)
+%! assert (Y, 27, e^-10)
+%! assert (Z, 33, e^-10)
+%! [U, V, W] = transformPointsInverse (tform, X, Y, Z);
+%! assert (U, 2, e^-10)
+%! assert (V, 3, e^-10)
+%! assert (W, 5, e^-10)
+%! assert (! isRigid (tform))
+%! assert (! isTranslation (tform))
+%! assert (! isSimilarity (tform))
 
 %!test
 %! A = [1 0 0 0; 0 1 0 0; 0 0 1 0; 5 10 1 1];
 %! tform = affine3d (A);
-%! [X, Y, Z] = transformPointsForward (tform, [1 2 3; 4 5 6; 7 8 9]);
-%! assert (round (X), [6; 9; 12])
-%! assert (round (Y), [12; 15; 18])
-%! assert (round (Z), [4; 7; 10])
-%! U = transformPointsInverse (tform, X, Y, Z);
+%! X = transformPointsForward (tform, [1 2 3; 4 5 6; 7 8 9]);
+%! assert (round (X), [6, 12, 4; 9, 15, 7; 12, 18, 10])
+%! U = transformPointsInverse (tform, X);
 %! assert (round (U), [1 2 3; 4 5 6; 7 8 9])
 %! assert (isRigid (tform))
 %! assert (isTranslation (tform))
 %! assert (isSimilarity (tform))
 
-
 %!test
 %! Sx = 1.2;
 %! Sy = 1.6;
@@ -86,3 +98,8 @@
 %! assert (xlim, [ 1.2000  153.6000],e^-4)
 %! assert (ylim, [1.6000  204.8000], e^-4)
 %! assert (zlim, [2.4000   64.8000], e^-4)
+
+%!xtest
+%! tform = affine3d
+%! assert (tform.T, eye (4))
+%! assert (tform.Dimensionality, 3)
\ No newline at end of file
