Skip to content

Commit

Permalink
bundle clipper for pt-in-poly for #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed May 1, 2023
1 parent 037ba9f commit 24ecd0f
Show file tree
Hide file tree
Showing 8 changed files with 5,115 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: utaengine
Title: Routing and aggregation engine for 'Urban Transport Analyst'
Version: 0.1.0.121
Version: 0.1.0.122
Authors@R:
person(given = "Mark",
family = "Padgham",
Expand Down
8 changes: 6 additions & 2 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Generated by cpp11: do not edit by hand

cpp_index_sort <- function (index0) {
.Call (`_utaengine_cpp_index_sort`, index0)
cpp_pip <- function(layer, xy) {
.Call(`_utaengine_cpp_pip`, layer, xy)
}

cpp_index_sort <- function(index0) {
.Call(`_utaengine_cpp_index_sort`, index0)
}
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"codeRepository": "https://github.com/UrbanAnalyst/uta-engine",
"issueTracker": "https://github.com/UrbanAnalyst/uta-engine/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.0.121",
"version": "0.1.0.122",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
52 changes: 52 additions & 0 deletions src/clipper-cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "clipper-cpp11.h"

// clipper only works with integers, so double values have to be multiplied by
// this amount before converting to int:
const long long mult = 1e6;

//' rcpp_pip
//'
//' polys List of polygons for which to extract membership
//' @noRd
writable::integers cpp_pip(
const list polys,
const list xy)
{
const doubles x = xy ["x"];
const doubles y = xy ["y"];
const int n = x.size ();

writable::integers res (n);
std::fill (res.begin (), res.end (), -1L);
for (int i = 0; i < polys.size (); i++)
{
doubles pi = polys [i];
const int nrow = pi.size () / 2;

ClipperLib::Path path;
for (size_t j = 0; j < nrow; j++)
{
path << ClipperLib::IntPoint (round (pi [j] * mult),
round (pi [nrow + j] * mult));
}

for (size_t j = 0; j < n; j++)
{
const ClipperLib::IntPoint pj =
ClipperLib::IntPoint (round (x [j] * mult),
round (y [j] * mult));
int pip = ClipperLib::PointInPolygon (pj, path);
if (pip != 0) res [j] = i;
}

const double progress = 100 * i / polys.size ();
std::cout << "\r" << i << " / " << polys.size () << ": " <<
progress << "%";
std::cout.flush ();
check_user_interrupt ();
}
std::cout << "\r" << polys.size () << " / " << polys.size () <<
": 100%" << std::endl;

return res;
}
14 changes: 14 additions & 0 deletions src/clipper-cpp11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <iostream>> // std::cout

#include "clipper.h"
#include "cpp11.hpp"
#include <cpp11/protect.hpp>

using namespace cpp11;

[[cpp11::register]]
writable::integers cpp_pip(
const list layer,
const list xy);
Loading

0 comments on commit 24ecd0f

Please sign in to comment.