Skip to content

Commit

Permalink
VehicleSpeedCurve: new class for speed curve
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgit committed Sep 12, 2024
1 parent 7d0c52c commit f34446d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
71 changes: 71 additions & 0 deletions server/src/vehicle/rail/vehiclespeedcurve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* server/src/vehicle/rail/vehiclespeedcurve.cpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2024 Filippo Gentile
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "vehiclespeedcurve.hpp"

#include <algorithm>

VehicleSpeedCurve::VehicleSpeedCurve(const std::array<double, 126> &arr)
: mSpeedCurve(arr)
{

}

double VehicleSpeedCurve::getSpeedForStep(uint8_t step) const
{
if(step <= 0 || step > 126)
return 0;

// We do not store zero so index is step - 1
return mSpeedCurve.at(step - 1);
}

uint8_t VehicleSpeedCurve::stepUpperBound(double speed) const
{
auto it = std::upper_bound(mSpeedCurve.begin(),
mSpeedCurve.end(),
speed);
if(it != mSpeedCurve.end())
{
int idx = std::distance(mSpeedCurve.begin(), it);

// We do not store zero so step is index + 1
int step = idx + 1;
return step;
}
return 0;
}

uint8_t VehicleSpeedCurve::stepLowerBound(double speed) const
{
auto it = std::lower_bound(mSpeedCurve.begin(),
mSpeedCurve.end(),
speed);
if(it != mSpeedCurve.end())
{
int idx = std::distance(mSpeedCurve.begin(), it);
// We do not store zero so step is index + 1
int step = idx + 1;
return step;
}
return 0;
}
43 changes: 43 additions & 0 deletions server/src/vehicle/rail/vehiclespeedcurve.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* server/src/vehicle/rail/vehiclespeedcurve.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2024 Filippo Gentile
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef TRAINTASTIC_SERVER_VEHICLE_RAIL_VEHICLESPEEDCURVE_HPP
#define TRAINTASTIC_SERVER_VEHICLE_RAIL_VEHICLESPEEDCURVE_HPP

#include <array>
#include <cstdint>

class VehicleSpeedCurve
{
public:
VehicleSpeedCurve(const std::array<double, 126>& arr = {});

double getSpeedForStep(uint8_t step) const;

uint8_t stepUpperBound(double speed) const;
uint8_t stepLowerBound(double speed) const;

private:
std::array<double, 126> mSpeedCurve;
};

#endif // TRAINTASTIC_SERVER_VEHICLE_RAIL_VEHICLESPEEDCURVE_HPP

0 comments on commit f34446d

Please sign in to comment.