function [ output_args ] = compute_segments(config, file_info)
% Superpixel file and directory paths
paths.seg_data = fullfile(config.paths.seg_feats, 'seg_data');
paths.feat_path = config.paths.img_feats;

% Make a directory if it doesn't exist
if ~exist(paths.seg_data, 'dir')
    mkdir(paths.seg_data);
end

parfor file_i = 1:file_info.num_files
    segment_rgbd_data(file_info, file_i, paths, config);
end

end

%% Parfor Friendly Helper Functions
function [] = segment_rgbd_data(file_info, file_i, paths, config)
% File name root and path
[parent_dirs, name_root] = get_file_info_entry(file_info, file_i);
paths.supix_path = fullfile(paths.seg_data, [name_root '.mat']);

% Compute superpixels if it doesn't already exist.
if ~exist(paths.supix_path, 'file')
    % segment_raw_color_depth(paths, parent_dirs, name_root, config);
    segment_clean_depth_normals(paths, parent_dirs, name_root, config);
end
end

function [] = segment_raw_color_depth(paths, parent_dirs, name_root, config)
% Compile paths to raw rgb and depth features
path_rgb = fullfile(parent_dirs{:}, ...
                    sprintf(config.data_info.rgb_format, name_root));
path_depth = fullfile(parent_dirs{:}, ...
                      sprintf(config.data_info.depth_format, name_root));

% Load features for superpixel segmentation
color_img = load_raw_image(path_rgb, config.load_params);
depth_img = load_raw_depth(path_depth, config.load_params);
cloud_img = depth_img_to_xyz(depth_img, config.load_params);

% Compute superpixels
[label_img, centers] = SLIC_Color_3D(color_img, cloud_img, config.supix_params);
save(paths.supix_path, 'label_img', 'centers');
end

function [] = segment_clean_depth_normals(paths, parent_dirs, name_root, config)
% Compile paths to features.
path_rgb = fullfile(parent_dirs{:}, ...
                    sprintf(config.data_info.rgb_format, name_root));
path_depth = fullfile(paths.feat_path, 'depth', name_root);
path_normals = fullfile(paths.feat_path, 'normals', name_root);

% Load features for superpixel segmentation.
load(path_depth, 'depth');
load(path_normals, 'normals');
color_img = load_raw_image(path_rgb, config.load_params);
cloud_img = depth_img_to_xyz(depth_img, config.load_params);

% Compute superpixels
[label_img, centers] = SLIC_Color_3D_Normals(color_img, cloud_img, config.supix_params);
save(paths.supix_path, 'label_img', 'centers');
end
