Skip to content

Commit

Permalink
Merge pull request #6 from delfrrr/layout-updates
Browse files Browse the repository at this point in the history
Use mapbox hpp skel
  • Loading branch information
delfrrr authored Sep 12, 2018
2 parents faaec71 + 90bd235 commit 5ba668d
Show file tree
Hide file tree
Showing 30 changed files with 1,298 additions and 780 deletions.
19 changes: 19 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Standard: Cpp11
IndentWidth: 4
AccessModifierOffset: -4
UseTab: Never
BinPackParameters: false
BinPackArguments: false
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowAllParametersOfDeclarationOnNextLine: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
AlwaysBreakTemplateDeclarations: true
NamespaceIndentation: None
PointerBindsToType: true
SpacesInParentheses: false
BreakBeforeBraces: Attach
ColumnLimit: 0
Cpp11BracedListStyle: false
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
.vscode
build
includes
cmake-build
.DS_Store
out
node_modules
node_modules
mason_packages
.toolchain
.mason
local.env
62 changes: 55 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
language: cpp
dist: trusty
compiler: clang++
os: linux
language: generic

matrix:
include:
# clang-format specific job
- os: linux
sudo: false
env: CLANG_FORMAT
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'libstdc++6', 'libstdc++-5-dev' ]
script:
- make format
- os: linux
sudo: false
env: CXX=g++-5
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'g++-5' ]
- os: linux
sudo: false
env: CXX=clang++
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test' ]
packages: [ 'libstdc++6', 'libstdc++-5-dev' ]
# disabled before fixing https://github.com/delfrrr/delaunator-cpp/issues/5
# - os: linux
# sudo: required # workaround https://github.com/mapbox/node-cpp-skel/issues/93
# env: CXXFLAGS="-fsanitize=address,undefined,integer -fno-sanitize-recover=all"
# addons:
# apt:
# sources: [ 'ubuntu-toolchain-r-test' ]
# packages: [ 'libstdc++6', 'libstdc++-5-dev' ]
env:
global:
- CMAKE_VERSION="3.8.2"

install:
- cmake -DCMAKE_BUILD_TYPE=Release
- VERBOSE=1 make
script: ./delaunator-test
# set up the environment by installing mason and clang++
- ./scripts/setup.sh --config local.env
# put mason and clang++ on PATH
- source local.env
- mason install cmake ${CMAKE_VERSION}
- mason link cmake ${CMAKE_VERSION}
- which cmake

script:
- make release
- make test
- make clean
- make debug
- make test
- make clean
77 changes: 51 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,63 @@
cmake_minimum_required(VERSION 3.0.0)
project(delaunator VERSION 0.1.0)
cmake_minimum_required(VERSION 3.8)
project(delaunator VERSION 0.2.0)
set (CMAKE_CXX_STANDARD 14)
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/includes")
execute_process(COMMAND bash "-c" "(cd ${CMAKE_CURRENT_SOURCE_DIR} && ./fetch-includes.sh)")
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/mason.cmake)

option(WERROR "Add -Werror flag to build (turns warnings into errors)" ON)

# configure optimization
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(OPTIMIZATION_FLAGS "-O0 -DDEBUG")
message("-- Configuring debug build")
else()
set(OPTIMIZATION_FLAGS "-O3 -DNDEBUG")
message("-- Configuring release build")
endif()

# Enable extra warnings to adhere to https://github.com/mapbox/cpp/issues/37
set(DESIRED_WARNINGS "-Wall -Wextra -Wconversion -Wunreachable-code -Wuninitialized -pedantic-errors -Wold-style-cast -Wno-error=unused-variable -Wshadow -Wfloat-equal -Weffc++")
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(DESIRED_WARNINGS "${DESIRED_WARNINGS} -Wmost")
endif()

#delaunator
add_library(delaunator src/delaunator.cpp)
target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint")
# Note: -D_GLIBCXX_USE_CXX11_ABI=0 is needed to support mason packages that are precompiled libs
# Currently we only depend on a header only library, but this will help avoid issues when more libs are added via mason
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPTIMIZATION_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0 ${DESIRED_WARNINGS}")

