diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/errors.cpp | 33 | ||||
-rw-r--r-- | src/utils/errors.hpp | 34 | ||||
-rw-r--r-- | src/utils/filesystem.cpp | 72 | ||||
-rw-r--r-- | src/utils/filesystem.hpp | 34 | ||||
-rw-r--r-- | src/utils/geometry.cpp | 78 | ||||
-rw-r--r-- | src/utils/geometry.hpp | 34 | ||||
-rw-r--r-- | src/utils/windows.hpp | 27 |
7 files changed, 312 insertions, 0 deletions
diff --git a/src/utils/errors.cpp b/src/utils/errors.cpp new file mode 100644 index 0000000..1baaaeb --- /dev/null +++ b/src/utils/errors.cpp @@ -0,0 +1,33 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + +#include "errors.hpp" + +#include <iostream> + +namespace utils { +namespace errors { + +void Die(std::string error_message) +{ + std::cerr << "Error: " << error_message << std::endl; + std::exit(EXIT_FAILURE); +} + +} // namespace errors +} // namespace utils diff --git a/src/utils/errors.hpp b/src/utils/errors.hpp new file mode 100644 index 0000000..8fa8c25 --- /dev/null +++ b/src/utils/errors.hpp @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + +#ifndef GRAPHCOLORING_UTILS_ERRORS_H_ +#define GRAPHCOLORING_UTILS_ERRORS_H_ + +#include <string> +#include <cassert> + +namespace utils { +namespace errors { + +// Output to std::cerr and exit. +extern void Die(std::string error_message); + +} // namespace errors +} // namespace utils + +#endif // GRAPHCOLORING_UTILS_ERRORS_H_ diff --git a/src/utils/filesystem.cpp b/src/utils/filesystem.cpp new file mode 100644 index 0000000..045006d --- /dev/null +++ b/src/utils/filesystem.cpp @@ -0,0 +1,72 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + + +#include "filesystem.hpp" + +#include <cstdio> +#include <fstream> +#include <sys/stat.h> +#include <dirent.h> + +#include "errors.hpp" +#include "windows.hpp" + +#ifdef WINDOWS +#include <windows.h> +#endif + +namespace utils { +namespace filesystem { + +void remove_file(std::string path) +{ + if (remove(path.c_str())) + utils::errors::Die(std::string("Failed to delete ") + path); +} + +void copy_file(std::string src, std::string dest) +{ + std::ifstream source(src, std::ios::binary); + std::ofstream destination(dest, std::ios::binary); + + destination << source.rdbuf(); +} + +void create_directory(std::string path) +{ +#ifdef WINDOWS + mkdir(path.c_str()); +#else + mkdir(path.c_str(), 0777); +#endif +} + +bool directory_exists(std::string path) +{ + DIR* dir = opendir(path.c_str()); + if (dir) + { + closedir(dir); + return true; + } + return false; +} + +} // namespace filesystem +} // namespace utils diff --git a/src/utils/filesystem.hpp b/src/utils/filesystem.hpp new file mode 100644 index 0000000..855dbf4 --- /dev/null +++ b/src/utils/filesystem.hpp @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// +#ifndef GRAPHCOLORING_UTILS_FILESYSTEM_H_ +#define GRAPHCOLORING_UTILS_FILESYSTEM_H_ + +#include <string> + +namespace utils { +namespace filesystem { + +extern void remove_file(std::string path); +extern void copy_file(std::string src, std::string dest); +extern void create_directory(std::string path); +extern bool directory_exists(std::string path); + +} // namespace filesystem +} // namespace utils + +#endif // GRAPHCOLORING_UTILS_FILESYSTEM_H_ diff --git a/src/utils/geometry.cpp b/src/utils/geometry.cpp new file mode 100644 index 0000000..733190f --- /dev/null +++ b/src/utils/geometry.cpp @@ -0,0 +1,78 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + +#include "geometry.hpp" + +#define _USE_MATH_DEFINES +#include <cmath> +#include <iostream> + +#include "errors.hpp" + +namespace utils { +namespace geometry { + +bool InRectangle(int x, int y, int rx, int ry, int rw, int rh) +{ + return x >= rx && x <= rx+rw && y >= ry && y <= ry+rh; +} + +bool InCircle(int x, int y, int cx, int cy, int r) +{ + return (x-cx)*(x-cx) + (y-cy)*(y-cy) <= r*r; +} + +double LineAngle(int x1, int y1, int x2, int y2) +{ + if (x1 == x2) + { + if (y1 == y2) + return 0; // Give up + if (y1 < y2) + return M_PI / 2; // Slope = Positive infinity + if (y1 > y2) + return 3 * M_PI / 2; // Negative infinity + } + double slope = (double)(y2-y1)/(x2-x1); + return x2 > x1 ? atan(slope) : M_PI + atan(slope); + +} + +int PointToLineSegmentDistance(int x, int y, int x1, int y1, int x2, int y2, + int tolerance) +{ + // See https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Line_defined_by_two_points + // Check if point is inside rectangle bounded by endpoints. + int min_x = std::min(x1, x2); + int min_y = std::min(y1, y2); + int max_x = std::max(x1, x2); + int max_y = std::max(y1, y2); + if (x < min_x-tolerance || x > max_x+tolerance || y < min_y-tolerance + || y > max_y+tolerance) + return -1; + // Check if the two points are the same. + if (x1 == x2 && y1 == y2) + return sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y)); + // Find distance to line. + int dx = x2-x1, dy = y2-y1; + double length = sqrt(dx*dx+dy*dy); + return abs(dy*x - dx*y + x2*y1 - y2*x1)/length; +} + +} // namespace geometry +} // namespace utils diff --git a/src/utils/geometry.hpp b/src/utils/geometry.hpp new file mode 100644 index 0000000..76afef7 --- /dev/null +++ b/src/utils/geometry.hpp @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// + +#ifndef GRAPHCOLORING_UTILS_GEOMETRY_H_ +#define GRAPHCOLORING_UTILS_GEOMETRY_H_ + +namespace utils { +namespace geometry { + +extern bool InRectangle(int x, int y, int rx, int ry, int rw, int rh); +extern bool InCircle(int x, int y, int cx, int cy, int r); +extern double LineAngle(int x1, int y1, int x2, int y2); // Angle from (x1,y1) to (x2,y2) +extern int PointToLineSegmentDistance(int x, int y, int x1, int y1, int x2, + int y2, int tolerance); // Returns -1 if point does not project to line segment +} // namespace geometry +} // namespace utils + + +#endif // GRAPHCOLORING_UTILS_GEOMETRY_H_ diff --git a/src/utils/windows.hpp b/src/utils/windows.hpp new file mode 100644 index 0000000..d95c052 --- /dev/null +++ b/src/utils/windows.hpp @@ -0,0 +1,27 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2018 Leo Tenenbaum +// This file is part of GraphColoring. +// +// GraphColoring 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. +// +// GraphColoring 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 GraphColoring. If not, see <https://www.gnu.org/licenses/>. +//////////////////////////////////////////////////////////////////////////////// +#ifndef GRAPHCOLORING_UTILS_WINDOWS_H_ +#define GRAPHCOLORING_UTILS_WINDOWS_H_ + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#define WINDOWS +#endif + + + +#endif // GRAPHCOLORING_UTILS_WINDOWS_H_ |