classdef CommonUtilities
    %   This class is for internal use only. It may be removed in the future.
    
    %   This is a static class that provides common utilities used within
    %   the sensor and emitter models.
    
    %   Copyright 2018 The MathWorks, Inc.
    
    %#codegen
    
    methods (Static)
        function val = getIndexed(x,idx)
            % Retrieves the indexed value from the array or cell array x.
            
            if iscell(x)
                val = x{idx};
            else
                val = x(idx);
            end
        end
        
        function x = setIndexed(x,idx,val)
            % Sets the indexed value of the array or cell array x to val.
            
            if iscell(x)
                x{idx} = val;
            else
                x(idx) = val;
            end
        end
        
        function flag = isIndexedEmpty(x)
            % Returns true if x is an empty array, or x is an empty cell
            % array or a cell array containing all empty elements.
            
            flag = isempty(x);
            if ~flag && iscell(x)
                for m = 1:numel(x)
                    if isempty(x{m})
                        flag = false;
                        break
                    end
                end
            end
        end
        
        function [rot, wasQuat] = getRotmat(orient)
            % Returns orientation in frame rotation matrix format. If
            % orientation was in quaternion format, then wasQuat is true.
            
            wasQuat = isa(orient,'quaternion');
            if wasQuat
                rot = rotmat(orient, 'frame');
            else
                rot = orient;
            end
        end
        
        function flag = allValidTypes(things, validTypesIn)
            % Returns true if all of the things in the cell array are of the types
            % defined by validTypes.
            
            validTypes = coder.const(fusion.internal.remotesensors.CommonUtilities.mkcell(validTypesIn));
            
            flag = true;
            for k = 1:numel(things)
                this = fusion.internal.remotesensors.CommonUtilities.getIndexed(things,k);
                
                % Check if this thing is valid for any one of the valid
                % types.
                isThisValid = false;
                for m = 1:numel(validTypes)
                    thisType = validTypes{m};
                    
                    % Once a valid type is found, we are done for this
                    % "thing".
                    if isa(this, thisType)
                        isThisValid = true;
                        break
                    end
                end
                
                % If any one is not valid, then they are not all valid.
                if ~isThisValid
                    flag = false;
                    break
                end
            end
        end
        
        function y = mkcell(x)
            % Returns x as a cell array. If x is already a cell array, x is
            % returned, otherwise {x} is returned.
            
            if iscell(x)
                y = x;
            else
                y = {x};
            end
        end
    end
end
