| |
| |
| |
|
|
| #include <Rcpp.h> |
| #include <cmath> |
| using namespace Rcpp; |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| NumericVector random_batch_cpp(int seed, int count, double min_val = 0.0, double max_val = 1.0) { |
| NumericVector result(count); |
| double range = max_val - min_val; |
| |
| for (int i = 0; i < count; i++) { |
| double x = std::sin(static_cast<double>(seed + i)) * 10000.0; |
| double r = x - std::floor(x); |
| result[i] = min_val + r * range; |
| } |
| |
| return result; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| NumericMatrix bezier_batch_cpp(NumericVector p0, NumericVector cp1, |
| NumericVector cp2, NumericVector p1, |
| int n_points) { |
| NumericMatrix result(n_points, 2); |
| |
| |
| double p0x = p0[0], p0y = p0[1]; |
| double cp1x = cp1[0], cp1y = cp1[1]; |
| double cp2x = cp2[0], cp2y = cp2[1]; |
| double p1x = p1[0], p1y = p1[1]; |
| |
| |
| for (int i = 0; i < n_points; i++) { |
| double t = static_cast<double>(i) / static_cast<double>(n_points - 1); |
| double one_minus_t = 1.0 - t; |
| |
| |
| double one_minus_t_sq = one_minus_t * one_minus_t; |
| double one_minus_t_cu = one_minus_t_sq * one_minus_t; |
| double t_sq = t * t; |
| double t_cu = t_sq * t; |
| |
| |
| double coef1 = one_minus_t_cu; |
| double coef2 = 3.0 * one_minus_t_sq * t; |
| double coef3 = 3.0 * one_minus_t * t_sq; |
| double coef4 = t_cu; |
| |
| result(i, 0) = coef1 * p0x + coef2 * cp1x + coef3 * cp2x + coef4 * p1x; |
| result(i, 1) = coef1 * p0y + coef2 * cp1y + coef3 * cp2y + coef4 * p1y; |
| } |
| |
| return result; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| std::string svg_translate_cpp(std::string path_string, double dx, double dy) { |
| |
| if (dx == 0.0 && dy == 0.0) { |
| return path_string; |
| } |
| |
| std::string result; |
| result.reserve(path_string.size() + 100); |
| |
| size_t i = 0; |
| size_t n = path_string.size(); |
| |
| while (i < n) { |
| char c = path_string[i]; |
| |
| |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { |
| result += c; |
| i++; |
| continue; |
| } |
| |
| |
| if (c == 'M' || c == 'L') { |
| |
| result += c; |
| result += ' '; |
| i++; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == '\t')) i++; |
| |
| |
| size_t start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double x = std::stod(path_string.substr(start, i - start)) + dx; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == ',' || path_string[i] == '\t')) i++; |
| |
| |
| start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double y = std::stod(path_string.substr(start, i - start)) + dy; |
| |
| char buf[64]; |
| std::snprintf(buf, sizeof(buf), "%.2f %.2f", x, y); |
| result += buf; |
| |
| } else if (c == 'C') { |
| |
| result += c; |
| result += ' '; |
| i++; |
| |
| for (int pair = 0; pair < 3; pair++) { |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == ',' || path_string[i] == '\t')) i++; |
| |
| |
| size_t start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double x = std::stod(path_string.substr(start, i - start)) + dx; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == ',' || path_string[i] == '\t')) i++; |
| |
| |
| start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double y = std::stod(path_string.substr(start, i - start)) + dy; |
| |
| char buf[64]; |
| std::snprintf(buf, sizeof(buf), "%.2f %.2f", x, y); |
| result += buf; |
| if (pair < 2) result += ' '; |
| } |
| |
| } else if (c == 'A') { |
| |
| result += c; |
| result += ' '; |
| i++; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == '\t')) i++; |
| |
| |
| for (int val = 0; val < 5; val++) { |
| size_t start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| result += path_string.substr(start, i - start); |
| result += ' '; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == ',' || path_string[i] == '\t')) i++; |
| } |
| |
| |
| size_t start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double x = std::stod(path_string.substr(start, i - start)) + dx; |
| |
| |
| while (i < n && (path_string[i] == ' ' || path_string[i] == ',' || path_string[i] == '\t')) i++; |
| |
| |
| start = i; |
| while (i < n && (path_string[i] == '-' || path_string[i] == '.' || |
| (path_string[i] >= '0' && path_string[i] <= '9'))) i++; |
| double y = std::stod(path_string.substr(start, i - start)) + dy; |
| |
| char buf[64]; |
| std::snprintf(buf, sizeof(buf), "%.2f %.2f", x, y); |
| result += buf; |
| |
| } else if (c == 'Z') { |
| |
| result += c; |
| i++; |
| |
| } else { |
| |
| result += c; |
| i++; |
| } |
| } |
| |
| return result; |
| } |
|
|