Skip to content

Commit

Permalink
scape
Browse files Browse the repository at this point in the history
  • Loading branch information
chen1474147 committed Oct 1, 2016
1 parent 34389ae commit dc6fdb3
Show file tree
Hide file tree
Showing 98 changed files with 300,796 additions and 0 deletions.
20 changes: 20 additions & 0 deletions prepare/mesh/data/tpose.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
0.001497 0.013568 0.152925
-0.001799 -0.040141 -0.171951
-0.003282 -0.027311 -0.366690
0.000990 -0.025286 0.391908
-0.097668 -0.025024 -0.399642
-0.104710 -0.000172 -0.865403
-0.113675 0.003586 -1.284691
-0.133968 -0.071822 -1.335158
0.090609 -0.025320 -0.398652
0.099851 -0.000438 -0.863821
0.110594 0.003457 -1.279811
0.131251 -0.080692 -1.332369
-0.162144 0.032131 0.093507
-0.401790 0.025970 0.116005
-0.615922 -0.072595 0.121641
-0.686757 -0.123359 0.110817
0.163958 0.032271 0.098162
0.404975 0.025376 0.122067
0.619772 -0.073468 0.126537
0.688443 -0.122522 0.115695
76 changes: 76 additions & 0 deletions prepare/mesh/demo_convertmesh.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@


clear
clc

%%
% prepare data

addpath('io/');
addpath('skel/');
addpath('quatern/');

% male
scapepath = '../scape/MATLAB_daz_m_srf';
addpath(genpath(scapepath));


%%
% predifined data

Meta.instance.readA;
Meta.instance.readPCA;

% mesh triangles
triangles = Meta.instance.triangles;

% ponints weights
weights = Meta.instance.weight;
[weights_sort, ind] = sort(weights, 2);


%%
% RR to points

RR = repmat(eye(3), 1, 1, 15);

shapepara = Meta.instance.sem_default;

tic;
points = Body(RR, shapepara).points;
toc;

tic;
generate_obj(points, triangles, 'data/tpose.obj');
toc;


%%

% points2skel

tic
[ skel ] = points2skel( points, weights_sort, ind );
toc

generate_skel(skel, 'data/tpose.txt');


%%
% skel2RR

skel_scape = skel;

% wangchuyu
ind_wangchuyu = [4, 1, 17, 18, 19, 13, 14, 15, 3, 9, 10, 11, 5, 6, 7];
skel_wangchuyu = skel_scape(ind_wangchuyu, :);

theta = -85/180*3.1416;
rot_x = [1 0 0; 0 cos(theta), -sin(theta); 0 sin(theta), cos(theta)];
skel_wangchuyu = rot_x*skel_wangchuyu';
skel_wangchuyu = skel_wangchuyu';

tic;
[ RR, R ] = skel2RR( skel_wangchuyu, skel_scape );
toc;

22 changes: 22 additions & 0 deletions prepare/mesh/io/generate_obj.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function generate_obj(points, triangles, file)
% points is 3d points, 6449x3
% triangles is mesh faces, 12894x3
% file is saved file name

pnum = size(points, 1);
fnum = size(triangles, 1);

fid = fopen(file, 'w');

for i = 1 : pnum
fprintf(fid, 'v %f %f %f\n', points(i, :));
end;

for i = 1 : fnum
fprintf(fid, 'f %d %d %d\n', triangles(i, :));
end;

fclose(fid);

end

16 changes: 16 additions & 0 deletions prepare/mesh/io/generate_skel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function generate_skel( skel, file )
% skel is 3d skeletons, 15x3 or 20x3
% file is saved file

snum = size(skel, 1);

fid = fopen(file, 'w');

for i = 1:snum
fprintf(fid, '%f %f %f\n', skel(i, :));
end;

fclose(fid);

end

17 changes: 17 additions & 0 deletions prepare/mesh/quatern/angle2q.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function [ q ] = angle2q( axis, angle )
% axis is rotating axis, (x, y ,z)'
% angle is rotating degree, in PI
% q is (x, y, z, w)'

