function bnd = moveBndToCenterOfMass(bnd)
% correct boundary so that the origin is the center of mass
%
% this operation assumes that the origin is in the polytope (no negative
%   point distances).

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

% the center of each circle section:
circleSectionCenterMatrix = 0.5*computeNormalFromAngle(bnd.polarAngle).*...
    [bnd.polarDistance;bnd.polarDistance];
% the area of each circle section:
circleSectionAreaVector = (bnd.polarDistance.^2.*bnd.polarBinSize)/2;
% center weighted by section area, summed up and divided by total area:
massCenter = sum(circleSectionCenterMatrix .* ...
    [circleSectionAreaVector; circleSectionAreaVector], 2) / ...
    sum(circleSectionAreaVector);
% massCenter = massCenter + [5;0];

% correct bnd
newCart = bsxfun(@plus, [bnd.cartX; bnd.cartY], -1*massCenter);
bnd.cartX = newCart(1,:);
bnd.cartY = newCart(2,:);
bnd.centerPointOriginal = bnd.centerPointOriginal - 1*massCenter;
bnd.centerPointMean = -1*massCenter;
% create polar data
[bnd.polarAngle bnd.polarDistance] = convertCartToPolar(...
    bnd.cartX, bnd.cartY);
% sort data
[bnd.polarAngle bnd.polarDistance, permutationVector] = ...
    standardizePolar(bnd.polarAngle, bnd.polarDistance);
bnd.cartX = bnd.cartX(permutationVector);
bnd.cartY = bnd.cartY(permutationVector);

