├── .gitignore ├── banner.png ├── test ├── catch.cpp ├── mirror_x.cpp ├── mirror_y.cpp ├── shift_x.cpp ├── shift_y.cpp ├── replicate.cpp ├── select_disk.cpp ├── merge.cpp ├── hash.cpp ├── select_rectangle.cpp ├── convert.cpp ├── mask_isolated.cpp ├── stitch.cpp ├── average_position.cpp ├── compute_activity.cpp ├── mask_redundant.cpp ├── track_blob.cpp ├── compute_flow.cpp ├── average_grid.cpp ├── compute_time_surface.cpp └── track_blob_multi.cpp ├── .gitmodules ├── source ├── mirror_x.hpp ├── mirror_y.hpp ├── shift_x.hpp ├── shift_y.hpp ├── select_disk.hpp ├── convert.hpp ├── mask_redundant.hpp ├── select_rectangle.hpp ├── replicate.hpp ├── mask_isolated.hpp ├── average_position.hpp ├── average_grid.hpp ├── compute_activity.hpp ├── stitch.hpp ├── track_blob.hpp ├── compute_time_surface.hpp ├── hash.hpp ├── track_blob_multi.hpp ├── compute_flow.hpp └── merge.hpp ├── premake4.lua ├── .travis.yml ├── README.md ├── .clang-format ├── template.lua └── LICENSE.txt /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .DS_STORE 3 | -------------------------------------------------------------------------------- /banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/neuromorphic-paris/tarsier/HEAD/banner.png -------------------------------------------------------------------------------- /test/catch.cpp: -------------------------------------------------------------------------------- 1 | #define CATCH_CONFIG_MAIN 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/Catch2"] 2 | path = third_party/Catch2 3 | url = https://github.com/catchorg/Catch2.git 4 | [submodule "third_party/json.lua"] 5 | path = third_party/json.lua 6 | url = https://github.com/rxi/json.lua.git 7 | -------------------------------------------------------------------------------- /test/mirror_x.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/mirror_x.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | }; 7 | 8 | TEST_CASE("Invert the x coordinate", "[mirror_x]") { 9 | auto mirror_x = tarsier::make_mirror_x(320, [](event event) -> void { REQUIRE(event.x == 100); }); 10 | mirror_x(event{219}); 11 | } 12 | -------------------------------------------------------------------------------- /test/mirror_y.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/mirror_y.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t y; 6 | }; 7 | 8 | TEST_CASE("Invert the y coordinate", "[mirror_y]") { 9 | auto mirror_y = tarsier::make_mirror_y(240, [](event event) -> void { REQUIRE(event.y == 100); }); 10 | mirror_y(event{139}); 11 | } 12 | -------------------------------------------------------------------------------- /test/shift_x.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/shift_x.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t x; 6 | }; 7 | 8 | TEST_CASE("Shift the x coordinate", "[shift_x]") { 9 | auto shift_x = tarsier::make_shift_x(320, 10, [](event event) -> void { REQUIRE(event.x == 210); }); 10 | shift_x(event{315}); 11 | shift_x(event{200}); 12 | } 13 | -------------------------------------------------------------------------------- /test/shift_y.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/shift_y.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t y; 6 | }; 7 | 8 | TEST_CASE("Shift the y coordinate", "[shift_y]") { 9 | auto shift_y = tarsier::make_shift_y(240, 10, [](event event) -> void { REQUIRE(event.y == 145); }); 10 | shift_y(event{235}); 11 | shift_y(event{135}); 12 | } 13 | -------------------------------------------------------------------------------- /test/replicate.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/replicate.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event {}; 5 | 6 | TEST_CASE("Replicate an event and trigger several callbacks", "[replicate]") { 7 | std::size_t count = 0; 8 | auto replicate = 9 | tarsier::make_replicate([&count](event) { ++count; }, [&](event) { ++count; }, [&](event) { ++count; }); 10 | replicate(event{}); 11 | REQUIRE(count == 3); 12 | } 13 | -------------------------------------------------------------------------------- /test/select_disk.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/select_disk.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | uint16_t y; 7 | }; 8 | 9 | TEST_CASE("Filter out events outside the disk", "[select_disk]") { 10 | auto select_disk = 11 | tarsier::make_select_disk(100, 100, 20.0, [](event event) -> void { REQUIRE(event.x == 100); }); 12 | select_disk(event{200, 200}); 13 | select_disk(event{100, 110}); 14 | } 15 | -------------------------------------------------------------------------------- /test/merge.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/merge.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t t; 6 | }; 7 | 8 | TEST_CASE("Merge two streams", "[merge]") { 9 | std::size_t index = 0; 10 | auto merge = tarsier::make_merge<2, event>(256, std::chrono::milliseconds(20), [&](event event) -> void { 11 | REQUIRE(event.t == index); 12 | ++index; 13 | }); 14 | merge->push<0>(event{1}); 15 | merge->push<1>(event{0}); 16 | } 17 | -------------------------------------------------------------------------------- /test/hash.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/hash.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | #include 4 | 5 | TEST_CASE("Hash a list of numbers", "[hash]") { 6 | auto hash = tarsier::make_hash([](std::pair hash) -> void { 7 | REQUIRE(std::get<0>(hash) == 0xb06f9999c14051caull); 8 | REQUIRE(std::get<1>(hash) == 0x0fbd6d93c8340799ull); 9 | }); 10 | for (uint8_t index = 0; index < 100; ++index) { 11 | hash(index); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/select_rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/select_rectangle.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | uint16_t y; 7 | }; 8 | 9 | TEST_CASE("Filter out events outside the rectangle", "[select_rectangle]") { 10 | auto select_rectangle = 11 | tarsier::make_select_rectangle(50, 50, 204, 140, [](event event) -> void { REQUIRE(event.x == 100); }); 12 | select_rectangle(event{300, 200}); 13 | select_rectangle(event{100, 100}); 14 | } 15 | -------------------------------------------------------------------------------- /test/convert.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/convert.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | bool is_increase; 6 | }; 7 | 8 | struct converted_event { 9 | bool polarity; 10 | }; 11 | 12 | TEST_CASE("Convert an event type to another", "[convert]") { 13 | auto convert = tarsier::make_convert( 14 | [](event event) -> converted_event { return {event.is_increase}; }, 15 | [](converted_event converted_event) -> void { REQUIRE(converted_event.polarity); }); 16 | convert(event{true}); 17 | } 18 | -------------------------------------------------------------------------------- /test/mask_isolated.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/mask_isolated.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t t; 6 | uint16_t x; 7 | uint16_t y; 8 | }; 9 | 10 | TEST_CASE("Filter out events with low spatial or temporal activity", "[mask_isolated]") { 11 | auto mask_isolated = 12 | tarsier::make_mask_isolated(320, 240, 10, [](event event) -> void { REQUIRE(event.x == 100); }); 13 | mask_isolated(event{0, 200, 200}); 14 | mask_isolated(event{1, 200, 202}); 15 | mask_isolated(event{20, 200, 201}); 16 | mask_isolated(event{40, 100, 100}); 17 | mask_isolated(event{41, 100, 101}); 18 | } 19 | -------------------------------------------------------------------------------- /test/stitch.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/stitch.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct threshold_crossing { 5 | uint64_t t; 6 | uint16_t x; 7 | uint16_t y; 8 | bool is_second; 9 | }; 10 | 11 | struct event { 12 | uint16_t x; 13 | uint16_t y; 14 | uint64_t delta_t; 15 | }; 16 | 17 | TEST_CASE("Stitch an threshold crossings stream", "[stitch]") { 18 | auto stitch = tarsier::make_stitch( 19 | 320, 20 | 240, 21 | [](threshold_crossing threshold_crossing, uint64_t delta_t) -> event { 22 | return {threshold_crossing.x, threshold_crossing.y, delta_t}; 23 | }, 24 | [](event event) -> void { REQUIRE(event.delta_t == 200); }); 25 | stitch(threshold_crossing{0, 200, 100, false}); 26 | stitch(threshold_crossing{100, 200, 0, false}); 27 | stitch(threshold_crossing{200, 200, 100, true}); 28 | } 29 | -------------------------------------------------------------------------------- /test/average_position.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/average_position.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | uint16_t y; 7 | }; 8 | 9 | struct position { 10 | float x; 11 | float y; 12 | }; 13 | 14 | TEST_CASE("Average the position of the given events", "[average_position]") { 15 | auto first_received = false; 16 | auto average_position = tarsier::make_average_position( 17 | 0.0, 18 | 0.0, 19 | 0.5, 20 | [](event event, float x, float y) -> position { 21 | return {x, y}; 22 | }, 23 | [&](position position) -> void { 24 | if (first_received) { 25 | REQUIRE(position.x == 100); 26 | REQUIRE(position.y == 50); 27 | } else { 28 | first_received = true; 29 | } 30 | }); 31 | average_position(event{0, 0}); 32 | average_position(event{200, 100}); 33 | } 34 | -------------------------------------------------------------------------------- /test/compute_activity.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/compute_activity.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t t; 6 | uint16_t x; 7 | uint16_t y; 8 | }; 9 | 10 | struct activity { 11 | uint64_t t; 12 | uint16_t x; 13 | uint16_t y; 14 | float potential; 15 | }; 16 | 17 | TEST_CASE("compute the activity from the given events", "[compute_activity]") { 18 | std::vector expected_potentials{1.0f, 1.9999000049998332f, 1.0f, 1.9999000049998332f, 1.0000908225624412f}; 19 | std::size_t index = 0; 20 | auto compute_activity = tarsier::make_compute_activity( 21 | 320, 22 | 240, 23 | 10000, 24 | [](event event, float potential) -> activity { 25 | return {event.t, event.x, event.y, potential}; 26 | }, 27 | [&](activity activity) -> void { 28 | REQUIRE(std::abs(activity.potential - expected_potentials[index]) / expected_potentials[index] < 1e-3); 29 | ++index; 30 | }); 31 | compute_activity(event{100000, 100, 100}); 32 | compute_activity(event{100001, 100, 100}); 33 | compute_activity(event{100002, 101, 100}); 34 | compute_activity(event{100003, 101, 100}); 35 | compute_activity(event{200000, 101, 100}); 36 | } 37 | -------------------------------------------------------------------------------- /test/mask_redundant.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/mask_redundant.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | uint16_t y; 7 | uint64_t t; 8 | bool is_increase; 9 | }; 10 | 11 | TEST_CASE("Filter successive similar events close in time", "[mask_redundant]") { 12 | uint16_t n_event_output = 0; 13 | auto mask_redundant = tarsier::make_mask_redundant(320, 240, 50, [&](event event) -> void { 14 | switch (n_event_output) { 15 | case 0: 16 | REQUIRE(event.t == 100); 17 | break; 18 | case 1: 19 | REQUIRE(event.t == 140); 20 | break; 21 | case 2: 22 | REQUIRE(event.t == 200); 23 | break; 24 | case 3: 25 | REQUIRE(event.t == 210); 26 | break; 27 | default: 28 | throw std::logic_error("Unexpected value"); 29 | } 30 | n_event_output++; 31 | }); 32 | mask_redundant(event{101, 51, 100, true}); 33 | mask_redundant(event{101, 51, 120, true}); 34 | mask_redundant(event{101, 52, 140, true}); 35 | mask_redundant(event{201, 21, 200, true}); 36 | mask_redundant(event{201, 21, 210, false}); 37 | REQUIRE(n_event_output == 4); 38 | } 39 | -------------------------------------------------------------------------------- /source/mirror_x.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | 9 | /// mirror_x inverts the x coordinate. 10 | template 11 | class mirror_x { 12 | public: 13 | mirror_x(uint16_t width, HandleEvent&& handle_event) : 14 | _width(width), 15 | _handle_event(std::forward(handle_event)) {} 16 | mirror_x(const mirror_x&) = delete; 17 | mirror_x(mirror_x&&) = default; 18 | mirror_x& operator=(const mirror_x&) = delete; 19 | mirror_x& operator=(mirror_x&&) = default; 20 | virtual ~mirror_x() = default; 21 | 22 | /// operator() handles an event. 23 | virtual void operator()(Event event) { 24 | event.x = _width - 1 - event.x; 25 | _handle_event(event); 26 | } 27 | 28 | protected: 29 | const uint16_t _width; 30 | HandleEvent _handle_event; 31 | }; 32 | 33 | /// make_mirror_x creates a mirror_x from a functor. 34 | template 35 | inline mirror_x make_mirror_x(uint16_t width, HandleEvent&& handle_event) { 36 | return mirror_x(width, std::forward(handle_event)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /source/mirror_y.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | 9 | /// mirror_y inverts the y coordinate. 10 | template 11 | class mirror_y { 12 | public: 13 | mirror_y(uint16_t height, HandleEvent&& handle_event) : 14 | _height(height), 15 | _handle_event(std::forward(handle_event)) {} 16 | mirror_y(const mirror_y&) = delete; 17 | mirror_y(mirror_y&&) = default; 18 | mirror_y& operator=(const mirror_y&) = delete; 19 | mirror_y& operator=(mirror_y&&) = default; 20 | virtual ~mirror_y() = default; 21 | 22 | /// operator() handles an event. 23 | virtual void operator()(Event event) { 24 | event.y = _height - 1 - event.y; 25 | _handle_event(event); 26 | } 27 | 28 | protected: 29 | const uint16_t _height; 30 | HandleEvent _handle_event; 31 | }; 32 | 33 | /// make_mirror_y creates a mirror_y from a functor. 34 | template 35 | inline mirror_y make_mirror_y(uint16_t height, HandleEvent&& handle_event) { 36 | return mirror_y(height, std::forward(handle_event)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /premake4.lua: -------------------------------------------------------------------------------- 1 | local template = require 'template' 2 | 3 | newaction { 4 | trigger = 'template', 5 | description = 'generate a template event handler', 6 | execute = function() 7 | if _OPTIONS['configuration'] == nil then 8 | template('configuration.json') 9 | else 10 | template(_OPTIONS['configuration']) 11 | end 12 | end 13 | } 14 | newoption { 15 | trigger = 'configuration', 16 | value = '/path/to/configuration.json', 17 | description = 'set the path to the configuration file for template generation' 18 | } 19 | 20 | solution 'tarsier' 21 | configurations {'release', 'debug'} 22 | location 'build' 23 | project 'tarsier' 24 | kind 'ConsoleApp' 25 | language 'C++' 26 | location 'build' 27 | files {'source/*.hpp', 'test/*.cpp'} 28 | configuration 'release' 29 | targetdir 'build/release' 30 | defines {'NDEBUG'} 31 | flags {'OptimizeSpeed'} 32 | configuration 'debug' 33 | targetdir 'build/debug' 34 | defines {'DEBUG'} 35 | flags {'Symbols'} 36 | configuration 'linux' 37 | links {'pthread'} 38 | buildoptions {'-std=c++11'} 39 | linkoptions {'-std=c++11'} 40 | configuration 'macosx' 41 | buildoptions {'-std=c++11'} 42 | linkoptions {'-std=c++11'} 43 | configuration 'windows' 44 | files {'.clang-format'} 45 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | matrix: 4 | include: 5 | - os: linux 6 | dist: trusty 7 | compiler: gcc 8 | sudo: required 9 | - os: osx 10 | osx_image: xcode9.3 11 | compiler: gcc 12 | - os: osx 13 | osx_image: xcode9.3 14 | compiler: clang 15 | 16 | before_install: 17 | - | 18 | if [ "$TRAVIS_OS_NAME" = 'osx' ]; then 19 | brew update 20 | brew install premake 21 | brew install clang-format 22 | fi 23 | - | 24 | if [ "$TRAVIS_OS_NAME" = 'linux' ]; then 25 | sudo apt-get -qq update -y 26 | sudo apt-get install premake4 -y 27 | sudo apt-get install clang-format -y 28 | sudo ln -s /usr/bin/make /usr/bin/gmake 29 | fi 30 | 31 | script: 32 | - | 33 | for filename in source/*.hpp test/*.cpp; do 34 | formatted_filename="$(dirname $filename)/formatted_$(basename $filename)" 35 | clang-format $filename > $formatted_filename 36 | if [ "$(diff $filename $formatted_filename)" != '' ]; then 37 | printf "'$filename' is not properly formatted, run \`clang-format -i $filename\`\n" 38 | printf "$(diff $filename $formatted_filename)\n" 39 | fi 40 | rm $formatted_filename 41 | done 42 | - premake4 gmake || travis_terminate 1 43 | - cd build || travis_terminate 1 44 | - make || travis_terminate 1 45 | - release/tarsier 46 | -------------------------------------------------------------------------------- /source/shift_x.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | 9 | /// shift_x translates the x coordinate. 10 | template 11 | class shift_x { 12 | public: 13 | shift_x(uint16_t width, int32_t shift, HandleEvent&& handle_event) : 14 | _width(width), 15 | _shift(shift), 16 | _handle_event(std::forward(handle_event)) {} 17 | shift_x(const shift_x&) = delete; 18 | shift_x(shift_x&&) = default; 19 | shift_x& operator=(const shift_x&) = delete; 20 | shift_x& operator=(shift_x&&) = default; 21 | virtual ~shift_x() = default; 22 | 23 | /// operator() handles an event. 24 | virtual void operator()(Event event) { 25 | const auto shifted = static_cast(event.x) + _shift; 26 | if (shifted >= 0 && shifted < _width) { 27 | event.x = shifted; 28 | _handle_event(event); 29 | } 30 | } 31 | 32 | protected: 33 | const uint16_t _width; 34 | const int32_t _shift; 35 | HandleEvent _handle_event; 36 | }; 37 | 38 | /// make_shift_x creates a shift_x from a functor. 39 | template 40 | inline shift_x make_shift_x(uint16_t width, int32_t shift, HandleEvent&& handle_event) { 41 | return shift_x(width, shift, std::forward(handle_event)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /source/shift_y.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | 9 | /// shift_y translates the x coordinate. 10 | template 11 | class shift_y { 12 | public: 13 | shift_y(uint16_t height, int32_t shift, HandleEvent&& handle_event) : 14 | _height(height), 15 | _shift(shift), 16 | _handle_event(std::forward(handle_event)) {} 17 | shift_y(const shift_y&) = delete; 18 | shift_y(shift_y&&) = default; 19 | shift_y& operator=(const shift_y&) = delete; 20 | shift_y& operator=(shift_y&&) = default; 21 | virtual ~shift_y() = default; 22 | 23 | /// operator() handles an event. 24 | virtual void operator()(Event event) { 25 | const auto shifted = static_cast(event.y) + _shift; 26 | if (shifted >= 0 && shifted < _height) { 27 | event.y = shifted; 28 | _handle_event(event); 29 | } 30 | } 31 | 32 | protected: 33 | const uint16_t _height; 34 | const int32_t _shift; 35 | HandleEvent _handle_event; 36 | }; 37 | 38 | /// make_shift_y creates a shift_y from a functor. 39 | template 40 | inline shift_y make_shift_y(uint16_t height, int32_t shift, HandleEvent&& handle_event) { 41 | return shift_y(height, shift, std::forward(handle_event)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /source/select_disk.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | /// tarsier is a collection of event handlers. 6 | namespace tarsier { 7 | /// select_disk propagates only the events within the given disk. 8 | template 9 | class select_disk { 10 | public: 11 | select_disk(float x, float y, float radius, HandleEvent&& handle_event) : 12 | _x(x), 13 | _y(y), 14 | _squared_radius(radius * radius), 15 | _handle_event(std::forward(handle_event)) {} 16 | select_disk(const select_disk&) = delete; 17 | select_disk(select_disk&&) = default; 18 | select_disk& operator=(const select_disk&) = delete; 19 | select_disk& operator=(select_disk&&) = default; 20 | virtual ~select_disk() = default; 21 | 22 | /// operator() handles an event. 23 | virtual void operator()(Event event) { 24 | const auto x_delta = event.x - _x; 25 | const auto y_delta = event.y - _y; 26 | if (x_delta * x_delta + y_delta * y_delta < _squared_radius) { 27 | _handle_event(event); 28 | } 29 | } 30 | 31 | protected: 32 | const float _x; 33 | const float _y; 34 | const float _squared_radius; 35 | HandleEvent _handle_event; 36 | }; 37 | 38 | /// make_select_disk creates a select_disk from a functor. 39 | template 40 | inline select_disk 41 | make_select_disk(float x, float y, float radius, HandleEvent&& handle_event) { 42 | return select_disk(x, y, radius, std::forward(handle_event)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/track_blob.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/track_blob.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint16_t x; 6 | uint16_t y; 7 | }; 8 | 9 | struct blob { 10 | float x; 11 | float y; 12 | float sigma_x_squared; 13 | float sigma_xy; 14 | float sigma_y_squared; 15 | }; 16 | 17 | TEST_CASE("Average the events with a Gaussian blob", "[track_blob]") { 18 | blob expected_blob{0.2f, 0.1f, 50.0f, 30.0f, 10.0f}; 19 | auto first_received = false; 20 | auto track_blob = tarsier::make_track_blob( 21 | 0.0f, 22 | 0.0f, 23 | 10.0f, 24 | 10.0f, 25 | 0.0f, 26 | 0.999f, 27 | 0.999f, 28 | [](event event, float x, float y, float sigma_x_squared, float sigma_xy, float sigma_y_squared) -> blob { 29 | return {x, y, sigma_x_squared, sigma_xy, sigma_y_squared}; 30 | }, 31 | [&](blob blob) -> void { 32 | if (first_received) { 33 | REQUIRE(std::abs(blob.x - expected_blob.x) / expected_blob.x < 1e-3f); 34 | REQUIRE(std::abs(blob.y - expected_blob.y) / expected_blob.y < 1e-3f); 35 | REQUIRE( 36 | std::abs(blob.sigma_x_squared - expected_blob.sigma_x_squared) / expected_blob.sigma_x_squared 37 | < 1e-3); 38 | REQUIRE(std::abs(blob.sigma_xy - expected_blob.sigma_xy) / expected_blob.sigma_xy < 1e-3f); 39 | REQUIRE( 40 | std::abs(blob.sigma_y_squared - expected_blob.sigma_y_squared) / expected_blob.sigma_y_squared 41 | < 1e-3f); 42 | } else { 43 | first_received = true; 44 | } 45 | }); 46 | track_blob(event{0, 0}); 47 | track_blob(event{200, 100}); 48 | } 49 | -------------------------------------------------------------------------------- /source/convert.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | /// tarsier is a collection of event handlers. 6 | namespace tarsier { 7 | 8 | /// convert maps a type to another. 9 | template 10 | class convert { 11 | public: 12 | convert(EventToConvertedEvent&& event_to_converted_event, HandleConvertedEvent&& handle_converted_event) : 13 | _event_to_converted_event(std::forward(event_to_converted_event)), 14 | _handle_converted_event(std::forward(handle_converted_event)) {} 15 | convert(const convert&) = delete; 16 | convert(convert&&) = default; 17 | convert& operator=(const convert&) = delete; 18 | convert& operator=(convert&&) = default; 19 | virtual ~convert() = default; 20 | 21 | /// operator() handles an event. 22 | virtual void operator()(Event event) { 23 | _handle_converted_event(_event_to_converted_event(event)); 24 | } 25 | 26 | protected: 27 | EventToConvertedEvent _event_to_converted_event; 28 | HandleConvertedEvent _handle_converted_event; 29 | }; 30 | 31 | /// make_convert creates a convert from functors. 32 | template 33 | inline convert 34 | make_convert(EventToConvertedEvent&& event_to_converted_event, HandleConvertedEvent&& handle_converted_event) { 35 | return convert( 36 | std::forward(event_to_converted_event), 37 | std::forward(handle_converted_event)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/compute_flow.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/compute_flow.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | 4 | struct event { 5 | uint64_t t; 6 | uint16_t x; 7 | uint16_t y; 8 | }; 9 | 10 | struct flow { 11 | uint64_t t; 12 | uint16_t x; 13 | uint16_t y; 14 | float vx; 15 | float vy; 16 | }; 17 | 18 | TEST_CASE("Compute the optical flow from the given events", "[compute_flow]") { 19 | flow expected_flow{ 20 | 2010000, 21 | 100, 22 | 100, 23 | 0.0000904721018f, 24 | 0.000232017177f, 25 | }; 26 | auto flow_generated = false; 27 | auto compute_flow = tarsier::make_compute_flow( 28 | 320, 29 | 240, 30 | 2, 31 | 1000000, 32 | 10, 33 | [](event event, float vx, float vy) -> flow { 34 | return {event.t, event.x, event.y, vx, vy}; 35 | }, 36 | [&](flow flow) -> void { 37 | flow_generated = true; 38 | REQUIRE(flow.t == expected_flow.t); 39 | REQUIRE(flow.x == expected_flow.x); 40 | REQUIRE(flow.y == expected_flow.y); 41 | REQUIRE(std::abs(flow.vx - expected_flow.vx) / expected_flow.vx < 1e-3f); 42 | REQUIRE(std::abs(flow.vy - expected_flow.vy) / expected_flow.vy < 1e-3f); 43 | }); 44 | compute_flow(event{2000000, 100 - 2, 100 - 2}); 45 | compute_flow(event{2001000, 100 - 1, 100 - 2}); 46 | compute_flow(event{2002000, 100 - 0, 100 - 2}); 47 | compute_flow(event{2003000, 100 - 2, 100 - 1}); 48 | compute_flow(event{2004000, 100 + 1, 100 - 2}); 49 | compute_flow(event{2005000, 100 - 1, 100 - 1}); 50 | compute_flow(event{2006000, 100 - 0, 100 - 1}); 51 | compute_flow(event{2007000, 100 - 2, 100 - 0}); 52 | compute_flow(event{2008000, 100 + 1, 100 - 1}); 53 | compute_flow(event{2010000, 100, 100}); 54 | REQUIRE(flow_generated); 55 | } 56 | -------------------------------------------------------------------------------- /source/mask_redundant.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | /// tarsier is a collection of event handlers. 9 | namespace tarsier { 10 | 11 | /// mask_redundant inverts the x coordinate. 12 | template 13 | class mask_redundant { 14 | public: 15 | mask_redundant(uint16_t width, uint16_t height, uint64_t duration, HandleEvent handle_event) : 16 | _width(width), 17 | _height(height), 18 | _duration(duration), 19 | _handle_event(std::forward(handle_event)), 20 | _ts(width * height * 2) {} 21 | mask_redundant(const mask_redundant&) = delete; 22 | mask_redundant(mask_redundant&&) = default; 23 | mask_redundant& operator=(const mask_redundant&) = delete; 24 | mask_redundant& operator=(mask_redundant&&) = default; 25 | virtual ~mask_redundant() {} 26 | 27 | /// operator() handles an event. 28 | virtual void operator()(Event event) { 29 | auto index = (event.x + event.y * _width) * 2 + (event.is_increase ? 1 : 0); 30 | if (_ts[index] < event.t - _duration) { 31 | _ts[index] = event.t; 32 | _handle_event(event); 33 | } 34 | } 35 | 36 | protected: 37 | const uint16_t _width; 38 | const uint16_t _height; 39 | const uint64_t _duration; 40 | HandleEvent _handle_event; 41 | std::vector _ts; 42 | }; 43 | 44 | /// make_mask_redundant creates a mask_redundant from a functor. 45 | template 46 | mask_redundant 47 | make_mask_redundant(uint16_t width, uint16_t height, uint64_t duration, HandleEvent handle_event) { 48 | return mask_redundant(width, height, duration, std::forward(handle_event)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /source/select_rectangle.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | 9 | /// select_rectangle propagates only the events within the given rectangular 10 | /// window. 11 | template 12 | class select_rectangle { 13 | public: 14 | select_rectangle(uint16_t left, uint16_t bottom, uint16_t width, uint16_t height, HandleEvent&& handle_event) : 15 | _left(left), 16 | _bottom(bottom), 17 | _right(left + width), 18 | _top(bottom + height), 19 | _handle_event(std::forward(handle_event)) {} 20 | select_rectangle(const select_rectangle&) = delete; 21 | select_rectangle(select_rectangle&&) = default; 22 | select_rectangle& operator=(const select_rectangle&) = delete; 23 | select_rectangle& operator=(select_rectangle&&) = default; 24 | virtual ~select_rectangle() = default; 25 | 26 | /// operator() handles an event. 27 | virtual void operator()(Event event) { 28 | if (event.x >= _left && event.x < _right && event.y >= _bottom && event.y < _top) { 29 | _handle_event(event); 30 | } 31 | } 32 | 33 | protected: 34 | const uint16_t _left; 35 | const uint16_t _bottom; 36 | const uint16_t _right; 37 | const uint16_t _top; 38 | HandleEvent _handle_event; 39 | }; 40 | 41 | /// make_select_rectangle creates a select_rectangle from a functor. 42 | template 43 | select_rectangle inline make_select_rectangle( 44 | uint16_t left, 45 | uint16_t bottom, 46 | uint16_t width, 47 | uint16_t height, 48 | HandleEvent&& handle_event) { 49 | return select_rectangle( 50 | left, bottom, width, height, std::forward(handle_event)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /source/replicate.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /// tarsier is a collection of event handlers. 8 | namespace tarsier { 9 | 10 | /// replicate triggers several handlers for each event. 11 | template 12 | class replicate { 13 | public: 14 | replicate(HandleEventCallbacks&&... handle_event_callbacks) : 15 | _handle_event_callbacks(std::forward(handle_event_callbacks)...) {} 16 | replicate(const replicate&) = delete; 17 | replicate(replicate&&) = default; 18 | replicate& operator=(const replicate&) = delete; 19 | replicate& operator=(replicate&&) = default; 20 | virtual ~replicate() = default; 21 | 22 | /// operator() handles an event. 23 | virtual void operator()(Event event) { 24 | replicate::trigger<0>(std::forward(event)); 25 | } 26 | 27 | protected: 28 | /// trigger calls the n-th event callback. 29 | template 30 | typename std::enable_if < index::type trigger(Event event) { 31 | std::get(_handle_event_callbacks)(event); 32 | trigger(std::forward(event)); 33 | } 34 | 35 | /// trigger is a termination for the template loop. 36 | template 37 | typename std::enable_if::type trigger(Event) {} 38 | 39 | std::tuple _handle_event_callbacks; 40 | }; 41 | 42 | /// make_replicate creates a replicate from functors. 43 | template 44 | inline replicate make_replicate(HandleEventCallbacks&&... handle_event_callbacks) { 45 | return replicate(std::forward(handle_event_callbacks)...); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/average_grid.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/average_grid.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | #include 4 | 5 | struct event { 6 | uint16_t x; 7 | uint16_t y; 8 | }; 9 | 10 | struct Position { 11 | float cx; 12 | float cy; 13 | bool valid; 14 | }; 15 | 16 | typedef std::vector> Grid; 17 | 18 | struct Centroids { 19 | std::vector> grid; 20 | uint16_t ir; 21 | uint16_t ic; 22 | }; 23 | 24 | 25 | Grid grid = {{{1, 1, true}, {4, 1, true}, {7, 1, true}}, 26 | {{1, 4, true}, {4, 4, true}, {7, 4, true}}, 27 | {{1, 7, true}, {4, 7, true}, {7, 7, false}}}; 28 | 29 | TEST_CASE("Average the position of the given events in a grid", "[average_grid]") { 30 | auto first_received = false; 31 | auto second_received = false; 32 | auto third_received = false; 33 | auto average_grid = tarsier::make_average_grid( 34 | grid, 35 | 3.0, 36 | 0.5, 37 | [](event event, Grid grid, uint16_t ir, uint16_t ic) -> Centroids { 38 | return {grid, ir, ic}; 39 | }, 40 | [&](Centroids centroids) -> void { 41 | if (third_received) { 42 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cx == 7); 43 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cy == 7); 44 | } else if (second_received) { 45 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cx == 4.5); 46 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cy == 1.5); 47 | third_received = true; 48 | } else if (first_received) { 49 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cx == 1.25); 50 | REQUIRE(centroids.grid[centroids.ir][centroids.ic].cy == 1.25); 51 | second_received = true; 52 | } else { 53 | first_received = true; 54 | } 55 | }); 56 | average_grid(event{0, 0}); 57 | average_grid(event{2, 2}); 58 | average_grid(event{5, 2}); 59 | average_grid(event{8, 8}); 60 | } 61 | -------------------------------------------------------------------------------- /source/mask_isolated.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /// tarsier is a collection of event handlers. 8 | namespace tarsier { 9 | 10 | /// mask_isolated propagates only events that are not isolated spatially or 11 | /// temporally. 12 | template 13 | class mask_isolated { 14 | public: 15 | mask_isolated(uint16_t width, uint16_t height, uint64_t temporal_window, HandleEvent&& handle_event) : 16 | _width(width), 17 | _height(height), 18 | _temporal_window(temporal_window), 19 | _handle_event(std::forward(handle_event)), 20 | _ts(width * height, 0) {} 21 | mask_isolated(const mask_isolated&) = delete; 22 | mask_isolated(mask_isolated&&) = default; 23 | mask_isolated& operator=(const mask_isolated&) = delete; 24 | mask_isolated& operator=(mask_isolated&&) = default; 25 | virtual ~mask_isolated() = default; 26 | 27 | /// operator() handles an event. 28 | virtual void operator()(Event event) { 29 | const auto index = event.x + event.y * _width; 30 | _ts[index] = event.t + _temporal_window; 31 | if ((event.x > 0 && _ts[index - 1] > event.t) || (event.x < _width - 1 && _ts[index + 1] > event.t) 32 | || (event.y > 0 && _ts[index - _width] > event.t) 33 | || (event.y < _height - 1 && _ts[index + _width] > event.t)) { 34 | _handle_event(event); 35 | } 36 | } 37 | 38 | protected: 39 | const uint16_t _width; 40 | const uint16_t _height; 41 | const uint64_t _temporal_window; 42 | HandleEvent _handle_event; 43 | std::vector _ts; 44 | }; 45 | 46 | /// make_mask_isolated creates a mask_isolated from a functor. 47 | template 48 | inline mask_isolated 49 | make_mask_isolated(uint16_t width, uint16_t height, uint64_t temporal_window, HandleEvent&& handle_event) { 50 | return mask_isolated( 51 | width, height, temporal_window, std::forward(handle_event)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![banner](banner.png) 2 | 3 | Tarsier is a collection of tools to build event-based algorithms. It is an header-only library. 4 | 5 | # install 6 | 7 | Within a Git repository, run the commands: 8 | 9 | ```sh 10 | mkdir -p third_party 11 | cd third_party 12 | git submodule add https://github.com/neuromorphic-paris/tarsier.git 13 | git submodule update --init --recursive 14 | ``` 15 | 16 | # user guides and documentation 17 | 18 | User guides and code documentation are held in the [wiki](https://github.com/neuromorphic-paris/tarsier/wiki). 19 | 20 | # contribute 21 | 22 | ## development dependencies 23 | 24 | ### Debian / Ubuntu 25 | 26 | Open a terminal and run: 27 | ```sh 28 | sudo apt install premake4 # cross-platform build configuration 29 | sudo apt install clang-format # formatting tool 30 | ``` 31 | 32 | ### macOS 33 | 34 | Open a terminal and run: 35 | ```sh 36 | brew install premake # cross-platform build configuration 37 | brew install clang-format # formatting tool 38 | ``` 39 | If the command is not found, you need to install Homebrew first with the command: 40 | ```sh 41 | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 42 | ``` 43 | 44 | ### Windows 45 | 46 | Download and install: 47 | - [Visual Studio Community](https://visualstudio.microsoft.com/vs/community/). Select at least __Desktop development with C++__ when asked. 48 | - [git](https://git-scm.com) 49 | - [premake 4.x](https://premake.github.io/download.html). In order to use it from the command line, the *premake4.exe* executable must be copied to a directory in your path. After downloading and decompressing *premake-4.4-beta5-windows.zip*, run in the command prompt: 50 | ```sh 51 | copy "%userprofile%\Downloads\premake-4.4-beta5-windows\premake4.exe" "%userprofile%\AppData\Local\Microsoft\WindowsApps" 52 | ``` 53 | 54 | ## test 55 | 56 | To test the library, run from the *tarsier* directory: 57 | ```sh 58 | premake4 gmake 59 | cd build 60 | make 61 | cd release 62 | ./tarsier 63 | ``` 64 | 65 | __Windows__ users must run `premake4 vs2010` instead, and open the generated solution with Visual Studio. 66 | 67 | After changing the code, format the source files by running from the *tarsier* directory: 68 | ```sh 69 | for file in source/*.hpp; do clang-format -i $file; done; 70 | for file in test/*.cpp; do clang-format -i $file; done; 71 | ``` 72 | 73 | __Windows__ users must run *Edit* > *Advanced* > *Format Document* from the Visual Studio menu instead. 74 | 75 | # license 76 | 77 | See the [LICENSE](LICENSE.txt) file for license rights and limitations (GNU GPLv3). 78 | -------------------------------------------------------------------------------- /source/average_position.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | /// average_position calculates the average position of the given events. 9 | /// An exponential event-wise decay is used as weight. 10 | template 11 | class average_position { 12 | public: 13 | average_position( 14 | float x, 15 | float y, 16 | float inertia, 17 | EventToPosition&& event_to_position, 18 | HandlePosition&& handle_position) : 19 | _x(x), 20 | _y(y), 21 | _inertia(inertia), 22 | _event_to_position(std::forward(event_to_position)), 23 | _handle_position(std::forward(handle_position)) { 24 | if (_inertia < 0 || _inertia > 1) { 25 | throw std::logic_error("inertia must be in the range [0, 1]"); 26 | } 27 | } 28 | average_position(const average_position&) = delete; 29 | average_position(average_position&&) = default; 30 | average_position& operator=(const average_position&) = delete; 31 | average_position& operator=(average_position&&) = default; 32 | virtual ~average_position() = default; 33 | 34 | /// operator() handles an event. 35 | virtual void operator()(Event event) { 36 | _x = _inertia * _x + (1 - _inertia) * event.x; 37 | _y = _inertia * _y + (1 - _inertia) * event.y; 38 | _handle_position(_event_to_position(event, _x, _y)); 39 | } 40 | 41 | protected: 42 | float _x; 43 | float _y; 44 | const float _inertia; 45 | EventToPosition _event_to_position; 46 | HandlePosition _handle_position; 47 | }; 48 | 49 | /// make_average_position creates an average_position from functors. 50 | template 51 | inline average_position make_average_position( 52 | float x, 53 | float y, 54 | float inertia, 55 | EventToPosition&& EventToposition, 56 | HandlePosition&& handle_position) { 57 | return average_position( 58 | x, 59 | y, 60 | inertia, 61 | std::forward(EventToposition), 62 | std::forward(handle_position)); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /source/average_grid.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /// tarsier is a collection of event handlers. 9 | namespace tarsier { 10 | /// average_grid calculates the average positions of the given events within each grid. 11 | /// An exponential event-wise decay is used as weight. 12 | template 13 | class average_grid { 14 | public: 15 | average_grid( 16 | Grid grid, 17 | float pitch, 18 | float inertia, 19 | EventToGrid&& event_to_grid, 20 | HandleGrid&& handle_grid) : 21 | _grid(grid), 22 | _pitch(pitch), 23 | _inertia(inertia), 24 | _event_to_grid(std::forward(event_to_grid)), 25 | _handle_grid(std::forward(handle_grid)) { 26 | if (_inertia < 0 || _inertia > 1) { 27 | throw std::logic_error("inertia must be in the range [0, 1]"); 28 | } 29 | } 30 | average_grid(const average_grid&) = delete; 31 | average_grid(average_grid&&) = default; 32 | average_grid& operator=(const average_grid&) = delete; 33 | average_grid& operator=(average_grid&&) = default; 34 | virtual ~average_grid() {} 35 | 36 | /// operator() handles an event. 37 | virtual void operator()(Event event) { 38 | const uint16_t ic = std::floor(event.x / _pitch); 39 | const uint16_t ir = std::floor(event.y / _pitch); 40 | if (_grid[ir][ic].valid) { 41 | _grid[ir][ic].cx = _inertia * _grid[ir][ic].cx + (1 - _inertia) * event.x; 42 | _grid[ir][ic].cy = _inertia * _grid[ir][ic].cy + (1 - _inertia) * event.y; 43 | } 44 | _handle_grid(_event_to_grid(event, _grid, ir, ic)); 45 | } 46 | 47 | protected: 48 | Grid _grid; 49 | const float _pitch; 50 | const float _inertia; 51 | EventToGrid _event_to_grid; 52 | HandleGrid _handle_grid; 53 | }; 54 | 55 | /// make_average_grid creates an average_grid from functors. 56 | template 57 | inline average_grid make_average_grid( 58 | Grid grid, 59 | float pitch, 60 | float inertia, 61 | EventToGrid&& event_to_grid, 62 | HandleGrid&& handle_grid) { 63 | return average_grid( 64 | grid, 65 | pitch, 66 | inertia, 67 | std::forward(event_to_grid), 68 | std::forward(handle_grid)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /source/compute_activity.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /// tarsier is a collection of event handlers. 9 | namespace tarsier { 10 | /// compute_activity evaluates the activity at each pixel, using an exponential 11 | /// decay. 12 | template 13 | class compute_activity { 14 | public: 15 | compute_activity( 16 | uint16_t width, 17 | uint16_t height, 18 | float decay, 19 | EventToActivity&& event_to_activity, 20 | HandleActivity&& handle_activity) : 21 | _width(width), 22 | _decay(decay), 23 | _event_to_activity(std::forward(event_to_activity)), 24 | _handle_activity(std::forward(handle_activity)), 25 | _potentials_and_ts(width * height, {0.0f, 0}) {} 26 | compute_activity(const compute_activity&) = delete; 27 | compute_activity(compute_activity&&) = default; 28 | compute_activity& operator=(const compute_activity&) = delete; 29 | compute_activity& operator=(compute_activity&&) = default; 30 | virtual ~compute_activity() = default; 31 | 32 | /// operator() handles an event. 33 | virtual void operator()(Event event) { 34 | auto& potential_and_t = _potentials_and_ts[event.x + event.y * _width]; 35 | potential_and_t.first = 36 | potential_and_t.first * std::exp(-static_cast(event.t - potential_and_t.second) / _decay) + 1; 37 | potential_and_t.second = event.t; 38 | _handle_activity(_event_to_activity(event, potential_and_t.first)); 39 | } 40 | 41 | protected: 42 | const uint16_t _width; 43 | const float _decay; 44 | EventToActivity _event_to_activity; 45 | HandleActivity _handle_activity; 46 | std::vector> _potentials_and_ts; 47 | }; 48 | 49 | /// make_compute_activity creates a compute_activity from functors. 50 | template 51 | inline compute_activity make_compute_activity( 52 | uint16_t width, 53 | uint16_t height, 54 | float decay, 55 | EventToActivity&& event_to_activity, 56 | HandleActivity&& handle_activity) { 57 | return compute_activity( 58 | width, 59 | height, 60 | decay, 61 | std::forward(event_to_activity), 62 | std::forward(handle_activity)); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /source/stitch.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /// tarsier is a collection of event handlers. 8 | namespace tarsier { 9 | 10 | /// stitch turns a stream of threshold crossings into a stream of time deltas. 11 | template 12 | class stitch { 13 | public: 14 | stitch( 15 | uint16_t width, 16 | uint16_t height, 17 | ThresholdCrossingToEvent&& threshold_crossing_to_event, 18 | HandleEvent&& handle_event) : 19 | _width(width), 20 | _height(height), 21 | _threshold_crossing_to_event(std::forward(threshold_crossing_to_event)), 22 | _handle_event(std::forward(handle_event)), 23 | _are_triggered_and_ts(width * height, {false, 0}) {} 24 | stitch(const stitch&) = delete; 25 | stitch(stitch&&) = default; 26 | stitch& operator=(const stitch&) = delete; 27 | stitch& operator=(stitch&&) = default; 28 | virtual ~stitch() = default; 29 | 30 | /// operator() handles a threshold crossing. 31 | virtual void operator()(ThresholdCrossing threshold_crossing) { 32 | auto& is_triggered_and_t = _are_triggered_and_ts[threshold_crossing.x + threshold_crossing.y * _width]; 33 | if (!is_triggered_and_t.first) { 34 | if (!threshold_crossing.is_second) { 35 | is_triggered_and_t.first = true; 36 | is_triggered_and_t.second = threshold_crossing.t; 37 | } 38 | } else { 39 | if (threshold_crossing.is_second) { 40 | is_triggered_and_t.first = false; 41 | _handle_event(_threshold_crossing_to_event( 42 | threshold_crossing, threshold_crossing.t - is_triggered_and_t.second)); 43 | } else { 44 | is_triggered_and_t.second = threshold_crossing.t; 45 | } 46 | } 47 | } 48 | 49 | protected: 50 | const uint16_t _width; 51 | const uint16_t _height; 52 | ThresholdCrossingToEvent _threshold_crossing_to_event; 53 | HandleEvent _handle_event; 54 | std::vector> _are_triggered_and_ts; 55 | }; 56 | 57 | /// make_stitch creates a stitch from functors. 58 | template 59 | inline stitch make_stitch( 60 | uint16_t width, 61 | uint16_t height, 62 | ThresholdCrossingToEvent&& threshold_crossing_to_event, 63 | HandleEvent&& handle_event) { 64 | return stitch( 65 | width, 66 | height, 67 | std::forward(threshold_crossing_to_event), 68 | std::forward(handle_event)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | AccessModifierOffset: 0 4 | AlignAfterOpenBracket: AlwaysBreak 5 | AlignConsecutiveAssignments: false 6 | AlignConsecutiveDeclarations: false 7 | AlignEscapedNewlines: Right 8 | AlignOperands: true 9 | AlignTrailingComments: true 10 | AllowAllParametersOfDeclarationOnNextLine: false 11 | AllowShortBlocksOnASingleLine: false 12 | AllowShortCaseLabelsOnASingleLine: false 13 | AllowShortFunctionsOnASingleLine: Empty 14 | AllowShortIfStatementsOnASingleLine: false 15 | AllowShortLoopsOnASingleLine: true 16 | AlwaysBreakAfterDefinitionReturnType: None 17 | AlwaysBreakAfterReturnType: None 18 | AlwaysBreakBeforeMultilineStrings: false 19 | AlwaysBreakTemplateDeclarations: true 20 | BinPackArguments: false 21 | BinPackParameters: false 22 | BraceWrapping: 23 | AfterClass: false 24 | AfterControlStatement: false 25 | AfterEnum: false 26 | AfterFunction: false 27 | AfterNamespace: false 28 | AfterObjCDeclaration: false 29 | AfterStruct: false 30 | AfterUnion: false 31 | BeforeCatch: false 32 | BeforeElse: false 33 | IndentBraces: false 34 | SplitEmptyFunction: true 35 | SplitEmptyRecord: true 36 | SplitEmptyNamespace: true 37 | BreakBeforeBinaryOperators: NonAssignment 38 | BreakBeforeBraces: Attach 39 | BreakBeforeInheritanceComma: false 40 | BreakBeforeTernaryOperators: false 41 | BreakConstructorInitializersBeforeComma: false 42 | BreakConstructorInitializers: AfterColon 43 | BreakAfterJavaFieldAnnotations: false 44 | BreakStringLiterals: true 45 | ColumnLimit: 120 46 | CommentPragmas: '^ IWYU pragma:' 47 | CompactNamespaces: false 48 | ConstructorInitializerAllOnOneLineOrOnePerLine: true 49 | ConstructorInitializerIndentWidth: 4 50 | ContinuationIndentWidth: 4 51 | Cpp11BracedListStyle: true 52 | DerivePointerAlignment: false 53 | DisableFormat: false 54 | ExperimentalAutoDetectBinPacking: false 55 | FixNamespaceComments: false 56 | ForEachMacros: 57 | - foreach 58 | - Q_FOREACH 59 | - BOOST_FOREACH 60 | IncludeCategories: 61 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 62 | Priority: 2 63 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' 64 | Priority: 3 65 | - Regex: '.*' 66 | Priority: 1 67 | IncludeIsMainRegex: '(Test)?$' 68 | IndentCaseLabels: true 69 | IndentWidth: 4 70 | IndentWrappedFunctionNames: false 71 | JavaScriptQuotes: Single 72 | JavaScriptWrapImports: true 73 | KeepEmptyLinesAtTheStartOfBlocks: false 74 | MacroBlockBegin: '' 75 | MacroBlockEnd: '' 76 | MaxEmptyLinesToKeep: 1 77 | NamespaceIndentation: All 78 | ObjCBlockIndentWidth: 4 79 | ObjCSpaceAfterProperty: false 80 | ObjCSpaceBeforeProtocolList: true 81 | PenaltyBreakAssignment: 2 82 | PenaltyBreakBeforeFirstCallParameter: 19 83 | PenaltyBreakComment: 300 84 | PenaltyBreakFirstLessLess: 120 85 | PenaltyBreakString: 1000 86 | PenaltyExcessCharacter: 1000000 87 | PenaltyReturnTypeOnItsOwnLine: 60 88 | PointerAlignment: Left 89 | ReflowComments: true 90 | SortIncludes: true 91 | SortUsingDeclarations: true 92 | SpaceAfterCStyleCast: false 93 | SpaceAfterTemplateKeyword: true 94 | SpaceBeforeAssignmentOperators: true 95 | SpaceBeforeParens: ControlStatements 96 | SpaceInEmptyParentheses: false 97 | SpacesBeforeTrailingComments: 1 98 | SpacesInAngles: false 99 | SpacesInContainerLiterals: false 100 | SpacesInCStyleCastParentheses: false 101 | SpacesInParentheses: false 102 | SpacesInSquareBrackets: false 103 | Standard: Cpp11 104 | TabWidth: 8 105 | UseTab: Never 106 | ... 107 | -------------------------------------------------------------------------------- /test/compute_time_surface.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/compute_time_surface.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | #include 4 | 5 | const uint16_t spatial_window = 2; 6 | const auto projections_size = (2 * spatial_window + 1) * (2 * spatial_window + 1); 7 | 8 | struct event { 9 | uint64_t t; 10 | uint16_t x; 11 | uint16_t y; 12 | bool polarity; 13 | }; 14 | 15 | struct time_surface { 16 | uint64_t t; 17 | uint16_t x; 18 | uint16_t y; 19 | std::array true_projections; 20 | std::array false_projections; 21 | }; 22 | 23 | TEST_CASE("Compute time surfaces from events", "[compute_time_surface]") { 24 | time_surface expected_time_surface{2010000, 100, 100}; 25 | expected_time_surface.true_projections[2] = 0.00033546262790251185f; 26 | expected_time_surface.true_projections[3] = 0.0024787521766663585f; 27 | expected_time_surface.true_projections[7] = 0.018315638888734179f; 28 | expected_time_surface.true_projections[8] = 0.1353352832366127f; 29 | expected_time_surface.false_projections[1] = 0.00012340980408667956f; 30 | expected_time_surface.false_projections[5] = 0.00091188196555451624f; 31 | expected_time_surface.false_projections[6] = 0.006737946999085467f; 32 | expected_time_surface.false_projections[10] = 0.049787068367863944f; 33 | expected_time_surface.false_projections[12] = 1.0f; 34 | std::size_t count = 0; 35 | auto compute_time_surface = tarsier::make_compute_time_surface( 36 | 320, 37 | 240, 38 | 10000, 39 | 1000, 40 | [](event event, std::array, projections_size> projections_and_polarities) { 41 | time_surface time_surface{event.t, event.x, event.y}; 42 | for (std::size_t index = 0; index < projections_size; ++index) { 43 | if (projections_and_polarities[index].second) { 44 | time_surface.true_projections[index] = projections_and_polarities[index].first; 45 | } else { 46 | time_surface.false_projections[index] = projections_and_polarities[index].first; 47 | } 48 | } 49 | return time_surface; 50 | }, 51 | [&](time_surface time_surface) { 52 | ++count; 53 | if (count == 10) { 54 | REQUIRE(time_surface.x == expected_time_surface.x); 55 | REQUIRE(time_surface.y == expected_time_surface.y); 56 | REQUIRE(time_surface.t == expected_time_surface.t); 57 | for (std::size_t index = 0; index < projections_size; ++index) { 58 | REQUIRE( 59 | std::abs(time_surface.true_projections[index] - expected_time_surface.true_projections[index]) 60 | <= 1e-3 * expected_time_surface.true_projections[index]); 61 | REQUIRE( 62 | std::abs(time_surface.false_projections[index] - expected_time_surface.false_projections[index]) 63 | <= 1e-3 * expected_time_surface.false_projections[index]); 64 | } 65 | } 66 | }); 67 | compute_time_surface(event{2000000, 100 - 2, 100 - 2, true}); 68 | compute_time_surface(event{2001000, 100 - 1, 100 - 2, false}); 69 | compute_time_surface(event{2002000, 100 - 0, 100 - 2, true}); 70 | compute_time_surface(event{2003000, 100 - 2, 100 - 1, false}); 71 | compute_time_surface(event{2004000, 100 + 1, 100 - 2, true}); 72 | compute_time_surface(event{2005000, 100 - 1, 100 - 1, false}); 73 | compute_time_surface(event{2006000, 100 - 0, 100 - 1, true}); 74 | compute_time_surface(event{2007000, 100 - 2, 100 - 0, false}); 75 | compute_time_surface(event{2008000, 100 + 1, 100 - 1, true}); 76 | compute_time_surface(event{2010000, 100, 100, false}); 77 | } 78 | -------------------------------------------------------------------------------- /source/track_blob.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /// tarsier is a collection of event handlers. 8 | namespace tarsier { 9 | /// track_blob averages the incoming events with a gaussian blob. 10 | template 11 | class track_blob { 12 | public: 13 | track_blob( 14 | float x, 15 | float y, 16 | float sigma_x_squared, 17 | float sigma_xy, 18 | float sigma_y_squared, 19 | float position_inertia, 20 | float variance_inertia, 21 | EventToBlob&& event_to_blob, 22 | HandleBlob&& handle_blob) : 23 | _x(x), 24 | _y(y), 25 | _sigma_x_squared(sigma_x_squared), 26 | _sigma_xy(sigma_xy), 27 | _sigma_y_squared(sigma_y_squared), 28 | _position_inertia(position_inertia), 29 | _variance_inertia(variance_inertia), 30 | _event_to_blob(std::forward(event_to_blob)), 31 | _handle_blob(std::forward(handle_blob)) { 32 | if (_position_inertia < 0 || _position_inertia > 1) { 33 | throw std::logic_error("position_inertia must be in the range [0, 1]"); 34 | } 35 | if (_variance_inertia < 0 || _variance_inertia > 1) { 36 | throw std::logic_error("variance_inertia must be in the range [0, 1]"); 37 | } 38 | } 39 | track_blob(const track_blob&) = delete; 40 | track_blob(track_blob&&) = default; 41 | track_blob& operator=(const track_blob&) = delete; 42 | track_blob& operator=(track_blob&&) = default; 43 | virtual ~track_blob() = default; 44 | 45 | /// operator() handles an event. 46 | virtual void operator()(Event event) { 47 | const auto x_delta = event.x - _x; 48 | const auto y_delta = event.y - _y; 49 | _x = _position_inertia * _x + (1 - _position_inertia) * event.x; 50 | _y = _position_inertia * _y + (1 - _position_inertia) * event.y; 51 | _sigma_x_squared = _variance_inertia * _sigma_x_squared + (1 - _variance_inertia) * x_delta * x_delta; 52 | _sigma_xy = _variance_inertia * _sigma_xy + (1 - _variance_inertia) * x_delta * y_delta; 53 | _sigma_y_squared = _variance_inertia * _sigma_y_squared + (1 - _variance_inertia) * y_delta * y_delta; 54 | _handle_blob(_event_to_blob(event, _x, _y, _sigma_x_squared, _sigma_xy, _sigma_y_squared)); 55 | } 56 | 57 | /// x returns the blob's center's x coordinate. 58 | float x() const { 59 | return _x; 60 | } 61 | 62 | /// y returns the blob's center's y coordinate. 63 | float y() const { 64 | return _y; 65 | } 66 | 67 | /// sigma_x_squared returns the blob's variance along the x axis. 68 | float sigma_x_squared() const { 69 | return _sigma_x_squared; 70 | } 71 | 72 | /// sigma_xy returns the blob's covariance. 73 | float sigma_xy() const { 74 | return _sigma_xy; 75 | } 76 | 77 | /// sigma_y_squared returns the blob's variance along the y axis. 78 | float sigma_y_squared() const { 79 | return _sigma_y_squared; 80 | } 81 | 82 | protected: 83 | float _x; 84 | float _y; 85 | float _sigma_x_squared; 86 | float _sigma_xy; 87 | float _sigma_y_squared; 88 | const float _position_inertia; 89 | const float _variance_inertia; 90 | EventToBlob _event_to_blob; 91 | HandleBlob _handle_blob; 92 | }; 93 | 94 | /// make_track_blob creates a track_blob from functors. 95 | template 96 | inline track_blob make_track_blob( 97 | float x, 98 | float y, 99 | float sigma_x_squared, 100 | float sigma_xy, 101 | float sigma_y_squared, 102 | float position_inertia, 103 | float variance_inertia, 104 | EventToBlob&& event_to_blob, 105 | HandleBlob&& handle_blob) { 106 | return track_blob( 107 | x, 108 | y, 109 | sigma_x_squared, 110 | sigma_xy, 111 | sigma_y_squared, 112 | position_inertia, 113 | variance_inertia, 114 | std::forward(event_to_blob), 115 | std::forward(handle_blob)); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /source/compute_time_surface.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | /// tarsier is a collection of event handlers. 10 | namespace tarsier { 11 | /// compute_time_surface extracts time surfaces from events. 12 | template < 13 | typename Event, 14 | typename Polarity, 15 | typename TimeSurface, 16 | uint16_t spatial_window, 17 | typename EventToTimeSurface, 18 | typename HandleTimeSurface> 19 | class compute_time_surface { 20 | public: 21 | compute_time_surface( 22 | uint16_t width, 23 | uint16_t height, 24 | uint64_t temporal_window, 25 | float decay, 26 | EventToTimeSurface&& event_to_time_surface, 27 | HandleTimeSurface&& handle_time_surface) : 28 | _width(width), 29 | _height(height), 30 | _temporal_window(temporal_window), 31 | _decay(decay), 32 | _event_to_time_surface(std::forward(event_to_time_surface)), 33 | _handle_time_surface(std::forward(handle_time_surface)), 34 | _ts_and_polarities(width * height, {0, false}) {} 35 | compute_time_surface(const compute_time_surface&) = delete; 36 | compute_time_surface(compute_time_surface&&) = default; 37 | compute_time_surface& operator=(const compute_time_surface&) = delete; 38 | compute_time_surface& operator=(compute_time_surface&&) = default; 39 | virtual ~compute_time_surface() = default; 40 | 41 | /// operator() handles an event. 42 | virtual void operator()(Event event) { 43 | { 44 | const auto index = event.x + event.y * _width; 45 | _ts_and_polarities[index].first = event.t; 46 | _ts_and_polarities[index].second = event.polarity; 47 | } 48 | const auto t_threshold = (event.t <= _temporal_window ? 0 : event.t - _temporal_window); 49 | std::array, (spatial_window * 2 + 1) * (spatial_window * 2 + 1)> 50 | projections_and_polarities; 51 | for (uint16_t y = (event.y <= spatial_window ? 0 : event.y - spatial_window); 52 | y <= (event.y >= _height - 1 - spatial_window ? _height - 1 : event.y + spatial_window); 53 | ++y) { 54 | for (uint16_t x = (event.x <= spatial_window ? 0 : event.x - spatial_window); 55 | x <= (event.x >= _width - 1 - spatial_window ? _width - 1 : event.x + spatial_window); 56 | ++x) { 57 | const auto t_and_polarity = _ts_and_polarities[x + y * _width]; 58 | if (t_and_polarity.first > t_threshold) { 59 | projections_and_polarities 60 | [x + spatial_window - event.x + (y + spatial_window - event.y) * (2 * spatial_window + 1)] = 61 | {std::exp(-static_cast(event.t - t_and_polarity.first) / _decay), 62 | t_and_polarity.second}; 63 | } 64 | } 65 | } 66 | _handle_time_surface(_event_to_time_surface(event, projections_and_polarities)); 67 | } 68 | 69 | protected: 70 | const uint16_t _width; 71 | const uint16_t _height; 72 | const uint64_t _temporal_window; 73 | const float _decay; 74 | EventToTimeSurface _event_to_time_surface; 75 | HandleTimeSurface _handle_time_surface; 76 | std::vector> _ts_and_polarities; 77 | }; 78 | 79 | /// make_compute_time_surface creates a compute_time_surface from functors. 80 | template < 81 | typename Event, 82 | typename Polarity, 83 | typename TimeSurface, 84 | uint16_t spatial_window, 85 | typename EventToTimeSurface, 86 | typename HandleTimeSurface> 87 | inline compute_time_surface 88 | make_compute_time_surface( 89 | uint16_t width, 90 | uint16_t height, 91 | uint64_t temporal_window, 92 | float decay, 93 | EventToTimeSurface&& event_to_time_surface, 94 | HandleTimeSurface&& handle_time_surface) { 95 | return compute_time_surface< 96 | Event, 97 | Polarity, 98 | TimeSurface, 99 | spatial_window, 100 | EventToTimeSurface, 101 | HandleTimeSurface>( 102 | width, 103 | height, 104 | temporal_window, 105 | decay, 106 | std::forward(event_to_time_surface), 107 | std::forward(handle_time_surface)); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /test/track_blob_multi.cpp: -------------------------------------------------------------------------------- 1 | #include "../source/track_blob_multi.hpp" 2 | #include "../third_party/Catch2/single_include/catch.hpp" 3 | #include 4 | struct Event { 5 | uint16_t x; 6 | uint16_t y; 7 | }; 8 | 9 | struct Blob { 10 | float x; 11 | float y; 12 | float sigma_x_squared; 13 | float sigma_xy; 14 | float sigma_y_squared; 15 | }; 16 | 17 | struct MultiBlobs { 18 | uint16_t id; 19 | std::vector blobs; 20 | }; 21 | 22 | TEST_CASE("Average the events with multiple Gaussian blobs", "[track_blob_multi]") { 23 | MultiBlobs multi_blobs_initial{0, {{2.0f, 2.0f, 0.16f, 0.0f, 0.16f}, 24 | {7.0f, 2.0f, 0.16f, 0.0f, 0.16f}}}; 25 | MultiBlobs expected_multi_blobs_0{0, {{2.5f, 2.5f, 0.58f, 0.5f, 0.58f}, 26 | {7.0f, 2.0f, 0.16f, 0.0f, 0.16f}}}; 27 | MultiBlobs expected_multi_blobs_1{1, {{2.5f, 2.5f, 0.58f, 0.5f, 0.58f}, 28 | {7.5f, 2.5f, 0.58f, 0.5f, 0.58f}}}; 29 | 30 | auto first_received = false; 31 | auto track_blob_multi = tarsier::make_track_blob_multi( 32 | multi_blobs_initial, 33 | 0.001f, 34 | 0.5f, 35 | 0.5f, 36 | [](Event event, MultiBlobs multi_blobs) -> MultiBlobs { return {multi_blobs}; }, 37 | [&](MultiBlobs multi_blobs) -> void { 38 | if (first_received) { 39 | REQUIRE(multi_blobs.id == expected_multi_blobs_1.id); 40 | 41 | REQUIRE(std::abs(multi_blobs.blobs[0].x - expected_multi_blobs_1.blobs[0].x) / expected_multi_blobs_1.blobs[0].x < 1e-3f); 42 | REQUIRE(std::abs(multi_blobs.blobs[0].y - expected_multi_blobs_1.blobs[0].y) / expected_multi_blobs_1.blobs[0].y < 1e-3f); 43 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_x_squared - expected_multi_blobs_1.blobs[0].sigma_x_squared) / expected_multi_blobs_1.blobs[0].sigma_x_squared < 1e-3f); 44 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_xy - expected_multi_blobs_1.blobs[0].sigma_xy) / (expected_multi_blobs_1.blobs[0].sigma_xy + 1e-3f) < 1e-3f); 45 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_y_squared - expected_multi_blobs_1.blobs[0].sigma_y_squared) / expected_multi_blobs_1.blobs[0].sigma_y_squared < 1e-3f); 46 | 47 | REQUIRE(std::abs(multi_blobs.blobs[1].x - expected_multi_blobs_1.blobs[1].x) / expected_multi_blobs_1.blobs[1].x < 1e-3f); 48 | REQUIRE(std::abs(multi_blobs.blobs[1].y - expected_multi_blobs_1.blobs[1].y) / expected_multi_blobs_1.blobs[1].y < 1e-3f); 49 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_x_squared - expected_multi_blobs_1.blobs[1].sigma_x_squared) / expected_multi_blobs_1.blobs[1].sigma_x_squared < 1e-3f); 50 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_xy - expected_multi_blobs_1.blobs[1].sigma_xy) / (expected_multi_blobs_1.blobs[1].sigma_xy + 1e-3f) < 1e-3f); 51 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_y_squared - expected_multi_blobs_1.blobs[1].sigma_y_squared) / expected_multi_blobs_1.blobs[1].sigma_y_squared < 1e-3f); 52 | } else { 53 | REQUIRE(multi_blobs.id == expected_multi_blobs_0.id); 54 | 55 | REQUIRE(std::abs(multi_blobs.blobs[0].x - expected_multi_blobs_0.blobs[0].x) / expected_multi_blobs_0.blobs[0].x < 1e-3f); 56 | REQUIRE(std::abs(multi_blobs.blobs[0].y - expected_multi_blobs_0.blobs[0].y) / expected_multi_blobs_0.blobs[0].y < 1e-3f); 57 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_x_squared - expected_multi_blobs_0.blobs[0].sigma_x_squared) / expected_multi_blobs_0.blobs[0].sigma_x_squared < 1e-3f); 58 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_xy - expected_multi_blobs_0.blobs[0].sigma_xy) / (expected_multi_blobs_0.blobs[0].sigma_xy + 1e-3f) < 1e-3f); 59 | REQUIRE(std::abs(multi_blobs.blobs[0].sigma_y_squared - expected_multi_blobs_0.blobs[0].sigma_y_squared) / expected_multi_blobs_0.blobs[0].sigma_y_squared < 1e-3f); 60 | 61 | REQUIRE(std::abs(multi_blobs.blobs[1].x - expected_multi_blobs_0.blobs[1].x) / expected_multi_blobs_0.blobs[1].x < 1e-3f); 62 | REQUIRE(std::abs(multi_blobs.blobs[1].y - expected_multi_blobs_0.blobs[1].y) / expected_multi_blobs_0.blobs[1].y < 1e-3f); 63 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_x_squared - expected_multi_blobs_0.blobs[1].sigma_x_squared) / expected_multi_blobs_0.blobs[1].sigma_x_squared < 1e-3f); 64 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_xy - expected_multi_blobs_0.blobs[1].sigma_xy) / (expected_multi_blobs_0.blobs[1].sigma_xy + 1e-3f) < 1e-3f); 65 | REQUIRE(std::abs(multi_blobs.blobs[1].sigma_y_squared - expected_multi_blobs_0.blobs[1].sigma_y_squared) / expected_multi_blobs_0.blobs[1].sigma_y_squared < 1e-3f); 66 | 67 | first_received = true; 68 | } 69 | }); 70 | track_blob_multi(Event{3, 3}); 71 | track_blob_multi(Event{8, 3}); 72 | } 73 | -------------------------------------------------------------------------------- /source/hash.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | /// tarsier is a collection of event handlers. 7 | namespace tarsier { 8 | /// hash calculates the MurmurHash3 (128 bits, x64 version) of the given values. 9 | template 10 | class hash { 11 | public: 12 | hash(HandleUint64Pair&& handle_uint64_pair) : 13 | _handle_uint64_pair(std::forward(handle_uint64_pair)), 14 | _shift(0), 15 | _block(0, 0), 16 | _hash(0, 0), 17 | _size(0) {} 18 | hash(const hash&) = delete; 19 | hash(hash&&) = default; 20 | hash& operator=(const hash&) = delete; 21 | hash& operator=(hash&&) = default; 22 | virtual ~hash() { 23 | if (_size > 0 || _shift > 0) { 24 | if (_shift * sizeof(Uint) > 8) { 25 | std::get<1>(_block) *= 0x4cf5ad432745937full; 26 | std::get<1>(_block) = rotate(std::get<1>(_block), 33); 27 | std::get<1>(_block) *= 0x87c37b91114253d5ull; 28 | std::get<1>(_hash) ^= std::get<1>(_block); 29 | } 30 | if (_shift * sizeof(Uint) > 0) { 31 | std::get<0>(_block) *= 0x87c37b91114253d5ull; 32 | std::get<0>(_block) = rotate(std::get<0>(_block), 31); 33 | std::get<0>(_block) *= 0x4cf5ad432745937full; 34 | std::get<0>(_hash) ^= std::get<0>(_block); 35 | } 36 | std::get<0>(_hash) ^= (_size * 16 + _shift); 37 | std::get<1>(_hash) ^= (_size * 16 + _shift); 38 | std::get<0>(_hash) += std::get<1>(_hash); 39 | std::get<1>(_hash) += std::get<0>(_hash); 40 | std::get<0>(_hash) = mix(std::get<0>(_hash)); 41 | std::get<1>(_hash) = mix(std::get<1>(_hash)); 42 | std::get<0>(_hash) += std::get<1>(_hash); 43 | std::get<1>(_hash) += std::get<0>(_hash); 44 | _handle_uint64_pair(_hash); 45 | } 46 | } 47 | 48 | /// operator() handles an event. 49 | virtual void operator()(Uint uint) { 50 | if (_shift < 8 / sizeof(Uint)) { 51 | std::get<0>(_block) |= (static_cast(uint) << (_shift * sizeof(Uint) * 8)); 52 | ++_shift; 53 | } else { 54 | std::get<1>(_block) |= (static_cast(uint) << (_shift * sizeof(Uint) * 8 - 64)); 55 | if (_shift < 16 / sizeof(Uint) - 1) { 56 | ++_shift; 57 | } else { 58 | _shift = 0; 59 | ++_size; 60 | std::get<0>(_block) *= 0x87c37b91114253d5ull; 61 | std::get<0>(_block) = rotate(std::get<0>(_block), 31); 62 | std::get<0>(_block) *= 0x4cf5ad432745937full; 63 | std::get<0>(_hash) ^= std::get<0>(_block); 64 | std::get<0>(_hash) = rotate(std::get<0>(_hash), 27); 65 | std::get<0>(_hash) += std::get<1>(_hash); 66 | std::get<0>(_hash) = std::get<0>(_hash) * 5 + 0x52dce729; 67 | std::get<1>(_block) *= 0x4cf5ad432745937full; 68 | std::get<1>(_block) = rotate(std::get<1>(_block), 33); 69 | std::get<1>(_block) *= 0x87c37b91114253d5ull; 70 | std::get<1>(_hash) ^= std::get<1>(_block); 71 | std::get<1>(_hash) = rotate(std::get<1>(_hash), 31); 72 | std::get<1>(_hash) += std::get<0>(_hash); 73 | std::get<1>(_hash) = std::get<1>(_hash) * 5 + 0x38495ab5; 74 | std::get<0>(_block) = 0; 75 | std::get<1>(_block) = 0; 76 | } 77 | } 78 | } 79 | 80 | protected: 81 | /// rotate implements a bit-wise rotation. 82 | static uint64_t rotate(uint64_t value, uint8_t range) { 83 | return (value << range) | (value >> (64 - range)); 84 | } 85 | 86 | /// mix implements a bit-wise mix. 87 | static uint64_t mix(uint64_t value) { 88 | value ^= value >> 33; 89 | value *= 0xff51afd7ed558ccdull; 90 | value ^= value >> 33; 91 | value *= 0xc4ceb9fe1a85ec53ull; 92 | value ^= value >> 33; 93 | return value; 94 | } 95 | 96 | HandleUint64Pair _handle_uint64_pair; 97 | uint8_t _shift; 98 | std::pair _block; 99 | std::pair _hash; 100 | uint64_t _size; 101 | }; 102 | 103 | /// make_hash creates a hash from functors. 104 | template 105 | inline hash make_hash(HandleUint64Pair&& handle_uint64_pair) { 106 | return hash(std::forward(handle_uint64_pair)); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /source/track_blob_multi.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /// tarsier is a collection of event handlers. 9 | namespace tarsier { 10 | /// track_blob_multi averages the incoming events with a gaussian blob. 11 | template 12 | class track_blob_multi { 13 | public: 14 | track_blob_multi( 15 | MultiBlobs multi_blobs, 16 | float prob_threshold, 17 | float position_inertia, 18 | float variance_inertia, 19 | EventToBlob&& event_to_blob, 20 | HandleBlob&& handle_blob) : 21 | _multi_blobs(multi_blobs), 22 | _prob_threshold(prob_threshold), 23 | _position_inertia(position_inertia), 24 | _variance_inertia(variance_inertia), 25 | _event_to_blob(std::forward(event_to_blob)), 26 | _handle_blob(std::forward(handle_blob)) { 27 | if (_prob_threshold < 0 || _prob_threshold > 1) { 28 | throw std::logic_error("prob_threshold must be in the range [0, 1]"); 29 | } 30 | if (_position_inertia < 0 || _position_inertia > 1) { 31 | throw std::logic_error("position_inertia must be in the range [0, 1]"); 32 | } 33 | if (_variance_inertia < 0 || _variance_inertia > 1) { 34 | throw std::logic_error("variance_inertia must be in the range [0, 1]"); 35 | } 36 | } 37 | track_blob_multi(const track_blob_multi&) = delete; 38 | track_blob_multi(track_blob_multi&&) = default; 39 | track_blob_multi& operator=(const track_blob_multi&) = delete; 40 | track_blob_multi& operator=(track_blob_multi&&) = default; 41 | virtual ~track_blob_multi() = default; 42 | 43 | /// operator() handles an event. 44 | virtual void operator()(Event event) { 45 | uint16_t max_id = 0; 46 | float max_prob = 0.0f; 47 | // compute probability of each tracker 48 | for (uint16_t i = 0; i < _multi_blobs.blobs.size(); i++) { 49 | auto sigma_x_squared = _multi_blobs.blobs[i].sigma_x_squared; 50 | auto sigma_xy = _multi_blobs.blobs[i].sigma_xy; 51 | auto sigma_y_squared = _multi_blobs.blobs[i].sigma_y_squared; 52 | 53 | const auto det = sigma_x_squared * sigma_y_squared - sigma_xy * sigma_xy; 54 | const auto x_delta = event.x - _multi_blobs.blobs[i].x; 55 | const auto y_delta = event.y - _multi_blobs.blobs[i].y; 56 | const auto exp_power = -0.5 / det * (x_delta * x_delta * sigma_y_squared + 57 | y_delta * y_delta * sigma_x_squared); 58 | float prob = std::pow(det, -0.5) * std::exp(exp_power) / (2 * M_PI); 59 | 60 | if (prob > max_prob) { 61 | max_prob = prob; 62 | max_id = i; 63 | } 64 | } 65 | 66 | // update tracker 67 | if (max_prob >= _prob_threshold) { 68 | _multi_blobs.id = max_id; 69 | const auto x_delta = event.x - _multi_blobs.blobs[max_id].x; 70 | const auto y_delta = event.y - _multi_blobs.blobs[max_id].y; 71 | _multi_blobs.blobs[max_id].x = _position_inertia * _multi_blobs.blobs[max_id].x + (1 - _position_inertia) * event.x; 72 | _multi_blobs.blobs[max_id].y = _position_inertia * _multi_blobs.blobs[max_id].y + (1 - _position_inertia) * event.y; 73 | _multi_blobs.blobs[max_id].sigma_x_squared = _variance_inertia * _multi_blobs.blobs[max_id].sigma_x_squared + (1 - _variance_inertia) * x_delta * x_delta; 74 | _multi_blobs.blobs[max_id].sigma_xy = _variance_inertia * _multi_blobs.blobs[max_id].sigma_xy + (1 - _variance_inertia) * x_delta * y_delta; 75 | _multi_blobs.blobs[max_id].sigma_y_squared = _variance_inertia * _multi_blobs.blobs[max_id].sigma_y_squared + (1 - _variance_inertia) * y_delta * y_delta; 76 | } 77 | 78 | // TODO: compute tracker activity 79 | 80 | _handle_blob(_event_to_blob(event, _multi_blobs)); 81 | } 82 | 83 | protected: 84 | MultiBlobs _multi_blobs; 85 | const float _prob_threshold; 86 | const float _position_inertia; 87 | const float _variance_inertia; 88 | EventToBlob _event_to_blob; 89 | HandleBlob _handle_blob; 90 | }; 91 | 92 | /// make_track_blob_multi creates a track_blob_multi from functors. 93 | template 94 | inline track_blob_multi make_track_blob_multi( 95 | MultiBlobs multi_blobs, 96 | float prob_threshold, 97 | float position_inertia, 98 | float variance_inertia, 99 | EventToBlob&& event_to_blob, 100 | HandleBlob&& handle_blob) { 101 | return track_blob_multi( 102 | multi_blobs, 103 | prob_threshold, 104 | position_inertia, 105 | variance_inertia, 106 | std::forward(event_to_blob), 107 | std::forward(handle_blob)); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /source/compute_flow.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /// tarsier is a collection of event handlers. 9 | namespace tarsier { 10 | /// compute_flow evaluates the optical flow. 11 | template 12 | class compute_flow { 13 | public: 14 | compute_flow( 15 | uint16_t width, 16 | uint16_t height, 17 | uint16_t spatial_window, 18 | uint64_t temporal_window, 19 | std::size_t minimum_number_of_events, 20 | EventToFlow&& event_to_flow, 21 | HandleFlow&& handle_flow) : 22 | _width(width), 23 | _height(height), 24 | _spatial_window(spatial_window), 25 | _temporal_window(temporal_window), 26 | _minimum_number_of_events(minimum_number_of_events), 27 | _event_to_flow(std::forward(event_to_flow)), 28 | _handle_flow(std::forward(handle_flow)), 29 | _ts(width * height, 0) {} 30 | compute_flow(const compute_flow&) = delete; 31 | compute_flow(compute_flow&&) = default; 32 | compute_flow& operator=(const compute_flow&) = delete; 33 | compute_flow& operator=(compute_flow&&) = default; 34 | virtual ~compute_flow() = default; 35 | 36 | /// operator() handles an event. 37 | virtual void operator()(Event event) { 38 | _ts[event.x + event.y * _width] = event.t; 39 | const auto t_threshold = (event.t <= _temporal_window ? 0 : event.t - _temporal_window); 40 | std::vector points; 41 | for (uint16_t y = (event.y <= _spatial_window ? 0 : event.y - _spatial_window); 42 | y <= (event.y >= _height - 1 - _spatial_window ? _height - 1 : event.y + _spatial_window); 43 | ++y) { 44 | for (uint16_t x = (event.x <= _spatial_window ? 0 : event.x - _spatial_window); 45 | x <= (event.x >= _width - 1 - _spatial_window ? _width - 1 : event.x + _spatial_window); 46 | ++x) { 47 | const auto t = _ts[x + y * _width]; 48 | if (t > t_threshold) { 49 | points.push_back(point{ 50 | static_cast(t), 51 | static_cast(x), 52 | static_cast(y), 53 | }); 54 | } 55 | } 56 | } 57 | if (points.size() >= _minimum_number_of_events) { 58 | auto t_mean = 0.0f; 59 | auto x_mean = 0.0f; 60 | auto y_mean = 0.0f; 61 | for (auto point : points) { 62 | t_mean += point.t; 63 | x_mean += point.x; 64 | y_mean += point.y; 65 | } 66 | t_mean /= points.size(); 67 | x_mean /= points.size(); 68 | y_mean /= points.size(); 69 | auto tx_sum = 0.0f; 70 | auto ty_sum = 0.0f; 71 | auto xx_sum = 0.0f; 72 | auto xy_sum = 0.0f; 73 | auto yy_sum = 0.0f; 74 | for (auto point : points) { 75 | const auto t_delta = point.t - t_mean; 76 | const auto x_delta = point.x - x_mean; 77 | const auto y_delta = point.y - y_mean; 78 | tx_sum += t_delta * x_delta; 79 | ty_sum += t_delta * y_delta; 80 | xx_sum += x_delta * x_delta; 81 | xy_sum += x_delta * y_delta; 82 | yy_sum += y_delta * y_delta; 83 | } 84 | const auto t_determinant = xx_sum * yy_sum - xy_sum * xy_sum; 85 | const auto x_determinant = tx_sum * yy_sum - ty_sum * xy_sum; 86 | const auto y_determinant = ty_sum * xx_sum - tx_sum * xy_sum; 87 | const auto inverse_squares_sum = 1.0f / (x_determinant * x_determinant + y_determinant * y_determinant); 88 | _handle_flow(_event_to_flow( 89 | event, 90 | t_determinant * x_determinant * inverse_squares_sum, 91 | t_determinant * y_determinant * inverse_squares_sum)); 92 | } 93 | } 94 | 95 | protected: 96 | /// point represents a point in xyt space. 97 | struct point { 98 | float t; 99 | float x; 100 | float y; 101 | }; 102 | 103 | const uint16_t _width; 104 | const uint16_t _height; 105 | const uint16_t _spatial_window; 106 | const uint64_t _temporal_window; 107 | const std::size_t _minimum_number_of_events; 108 | EventToFlow _event_to_flow; 109 | HandleFlow _handle_flow; 110 | std::vector _ts; 111 | }; 112 | 113 | /// make_compute_flow creates an optical flow estimator from functors. 114 | template 115 | inline compute_flow make_compute_flow( 116 | uint16_t width, 117 | uint16_t height, 118 | uint16_t spatial_window, 119 | uint64_t temporal_window, 120 | std::size_t minimum_number_of_events, 121 | EventToFlow&& EventToflow, 122 | HandleFlow&& handle_flow) { 123 | return compute_flow( 124 | width, 125 | height, 126 | spatial_window, 127 | temporal_window, 128 | minimum_number_of_events, 129 | std::forward(EventToflow), 130 | std::forward(handle_flow)); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /source/merge.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | /// tarsier is a collection of event handlers. 13 | namespace tarsier { 14 | /// merge creates a unique event stream from sources running on different 15 | /// threads. 16 | template 17 | class merge { 18 | public: 19 | merge( 20 | std::size_t fifo_size, 21 | std::chrono::high_resolution_clock::duration sleep_duration, 22 | HandleEvent&& handle_event) : 23 | _fifo_size(fifo_size), 24 | _sleep_duration(sleep_duration), 25 | _handle_event(std::forward(handle_event)), 26 | _running(true) { 27 | for (auto& fifo : _fifos) { 28 | fifo.events.resize(_fifo_size); 29 | fifo.head.store(0, std::memory_order_release); 30 | fifo.tail.store(0, std::memory_order_release); 31 | } 32 | _loop = std::thread([this]() { 33 | while (_running.load(std::memory_order_acquire)) { 34 | auto minimum_t = std::numeric_limits::max(); 35 | std::size_t minimum_source = 0; 36 | auto dispatch = true; 37 | for (std::size_t source = 0; source < sources; ++source) { 38 | if (_next_events_and_exists[source].second) { 39 | if (dispatch && _next_events_and_exists[source].first.t < minimum_t) { 40 | minimum_t = _next_events_and_exists[source].first.t; 41 | minimum_source = source; 42 | } 43 | } else { 44 | const auto current_head = _fifos[source].head.load(std::memory_order_relaxed); 45 | if (current_head == _fifos[source].tail.load(std::memory_order_acquire)) { 46 | dispatch = false; 47 | } else { 48 | _next_events_and_exists[source].first = _fifos[source].events[current_head]; 49 | _fifos[source].head.store((current_head + 1) % _fifo_size, std::memory_order_release); 50 | _next_events_and_exists[source].second = true; 51 | if (dispatch && _next_events_and_exists[source].first.t < minimum_t) { 52 | minimum_t = _next_events_and_exists[source].first.t; 53 | minimum_source = source; 54 | } 55 | } 56 | } 57 | } 58 | if (dispatch) { 59 | _handle_event(_next_events_and_exists[minimum_source].first); 60 | _next_events_and_exists[minimum_source].second = false; 61 | } else { 62 | std::this_thread::sleep_for(_sleep_duration); 63 | } 64 | } 65 | }); 66 | } 67 | merge(const merge&) = delete; 68 | merge(merge&&) = default; 69 | merge& operator=(const merge&) = delete; 70 | merge& operator=(merge&&) = default; 71 | virtual ~merge() { 72 | _running.store(false, std::memory_order_release); 73 | _loop.join(); 74 | std::array has_events; 75 | has_events.fill(true); 76 | for (;;) { 77 | auto minimum_t = std::numeric_limits::max(); 78 | std::size_t minimum_source = 0; 79 | for (std::size_t source = 0; source < sources; ++source) { 80 | if (has_events[source]) { 81 | if (_next_events_and_exists[source].second) { 82 | minimum_t = _next_events_and_exists[source].first.t; 83 | minimum_source = source; 84 | } else { 85 | const auto current_head = _fifos[source].head.load(std::memory_order_relaxed); 86 | if (current_head == _fifos[source].tail.load(std::memory_order_acquire)) { 87 | has_events[source] = false; 88 | } else { 89 | _next_events_and_exists[source].first = _fifos[source].events[current_head]; 90 | _fifos[source].head.store((current_head + 1) % _fifo_size, std::memory_order_release); 91 | _next_events_and_exists[source].second = true; 92 | if (_next_events_and_exists[source].first.t < minimum_t) { 93 | minimum_t = _next_events_and_exists[source].first.t; 94 | minimum_source = source; 95 | } 96 | } 97 | } 98 | } 99 | } 100 | if (minimum_t == std::numeric_limits::max()) { 101 | break; 102 | } else { 103 | _handle_event(_next_events_and_exists[minimum_source].first); 104 | _next_events_and_exists[minimum_source].second = false; 105 | } 106 | } 107 | } 108 | 109 | /// push handles an event from a specified source. 110 | template 111 | bool push(Event event) { 112 | static_assert(source < sources, "source must be in the integer range [0, sources["); 113 | return push(source, event); 114 | } 115 | bool push(std::size_t source, Event event) { 116 | const auto current_tail = _fifos[source].tail.load(std::memory_order_relaxed); 117 | const auto next_tail = (current_tail + 1) % _fifo_size; 118 | if (next_tail == _fifos[source].head.load(std::memory_order_acquire)) { 119 | return false; 120 | } 121 | _fifos[source].events[current_tail] = event; 122 | _fifos[source].tail.store(next_tail, std::memory_order_release); 123 | return true; 124 | } 125 | 126 | protected: 127 | /// fifo stores the variables of a thread-safe fifo. 128 | struct fifo { 129 | std::vector events; 130 | std::atomic head; 131 | std::atomic tail; 132 | }; 133 | 134 | const std::size_t _fifo_size; 135 | const std::chrono::high_resolution_clock::duration _sleep_duration; 136 | HandleEvent _handle_event; 137 | std::array _fifos; 138 | std::array, sources> _next_events_and_exists; 139 | std::thread _loop; 140 | std::atomic_bool _running; 141 | }; 142 | 143 | /// make_merge creates a merge from a functor. 144 | template 145 | inline std::unique_ptr> make_merge( 146 | std::size_t fifo_size, 147 | std::chrono::high_resolution_clock::duration sleep_duration, 148 | HandleEvent&& handle_event) { 149 | return std::unique_ptr>( 150 | new merge(fifo_size, sleep_duration, std::forward(handle_event))); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /template.lua: -------------------------------------------------------------------------------- 1 | local json = (loadfile 'third_party/json.lua/json.lua')() 2 | 3 | -- check throws an error if the given variable does not exist or does not have the expected type. 4 | local function check(name, value, expected_type, json_type_name) 5 | if value == nil then 6 | error(name .. ' is not defined') 7 | end 8 | if type(value) ~= expected_type then 9 | error(name .. ' has an incorrect type (expected ' .. json_type_name .. ')') 10 | end 11 | end 12 | 13 | return function(configuration_filename) 14 | local configuration_file = io.open(configuration_filename, 'r') 15 | if configuration_file == nil then 16 | error('\'' .. configuration_filename .. '\' could not be open for reading') 17 | end 18 | configuration = json.decode(configuration_file:read('*a')) 19 | configuration_file:close() 20 | if type(configuration) ~= 'table' then 21 | error('\'' .. configuration_filename .. '\' does not contain an object') 22 | end 23 | check( 24 | '\'' .. 'configuration_filename' .. '\'', 25 | configuration, 26 | 'table', 27 | 'an object') 28 | check( 29 | '\'' .. configuration_filename .. '\'[\'filename\']', 30 | configuration['filename'], 31 | 'string', 32 | 'a string') 33 | check( 34 | '\'' .. configuration_filename .. '\'[\'name\']', 35 | configuration['name'], 36 | 'string', 37 | 'a string') 38 | check( 39 | '\'' .. configuration_filename .. '\'[\'description\']', 40 | configuration['description'], 41 | 'table', 42 | 'an array') 43 | check( 44 | '\'' .. configuration_filename .. '\'[\'template_parameters\']', 45 | configuration['template_parameters'], 46 | 'table', 47 | 'an array') 48 | check( 49 | '\'' .. configuration_filename .. '\'[\'parameters\']', 50 | configuration['parameters'], 51 | 'table', 52 | 'an array') 53 | check( 54 | '\'' .. configuration_filename .. '\'[\'input\']', 55 | configuration['input'], 56 | 'table', 57 | 'an object') 58 | for key, value in pairs(configuration['description']) do 59 | if type(key) ~= 'number' then 60 | error('\'' .. configuration_filename .. '\'[\'description\'] is not an array') 61 | end 62 | check( 63 | '\'' .. configuration_filename .. '\'[\'description\'][' .. tostring(key - 1) .. ']', 64 | value, 65 | 'string', 66 | 'a string') 67 | end 68 | for key, value in pairs(configuration['template_parameters']) do 69 | if type(key) ~= 'number' then 70 | error('\'' .. configuration_filename .. '\'[\'template_parameters\'] is not an array') 71 | end 72 | local name = '\'' .. configuration_filename .. '\'[\'template_parameters\'][' .. tostring(key - 1) .. ']' 73 | check( 74 | name, 75 | value, 76 | 'table', 77 | 'an object') 78 | check( 79 | name .. '[\'name\']', 80 | value['name'], 81 | 'string', 82 | 'a string') 83 | check( 84 | name .. '[\'type\']', 85 | value['type'], 86 | 'string', 87 | 'a string', 88 | true) 89 | end 90 | for key, value in pairs(configuration['parameters']) do 91 | if type(key) ~= 'number' then 92 | error('\'' .. configuration_filename .. '\'[\'parameters\'] is not an array') 93 | end 94 | local name = '\'' .. configuration_filename .. '\'[\'parameters\'][' .. tostring(key - 1) .. ']' 95 | check( 96 | name, 97 | value, 98 | 'table', 99 | 'an object') 100 | check( 101 | name .. '[\'name\']', 102 | value['name'], 103 | 'string', 104 | 'a string') 105 | check( 106 | name .. '[\'type\']', 107 | value['type'], 108 | 'string', 109 | 'a string') 110 | check( 111 | name .. '[\'store\']', 112 | value['store'], 113 | 'string', 114 | 'a string') 115 | if value['store'] ~= nil 116 | and value['store'] ~= 'no' 117 | and value['store'] ~= 'mutable' 118 | and value['store'] ~= 'constant' 119 | and value['store'] ~= 'forward' then 120 | error(name .. '[\'store\'] must be one of {\'no\', \'mutable\', \'constant\', \'forward\'}') 121 | end 122 | end 123 | check( 124 | '\'' .. configuration_filename .. '\'[\'input\'][\'name\']', 125 | configuration['input']['name'], 126 | 'string', 127 | 'a string') 128 | check( 129 | '\'' .. configuration_filename .. '\'[\'input\'][\'type\']', 130 | configuration['input']['type'], 131 | 'string', 132 | 'a string') 133 | local output_file = io.open(configuration['filename'], 'w') 134 | if output_file == nil then 135 | error('\'' .. configuration['filename'] .. '\' could not be open for writing') 136 | end 137 | local template_parameters = {} 138 | local template_parameters_names = {} 139 | for index, parameter in ipairs(configuration['template_parameters']) do 140 | template_parameters[index] = parameter['type'] .. ' ' .. parameter['name'] 141 | template_parameters_names[index] = parameter['name'] 142 | end 143 | local parameters = {} 144 | local parameters_names = {} 145 | for index, parameter in ipairs(configuration['parameters']) do 146 | if parameter['store'] == 'forward' then 147 | parameters[index] = parameter['type'] .. '&& ' .. parameter['name'] 148 | parameters_names[index] = 'std::forward<' .. parameter['type'] .. '>(' .. parameter['name'] .. ')' 149 | else 150 | parameters[index] = parameter['type'] .. ' ' .. parameter['name'] 151 | parameters_names[index] = parameter['name'] 152 | end 153 | end 154 | local initialization_parameters = {} 155 | local initialization_parameters_index = 1 156 | local has_initialization_parameters = false 157 | for index, parameter in ipairs(configuration['parameters']) do 158 | if parameter['store'] ~= 'no' then 159 | has_initialization_parameters = true 160 | if parameter['store'] == 'forward' then 161 | initialization_parameters[initialization_parameters_index] = '_' .. 162 | parameter['name'] .. 163 | '(std::forward<' .. 164 | parameter['type'] .. 165 | '>(' .. 166 | parameter['name'] .. 167 | '))' 168 | else 169 | initialization_parameters[initialization_parameters_index] = '_' .. parameter['name'] .. '(' .. parameter['name'] .. ')' 170 | end 171 | initialization_parameters_index = initialization_parameters_index + 1 172 | end 173 | end 174 | output_file:write( 175 | '#pragma once\n\n', 176 | '#include \n\n', 177 | '/// tarsier is a collection of event handlers.\n', 178 | 'namespace tarsier {\n') 179 | for key, value in pairs(configuration['description']) do 180 | output_file:write(' /// ', value, '\n') 181 | end 182 | output_file:write( 183 | ' template <', table.concat(template_parameters, ', '), '>\n', 184 | ' class ', configuration['name'], ' {\n', 185 | ' public:\n', 186 | ' ', configuration['name'], '(', table.concat(parameters, ', '), ')') 187 | if has_initialization_parameters then 188 | output_file:write(' :\n', 189 | ' ', table.concat(initialization_parameters, ', ')) 190 | end 191 | output_file:write(' {\n', 192 | ' }\n', 193 | ' ', configuration['name'], '(const ', configuration['name'], '&) = delete;\n', 194 | ' ', configuration['name'], '(', configuration['name'], '&&) = default;\n', 195 | ' ', configuration['name'], '& operator=(const ', configuration['name'], '&) = delete;\n', 196 | ' ', configuration['name'], '& operator=(', configuration['name'], '&&) = default;\n', 197 | ' virtual ~', configuration['name'], '() {}\n\n', 198 | ' /// operator() handles an event.\n', 199 | ' virtual void operator()(', configuration['input']['type'], ' ', configuration['input']['name'], ') {\n', 200 | ' }\n\n', 201 | ' protected:\n') 202 | for index, parameter in ipairs(configuration['parameters']) do 203 | output_file:write(' ') 204 | if parameter['store'] ~= 'no' then 205 | if parameter['store'] == 'constant' then 206 | output_file:write('const ') 207 | end 208 | output_file:write(parameter['type'], ' _', parameter['name'], ';\n') 209 | end 210 | end 211 | output_file:write(' };\n\n', 212 | ' /// make_', configuration['name'], ' creates a') 213 | if string.find(configuration['name'], '^\s*[AEIOUaeiou]') then 214 | output_file:write('n') 215 | end 216 | output_file:write(' ', configuration['name'], ' from functors.\n', 217 | ' template <', table.concat(template_parameters, ', '), '>\n', 218 | ' inline ', configuration['name'], '<', table.concat(template_parameters_names, ', '), '> make_', configuration['name'], '(\n', 219 | ' ', table.concat(parameters, ', '), ') {\n', 220 | ' return ', configuration['name'], '<', table.concat(template_parameters_names, ', '), '>(\n', 221 | ' ', table.concat(parameters_names, ', '), ');\n', 222 | ' }\n', 223 | '}\n') 224 | output_file:close() 225 | end 226 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------