if (WERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()

# mason_use is a mason function within the mason.cmake file and provides ready-to-go vars, like "STATIC_LIBS" and "INCLUDE_DIRS"
mason_use(catch VERSION 2.4.0 HEADER_ONLY)
include_directories(SYSTEM ${MASON_PACKAGE_catch_INCLUDE_DIRS})

mason_use(rapidjson VERSION 1.1.0 HEADER_ONLY)
include_directories(SYSTEM ${MASON_PACKAGE_rapidjson_INCLUDE_DIRS})

mason_use(benchmark VERSION 1.2.0)
include_directories(SYSTEM ${MASON_PACKAGE_benchmark_INCLUDE_DIRS})

#delaunator
add_library(json-helpers src/json-helpers.cpp)
target_include_directories (json-helpers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
include_directories("${PROJECT_SOURCE_DIR}/include")

#delaunator-test
add_executable(delaunator-test src/delaunator-test.cpp)
target_link_libraries(delaunator-test delaunator)
target_include_directories (delaunator-test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/catch/single_include/catch2")
target_link_libraries(delaunator-test json-helpers)
file(GLOB TEST_SOURCES test/*.cpp)
add_executable(unit-tests ${TEST_SOURCES})

#benchmark
add_executable(benchmark src/benchmark.cpp)
target_link_libraries(benchmark delaunator)
target_link_libraries(benchmark json-helpers)
# libbenchmark.a supports threads and therefore needs pthread support
find_package(Threads REQUIRED)
file(GLOB BENCH_SOURCES bench/*.cpp)
add_executable(bench-tests ${BENCH_SOURCES})

#examples
add_executable(triangulate-geojson examples/triangulate_geojson.cpp)
add_executable(basic examples/basic.cpp)

#triangulate
add_executable(triangulate src/triangulate.cpp)
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint")
target_link_libraries(triangulate delaunator)
target_link_libraries(triangulate json-helpers)

# link benchmark static library to the bench-tests binary so the bench tests know where to find the benchmark impl code
target_link_libraries(bench-tests ${MASON_PACKAGE_benchmark_STATIC_LIBS} ${CMAKE_THREAD_LIBS_INIT})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# Whether to turn compiler warnings into errors
export WERROR ?= true
export BUILD_DIR ?= cmake-build

default: release

release:
mkdir -p ./$(BUILD_DIR) && cd ./$(BUILD_DIR) && cmake ../ -DCMAKE_BUILD_TYPE=Release -DWERROR=$(WERROR) && VERBOSE=1 cmake --build .

debug:
mkdir -p ./$(BUILD_DIR) && cd ./$(BUILD_DIR) && cmake ../ -DCMAKE_BUILD_TYPE=Debug -DWERROR=$(WERROR) && VERBOSE=1 cmake --build .

test:
@if [ -f ./$(BUILD_DIR)/unit-tests ]; then ./$(BUILD_DIR)/unit-tests; else echo "Please run 'make release' or 'make debug' first" && exit 1; fi

bench:
@if [ -f ./$(BUILD_DIR)/bench-tests ]; then ./$(BUILD_DIR)/bench-tests; else echo "Please run 'make release' or 'make debug' first" && exit 1; fi

tidy:
@echo "not implmented"

coverage:
@echo "not implmented"

clean:
rm -rf ./$(BUILD_DIR)
# remove remains from running 'make coverage'
rm -f *.profraw
rm -f *.profdata
@echo "run 'make distclean' to also clear mason_packages, .mason, and .toolchain directories"

distclean: clean
rm -rf mason_packages
# remove remains from running './scripts/setup.sh'
rm -rf .mason
rm -rf .toolchain
rm -f local.env

format:
./scripts/format.sh

.PHONY: test bench
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,41 @@ A really fast C++ library for
delaunator-cpp is a C++ port from https://github.com/mapbox/delaunator a JavaScript implementation of very fast 2D Delaunay algorithm.

[![Build Status](https://travis-ci.org/delfrrr/delaunator-cpp.svg?branch=master)](https://travis-ci.org/delfrrr/delaunator-cpp)
[![badge](https://mapbox.s3.amazonaws.com/cpp-assets/hpp-skel-badge_blue.svg)](https://github.com/mapbox/hpp-skel)

## Features

* Probably the fastest C++ open source 2D Delaunay implementation
* Roughly 3 times faster then JS version.
* Roughly 6 times faster then JS version (more improvements are coming).
* Example showing triangulation of GeoJson points

## Usage

`examples/basic.cpp`

```CPP
#include "delaunator.h"
#include <cstdio>
using namespace std;
//...
int main(int, char* argv[]) {
//...
const vector<double> coords = {/* x0, y0, x1, y1, ... */};
#include <delaunator.hpp>
#include <cstdio>

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

//triangulation happens here
//note moving points to constructor
Delaunator delaunator(move(coords));
delaunator::Delaunator d(coords);

for(long int i = 0; i < delaunator.triangles.size(); i+=3) {
for(std::size_t i = 0; i < d.triangles.size(); i+=3) {
printf(
"Triangle points: [[%f, %f], [%f, %f], [%f, %f]]\n",
delaunator.coords[2 * delaunator.triangles[i]], //tx0
delaunator.coords[2 * delaunator.triangles[i] + 1], //ty0
delaunator.coords[2 * delaunator.triangles[i + 1]], //tx1
delaunator.coords[2 * delaunator.triangles[i + 1] + 1], //ty1
delaunator.coords[2 * delaunator.triangles[i + 2]], //tx2
delaunator.coords[2 * delaunator.triangles[i + 2] + 1], //ty2
)
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
);
}
}
```

For full example see `src/triangulate.cpp`
## TODO
* Benchmarks
* Unit tests
[See more examples here](./examples)
19 changes: 19 additions & 0 deletions bench/run.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "../examples/utils.hpp"
#include <benchmark/benchmark.h>
#include <delaunator.hpp>
#include <string>

namespace {
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);

while (state.KeepRunning()) {
delaunator::Delaunator delaunator(coords);
}
}
} // namespace

BENCHMARK(BM_45K_geojson_nodes)->Unit(benchmark::kMillisecond);

BENCHMARK_MAIN()
Loading

0 comments on commit 5ba668d

Please sign in to comment.