Skip to content

Instantly share code, notes, and snippets.

@simonlynen
Created April 9, 2013 20:36
Show Gist options
  • Save simonlynen/5349171 to your computer and use it in GitHub Desktop.
Save simonlynen/5349171 to your computer and use it in GitHub Desktop.
quaternion average of a power two length vector of quaternions
function [ averagequat ] = average_quats( diffquats )
%AVERAGE_QUATS Average a series of quats, needs to be a two power length
%vector
% using slrp
range = size(diffquats);
if(range(1) ~= 4)
error 'quaternions must be 4 row vectors'
end
range = range(2);
%check num quats is power of 2
if(mod(log2(numel(range)),1) > 10 * eps)
error 'number of quaternions to average must be a power of 2'
end
values = diffquats;
while(1)
nelem = size(values);
nelem = nelem(2);
if(nelem == 1)
break;
end
idx = 1;
results = zeros(4, nelem / 2);
for i = 1:2:nelem
results(:,idx) = slerp(values(:,i), values(:,i + 1), 0.5);
idx = idx + 1;
end
values = results;
end
averagequat = values;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment