function [T] = fiducial_coregister(P,Q)
% fiducial_coregister - Determine fiducial points coregistration
% [T] = fiducial_coregister(P,Q)
% P is [3x3], three points in Cartesian XYZ
% Q is [3x3], three points in Cartesian XYZ
% This function 3D rotates and translates P into
% the space of Q.
The method is described in
% this m file, developed from Mortenson, M. (1985),
% pp. 353-355.
% The return 4x4 matrix (T) contains a 3x3 rotation
% matrix in the upper left and a 1x3 translation
% row vector in the bottom row. The last column contains
% a projection vector in T(1:3,4)(all zeros here) and
% the scalar at T(4,4) is a homogeneous coordinate scale
% factor, usually 1.
% Using this T matrix:
% to get P values in the space of Q,
% P2Q = [P,ones(size(P,1),1)] * T;
% to get Q values in the space of P,
% Q2P = [Q,ones(size(Q,1),1)] * inv(T);
% In this manner, the rotation and translation is
% applied to any Nx3 points in relation to P space.
% For use in coregistration of electrodes to a scalp
% P(1,:) is the electrode nasion XYZ
% P(2,:) is the electrode right preauricular XYZ
% P(3,:) is the electrode left preauricular XYZ
% Q(1,:) is the scalp mesh nasion XYZ
% Q(2,:) is the scalp mesh right preauricular XYZ