Skip to content

Commit

Permalink
add matlab code
Browse files Browse the repository at this point in the history
  • Loading branch information
jojolebarjos committed Jul 12, 2016
0 parents commit 5ab9e78
Show file tree
Hide file tree
Showing 1,871 changed files with 848,821 additions and 0 deletions.
1 change: 1 addition & 0 deletions matlab/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.asv
85 changes: 85 additions & 0 deletions matlab/bone_axes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
function axes = bone_axes(centers)
%BONE_AXES Compute local axes (local to world transform matrix) for each bone
%
% Inputs
% centers: 30 by 1 array of spheres or 30 by 3 double
%
% Outpus
% axes: 18 by 1 array of 4 by 4 double
%

% Extract sphere centers if needed
if iscell(centers)
tmp = zeros(30, 3);
for i = 1 : 30
tmp(i, :) = centers{i}.center;
end
centers = tmp;
end

% Compute axes for each bone
axes = cell(1, 18);

% Palm
root = centers(26, :) * 0.5 + centers(27, :) * 0.5;
axes{1} = build(root, centers(22, :) - root, centers(25, :) - centers(24, :));
axes{2} = build(centers(22, :), centers(12, :) - centers(22, :), centers(20, :) - centers(23, :));
proto = centers(8, :) * 0.5 + centers(23, :) * 0.5;
axes{15} = build(proto, centers(4, :) - proto, centers(8, :) - centers(4, :));

% Thumb
% TODO is thumb normal correct?
n_thumb = -finger_normal(centers(20, :), centers(19, :), centers(17, :), centers(16, :));
axes{3} = build(centers(20, :), centers(19, :) - centers(20, :), n_thumb);
axes{4} = build(centers(19, :), centers(18, :) - centers(19, :), n_thumb);
axes{5} = build(centers(18, :), centers(17, :) - centers(18, :), n_thumb);

% Index
n_index = -finger_normal(centers(16, :), centers(15, :), centers(13, :), centers(12, :));
axes{6} = build(centers(16, :), centers(15, :) - centers(16, :), n_index);
axes{7} = build(centers(15, :), centers(14, :) - centers(15, :), n_index);
axes{8} = build(centers(14, :), centers(13, :) - centers(14, :), n_index);

% Middle
n_middle = -finger_normal(centers(12, :), centers(11, :), centers(9, :), centers(8, :));
axes{9} = build(centers(12, :), centers(11, :) - centers(12, :), n_middle);
axes{10} = build(centers(11, :), centers(10, :) - centers(11, :), n_middle);
axes{11} = build(centers(10, :), centers(9, :) - centers(10, :), n_middle);

% Ring
n_ring = -finger_normal(centers(8, :), centers(7, :), centers(5, :), centers(4, :));
axes{12} = build(centers(8, :), centers(7, :) - centers(8, :), n_ring);
axes{13} = build(centers(7, :), centers(6, :) - centers(7, :), n_ring);
axes{14} = build(centers(6, :), centers(5, :) - centers(6, :), n_ring);

% Pinky
n_pinky = finger_normal(centers(4, :), centers(1, :), centers(3, :), centers(8, :));
axes{16} = build(centers(4, :), centers(3, :) - centers(4, :), n_pinky);
axes{17} = build(centers(3, :), centers(2, :) - centers(3, :), n_pinky);
axes{18} = build(centers(2, :), centers(1, :) - centers(2, :), n_pinky);
end

function normal = finger_normal(u, v, w, x)
uv = normalizerow(v - u);
uw = normalizerow(w - u);
ux = normalizerow(x - u);
n = cross(uw, uv);
l = norm(n);
if l < 0.001
n = ux;
else
n = n / l;
end
if dot(ux, n) < 0
n = -n;
end
normal = l * n + (1 - l) * ux;
end

function axe = build(o, x, y)
x = normalizerow(x);
y = normalizerow(y);
z = normalizerow(cross(x, y));
y = cross(z, x);
axe = [x', y', z', o'; 0, 0, 0, 1];
end
23 changes: 23 additions & 0 deletions matlab/bone_local_transforms.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function locals = bone_local_transforms(mesh, globals)
%BONE_LOCAL_TRANSFORMS Compute local transforms for each bone
%
% Inputs
% mesh: complete mesh object
% globals: 18 by 1 array of 4 by 4 double returned by bone_axes
%
% Outputs
% locals: 18 by 1 array of 4 by 4 double
%

