function seg2_out = seg2_shift( seg2_in, shift_sec, total_duration )

% FUNCTION SEG2_OUT = SEG2_SHIFT( SEG2_IN, SHIFT_SEC, TOTAL_DURATION )
%
% Circular shift of a time segmentation.
  
  if nargin < 3
    error( [ mfilename ' needs 3 input arguments.' ] );
  end
  
  if size( seg2_in, 1 ) ~= 2
    error( [ mfilename ': "seg2_in" must be a 2-rows matrix.' ] );
  end
  
  tmp = seg2_in + shift_sec;
  
  tmp1 =  total_duration + seg2_intersection( { tmp, [ -total_duration; 0 ] } );
  tmp2 =  seg2_intersection( { tmp, [ 0; total_duration ] } );
  tmp3 = -total_duration + seg2_intersection( { tmp, [ total_duration; 2*total_duration ] } );

  seg2_out = seg2_union( { tmp1, tmp2, tmp3 } );
  