/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
// Copyright (C) 2011-2026 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file    NGFrame.cpp
/// @author  Daniel Krajzewicz
/// @author  Jakob Erdmann
/// @author  Michael Behrisch
/// @date    06.05.2011
///
// Sets and checks options for netgen
/****************************************************************************/
#include <config.h>

#include <string>
#include "NGFrame.h"
#include <netbuild/NBNetBuilder.h>
#include <utils/options/Option.h>
#include <utils/options/OptionsCont.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/SystemFrame.h>
#include <utils/common/ToString.h>


// ===========================================================================
// method definitions
// ===========================================================================
void
NGFrame::fillOptions() {
    OptionsCont& oc = OptionsCont::getOptions();
    oc.doRegister("type-files", 't', new Option_FileName());
    oc.addDescription("type-files", "Input", TL("Read edge-type defs from FILE"));

    oc.doRegister("alphanumerical-ids", new Option_Bool(true));
    oc.addDescription("alphanumerical-ids", "Output", TL("The Ids of generated nodes use an alphanumerical code for easier readability when possible"));

    // register processing options
    oc.doRegister("turn-lanes", new Option_Integer(0));
    oc.addDescription("turn-lanes", "Processing", TL("Generate INT left-turn lanes"));
    oc.doRegister("turn-lanes.length", new Option_Float(20));
    oc.addDescription("turn-lanes.length", "Processing", TL("Set the length of generated turning lanes to FLOAT"));

    oc.doRegister("perturb-x", new Option_String("0"));
    oc.addDescription("perturb-x", "Processing", TL("Apply random spatial perturbation in x direction according to the given distribution"));
    oc.doRegister("perturb-y", new Option_String("0"));
    oc.addDescription("perturb-y", "Processing", TL("Apply random spatial perturbation in y direction according to the given distribution"));
    oc.doRegister("perturb-z", new Option_String("0"));
    oc.addDescription("perturb-z", "Processing", TL("Apply random spatial perturbation in z direction according to the given distribution"));

    oc.doRegister("bidi-probability", new Option_Float(1));
    oc.addSynonyme("bidi-probability", "rand-bidi-probability", true);
    oc.addSynonyme("bidi-probability", "rand.bidi-probability");
    oc.addSynonyme("bidi-probability", "bidi");
    oc.addDescription("bidi-probability", "Processing", TL("Defines the probability to build a reverse edge"));

    oc.doRegister("random-lanenumber", new Option_Bool(false));
    oc.addSynonyme("random-lanenumber", "rand.random-lanenumber", false);
    oc.addDescription("random-lanenumber", "Processing", TL("Draw lane numbers randomly from [1,default.lanenumber]"));

    oc.doRegister("random-priority", new Option_Bool(false));
    oc.addSynonyme("random-priority", "rand.random-priority", false);
    oc.addDescription("random-priority", "Processing", TL("Draw edge priority randomly from [1,default.priority]"));

    oc.doRegister("random-type", new Option_Bool(false));
    oc.addDescription("random-type", "Processing", TL("Draw edge type randomly from all loaded types"));


    //  register grid-net options
    oc.doRegister("grid", 'g', new Option_Bool(false));
    oc.addSynonyme("grid", "grid-net", true);
    oc.addDescription("grid", "Grid Network", TL("Forces NETGEN to build a grid-like network"));

    oc.doRegister("grid.number", new Option_Integer(5));
    oc.addSynonyme("grid.number", "grid-number", true);
    oc.addSynonyme("grid.number", "number");
    oc.addDescription("grid.number", "Grid Network", TL("The number of junctions in both dirs"));

    oc.doRegister("grid.length", new Option_Float(100));
    oc.addSynonyme("grid.length", "grid-length", true);
    oc.addSynonyme("grid.length", "length");
    oc.addDescription("grid.length", "Grid Network", TL("The length of streets in both dirs"));

    oc.doRegister("grid.x-number", new Option_Integer(5));
    oc.addSynonyme("grid.x-number", "grid-x-number", true);
    oc.addSynonyme("grid.x-number", "x-no");
    oc.addDescription("grid.x-number", "Grid Network", TL("The number of junctions in x-dir; Overrides --grid-number"));

    oc.doRegister("grid.y-number", new Option_Integer(5));
    oc.addSynonyme("grid.y-number", "grid-y-number", true);
    oc.addSynonyme("grid.y-number", "y-no");
    oc.addDescription("grid.y-number", "Grid Network", TL("The number of junctions in y-dir; Overrides --grid-number"));

    oc.doRegister("grid.x-length", new Option_Float(100));
    oc.addSynonyme("grid.x-length", "grid-x-length", true);
    oc.addSynonyme("grid.x-length", "x-length");
    oc.addDescription("grid.x-length", "Grid Network", TL("The length of horizontal streets; Overrides --grid-length"));

    oc.doRegister("grid.y-length", new Option_Float(100));
    oc.addSynonyme("grid.y-length", "grid-y-length", true);
    oc.addSynonyme("grid.y-length", "y-length");
    oc.addDescription("grid.y-length", "Grid Network", TL("The length of vertical streets; Overrides --grid-length"));

    oc.doRegister("grid.attach-length", new Option_Float(0));
    oc.addSynonyme("grid.attach-length", "attach-length", true);
    oc.addDescription("grid.attach-length", "Grid Network", TL("The length of streets attached at the boundary; 0 means no streets are attached"));

    oc.doRegister("grid.x-attach-length", new Option_Float(0));
    oc.addDescription("grid.x-attach-length", "Grid Network", TL("The length of streets attached at the boundary in x direction; 0 means no streets are attached"));
    oc.doRegister("grid.y-attach-length", new Option_Float(0));
    oc.addDescription("grid.y-attach-length", "Grid Network", TL("The length of streets attached at the boundary in y direction; 0 means no streets are attached"));

    //  register spider-net options
    oc.doRegister("spider", 's', new Option_Bool(false));
    oc.addSynonyme("spider", "spider-net", true);
    oc.addDescription("spider", "Spider Network", TL("Forces NETGEN to build a spider-net-like network"));

    oc.doRegister("spider.arm-number", new Option_Integer(7));
    oc.addSynonyme("spider.arm-number", "spider-arm-number", true);
    oc.addSynonyme("spider.arm-number", "arms");
    oc.addDescription("spider.arm-number", "Spider Network", TL("The number of axes within the net"));

    oc.doRegister("spider.circle-number", new Option_Integer(5));
    oc.addSynonyme("spider.circle-number", "spider-circle-number", true);
    oc.addSynonyme("spider.circle-number", "circles");
    oc.addDescription("spider.circle-number", "Spider Network", TL("The number of circles of the net"));

    oc.doRegister("spider.space-radius", new Option_Float(100));
    oc.addSynonyme("spider.space-radius", "spider-space-rad", true);
    oc.addSynonyme("spider.space-radius", "radius");
    oc.addDescription("spider.space-radius", "Spider Network", TL("The distances between the circles"));

    oc.doRegister("spider.omit-center", new Option_Bool(false));
    oc.addSynonyme("spider.omit-center", "spider-omit-center", true);
    oc.addSynonyme("spider.omit-center", "nocenter");
    oc.addDescription("spider.omit-center", "Spider Network", TL("Omit the central node of the network"));

    oc.doRegister("spider.attach-length", new Option_Float(0));
    oc.addDescription("spider.attach-length", "Spider Network", TL("The length of streets attached at the boundary; 0 means no streets are attached"));

    //  register random-net options
    oc.doRegister("rand", 'r', new Option_Bool(false));
    oc.addSynonyme("rand", "random-net", true);
    oc.addDescription("rand", "Random Network", TL("Forces NETGEN to build a random network"));

    oc.doRegister("rand.iterations", new Option_Integer(100));
    oc.addSynonyme("rand.iterations", "rand-iterations", true);
    oc.addSynonyme("rand.iterations", "iterations");
    oc.addDescription("rand.iterations", "Random Network", TL("Describes how many times an edge shall be added to the net"));

    oc.doRegister("rand.max-distance", new Option_Float(250));
    oc.addSynonyme("rand.max-distance", "rand-max-distance", true);
    oc.addSynonyme("rand.max-distance", "max-dist");
    oc.addDescription("rand.max-distance", "Random Network", TL("The maximum distance for each edge"));

    oc.doRegister("rand.min-distance", new Option_Float(100));
    oc.addSynonyme("rand.min-distance", "rand-min-distance", true);
    oc.addSynonyme("rand.min-distance", "min-dist");
    oc.addDescription("rand.min-distance", "Random Network", TL("The minimum distance for each edge"));

    oc.doRegister("rand.min-angle", new Option_Float(45.0));
    oc.addSynonyme("rand.min-angle", "rand-min-anglee", true);
    oc.addSynonyme("rand.min-angle", "min-angle");
    oc.addDescription("rand.min-angle", "Random Network", TL("The minimum angle for each pair of (bidirectional) roads in DEGREES"));

    oc.doRegister("rand.num-tries", new Option_Integer(50));
    oc.addSynonyme("rand.num-tries", "rand-num-tries", true);
    oc.addSynonyme("rand.num-tries", "num-tries");
    oc.addDescription("rand.num-tries", "Random Network", TL("The number of tries for creating each node"));

    oc.doRegister("rand.connectivity", new Option_Float((double) 0.95));
    oc.addSynonyme("rand.connectivity", "rand-connectivity", true);
    oc.addSynonyme("rand.connectivity", "connectivity");
    oc.addDescription("rand.connectivity", "Random Network", TL("Probability for roads to continue at each node"));

    oc.doRegister("rand.neighbor-dist1", new Option_Float(0));
    oc.addSynonyme("rand.neighbor-dist1", "rand-neighbor-dist1", true);
    oc.addSynonyme("rand.neighbor-dist1", "dist1");
    oc.addDescription("rand.neighbor-dist1", "Random Network", TL("Probability for a node having at most 1 neighbor"));

    oc.doRegister("rand.neighbor-dist2", new Option_Float(0));
    oc.addSynonyme("rand.neighbor-dist2", "rand-neighbor-dist2", true);
    oc.addSynonyme("rand.neighbor-dist2", "dist2");
    oc.addDescription("rand.neighbor-dist2", "Random Network", TL("Probability for a node having at most 2 neighbors"));

    oc.doRegister("rand.neighbor-dist3", new Option_Float(10));
    oc.addSynonyme("rand.neighbor-dist3", "rand-neighbor-dist3", true);
    oc.addSynonyme("rand.neighbor-dist3", "dist3");
    oc.addDescription("rand.neighbor-dist3", "Random Network", TL("Probability for a node having at most 3 neighbors"));

    oc.doRegister("rand.neighbor-dist4", new Option_Float(10));
    oc.addSynonyme("rand.neighbor-dist4", "rand-neighbor-dist4", true);
    oc.addSynonyme("rand.neighbor-dist4", "dist4");
    oc.addDescription("rand.neighbor-dist4", "Random Network", TL("Probability for a node having at most 4 neighbors"));

    oc.doRegister("rand.neighbor-dist5", new Option_Float(2));
    oc.addSynonyme("rand.neighbor-dist5", "rand-neighbor-dist5", true);
    oc.addSynonyme("rand.neighbor-dist5", "dist5");
    oc.addDescription("rand.neighbor-dist5", "Random Network", TL("Probability for a node having at most 5 neighbors"));

    oc.doRegister("rand.neighbor-dist6", new Option_Float(1));
    oc.addSynonyme("rand.neighbor-dist6", "rand-neighbor-dist6", true);
    oc.addSynonyme("rand.neighbor-dist6", "dist6");
    oc.addDescription("rand.neighbor-dist6", "Random Network", TL("Probability for a node having at most 6 neighbors"));

    oc.doRegister("rand.grid", new Option_Bool(false));
    oc.addDescription("rand.grid", "Random Network", TL("Place nodes on a regular grid with spacing rand.min-distance"));
}


bool
NGFrame::checkOptions() {
    OptionsCont& oc = OptionsCont::getOptions();
    bool ok = true;
    // check whether exactly one type of a network to build was wished
    int no = 0;
    if (oc.getBool("spider")) {
        no++;
    }
    if (oc.getBool("grid")) {
        no++;
    }
    if (oc.getBool("rand")) {
        no++;
    }
    if (no == 0) {
        WRITE_ERROR(TL("You have to specify the type of network to generate."));
        ok = false;
    }
    if (no > 1) {
        WRITE_ERROR(TL("You may specify only one type of network to generate at once."));
        ok = false;
    }
    if (oc.getBool("random-type") && !oc.isSet("type-files")) {
        WRITE_WARNING(TL("Option 'random-type' takes no effect unless 'type-files' are loaded"));
    }
    if (oc.getFloat("rand.connectivity") > 0.999) {
        WRITE_ERROR(TL("Option 'rand.connectivity' requires a value below 0.999"));
        ok = false;
    }
    return ok;
}


/****************************************************************************/
