polygon

Polygon triangulation
git clone git://git.meso-star.fr/polygon.git
Log | Files | Refs | README | LICENSE

commit d3fe03a5b8fdf2bf74adb57d43503aa3c18ffb65
parent e39719ad6697447924038f2b47730c90411f049f
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 18 Oct 2023 14:31:41 +0200

Merge branch 'feature_posix_make' into develop

Diffstat:
M.gitignore | 12+++++++-----
AMakefile | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 36++++++++++++++++++++----------------
Dcmake/CMakeLists.txt | 91-------------------------------------------------------------------------------
Aconfig.mk | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amake.sh | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apolygon.pc.in | 11+++++++++++
Msrc/polygon.c | 2+-
Msrc/polygon.h | 2+-
Msrc/test_polygon.c | 2+-
Msrc/test_polygon1.c | 2+-
Msrc/test_polygon_utils.h | 2+-
12 files changed, 337 insertions(+), 117 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,10 +1,12 @@ .gitignore -CMakeCache.txt -CMakeFiles -Makefile -tmp [Bb]uild* *.sw[po] -*.[ao] +*.[aod] +*.so *~ +test* +!test*.[ch] +.test +.config tags +polygon*.pc diff --git a/Makefile b/Makefile @@ -0,0 +1,145 @@ +# Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) +# +# This program 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. +# +# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + +.POSIX: +.SUFFIXES: # Clean up default inference rules + +include config.mk + +LIBNAME_STATIC = libpolygon.a +LIBNAME_SHARED = libpolygon.so +LIBNAME = $(LIBNAME_$(LIB_TYPE)) + +################################################################################ +# Library building +################################################################################ +SRC = src/polygon.c +OBJ = $(SRC:.c=.o) +DEP = $(SRC:.c=.d) + +build_library: .config $(DEP) + @$(MAKE) -fMakefile $$(for i in $(DEP); do echo -f $${i}; done) \ + $$(if [ -n "$(LIBNAME)" ]; then\ + echo "$(LIBNAME)";\ + else\ + echo "$(LIBNAME_SHARED)";\ + fi) + +$(DEP) $(OBJ): config.mk + +$(LIBNAME_SHARED): $(OBJ) + $(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(DPDC_LIBS) + +$(LIBNAME_STATIC): libpolygon.o + $(AR) -rc $@ $? + $(RANLIB) $@ + +libpolygon.o: $(OBJ) + $(LD) -r $(OBJ) -o $@ + $(OBJCOPY) $(OCPFLAGS) $@ + +.config: Makefile config.mk + @if ! $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys; then \ + echo "rsys $(RSYS_VERSION) not found"; exit 1; fi + @echo "config done" > .config + +.SUFFIXES: .c .d .o +.c.d: + @$(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + +.c.o: + $(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -DPOLYGON_SHARED_BUILD -c $< -o $@ + +################################################################################ +# Installation +################################################################################ +pkg: + sed -e 's#@PREFIX@#$(PREFIX)#g' \ + -e 's#@VERSION@#$(VERSION)#g' \ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g' \ + polygon.pc.in > polygon.pc + +polygon-local.pc: polygon.pc.in + sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\ + polygon.pc.in > $@ + +install: build_library pkg + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib" $(LIBNAME) + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/lib/pkgconfig" polygon.pc + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/include" src/polygon.h + @$(SHELL) make.sh install "$(DESTDIR)$(PREFIX)/share/doc/polygon" COPYING README.md + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)/lib/$(LIBNAME)" + rm -f "$(DESTDIR)$(PREFIX)/lib/pkgconfig/polygon.pc" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/polygon/COPYING" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/polygon/README.md" + rm -f "$(DESTDIR)$(PREFIX)/include/polygon.h" + +################################################################################ +# Miscellaneous targets +################################################################################ +all: build_library build_tests + +clean: clean_test + rm -f $(OBJ) $(TEST_OBJ) $(LIBNAME) + rm -f .config .test libpolygon.o polygon.pc polygon-local.pc + +distclean: clean + rm -f $(DEP) $(TEST_DEP) + +lint: + shellcheck -o all make.sh + +################################################################################ +# Tests +################################################################################ +TEST_SRC =\ + src/test_polygon.c\ + src/test_polygon1.c +TEST_OBJ = $(TEST_SRC:.c=.o) +TEST_DEP = $(TEST_SRC:.c=.d) + +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +POLYGON_CFLAGS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags polygon-local.pc) +POLYGON_LIBS = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs polygon-local.pc) + +build_tests: build_library $(TEST_DEP) .test + @$(MAKE) -fMakefile -f.test $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) test_bin + +test: build_tests + @$(SHELL) make.sh run_test $(TEST_SRC) + +.test: Makefile make.sh + @$(SHELL) make.sh config_test $(TEST_SRC) > $@ + +clean_test: + @$(SHELL) make.sh clean_test $(TEST_SRC) + +$(TEST_DEP) $(TEST_OBJ): config.mk polygon-local.pc + +$(TEST_DEP): + @$(CC) $(CFLAGS_EXE) $(POLYGON_CFLAGS) $(RSYS_CFLAGS) \ + -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(TEST_OBJ): config.mk polygon-local.pc + $(CC) $(CFLAGS_EXE) $(POLYGON_CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ + +test_polygon test_polygon1: config.mk polygon-local.pc $(LIBNAME) + $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(POLYGON_LIBS) $(RSYS_LIBS) -lm diff --git a/README.md b/README.md @@ -1,19 +1,22 @@ # Polygon -This small C89 library triangulates simple polygons, i.e. polygons whose their -non consecutive edges does not intersect. It relies on the ear-clipping variant -of Xianshu Kong et al. presented in "The Graham Scan Triangulates Simple -Polygons". +Polygon is a C library that triangulates polygons whose nonconsecutive edges do +not intersect. It implements a variation of the ear clipping algorithm of +Xianshu Kong et al. featured in [The Graham Scan Triangulates Simple +Polygons](https://doi.org/10.1016/0167-8655\(90\)90089-K) -## How to build +## Requirments -The library uses [CMake](http://www.cmake.org) and the -[RCMake](https://gitlab.com/vaplv/rcmake/#tab-readme) package to build. It also -depends on the [RSys](https://gitlab.com/vaplv/rsys/#tab-readme) library. -First, install the RCMake package and the RSys library. Then, generate the -project from the cmake/CMakeLists.txt file by appending the RCMake and RSys -install directories to the `CMAKE_PREFIX_PATH` variable. The resulting project -can be edited, built, tested and installed as any CMake project. +- C compiler +- POSIX make +- pkg-config +- [RSys](https://gitlab.com/vaplv/rsys/) + +## Installation + +Edit config.mk as needed, then run: + + make clean install ## Release notes @@ -33,7 +36,8 @@ Update the version of the RSys dependency to 0.6: replace the deprecated ## License -Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr). Polygon is free -software released under GPL v3+ license: GNU GPL version 3 or later. You are -welcome to redistribute it under certain conditions; refer to the COPYING file -for details. +Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) + +Polygon is free software released under GPL v3+ license: GNU GPL version 3 or +later. You are welcome to redistribute it under certain conditions; refer to +the COPYING file for details. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -1,91 +0,0 @@ -# Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) -# -# This program 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. -# -# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. - -cmake_minimum_required(VERSION 3.1) -project(polygon C) -enable_testing() - -option(NO_TEST "Do not compile the test pograms" OFF) - -set(POLYGON_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) - -################################################################################ -# Dependencies -################################################################################ -find_package(RCMake 0.4 REQUIRED) -find_package(RSys 0.6 REQUIRED) - -set(CMAKE_MODULE_PATH ${RCMAKE_SOURCE_DIR}) -include(rcmake) -include(rcmake_runtime) - -rcmake_append_runtime_dirs(_runtime_dirs RSys) - -################################################################################ -# Define targets -################################################################################ -set(VERSION_MAJOR 0) -set(VERSION_MINOR 1) -set(VERSION_PATCH 4) -set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) - -set(POLYGON_FILES_SRC polygon.c) -set(POLYGON_FILES_INC polygon.h) -set(POLYGON_FILES_DOC COPYING README.md) -rcmake_prepend_path(POLYGON_FILES_SRC ${POLYGON_SOURCE_DIR}) -rcmake_prepend_path(POLYGON_FILES_INC ${POLYGON_SOURCE_DIR}) -rcmake_prepend_path(POLYGON_FILES_DOC ${PROJECT_SOURCE_DIR}/../) - -if(CMAKE_COMPILER_IS_GNUCC) - set(MATH_LIB m) -endif() - -add_library(polygon SHARED ${POLYGON_FILES_SRC} ${POLYGON_FILES_INC}) -set_target_properties(polygon PROPERTIES - DEFINE_SYMBOL POLYGON_SHARED_BUILD - VERSION ${VERSION} - SOVERSION ${VERSION_MAJOR}) -target_link_libraries(polygon RSys ${MATH_LIB}) - -rcmake_setup_devel(polygon Polygon ${VERSION} polygon_version.h) - -################################################################################ -# Define tests -################################################################################ -if(NOT NO_TEST) - function(new_test _name) - add_executable(${_name} - ${POLYGON_SOURCE_DIR}/${_name}.c - ${POLYGON_SOURCE_DIR}/test_polygon_utils.h) - target_link_libraries(${_name} polygon ${MATH_LIB}) - add_test(${_name} ${_name}) - - rcmake_set_test_runtime_dirs(${_name} _runtime_dirs) - endfunction() - - new_test(test_polygon) - new_test(test_polygon1) -endif() - -################################################################################ -# Install directories -################################################################################ -install(TARGETS polygon - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) -install(FILES ${POLYGON_FILES_INC} DESTINATION include/) -install(FILES ${POLYGON_FILES_DOC} DESTINATION share/doc/polygon/) - diff --git a/config.mk b/config.mk @@ -0,0 +1,78 @@ +VERSION = 0.1.4 +PREFIX = /usr/local + +LIB_TYPE = SHARED +#LIB_TYPE = STATIC + +BUILD_TYPE = RELEASE +#BUILD_TYPE = DEBUG + +################################################################################ +# Tools +################################################################################ +AR = ar +CC = cc +LD = ld +PKG_CONFIG = pkg-config +OBJCOPY = objcopy +RANLIB = ranlib + +################################################################################ +# Dependencies +################################################################################ +PCFLAGS_STATIC = --static +PCFLAGS = $(PCFLAGS_$(LIB_TYPE)) + +RSYS_VERSION = 0.14 +RSYS_CFLAGS = $$($(PKG_CONFIG) $(PCFLAGS) --cflags rsys) +RSYS_LIBS = $$($(PKG_CONFIG) $(PCFLAGS) --libs rsys) + +DPDC_CFLAGS = $(RSYS_CFLAGS) +DPDC_LIBS = $(RSYS_LIBS) -lm + +################################################################################ +# Compilation options +################################################################################ +WFLAGS =\ + -Wall\ + -Wcast-align\ + -Wconversion\ + -Wextra\ + -Wmissing-declarations\ + -Wmissing-prototypes\ + -Wshadow + +CFLAGS_HARDENED =\ + -D_FORTIFY_SOURCES=2\ + -fcf-protection=full\ + -fstack-clash-protection\ + -fstack-protector-strong + +CFLAGS_COMMON =\ + -std=c89\ + -pedantic\ + -fvisibility=hidden\ + -fstrict-aliasing\ + $(CFLAGS_HARDENED)\ + $(WFLAGS) + +CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON) +CFLAGS_DEBUG = -g $(CFLAGS_COMMON) +CFLAGS = $(CFLAGS_$(BUILD_TYPE)) +CFLAGS_SO = $(CFLAGS) -fPIC +CFLAGS_EXE = $(CFLAGS) -fPIE + +################################################################################ +# Linker options +################################################################################ +LDFLAGS_HARDENED = -Wl,-z,relro,-z,now +LDFLAGS_DEBUG = $(LDFLAGS_HARDENED) +LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED) +LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) + +LDFLAGS_SO = $(LDFLAGS) -shared -Wl,--no-undefined +LDFLAGS_EXE = $(LDFLAGS) -pie + +OCPFLAGS_DEBUG = --localize-hidden +OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded +OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE)) diff --git a/make.sh b/make.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) +# +# This program 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. +# +# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + +set -e + +config_test() +{ + for i in "$@"; do + test=$(basename "${i}" ".c") + test_list="${test_list} ${test}" + printf "%s: src/%s.o\n" "${test}" "${test}" + done + printf "test_bin: %s\n" "${test_list}" +} + +run_test() +{ + for i in "$@"; do + test=$(basename "${i}" ".c") + + printf "%s " "${test}" + if ./"${test}" > /dev/null 2>&1; then + printf "\033[1;32mOK\033[m\n" + else + printf "\033[1;31mError\033[m\n" + n=$((n+1)) + fi + done 2> /dev/null +} + +clean_test() +{ + for i in "$@"; do + rm -f "$(basename "${i}" ".c")" + done +} + +install() +{ + prefix=$1 + shift 1 + + mkdir -p "${prefix}" + + for i in "$@"; do + dst="${prefix}/${i##*/}" + + if cmp -s "${i}" "${dst}"; then + printf "Up to date %s\n" "${dst}" + else + printf "Installing %s\n" "${dst}" + cp "${i}" "${prefix}" + fi + done +} + +"$@" diff --git a/polygon.pc.in b/polygon.pc.in @@ -0,0 +1,11 @@ +prefix=@PREFIX@ +includedir=${prefix}/include +libdir=${prefix}/lib + +Requires: rsys >= @RSYS_VERSION@ +Name: Polygon +Description: Polygon library +Version: @VERSION@ +Libs: -L${libdir} -lpolygon +Libs.private: -lm +CFlags: -I${includedir} diff --git a/src/polygon.c b/src/polygon.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/polygon.h b/src/polygon.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/test_polygon.c b/src/test_polygon.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/test_polygon1.c b/src/test_polygon1.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/test_polygon_utils.h b/src/test_polygon_utils.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2017, 2021 Vincent Forest (vaplv@free.fr) +/* Copyright (C) 2014-2017, 2021-2023 Vincent Forest (vaplv@free.fr) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by