axis = axis/sqrt(axis'*axis);

angle = angle/2;

q = zeros(4, 1);

q(4, 1) = cos(angle);

q(1:3, 1) = sin(angle)*axis;

end

94 changes: 94 additions & 0 deletions prepare/mesh/quatern/demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@


% test functions

clear
clc

%%
% rotation axis and angle

vec = rand(3, 1);
theta = pi * rand();

q = angle2q(vec, theta);

R = q2matrix(q);

q2 = matrix2quaternion(R);

disp(q - q2);

% q = q2

%%
% two vecs

vec1 = rand(3, 1);

vec2 = R*vec1;

R2 = vector2matrix(vec1, vec2);

disp(R);
disp(R2);

disp(vec2 - R*vec1);
disp(vec2 - R2*vec1);

% it is interesting.
% R != R2
% vec2 = R*vec1 = R2*vec1

%%
% q1mulq2

clear
clc

vec = [1 2 3]';

vec1 = [2 3 4]';
theta1 = 30/180*pi;
q1 = angle2q(vec1, theta1);
R1 = q2matrix(q1);
vecr = R1*vec;

vec2 = [7 1 -3]';
theta2 = 50/180*pi;
q2 = angle2q(vec2, theta2);
R2 = q2matrix(q2);
vecr = R2*vecr;


q3 = q1mulq2(q1, q2);
R3 = q2matrix(q3);
vecrr = R3*vec;

disp(vecr - vecrr);
disp(R2*R1 - R3);

%%
% points
% p2 = q*p*qinv

clear
clc

% q & qinv
q = rand(4, 1);
q = q/sqrt(q'*q);
matrix = q2matrix(q);

qinv = [-q(1:3); q(4)];

% points
p = rand(3, 1);
p4 = [p; 0];

% cal
pr = matrix * p;

prr = q1mulq2(q1mulq2(qinv, p4), q);

disp(pr - prr(1:3));
61 changes: 61 additions & 0 deletions prepare/mesh/quatern/matrix2quaternion.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
% MATRIX2QUATERNION - Homogeneous matrix to quaternion
%
% Converts 4x4 homogeneous rotation matrix to quaternion
%
% Usage: Q = matrix2quaternion(T)
%
% Argument: T - 4x4 Homogeneous transformation matrix
% Returns: Q - a quaternion in the form [w, xi, yj, zk]
%
% See Also QUATERNION2MATRIX

% Copyright (c) 2008 Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk at csse uwa edu au
% http://www.csse.uwa.edu.au/
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.

function Q = matrix2quaternion(T)

% This code follows the implementation suggested by Hartley and Zisserman
R = T(1:3, 1:3); % Extract rotation part of T

% Find rotation axis as the eigenvector having unit eigenvalue
% Solve (R-I)v = 0;
[v,d] = eig(R-eye(3));

% The following code assumes the eigenvalues returned are not necessarily
% sorted by size. This may be overcautious on my part.
d = diag(abs(d)); % Extract eigenvalues
[s, ind] = sort(d); % Find index of smallest one
if d(ind(1)) > 0.001 % Hopefully it is close to 0
warning('Rotation matrix is dubious');
end

axis = v(:,ind(1)); % Extract appropriate eigenvector

if abs(norm(axis) - 1) > .0001 % Debug
warning('non unit rotation axis');
end

% Now determine the rotation angle
twocostheta = trace(R)-1;
twosinthetav = [R(3,2)-R(2,3), R(1,3)-R(3,1), R(2,1)-R(1,2)]';
twosintheta = axis'*twosinthetav;

theta = atan2(twosintheta, twocostheta);

% Q = [cos(theta/2); axis*sin(theta/2)];
Q = [axis*sin(theta/2); cos(theta/2)];

end

28 changes: 28 additions & 0 deletions prepare/mesh/quatern/q1mulq2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function [ q ] = q1mulq2( q1, q2 )
% q is (x, y, z, w)'
% q = q1*q2
% the rotate matrix will first rotate q1
% then rotate q2

w1 = q1(4, 1);
w2 = q2(4, 1);

x1 = q1(1, 1);
y1 = q1(2, 1);
z1 = q1(3, 1);

x2 = q2(1, 1);
y2 = q2(2, 1);
z2 = q2(3, 1);

w = w1*w2 - x1*x2 - y1*y2 - z1*z2;
x = w1*x2 + x1*w2 + z1*y2 - y1*z2;
y = w1*y2 + y1*w2 + x1*z2 - z1*x2;
z = w1*z2 + z1*w2 + y1*x2 - x1*y2;

q = [x, y, z, w]';

% q = q/sqrt(q'*q);

end

32 changes: 32 additions & 0 deletions prepare/mesh/quatern/q2matrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function [ matrix ] = q2matrix( q )
% q is (x, y, z, w)'
% matrix is V2 = R*V1

q = q/sqrt(q'*q);

w = q(4, 1);

x = q(1, 1);
y = q(2, 1);
z = q(3, 1);

r=zeros(3, 3);

r(1,1) = 1-2*y*y-2*z*z;
r(1,2) = 2*x*y+2*w*z;
r(1,3) = 2*x*z-2*w*y;

r(2,1) = 2*x*y-2*w*z;
r(2,2) = 1-2*x*x-2*z*z;
r(2,3) = 2*z*y+2*w*x;

r(3,1) = 2*x*z+2*w*y;
r(3,2) = 2*y*z-2*w*x;
r(3,3) = 1-2*x*x-2*y*y;

r = r';

matrix = r;

end

18 changes: 18 additions & 0 deletions prepare/mesh/quatern/vector2matrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function [ matrix ] = vector2matrix( vec1, vec2 )
% vec is (x, y, z)'
% vec2 = matrix*vec1

v1 = vec1/sqrt(vec1'*vec1);
v2 = vec2/sqrt(vec2'*vec2);

angle = acos(v1'*v2);

v3 = cross(v1, v2);
v3 = v3/sqrt(v3'*v3);

q = angle2q(v3, angle);

matrix = q2matrix(q);

end

Loading

0 comments on commit dc6fdb3

Please sign in to comment.