Skip to content

Restructure #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions bench/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
#include <string>
#include <vector>

std::vector<double> generate_uniform(std::size_t n) {
std::vector<double> coords;
coords.reserve(2 * n);
std::vector<utils::point> generate_uniform(std::size_t n) {
std::vector<utils::point> coords;
coords.reserve(n);
std::srand(350);
double norm = static_cast<double>(RAND_MAX) / 1e3;
for (size_t i = 0; i < n; i++) {
coords.push_back(static_cast<double>(std::rand()) / norm);
coords.push_back(static_cast<double>(std::rand()) / norm);
coords.push_back({(static_cast<double>(std::rand()) / norm), (static_cast<double>(std::rand()) / norm)});
}

return coords;
}

void BM_45K_geojson_nodes(benchmark::State& state) {
std::string points_str = utils::read_file("./test/test-files/osm-nodes-45331-epsg-3857.geojson");
std::vector<double> coords = utils::get_geo_json_points(points_str);
auto coords = utils::get_geo_json_points(points_str);

while (state.KeepRunning()) {
delaunator::Delaunator delaunator(coords);
}
auto result = delaunator::delaunator(coords);
benchmark::DoNotOptimize(result);
}
}

void BM_uniform(benchmark::State& state) {
std::vector<double> coords = generate_uniform(static_cast<std::size_t>(state.range(0)));
std::vector<utils::point> coords = generate_uniform(static_cast<std::size_t>(state.range(0)));
while (state.KeepRunning()) {
delaunator::Delaunator delaunator(coords);
auto result = delaunator::delaunator(coords);
benchmark::DoNotOptimize(result);
}
state.SetComplexityN(state.range(0));
}
Expand Down
22 changes: 14 additions & 8 deletions examples/basic.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#include <delaunator.hpp>
#include <cstdio>

// Define your point type
struct my_point {
double x;
double y;
};

int main() {
/* x0, y0, x1, y1, ... */
std::vector<double> coords = {-1, 1, 1, 1, 1, -1, -1, -1};
std::vector<my_point> coords = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

//triangulation happens here
delaunator::Delaunator d(coords);
auto d = delaunator::delaunator<my_point>(coords);

for(std::size_t i = 0; i < d.triangles.size(); i+=3) {
printf(
"Triangle points: [[%f, %f], [%f, %f], [%f, %f]]\n",
d.coords[2 * d.triangles[i]], //tx0
d.coords[2 * d.triangles[i] + 1], //ty0
d.coords[2 * d.triangles[i + 1]], //tx1
d.coords[2 * d.triangles[i + 1] + 1],//ty1
d.coords[2 * d.triangles[i + 2]], //tx2
d.coords[2 * d.triangles[i + 2] + 1] //ty2
coords[d.triangles[i]].x, //tx0
coords[d.triangles[i]].y, //ty0
coords[d.triangles[i + 1]].x, //tx1
coords[d.triangles[i + 1]].y, //ty1
coords[d.triangles[i + 2]].x, //tx2
coords[d.triangles[i + 2]].y //ty2
);
}
}
25 changes: 13 additions & 12 deletions examples/triangulate_geojson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include <initializer_list>
#include <iostream>

std::string serialize_to_json(delaunator::Delaunator const& delaunator) {
std::string serialize_to_json(delaunator::DelaunatorResult const& delaunator,
std::vector<utils::point> const& coords) {
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
writer.StartObject();
Expand All @@ -34,20 +35,20 @@ std::string serialize_to_json(delaunator::Delaunator const& delaunator) {
writer.StartArray();
writer.StartArray();
writer.StartArray();
writer.Double(delaunator.coords[2 * delaunator.triangles[i]]);
writer.Double(delaunator.coords[2 * delaunator.triangles[i] + 1]);
writer.Double(coords[delaunator.triangles[i]].x);
writer.Double(coords[delaunator.triangles[i]].y);
writer.EndArray();
writer.StartArray();
writer.Double(delaunator.coords[2 * delaunator.triangles[i + 1]]);
writer.Double(delaunator.coords[2 * delaunator.triangles[i + 1] + 1]);
writer.Double(coords[delaunator.triangles[i + 1]].x);
writer.Double(coords[delaunator.triangles[i + 1]].y);
writer.EndArray();
writer.StartArray();
writer.Double(delaunator.coords[2 * delaunator.triangles[i + 2]]);
writer.Double(delaunator.coords[2 * delaunator.triangles[i + 2] + 1]);
writer.Double(coords[delaunator.triangles[i + 2]].x);
writer.Double(coords[delaunator.triangles[i + 2]].y);
writer.EndArray();
writer.StartArray();
writer.Double(delaunator.coords[2 * delaunator.triangles[i]]);
writer.Double(delaunator.coords[2 * delaunator.triangles[i] + 1]);
writer.Double(coords[delaunator.triangles[i]].x);
writer.Double(coords[delaunator.triangles[i]].y);
writer.EndArray();
writer.EndArray();
writer.EndArray();
Expand All @@ -63,9 +64,9 @@ int main(int, char* argv[]) {
const char* filename = argv[1];
const char* output = argv[2];
std::string json = utils::read_file(filename);
std::vector<double> coords = utils::get_geo_json_points(json);
delaunator::Delaunator delaunator(coords);
const char* out_json = serialize_to_json(delaunator).c_str();
auto coords = utils::get_geo_json_points(json);
auto result = delaunator::delaunator(coords);
const char* out_json = serialize_to_json(result, coords).c_str();

if (output) {
printf("Writing to file %s", output);
Expand Down
13 changes: 9 additions & 4 deletions examples/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

namespace utils {

// Define your point type
struct point {
double x;
double y;
};

inline std::string read_file(const char* filename) {
std::ifstream input_file(filename);
if(input_file.good()) {
Expand All @@ -22,20 +28,19 @@ inline std::string read_file(const char* filename) {
}
}

inline std::vector< double> get_geo_json_points(std::string const& json) {
inline std::vector<point> get_geo_json_points(std::string const& json) {
rapidjson::Document document;
if(document.Parse(json.c_str()).HasParseError()) {
throw std::runtime_error("Cannot parse JSON");
}
const rapidjson::Value& features = document["features"];
std::vector<double> coords;
std::vector<point> coords;
// vector<double> y_vector;
for(rapidjson::SizeType i = 0; i < features.Size(); i++) {
const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"];
const double x = coordinates[0].GetDouble();
const double y = coordinates[1].GetDouble();
coords.push_back(x);
coords.push_back(y);
coords.push_back({x, y});
}
return coords;
}
Expand Down
Loading