function [params, variable_params, knot_types, Xstops] = initKnots(scene, ad, vd, stopdt)
% Computes an initial guess of parameter knots for the constant
% acceleration given by ad and desired velocity given by vd.
% Found knots are marked by the x locations in the below plot of position
% vs velocity. The horizontal line is at vd and the final x is at a stop
% location given by the ScenarioDescription scene. Stop locations are
% either stop sign stopping points or the goal point. There are two knots
% placed at the final x with a positive delta t of stopdt between them to 
% enforce stopping for a nonzero time.
%    x---------x
%   /           \
%  /             \
% /               x
%    |         |  |
%    X1       X2  XStop
    stop_region_pts = scene.getStopSignPoints(2); % 2 to signify coming from the left.
    crosswalk_region_pts = scene.getCrosswalkCenters();
    
    
    Xstops = [stop_region_pts(:,1)', scene.goal_pt(1)];
    Xcrosswalks = crosswalk_region_pts(:,1)';
    x0 = scene.getx0;
    
    Xa = x0(1);
    va = x0(4);
    
    kKNOT_ACC = 1;
    kKNOT_CRUISE = 2;
    kKNOT_STOP = 3;
    kKNOT_DWELL = 4;
    
    knots = [];
    knot_types = [];
    variable_params = [];
    for i = 1:length(Xstops)
        X1 = (vd^2 - va^2)/(2*ad) + Xa;
        X2 = Xstops(i) - vd^2/(2*ad);
        
        
        % accelerate
        knots = [knots, (vd - va)/ad, X1, vd];
        variable_params = [variable_params, 0, 1, 1];
        knot_types = [knot_types, kKNOT_ACC];
        
        % check if any crosswalks are in current region or there is a vehicle.
        if scene.is_vehicle_set || any(and(Xcrosswalks<X2,Xcrosswalks>X1))
            %put in acceleration knots to handle the crosswalks
            acc_step_length = 7;
            num_acc_knots = floor((X2 - X1)/acc_step_length);
            Xmid_prev = X1;
            for k = 1:num_acc_knots
                Xmid = X1 + k*(X2 - X1)/num_acc_knots;
                knots = [knots, (Xmid-Xmid_prev)/vd, Xmid, vd];
                variable_params = [variable_params, 0, 1, 1];
                knot_types = [knot_types, kKNOT_ACC];
            end
        else
            %coast
            knots = [knots, (X2-X1)/vd, X2, vd];
            variable_params = [variable_params, 0, 1, 0];
            knot_types = [knot_types, kKNOT_CRUISE];
        end
        
        % stop
        knots = [knots, vd/ad, Xstops(i), 0];
        variable_params = [variable_params, 0, 0, 0];
        knot_types = [knot_types, kKNOT_STOP];
        
        % dwell
        if i~=length(Xstops)
            knots = [knots, stopdt, Xstops(i), 0];
            variable_params = [variable_params, 1, 0, 0];
            knot_types = [knot_types, kKNOT_DWELL];
        end
        Xa = Xstops(i);
        va = 0;
    end
%     Nknots = numel(knots)/3;
    params = reshape(knots, 1, numel(knots));
    variable_params = find(variable_params == 1);
end


