% need to include the dataset_positions thing here.  All of that stuff.

ParametersRun500
dataset_positions = [{2,0}; {2,1}; {2,2}; {2,3}; {4,0}; {4,2}; {4,3}; {4,4}; {4,5}; {5,0}; {5,1}; {5,2}; {5,3}; {6,0}; {6,1}; {6,2}; {6,3}; {6,4}; {6,5}; {7,0}; {7,1}; {7,2}; {7,3}; {7,4}; {7,5}; {10,0}; {10,1}; {10,2}; {10,3}; {10,5}];



all_rotations = zeros(1,size(dataset_positions,1));
all_rotations_acos = zeros(1,size(dataset_positions,1));

moving_run_number = 500;


for i = 1:size(dataset_positions,1)

    dataset = dataset_positions{i,1};
    position = dataset_positions{i,2};
    first_stack = calculated_stacks{dataset,position+1}(1);
    last_stack = calculated_stacks{dataset,position+1}(2);
    % this might need to be replaced with calculated stac
    
    moving_start_time = moving_start_times{dataset,position+1};
    moving_moments_time = moments_times{dataset,position+1};
    
    % stacks are the time point bounds held in calculated_params).
    
    if (dataset~= 2)&&(dataset~=4)
        moving_dataset_directory =  ['L:\seg2 5-31\Set ' num2str(dataset) '\Stacks\Position ' num2str(position) '\resized images'];
    else
        moving_dataset_directory =  ['K:\Laptop F drive copy\new reg 4-8 classifier 6-13-15\' num2str(dataset) '-' num2str(position)];
    end
    
    cd(moving_dataset_directory)
    cd(['Run ' num2str(moving_run_number)]);
    load(['moving transform matrix_' num2str(dataset) '_' num2str(position) '_r' num2str(moving_run_number) '. ' moving_moments_time '.mat'])
    
    cd('SelfTransforms');
    load(['composed cpd conjugate_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(last_stack) ' al Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(first_stack) '_r' num2str(moving_run_number) '. ' moving_start_time '.mat']);
    rotation = composed_cpd_conjugate.';
    rotation = rotation(2:3,2:3);
    rotation_angle = -asin(rotation(2,1));
    rotation_angle_acos = acos(rotation(1,1));
    all_rotations(i) = rotation_angle;
    all_rotations_acos(i) = rotation_angle_acos;
    transform_matrices{i} = transform_matrix;
    external_vector = squeeze(transform_matrix(1,1:3));
    external_projections(i) = external_vector(3);
    return_matrix = inv(transform_matrix.');
    return_matrix = return_matrix(1:3,1:3);
    internal_vector = [0 0 1]*return_matrix;
    external_vectors{i} = external_vector;
    internal_vectors{i} = internal_vector;
    internal_projections(i) = internal_vector(3);
end

all_rotations_corrected = all_rotations;

for i = 1:length(all_rotations)
    if all_rotations_acos(i)>pi/2
        if all_rotations_corrected(i) > 0
            all_rotations_corrected(i) = pi-all_rotations(i);
        else
            all_rotations_corrected(i) = -pi - all_rotations(i);
        end
    end
end
            

x = 1:length(all_rotations_corrected);

figure; scatter(x,all_rotations_corrected*180/pi);

fprintf(['n = ' num2str(length(all_rotations_corrected))])

corrcoef(internal_projections,all_rotations_corrected)
corrcoef(external_projections,all_rotations_corrected)
