classdef coverageBuiltins
    %This class is for internal use only. It may be removed in the future.

    %COVERAGEBUILTINS Interface to builtins used for Coverage Path
    %Optimization.
    %
    %   This class is a collection of static functions. Its main purpose is to dispatch
    %   function calls correctly when executed in MATLAB or code
    %   generation.
    %   During MATLAB execution, we call the standard builtins. During code
    %   generation we will use a codegen-compatible version in coverageBuildable.

    % Copyright 2022 The MathWorks, Inc.

    %#codegen

    methods (Static)

        function pathSolnObj = connectExhaustivePath(VisitOrderSpecified,SpecifiedSequence,MinAdjacencyCount,NumPolygons,AdjacencyInfo,startPoses,endPoses,Takeoff,Landing)
            %connectExhaustivePath call underlying C API method to connect
            %using Exhaustive Path Traversal.

            if coder.target('MATLAB')
                % Call standard builtin in MATLAB

                mcosObj =uav.algs.internal.builtin.CoveragePathOptimization();
                pathInternalObj=mcosObj.connectExhaustivePath(VisitOrderSpecified,int32(SpecifiedSequence),uint32(MinAdjacencyCount),NumPolygons,int32(AdjacencyInfo),startPoses,endPoses,Takeoff,Landing);
                %Send back out a MATLAB friendly format file that holds all
                %the same info.
                pathSolnObj=uav.internal.coveragePlanner.coverageBuiltins.getStructParameters(pathInternalObj.VisitingSequence,pathInternalObj.SweepSequence,pathInternalObj.TransitionCost,pathInternalObj.TransitionCost);
            else
                buildableObj = uav.internal.coder.coverageBuildable();
                [VisitingSequence,SweepSequence,TransitionCost]=buildableObj.planExhaustivePath(VisitOrderSpecified,SpecifiedSequence,uint32(MinAdjacencyCount),NumPolygons,AdjacencyInfo,startPoses,endPoses,Takeoff,Landing);
                pathSolnObj=uav.internal.coveragePlanner.coverageBuiltins.getStructParameters(VisitingSequence,SweepSequence,TransitionCost,TransitionCost);
            end
        end

        function s=getStructParameters(seq,sseq,tcost,dcost)
            %getStructParameters create a structure.
            %Add one to sequence and sweep sequence to account for
            %indexing difference in MATLAB and C++.
            %MCOS returns index-1 as result.
            %If no solution then uniform zeros returned in solution.
            %Check for this condition.
            pathFoundFlag=isequal(0:1:(numel(seq)-1),sort(seq));
            s=struct('IsPathFound',pathFoundFlag,'VisitingSequence',seq+1,'SweepPattern',sseq+1,'TransitionCost',tcost,'DistanceCost',dcost,'Takeoff',[0 0 0],'Landing',[0 0 0]);
        end

        function pathSolnObj = connectMinTraversalPath(VisitOrderSpecified,SpecifiedSequence,StartingPolygonIndex,NumPolygons,AdjacencyInfo,startPoses,endPoses,Takeoff,Landing)
            %connectExhaustivePath call underlying C API method to connect
            %using Exhaustive Path Traversal.

            if coder.target('MATLAB')
                % Call standard builtin in MATLAB
                mcosObj =uav.algs.internal.builtin.CoveragePathOptimization();
                pathInternalObj=mcosObj.connectMinTraversalPath(VisitOrderSpecified,int32(SpecifiedSequence),uint32(StartingPolygonIndex),NumPolygons,int32(AdjacencyInfo),startPoses,endPoses,Takeoff,Landing);

                %Send back out a MATLAB friendly format file that holds all
                %the same info.
                pathSolnObj=uav.internal.coveragePlanner.coverageBuiltins.getStructParameters(pathInternalObj.VisitingSequence,pathInternalObj.SweepSequence,pathInternalObj.TransitionCost,pathInternalObj.TransitionCost);
            else
                buildableObj = uav.internal.coder.coverageBuildable();
                [VisitingSequence,SweepSequence,TransitionCost] = planMinTraversalPath(buildableObj,VisitOrderSpecified,SpecifiedSequence,StartingPolygonIndex,NumPolygons,AdjacencyInfo,startPoses,endPoses,Takeoff,Landing);
            pathSolnObj=uav.internal.coveragePlanner.coverageBuiltins.getStructParameters(VisitingSequence,SweepSequence,TransitionCost,TransitionCost);
            end
        end

    end

end

% LocalWords:  builtins
