function defV = mosek_deform(insideT, outsideT, restV, constraintMap, frameNum, useAvgDefoGradient, cornerIdx, perTriRot, num_insideV, rot_angle)
    disp('MOSEK DEFORM');
    kk = 10000;
    
    %This is going to be similar to our cvx_deform, except instead of
    %calling CVX, we call MOSEK directly. 
    
    %At first, we assume no air!

    %restV is an nx2 array of vertex positions, where n is 
    %the number or verrtices.
    %
    %Faces is a kx3 array of indices for the vertices of a face, where k is
    %the number of faces. E.g. if a face is made of vertices 1, 4, and 2, then 
    %its row in Faces is [1, 4, 2]
    %
    %constraintMap a mapping of integers to 1x2 arrays. Semantically, it is
    %a mapping of the index of a constained vertex to its constrained
    %position. E.g. if we want to constrain the vertex in row 4 of restV to
    %be at position x', y', then we put in the mapping 4 -> [x' y'] in the
    %constraintMap.
   % tic;
    Faces = [insideT; outsideT];
    facesSize = size(Faces);
    insideFSize = size(insideT);
    restVSize = size(restV);

    %Gstar = calc_Gi_stack(Faces, restV);
    GstarCut = extractG(restV, Faces);
    
%     GstarCut = zeros(size(Gstar));
%     GstarCut(1:(4*insideFSize),:) = Gstar(1:(4*insideFSize),:);
    
    k = zeros(4*facesSize(1), 1);
    for i = 1:(facesSize(1))
       kIdx = 4*(i-1) + 1;
       Ri = perTriRot(:,:,i);
       k(kIdx:kIdx+3) = [Ri(1); Ri(2); Ri(3); Ri(4)];
    end
  
    l = -1 * transpose(GstarCut) * k;
    numConstrainedVtx = size(constraintMap.keys,2);
    % Number of constraints given to MOSEK is 2 times the number of constrained
    % vertices in 2D, because there is one constraint per dimension.
    numConstraints = 2*numConstrainedVtx;
    Aeq = zeros(numConstraints, 2*restVSize(1)); %neq by #x linear equality matrix
    beq = zeros(numConstraints, 1); %neq by 1 linear equality rhs
    
    constraintCount = 0;
    for idx = constraintMap.keys
        mapValue = constraintMap(idx{1});
        %which column to insert constraint into.
        xCol = 2*idx{1} - 1;
        yCol = 2*idx{1};
        constraintIdx = 2*constraintCount + 1;
        Aeq(constraintIdx, xCol) = 1;
        Aeq(constraintIdx + 1, yCol) = 1;
        beq(constraintIdx) = mapValue(1);
        beq(constraintIdx + 1) = mapValue(2);
        constraintCount = constraintCount + 1;
    end

    consM = [];
    consb = [];
    
    
    
    consM = [sqrt(kk)*sparse(Aeq)];
    consb = -kk*sparse(Aeq)'*beq;
    TAeq = Aeq;
    Tbeq = beq;
    Aeq = [];
    beq = [];

    
%     nCons = size(constraintMap.keys,2);
%     rowi = zeros(2*nCons,1);
%     vali = ones(2*nCons,1);
%     consRhs = zeros(2*size(restV,1),1);
%     constraintCount = 1;
%     for idx = constraintMap.keys
%         mapValue = constraintMap(idx{1});
%         rowi(2*constraintCount-1,1) = 2*idx{1} - 1;
%         rowi(2*constraintCount,1) = 2*idx{1};
%         consRhs(2*idx{1} - 1,1) = -mapValue(1);
%         consRhs(2*idx{1},1) = -mapValue(2);
%         constraintCount = constraintCount + 1;
%     end
%     
%     nV = size(restV,1);
%     consMat = kk*sparse(rowi, rowi, vali, size(GstarCut,1), 2*nV);
%     consRhs = kk*consRhs;
    
    
    GstarCut=[GstarCut;consM];
    %Setup parameters to feed to 'conic'
    F = [GstarCut];
    c = l + consb;
    % F = Gstar;
    % c = l;
    % Aeq = [];
    % beq = [];
    Aleq = [];
    bleq = [];
    lx = -Inf * ones(size(F,2),1);
    ux = Inf * ones(size(F,2),1);
    b = [];
    bc = [];

    GstarRows = size(GstarCut,1);
    
    GPrimeStack = [GstarCut,  sparse([],[],[],GstarRows, GstarRows), sparse([],[],[],GstarRows,1), sparse([],[],[],GstarRows,1)];
    
    
    %toc;
    %disp('SETUP 1 TIME ^');
    v = conic_semidefinite_multi(F, c, Aeq, beq, Aleq, bleq, lx, ux, b, bc, GPrimeStack, facesSize(1),k);
%OLD CVX STUFF BELOW... we want to change it to work with MOSEK.    
%     cvx_begin
%         variable v(2*restVSize(1),1);
%         variable t(4*insideFSize(1),1);
%         expression fis(4*facesSize(1),1);
%         variable u;
%         fis = Gstar * v;
%   
%         %Setup semidefinite constraint
%         for i = 1:(facesSize(1))
%             fisIdx = 4*(i-1) + 1;
%             fiLocal = fis(fisIdx:fisIdx+3);
%             exrot = perTriRot(:,:,i);
%             rotFi = kron(eye(2), exrot')*fiLocal;
%             Fi = [rotFi(1) rotFi(3); rotFi(2) rotFi(4)];
%             energyMat = Fi - eye(2);
%             
%            (Fi+Fi')/2 == semidefinite(2);
%         end
%         
%         %setup constraints to convert problem from quadratic to conic
%         Gstar * v(1:(2*num_insideV)) - t == 0;
%         2 * u >= transpose(t) * t;
%         
%         %setup position constraints
%         equalEqualCount = 0;
%         for idx = constraintMap.keys
%             mapValue = constraintMap(idx{1});
%             v(2*idx{1} - 1) == mapValue(1);
%             v(2*idx{1}) == mapValue(2);
%             equalEqualCount = equalEqualCount + 2;
%         end
%         
%          minimize( u + transpose(v(1:(2*num_insideV)))*l );
%     cvx_end
    
    EPSILON = 0.0001;
    
    %Check if any elements inverted
%     for i = 1:facesSize(1)
%         GstarIdx = 4*(i-1) + 1;
%         Gi = Gstar(GstarIdx:GstarIdx+3,:);
%         fi = Gi * v;
%         Fi = [fi(1) fi(3); fi(2) fi(4)];
%         if(det(Fi) < -EPSILON)
%             %disp(strcat('Element corresponding to face: ',num2str(i), ' is inverted, det ', num2str(det(Fi))));
% %            error('Inverted elements detected!');
%         end
%     end

     defV = transpose(reshape(v, 2, restVSize(1)));   
%     h=figure(2); 
%     tsurf(outsideT,defV, frameNum);
%     saveas(h,strcat('air',num2str(frameNum)),'png');
%     h=figure(3);
%     tsurf(insideT,defV, frameNum);
% 
%     for idx = constraintMap.keys %plot constraints as asterisk
%         hold on; 
%         mapValue = constraintMap(idx{1});
%         scatter(mapValue(1), mapValue(2), 100, 'red', '*');
%     end   
%     saveas(h,strcat('skin',num2str(frameNum)),'png');

    %TAeq*v - Tbeq
end