locals = cell(size(globals));
for i = 1 : length(globals)
p = mesh.bones{i}.parent;
if p > 0
locals{i} = inv(globals{p}) * globals{i};
else
locals{i} = globals{i};
end
end

end

74 changes: 74 additions & 0 deletions matlab/bone_transforms.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function transforms = bone_transforms(mesh, centers, preserve_distances)
%BONE_TRANSFORMS Compute global transforms for each bone
%
% Inputs
% mesh: complete mesh object
% centers: 30 by 1 array of spheres or 30 by 3 double
% preserve_distances: logical or double (default true)
%
% Outputs
% transforms: 18 by 1 array of 4 by 4 double
%

% Define missing arguments
if nargin < 3
preserve_distances = true;
end
if strcmp(preserve_distances, 'optimize')
% TODO use optimization to compute ideal transforms
return;
end
if islogical(preserve_distances)
preserve_distances = double(preserve_distances);
end
preserve_distances = clamp(preserve_distances, 0, 1);

% Compute original and new axes
A = bone_axes(mesh.spheres);
B = bone_axes(centers);

if preserve_distances > 0
% TODO need to test this distance preservation

% Compute delta required to preserve distances
deltas = cell(1, length(mesh.bones));
for i = 1 : length(mesh.bones)
parent = mesh.bones{i}.parent;
if parent <= 0
deltas{i} = [0; 0; 0];
else
distance = norm(A{i}(1 : 3, 4) - A{parent}(1 : 3, 4));
current = B{i}(1 : 3, 4) - B{parent}(1 : 3, 4);
expected = current * (distance / sqrt(current' * current));
deltas{i} = expected - current;
end
end

% Propagate delta to children
done = false(1, length(mesh.bones));
while ~all(done)
for i = 1 : length(mesh.bones)
parent = mesh.bones{i}.parent;
if parent <= 0
done(i) = true;
elseif done(parent)
deltas{i} = deltas{i} + deltas{parent};
done(i) = true;
end
end
end

% Apply delta
for i = 1 : length(mesh.bones)
B{i}(1 : 3, 4) = B{i}(1 : 3, 4) + preserve_distances * deltas{i};
end

end

% Compute transforms
transforms = cell(1, length(mesh.bones));
for i = 1 : length(mesh.bones)
transforms{i} = B{i} * inv(A{i});
end

end
9 changes: 9 additions & 0 deletions matlab/external/gptoolbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# use glob syntax.
syntax: glob
*.o
*.*.swp
*.a
*~
.DS_Store
*.mexmaci64
*.cache.mat
14 changes: 14 additions & 0 deletions matlab/external/gptoolbox/.mailmap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# This list is used by git-shortlog to fix a few botched name translations
# in the libigl archive, either because the author's full name was messed up
# and/or not always written the same way, making contributions from the
# same person appearing not to be so.
#
Alec Jacobson <[email protected]> Alec Jacobson (jalec <Alec Jacobson ([email protected])>
Alec Jacobson <[email protected]> jalec <devnull@localhost>
Alec Jacobson <[email protected]> Alec Jacobson <[email protected]>
Alec Jacobson <[email protected]> ajx <devnull@localhost>
Alec Jacobson <[email protected]> mangledorf <[email protected]>
Daniele Panozzo <[email protected]> Daniele Panozzo <[email protected]>
Daniele Panozzo <[email protected]> dpanozzo <devnull@localhost>
Olga Diamanti <[email protected]> dolga <devnull@localhost>
119 changes: 119 additions & 0 deletions matlab/external/gptoolbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
gptoolbox - Geometry Processing Toolbox
=======================================

<https://github.com/alecjacobson/gptoolbox/>

This is a toolbox of useful matlab functions for geometry processing. There are
also tools related to constrainted optimization and image processing. Typically
these are utility functions that are not stand alone applications.

Here's an incomplete list of cool features this matlab toolbox contains:

- wrappers for TetGen, Triangle, QSlim, meshfix
- mesh smoothing
- mesh clean up (remove duplicates, remove unreferenced)
- geodesic distances on triangle and tetrahedral meshes
- mesh quantities and queries (normals, discrete gaussian curvature, list
boundary edges, topology, angles, dihedral angles etc.)
- mesh deformation (as-rigid-as-possible (ARAP), moving least-squares, etc.)
- mesh parameterization (harmonic, least squares conformal, ARAP, etc.)
- automatic skinning weight computation (bounded biharmonic weights, bone heat)
- 2D triangle mesh from binary image
- Input/Output for many mesh formats
(.obj,.off,.stl,.wrl,.ply,.mesh,.node,.ele,.poly,.smf,.bdl,.face)
- discrete differential geometry operators for triangle and tetrahedral meshes
(cotangent Laplacian, gradient, divergence)
- quadratic programming, active set solver
- scribble-based image colorization, diffusion curves
- exact (un)signed distance field computation for meshes
- constructive solid geometry operations on meshes, booleans
- accelerated point location in triangle and tetrahedral meshes
- image dithering
- deep matlab function dependency

The functions have been organized into folders based on their primary
application:

- external/
- imageprocessing/
- images/
- matrix/
- mesh/
- mex/
- utility/
- wrappers/

## Installation ##
The vast majority of this code is __straight MATLAB__ (`*.m` files). Thus, only
installing MATLAB and adding the qptoolbox directory and its subdirectories to
your MATLAB path is needed for installation:

addpath(genpath('/absolute/path/to/gptoolbox'))

To make this change permanent, then issue:

savepath

There are some mex files, whose documentation for installation are included in
respective `mex/README.md` file.

To enable tab completion on gptoolbox's IO functions issue:

add_gptoolbox_tab_completion

this takes a second or two (or 30) and then you'll need to restart MATLAB for
it to take effect.

### Full installation ###

This strives to be full installation instructions, but will no doubt remain
incomplete for some time. Begin by adding paths as above.

As stated above, most files are straight matlab and will _just run_ if you have
gptoolbox in your path.

#### Compile `/mex` ####
Most of our mex files will depend on
[libigl](https://github.com/libigl/libigl). The following will assume your
usign a "standard" unix-y install of libigl as a static library.

In MATLAB issue:

cd mex
compile_qptoolbox_mex

#### Compile `toolbox_fast_marching` ####

In MATLAB issue:

cd external/toolbox_fast_marching/
compile_mex

## Dependencies ##
This depends on MATLAB and its various toolbox extensions. Many functions
should also work with Octave, though this has not been tested.

Functions that rely on `quadprog` have been tested and optimized assuming that
the Mosek toolbox has been installed, but should also work with the `quadprog`
in MATLAB's Optimization Toolbox.

Mex files may have other external dependencies (e.g. CGAL, Eigen, libigl). See
their respective READMEs for more information.

## License ##
Unless marked otherwise, all code is Copyright Alec Jacobson 2014.

We will probably switch to a MPL2 license in the near future.

## Contact ##
The Geometry Processing Toolbox grew out of Alec Jacobson's private codebase
during his PhD, but has benefited a lot from various collaborators at NYU and
ETH Zurich. Now, the Geometry Processing Toolbox is a group endeavour. If
you're intersted in contributing, please contact Alec Jacobson
([email protected]) or submit a pull request on github.

## Documentation ##
For now, documentation is limited to a per-function basis. For example, to find
documentation for `cotmatrix` issue:

help cotmatrix
2 changes: 2 additions & 0 deletions matlab/external/gptoolbox/RELEASE_HISTORY.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.1.1 Cleaned up organization, removed some useless functions, added external
fast marching geodesic distance dependency.
3 changes: 3 additions & 0 deletions matlab/external/gptoolbox/TODO.todo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# New Entries up here
- merge normalsvertex, per_vertex_normals and have optional for angle vs. area
vs. uniform weighting
12 changes: 12 additions & 0 deletions matlab/external/gptoolbox/external/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This directory contains matlab functions written by others, which may be under
their own licenses.

To reduce clutter, licenses are listed in the comments beneath single file
programs.

Filenames should be original. If prototypes or filenames are strange, write a
wrapper and place in ../matrix, ../mesh, etc. where appropriate.

Some files are patched to work correctly or have more features, so careful when
updating: use diff.

Loading

0 comments on commit 5ab9e78

Please sign in to comment.