changeset 151:3a3cf386bb7e feature/grids

Moved Curves and Tis from package grid to package parametrization.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 21 Dec 2015 15:09:32 +0100
parents d5a794c734bc
children 276dcccf6155
files +grid/Curve.m +grid/Ti.m +grid/equal_step_size.m +grid/old/concat_curve.m +grid/old/curve_discretise.m +grid/old/curve_interp.m +grid/old/max_h.m +grid/old/min_h.m +grid/old/plot_shape.m +grid/old/shape.m +grid/old/shape_discretise.m +grid/old/shape_linesegments.m +grid/old/triang_interp.m +grid/old/triang_interp_pts.m +grid/old/triang_map.m +grid/old/triang_plot_interp.m +grid/old/triang_show.m +grid/place_label.m +parametrization/Curve.m +parametrization/Ti.m +parametrization/equal_step_size.m +parametrization/old/concat_curve.m +parametrization/old/curve_discretise.m +parametrization/old/curve_interp.m +parametrization/old/max_h.m +parametrization/old/min_h.m +parametrization/old/plot_shape.m +parametrization/old/shape.m +parametrization/old/shape_discretise.m +parametrization/old/shape_linesegments.m +parametrization/old/triang_interp.m +parametrization/old/triang_interp_pts.m +parametrization/old/triang_map.m +parametrization/old/triang_plot_interp.m +parametrization/old/triang_show.m +parametrization/place_label.m
diffstat 36 files changed, 1180 insertions(+), 1180 deletions(-) [+]
line wrap: on
line diff
--- a/+grid/Curve.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,388 +0,0 @@
-classdef Curve
-    properties
-        g
-        gp
-        transformation
-    end
-
-    methods
-        %TODO:
-        % Concatenation of curves
-        % Subsections of curves
-        % Stretching of curve parameter - done for arc length.
-        % Curve to cell array of linesegments
-
-        % Returns a curve object.
-        %   g -- curve parametrization for parameter between 0 and 1
-        %  gp -- parametrization of curve derivative
-        function obj = Curve(g,gp,transformation)
-            default_arg('gp',[]);
-            default_arg('transformation',[]);
-            p_test = g(0);
-            assert(all(size(p_test) == [2,1]), 'A curve parametrization must return a 2x1 vector.');
-
-            if ~isempty(transformation)
-                transformation.base_g = g;
-                transformation.base_gp = gp;
-                [g,gp] = grid.Curve.transform_g(g,gp,transformation);
-            end
-
-            obj.g = g;
-            obj.gp = gp;
-            obj.transformation = transformation;
-
-        end
-
-        function n = normal(obj,t)
-            assert(~isempty(obj.gp),'Curve has no derivative!');
-            deriv = obj.gp(t);
-            normalization = sqrt(sum(deriv.^2,1));
-            n = [-deriv(2,:)./normalization; deriv(1,:)./normalization];
-        end
-
-
-        % Plots a curve g(t) for 0<t<1, using n points. Returns a handle h to the plotted curve.
-        %   h = plot_curve(g,n)
-        function h = plot(obj,n)
-            default_arg('n',100);
-
-            t = linspace(0,1,n);
-            p = obj.g(t);
-            h = line(p(1,:),p(2,:));
-        end
-
-        function h= plot_normals(obj,l,n,m)
-            default_arg('l',0.1);
-            default_arg('n',10);
-            default_arg('m',100);
-            t_n = linspace(0,1,n);
-
-            normals = obj.normal(t_n)*l;
-
-            n0 = obj.g(t_n);
-            n1 = n0 + normals;
-
-            h = line([n0(1,:); n1(1,:)],[n0(2,:); n1(2,:)]);
-            set(h,'Color',Color.red);
-            obj.plot(m);
-        end
-
-        function h= show(obj,name)
-            p = obj.g(1/2);
-            n = obj.normal(1/2);
-            p = p + n*0.1;
-
-            % Add arrow
-
-            h = text(p(1),p(2),name);
-            h.HorizontalAlignment = 'center';
-            h.VerticalAlignment = 'middle';
-
-            obj.plot();
-        end
-            % Shows curve with name and arrow for direction.
-
-        % Length of arc from parameter t0 to t1 (which may be vectors).
-        % Computed using derivative.
-        function L = arcLength(obj,t0,t1)
-            assert(~isempty(obj.gp),'Curve has no derivative!');
-            speed = @(t) sqrt(sum(obj.gp(t).^2,1));
-            L =  integral_vec(speed,t0,t1);
-        end
-
-        % Creates the arc length parameterization of a curve.
-        %    N -- number of points used to approximate the arclength function
-        function curve = arcLengthParametrization(obj,N)
-            default_arg('N',100);
-            assert(~isempty(obj.gp),'Curve has no derivative!');
-
-            % Construct arcLength function using splines
-            tvec = linspace(0,1,N);
-            arcVec = obj.arcLength(0,tvec);
-            tFunc = spline(arcVec,tvec); % t as a function of arcLength
-            L = obj.arcLength(0,1);
-            arcPar = @(s) tFunc(s*L);
-
-            % New function and derivative
-            g_new = @(t)obj.g(arcPar(t));
-            gp_new = @(t) normalize(obj.gp(arcPar(t)));
-            curve = grid.Curve(g_new,gp_new);
-
-        end
-
-        % how to make it work for methods without returns
-        function p = subsref(obj,S)
-            %Should i add error checking here?
-            %Maybe if you want performance you fetch obj.g and then use that
-            switch S(1).type
-                case '()'
-                    p = obj.g(S.subs{1});
-                % case '.'
-
-                    % p = obj.(S.subs);
-                otherwise
-                    p = builtin('subsref',obj,S);
-                    % error()
-            end
-        end
-
-
-        %% TRANSFORMATION OF A CURVE
-        function D = reverse(C)
-            % g = C.g;
-            % gp = C.gp;
-            % D = grid.Curve(@(t)g(1-t),@(t)-gp(1-t));
-            D = C.transform([],[],-1);
-        end
-
-        function D = transform(C,A,b,flip)
-            default_arg('A',[1 0; 0 1]);
-            default_arg('b',[0; 0]);
-            default_arg('flip',1);
-            if isempty(C.transformation)
-                g  = C.g;
-                gp = C.gp;
-                transformation.A = A;
-                transformation.b = b;
-                transformation.flip = flip;
-            else
-                g  = C.transformation.base_g;
-                gp = C.transformation.base_gp;
-                A_old = C.transformation.A;
-                b_old = C.transformation.b;
-                flip_old = C.transformation.flip;
-
-                transformation.A = A*A_old;
-                transformation.b = A*b_old + b;
-                transformation.flip = flip*flip_old;
-            end
-
-            D = grid.Curve(g,gp,transformation);
-
-        end
-
-        function D = translate(C,a)
-            g = C.g;
-            gp = C.gp;
-
-            % function v = g_fun(t)
-            %     x = g(t);
-            %     v(1,:) = x(1,:)+a(1);
-            %     v(2,:) = x(2,:)+a(2);
-            % end
-
-            % D = grid.Curve(@g_fun,gp);
-
-            D = C.transform([],a);
-        end
-
-        function D = mirror(C, a, b)
-            assert_size(a,[2,1]);
-            assert_size(b,[2,1]);
-
-            g = C.g;
-            gp = C.gp;
-
-            l = b-a;
-            lx = l(1);
-            ly = l(2);
-
-
-            % fprintf('Singular?\n')
-
-            A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l);
-
-            % function v = g_fun(t)
-            %     % v = a + A*(g(t)-a)
-            %     x = g(t);
-
-            %     ax1 = x(1,:)-a(1);
-            %     ax2 = x(2,:)-a(2);
-            %     v(1,:) = a(1)+A(1,:)*[ax1;ax2];
-            %     v(2,:) = a(2)+A(2,:)*[ax1;ax2];
-            % end
-
-            % function v = gp_fun(t)
-            %     v = A*gp(t);
-            % end
-
-            % D = grid.Curve(@g_fun,@gp_fun);
-
-            % g = A(g-a)+a = Ag - Aa + a;
-            b = - A*a + a;
-            D = C.transform(A,b);
-
-        end
-
-        function D = rotate(C,a,rad)
-            assert_size(a, [2,1]);
-            assert_size(rad, [1,1]);
-            g = C.g;
-            gp = C.gp;
-
-
-            A = [cos(rad) -sin(rad); sin(rad) cos(rad)];
-
-            % function v = g_fun(t)
-            %     % v = a + A*(g(t)-a)
-            %     x = g(t);
-
-            %     ax1 = x(1,:)-a(1);
-            %     ax2 = x(2,:)-a(2);
-            %     v(1,:) = a(1)+A(1,:)*[ax1;ax2];
-            %     v(2,:) = a(2)+A(2,:)*[ax1;ax2];
-            % end
-
-            % function v = gp_fun(t)
-            %     v = A*gp(t);
-            % end
-
-            % D = grid.Curve(@g_fun,@gp_fun);
-
-
-             % g = A(g-a)+a = Ag - Aa + a;
-            b = - A*a + a;
-            D = C.transform(A,b);
-        end
-    end
-
-    methods (Static)
-
-        % Computes the derivative of g: R -> R^2 using an operator D1
-        function gp_out = numericalDerivative(g,D1)
-            m = length(D1);
-            t = linspace(0,1,m);
-            gVec = g(t)';
-            gpVec = (D1*gVec)';
-
-            gp1_fun = spline(t,gpVec(1,:));
-            gp2_fun = spline(t,gpVec(2,:));
-            gp_out = @(t) [gp1_fun(t);gp2_fun(t)];
-        end
-
-        function obj = line(p1, p2)
-
-            function v = g_fun(t)
-                v(1,:) = p1(1) + t.*(p2(1)-p1(1));
-                v(2,:) = p1(2) + t.*(p2(2)-p1(2));
-            end
-            g = @g_fun;
-
-            obj = grid.Curve(g);
-        end
-
-        function obj = circle(c,r,phi)
-            default_arg('phi',[0; 2*pi])
-            default_arg('c',[0; 0])
-            default_arg('r',1)
-
-            function v = g_fun(t)
-                w = phi(1)+t*(phi(2)-phi(1));
-                v(1,:) = c(1) + r*cos(w);
-                v(2,:) = c(2) + r*sin(w);
-            end
-
-            function v = g_fun_deriv(t)
-                w = phi(1)+t*(phi(2)-phi(1));
-                v(1,:) = -(phi(2)-phi(1))*r*sin(w);
-                v(2,:) =  (phi(2)-phi(1))*r*cos(w);
-            end
-
-            obj = grid.Curve(@g_fun,@g_fun_deriv);
-        end
-
-        function obj = bezier(p0, p1, p2, p3)
-            function v = g_fun(t)
-                v(1,:) = (1-t).^3*p0(1) + 3*(1-t).^2.*t*p1(1) + 3*(1-t).*t.^2*p2(1) + t.^3*p3(1);
-                v(2,:) = (1-t).^3*p0(2) + 3*(1-t).^2.*t*p1(2) + 3*(1-t).*t.^2*p2(2) + t.^3*p3(2);
-            end
-
-            function v = g_fun_deriv(t)
-                v(1,:) = 3*(1-t).^2*(p1(1)-p0(1)) + 6*(1-t).*t*(p2(1)-p1(1)) + 3*t.^2*(p3(1)-p2(1));
-                v(2,:) = 3*(1-t).^2*(p1(2)-p0(2)) + 6*(1-t).*t*(p2(2)-p1(2)) + 3*t.^2*(p3(2)-p2(2));
-            end
-
-            obj = grid.Curve(@g_fun,@g_fun_deriv);
-        end
-
-
-        function [g_out,gp_out] = transform_g(g,gp,tr)
-            A = tr.A;
-            b = tr.b;
-            flip = tr.flip;
-
-            function v = g_fun_noflip(t)
-                % v = A*g + b
-                x = g(t);
-
-                v(1,:) = A(1,:)*x+b(1);
-                v(2,:) = A(2,:)*x+b(2);
-            end
-
-            function v = g_fun_flip(t)
-                % v = A*g + b
-                x = g(1-t);
-
-                v(1,:) = A(1,:)*x+b(1);
-                v(2,:) = A(2,:)*x+b(2);
-            end
-
-
-            switch flip
-                case 1
-                    g_out  = @g_fun_noflip;
-                    gp_out = @(t)A*gp(t);
-                case -1
-                    g_out  = @g_fun_flip;
-                    gp_out = @(t)-A*gp(1-t);
-            end
-        end
-
-    end
-end
-
-
-
-function g_norm = normalize(g0)
-    g1 = g0(1,:);
-    g2 = g0(2,:);
-    normalization = sqrt(sum(g0.^2,1));
-    g_norm = [g1./normalization; g2./normalization];
-end
-
-function I = integral_vec(f,a,b)
-% Wrapper around the built-in function integral that
-% handles multiple limits.
-
-    Na = length(a);
-    Nb = length(b);
-    assert(Na == 1 || Nb == 1 || Na==Nb,...
-        'a and b must have same length, unless one is a scalar.');
-
-    if(Na>Nb);
-        I = zeros(size(a));
-        for i = 1:Na
-            I(i) = integral(f,a(i),b);
-        end
-    elseif(Nb>Na)
-        I = zeros(size(b));
-        for i = 1:Nb
-            I(i) = integral(f,a,b(i));
-        end
-    else
-        I = zeros(size(b));
-        for i = 1:Nb
-            I(i) = integral(f,a(i),b(i));
-        end
-    end
-end
-
-% Returns a function handle to the spline.
-function f = spline(tval,fval,spline_order)
-    default_arg('spline_order',4);
-    [m,~] = size(tval);
-    assert(m==1,'Need row vectors.');
-
-    f_spline = spapi( optknt(tval,spline_order), tval, fval );
-    f = @(t) fnval(f_spline,t);
-end
--- a/+grid/Ti.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,201 +0,0 @@
-classdef Ti
-    properties
-        gs % {4}Curve
-        S  % FunctionHandle(u,v)
-    end
-
-    methods
-        % TODO function to label boundary names.
-        %  function to find largest and smallest delta h in the grid. Maybe shouldnt live here
-        function obj = Ti(C1,C2,C3,C4)
-            obj.gs = {C1,C2,C3,C4};
-
-            g1 = C1.g;
-            g2 = C2.g;
-            g3 = C3.g;
-            g4 = C4.g;
-
-            A = g1(0);
-            B = g2(0);
-            C = g3(0);
-            D = g4(0);
-
-            function o = S_fun(u,v)
-                x1 = g1(u);
-                x2 = g2(v);
-                x3 = g3(1-u);
-                x4 = g4(1-v);
-                o1 = (1-v).*x1(1,:) + u.*x2(1,:) + v.*x3(1,:) + (1-u).*x4(1,:) ...
-                    -((1-u)*(1-v).*A(1,:) + u*(1-v).*B(1,:) + u*v.*C(1,:) + (1-u)*v.*D(1,:));
-                o2 = (1-v).*x1(2,:) + u.*x2(2,:) + v.*x3(2,:) + (1-u).*x4(2,:) ...
-                    -((1-u)*(1-v).*A(2,:) + u*(1-v).*B(2,:) + u*v.*C(2,:) + (1-u)*v.*D(2,:));
-
-                o = [o1;o2];
-            end
-
-            obj.S = @S_fun;
-        end
-
-        function [X,Y] = map(obj,u,v)
-            default_arg('v',u);
-
-            if isscalar(u)
-                u = linspace(0,1,u);
-            end
-
-            if isscalar(v)
-                v = linspace(0,1,v);
-            end
-
-            S = obj.S;
-
-            nu = length(u);
-            nv = length(v);
-
-            X = zeros(nv,nu);
-            Y = zeros(nv,nu);
-
-            u = rowVector(u);
-            v = rowVector(v);
-
-            for i = 1:nv
-                p = S(u,v(i));
-                X(i,:) = p(1,:);
-                Y(i,:) = p(2,:);
-            end
-        end
-
-        function h = plot(obj,nu,nv)
-            S = obj.S;
-
-            default_arg('nv',nu)
-
-            u = linspace(0,1,nu);
-            v = linspace(0,1,nv);
-
-            m = 100;
-
-            X = zeros(nu+nv,m);
-            Y = zeros(nu+nv,m);
-
-
-            t = linspace(0,1,m);
-            for i = 1:nu
-                p = S(u(i),t);
-                X(i,:) = p(1,:);
-                Y(i,:) = p(2,:);
-            end
-
-            for i = 1:nv
-                p = S(t,v(i));
-                X(i+nu,:) = p(1,:);
-                Y(i+nu,:) = p(2,:);
-            end
-
-            h = line(X',Y');
-        end
-
-
-        function h = show(obj,nu,nv)
-            default_arg('nv',nu)
-            S = obj.S;
-
-            if(nu>2 || nv>2)
-                h_grid = obj.plot(nu,nv);
-                set(h_grid,'Color',[0 0.4470 0.7410]);
-            end
-
-            h_bord = obj.plot(2,2);
-            set(h_bord,'Color',[0.8500 0.3250 0.0980]);
-            set(h_bord,'LineWidth',2);
-        end
-
-
-        % TRANSFORMATIONS
-        function ti = translate(obj,a)
-            gs = obj.gs;
-
-            for i = 1:length(gs)
-                new_gs{i} = gs{i}.translate(a);
-            end
-
-            ti = grid.Ti(new_gs{:});
-        end
-
-        % Mirrors the Ti so that the resulting Ti is still left handed.
-        %  (Corrected by reversing curves and switching e and w)
-        function ti = mirror(obj, a, b)
-            gs = obj.gs;
-
-            new_gs = cell(1,4);
-
-            new_gs{1} = gs{1}.mirror(a,b).reverse();
-            new_gs{3} = gs{3}.mirror(a,b).reverse();
-            new_gs{2} = gs{4}.mirror(a,b).reverse();
-            new_gs{4} = gs{2}.mirror(a,b).reverse();
-
-            ti = grid.Ti(new_gs{:});
-        end
-
-        function ti = rotate(obj,a,rad)
-            gs = obj.gs;
-
-            for i = 1:length(gs)
-                new_gs{i} = gs{i}.rotate(a,rad);
-            end
-
-            ti = grid.Ti(new_gs{:});
-        end
-
-        function ti = rotate_edges(obj,n);
-            new_gs = cell(1,4);
-            for i = 0:3
-                new_i = mod(i - n,4);
-                new_gs{new_i+1} = obj.gs{i+1};
-            end
-            ti = grid.Ti(new_gs{:});
-        end
-    end
-
-    methods(Static)
-        function obj = points(p1, p2, p3, p4)
-            g1 = grid.Curve.line(p1,p2);
-            g2 = grid.Curve.line(p2,p3);
-            g3 = grid.Curve.line(p3,p4);
-            g4 = grid.Curve.line(p4,p1);
-
-            obj = grid.Ti(g1,g2,g3,g4);
-        end
-
-        function label(varargin)
-            if nargin == 2 && ischar(varargin{2})
-                label_impl(varargin{:});
-            else
-                for i = 1:length(varargin)
-                    label_impl(varargin{i},inputname(i));
-                end
-            end
-
-
-            function label_impl(ti,str)
-                S = ti.S;
-
-                pc = S(0.5,0.5);
-
-                margin = 0.1;
-                pw = S(  margin,      0.5);
-                pe = S(1-margin,      0.5);
-                ps = S(     0.5,   margin);
-                pn = S(     0.5, 1-margin);
-
-
-                ti.show(2,2);
-                grid.place_label(pc,str);
-                grid.place_label(pw,'w');
-                grid.place_label(pe,'e');
-                grid.place_label(ps,'s');
-                grid.place_label(pn,'n');
-            end
-        end
-    end
-end
\ No newline at end of file
--- a/+grid/equal_step_size.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-% Calculates M so that the stepsize m/M is as close to n/M as possible
-function M = equal_step_size(n,N,m)
-    M = round((m*(N-1)+n)/n);
-end
\ No newline at end of file
--- a/+grid/old/concat_curve.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-% Concatenate two curves g1 and g2 intop
-%   g = concat_curve(g1,g2)
-function g = concat_curve(g1,g2)
-    function v = g_fun(t)
-        if t < 1/2
-            v = g1(2*t);
-        else
-            v = g2(2*t-1);
-        end
-    end
-    g = @g_fun;
-end
\ No newline at end of file
--- a/+grid/old/curve_discretise.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-% Discretises the curve g with the smallest number of points such that all segments
-% are shorter than h. If do_plot is true the points of the discretisation and
-% the normals of the curve in those points are plotted.
-%
-%   [t,p,d] = curve_discretise(g,h,do_plot)
-%
-%   t is a vector of input values to g.
-%   p is a cector of points.
-%   d are the length of the segments.
-function [t,p,d] = curve_discretise(g,h,do_plot)
-    default_arg('do_plot',false)
-
-    n = 10;
-
-    [t,p,d] = curve_discretise_n(g,n);
-
-    % ni = 0;
-    while any(d>h)
-        [t,p,d] = curve_discretise_n(g,n);
-        n = ceil(n*d(1)/h);
-        % ni = ni+1;
-    end
-
-    % nj = 0;
-    while all(d<h)
-        [t,p,d] = curve_discretise_n(g,n);
-        n = n-1;
-        % nj = nj+1;
-    end
-    [t,p,d] = curve_discretise_n(g,n+1);
-
-    % fprintf('ni = %d, nj = %d\n',ni,nj);
-
-    if do_plot
-        fprintf('n:%d  max: %f min: %f\n', n, max(d),min(d));
-        p = grid.map_curve(g,t);
-        figure
-        show(g,t,h);
-    end
-
-end
-
-function [t,p,d] = curve_discretise_n(g,n)
-    t = linspace(0,1,n);
-    t = equalize_d(g,t);
-    d = D(g,t);
-    p = grid.map_curve(g,t);
-end
-
-function d = D(g,t)
-    p = grid.map_curve(g,t);
-
-    d = zeros(1,length(t)-1);
-    for i = 1:length(d)
-        d(i) = norm(p(:,i) - p(:,i+1));
-    end
-end
-
-function t = equalize_d(g,t)
-    d = D(g,t);
-    v = d-mean(d);
-    while any(abs(v)>0.01*mean(d))
-        dt = t(2:end)-t(1:end-1);
-        t(2:end) = t(2:end) - cumsum(dt.*v./d);
-
-        t = t/t(end);
-        d = D(g,t);
-        v = d-mean(d);
-    end
-end
-
-
-function show(g,t,hh)
-    p = grid.map_curve(g,t);
-
-
-
-    h = grid.plot_curve(g);
-    h.LineWidth = 2;
-    axis equal
-    hold on
-    h = plot(p(1,:),p(2,:),'.');
-    h.Color = [0.8500 0.3250 0.0980];
-    h.MarkerSize = 24;
-    hold off
-
-    n = grid.curve_normals(g,t);
-    hold on
-    for  i = 1:length(t)
-        p0 = p(:,i);
-        p1 = p0 + hh*n(:,i);
-        l = [p0, p1];
-        h = plot(l(1,:),l(2,:));
-        h.Color = [0.8500 0.3250 0.0980];
-    end
-
-end
\ No newline at end of file
--- a/+grid/old/curve_interp.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-% Create a cubic spline from the points (x,y) using periodic conditions.
-%   g = curve_interp(x,y)
-function g = curve_interp(x,y)
-    default_arg('x',[0 2 2 1 1 0])
-    default_arg('y',[0 0 2 2 1 1])
-    % solve for xp and yp
-
-    % x(t) = at^4 + bt^2+ct+d
-
-    % a = xp1 -2x1 + 2x0 +  xp0
-    % b = 3x1 -xp1 - 3x0 + 2xp0
-    % c = xp0
-    % d = x0
-
-    assert(length(x) == length(y))
-    n = length(x);
-    A = spdiags(ones(n,1)*[2, 8, 2],-1:1,n,n);
-    A(n,1) = 2;
-    A(1,n) = 2;
-
-    bx = zeros(n,1);
-    for i = 2:n-1
-        bx(i) = -6*x(i-1)+6*x(i+1);
-    end
-    bx(1) = -6*x(n)+6*x(2);
-    bx(n) = -6*x(n-1)+6*x(1);
-
-    by = zeros(n,1);
-    for i = 2:n-1
-        by(i) = -6*y(i-1)+6*y(i+1);
-    end
-    by(1) = -6*y(n)+6*y(2);
-    by(n) = -6*y(n-1)+6*y(1);
-
-
-    xp = A\bx;
-    yp = A\by;
-
-    x(end+1) = x(1);
-    y(end+1) = y(1);
-
-    xp(end+1) = xp(1);
-    yp(end+1) = yp(1);
-
-    function v = g_fun(t)
-        t = mod(t,1);
-        i = mod(floor(t*n),n) + 1;
-        t = t * n -(i-1);
-        X = (2*x(i)-2*x(i+1)+xp(i)+xp(i+1))*t.^3 + (-3*x(i)+3*x(i+1)-2*xp(i)-xp(i+1))*t.^2 + (xp(i))*t + x(i);
-        Y = (2*y(i)-2*y(i+1)+yp(i)+yp(i+1))*t.^3 + (-3*y(i)+3*y(i+1)-2*yp(i)-yp(i+1))*t.^2 + (yp(i))*t + y(i);
-        v = [X;Y];
-    end
-
-    g = @g_fun;
-end
-
-
-
--- a/+grid/old/max_h.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-function [d_max, i1_max, j1_max, i2_max, j2_max] = max_h(X,Y)
-    ni = size(X,1);
-    nj = size(X,2);
-    d_max = 0;
-
-    i1_max = 0;
-    j1_max = 0;
-    i2_max = 0;
-    j2_max = 0;
-
-    D = {[0,-1],[1,0],[0,1],[-1,0]};
-
-    for i = 1:ni
-        for j = 1:nj
-            p1 = [X(i,j); Y(i,j)];
-            for k = 1:length(D)
-                i2 = i+D{k}(1);
-                j2 = j+D{k}(2);
-                if i2 >= 1 && i2 <= ni && j2 >= 1 && j2 <= nj
-                    p2 = [X(i2,j2); Y(i2,j2)];
-                    d = norm(p2-p1);
-                    if d > d_max;
-                        d_max = d;
-                        i1_max = i;
-                        j1_max = j;
-                        i2_max = i2;
-                        j2_max = j2;
-                    end
-                end
-            end
-        end
-    end
-end
--- a/+grid/old/min_h.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-function [d_min, i1_min, j1_min, i2_min, j2_min] = min_h(X,Y)
-    ni = size(X,1);
-    nj = size(X,2);
-    d_min = norm([X(1,1);Y(1,1)] - [X(ni,nj);Y(ni,nj)]);
-
-    i1_min = 0;
-    j1_min = 0;
-    i2_min = 0;
-    j2_min = 0;
-
-    D = {[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]};
-    % D = {[0,-1],[1,0],[0,1],[-1,0]};
-
-    for i = 1:ni
-        for j = 1:nj
-            p1 = [X(i,j); Y(i,j)];
-            for k = 1:length(D)
-                i2 = i+D{k}(1);
-                j2 = j+D{k}(2);
-                if i2 >= 1 && i2 <= ni && j2 >= 1 && j2 <= nj
-                    p2 = [X(i2,j2); Y(i2,j2)];
-                    d = norm(p2-p1);
-                    if d < d_min;
-                        d_min = d;
-                        i1_min = i;
-                        j1_min = j;
-                        i2_min = i2;
-                        j2_min = j2;
-                    end
-                end
-            end
-        end
-    end
-end
--- a/+grid/old/plot_shape.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-% Plot a shape using n points. Returns cell array of plot handles.
-%   hs = plot_shape(s,n)
-function hs = plot_shape(s,n)
-    default_arg('n',100);
-
-    hs = {};
-    hold on
-    for i = 1:length(s)
-        hs{end+1} = grid.plot_curve(s{i},n);
-    end
-    hold off
-end
\ No newline at end of file
--- a/+grid/old/shape.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-% Creates a shape from a number of curves. A shape is a cell array of curves.
-function s = shape(varargin);
-    s = varargin;
-end
\ No newline at end of file
--- a/+grid/old/shape_discretise.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-% Discretises a shape such that points on the curves are no further than h appart.
-function p = shape_discretise(s,h)
-    p = [];
-    for i = 1:length(s)
-        [~,pt] = grid.curve_discretise(s{i},h);
-        p = [p, pt];
-    end
-end
\ No newline at end of file
--- a/+grid/old/shape_linesegments.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-% Converts a shape into a cell array of linesegments shorter than h.
-function l = shape_linesegments(s,h)
-    l = {};
-
-    for i = 1:length(s)
-        t = grid.curve_discretise(s{i},h);
-        l = [l, grid.curve_linesegments(s{i},t)];
-    end
-end
\ No newline at end of file
--- a/+grid/old/triang_interp.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,132 +0,0 @@
-classdef triang_interp
-    properties
-        g1, g2 ,g3  % Curves encirling the tirangle in the positive direction.
-        A,B,C  % The corners of the triangle
-        Sa, Sb, Sc % Mappings from square with different sides collapsed
-    end
-
-    methods
-        function o = triang_interp(g1,g2,g3)
-            o.g1 = g1;
-            o.g2 = g2;
-            o.g3 = g3;
-            o.A = g1(0);
-            o.B = g2(0);
-            o.C = g3(0);
-            o.Sa = grid.triang_interp.square_to_triangle_interp(g2,g3,g1);
-            o.Sb = grid.triang_interp.square_to_triangle_interp(g3,g1,g2);
-            o.Sc = grid.triang_interp.square_to_triangle_interp(g1,g2,g3);
-        end
-
-
-        function show(o,N)
-            % Show the mapped meridians of the triangle.
-            % Might be used for the barycentric coordinates.
-            ma = @(t)o.Sa(1/2,1-t);
-            mb = @(t)o.Sb(1/2,1-t);
-            mc = @(t)o.Sc(1/2,1-t);
-
-            na = @(t)o.Sa(t,1/2);
-
-            ka = @(t)(o.g1(1-t)+o.g2(t))/2;
-
-            h = grid.plot_curve(ma);
-            h.Color = Color.blue;
-            h = grid.plot_curve(mb);
-            h.Color = Color.blue;
-            h = grid.plot_curve(mc);
-            h.Color = Color.blue;
-
-            h = grid.plot_curve(na);
-            h.Color = Color.red;
-
-            h = grid.plot_curve(ka);
-            h.Color = Color.red;
-
-            [a(1),a(2)] = ma(1/3);
-            [b(1),b(2)] = mb(1/3);
-            [c(1),c(2)] = mc(1/3);
-
-            d = ka(1-1/3);
-
-
-            grid.label_pt(a,b,c,d);
-
-
-            % t = linspace(0,1,N);
-            % for i = 1:N
-            %     sa = @(s)o.Sa(s,t(i));
-            %     sb = @(s)o.Sb(s,t(i));
-            %     sc = @(s)o.Sc(s,t(i));
-
-            %     h = grid.plot_curve(sa);
-            %     h.Color = Color.blue;
-            %     h = grid.plot_curve(sb);
-            %     h.Color = Color.blue;
-            %     h = grid.plot_curve(sc);
-            %     h.Color = Color.blue;
-            % end
-
-            h = grid.plot_curve(o.g1);
-            h.LineWidth = 2;
-            h.Color = Color.red;
-
-            h = grid.plot_curve(o.g2);
-            h.LineWidth = 2;
-            h.Color = Color.red;
-
-            h = grid.plot_curve(o.g3);
-            h.LineWidth = 2;
-            h.Color = Color.red;
-
-        end
-
-
-    end
-
-    methods(Static)
-        % Makes a mapping from the unit square to a triangle by collapsing
-        % one of the sides of the squares to a corner on the triangle
-        % The collapsed side is mapped to the corner oposite to g1.
-        % This is done such that for S(s,t), S(s,1) = g1(s)
-        function S = square_to_triangle_interp(g1,g2,g3)
-            corner = grid.line_segment(g3(0),g3(0));
-            S = grid.transfinite_interp(corner,g3,f(g1),f(g2))
-
-            % Function to flip a curve
-            function h = f(g)
-                h = @(t)g(1-t);
-            end
-        end
-    end
-
-end
-
-% % Return a mapping from u.v to x,y of the domain encircled by g1 g2 g3 in the the positive direction. created be using transfinite interpolation.
-% function S = triang_interp(g1,g2,g3)
-%     A = g1(0)
-%     B = g2(0)
-%     C = g3(0)
-
-%     function [x,y] = S_fun(u,v)
-%         w = sqrt((u-1)^2+v^2)/sqrt(2); % Parameter for g3
-%         v = v*(1-u-v)*g1(u) + u*(1-u-v)*g2(v) + u*v*g3(w) ...
-%             +(1-u)*(1-v)*A+u*(1-v)*B + (1-u)*v*C;
-%         x = v(1);
-%         y = v(2);
-%     end
-%     S = @S_fun;
-% end
-
-
-
-% function subsref(obj,S)
-%       if ~all(isnumeric(S.subs{:}))
-%         error('Only supports calling object with number')
-%       end
-%       if numel(S.subs{:}) > 1
-%         disp('You''ve called the object with more than one argument');
-%       else
-%         disp(['You called the object with argument = ',num2str(S.subs{:})]);
-%       end
-%     end
\ No newline at end of file
--- a/+grid/old/triang_interp_pts.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-% Creates a transfinite interpolation from connecting the four points wiht straight lines.
-function [S, g1, g2, g3] = triang_interp_pts(p1,p2,p3)
-    if size(p1) ~= [2 1]
-        error('p1 is strange!');
-    end
-
-    g1 = @(t)(p1 + t*(p2-p1));
-    g2 = @(t)(p2 + t*(p3-p2));
-    g3 = @(t)(p3 + t*(p1-p3));
-
-    S = grid.triang_interp(g1,g2,g3);
-end
--- a/+grid/old/triang_map.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-% Creates a grid [X,Y] from the mapping function S at points in vectors u,v
-function [X, Y] = traing_map(S,u,v)
-    error('not done')
-    if nargin == 2
-        v = u;
-    end
-
-    if isscalar(u)
-        u = linspace(0,1,u);
-    end
-
-    if isscalar(v)
-        v = linspace(0,1,v);
-    end
-
-    nu = length(u);
-    nv = length(v);
-
-    X = zeros(nu,nv);
-    Y = zeros(nu,nv);
-
-    for i = 1:nu
-        for j = 1:nv
-            [x,y] = S(u(i),v(j));
-            X(i,j) = x;
-            Y(i,j) = y;
-        end
-    end
-end
--- a/+grid/old/triang_plot_interp.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-% Plots a transfinite interpolation in x,y space using nu and nv curves along u and v axes.
-
-
-
-
-
-
-% Plots a interp of a triangle where one the interpolation is from a square
-% with one side collapsed to
-function h = triang_plot_interp_kindaworking(S,n)
-    u = linspace(0,1,n);
-    v = linspace(0,1,n);
-
-    m = 100;
-    m = 20;
-
-    Xl_curves = cell(n,1);
-    Xr_curves = cell(n,1);
-    Y_curves = cell(n,1);
-
-
-    function u = wierdness(v,d,N)
-        if N == 0
-            u = 0;
-        else
-            u = N*d./(1-v);
-        end
-    end
-
-
-    %Y curves
-    t = linspace(0,1,m);
-    for i = 1:n
-        x = []; y = [];
-        for j = 1:length(t)
-            [x(j),y(j)] = S(t(j),v(i));
-        end
-        Y_curves{i} = [x', y'];
-    end
-
-
-    % Right and left X curves
-    t = linspace(0,1,m);
-    d = u(2);
-    for i = 1:n
-        xl = []; yl = [];
-        xr = []; yr = [];
-        N = i-1;
-        t = linspace(0,1-N*d,m);
-        for j = 1:length(t)
-            w = wierdness(t(j),d,N);
-            [xr(j),yr(j)] = S(w,t(j));
-            [xl(j),yl(j)] = S(1-w,t(j));
-        end
-        Xl_curves{i} = [xl', yl'];
-        Xr_curves{i} = [xr', yr'];
-    end
-
-    for i = 1:n-1
-        line(Xl_curves{i}(:,1),Xl_curves{i}(:,2))
-        line(Xr_curves{i}(:,1),Xr_curves{i}(:,2))
-        line(Y_curves{i}(:,1),Y_curves{i}(:,2))
-    end
-end
-
-
-
-
-function h = triang_plot_interp_nonworking(S,n)
-
-    u = linspace(0,1,n);
-    v = linspace(0,1,n);
-
-    m = 100;
-
-    X_curves = cell(n-1,1);
-    Y_curves = cell(n-1,1);
-    K_curves = cell(n-1,1);
-
-
-    t = linspace(0,1,m);
-    for i = 1:n-1
-        x = []; y = [];
-        for j = find(t+u(i) <= 1)
-            [x(j),y(j)] = S(u(i),t(j));
-        end
-        X_curves{i} = [x', y'];
-    end
-
-    for i = 1:n-1
-        x = []; y = [];
-        for j = find(t+v(i) <= 1)
-            [x(j),y(j)] = S(t(j),v(i));
-        end
-        Y_curves{i} = [x', y'];
-    end
-
-    for i = 2:n
-        x = []; y = [];
-        for j = find(t<u(i))
-            [x(j),y(j)] = S(t(j), u(i)-t(j));
-        end
-        K_curves{i-1} = [x', y'];
-    end
-
-    for i = 1:n-1
-        line(X_curves{i}(:,1),X_curves{i}(:,2))
-        line(Y_curves{i}(:,1),Y_curves{i}(:,2))
-        line(K_curves{i}(:,1),K_curves{i}(:,2))
-    end
-
-    h = -1;
-    % h = plot(X_curves{:},Y_curves{:},K_curves{:});
-end
--- a/+grid/old/triang_show.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-% Show a grid as a matlab figure.
-function triang_show(S,n)
-
-    ih = ishold();
-
-    hold on
-    h_grid = grid.triang_plot_interp(S,n);
-    h_bord = grid.triang_plot_interp(S,2);
-
-    set(h_grid,'Color',[0 0.4470 0.7410]);
-    set(h_bord,'Color',[0.8500 0.3250 0.0980]);
-    set(h_bord,'LineWidth',2);
-
-    % axis auto
-    % axis equal
-    % axis square
-
-    if ih
-        hold on
-    else
-        hold off
-    end
-end
--- a/+grid/place_label.m	Mon Dec 21 15:06:16 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-function place_label(pt,str,horzAl,vertAl)
-    default_arg('horzAl','center');
-    default_arg('vertAl', 'middle');
-
-    x = pt(1);
-    y = pt(2);
-    h = text(x,y,str);
-    h.HorizontalAlignment = horzAl;
-    h.VerticalAlignment = vertAl;
-end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/Curve.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,388 @@
+classdef Curve
+    properties
+        g
+        gp
+        transformation
+    end
+
+    methods
+        %TODO:
+        % Concatenation of curves
+        % Subsections of curves
+        % Stretching of curve parameter - done for arc length.
+        % Curve to cell array of linesegments
+
+        % Returns a curve object.
+        %   g -- curve parametrization for parameter between 0 and 1
+        %  gp -- parametrization of curve derivative
+        function obj = Curve(g,gp,transformation)
+            default_arg('gp',[]);
+            default_arg('transformation',[]);
+            p_test = g(0);
+            assert(all(size(p_test) == [2,1]), 'A curve parametrization must return a 2x1 vector.');
+
+            if ~isempty(transformation)
+                transformation.base_g = g;
+                transformation.base_gp = gp;
+                [g,gp] = grid.Curve.transform_g(g,gp,transformation);
+            end
+
+            obj.g = g;
+            obj.gp = gp;
+            obj.transformation = transformation;
+
+        end
+
+        function n = normal(obj,t)
+            assert(~isempty(obj.gp),'Curve has no derivative!');
+            deriv = obj.gp(t);
+            normalization = sqrt(sum(deriv.^2,1));
+            n = [-deriv(2,:)./normalization; deriv(1,:)./normalization];
+        end
+
+
+        % Plots a curve g(t) for 0<t<1, using n points. Returns a handle h to the plotted curve.
+        %   h = plot_curve(g,n)
+        function h = plot(obj,n)
+            default_arg('n',100);
+
+            t = linspace(0,1,n);
+            p = obj.g(t);
+            h = line(p(1,:),p(2,:));
+        end
+
+        function h= plot_normals(obj,l,n,m)
+            default_arg('l',0.1);
+            default_arg('n',10);
+            default_arg('m',100);
+            t_n = linspace(0,1,n);
+
+            normals = obj.normal(t_n)*l;
+
+            n0 = obj.g(t_n);
+            n1 = n0 + normals;
+
+            h = line([n0(1,:); n1(1,:)],[n0(2,:); n1(2,:)]);
+            set(h,'Color',Color.red);
+            obj.plot(m);
+        end
+
+        function h= show(obj,name)
+            p = obj.g(1/2);
+            n = obj.normal(1/2);
+            p = p + n*0.1;
+
+            % Add arrow
+
+            h = text(p(1),p(2),name);
+            h.HorizontalAlignment = 'center';
+            h.VerticalAlignment = 'middle';
+
+            obj.plot();
+        end
+            % Shows curve with name and arrow for direction.
+
+        % Length of arc from parameter t0 to t1 (which may be vectors).
+        % Computed using derivative.
+        function L = arcLength(obj,t0,t1)
+            assert(~isempty(obj.gp),'Curve has no derivative!');
+            speed = @(t) sqrt(sum(obj.gp(t).^2,1));
+            L =  integral_vec(speed,t0,t1);
+        end
+
+        % Creates the arc length parameterization of a curve.
+        %    N -- number of points used to approximate the arclength function
+        function curve = arcLengthParametrization(obj,N)
+            default_arg('N',100);
+            assert(~isempty(obj.gp),'Curve has no derivative!');
+
+            % Construct arcLength function using splines
+            tvec = linspace(0,1,N);
+            arcVec = obj.arcLength(0,tvec);
+            tFunc = spline(arcVec,tvec); % t as a function of arcLength
+            L = obj.arcLength(0,1);
+            arcPar = @(s) tFunc(s*L);
+
+            % New function and derivative
+            g_new = @(t)obj.g(arcPar(t));
+            gp_new = @(t) normalize(obj.gp(arcPar(t)));
+            curve = grid.Curve(g_new,gp_new);
+
+        end
+
+        % how to make it work for methods without returns
+        function p = subsref(obj,S)
+            %Should i add error checking here?
+            %Maybe if you want performance you fetch obj.g and then use that
+            switch S(1).type
+                case '()'
+                    p = obj.g(S.subs{1});
+                % case '.'
+
+                    % p = obj.(S.subs);
+                otherwise
+                    p = builtin('subsref',obj,S);
+                    % error()
+            end
+        end
+
+
+        %% TRANSFORMATION OF A CURVE
+        function D = reverse(C)
+            % g = C.g;
+            % gp = C.gp;
+            % D = grid.Curve(@(t)g(1-t),@(t)-gp(1-t));
+            D = C.transform([],[],-1);
+        end
+
+        function D = transform(C,A,b,flip)
+            default_arg('A',[1 0; 0 1]);
+            default_arg('b',[0; 0]);
+            default_arg('flip',1);
+            if isempty(C.transformation)
+                g  = C.g;
+                gp = C.gp;
+                transformation.A = A;
+                transformation.b = b;
+                transformation.flip = flip;
+            else
+                g  = C.transformation.base_g;
+                gp = C.transformation.base_gp;
+                A_old = C.transformation.A;
+                b_old = C.transformation.b;
+                flip_old = C.transformation.flip;
+
+                transformation.A = A*A_old;
+                transformation.b = A*b_old + b;
+                transformation.flip = flip*flip_old;
+            end
+
+            D = grid.Curve(g,gp,transformation);
+
+        end
+
+        function D = translate(C,a)
+            g = C.g;
+            gp = C.gp;
+
+            % function v = g_fun(t)
+            %     x = g(t);
+            %     v(1,:) = x(1,:)+a(1);
+            %     v(2,:) = x(2,:)+a(2);
+            % end
+
+            % D = grid.Curve(@g_fun,gp);
+
+            D = C.transform([],a);
+        end
+
+        function D = mirror(C, a, b)
+            assert_size(a,[2,1]);
+            assert_size(b,[2,1]);
+
+            g = C.g;
+            gp = C.gp;
+
+            l = b-a;
+            lx = l(1);
+            ly = l(2);
+
+
+            % fprintf('Singular?\n')
+
+            A = [lx^2-ly^2 2*lx*ly; 2*lx*ly ly^2-lx^2]/(l'*l);
+
+            % function v = g_fun(t)
+            %     % v = a + A*(g(t)-a)
+            %     x = g(t);
+
+            %     ax1 = x(1,:)-a(1);
+            %     ax2 = x(2,:)-a(2);
+            %     v(1,:) = a(1)+A(1,:)*[ax1;ax2];
+            %     v(2,:) = a(2)+A(2,:)*[ax1;ax2];
+            % end
+
+            % function v = gp_fun(t)
+            %     v = A*gp(t);
+            % end
+
+            % D = grid.Curve(@g_fun,@gp_fun);
+
+            % g = A(g-a)+a = Ag - Aa + a;
+            b = - A*a + a;
+            D = C.transform(A,b);
+
+        end
+
+        function D = rotate(C,a,rad)
+            assert_size(a, [2,1]);
+            assert_size(rad, [1,1]);
+            g = C.g;
+            gp = C.gp;
+
+
+            A = [cos(rad) -sin(rad); sin(rad) cos(rad)];
+
+            % function v = g_fun(t)
+            %     % v = a + A*(g(t)-a)
+            %     x = g(t);
+
+            %     ax1 = x(1,:)-a(1);
+            %     ax2 = x(2,:)-a(2);
+            %     v(1,:) = a(1)+A(1,:)*[ax1;ax2];
+            %     v(2,:) = a(2)+A(2,:)*[ax1;ax2];
+            % end
+
+            % function v = gp_fun(t)
+            %     v = A*gp(t);
+            % end
+
+            % D = grid.Curve(@g_fun,@gp_fun);
+
+
+             % g = A(g-a)+a = Ag - Aa + a;
+            b = - A*a + a;
+            D = C.transform(A,b);
+        end
+    end
+
+    methods (Static)
+
+        % Computes the derivative of g: R -> R^2 using an operator D1
+        function gp_out = numericalDerivative(g,D1)
+            m = length(D1);
+            t = linspace(0,1,m);
+            gVec = g(t)';
+            gpVec = (D1*gVec)';
+
+            gp1_fun = spline(t,gpVec(1,:));
+            gp2_fun = spline(t,gpVec(2,:));
+            gp_out = @(t) [gp1_fun(t);gp2_fun(t)];
+        end
+
+        function obj = line(p1, p2)
+
+            function v = g_fun(t)
+                v(1,:) = p1(1) + t.*(p2(1)-p1(1));
+                v(2,:) = p1(2) + t.*(p2(2)-p1(2));
+            end
+            g = @g_fun;
+
+            obj = grid.Curve(g);
+        end
+
+        function obj = circle(c,r,phi)
+            default_arg('phi',[0; 2*pi])
+            default_arg('c',[0; 0])
+            default_arg('r',1)
+
+            function v = g_fun(t)
+                w = phi(1)+t*(phi(2)-phi(1));
+                v(1,:) = c(1) + r*cos(w);
+                v(2,:) = c(2) + r*sin(w);
+            end
+
+            function v = g_fun_deriv(t)
+                w = phi(1)+t*(phi(2)-phi(1));
+                v(1,:) = -(phi(2)-phi(1))*r*sin(w);
+                v(2,:) =  (phi(2)-phi(1))*r*cos(w);
+            end
+
+            obj = grid.Curve(@g_fun,@g_fun_deriv);
+        end
+
+        function obj = bezier(p0, p1, p2, p3)
+            function v = g_fun(t)
+                v(1,:) = (1-t).^3*p0(1) + 3*(1-t).^2.*t*p1(1) + 3*(1-t).*t.^2*p2(1) + t.^3*p3(1);
+                v(2,:) = (1-t).^3*p0(2) + 3*(1-t).^2.*t*p1(2) + 3*(1-t).*t.^2*p2(2) + t.^3*p3(2);
+            end
+
+            function v = g_fun_deriv(t)
+                v(1,:) = 3*(1-t).^2*(p1(1)-p0(1)) + 6*(1-t).*t*(p2(1)-p1(1)) + 3*t.^2*(p3(1)-p2(1));
+                v(2,:) = 3*(1-t).^2*(p1(2)-p0(2)) + 6*(1-t).*t*(p2(2)-p1(2)) + 3*t.^2*(p3(2)-p2(2));
+            end
+
+            obj = grid.Curve(@g_fun,@g_fun_deriv);
+        end
+
+
+        function [g_out,gp_out] = transform_g(g,gp,tr)
+            A = tr.A;
+            b = tr.b;
+            flip = tr.flip;
+
+            function v = g_fun_noflip(t)
+                % v = A*g + b
+                x = g(t);
+
+                v(1,:) = A(1,:)*x+b(1);
+                v(2,:) = A(2,:)*x+b(2);
+            end
+
+            function v = g_fun_flip(t)
+                % v = A*g + b
+                x = g(1-t);
+
+                v(1,:) = A(1,:)*x+b(1);
+                v(2,:) = A(2,:)*x+b(2);
+            end
+
+
+            switch flip
+                case 1
+                    g_out  = @g_fun_noflip;
+                    gp_out = @(t)A*gp(t);
+                case -1
+                    g_out  = @g_fun_flip;
+                    gp_out = @(t)-A*gp(1-t);
+            end
+        end
+
+    end
+end
+
+
+
+function g_norm = normalize(g0)
+    g1 = g0(1,:);
+    g2 = g0(2,:);
+    normalization = sqrt(sum(g0.^2,1));
+    g_norm = [g1./normalization; g2./normalization];
+end
+
+function I = integral_vec(f,a,b)
+% Wrapper around the built-in function integral that
+% handles multiple limits.
+
+    Na = length(a);
+    Nb = length(b);
+    assert(Na == 1 || Nb == 1 || Na==Nb,...
+        'a and b must have same length, unless one is a scalar.');
+
+    if(Na>Nb);
+        I = zeros(size(a));
+        for i = 1:Na
+            I(i) = integral(f,a(i),b);
+        end
+    elseif(Nb>Na)
+        I = zeros(size(b));
+        for i = 1:Nb
+            I(i) = integral(f,a,b(i));
+        end
+    else
+        I = zeros(size(b));
+        for i = 1:Nb
+            I(i) = integral(f,a(i),b(i));
+        end
+    end
+end
+
+% Returns a function handle to the spline.
+function f = spline(tval,fval,spline_order)
+    default_arg('spline_order',4);
+    [m,~] = size(tval);
+    assert(m==1,'Need row vectors.');
+
+    f_spline = spapi( optknt(tval,spline_order), tval, fval );
+    f = @(t) fnval(f_spline,t);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/Ti.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,201 @@
+classdef Ti
+    properties
+        gs % {4}Curve
+        S  % FunctionHandle(u,v)
+    end
+
+    methods
+        % TODO function to label boundary names.
+        %  function to find largest and smallest delta h in the grid. Maybe shouldnt live here
+        function obj = Ti(C1,C2,C3,C4)
+            obj.gs = {C1,C2,C3,C4};
+
+            g1 = C1.g;
+            g2 = C2.g;
+            g3 = C3.g;
+            g4 = C4.g;
+
+            A = g1(0);
+            B = g2(0);
+            C = g3(0);
+            D = g4(0);
+
+            function o = S_fun(u,v)
+                x1 = g1(u);
+                x2 = g2(v);
+                x3 = g3(1-u);
+                x4 = g4(1-v);
+                o1 = (1-v).*x1(1,:) + u.*x2(1,:) + v.*x3(1,:) + (1-u).*x4(1,:) ...
+                    -((1-u)*(1-v).*A(1,:) + u*(1-v).*B(1,:) + u*v.*C(1,:) + (1-u)*v.*D(1,:));
+                o2 = (1-v).*x1(2,:) + u.*x2(2,:) + v.*x3(2,:) + (1-u).*x4(2,:) ...
+                    -((1-u)*(1-v).*A(2,:) + u*(1-v).*B(2,:) + u*v.*C(2,:) + (1-u)*v.*D(2,:));
+
+                o = [o1;o2];
+            end
+
+            obj.S = @S_fun;
+        end
+
+        function [X,Y] = map(obj,u,v)
+            default_arg('v',u);
+
+            if isscalar(u)
+                u = linspace(0,1,u);
+            end
+
+            if isscalar(v)
+                v = linspace(0,1,v);
+            end
+
+            S = obj.S;
+
+            nu = length(u);
+            nv = length(v);
+
+            X = zeros(nv,nu);
+            Y = zeros(nv,nu);
+
+            u = rowVector(u);
+            v = rowVector(v);
+
+            for i = 1:nv
+                p = S(u,v(i));
+                X(i,:) = p(1,:);
+                Y(i,:) = p(2,:);
+            end
+        end
+
+        function h = plot(obj,nu,nv)
+            S = obj.S;
+
+            default_arg('nv',nu)
+
+            u = linspace(0,1,nu);
+            v = linspace(0,1,nv);
+
+            m = 100;
+
+            X = zeros(nu+nv,m);
+            Y = zeros(nu+nv,m);
+
+
+            t = linspace(0,1,m);
+            for i = 1:nu
+                p = S(u(i),t);
+                X(i,:) = p(1,:);
+                Y(i,:) = p(2,:);
+            end
+
+            for i = 1:nv
+                p = S(t,v(i));
+                X(i+nu,:) = p(1,:);
+                Y(i+nu,:) = p(2,:);
+            end
+
+            h = line(X',Y');
+        end
+
+
+        function h = show(obj,nu,nv)
+            default_arg('nv',nu)
+            S = obj.S;
+
+            if(nu>2 || nv>2)
+                h_grid = obj.plot(nu,nv);
+                set(h_grid,'Color',[0 0.4470 0.7410]);
+            end
+
+            h_bord = obj.plot(2,2);
+            set(h_bord,'Color',[0.8500 0.3250 0.0980]);
+            set(h_bord,'LineWidth',2);
+        end
+
+
+        % TRANSFORMATIONS
+        function ti = translate(obj,a)
+            gs = obj.gs;
+
+            for i = 1:length(gs)
+                new_gs{i} = gs{i}.translate(a);
+            end
+
+            ti = grid.Ti(new_gs{:});
+        end
+
+        % Mirrors the Ti so that the resulting Ti is still left handed.
+        %  (Corrected by reversing curves and switching e and w)
+        function ti = mirror(obj, a, b)
+            gs = obj.gs;
+
+            new_gs = cell(1,4);
+
+            new_gs{1} = gs{1}.mirror(a,b).reverse();
+            new_gs{3} = gs{3}.mirror(a,b).reverse();
+            new_gs{2} = gs{4}.mirror(a,b).reverse();
+            new_gs{4} = gs{2}.mirror(a,b).reverse();
+
+            ti = grid.Ti(new_gs{:});
+        end
+
+        function ti = rotate(obj,a,rad)
+            gs = obj.gs;
+
+            for i = 1:length(gs)
+                new_gs{i} = gs{i}.rotate(a,rad);
+            end
+
+            ti = grid.Ti(new_gs{:});
+        end
+
+        function ti = rotate_edges(obj,n);
+            new_gs = cell(1,4);
+            for i = 0:3
+                new_i = mod(i - n,4);
+                new_gs{new_i+1} = obj.gs{i+1};
+            end
+            ti = grid.Ti(new_gs{:});
+        end
+    end
+
+    methods(Static)
+        function obj = points(p1, p2, p3, p4)
+            g1 = grid.Curve.line(p1,p2);
+            g2 = grid.Curve.line(p2,p3);
+            g3 = grid.Curve.line(p3,p4);
+            g4 = grid.Curve.line(p4,p1);
+
+            obj = grid.Ti(g1,g2,g3,g4);
+        end
+
+        function label(varargin)
+            if nargin == 2 && ischar(varargin{2})
+                label_impl(varargin{:});
+            else
+                for i = 1:length(varargin)
+                    label_impl(varargin{i},inputname(i));
+                end
+            end
+
+
+            function label_impl(ti,str)
+                S = ti.S;
+
+                pc = S(0.5,0.5);
+
+                margin = 0.1;
+                pw = S(  margin,      0.5);
+                pe = S(1-margin,      0.5);
+                ps = S(     0.5,   margin);
+                pn = S(     0.5, 1-margin);
+
+
+                ti.show(2,2);
+                grid.place_label(pc,str);
+                grid.place_label(pw,'w');
+                grid.place_label(pe,'e');
+                grid.place_label(ps,'s');
+                grid.place_label(pn,'n');
+            end
+        end
+    end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/equal_step_size.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,4 @@
+% Calculates M so that the stepsize m/M is as close to n/M as possible
+function M = equal_step_size(n,N,m)
+    M = round((m*(N-1)+n)/n);
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/concat_curve.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,12 @@
+% Concatenate two curves g1 and g2 intop
+%   g = concat_curve(g1,g2)
+function g = concat_curve(g1,g2)
+    function v = g_fun(t)
+        if t < 1/2
+            v = g1(2*t);
+        else
+            v = g2(2*t-1);
+        end
+    end
+    g = @g_fun;
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/curve_discretise.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,97 @@
+% Discretises the curve g with the smallest number of points such that all segments
+% are shorter than h. If do_plot is true the points of the discretisation and
+% the normals of the curve in those points are plotted.
+%
+%   [t,p,d] = curve_discretise(g,h,do_plot)
+%
+%   t is a vector of input values to g.
+%   p is a cector of points.
+%   d are the length of the segments.
+function [t,p,d] = curve_discretise(g,h,do_plot)
+    default_arg('do_plot',false)
+
+    n = 10;
+
+    [t,p,d] = curve_discretise_n(g,n);
+
+    % ni = 0;
+    while any(d>h)
+        [t,p,d] = curve_discretise_n(g,n);
+        n = ceil(n*d(1)/h);
+        % ni = ni+1;
+    end
+
+    % nj = 0;
+    while all(d<h)
+        [t,p,d] = curve_discretise_n(g,n);
+        n = n-1;
+        % nj = nj+1;
+    end
+    [t,p,d] = curve_discretise_n(g,n+1);
+
+    % fprintf('ni = %d, nj = %d\n',ni,nj);
+
+    if do_plot
+        fprintf('n:%d  max: %f min: %f\n', n, max(d),min(d));
+        p = grid.map_curve(g,t);
+        figure
+        show(g,t,h);
+    end
+
+end
+
+function [t,p,d] = curve_discretise_n(g,n)
+    t = linspace(0,1,n);
+    t = equalize_d(g,t);
+    d = D(g,t);
+    p = grid.map_curve(g,t);
+end
+
+function d = D(g,t)
+    p = grid.map_curve(g,t);
+
+    d = zeros(1,length(t)-1);
+    for i = 1:length(d)
+        d(i) = norm(p(:,i) - p(:,i+1));
+    end
+end
+
+function t = equalize_d(g,t)
+    d = D(g,t);
+    v = d-mean(d);
+    while any(abs(v)>0.01*mean(d))
+        dt = t(2:end)-t(1:end-1);
+        t(2:end) = t(2:end) - cumsum(dt.*v./d);
+
+        t = t/t(end);
+        d = D(g,t);
+        v = d-mean(d);
+    end
+end
+
+
+function show(g,t,hh)
+    p = grid.map_curve(g,t);
+
+
+
+    h = grid.plot_curve(g);
+    h.LineWidth = 2;
+    axis equal
+    hold on
+    h = plot(p(1,:),p(2,:),'.');
+    h.Color = [0.8500 0.3250 0.0980];
+    h.MarkerSize = 24;
+    hold off
+
+    n = grid.curve_normals(g,t);
+    hold on
+    for  i = 1:length(t)
+        p0 = p(:,i);
+        p1 = p0 + hh*n(:,i);
+        l = [p0, p1];
+        h = plot(l(1,:),l(2,:));
+        h.Color = [0.8500 0.3250 0.0980];
+    end
+
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/curve_interp.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,58 @@
+% Create a cubic spline from the points (x,y) using periodic conditions.
+%   g = curve_interp(x,y)
+function g = curve_interp(x,y)
+    default_arg('x',[0 2 2 1 1 0])
+    default_arg('y',[0 0 2 2 1 1])
+    % solve for xp and yp
+
+    % x(t) = at^4 + bt^2+ct+d
+
+    % a = xp1 -2x1 + 2x0 +  xp0
+    % b = 3x1 -xp1 - 3x0 + 2xp0
+    % c = xp0
+    % d = x0
+
+    assert(length(x) == length(y))
+    n = length(x);
+    A = spdiags(ones(n,1)*[2, 8, 2],-1:1,n,n);
+    A(n,1) = 2;
+    A(1,n) = 2;
+
+    bx = zeros(n,1);
+    for i = 2:n-1
+        bx(i) = -6*x(i-1)+6*x(i+1);
+    end
+    bx(1) = -6*x(n)+6*x(2);
+    bx(n) = -6*x(n-1)+6*x(1);
+
+    by = zeros(n,1);
+    for i = 2:n-1
+        by(i) = -6*y(i-1)+6*y(i+1);
+    end
+    by(1) = -6*y(n)+6*y(2);
+    by(n) = -6*y(n-1)+6*y(1);
+
+
+    xp = A\bx;
+    yp = A\by;
+
+    x(end+1) = x(1);
+    y(end+1) = y(1);
+
+    xp(end+1) = xp(1);
+    yp(end+1) = yp(1);
+
+    function v = g_fun(t)
+        t = mod(t,1);
+        i = mod(floor(t*n),n) + 1;
+        t = t * n -(i-1);
+        X = (2*x(i)-2*x(i+1)+xp(i)+xp(i+1))*t.^3 + (-3*x(i)+3*x(i+1)-2*xp(i)-xp(i+1))*t.^2 + (xp(i))*t + x(i);
+        Y = (2*y(i)-2*y(i+1)+yp(i)+yp(i+1))*t.^3 + (-3*y(i)+3*y(i+1)-2*yp(i)-yp(i+1))*t.^2 + (yp(i))*t + y(i);
+        v = [X;Y];
+    end
+
+    g = @g_fun;
+end
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/max_h.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,33 @@
+function [d_max, i1_max, j1_max, i2_max, j2_max] = max_h(X,Y)
+    ni = size(X,1);
+    nj = size(X,2);
+    d_max = 0;
+
+    i1_max = 0;
+    j1_max = 0;
+    i2_max = 0;
+    j2_max = 0;
+
+    D = {[0,-1],[1,0],[0,1],[-1,0]};
+
+    for i = 1:ni
+        for j = 1:nj
+            p1 = [X(i,j); Y(i,j)];
+            for k = 1:length(D)
+                i2 = i+D{k}(1);
+                j2 = j+D{k}(2);
+                if i2 >= 1 && i2 <= ni && j2 >= 1 && j2 <= nj
+                    p2 = [X(i2,j2); Y(i2,j2)];
+                    d = norm(p2-p1);
+                    if d > d_max;
+                        d_max = d;
+                        i1_max = i;
+                        j1_max = j;
+                        i2_max = i2;
+                        j2_max = j2;
+                    end
+                end
+            end
+        end
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/min_h.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,34 @@
+function [d_min, i1_min, j1_min, i2_min, j2_min] = min_h(X,Y)
+    ni = size(X,1);
+    nj = size(X,2);
+    d_min = norm([X(1,1);Y(1,1)] - [X(ni,nj);Y(ni,nj)]);
+
+    i1_min = 0;
+    j1_min = 0;
+    i2_min = 0;
+    j2_min = 0;
+
+    D = {[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]};
+    % D = {[0,-1],[1,0],[0,1],[-1,0]};
+
+    for i = 1:ni
+        for j = 1:nj
+            p1 = [X(i,j); Y(i,j)];
+            for k = 1:length(D)
+                i2 = i+D{k}(1);
+                j2 = j+D{k}(2);
+                if i2 >= 1 && i2 <= ni && j2 >= 1 && j2 <= nj
+                    p2 = [X(i2,j2); Y(i2,j2)];
+                    d = norm(p2-p1);
+                    if d < d_min;
+                        d_min = d;
+                        i1_min = i;
+                        j1_min = j;
+                        i2_min = i2;
+                        j2_min = j2;
+                    end
+                end
+            end
+        end
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/plot_shape.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,12 @@
+% Plot a shape using n points. Returns cell array of plot handles.
+%   hs = plot_shape(s,n)
+function hs = plot_shape(s,n)
+    default_arg('n',100);
+
+    hs = {};
+    hold on
+    for i = 1:length(s)
+        hs{end+1} = grid.plot_curve(s{i},n);
+    end
+    hold off
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/shape.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,4 @@
+% Creates a shape from a number of curves. A shape is a cell array of curves.
+function s = shape(varargin);
+    s = varargin;
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/shape_discretise.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,8 @@
+% Discretises a shape such that points on the curves are no further than h appart.
+function p = shape_discretise(s,h)
+    p = [];
+    for i = 1:length(s)
+        [~,pt] = grid.curve_discretise(s{i},h);
+        p = [p, pt];
+    end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/shape_linesegments.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,9 @@
+% Converts a shape into a cell array of linesegments shorter than h.
+function l = shape_linesegments(s,h)
+    l = {};
+
+    for i = 1:length(s)
+        t = grid.curve_discretise(s{i},h);
+        l = [l, grid.curve_linesegments(s{i},t)];
+    end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/triang_interp.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,132 @@
+classdef triang_interp
+    properties
+        g1, g2 ,g3  % Curves encirling the tirangle in the positive direction.
+        A,B,C  % The corners of the triangle
+        Sa, Sb, Sc % Mappings from square with different sides collapsed
+    end
+
+    methods
+        function o = triang_interp(g1,g2,g3)
+            o.g1 = g1;
+            o.g2 = g2;
+            o.g3 = g3;
+            o.A = g1(0);
+            o.B = g2(0);
+            o.C = g3(0);
+            o.Sa = grid.triang_interp.square_to_triangle_interp(g2,g3,g1);
+            o.Sb = grid.triang_interp.square_to_triangle_interp(g3,g1,g2);
+            o.Sc = grid.triang_interp.square_to_triangle_interp(g1,g2,g3);
+        end
+
+
+        function show(o,N)
+            % Show the mapped meridians of the triangle.
+            % Might be used for the barycentric coordinates.
+            ma = @(t)o.Sa(1/2,1-t);
+            mb = @(t)o.Sb(1/2,1-t);
+            mc = @(t)o.Sc(1/2,1-t);
+
+            na = @(t)o.Sa(t,1/2);
+
+            ka = @(t)(o.g1(1-t)+o.g2(t))/2;
+
+            h = grid.plot_curve(ma);
+            h.Color = Color.blue;
+            h = grid.plot_curve(mb);
+            h.Color = Color.blue;
+            h = grid.plot_curve(mc);
+            h.Color = Color.blue;
+
+            h = grid.plot_curve(na);
+            h.Color = Color.red;
+
+            h = grid.plot_curve(ka);
+            h.Color = Color.red;
+
+            [a(1),a(2)] = ma(1/3);
+            [b(1),b(2)] = mb(1/3);
+            [c(1),c(2)] = mc(1/3);
+
+            d = ka(1-1/3);
+
+
+            grid.label_pt(a,b,c,d);
+
+
+            % t = linspace(0,1,N);
+            % for i = 1:N
+            %     sa = @(s)o.Sa(s,t(i));
+            %     sb = @(s)o.Sb(s,t(i));
+            %     sc = @(s)o.Sc(s,t(i));
+
+            %     h = grid.plot_curve(sa);
+            %     h.Color = Color.blue;
+            %     h = grid.plot_curve(sb);
+            %     h.Color = Color.blue;
+            %     h = grid.plot_curve(sc);
+            %     h.Color = Color.blue;
+            % end
+
+            h = grid.plot_curve(o.g1);
+            h.LineWidth = 2;
+            h.Color = Color.red;
+
+            h = grid.plot_curve(o.g2);
+            h.LineWidth = 2;
+            h.Color = Color.red;
+
+            h = grid.plot_curve(o.g3);
+            h.LineWidth = 2;
+            h.Color = Color.red;
+
+        end
+
+
+    end
+
+    methods(Static)
+        % Makes a mapping from the unit square to a triangle by collapsing
+        % one of the sides of the squares to a corner on the triangle
+        % The collapsed side is mapped to the corner oposite to g1.
+        % This is done such that for S(s,t), S(s,1) = g1(s)
+        function S = square_to_triangle_interp(g1,g2,g3)
+            corner = grid.line_segment(g3(0),g3(0));
+            S = grid.transfinite_interp(corner,g3,f(g1),f(g2))
+
+            % Function to flip a curve
+            function h = f(g)
+                h = @(t)g(1-t);
+            end
+        end
+    end
+
+end
+
+% % Return a mapping from u.v to x,y of the domain encircled by g1 g2 g3 in the the positive direction. created be using transfinite interpolation.
+% function S = triang_interp(g1,g2,g3)
+%     A = g1(0)
+%     B = g2(0)
+%     C = g3(0)
+
+%     function [x,y] = S_fun(u,v)
+%         w = sqrt((u-1)^2+v^2)/sqrt(2); % Parameter for g3
+%         v = v*(1-u-v)*g1(u) + u*(1-u-v)*g2(v) + u*v*g3(w) ...
+%             +(1-u)*(1-v)*A+u*(1-v)*B + (1-u)*v*C;
+%         x = v(1);
+%         y = v(2);
+%     end
+%     S = @S_fun;
+% end
+
+
+
+% function subsref(obj,S)
+%       if ~all(isnumeric(S.subs{:}))
+%         error('Only supports calling object with number')
+%       end
+%       if numel(S.subs{:}) > 1
+%         disp('You''ve called the object with more than one argument');
+%       else
+%         disp(['You called the object with argument = ',num2str(S.subs{:})]);
+%       end
+%     end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/triang_interp_pts.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,12 @@
+% Creates a transfinite interpolation from connecting the four points wiht straight lines.
+function [S, g1, g2, g3] = triang_interp_pts(p1,p2,p3)
+    if size(p1) ~= [2 1]
+        error('p1 is strange!');
+    end
+
+    g1 = @(t)(p1 + t*(p2-p1));
+    g2 = @(t)(p2 + t*(p3-p2));
+    g3 = @(t)(p3 + t*(p1-p3));
+
+    S = grid.triang_interp(g1,g2,g3);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/triang_map.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,29 @@
+% Creates a grid [X,Y] from the mapping function S at points in vectors u,v
+function [X, Y] = traing_map(S,u,v)
+    error('not done')
+    if nargin == 2
+        v = u;
+    end
+
+    if isscalar(u)
+        u = linspace(0,1,u);
+    end
+
+    if isscalar(v)
+        v = linspace(0,1,v);
+    end
+
+    nu = length(u);
+    nv = length(v);
+
+    X = zeros(nu,nv);
+    Y = zeros(nu,nv);
+
+    for i = 1:nu
+        for j = 1:nv
+            [x,y] = S(u(i),v(j));
+            X(i,j) = x;
+            Y(i,j) = y;
+        end
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/triang_plot_interp.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,114 @@
+% Plots a transfinite interpolation in x,y space using nu and nv curves along u and v axes.
+
+
+
+
+
+
+% Plots a interp of a triangle where one the interpolation is from a square
+% with one side collapsed to
+function h = triang_plot_interp_kindaworking(S,n)
+    u = linspace(0,1,n);
+    v = linspace(0,1,n);
+
+    m = 100;
+    m = 20;
+
+    Xl_curves = cell(n,1);
+    Xr_curves = cell(n,1);
+    Y_curves = cell(n,1);
+
+
+    function u = wierdness(v,d,N)
+        if N == 0
+            u = 0;
+        else
+            u = N*d./(1-v);
+        end
+    end
+
+
+    %Y curves
+    t = linspace(0,1,m);
+    for i = 1:n
+        x = []; y = [];
+        for j = 1:length(t)
+            [x(j),y(j)] = S(t(j),v(i));
+        end
+        Y_curves{i} = [x', y'];
+    end
+
+
+    % Right and left X curves
+    t = linspace(0,1,m);
+    d = u(2);
+    for i = 1:n
+        xl = []; yl = [];
+        xr = []; yr = [];
+        N = i-1;
+        t = linspace(0,1-N*d,m);
+        for j = 1:length(t)
+            w = wierdness(t(j),d,N);
+            [xr(j),yr(j)] = S(w,t(j));
+            [xl(j),yl(j)] = S(1-w,t(j));
+        end
+        Xl_curves{i} = [xl', yl'];
+        Xr_curves{i} = [xr', yr'];
+    end
+
+    for i = 1:n-1
+        line(Xl_curves{i}(:,1),Xl_curves{i}(:,2))
+        line(Xr_curves{i}(:,1),Xr_curves{i}(:,2))
+        line(Y_curves{i}(:,1),Y_curves{i}(:,2))
+    end
+end
+
+
+
+
+function h = triang_plot_interp_nonworking(S,n)
+
+    u = linspace(0,1,n);
+    v = linspace(0,1,n);
+
+    m = 100;
+
+    X_curves = cell(n-1,1);
+    Y_curves = cell(n-1,1);
+    K_curves = cell(n-1,1);
+
+
+    t = linspace(0,1,m);
+    for i = 1:n-1
+        x = []; y = [];
+        for j = find(t+u(i) <= 1)
+            [x(j),y(j)] = S(u(i),t(j));
+        end
+        X_curves{i} = [x', y'];
+    end
+
+    for i = 1:n-1
+        x = []; y = [];
+        for j = find(t+v(i) <= 1)
+            [x(j),y(j)] = S(t(j),v(i));
+        end
+        Y_curves{i} = [x', y'];
+    end
+
+    for i = 2:n
+        x = []; y = [];
+        for j = find(t<u(i))
+            [x(j),y(j)] = S(t(j), u(i)-t(j));
+        end
+        K_curves{i-1} = [x', y'];
+    end
+
+    for i = 1:n-1
+        line(X_curves{i}(:,1),X_curves{i}(:,2))
+        line(Y_curves{i}(:,1),Y_curves{i}(:,2))
+        line(K_curves{i}(:,1),K_curves{i}(:,2))
+    end
+
+    h = -1;
+    % h = plot(X_curves{:},Y_curves{:},K_curves{:});
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/old/triang_show.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,23 @@
+% Show a grid as a matlab figure.
+function triang_show(S,n)
+
+    ih = ishold();
+
+    hold on
+    h_grid = grid.triang_plot_interp(S,n);
+    h_bord = grid.triang_plot_interp(S,2);
+
+    set(h_grid,'Color',[0 0.4470 0.7410]);
+    set(h_bord,'Color',[0.8500 0.3250 0.0980]);
+    set(h_bord,'LineWidth',2);
+
+    % axis auto
+    % axis equal
+    % axis square
+
+    if ih
+        hold on
+    else
+        hold off
+    end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+parametrization/place_label.m	Mon Dec 21 15:09:32 2015 +0100
@@ -0,0 +1,10 @@
+function place_label(pt,str,horzAl,vertAl)
+    default_arg('horzAl','center');
+    default_arg('vertAl', 'middle');
+
+    x = pt(1);
+    y = pt(2);
+    h = text(x,y,str);
+    h.HorizontalAlignment = horzAl;
+    h.VerticalAlignment = vertAl;
+end
\ No newline at end of file