Commit 648bee3b authored by VAN TOLL Wouter's avatar VAN TOLL Wouter

- Reordered some code between Color and HelperFunctions.

- Added more documentation to HelperFunctions, Polygon2D, and Color.
parent b17c18d7
......@@ -39,6 +39,64 @@ struct Color
/// Creates a Color with the given R, G, B, and A components.
Color(unsigned short r, unsigned short g, unsigned short b, unsigned short a=255) : r(r), g(g), b(b), a(a) {}
/// <summary>Creates a Color based on HSV (Hue, Saturation, Value) values.</summary>
/// <details>Source: https://gist.github.com/kuathadianto/. </details>
/// <param name="H">The H (Hue) component of the color, between 0 and 360.</param>
/// <param name="S">The S (Saturation) component of the color, between 0 and 1.</param>
/// <param name="V">The V (Vaue) component of the color, between 0 and 1.</param>
/// <returns>A Color with RGB values that constitute the same color as the given HSV values.</returns>
static Color FromHSV(int H, double S, double V)
{
H = H % 360;
double C = S * V;
double X = C * (1 - abs(fmod(H / 60.0, 2) - 1));
double m = V - C;
double Rs, Gs, Bs;
if (H < 60) // red --> yellow
{
Rs = C;
Gs = X;
Bs = 0;
}
else if (H < 120) // yellow --> green
{
Rs = X;
Gs = C;
Bs = 0;
}
else if (H < 180) // green --> cyan
{
Rs = 0;
Gs = C;
Bs = X;
}
else if (H < 240) // cyan --> blue
{
Rs = 0;
Gs = X;
Bs = C;
}
else if (H < 300) // blue --> magenta
{
Rs = X;
Gs = 0;
Bs = C;
}
else // magenta --> red
{
Rs = C;
Gs = 0;
Bs = X;
}
return Color(
(int)((Rs + m) * 255),
(int)((Gs + m) * 255),
(int)((Bs + m) * 255));
}
};
#endif //COLOR_H
\ No newline at end of file
......@@ -42,13 +42,6 @@ namespace HelperFunctions
{
return std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count() / 1000000.0;
}
double GetRandomNumber(double minValue, double maxValue)
{
int range = (int)((maxValue - minValue) * 1000);
double val = (std::rand() % range) / 1000.0;
return minValue + val;
}
std::vector<std::string> SplitString(const std::string& str, const char delim)
{
......@@ -119,57 +112,6 @@ namespace HelperFunctions
return ss.str();
}
/*
* Source: https://gist.github.com/kuathadianto/
* H(Hue): 0 - 360 degree (integer)
* S(Saturation): 0 - 1.00 (double)
* V(Value): 0 - 1.00 (double)
*
* output[3]: Output, array size 3, int
*/
void HSVtoRGB(int H, double S, double V, int output[3])
{
double C = S * V;
double X = C * (1 - abs(fmod(H / 60.0, 2) - 1));
double m = V - C;
double Rs, Gs, Bs;
if (H >= 0 && H < 60) {
Rs = C;
Gs = X;
Bs = 0;
}
else if (H >= 60 && H < 120) {
Rs = X;
Gs = C;
Bs = 0;
}
else if (H >= 120 && H < 180) {
Rs = 0;
Gs = C;
Bs = X;
}
else if (H >= 180 && H < 240) {
Rs = 0;
Gs = X;
Bs = C;
}
else if (H >= 240 && H < 300) {
Rs = X;
Gs = 0;
Bs = C;
}
else {
Rs = C;
Gs = 0;
Bs = X;
}
output[0] = (int)((Rs + m) * 255);
output[1] = (int)((Gs + m) * 255);
output[2] = (int)((Bs + m) * 255);
}
bool DirectoryExists(const std::string &dirname)
{
struct stat info;
......
......@@ -29,22 +29,33 @@
namespace HelperFunctions
{
typedef std::chrono::time_point<std::chrono::high_resolution_clock> Timestamp;
/// <summary>Returns the current system time.</summary>
Timestamp GetCurrentTime();
/// <summary>Returns the number of milliseconds between two time stamps.</summary>
double GetIntervalMilliseconds(const Timestamp& startTime, const Timestamp& endTime);
/// <summary>Splits a string into parts according to a given delimiter.
/// The delimiter itself will be excluded from these parts.</summary>
/// <param name="str">The string to split.</param>
/// <param name="delim">The delimiter to use for splitting.</param>
/// <returns>A list of substrings, each containing a part of the input string between two subsequent delimiters.
/// The delimiter itself is not included in any of these parts.</returns>
std::vector<std::string> SplitString(const std::string& str, const char delim);
/// <summary>Tries to convert a string to a double.</summary>
double ParseDouble(const std::string& str);
/// <summary>Tries to convert a string to an int.</summary>
int ParseInt(const std::string& str);
/// <summary>Tries to convert a string to a bool.</summary>
bool ParseBool(const std::string& str);
/// <summary>Converts an integer to a string.</summary>
std::string ToString(int val);
/// <summary>Converts an integer to a string with a given minimum length.
/// If the integer is shorter than that, the string will be augmented with leading 0s.</summary>
std::string ToStringWithLeadingZeros(int value, int length);
double GetRandomNumber(double minValue, double maxValue);
void HSVtoRGB(int H, double S, double V, int output[3]);
template <typename T> inline T Clamp(T val, T minValue, T maxValue)
{
if (val < minValue)
......@@ -56,7 +67,14 @@ namespace HelperFunctions
return val;
}
/// <summary>Checks and returns whether a given directory exists.</summary>
/// <param name="dirname">The name of a directory, either absolute or relative.</param>
/// <returns>true if the directory exists; false otherwise.</returns>
bool DirectoryExists(const std::string& dirname);
/// <summary>Tries to create a directory if it does not yet exist.</summary>
/// <param name="dirname">The name of a directory, either absolute or relative.</param>
/// <returns>true if the directory either was successfully created or already existed; false otherwise.</returns>
bool CreateDirectoryIfNonExistent(const std::string& dirname);
};
......
/* UMANS: Unified Microscopic Agent Navigation Simulator
** Copyright (C) 2018-2020 Inria Rennes Bretagne Atlantique - Rainbow - Julien Pettré
**
** 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 3 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, see <https://www.gnu.org/licenses/>.
**
** Contact: crowd_group@inria.fr
** Website: https://project.inria.fr/crowdscience/
** See the file AUTHORS.md for a list of all contributors.
*/
#include <tools/Polygon2D.h>
#include <array>
......
......@@ -25,6 +25,8 @@
#include <vector>
#include <tools/vector2D.h>
/// <summary>A wrapper for a 2D polygon defined by a list of Vector2D points.</summary>
/// <details>Upon creation, a Polygon2D object pre-computes a list of edges *and* a triangulation.</details>
class Polygon2D
{
private:
......@@ -33,11 +35,16 @@ private:
std::vector<size_t> triangleIndices;
public:
Polygon2D(const std::vector<Vector2D> & vertices);
/// <summary>Creates a Polygon2D with the given vertices.</summary>
Polygon2D(const std::vector<Vector2D>& vertices);
/// <summary>Returns the vertices of this Polygon2D.</summary>
inline const std::vector<Vector2D>& GetVertices() const { return vertices_; }
/// <summary>Returns the edges of this Polygon2D.</summary>
inline const std::vector<LineSegment2D>& GetEdges() const { return edges_; }
/// <summary>Creates and returns a triangulation of this Polygon2D.
/// The triangle indices have been precomputed; this method only converts these triangles to actual objects.</summary>
std::vector<std::vector<Vector2D>> GetTriangles() const;
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment