From 6fe8bfdc843e279ad7d0bb306a845273fd93cc14 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Thu, 21 Nov 2019 08:51:50 +0100 Subject: [PATCH] Various updates (#6) * Cleanup CMake * Update Readme * Added example * Added CI --- .appveyor.yml | 55 +++++++++ .travis.yml | 101 +++++++++++++++ CMakeLists.txt | 170 +++++++++++++------------- README.md | 117 +++++++++++++----- cppcolormap.pc.in | 12 +- cppcolormapConfig.cmake.in | 15 ++- examples/cpp/CMakeLists.txt | 25 ++++ examples/cpp/match.cpp | 14 +++ examples/match.py | 22 ---- examples/python/match.py | 26 ++++ include/cppcolormap.h | 18 +-- include/python.cpp => python/main.cpp | 2 +- setup.cfg | 2 - setup.py | 52 ++++---- 14 files changed, 439 insertions(+), 192 deletions(-) create mode 100644 .appveyor.yml create mode 100644 .travis.yml create mode 100644 examples/cpp/CMakeLists.txt create mode 100644 examples/cpp/match.cpp delete mode 100644 examples/match.py create mode 100644 examples/python/match.py rename include/python.cpp => python/main.cpp (99%) delete mode 100644 setup.cfg diff --git a/.appveyor.yml b/.appveyor.yml new file mode 100644 index 0000000..1065c1d --- /dev/null +++ b/.appveyor.yml @@ -0,0 +1,55 @@ +build: false + +branches: + only: + - master + +platform: + - x64 + +image: + - Visual Studio 2017 + - Visual Studio 2015 + +environment: + matrix: + - MINICONDA: C:\pyxtensor-conda + +init: + - "ECHO %MINICONDA%" + - if "%APPVEYOR_BUILD_WORKER_IMAGE%" == "Visual Studio 2015" set VCVARPATH="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" + - if "%APPVEYOR_BUILD_WORKER_IMAGE%" == "Visual Studio 2015" set VCARGUMENT=%PLATFORM% + - if "%APPVEYOR_BUILD_WORKER_IMAGE%" == "Visual Studio 2017" set VCVARPATH="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" + - echo "%VCVARPATH% %VCARGUMENT%" + - "%VCVARPATH% %VCARGUMENT%" + - ps: if($env:Platform -eq "x64"){Start-FileDownload 'http://repo.continuum.io/miniconda/Miniconda3-latest-Windows-x86_64.exe' C:\Miniconda.exe; echo "Done"} + - ps: if($env:Platform -eq "x86"){Start-FileDownload 'http://repo.continuum.io/miniconda/Miniconda3-latest-Windows-x86.exe' C:\Miniconda.exe; echo "Done"} + - cmd: C:\Miniconda.exe /S /D=C:\pyxtensor-conda + - "set PATH=%MINICONDA%;%MINICONDA%\\Scripts;%MINICONDA%\\Library\\bin;%PATH%" + +install: + # Set environment using Conda + - conda config --set always_yes yes --set changeps1 no + - conda update -q conda + - conda info -a + - conda install python -c conda-forge + - conda install numpy -c conda-forge + - conda install matplotlib -c conda-forge + - conda install cmake -c conda-forge + - conda install xtensor -c conda-forge + - conda install pyxtensor -c conda-forge + # Install library + - cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX=%MINICONDA%\\LIBRARY -DCMAKE_BUILD_TYPE=RELEASE . + - nmake + - nmake install + - python setup.py build + - python setup.py install + +build_script: + # Run Python example + - python examples\python\match.py + # Compile and run C++ example + - cd examples\cpp + - cmake -G "NMake Makefiles" . + - nmake + - .\example diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a5cf368 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,101 @@ +language: cpp +dist: trusty +env: +matrix: + fast_finish: true + include: + - os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.9 + env: COMPILER=gcc GCC=4.9 + - os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-5 + env: COMPILER=gcc GCC=5 + - os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-6 + env: COMPILER=gcc GCC=6 + - os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-7 + env: COMPILER=gcc GCC=7 + - os: linux + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-trusty-6.0 + packages: + - clang-6.0 + env: COMPILER=clang CLANG=6.0 + - os: osx + osx_image: xcode8 + compiler: clang +env: + global: + - MINCONDA_VERSION="latest" + - MINCONDA_LINUX="Linux-x86_64" + - MINCONDA_OSX="MacOSX-x86_64" +before_install: + - | + # Configure build variables + if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + if [[ "$COMPILER" == "gcc" ]]; then + export CXX=g++-$GCC CC=gcc-$GCC; + fi + if [[ "$COMPILER" == "clang" ]]; then + export CXX=clang++-$CLANG CC=clang-$CLANG; + fi + elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then + export CXX=clang++ CC=clang; + fi +install: + # Set environment using Conda + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + MINCONDA_OS=$MINCONDA_LINUX; + elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then + MINCONDA_OS=$MINCONDA_OSX; + fi + - wget "http://repo.continuum.io/miniconda/Miniconda3-$MINCONDA_VERSION-$MINCONDA_OS.sh" -O miniconda.sh; + - bash miniconda.sh -b -p $HOME/miniconda + - export PATH="$HOME/miniconda/bin:$PATH" + - hash -r + - conda config --set always_yes yes --set changeps1 no + - conda update -q conda + - conda install python -c conda-forge + - conda install numpy -c conda-forge + - conda install matplotlib -c conda-forge + - conda install cmake -c conda-forge + - conda install xtensor -c conda-forge + - conda install pyxtensor -c conda-forge + # Install library + - cmake . + - sudo make install + - python setup.py build + - python setup.py install +script: + # Run Python example + - python examples/python/match.py + # Compile and run C++ example + - cd examples/cpp + - cmake . + - make + - ./example + diff --git a/CMakeLists.txt b/CMakeLists.txt index dd4fc3a..a4cfbcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,103 +1,101 @@ -# required to specify the c++ standard -cmake_minimum_required(VERSION 3.0) -# required for install -include(CMakePackageConfigHelpers) -include(GNUInstallDirs) +cmake_minimum_required(VERSION 3.0) -# project settings -# ---------------- +# Options +# ======= -# name project(cppcolormap) -# file that contains the version information -set(parse_version include/cppcolormap.h) +# Internals +# ========= + +set(CPPCOLORMAP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) + +# Version +# ======= + +file( + STRINGS "${CPPCOLORMAP_INCLUDE_DIR}/cppcolormap.h" cppcolormap_version_defines + REGEX "#define CPPCOLORMAP_VERSION_(MAJOR|MINOR|PATCH)") + +foreach(ver ${cppcolormap_version_defines}) + if(ver MATCHES "#define CPPCOLORMAP_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") + set(CPPCOLORMAP_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") + endif() +endforeach() + +set(CPPCOLORMAP_VERSION + ${CPPCOLORMAP_VERSION_MAJOR}.${CPPCOLORMAP_VERSION_MINOR}.${CPPCOLORMAP_VERSION_PATCH}) + +message(STATUS "Building cppcolormap v${CPPCOLORMAP_VERSION}") -# header files -set(headers include/cppcolormap.h) +# Build +# ===== -# automatically parse the version number -file(READ "${parse_version}" version) -string(REGEX MATCH "define[ \t]+CPPCOLORMAP_WORLD_VERSION[ \t]+([0-9]+)" _ "${version}") -set(world_version "${CMAKE_MATCH_1}") -string(REGEX MATCH "define[ \t]+CPPCOLORMAP_MAJOR_VERSION[ \t]+([0-9]+)" _ "${version}") -set(major_version "${CMAKE_MATCH_1}") -string(REGEX MATCH "define[ \t]+CPPCOLORMAP_MINOR_VERSION[ \t]+([0-9]+)" _ "${version}") -set(minor_version "${CMAKE_MATCH_1}") -set(CPPCOLORMAP_VERSION_NUMBER ${world_version}.${major_version}.${minor_version}) +set(CPPCOLORMAP_HEADERS + ${CPPCOLORMAP_INCLUDE_DIR}/cppcolormap.h) -# paths -# ----- +add_library(${PROJECT_NAME} INTERFACE) -set(CPPCOLORMAP_ROOT_DIR "${CMAKE_INSTALL_PREFIX}") -set(CPPCOLORMAP_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") -set(CMAKEPACKAGE_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}") -set(PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig") -set(fcmake "cppcolormapConfig.cmake") -set(fpkg "cppcolormap.pc") +target_include_directories( + ${PROJECT_NAME} INTERFACE + $ + $) -# options -# ------- +# Installation +# ============ -# configure pkg-config (default: on) -option(PKGCONFIG "Build pkg-config ${fpkg} file" ON) +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) -# disable pkg-config for native Windows builds -if(WIN32 OR CMAKE_HOST_SYSTEM_NAME MATCHES Windows) - option(PKGCONFIG "Build pkg-config ${fpkg} file" OFF) -endif() +install( + TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}-targets) -# C++ standard -# ------------ +export( + EXPORT ${PROJECT_NAME}-targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake") -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) -if(NOT CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD LESS 14) - set(CMAKE_CXX_STANDARD 14) -endif() +install( + FILES ${CPPCOLORMAP_HEADERS} + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) -# configure CMake -# --------------- +set(CPPCOLORMAP_CMAKECONFIG_INSTALL_DIR + "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE + STRING "install path for ${PROJECT_NAME}Config.cmake") configure_package_config_file( - ${CMAKE_CURRENT_SOURCE_DIR}/cppcolormapConfig.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/cppcolormapConfig.cmake - PATH_VARS CPPCOLORMAP_INCLUDE_DIR CPPCOLORMAP_ROOT_DIR - INSTALL_DESTINATION ${CMAKEPACKAGE_INSTALL_DIR} - NO_CHECK_REQUIRED_COMPONENTS_MACRO -) - -# install -# ------- - -# pkg-config -if(PKGCONFIG) - configure_file(${fpkg}.in ${fpkg} @ONLY) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${fpkg} DESTINATION ${PKGCONFIG_INSTALL_DIR}) -endif() - -# CMake -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${fcmake} DESTINATION ${CMAKEPACKAGE_INSTALL_DIR}) - -# header files -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${headers} DESTINATION ${INCLUDE_INSTALL_DIR}) - -# print information to screen -# --------------------------- - -message(STATUS "") -message(STATUS "+---------------------------------------------------------------------------------") -message(STATUS "|") -message(STATUS "| Use 'make install' to install in") -message(STATUS "| ${CMAKE_INSTALL_PREFIX}") -message(STATUS "|") -message(STATUS "| To specify a custom directory call") -message(STATUS "| cmake /path/to/${PROJECT_NAME} -DCMAKE_INSTALL_PREFIX=yourprefix") -message(STATUS "|") -message(STATUS "| For custom paths, add the following line to your '~/.bashrc'") -message(STATUS "| export PKG_CONFIG_PATH=${CMAKE_INSTALL_PREFIX}/share/pkgconfig:$PKG_CONFIG_PATH") -message(STATUS "|") -message(STATUS "+---------------------------------------------------------------------------------") -message(STATUS "") + "cppcolormapConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION ${CPPCOLORMAP_CMAKECONFIG_INSTALL_DIR}) + +set(_CPPCOLORMAP_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) + +unset(CMAKE_SIZEOF_VOID_P) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${CPPCOLORMAP_VERSION} + COMPATIBILITY AnyNewerVersion) + +set(CMAKE_SIZEOF_VOID_P ${_CPPCOLORMAP_CMAKE_SIZEOF_VOID_P}) + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION ${CPPCOLORMAP_CMAKECONFIG_INSTALL_DIR}) + +install( + EXPORT ${PROJECT_NAME}-targets + FILE "${PROJECT_NAME}Targets.cmake" + DESTINATION ${CPPCOLORMAP_CMAKECONFIG_INSTALL_DIR}) + +configure_file( + "cppcolormap.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + @ONLY) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/") diff --git a/README.md b/README.md index 9e272ed..510cbfa 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,11 @@ # cppcolormap -Library with colormaps for C++. Quick start: `#include `, that's about it. Note that the library depends on [xtensor](http://xtensor.readthedocs.io). Its installation and use are equally straightforward. +[![Travis](https://travis-ci.org/tdegeus/cppcolormap.svg?branch=master)](https://travis-ci.org/tdegeus/cppcolormap) +[![Appveyor](https://ci.appveyor.com/api/projects/status/lmrkifr8q9vledv5?svg=true)](https://ci.appveyor.com/project/tdegeus/cppcolormap) > **Disclaimer** > -> This library is free to use under the [GPLv3 license](https://github.com/tdegeus/cppcolormap/blob/master/LICENSE). Any additions are very much appreciated, in terms of suggested functionality, code, documentation, testimonials, word of mouth advertisement, .... Bug reports or feature requests can be filed on [GitHub](https://github.com/tdegeus/cppcolormap). As always, the code comes with no guarantee. None of the developers can be held responsible for possible mistakes. +> This library is free to use under the [GPLv3 license](https://github.com/tdegeus/cppcolormap/blob/master/LICENSE). Any additions are very much appreciated, in terms of suggested functionality, code, documentation, testimonials, word-of-mouth advertisement, etc. Bug reports or feature requests can be filed on [GitHub](https://github.com/tdegeus/cppcolormap). As always, the code comes with no guarantee. None of the developers can be held responsible for possible mistakes. > > Download: [.zip file](https://github.com/tdegeus/cppcolormap/zipball/master) | [.tar.gz file](https://github.com/tdegeus/cppcolormap/tarball/master). > @@ -18,12 +19,19 @@ Library with colormaps for C++. Quick start: `#include `, that's +- [Introduction](#introduction) - [Usage from C++](#usage-from-c) - - [Installation](#installation) + - [Getting cppcolormap](#getting-cppcolormap) + - [Using conda](#using-conda) + - [From source](#from-source) - [Usage](#usage) - [Find match](#find-match) + - [Compiling](#compiling) + - [Using `CMakeLists.txt`](#using-cmakeliststxt) - [Usage from Python](#usage-from-python) - - [Installation](#installation-1) + - [Getting cppcolormap](#getting-cppcolormap-1) + - [Using conda](#using-conda-1) + - [From source](#from-source-1) - [Usage](#usage-1) - [Find match](#find-match-1) - [Example](#example) @@ -37,37 +45,42 @@ Library with colormaps for C++. Quick start: `#include `, that's -# Usage from C++ +# Introduction -## Installation +Library with colormaps for C++/Python. -The library is header only. This means that one has to only include the header-file `cppcolormap.h`. Really, that's it! +# Usage from C++ -To be able to set the include path semi-automatic, one can choose to 'install' cppcolormap. To do this using CMake: +## Getting cppcolormap -1. Proceed to a (temporary) build directory. For example +### Using conda - ```bash - $ cd /path/to/cppcolormap/build - ``` +```bash +conda install -c conda-forge cppcolormap +``` -2. 'Build' cppcolormap +### From source - ```bash - $ cmake .. - $ make install - ``` +```bash +# Download cppcolormap +git checkout https://github.com/tdegeus/cppcolormap.git +cd cppcolormap + +# For CMake or pkg-config use +cmake . +make install +``` ## Usage -The main interface is with two functions: +The principle interface is with these two functions: ```cpp #include int main() { - std::cout << cppcolormap::colormap("Reds") << std::endl; + std::cout << cppcolormap::colormap("Reds") << std::endl; std::cout << cppcolormap::colorcycle("tue") << std::endl; return 0; @@ -83,7 +96,7 @@ The colormaps are stored as a matrix whereby each row contains the (R,G,B) color int main() { - std::cout << cppcolormap::colormap("Reds", 256) << std::endl; + std::cout << cppcolormap::colormap("Reds", 256) << std::endl; return 0; } @@ -96,9 +109,9 @@ Note that the colorcycles are not interpolatable. Consequently the functions do int main() { - std::cout << cppcolormap::Reds() << std::endl; + std::cout << cppcolormap::Reds() << std::endl; std::cout << cppcolormap::Reds(256) << std::endl; - std::cout << cppcolormap::tue() << std::endl; + std::cout << cppcolormap::tue() << std::endl; return 0; } @@ -119,25 +132,63 @@ The following metrics can be used: * fast_perceptual * perceptual +## Compiling + +### Using `CMakeLists.txt` + +Using *cppcolormap* the `CMakeLists.txt` can be as follows + +```cmake +cmake_minimum_required(VERSION 3.1) + +project(example) + +find_package(xtl REQUIRED) +find_package(xtensor REQUIRED) +find_package(cppcolormap REQUIRED) + +add_executable(example example.cpp) + +target_link_libraries(example + PRIVATE + xtensor + cppcolormap) +``` + +[download "CMakeLists.txt"](./example/CMakeLists.txt) + +Compilation can then proceed using + +```bash +cmake . +make +``` + # Usage from Python -## Installation +## Getting cppcolormap -Clone the repository and then run: +### Using conda ```bash -# if you are using Python 2.x +conda install -c conda-forge cppcolormap +``` + +### From source + +```bash +# Download cppcolormap +git checkout https://github.com/tdegeus/cppcolormap.git +cd cppcolormap + +# Compile & build python setup.py build python setup.py install - -# if you are using Python 3.x -python3 setup.py build -python3 setup.py install ``` ## Usage -There are two functions, each returns a 2-d NumPy array: +There are two principle functions, each returns a 2-d NumPy array: ```python import cppcolormap as cm @@ -146,8 +197,8 @@ import cppcolormap as cm N = 256 # specify the colormap as string -cols = cm.colormap("Reds",N) -cols = cm.colorcycle("tue",N) +cols = cm.colormap("Reds", N) +cols = cm.colorcycle("tue", N) # or call the functions directly cols = cm.Reds(N) @@ -162,7 +213,7 @@ To find the closest match of each color of a colormap in another colormap you ca ```cpp idx = cm.match(cmap1, cmap2) -idx = cm.match(cmap1, cmap2, cm.DistanceMetric.perceptual) +idx = cm.match(cmap1, cmap2, cm.metric.perceptual) ``` (See metrics above.) diff --git a/cppcolormap.pc.in b/cppcolormap.pc.in index 0af9b82..965ec88 100644 --- a/cppcolormap.pc.in +++ b/cppcolormap.pc.in @@ -1,9 +1,7 @@ prefix=@CMAKE_INSTALL_PREFIX@ -exec_prefix=${prefix} +includedir=${prefix}/include -Name: cppcolormap -Description: Library that contains many colormaps from different sources -Requires: -Version: @CPPCOLORMAP_VERSION_NUMBER@ -Libs: -Cflags: -I${prefix}/@INCLUDE_INSTALL_DIR@ +Name: @PROJECT_NAME@ +Description: colormaps for C++ +Version: @PCPPCOLORMAP_VERSION@ +Cflags: -I${includedir} diff --git a/cppcolormapConfig.cmake.in b/cppcolormapConfig.cmake.in index 875d532..db855d7 100644 --- a/cppcolormapConfig.cmake.in +++ b/cppcolormapConfig.cmake.in @@ -1,4 +1,13 @@ -set(CPPCOLORMAP_FOUND 1) +# cppcolormap cmake module +# This module sets the following variables: +# +# cppcolormap_FOUND - true if cppcolormap found +# cppcolormap_INCLUDE_DIRS - the directory containing cppcolormap headers +# cppcolormap_LIBRARY - empty -set(CPPCOLORMAP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) -set(CPPCOLORMAP_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) +@PACKAGE_INIT@ + +if(NOT TARGET @PROJECT_NAME@) + include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") + get_target_property(@PROJECT_NAME@_INCLUDE_DIRS @PROJECT_NAME@ INTERFACE_INCLUDE_DIRECTORIES) +endif() diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt new file mode 100644 index 0000000..9165a34 --- /dev/null +++ b/examples/cpp/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.1) + +project(example) + +find_package(xtl REQUIRED) +find_package(xtensor REQUIRED) +find_package(cppcolormap REQUIRED) + +add_executable(example match.cpp) + +if(MSVC) + target_compile_options(example PRIVATE /EHsc /MP /bigobj) + set(CMAKE_EXE_LINKER_FLAGS /MANIFEST:NO) +endif() + +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR + CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR + (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT WIN32)) + target_compile_options(example PRIVATE -march=native -std=c++14) +endif() + +target_link_libraries(example + PRIVATE + xtensor + cppcolormap) diff --git a/examples/cpp/match.cpp b/examples/cpp/match.cpp new file mode 100644 index 0000000..b991a8d --- /dev/null +++ b/examples/cpp/match.cpp @@ -0,0 +1,14 @@ +#include + +int main() +{ + if (!CPPCOLORMAP_VERSION(0,2,1)) + std::cout << "Problems!" << std::endl; + + auto cmap = cppcolormap::Reds(); + auto xterm = cppcolormap::xterm(); + auto idx = cppcolormap::match(cmap, xterm, cppcolormap::metric::perceptual); + auto cmap_as_xterm = xt::view(xterm, xt::keep(idx), xt::all()); + + return 0; +} diff --git a/examples/match.py b/examples/match.py deleted file mode 100644 index 829af29..0000000 --- a/examples/match.py +++ /dev/null @@ -1,22 +0,0 @@ -import cppcolormap as cm -import matplotlib as mpl -import matplotlib.pyplot as plt -import numpy as np - -cmap = cm.Reds() -m_cmap = mpl.colors.ListedColormap(cmap, name='Reds', N=cmap.shape[0]) - -xterm = cm.xterm() -idx = cm.match(cmap, xterm, cm.DistanceMetric.perceptual) -m_xterm = mpl.colors.ListedColormap(xterm[idx,:], name='xterm', N=cmap.shape[0]) - -fig, axes = plt.subplots(figsize=(16,8), ncols=2) - -x,y = np.meshgrid(np.linspace(0,99,100),np.linspace(0,99,100)) -z = (x-50)**2. + (y-50)**2. - -im = axes[0].imshow(z, cmap=m_cmap , clim=(0,5000)) -im = axes[1].imshow(z, cmap=m_xterm, clim=(0,5000)) - -plt.show() - diff --git a/examples/python/match.py b/examples/python/match.py new file mode 100644 index 0000000..46c7404 --- /dev/null +++ b/examples/python/match.py @@ -0,0 +1,26 @@ +import cppcolormap as cm +import matplotlib as mpl +import matplotlib.pyplot as plt +import numpy as np + +cmap = cm.Reds() +m_cmap = mpl.colors.ListedColormap(cmap, name='Reds', N=cmap.shape[0]) + +xterm = cm.xterm() +idx = cm.match(cmap, xterm, cm.metric.perceptual) +m_xterm = mpl.colors.ListedColormap(xterm[idx,:], name='xterm', N=cmap.shape[0]) + +fig, axes = plt.subplots(figsize=(16,8), ncols=2) + +x, y = np.meshgrid( + np.linspace(0, 99, 100), + np.linspace(0, 99, 100)) + +z = (x - 50)**2.0 + (y - 50)**2.0 + +im = axes[0].imshow(z, cmap=m_cmap, clim=(0, 5000)) +im = axes[1].imshow(z, cmap=m_xterm, clim=(0, 5000)) + +plt.savefig('match.pdf') +plt.close() + diff --git a/include/cppcolormap.h b/include/cppcolormap.h index 12da9e9..f3c2fbf 100644 --- a/include/cppcolormap.h +++ b/include/cppcolormap.h @@ -7,19 +7,19 @@ #ifndef CPPCOLORMAP_H #define CPPCOLORMAP_H -#define CPPCOLORMAP_WORLD_VERSION 0 -#define CPPCOLORMAP_MAJOR_VERSION 2 -#define CPPCOLORMAP_MINOR_VERSION 0 +#define CPPCOLORMAP_VERSION_MAJOR 0 +#define CPPCOLORMAP_VERSION_MINOR 2 +#define CPPCOLORMAP_VERSION_PATCH 1 #define CPPCOLORMAP_VERSION_AT_LEAST(x,y,z) \ - (CPPCOLORMAP_WORLD_VERSION>x || (CPPCOLORMAP_WORLD_VERSION>=x && \ - (CPPCOLORMAP_MAJOR_VERSION>y || (CPPCOLORMAP_MAJOR_VERSION>=y && \ - CPPCOLORMAP_MINOR_VERSION>=z)))) + (CPPCOLORMAP_VERSION_MAJOR>x || (CPPCOLORMAP_VERSION_MAJOR>=x && \ + (CPPCOLORMAP_VERSION_MINOR>y || (CPPCOLORMAP_VERSION_MINOR>=y && \ + CPPCOLORMAP_VERSION_PATCH>=z)))) #define CPPCOLORMAP_VERSION(x,y,z) \ - (CPPCOLORMAP_WORLD_VERSION==x && \ - CPPCOLORMAP_MAJOR_VERSION==y && \ - CPPCOLORMAP_MINOR_VERSION==z) + (CPPCOLORMAP_VERSION_MAJOR==x && \ + CPPCOLORMAP_VERSION_MINOR==y && \ + CPPCOLORMAP_VERSION_PATCH==z) #include #include diff --git a/include/python.cpp b/python/main.cpp similarity index 99% rename from include/python.cpp rename to python/main.cpp index 950c987..3fb930b 100644 --- a/include/python.cpp +++ b/python/main.cpp @@ -129,7 +129,7 @@ m.def("colormap" ,&cppcolormap::colormap ,"Colormap by name" ,py::arg("cmap" // ------------------------------------------------------------------------------------------------- -py::enum_(m, "DistanceMetric", "Distance metric for color matching") +py::enum_(m, "metric", "Distance metric for color matching") .value("euclidean" , cppcolormap::metric::euclidean ) .value("fast_perceptual", cppcolormap::metric::fast_perceptual) .value("perceptual" , cppcolormap::metric::perceptual ) diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 2a9acf1..0000000 --- a/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/setup.py b/setup.py index 3eb1b05..f3599c8 100644 --- a/setup.py +++ b/setup.py @@ -1,49 +1,43 @@ -desc = ''' -GooseFEM is a C++ module, wrapped in Python, that provides several predefined finite element meshes. -The original C++ module also includes element definitions and several standard finite element -simulations. -''' from setuptools import setup, Extension -import sys, re -import setuptools +import re import pybind11 import pyxtensor -header = open('./include/cppcolormap.h','r').read() -world = re.split(r'(.*)(\#define CPPCOLORMAP_WORLD_VERSION\ )([0-9]+)(.*)',header)[3] -major = re.split(r'(.*)(\#define CPPCOLORMAP_MAJOR_VERSION\ )([0-9]+)(.*)',header)[3] -minor = re.split(r'(.*)(\#define CPPCOLORMAP_MINOR_VERSION\ )([0-9]+)(.*)',header)[3] +header = open('./include/cppcolormap.h', 'r').read() +major = re.split(r'(.*)(\#define CPPCOLORMAP_VERSION_MAJOR\ )([0-9]+)(.*)', header)[3] +minor = re.split(r'(.*)(\#define CPPCOLORMAP_VERSION_MINOR\ )([0-9]+)(.*)', header)[3] +patch = re.split(r'(.*)(\#define CPPCOLORMAP_VERSION_PATCH\ )([0-9]+)(.*)', header)[3] -__version__ = '.'.join([world,major,minor]) +__version__ = '.'.join([major, minor, patch]) ext_modules = [ Extension( 'cppcolormap', - ['include/python.cpp'], + ['python/main.cpp'], include_dirs=[ - pybind11 .get_include(False), - pybind11 .get_include(True ), + pybind11.get_include(False), + pybind11.get_include(True), pyxtensor.get_include(False), - pyxtensor.get_include(True ), + pyxtensor.get_include(True), ], language='c++' ), ] setup( - name = 'cppcolormap', - description = 'Library with colormaps', - long_description = desc, - keywords = 'colormap, plot, matplotlib', - version = __version__, - license = 'MIT', - author = 'Tom de Geus', - author_email = 'tom@geus.me', - url = 'https://github.com/tdegeus/cppcolormap', - ext_modules = ext_modules, - install_requires = ['pybind11>=2.2.0','pyxtensor>=0.0.1'], - cmdclass = {'build_ext': pyxtensor.BuildExt}, - zip_safe = False, + name = 'cppcolormap', + description = 'Library with colormaps', + long_description = 'Library with colormaps', + keywords = 'colormap, plot, matplotlib', + version = __version__, + license = 'MIT', + author = 'Tom de Geus', + author_email = 'tom@geus.me', + url = 'https://github.com/tdegeus/cppcolormap', + ext_modules = ext_modules, + install_requires = ['pybind11>=2.2.0', 'pyxtensor>=0.0.1'], + cmdclass = {'build_ext': pyxtensor.BuildExt}, + zip_safe = False, )