clear 
clc
exVScale = 1;
bNoninversion = 0;

DataPath = './quad25_180/'; % <-------- update with your YALMIP path
OBJFile = 'quad25t.obj';
nVt = 676;
exVScale = 5;

% DataPath = './quad100_360/'; % <-------- update with your YALMIP path
% OBJFile = 'quad100t.obj';
% nVt = 10201;

addpath(genpath(DataPath));

%load('G.mat');
rawExV = importdata('targetconstraints.txt');
rawExR = importdata('exrotation.txt');
rawOBJ = importdata(OBJFile);

sizeExV = size(rawExV, 1)/3;
sizeExR = size(rawExR, 1)/5;
%n = size(G,2);
n = nVt*2;
m = sizeExR;

VList = rawOBJ.data(1:n/2,1:2);
FList = rawOBJ.data(n/2+1:n/2+sizeExR,:);
tri = FList;
X_source = VList;

G = extractG(VList, FList);

indices = zeros(2*sizeExV,1);
exV = zeros(n, 1);
for i=1:sizeExV
    indices(2*i-1) = 2*(rawExV(3*i-2)+1)-1; %0-based --> 1-based
    indices(2*i) = 2*(rawExV(3*i-2)+1); %0-based --> 1-based
    exV(2*(rawExV(3*i-2)+1)-1) = rawExV(3*i-1);
    exV(2*(rawExV(3*i-2)+1))   = rawExV(3*i);
end
exV = exV / exVScale;

exR = zeros(4*sizeExR,1);
for i=1:sizeExR
    exR(4*i-3) = rawExR(5*i-3);
    exR(4*i-2) = rawExR(5*i-1);
    exR(4*i-1) = rawExR(5*i-2);
    exR(4*i  ) = rawExR(5*i  );
end

nnz = 2*sizeExV;
rowi = zeros(nnz,1);
coli = zeros(nnz,1);
vali = ones(nnz,1);
constraint_rhs = zeros(sizeExV*2,1);
for i=1:sizeExV
    rowi(2*i-1,1) = 2*i-1;
    rowi(2*i,1)   = 2*i;    
    coli(2*i-1,1) = 2*(rawExV(3*i-2)+1)-1; %0-based --> 1-based
    coli(2*i,1)   = 2*(rawExV(3*i-2)+1); %0-based --> 1-based
    constraint_rhs(2*i-1,1) = rawExV(3*i-1);
    constraint_rhs(2*i,1)   = rawExV(3*i);
end
constraint_matrix = sparse(rowi, coli, vali, sizeExV*2, n);

runTTDataYM;

X_target = optiV(:,1:2);





