function varargout = getTrackVelocities(tracks, arg1)
% getTracVelocities   Returns the track velocities and velocity covariances
%   velocities = getTracVelocities(TRACKS, MODEL) returns an MxD matrix of
%   track velocities. M is the number of tracks and D is the dimension of
%   each track velocity.
%   TRACKS is an M-element array of objectTrack objects or structures with
%   equivalent fields. Tracks must have a State  field, specified as an N
%   element vector. If both outputs are used, Tracks must have a
%   StateCovariance field, an NxN matrix.
%   MODEL must be one of the following names:
%       "constvel"  - The constant velocity model as defined in the 
%                     constvel function.
%       "constacc"  - The constant acceleration model as defined in the 
%                     constacc function.
%       "constturn" - The constant turn rate model as defined in the 
%                     constturn function.
%       "singer"    - The Singer acceleration model as defined in the 
%                     singer function.
%
%   When using this syntax, TRACKS must not be empty.
%
%   velocities = getTracVelocities(tracks, velocitySelector) returns an MxD
%   matrix of track velocities, where each row represents the velocity of a
%   track. The velocity selector, velocitySelector, must be a DxN binary
%   matrix, with 1 in the elements that correspond to the velocity elements
%   in the state.
%
%   [..., velocityCovariances] = getTracVelocities(...)   in addition,
%   returns the velocity covariance matrices as a DxDxM array, where 
%   velocityCovariances(:,:,k) is the velocity covariance of the k-th
%   track. To use this syntax, Tracks must have a StateCovariance field,
%   specified as an NxN matrix.
%
%   An example of a selector matrix: if the state is [x;vx;y;vy], which
%   represents a 2D constant velocity motion. N=4, D=2 and:
%   velocitySelector = [0 1 0 0;0 0 0 1];
%
%   Example: Extract velocity From a 2D Constant Velocity Track
%   -----------------------------------------------------------
%   tracks = struct('TrackID', 1, 'UpdateTime', 2.1, 'State', [10;1;40;-1], ...
%		'StateCovariance', diag([1,3,1,3]), 'IsConfirmed', true, ...
%		'IsCoasted', false, 'ObjectClassID', 0, 'ObjectAttributes', struct)
% 
%   % Get the velocity vector and velocity covariance using a model name
%   [velocity, velocityCovariance] = getTrackVelocities(tracks, 'constvel')
%
%   % Get the velocity vector and velocity covariance using a selector
%   velocitySelector = [0 1 0 0; 0 0 0 1]
%   [velocity, velocityCovariance] = getTrackVelocities(tracks, velocitySelector)
%
%   See also: getTrackPositions

% Copyright 2016-2022 The MathWorks, Inc.

%#codegen

arguments
    tracks {matlabshared.tracking.internal.fusion.mustBeTracks}
    arg1
end

nargoutchk(0,2);

if isnumeric(arg1)
    [varargout{1:nargout}] = matlabshared.tracking.internal.fusion.getTrackPosVel(tracks, arg1, mfilename, 'velocitySelector');
else
    [varargout{1:nargout}] = matlabshared.tracking.internal.fusion.trackPosVelModel(tracks, arg1, mfilename, false);
end