From e9e999e315c146c33eeff574d675a8d3e301ab3d Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Thu, 3 Dec 2020 20:17:16 +0100 Subject: [PATCH 1/6] Fixed RGBW-Sinus animation --- src/Animation/RgbwSinusAnimation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Animation/RgbwSinusAnimation.cpp b/src/Animation/RgbwSinusAnimation.cpp index aef2f12..897fe3f 100644 --- a/src/Animation/RgbwSinusAnimation.cpp +++ b/src/Animation/RgbwSinusAnimation.cpp @@ -32,7 +32,7 @@ void RgbwSinusAnimation::loop(uint64_t frame) for(std::size_t led = 0; led < nled; led++) { for(std::size_t strip = 0; strip < nstrip; strip++) { - uint32_t pixelphase = led * fasttrigon::LUT_SIZE / nstrip + strip * fasttrigon::LUT_SIZE / nstrip; + uint32_t pixelphase = led * fasttrigon::LUT_SIZE / nled + strip * fasttrigon::LUT_SIZE / nstrip; Fader::Color color{ static_cast(((127 + fasttrigon::fastsin(m_phi[0] / m_speedDivider + pixelphase)) * m_brightnessScale) >> 8), From 50fb58e494e75593a325a3bbc497e3d17b16bd8e Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 21:22:56 +0200 Subject: [PATCH 2/6] animation_test: fixed build; test new animation --- animation_test/CMakeLists.txt | 6 +- .../Animation/RgbwPsychedelicAnimation.cpp | 87 ++++++++++++++ .../src/Animation/RgbwPsychedelicAnimation.h | 46 ++++++++ animation_test/src/Fader.cpp | 2 +- animation_test/src/fasttrigon.cpp | 111 ++++++++++++++++++ animation_test/src/fasttrigon.h | 34 ++++++ animation_test/src/main.cpp | 30 +---- 7 files changed, 287 insertions(+), 29 deletions(-) create mode 100644 animation_test/src/Animation/RgbwPsychedelicAnimation.cpp create mode 100644 animation_test/src/Animation/RgbwPsychedelicAnimation.h create mode 100644 animation_test/src/fasttrigon.cpp create mode 100644 animation_test/src/fasttrigon.h diff --git a/animation_test/CMakeLists.txt b/animation_test/CMakeLists.txt index 7c6eb63..9acbc0c 100644 --- a/animation_test/CMakeLists.txt +++ b/animation_test/CMakeLists.txt @@ -3,12 +3,14 @@ cmake_minimum_required(VERSION 3.5) set(SOURCES src/Fader.cpp - src/Animation/ImageScrollerAnimation.cpp + src/Animation/RgbwPsychedelicAnimation.cpp + src/fasttrigon.cpp src/main.cpp src/Animation/Animation.h src/Fader.h - src/Animation/ImageScrollerAnimation.h + src/Animation/RgbwPsychedelicAnimation.h + src/fasttrigon.h ) add_executable(animation_test ${SOURCES}) diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp new file mode 100644 index 0000000..e4046dd --- /dev/null +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp @@ -0,0 +1,87 @@ +#include + +#include "Animation/RgbwPsychedelicAnimation.h" + +RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds, + const rgbw_val_array &tilt_speeds, + int32_t tilt_maxperiods, + uint32_t move_speed_divider, + uint32_t tilt_speed_divider, + uint16_t brightness_scale) + : Animation(fader), + m_move_speeds(move_speeds), + m_tilt_speeds(move_speeds), + m_moveSpeedDivider(move_speed_divider), + m_tiltSpeedDivider(tilt_speed_divider), + m_brightnessScale(brightness_scale), + m_tilt_maxperiods(tilt_maxperiods), + m_move_phi{0,0,0,0}, + m_tilt_phi{100,200,300,400}, + m_moveOverflowInterval(fasttrigon::LUT_SIZE * move_speed_divider), + m_tiltOverflowInterval(fasttrigon::LUT_SIZE * tilt_speed_divider) +{ + reset(); +} + +void RgbwPsychedelicAnimation::loop(uint64_t frame) +{ + std::size_t nled = m_fader->modules_per_strip(); + std::size_t nstrip = m_fader->strips(); + + if(!m_stopping) { + for(unsigned i = 0; i < m_move_speeds.size(); i++) { + m_move_phi[i] += m_move_speeds[i]; + + if(m_move_phi[i] > m_moveOverflowInterval) { + m_move_phi[i] -= m_moveOverflowInterval; + } + } + + rgbw_val_array tilt_nperiods; + + for(unsigned i = 0; i < m_tilt_speeds.size(); i++) { + m_tilt_phi[i] += m_tilt_speeds[i]; + + if(m_tilt_phi[i] > m_tiltOverflowInterval) { + m_tilt_phi[i] -= m_tiltOverflowInterval; + } + + tilt_nperiods[i] = m_tilt_maxperiods * fasttrigon::fastsin(m_tilt_phi[i] / m_tiltSpeedDivider); + } + + + for(std::size_t led = 0; led < nled; led++) { + for(std::size_t strip = 0; strip < nstrip; strip++) { + int32_t x = fasttrigon::LUT_SIZE * led / nled; + int32_t y = fasttrigon::LUT_SIZE * strip / nstrip; + + rgbw_val_array rgbw { + (127 + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> 7) + y)) + }; + + Fader::Color color{ + static_cast(((rgbw[0] * rgbw[0] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[1] * rgbw[1] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[2] * rgbw[2] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[3] * rgbw[3] >> 7) * m_brightnessScale) >> 10)}; // white is too bright otherwise + + m_fader->set_color(strip, led, color); + } + } + } else { + m_fader->fade_color(Fader::Color{0, 0, 0, 0}); + } + + m_fader->update(); + m_running = !m_stopping || m_fader->something_changed(); +} + +void RgbwPsychedelicAnimation::reset(void) +{ + m_stopping = false; + m_running = true; +} diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.h b/animation_test/src/Animation/RgbwPsychedelicAnimation.h new file mode 100644 index 0000000..251f7f6 --- /dev/null +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.h @@ -0,0 +1,46 @@ +#pragma once + +#include + +#include "Animation.h" + +#include "fasttrigon.h" + +class RgbwPsychedelicAnimation : public Animation +{ + public: + typedef std::array rgbw_val_array; + RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds = {2273, 2281, 2287, 2293}, + const rgbw_val_array &tilt_speeds = {2297, 2309, 2311, 2333}, + int32_t tilt_maxperiods = 3, + uint32_t move_speed_divider = 1024, + uint32_t tilt_speed_divider = 8192, + uint16_t brightness_scale = 64); // max: 255 + + void loop(uint64_t frame) override; + + void stop(void) override + { + m_stopping = true; + } + + void reset(void) override; + + private: + bool m_stopping; + + rgbw_val_array m_move_speeds; + rgbw_val_array m_tilt_speeds; + uint32_t m_moveSpeedDivider; + uint32_t m_tiltSpeedDivider; + uint16_t m_brightnessScale; + + int32_t m_tilt_maxperiods; + + rgbw_val_array m_move_phi; + rgbw_val_array m_tilt_phi; + + int32_t m_moveOverflowInterval; + int32_t m_tiltOverflowInterval; +}; diff --git a/animation_test/src/Fader.cpp b/animation_test/src/Fader.cpp index 424ce79..70dafc4 100644 --- a/animation_test/src/Fader.cpp +++ b/animation_test/src/Fader.cpp @@ -104,7 +104,7 @@ void Fader::update(void) target_addr.sin_family = AF_INET; target_addr.sin_port = htons(2703); - target_addr.sin_addr.s_addr = inet_addr("10.42.7.145"); + target_addr.sin_addr.s_addr = inet_addr("192.168.2.145"); int ret = sendto(m_fd, packet.data(), packet.size(), 0, reinterpret_cast(&target_addr), sizeof(target_addr)); if(ret == -1) { diff --git a/animation_test/src/fasttrigon.cpp b/animation_test/src/fasttrigon.cpp new file mode 100644 index 0000000..b0da8e3 --- /dev/null +++ b/animation_test/src/fasttrigon.cpp @@ -0,0 +1,111 @@ +#include + +#include "fasttrigon.h" + +namespace fasttrigon { + static const std::array LUT { + 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, + 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, + 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, + 42, 43, 44, 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, + 54, 55, 56, 56, 57, 58, 58, 59, 60, 61, 61, 62, 63, 63, 64, 65, 65, + 66, 67, 67, 68, 69, 69, 70, 71, 71, 72, 72, 73, 74, 74, 75, 76, 76, + 77, 78, 78, 79, 79, 80, 81, 81, 82, 82, 83, 84, 84, 85, 85, 86, 86, + 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 93, 93, 94, 94, 95, 95, 96, + 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 102, 103, + 103, 104, 104, 105, 105, 106, 106, 106, 107, 107, 108, 108, 109, 109, + 109, 110, 110, 111, 111, 111, 112, 112, 112, 113, 113, 113, 114, 114, + 114, 115, 115, 115, 116, 116, 116, 117, 117, 117, 118, 118, 118, 118, + 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, + 122, 122, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 125, + 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, + 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, + 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, + 127, 127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, + 126, 125, 125, 125, 125, 125, 125, 125, 124, 124, 124, 124, 124, 124, + 123, 123, 123, 123, 123, 122, 122, 122, 122, 122, 121, 121, 121, 121, + 120, 120, 120, 120, 119, 119, 119, 118, 118, 118, 118, 117, 117, 117, + 116, 116, 116, 115, 115, 115, 114, 114, 114, 113, 113, 113, 112, 112, + 112, 111, 111, 111, 110, 110, 109, 109, 109, 108, 108, 107, 107, 106, + 106, 106, 105, 105, 104, 104, 103, 103, 102, 102, 102, 101, 101, 100, + 100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 91, + 91, 90, 90, 89, 89, 88, 88, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, + 81, 81, 80, 79, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, + 71, 70, 69, 69, 68, 67, 67, 66, 65, 65, 64, 63, 63, 62, 61, 61, 60, + 59, 58, 58, 57, 56, 56, 55, 54, 54, 53, 52, 51, 51, 50, 49, 49, 48, + 47, 46, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38, 37, 36, 35, + 35, 34, 33, 32, 32, 31, 30, 29, 29, 28, 27, 26, 26, 25, 24, 23, 22, + 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 14, 13, 12, 12, 11, 10, 9, 9, + 8, 7, 6, 5, 5, 4, 3, 2, 2, 1, 0, -1, -2, -2, -3, -4, -5, -5, -6, -7, + -8, -9, -9, -10, -11, -12, -12, -13, -14, -15, -16, -16, -17, -18, + -19, -19, -20, -21, -22, -22, -23, -24, -25, -26, -26, -27, -28, -29, + -29, -30, -31, -32, -32, -33, -34, -35, -35, -36, -37, -38, -38, -39, + -40, -41, -41, -42, -43, -44, -44, -45, -46, -46, -47, -48, -49, -49, + -50, -51, -51, -52, -53, -54, -54, -55, -56, -56, -57, -58, -58, -59, + -60, -61, -61, -62, -63, -63, -64, -65, -65, -66, -67, -67, -68, -69, + -69, -70, -71, -71, -72, -72, -73, -74, -74, -75, -76, -76, -77, -78, + -78, -79, -79, -80, -81, -81, -82, -82, -83, -84, -84, -85, -85, -86, + -86, -87, -88, -88, -89, -89, -90, -90, -91, -91, -92, -93, -93, -94, + -94, -95, -95, -96, -96, -97, -97, -98, -98, -99, -99, -100, -100, + -101, -101, -102, -102, -102, -103, -103, -104, -104, -105, -105, + -106, -106, -106, -107, -107, -108, -108, -109, -109, -109, -110, + -110, -111, -111, -111, -112, -112, -112, -113, -113, -113, -114, + -114, -114, -115, -115, -115, -116, -116, -116, -117, -117, -117, + -118, -118, -118, -118, -119, -119, -119, -120, -120, -120, -120, + -121, -121, -121, -121, -122, -122, -122, -122, -122, -123, -123, + -123, -123, -123, -124, -124, -124, -124, -124, -124, -125, -125, + -125, -125, -125, -125, -125, -126, -126, -126, -126, -126, -126, + -126, -126, -126, -126, -126, -127, -127, -127, -127, -127, -127, + -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, + -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, + -127, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126, + -126, -125, -125, -125, -125, -125, -125, -125, -124, -124, -124, + -124, -124, -124, -123, -123, -123, -123, -123, -122, -122, -122, + -122, -122, -121, -121, -121, -121, -120, -120, -120, -120, -119, + -119, -119, -118, -118, -118, -118, -117, -117, -117, -116, -116, + -116, -115, -115, -115, -114, -114, -114, -113, -113, -113, -112, + -112, -112, -111, -111, -111, -110, -110, -109, -109, -109, -108, + -108, -107, -107, -106, -106, -106, -105, -105, -104, -104, -103, + -103, -102, -102, -102, -101, -101, -100, -100, -99, -99, -98, -98, + -97, -97, -96, -96, -95, -95, -94, -94, -93, -93, -92, -91, -91, -90, + -90, -89, -89, -88, -88, -87, -86, -86, -85, -85, -84, -84, -83, -82, + -82, -81, -81, -80, -79, -79, -78, -78, -77, -76, -76, -75, -74, -74, + -73, -72, -72, -71, -71, -70, -69, -69, -68, -67, -67, -66, -65, -65, + -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, + -54, -54, -53, -52, -51, -51, -50, -49, -49, -48, -47, -46, -46, -45, + -44, -44, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -35, -35, + -34, -33, -32, -32, -31, -30, -29, -29, -28, -27, -26, -26, -25, -24, + -23, -22, -22, -21, -20, -19, -19, -18, -17, -16, -16, -15, -14, -13, + -12, -12, -11, -10, -9, -9, -8, -7, -6, -5, -5, -4, -3, -2, -2, -1 + }; + + int32_t fastsin(int32_t arg) + { + bool neg = arg < 0; + uint32_t idx; + if(neg) { + idx = (-arg) % LUT_SIZE; + return -LUT[idx]; + } else { + idx = arg % LUT_SIZE; + return LUT[idx]; + } + } + + int32_t fastcos(int32_t arg) + { + return fastsin(arg + LUT_SIZE/4); + } + + int32_t fasttan(int32_t arg) + { + int32_t denom = fastcos(arg); + if(denom == 0) { + return 0x7FFFFFFF; + } + + int32_t numer = fastsin(arg); + + return numer * 127 / denom; + } +} diff --git a/animation_test/src/fasttrigon.h b/animation_test/src/fasttrigon.h new file mode 100644 index 0000000..694418a --- /dev/null +++ b/animation_test/src/fasttrigon.h @@ -0,0 +1,34 @@ +/*! + * \file + * + * This file contains function prototypes for very fast lookup-table based + * trigonometric functions. + * + * The functions take an argument that is dependent on the lookup-table size + * (LUT_SIZE). LUT_SIZE corresponds to 2π. Values outside the range + * [0..LUT_SIZE] are supported and interpreted normally, so just replace 2π + * with LUT_SIZE and you're done. + */ + +#pragma once + +#include + +namespace fasttrigon { + static const uint32_t LUT_SIZE = 1024; + + /*! + * \returns 127 * sin(2π * arg / LUT_SIZE) + */ + int32_t fastsin(int32_t arg); + + /*! + * \returns 127 * cos(2π * arg / LUT_SIZE) + */ + int32_t fastcos(int32_t arg); + + /*! + * \returns 127 * tan(2π * arg / LUT_SIZE) + */ + int32_t fasttan(int32_t arg); +} diff --git a/animation_test/src/main.cpp b/animation_test/src/main.cpp index d19ca0d..4ac915d 100644 --- a/animation_test/src/main.cpp +++ b/animation_test/src/main.cpp @@ -3,35 +3,13 @@ #include #include "Fader.h" -#include "Animation/ImageScrollerAnimation.h" +#include "Animation/RgbwPsychedelicAnimation.h" int main(void) { Fader fader(8, 16); - ImageScrollerAnimation::Image img(12, 3); - img.pixel( 0 + 1, 0).r = 48; - img.pixel( 0 + 2, 1).r = 48; - img.pixel( 0 + 0, 2).r = 48; - img.pixel( 0 + 1, 2).r = 48; - img.pixel( 0 + 2, 2).r = 48; - img.pixel( 3 + 1, 0).g = 48; - img.pixel( 3 + 2, 1).g = 48; - img.pixel( 3 + 0, 2).g = 48; - img.pixel( 3 + 1, 2).g = 48; - img.pixel( 3 + 2, 2).g = 48; - img.pixel( 6 + 1, 0).b = 48; - img.pixel( 6 + 2, 1).b = 48; - img.pixel( 6 + 0, 2).b = 48; - img.pixel( 6 + 1, 2).b = 48; - img.pixel( 6 + 2, 2).b = 48; - img.pixel( 9 + 1, 0).w = 48; - img.pixel( 9 + 2, 1).w = 48; - img.pixel( 9 + 0, 2).w = 48; - img.pixel( 9 + 1, 2).w = 48; - img.pixel( 9 + 2, 2).w = 48; - - Animation *animation = new ImageScrollerAnimation(&fader, &img); + Animation *animation = new RgbwPsychedelicAnimation(&fader); for(uint64_t frame = 0; !animation->finished(); frame++) { animation->loop(frame); @@ -40,11 +18,11 @@ int main(void) fader.update(); } - if(frame == 3000) { + if(frame == 60 * 900) { animation->stop(); } - usleep(100000); + usleep(16667); }; delete animation; From 75c17f28f8c5bbcebd48c69d6fc021bb32a151d5 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 21:24:44 +0200 Subject: [PATCH 3/6] Added new RgbwPsychedelicAnimation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s an extension to the previous RgbwSinusAnimation and tilts and moves the sine waves differently for each color band. Additive color mixing generates very interesting patterns. --- include/Animation/AllAnimations.h | 1 + include/Animation/RgbwPsychedelicAnimation.h | 46 +++++++++++ src/Animation/RgbwPsychedelicAnimation.cpp | 87 ++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 include/Animation/RgbwPsychedelicAnimation.h create mode 100644 src/Animation/RgbwPsychedelicAnimation.cpp diff --git a/include/Animation/AllAnimations.h b/include/Animation/AllAnimations.h index 1176eeb..8fd2e14 100644 --- a/include/Animation/AllAnimations.h +++ b/include/Animation/AllAnimations.h @@ -8,5 +8,6 @@ #include "ImageScrollerAnimation.h" #include "MatrixCodeAnimation.h" #include "RgbwSinusAnimation.h" +#include "RgbwPsychedelicAnimation.h" #include "SnowfallAnimation.h" #include "StellarAnimation.h" diff --git a/include/Animation/RgbwPsychedelicAnimation.h b/include/Animation/RgbwPsychedelicAnimation.h new file mode 100644 index 0000000..251f7f6 --- /dev/null +++ b/include/Animation/RgbwPsychedelicAnimation.h @@ -0,0 +1,46 @@ +#pragma once + +#include + +#include "Animation.h" + +#include "fasttrigon.h" + +class RgbwPsychedelicAnimation : public Animation +{ + public: + typedef std::array rgbw_val_array; + RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds = {2273, 2281, 2287, 2293}, + const rgbw_val_array &tilt_speeds = {2297, 2309, 2311, 2333}, + int32_t tilt_maxperiods = 3, + uint32_t move_speed_divider = 1024, + uint32_t tilt_speed_divider = 8192, + uint16_t brightness_scale = 64); // max: 255 + + void loop(uint64_t frame) override; + + void stop(void) override + { + m_stopping = true; + } + + void reset(void) override; + + private: + bool m_stopping; + + rgbw_val_array m_move_speeds; + rgbw_val_array m_tilt_speeds; + uint32_t m_moveSpeedDivider; + uint32_t m_tiltSpeedDivider; + uint16_t m_brightnessScale; + + int32_t m_tilt_maxperiods; + + rgbw_val_array m_move_phi; + rgbw_val_array m_tilt_phi; + + int32_t m_moveOverflowInterval; + int32_t m_tiltOverflowInterval; +}; diff --git a/src/Animation/RgbwPsychedelicAnimation.cpp b/src/Animation/RgbwPsychedelicAnimation.cpp new file mode 100644 index 0000000..e4046dd --- /dev/null +++ b/src/Animation/RgbwPsychedelicAnimation.cpp @@ -0,0 +1,87 @@ +#include + +#include "Animation/RgbwPsychedelicAnimation.h" + +RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, + const rgbw_val_array &move_speeds, + const rgbw_val_array &tilt_speeds, + int32_t tilt_maxperiods, + uint32_t move_speed_divider, + uint32_t tilt_speed_divider, + uint16_t brightness_scale) + : Animation(fader), + m_move_speeds(move_speeds), + m_tilt_speeds(move_speeds), + m_moveSpeedDivider(move_speed_divider), + m_tiltSpeedDivider(tilt_speed_divider), + m_brightnessScale(brightness_scale), + m_tilt_maxperiods(tilt_maxperiods), + m_move_phi{0,0,0,0}, + m_tilt_phi{100,200,300,400}, + m_moveOverflowInterval(fasttrigon::LUT_SIZE * move_speed_divider), + m_tiltOverflowInterval(fasttrigon::LUT_SIZE * tilt_speed_divider) +{ + reset(); +} + +void RgbwPsychedelicAnimation::loop(uint64_t frame) +{ + std::size_t nled = m_fader->modules_per_strip(); + std::size_t nstrip = m_fader->strips(); + + if(!m_stopping) { + for(unsigned i = 0; i < m_move_speeds.size(); i++) { + m_move_phi[i] += m_move_speeds[i]; + + if(m_move_phi[i] > m_moveOverflowInterval) { + m_move_phi[i] -= m_moveOverflowInterval; + } + } + + rgbw_val_array tilt_nperiods; + + for(unsigned i = 0; i < m_tilt_speeds.size(); i++) { + m_tilt_phi[i] += m_tilt_speeds[i]; + + if(m_tilt_phi[i] > m_tiltOverflowInterval) { + m_tilt_phi[i] -= m_tiltOverflowInterval; + } + + tilt_nperiods[i] = m_tilt_maxperiods * fasttrigon::fastsin(m_tilt_phi[i] / m_tiltSpeedDivider); + } + + + for(std::size_t led = 0; led < nled; led++) { + for(std::size_t strip = 0; strip < nstrip; strip++) { + int32_t x = fasttrigon::LUT_SIZE * led / nled; + int32_t y = fasttrigon::LUT_SIZE * strip / nstrip; + + rgbw_val_array rgbw { + (127 + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> 7) + y)), + (127 + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> 7) + y)) + }; + + Fader::Color color{ + static_cast(((rgbw[0] * rgbw[0] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[1] * rgbw[1] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[2] * rgbw[2] >> 7) * m_brightnessScale) >> 8), + static_cast(((rgbw[3] * rgbw[3] >> 7) * m_brightnessScale) >> 10)}; // white is too bright otherwise + + m_fader->set_color(strip, led, color); + } + } + } else { + m_fader->fade_color(Fader::Color{0, 0, 0, 0}); + } + + m_fader->update(); + m_running = !m_stopping || m_fader->something_changed(); +} + +void RgbwPsychedelicAnimation::reset(void) +{ + m_stopping = false; + m_running = true; +} From bd6489e3470d41ec1772adcd26922d2410c08399 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 22:00:58 +0200 Subject: [PATCH 4/6] Make the new animation on of the default animations --- include/Animation/AnimationController.h | 4 +++- src/Animation/AnimationController.cpp | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/Animation/AnimationController.h b/include/Animation/AnimationController.h index 943d4f2..c497a2b 100644 --- a/include/Animation/AnimationController.h +++ b/include/Animation/AnimationController.h @@ -20,6 +20,7 @@ class AnimationController FIREWORK = 5, STELLAR = 6, RGBW_SINUS = 7, + RGBW_PSYCHEDELIC = 8, NUM_DEFAULT_ANIMATIONS }; @@ -38,7 +39,8 @@ class AnimationController "Matrix Code", "Fireworks", "Twinkling Sky", - "RGBW Sinus" + "RGBW Sinus", + "RGBW Psychedelic" }; AnimationController(Fader *fader); diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 16643d6..a8e4081 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -78,6 +78,10 @@ void AnimationController::changeAnimation(AnimationController::DefaultAnimation anim.reset(new RgbwSinusAnimation(m_fader)); break; + case RGBW_PSYCHEDELIC: + anim.reset(new RgbwPsychedelicAnimation(m_fader)); + break; + default: return; // unknown id, do nothing } From 10892e1a295afa9d28d7740f2ab63fbfaf82cc5a Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 23:40:16 +0200 Subject: [PATCH 5/6] animation_test: remove flickering by increased LUT size --- .gitignore | 2 + .../Animation/RgbwPsychedelicAnimation.cpp | 25 +- .../src/Animation/RgbwPsychedelicAnimation.h | 2 +- animation_test/src/fasttrigon.cpp | 250 ++++++++++++------ animation_test/src/fasttrigon.h | 7 +- animation_test/src/main.cpp | 2 +- scripts/fasttrigon_gen_lut.py | 17 ++ 7 files changed, 218 insertions(+), 87 deletions(-) create mode 100755 scripts/fasttrigon_gen_lut.py diff --git a/.gitignore b/.gitignore index 567c20a..c6ae3db 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ data/etc/* !data/etc/.* + +animation_test/build/ diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp index e4046dd..f59b8f0 100644 --- a/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.cpp @@ -1,5 +1,7 @@ #include +//#include + #include "Animation/RgbwPsychedelicAnimation.h" RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, @@ -16,8 +18,8 @@ RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, m_tiltSpeedDivider(tilt_speed_divider), m_brightnessScale(brightness_scale), m_tilt_maxperiods(tilt_maxperiods), - m_move_phi{0,0,0,0}, - m_tilt_phi{100,200,300,400}, + m_move_phi{800,600,400,200}, + m_tilt_phi{200,400,600,800}, m_moveOverflowInterval(fasttrigon::LUT_SIZE * move_speed_divider), m_tiltOverflowInterval(fasttrigon::LUT_SIZE * tilt_speed_divider) { @@ -57,17 +59,20 @@ void RgbwPsychedelicAnimation::loop(uint64_t frame) int32_t y = fasttrigon::LUT_SIZE * strip / nstrip; rgbw_val_array rgbw { - (127 + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> 7) + y)) + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> fasttrigon::UNIT_SHIFT) + y)) }; Fader::Color color{ - static_cast(((rgbw[0] * rgbw[0] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[1] * rgbw[1] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[2] * rgbw[2] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[3] * rgbw[3] >> 7) * m_brightnessScale) >> 10)}; // white is too bright otherwise + static_cast((FASTTRIGON_8BIT(rgbw[0] * rgbw[0] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[1] * rgbw[1] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[2] * rgbw[2] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[3] * rgbw[3] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> (fasttrigon::PRECISION_BITS + 2))}; // white is too bright otherwise + + //std::cerr << rgbw[0] << " " << rgbw[1] << " " << rgbw[2] << " " << rgbw[3] << std::endl; + //std::cerr << color.r << " " << color.g << " " << color.b << " " << color.w << std::endl; m_fader->set_color(strip, led, color); } diff --git a/animation_test/src/Animation/RgbwPsychedelicAnimation.h b/animation_test/src/Animation/RgbwPsychedelicAnimation.h index 251f7f6..764be1d 100644 --- a/animation_test/src/Animation/RgbwPsychedelicAnimation.h +++ b/animation_test/src/Animation/RgbwPsychedelicAnimation.h @@ -16,7 +16,7 @@ class RgbwPsychedelicAnimation : public Animation int32_t tilt_maxperiods = 3, uint32_t move_speed_divider = 1024, uint32_t tilt_speed_divider = 8192, - uint16_t brightness_scale = 64); // max: 255 + uint16_t brightness_scale = fasttrigon::SCALE/4); // max: fasttrigon::SCALE void loop(uint64_t frame) override; diff --git a/animation_test/src/fasttrigon.cpp b/animation_test/src/fasttrigon.cpp index b0da8e3..248d0c3 100644 --- a/animation_test/src/fasttrigon.cpp +++ b/animation_test/src/fasttrigon.cpp @@ -3,80 +3,182 @@ #include "fasttrigon.h" namespace fasttrigon { - static const std::array LUT { - 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, - 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, - 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, - 42, 43, 44, 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, - 54, 55, 56, 56, 57, 58, 58, 59, 60, 61, 61, 62, 63, 63, 64, 65, 65, - 66, 67, 67, 68, 69, 69, 70, 71, 71, 72, 72, 73, 74, 74, 75, 76, 76, - 77, 78, 78, 79, 79, 80, 81, 81, 82, 82, 83, 84, 84, 85, 85, 86, 86, - 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 93, 93, 94, 94, 95, 95, 96, - 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 102, 103, - 103, 104, 104, 105, 105, 106, 106, 106, 107, 107, 108, 108, 109, 109, - 109, 110, 110, 111, 111, 111, 112, 112, 112, 113, 113, 113, 114, 114, - 114, 115, 115, 115, 116, 116, 116, 117, 117, 117, 118, 118, 118, 118, - 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, - 122, 122, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 125, - 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, - 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, - 126, 125, 125, 125, 125, 125, 125, 125, 124, 124, 124, 124, 124, 124, - 123, 123, 123, 123, 123, 122, 122, 122, 122, 122, 121, 121, 121, 121, - 120, 120, 120, 120, 119, 119, 119, 118, 118, 118, 118, 117, 117, 117, - 116, 116, 116, 115, 115, 115, 114, 114, 114, 113, 113, 113, 112, 112, - 112, 111, 111, 111, 110, 110, 109, 109, 109, 108, 108, 107, 107, 106, - 106, 106, 105, 105, 104, 104, 103, 103, 102, 102, 102, 101, 101, 100, - 100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 91, - 91, 90, 90, 89, 89, 88, 88, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, - 81, 81, 80, 79, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, - 71, 70, 69, 69, 68, 67, 67, 66, 65, 65, 64, 63, 63, 62, 61, 61, 60, - 59, 58, 58, 57, 56, 56, 55, 54, 54, 53, 52, 51, 51, 50, 49, 49, 48, - 47, 46, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38, 37, 36, 35, - 35, 34, 33, 32, 32, 31, 30, 29, 29, 28, 27, 26, 26, 25, 24, 23, 22, - 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 14, 13, 12, 12, 11, 10, 9, 9, - 8, 7, 6, 5, 5, 4, 3, 2, 2, 1, 0, -1, -2, -2, -3, -4, -5, -5, -6, -7, - -8, -9, -9, -10, -11, -12, -12, -13, -14, -15, -16, -16, -17, -18, - -19, -19, -20, -21, -22, -22, -23, -24, -25, -26, -26, -27, -28, -29, - -29, -30, -31, -32, -32, -33, -34, -35, -35, -36, -37, -38, -38, -39, - -40, -41, -41, -42, -43, -44, -44, -45, -46, -46, -47, -48, -49, -49, - -50, -51, -51, -52, -53, -54, -54, -55, -56, -56, -57, -58, -58, -59, - -60, -61, -61, -62, -63, -63, -64, -65, -65, -66, -67, -67, -68, -69, - -69, -70, -71, -71, -72, -72, -73, -74, -74, -75, -76, -76, -77, -78, - -78, -79, -79, -80, -81, -81, -82, -82, -83, -84, -84, -85, -85, -86, - -86, -87, -88, -88, -89, -89, -90, -90, -91, -91, -92, -93, -93, -94, - -94, -95, -95, -96, -96, -97, -97, -98, -98, -99, -99, -100, -100, - -101, -101, -102, -102, -102, -103, -103, -104, -104, -105, -105, - -106, -106, -106, -107, -107, -108, -108, -109, -109, -109, -110, - -110, -111, -111, -111, -112, -112, -112, -113, -113, -113, -114, - -114, -114, -115, -115, -115, -116, -116, -116, -117, -117, -117, - -118, -118, -118, -118, -119, -119, -119, -120, -120, -120, -120, - -121, -121, -121, -121, -122, -122, -122, -122, -122, -123, -123, - -123, -123, -123, -124, -124, -124, -124, -124, -124, -125, -125, - -125, -125, -125, -125, -125, -126, -126, -126, -126, -126, -126, - -126, -126, -126, -126, -126, -127, -127, -127, -127, -127, -127, - -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, - -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, - -127, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126, - -126, -125, -125, -125, -125, -125, -125, -125, -124, -124, -124, - -124, -124, -124, -123, -123, -123, -123, -123, -122, -122, -122, - -122, -122, -121, -121, -121, -121, -120, -120, -120, -120, -119, - -119, -119, -118, -118, -118, -118, -117, -117, -117, -116, -116, - -116, -115, -115, -115, -114, -114, -114, -113, -113, -113, -112, - -112, -112, -111, -111, -111, -110, -110, -109, -109, -109, -108, - -108, -107, -107, -106, -106, -106, -105, -105, -104, -104, -103, - -103, -102, -102, -102, -101, -101, -100, -100, -99, -99, -98, -98, - -97, -97, -96, -96, -95, -95, -94, -94, -93, -93, -92, -91, -91, -90, - -90, -89, -89, -88, -88, -87, -86, -86, -85, -85, -84, -84, -83, -82, - -82, -81, -81, -80, -79, -79, -78, -78, -77, -76, -76, -75, -74, -74, - -73, -72, -72, -71, -71, -70, -69, -69, -68, -67, -67, -66, -65, -65, - -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, - -54, -54, -53, -52, -51, -51, -50, -49, -49, -48, -47, -46, -46, -45, - -44, -44, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -35, -35, - -34, -33, -32, -32, -31, -30, -29, -29, -28, -27, -26, -26, -25, -24, - -23, -22, -22, -21, -20, -19, -19, -18, -17, -16, -16, -15, -14, -13, - -12, -12, -11, -10, -9, -9, -8, -7, -6, -5, -5, -4, -3, -2, -2, -1 + static const std::array LUT { + 0, 3, 6, 9, 12, 15, 18, 21, 25, 28, 31, 34, 37, 40, 43, 47, 50, 53, 56, + 59, 62, 65, 68, 72, 75, 78, 81, 84, 87, 90, 94, 97, 100, 103, 106, + 109, 112, 115, 118, 122, 125, 128, 131, 134, 137, 140, 143, 147, + 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 181, 184, 187, + 190, 193, 196, 199, 202, 205, 208, 211, 214, 218, 221, 224, 227, + 230, 233, 236, 239, 242, 245, 248, 251, 254, 257, 260, 263, 266, + 269, 272, 275, 278, 281, 284, 287, 290, 293, 296, 299, 302, 305, + 308, 311, 314, 317, 320, 323, 326, 329, 332, 335, 338, 341, 344, + 347, 350, 353, 356, 359, 362, 365, 368, 371, 374, 376, 379, 382, + 385, 388, 391, 394, 397, 400, 403, 405, 408, 411, 414, 417, 420, + 423, 426, 428, 431, 434, 437, 440, 443, 445, 448, 451, 454, 457, + 459, 462, 465, 468, 471, 473, 476, 479, 482, 485, 487, 490, 493, + 496, 498, 501, 504, 506, 509, 512, 515, 517, 520, 523, 525, 528, + 531, 533, 536, 539, 541, 544, 547, 549, 552, 555, 557, 560, 563, + 565, 568, 570, 573, 576, 578, 581, 583, 586, 589, 591, 594, 596, + 599, 601, 604, 606, 609, 611, 614, 616, 619, 621, 624, 626, 629, + 631, 634, 636, 639, 641, 644, 646, 648, 651, 653, 656, 658, 661, + 663, 665, 668, 670, 672, 675, 677, 679, 682, 684, 687, 689, 691, + 693, 696, 698, 700, 703, 705, 707, 709, 712, 714, 716, 718, 721, + 723, 725, 727, 729, 732, 734, 736, 738, 740, 743, 745, 747, 749, + 751, 753, 755, 757, 760, 762, 764, 766, 768, 770, 772, 774, 776, + 778, 780, 782, 784, 786, 788, 790, 792, 794, 796, 798, 800, 802, + 804, 806, 808, 810, 812, 814, 816, 817, 819, 821, 823, 825, 827, + 829, 830, 832, 834, 836, 838, 839, 841, 843, 845, 847, 848, 850, + 852, 854, 855, 857, 859, 860, 862, 864, 865, 867, 869, 870, 872, + 874, 875, 877, 879, 880, 882, 883, 885, 886, 888, 890, 891, 893, + 894, 896, 897, 899, 900, 902, 903, 905, 906, 908, 909, 910, 912, + 913, 915, 916, 917, 919, 920, 922, 923, 924, 926, 927, 928, 930, + 931, 932, 933, 935, 936, 937, 939, 940, 941, 942, 943, 945, 946, + 947, 948, 949, 951, 952, 953, 954, 955, 956, 957, 958, 959, 961, + 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, + 975, 976, 977, 978, 978, 979, 980, 981, 982, 983, 984, 985, 985, + 986, 987, 988, 989, 990, 990, 991, 992, 993, 993, 994, 995, 996, + 996, 997, 998, 998, 999, 1000, 1000, 1001, 1002, 1002, 1003, 1003, + 1004, 1005, 1005, 1006, 1006, 1007, 1007, 1008, 1008, 1009, 1010, + 1010, 1010, 1011, 1011, 1012, 1012, 1013, 1013, 1014, 1014, 1014, + 1015, 1015, 1016, 1016, 1016, 1017, 1017, 1017, 1018, 1018, 1018, + 1018, 1019, 1019, 1019, 1019, 1020, 1020, 1020, 1020, 1021, 1021, + 1021, 1021, 1021, 1021, 1022, 1022, 1022, 1022, 1022, 1022, 1022, + 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1023, 1022, 1022, 1022, + 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, + 1021, 1021, 1021, 1021, 1021, 1021, 1020, 1020, 1020, 1020, 1019, + 1019, 1019, 1019, 1018, 1018, 1018, 1018, 1017, 1017, 1017, 1016, + 1016, 1016, 1015, 1015, 1014, 1014, 1014, 1013, 1013, 1012, 1012, + 1011, 1011, 1010, 1010, 1010, 1009, 1008, 1008, 1007, 1007, 1006, + 1006, 1005, 1005, 1004, 1003, 1003, 1002, 1002, 1001, 1000, 1000, + 999, 998, 998, 997, 996, 996, 995, 994, 993, 993, 992, 991, 990, + 990, 989, 988, 987, 986, 985, 985, 984, 983, 982, 981, 980, 979, + 978, 978, 977, 976, 975, 974, 973, 972, 971, 970, 969, 968, 967, + 966, 965, 964, 963, 962, 961, 959, 958, 957, 956, 955, 954, 953, + 952, 951, 949, 948, 947, 946, 945, 943, 942, 941, 940, 939, 937, + 936, 935, 933, 932, 931, 930, 928, 927, 926, 924, 923, 922, 920, + 919, 917, 916, 915, 913, 912, 910, 909, 908, 906, 905, 903, 902, + 900, 899, 897, 896, 894, 893, 891, 890, 888, 886, 885, 883, 882, + 880, 879, 877, 875, 874, 872, 870, 869, 867, 865, 864, 862, 860, + 859, 857, 855, 854, 852, 850, 848, 847, 845, 843, 841, 839, 838, + 836, 834, 832, 830, 829, 827, 825, 823, 821, 819, 817, 816, 814, + 812, 810, 808, 806, 804, 802, 800, 798, 796, 794, 792, 790, 788, + 786, 784, 782, 780, 778, 776, 774, 772, 770, 768, 766, 764, 762, + 760, 757, 755, 753, 751, 749, 747, 745, 743, 740, 738, 736, 734, + 732, 729, 727, 725, 723, 721, 718, 716, 714, 712, 709, 707, 705, + 703, 700, 698, 696, 693, 691, 689, 687, 684, 682, 679, 677, 675, + 672, 670, 668, 665, 663, 661, 658, 656, 653, 651, 648, 646, 644, + 641, 639, 636, 634, 631, 629, 626, 624, 621, 619, 616, 614, 611, + 609, 606, 604, 601, 599, 596, 594, 591, 589, 586, 583, 581, 578, + 576, 573, 570, 568, 565, 563, 560, 557, 555, 552, 549, 547, 544, + 541, 539, 536, 533, 531, 528, 525, 523, 520, 517, 515, 512, 509, + 506, 504, 501, 498, 496, 493, 490, 487, 485, 482, 479, 476, 473, + 471, 468, 465, 462, 459, 457, 454, 451, 448, 445, 443, 440, 437, + 434, 431, 428, 426, 423, 420, 417, 414, 411, 408, 405, 403, 400, + 397, 394, 391, 388, 385, 382, 379, 376, 374, 371, 368, 365, 362, + 359, 356, 353, 350, 347, 344, 341, 338, 335, 332, 329, 326, 323, + 320, 317, 314, 311, 308, 305, 302, 299, 296, 293, 290, 287, 284, + 281, 278, 275, 272, 269, 266, 263, 260, 257, 254, 251, 248, 245, + 242, 239, 236, 233, 230, 227, 224, 221, 218, 214, 211, 208, 205, + 202, 199, 196, 193, 190, 187, 184, 181, 177, 174, 171, 168, 165, + 162, 159, 156, 153, 150, 147, 143, 140, 137, 134, 131, 128, 125, + 122, 118, 115, 112, 109, 106, 103, 100, 97, 94, 90, 87, 84, 81, 78, + 75, 72, 68, 65, 62, 59, 56, 53, 50, 47, 43, 40, 37, 34, 31, 28, 25, + 21, 18, 15, 12, 9, 6, 3, 0, -3, -6, -9, -12, -15, -18, -21, -25, + -28, -31, -34, -37, -40, -43, -47, -50, -53, -56, -59, -62, -65, + -68, -72, -75, -78, -81, -84, -87, -90, -94, -97, -100, -103, -106, + -109, -112, -115, -118, -122, -125, -128, -131, -134, -137, -140, + -143, -147, -150, -153, -156, -159, -162, -165, -168, -171, -174, + -177, -181, -184, -187, -190, -193, -196, -199, -202, -205, -208, + -211, -214, -218, -221, -224, -227, -230, -233, -236, -239, -242, + -245, -248, -251, -254, -257, -260, -263, -266, -269, -272, -275, + -278, -281, -284, -287, -290, -293, -296, -299, -302, -305, -308, + -311, -314, -317, -320, -323, -326, -329, -332, -335, -338, -341, + -344, -347, -350, -353, -356, -359, -362, -365, -368, -371, -374, + -376, -379, -382, -385, -388, -391, -394, -397, -400, -403, -405, + -408, -411, -414, -417, -420, -423, -426, -428, -431, -434, -437, + -440, -443, -445, -448, -451, -454, -457, -459, -462, -465, -468, + -471, -473, -476, -479, -482, -485, -487, -490, -493, -496, -498, + -501, -504, -506, -509, -512, -515, -517, -520, -523, -525, -528, + -531, -533, -536, -539, -541, -544, -547, -549, -552, -555, -557, + -560, -563, -565, -568, -570, -573, -576, -578, -581, -583, -586, + -589, -591, -594, -596, -599, -601, -604, -606, -609, -611, -614, + -616, -619, -621, -624, -626, -629, -631, -634, -636, -639, -641, + -644, -646, -648, -651, -653, -656, -658, -661, -663, -665, -668, + -670, -672, -675, -677, -679, -682, -684, -687, -689, -691, -693, + -696, -698, -700, -703, -705, -707, -709, -712, -714, -716, -718, + -721, -723, -725, -727, -729, -732, -734, -736, -738, -740, -743, + -745, -747, -749, -751, -753, -755, -757, -760, -762, -764, -766, + -768, -770, -772, -774, -776, -778, -780, -782, -784, -786, -788, + -790, -792, -794, -796, -798, -800, -802, -804, -806, -808, -810, + -812, -814, -816, -817, -819, -821, -823, -825, -827, -829, -830, + -832, -834, -836, -838, -839, -841, -843, -845, -847, -848, -850, + -852, -854, -855, -857, -859, -860, -862, -864, -865, -867, -869, + -870, -872, -874, -875, -877, -879, -880, -882, -883, -885, -886, + -888, -890, -891, -893, -894, -896, -897, -899, -900, -902, -903, + -905, -906, -908, -909, -910, -912, -913, -915, -916, -917, -919, + -920, -922, -923, -924, -926, -927, -928, -930, -931, -932, -933, + -935, -936, -937, -939, -940, -941, -942, -943, -945, -946, -947, + -948, -949, -951, -952, -953, -954, -955, -956, -957, -958, -959, + -961, -962, -963, -964, -965, -966, -967, -968, -969, -970, -971, + -972, -973, -974, -975, -976, -977, -978, -978, -979, -980, -981, + -982, -983, -984, -985, -985, -986, -987, -988, -989, -990, -990, + -991, -992, -993, -993, -994, -995, -996, -996, -997, -998, -998, + -999, -1000, -1000, -1001, -1002, -1002, -1003, -1003, -1004, + -1005, -1005, -1006, -1006, -1007, -1007, -1008, -1008, -1009, + -1010, -1010, -1010, -1011, -1011, -1012, -1012, -1013, -1013, + -1014, -1014, -1014, -1015, -1015, -1016, -1016, -1016, -1017, + -1017, -1017, -1018, -1018, -1018, -1018, -1019, -1019, -1019, + -1019, -1020, -1020, -1020, -1020, -1021, -1021, -1021, -1021, + -1021, -1021, -1022, -1022, -1022, -1022, -1022, -1022, -1022, + -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1023, -1022, + -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1022, + -1022, -1022, -1022, -1022, -1021, -1021, -1021, -1021, -1021, + -1021, -1020, -1020, -1020, -1020, -1019, -1019, -1019, -1019, + -1018, -1018, -1018, -1018, -1017, -1017, -1017, -1016, -1016, + -1016, -1015, -1015, -1014, -1014, -1014, -1013, -1013, -1012, + -1012, -1011, -1011, -1010, -1010, -1010, -1009, -1008, -1008, + -1007, -1007, -1006, -1006, -1005, -1005, -1004, -1003, -1003, + -1002, -1002, -1001, -1000, -1000, -999, -998, -998, -997, -996, + -996, -995, -994, -993, -993, -992, -991, -990, -990, -989, -988, + -987, -986, -985, -985, -984, -983, -982, -981, -980, -979, -978, + -978, -977, -976, -975, -974, -973, -972, -971, -970, -969, -968, + -967, -966, -965, -964, -963, -962, -961, -959, -958, -957, -956, + -955, -954, -953, -952, -951, -949, -948, -947, -946, -945, -943, + -942, -941, -940, -939, -937, -936, -935, -933, -932, -931, -930, + -928, -927, -926, -924, -923, -922, -920, -919, -917, -916, -915, + -913, -912, -910, -909, -908, -906, -905, -903, -902, -900, -899, + -897, -896, -894, -893, -891, -890, -888, -886, -885, -883, -882, + -880, -879, -877, -875, -874, -872, -870, -869, -867, -865, -864, + -862, -860, -859, -857, -855, -854, -852, -850, -848, -847, -845, + -843, -841, -839, -838, -836, -834, -832, -830, -829, -827, -825, + -823, -821, -819, -817, -816, -814, -812, -810, -808, -806, -804, + -802, -800, -798, -796, -794, -792, -790, -788, -786, -784, -782, + -780, -778, -776, -774, -772, -770, -768, -766, -764, -762, -760, + -757, -755, -753, -751, -749, -747, -745, -743, -740, -738, -736, + -734, -732, -729, -727, -725, -723, -721, -718, -716, -714, -712, + -709, -707, -705, -703, -700, -698, -696, -693, -691, -689, -687, + -684, -682, -679, -677, -675, -672, -670, -668, -665, -663, -661, + -658, -656, -653, -651, -648, -646, -644, -641, -639, -636, -634, + -631, -629, -626, -624, -621, -619, -616, -614, -611, -609, -606, + -604, -601, -599, -596, -594, -591, -589, -586, -583, -581, -578, + -576, -573, -570, -568, -565, -563, -560, -557, -555, -552, -549, + -547, -544, -541, -539, -536, -533, -531, -528, -525, -523, -520, + -517, -515, -512, -509, -506, -504, -501, -498, -496, -493, -490, + -487, -485, -482, -479, -476, -473, -471, -468, -465, -462, -459, + -457, -454, -451, -448, -445, -443, -440, -437, -434, -431, -428, + -426, -423, -420, -417, -414, -411, -408, -405, -403, -400, -397, + -394, -391, -388, -385, -382, -379, -376, -374, -371, -368, -365, + -362, -359, -356, -353, -350, -347, -344, -341, -338, -335, -332, + -329, -326, -323, -320, -317, -314, -311, -308, -305, -302, -299, + -296, -293, -290, -287, -284, -281, -278, -275, -272, -269, -266, + -263, -260, -257, -254, -251, -248, -245, -242, -239, -236, -233, + -230, -227, -224, -221, -218, -214, -211, -208, -205, -202, -199, + -196, -193, -190, -187, -184, -181, -177, -174, -171, -168, -165, + -162, -159, -156, -153, -150, -147, -143, -140, -137, -134, -131, + -128, -125, -122, -118, -115, -112, -109, -106, -103, -100, -97, + -94, -90, -87, -84, -81, -78, -75, -72, -68, -65, -62, -59, -56, + -53, -50, -47, -43, -40, -37, -34, -31, -28, -25, -21, -18, -15, + -12, -9, -6, -3 }; int32_t fastsin(int32_t arg) diff --git a/animation_test/src/fasttrigon.h b/animation_test/src/fasttrigon.h index 694418a..2be76cc 100644 --- a/animation_test/src/fasttrigon.h +++ b/animation_test/src/fasttrigon.h @@ -14,8 +14,13 @@ #include +#define FASTTRIGON_8BIT(x) (x >> (fasttrigon::PRECISION_BITS - 8)) + namespace fasttrigon { - static const uint32_t LUT_SIZE = 1024; + static const uint32_t LUT_SIZE = 2048; + static const uint32_t PRECISION_BITS = 11; + static const int32_t SCALE = 1023; + static const uint32_t UNIT_SHIFT = PRECISION_BITS-1; /*! * \returns 127 * sin(2π * arg / LUT_SIZE) diff --git a/animation_test/src/main.cpp b/animation_test/src/main.cpp index 4ac915d..89a5053 100644 --- a/animation_test/src/main.cpp +++ b/animation_test/src/main.cpp @@ -18,7 +18,7 @@ int main(void) fader.update(); } - if(frame == 60 * 900) { + if(frame == 60 * 90000) { animation->stop(); } diff --git a/scripts/fasttrigon_gen_lut.py b/scripts/fasttrigon_gen_lut.py new file mode 100755 index 0000000..2e0e60d --- /dev/null +++ b/scripts/fasttrigon_gen_lut.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import numpy as np + +LUT_SIZE = 2048 +PRECISION_BITS = 11 + +SCALE = (1 << (PRECISION_BITS-1)) - 1 + +x = np.array(range(LUT_SIZE)) * 2*np.pi / LUT_SIZE +y = SCALE * np.sin(x) + +print(f"static const uint32_t LUT_SIZE = {LUT_SIZE};") +print(f"static const uint32_t PRECISION_BITS = {PRECISION_BITS};") +print(f"static const uint32_t SCALE = {SCALE};") + +print(list(y.astype('int'))) From 25abc446b05b4a3c3bd13d689aae16925e47cc7c Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 23 Aug 2021 23:57:19 +0200 Subject: [PATCH 6/6] Increased sin LUT precision: 16-bit x 2048 entries This massively improves glitches in functions like sin(t + sin(t)), as used in the RgbwPsychedelicAnimation. That animation is now almost flicker-free. --- include/Animation/RgbwPsychedelicAnimation.h | 2 +- include/Animation/RgbwSinusAnimation.h | 2 +- include/fasttrigon.h | 7 +- src/Animation/ConnectingAnimation.cpp | 3 +- src/Animation/FireworkAnimation.cpp | 4 +- src/Animation/RgbwPsychedelicAnimation.cpp | 25 +- src/Animation/RgbwSinusAnimation.cpp | 8 +- src/Animation/SnowfallAnimation.cpp | 2 +- src/fasttrigon.cpp | 250 +++++++++++++------ 9 files changed, 207 insertions(+), 96 deletions(-) diff --git a/include/Animation/RgbwPsychedelicAnimation.h b/include/Animation/RgbwPsychedelicAnimation.h index 251f7f6..764be1d 100644 --- a/include/Animation/RgbwPsychedelicAnimation.h +++ b/include/Animation/RgbwPsychedelicAnimation.h @@ -16,7 +16,7 @@ class RgbwPsychedelicAnimation : public Animation int32_t tilt_maxperiods = 3, uint32_t move_speed_divider = 1024, uint32_t tilt_speed_divider = 8192, - uint16_t brightness_scale = 64); // max: 255 + uint16_t brightness_scale = fasttrigon::SCALE/4); // max: fasttrigon::SCALE void loop(uint64_t frame) override; diff --git a/include/Animation/RgbwSinusAnimation.h b/include/Animation/RgbwSinusAnimation.h index 487647a..814d16c 100644 --- a/include/Animation/RgbwSinusAnimation.h +++ b/include/Animation/RgbwSinusAnimation.h @@ -13,7 +13,7 @@ class RgbwSinusAnimation : public Animation RgbwSinusAnimation(Fader *fader, const rgbw_val_array &speeds = {2273, 2281, 2287, 2293}, uint32_t speed_divider = 1024, - uint16_t brightness_scale = 64); // max: 255 + uint16_t brightness_scale = fasttrigon::SCALE/4); // max: 255 void loop(uint64_t frame) override; diff --git a/include/fasttrigon.h b/include/fasttrigon.h index 694418a..2a3999e 100644 --- a/include/fasttrigon.h +++ b/include/fasttrigon.h @@ -14,8 +14,13 @@ #include +#define FASTTRIGON_8BIT(x) ((x) >> (fasttrigon::PRECISION_BITS - 8)) + namespace fasttrigon { - static const uint32_t LUT_SIZE = 1024; + static const uint32_t LUT_SIZE = 2048; + static const uint32_t PRECISION_BITS = 11; + static const int32_t SCALE = 1023; + static const uint32_t UNIT_SHIFT = PRECISION_BITS-1; /*! * \returns 127 * sin(2π * arg / LUT_SIZE) diff --git a/src/Animation/ConnectingAnimation.cpp b/src/Animation/ConnectingAnimation.cpp index 0c8e821..ca17f8c 100644 --- a/src/Animation/ConnectingAnimation.cpp +++ b/src/Animation/ConnectingAnimation.cpp @@ -8,7 +8,6 @@ ConnectingAnimation::ConnectingAnimation(Fader *fader) void ConnectingAnimation::loop(uint64_t frame) { - std::size_t nled = m_fader->modules_per_strip(); std::size_t nstrip = m_fader->strips(); uint8_t intensity = 0; @@ -17,7 +16,7 @@ void ConnectingAnimation::loop(uint64_t frame) } for(std::size_t strip = 0; strip < nstrip; strip++) { - intensity = (fasttrigon::fastsin(1 * frame * fasttrigon::LUT_SIZE / 60 + fasttrigon::LUT_SIZE*3/4) + 127) / 4; + intensity = (FASTTRIGON_8BIT(fasttrigon::fastsin(1 * frame * fasttrigon::LUT_SIZE / 60 + fasttrigon::LUT_SIZE*3/4)) + 127) / 4; Fader::Color c; c.b = intensity; diff --git a/src/Animation/FireworkAnimation.cpp b/src/Animation/FireworkAnimation.cpp index a4b457b..07fd19d 100644 --- a/src/Animation/FireworkAnimation.cpp +++ b/src/Animation/FireworkAnimation.cpp @@ -28,8 +28,8 @@ void FireworkRocket::move(uint64_t frame) case PHASE_SPREADING: for(size_t i = 0; i < m_flareXPos.size(); i++) { - int speedX = fasttrigon::fastcos(fasttrigon::LUT_SIZE / 12 + i * fasttrigon::LUT_SIZE / m_flareXPos.size()) / 2; - int speedY = fasttrigon::fastsin(fasttrigon::LUT_SIZE / 12 + i * fasttrigon::LUT_SIZE / m_flareXPos.size()) / 2; + int speedX = FASTTRIGON_8BIT(fasttrigon::fastcos(fasttrigon::LUT_SIZE / 12 + i * fasttrigon::LUT_SIZE / m_flareXPos.size())) / 2; + int speedY = FASTTRIGON_8BIT(fasttrigon::fastsin(fasttrigon::LUT_SIZE / 12 + i * fasttrigon::LUT_SIZE / m_flareXPos.size())) / 2; m_flareXPos[i] += speedX; m_flareYPos[i] += speedY; diff --git a/src/Animation/RgbwPsychedelicAnimation.cpp b/src/Animation/RgbwPsychedelicAnimation.cpp index e4046dd..f59b8f0 100644 --- a/src/Animation/RgbwPsychedelicAnimation.cpp +++ b/src/Animation/RgbwPsychedelicAnimation.cpp @@ -1,5 +1,7 @@ #include +//#include + #include "Animation/RgbwPsychedelicAnimation.h" RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, @@ -16,8 +18,8 @@ RgbwPsychedelicAnimation::RgbwPsychedelicAnimation(Fader *fader, m_tiltSpeedDivider(tilt_speed_divider), m_brightnessScale(brightness_scale), m_tilt_maxperiods(tilt_maxperiods), - m_move_phi{0,0,0,0}, - m_tilt_phi{100,200,300,400}, + m_move_phi{800,600,400,200}, + m_tilt_phi{200,400,600,800}, m_moveOverflowInterval(fasttrigon::LUT_SIZE * move_speed_divider), m_tiltOverflowInterval(fasttrigon::LUT_SIZE * tilt_speed_divider) { @@ -57,17 +59,20 @@ void RgbwPsychedelicAnimation::loop(uint64_t frame) int32_t y = fasttrigon::LUT_SIZE * strip / nstrip; rgbw_val_array rgbw { - (127 + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> 7) + y)), - (127 + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> 7) + y)) + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[0] / m_moveSpeedDivider + ((x * tilt_nperiods[0]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[1] / m_moveSpeedDivider + ((x * tilt_nperiods[1]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[2] / m_moveSpeedDivider + ((x * tilt_nperiods[2]) >> fasttrigon::UNIT_SHIFT) + y)), + (fasttrigon::SCALE + fasttrigon::fastsin(m_move_phi[3] / m_moveSpeedDivider + ((x * tilt_nperiods[3]) >> fasttrigon::UNIT_SHIFT) + y)) }; Fader::Color color{ - static_cast(((rgbw[0] * rgbw[0] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[1] * rgbw[1] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[2] * rgbw[2] >> 7) * m_brightnessScale) >> 8), - static_cast(((rgbw[3] * rgbw[3] >> 7) * m_brightnessScale) >> 10)}; // white is too bright otherwise + static_cast((FASTTRIGON_8BIT(rgbw[0] * rgbw[0] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[1] * rgbw[1] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[2] * rgbw[2] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast((FASTTRIGON_8BIT(rgbw[3] * rgbw[3] >> fasttrigon::UNIT_SHIFT) * m_brightnessScale) >> (fasttrigon::PRECISION_BITS + 2))}; // white is too bright otherwise + + //std::cerr << rgbw[0] << " " << rgbw[1] << " " << rgbw[2] << " " << rgbw[3] << std::endl; + //std::cerr << color.r << " " << color.g << " " << color.b << " " << color.w << std::endl; m_fader->set_color(strip, led, color); } diff --git a/src/Animation/RgbwSinusAnimation.cpp b/src/Animation/RgbwSinusAnimation.cpp index 897fe3f..ff50318 100644 --- a/src/Animation/RgbwSinusAnimation.cpp +++ b/src/Animation/RgbwSinusAnimation.cpp @@ -35,10 +35,10 @@ void RgbwSinusAnimation::loop(uint64_t frame) uint32_t pixelphase = led * fasttrigon::LUT_SIZE / nled + strip * fasttrigon::LUT_SIZE / nstrip; Fader::Color color{ - static_cast(((127 + fasttrigon::fastsin(m_phi[0] / m_speedDivider + pixelphase)) * m_brightnessScale) >> 8), - static_cast(((127 + fasttrigon::fastsin(m_phi[1] / m_speedDivider + pixelphase)) * m_brightnessScale) >> 8), - static_cast(((127 + fasttrigon::fastsin(m_phi[2] / m_speedDivider + pixelphase)) * m_brightnessScale) >> 8), - static_cast(((127 + fasttrigon::fastsin(m_phi[3] / m_speedDivider + pixelphase)) * m_brightnessScale) >> 9)}; // white is too bright otherwise + static_cast(((fasttrigon::SCALE + fasttrigon::fastsin(m_phi[0] / m_speedDivider + pixelphase)) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast(((fasttrigon::SCALE + fasttrigon::fastsin(m_phi[1] / m_speedDivider + pixelphase)) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast(((fasttrigon::SCALE + fasttrigon::fastsin(m_phi[2] / m_speedDivider + pixelphase)) * m_brightnessScale) >> fasttrigon::PRECISION_BITS), + static_cast(((fasttrigon::SCALE + fasttrigon::fastsin(m_phi[3] / m_speedDivider + pixelphase)) * m_brightnessScale) >> (fasttrigon::PRECISION_BITS + 2))}; // white is too bright otherwise m_fader->set_color(strip, led, color); } diff --git a/src/Animation/SnowfallAnimation.cpp b/src/Animation/SnowfallAnimation.cpp index f23fbfa..2d857f5 100644 --- a/src/Animation/SnowfallAnimation.cpp +++ b/src/Animation/SnowfallAnimation.cpp @@ -23,7 +23,7 @@ void SnowfallAnimation::SnowFlake::move(void) m_phase -= fasttrigon::LUT_SIZE; } - m_posStrip = m_basePosStrip + 2*fasttrigon::fastsin(m_phase); + m_posStrip = m_basePosStrip + FASTTRIGON_8BIT(2*fasttrigon::fastsin(m_phase)); m_posLED -= m_vertSpeed; } diff --git a/src/fasttrigon.cpp b/src/fasttrigon.cpp index b0da8e3..248d0c3 100644 --- a/src/fasttrigon.cpp +++ b/src/fasttrigon.cpp @@ -3,80 +3,182 @@ #include "fasttrigon.h" namespace fasttrigon { - static const std::array LUT { - 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, - 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, - 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 38, 38, 39, 40, 41, 41, - 42, 43, 44, 44, 45, 46, 46, 47, 48, 49, 49, 50, 51, 51, 52, 53, 54, - 54, 55, 56, 56, 57, 58, 58, 59, 60, 61, 61, 62, 63, 63, 64, 65, 65, - 66, 67, 67, 68, 69, 69, 70, 71, 71, 72, 72, 73, 74, 74, 75, 76, 76, - 77, 78, 78, 79, 79, 80, 81, 81, 82, 82, 83, 84, 84, 85, 85, 86, 86, - 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 93, 93, 94, 94, 95, 95, 96, - 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102, 102, 102, 103, - 103, 104, 104, 105, 105, 106, 106, 106, 107, 107, 108, 108, 109, 109, - 109, 110, 110, 111, 111, 111, 112, 112, 112, 113, 113, 113, 114, 114, - 114, 115, 115, 115, 116, 116, 116, 117, 117, 117, 118, 118, 118, 118, - 119, 119, 119, 120, 120, 120, 120, 121, 121, 121, 121, 122, 122, 122, - 122, 122, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 125, - 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126, - 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, - 127, 127, 127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, - 126, 125, 125, 125, 125, 125, 125, 125, 124, 124, 124, 124, 124, 124, - 123, 123, 123, 123, 123, 122, 122, 122, 122, 122, 121, 121, 121, 121, - 120, 120, 120, 120, 119, 119, 119, 118, 118, 118, 118, 117, 117, 117, - 116, 116, 116, 115, 115, 115, 114, 114, 114, 113, 113, 113, 112, 112, - 112, 111, 111, 111, 110, 110, 109, 109, 109, 108, 108, 107, 107, 106, - 106, 106, 105, 105, 104, 104, 103, 103, 102, 102, 102, 101, 101, 100, - 100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 91, - 91, 90, 90, 89, 89, 88, 88, 87, 86, 86, 85, 85, 84, 84, 83, 82, 82, - 81, 81, 80, 79, 79, 78, 78, 77, 76, 76, 75, 74, 74, 73, 72, 72, 71, - 71, 70, 69, 69, 68, 67, 67, 66, 65, 65, 64, 63, 63, 62, 61, 61, 60, - 59, 58, 58, 57, 56, 56, 55, 54, 54, 53, 52, 51, 51, 50, 49, 49, 48, - 47, 46, 46, 45, 44, 44, 43, 42, 41, 41, 40, 39, 38, 38, 37, 36, 35, - 35, 34, 33, 32, 32, 31, 30, 29, 29, 28, 27, 26, 26, 25, 24, 23, 22, - 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 14, 13, 12, 12, 11, 10, 9, 9, - 8, 7, 6, 5, 5, 4, 3, 2, 2, 1, 0, -1, -2, -2, -3, -4, -5, -5, -6, -7, - -8, -9, -9, -10, -11, -12, -12, -13, -14, -15, -16, -16, -17, -18, - -19, -19, -20, -21, -22, -22, -23, -24, -25, -26, -26, -27, -28, -29, - -29, -30, -31, -32, -32, -33, -34, -35, -35, -36, -37, -38, -38, -39, - -40, -41, -41, -42, -43, -44, -44, -45, -46, -46, -47, -48, -49, -49, - -50, -51, -51, -52, -53, -54, -54, -55, -56, -56, -57, -58, -58, -59, - -60, -61, -61, -62, -63, -63, -64, -65, -65, -66, -67, -67, -68, -69, - -69, -70, -71, -71, -72, -72, -73, -74, -74, -75, -76, -76, -77, -78, - -78, -79, -79, -80, -81, -81, -82, -82, -83, -84, -84, -85, -85, -86, - -86, -87, -88, -88, -89, -89, -90, -90, -91, -91, -92, -93, -93, -94, - -94, -95, -95, -96, -96, -97, -97, -98, -98, -99, -99, -100, -100, - -101, -101, -102, -102, -102, -103, -103, -104, -104, -105, -105, - -106, -106, -106, -107, -107, -108, -108, -109, -109, -109, -110, - -110, -111, -111, -111, -112, -112, -112, -113, -113, -113, -114, - -114, -114, -115, -115, -115, -116, -116, -116, -117, -117, -117, - -118, -118, -118, -118, -119, -119, -119, -120, -120, -120, -120, - -121, -121, -121, -121, -122, -122, -122, -122, -122, -123, -123, - -123, -123, -123, -124, -124, -124, -124, -124, -124, -125, -125, - -125, -125, -125, -125, -125, -126, -126, -126, -126, -126, -126, - -126, -126, -126, -126, -126, -127, -127, -127, -127, -127, -127, - -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, - -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, - -127, -126, -126, -126, -126, -126, -126, -126, -126, -126, -126, - -126, -125, -125, -125, -125, -125, -125, -125, -124, -124, -124, - -124, -124, -124, -123, -123, -123, -123, -123, -122, -122, -122, - -122, -122, -121, -121, -121, -121, -120, -120, -120, -120, -119, - -119, -119, -118, -118, -118, -118, -117, -117, -117, -116, -116, - -116, -115, -115, -115, -114, -114, -114, -113, -113, -113, -112, - -112, -112, -111, -111, -111, -110, -110, -109, -109, -109, -108, - -108, -107, -107, -106, -106, -106, -105, -105, -104, -104, -103, - -103, -102, -102, -102, -101, -101, -100, -100, -99, -99, -98, -98, - -97, -97, -96, -96, -95, -95, -94, -94, -93, -93, -92, -91, -91, -90, - -90, -89, -89, -88, -88, -87, -86, -86, -85, -85, -84, -84, -83, -82, - -82, -81, -81, -80, -79, -79, -78, -78, -77, -76, -76, -75, -74, -74, - -73, -72, -72, -71, -71, -70, -69, -69, -68, -67, -67, -66, -65, -65, - -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, - -54, -54, -53, -52, -51, -51, -50, -49, -49, -48, -47, -46, -46, -45, - -44, -44, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -35, -35, - -34, -33, -32, -32, -31, -30, -29, -29, -28, -27, -26, -26, -25, -24, - -23, -22, -22, -21, -20, -19, -19, -18, -17, -16, -16, -15, -14, -13, - -12, -12, -11, -10, -9, -9, -8, -7, -6, -5, -5, -4, -3, -2, -2, -1 + static const std::array LUT { + 0, 3, 6, 9, 12, 15, 18, 21, 25, 28, 31, 34, 37, 40, 43, 47, 50, 53, 56, + 59, 62, 65, 68, 72, 75, 78, 81, 84, 87, 90, 94, 97, 100, 103, 106, + 109, 112, 115, 118, 122, 125, 128, 131, 134, 137, 140, 143, 147, + 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 181, 184, 187, + 190, 193, 196, 199, 202, 205, 208, 211, 214, 218, 221, 224, 227, + 230, 233, 236, 239, 242, 245, 248, 251, 254, 257, 260, 263, 266, + 269, 272, 275, 278, 281, 284, 287, 290, 293, 296, 299, 302, 305, + 308, 311, 314, 317, 320, 323, 326, 329, 332, 335, 338, 341, 344, + 347, 350, 353, 356, 359, 362, 365, 368, 371, 374, 376, 379, 382, + 385, 388, 391, 394, 397, 400, 403, 405, 408, 411, 414, 417, 420, + 423, 426, 428, 431, 434, 437, 440, 443, 445, 448, 451, 454, 457, + 459, 462, 465, 468, 471, 473, 476, 479, 482, 485, 487, 490, 493, + 496, 498, 501, 504, 506, 509, 512, 515, 517, 520, 523, 525, 528, + 531, 533, 536, 539, 541, 544, 547, 549, 552, 555, 557, 560, 563, + 565, 568, 570, 573, 576, 578, 581, 583, 586, 589, 591, 594, 596, + 599, 601, 604, 606, 609, 611, 614, 616, 619, 621, 624, 626, 629, + 631, 634, 636, 639, 641, 644, 646, 648, 651, 653, 656, 658, 661, + 663, 665, 668, 670, 672, 675, 677, 679, 682, 684, 687, 689, 691, + 693, 696, 698, 700, 703, 705, 707, 709, 712, 714, 716, 718, 721, + 723, 725, 727, 729, 732, 734, 736, 738, 740, 743, 745, 747, 749, + 751, 753, 755, 757, 760, 762, 764, 766, 768, 770, 772, 774, 776, + 778, 780, 782, 784, 786, 788, 790, 792, 794, 796, 798, 800, 802, + 804, 806, 808, 810, 812, 814, 816, 817, 819, 821, 823, 825, 827, + 829, 830, 832, 834, 836, 838, 839, 841, 843, 845, 847, 848, 850, + 852, 854, 855, 857, 859, 860, 862, 864, 865, 867, 869, 870, 872, + 874, 875, 877, 879, 880, 882, 883, 885, 886, 888, 890, 891, 893, + 894, 896, 897, 899, 900, 902, 903, 905, 906, 908, 909, 910, 912, + 913, 915, 916, 917, 919, 920, 922, 923, 924, 926, 927, 928, 930, + 931, 932, 933, 935, 936, 937, 939, 940, 941, 942, 943, 945, 946, + 947, 948, 949, 951, 952, 953, 954, 955, 956, 957, 958, 959, 961, + 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, + 975, 976, 977, 978, 978, 979, 980, 981, 982, 983, 984, 985, 985, + 986, 987, 988, 989, 990, 990, 991, 992, 993, 993, 994, 995, 996, + 996, 997, 998, 998, 999, 1000, 1000, 1001, 1002, 1002, 1003, 1003, + 1004, 1005, 1005, 1006, 1006, 1007, 1007, 1008, 1008, 1009, 1010, + 1010, 1010, 1011, 1011, 1012, 1012, 1013, 1013, 1014, 1014, 1014, + 1015, 1015, 1016, 1016, 1016, 1017, 1017, 1017, 1018, 1018, 1018, + 1018, 1019, 1019, 1019, 1019, 1020, 1020, 1020, 1020, 1021, 1021, + 1021, 1021, 1021, 1021, 1022, 1022, 1022, 1022, 1022, 1022, 1022, + 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1023, 1022, 1022, 1022, + 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, 1022, + 1021, 1021, 1021, 1021, 1021, 1021, 1020, 1020, 1020, 1020, 1019, + 1019, 1019, 1019, 1018, 1018, 1018, 1018, 1017, 1017, 1017, 1016, + 1016, 1016, 1015, 1015, 1014, 1014, 1014, 1013, 1013, 1012, 1012, + 1011, 1011, 1010, 1010, 1010, 1009, 1008, 1008, 1007, 1007, 1006, + 1006, 1005, 1005, 1004, 1003, 1003, 1002, 1002, 1001, 1000, 1000, + 999, 998, 998, 997, 996, 996, 995, 994, 993, 993, 992, 991, 990, + 990, 989, 988, 987, 986, 985, 985, 984, 983, 982, 981, 980, 979, + 978, 978, 977, 976, 975, 974, 973, 972, 971, 970, 969, 968, 967, + 966, 965, 964, 963, 962, 961, 959, 958, 957, 956, 955, 954, 953, + 952, 951, 949, 948, 947, 946, 945, 943, 942, 941, 940, 939, 937, + 936, 935, 933, 932, 931, 930, 928, 927, 926, 924, 923, 922, 920, + 919, 917, 916, 915, 913, 912, 910, 909, 908, 906, 905, 903, 902, + 900, 899, 897, 896, 894, 893, 891, 890, 888, 886, 885, 883, 882, + 880, 879, 877, 875, 874, 872, 870, 869, 867, 865, 864, 862, 860, + 859, 857, 855, 854, 852, 850, 848, 847, 845, 843, 841, 839, 838, + 836, 834, 832, 830, 829, 827, 825, 823, 821, 819, 817, 816, 814, + 812, 810, 808, 806, 804, 802, 800, 798, 796, 794, 792, 790, 788, + 786, 784, 782, 780, 778, 776, 774, 772, 770, 768, 766, 764, 762, + 760, 757, 755, 753, 751, 749, 747, 745, 743, 740, 738, 736, 734, + 732, 729, 727, 725, 723, 721, 718, 716, 714, 712, 709, 707, 705, + 703, 700, 698, 696, 693, 691, 689, 687, 684, 682, 679, 677, 675, + 672, 670, 668, 665, 663, 661, 658, 656, 653, 651, 648, 646, 644, + 641, 639, 636, 634, 631, 629, 626, 624, 621, 619, 616, 614, 611, + 609, 606, 604, 601, 599, 596, 594, 591, 589, 586, 583, 581, 578, + 576, 573, 570, 568, 565, 563, 560, 557, 555, 552, 549, 547, 544, + 541, 539, 536, 533, 531, 528, 525, 523, 520, 517, 515, 512, 509, + 506, 504, 501, 498, 496, 493, 490, 487, 485, 482, 479, 476, 473, + 471, 468, 465, 462, 459, 457, 454, 451, 448, 445, 443, 440, 437, + 434, 431, 428, 426, 423, 420, 417, 414, 411, 408, 405, 403, 400, + 397, 394, 391, 388, 385, 382, 379, 376, 374, 371, 368, 365, 362, + 359, 356, 353, 350, 347, 344, 341, 338, 335, 332, 329, 326, 323, + 320, 317, 314, 311, 308, 305, 302, 299, 296, 293, 290, 287, 284, + 281, 278, 275, 272, 269, 266, 263, 260, 257, 254, 251, 248, 245, + 242, 239, 236, 233, 230, 227, 224, 221, 218, 214, 211, 208, 205, + 202, 199, 196, 193, 190, 187, 184, 181, 177, 174, 171, 168, 165, + 162, 159, 156, 153, 150, 147, 143, 140, 137, 134, 131, 128, 125, + 122, 118, 115, 112, 109, 106, 103, 100, 97, 94, 90, 87, 84, 81, 78, + 75, 72, 68, 65, 62, 59, 56, 53, 50, 47, 43, 40, 37, 34, 31, 28, 25, + 21, 18, 15, 12, 9, 6, 3, 0, -3, -6, -9, -12, -15, -18, -21, -25, + -28, -31, -34, -37, -40, -43, -47, -50, -53, -56, -59, -62, -65, + -68, -72, -75, -78, -81, -84, -87, -90, -94, -97, -100, -103, -106, + -109, -112, -115, -118, -122, -125, -128, -131, -134, -137, -140, + -143, -147, -150, -153, -156, -159, -162, -165, -168, -171, -174, + -177, -181, -184, -187, -190, -193, -196, -199, -202, -205, -208, + -211, -214, -218, -221, -224, -227, -230, -233, -236, -239, -242, + -245, -248, -251, -254, -257, -260, -263, -266, -269, -272, -275, + -278, -281, -284, -287, -290, -293, -296, -299, -302, -305, -308, + -311, -314, -317, -320, -323, -326, -329, -332, -335, -338, -341, + -344, -347, -350, -353, -356, -359, -362, -365, -368, -371, -374, + -376, -379, -382, -385, -388, -391, -394, -397, -400, -403, -405, + -408, -411, -414, -417, -420, -423, -426, -428, -431, -434, -437, + -440, -443, -445, -448, -451, -454, -457, -459, -462, -465, -468, + -471, -473, -476, -479, -482, -485, -487, -490, -493, -496, -498, + -501, -504, -506, -509, -512, -515, -517, -520, -523, -525, -528, + -531, -533, -536, -539, -541, -544, -547, -549, -552, -555, -557, + -560, -563, -565, -568, -570, -573, -576, -578, -581, -583, -586, + -589, -591, -594, -596, -599, -601, -604, -606, -609, -611, -614, + -616, -619, -621, -624, -626, -629, -631, -634, -636, -639, -641, + -644, -646, -648, -651, -653, -656, -658, -661, -663, -665, -668, + -670, -672, -675, -677, -679, -682, -684, -687, -689, -691, -693, + -696, -698, -700, -703, -705, -707, -709, -712, -714, -716, -718, + -721, -723, -725, -727, -729, -732, -734, -736, -738, -740, -743, + -745, -747, -749, -751, -753, -755, -757, -760, -762, -764, -766, + -768, -770, -772, -774, -776, -778, -780, -782, -784, -786, -788, + -790, -792, -794, -796, -798, -800, -802, -804, -806, -808, -810, + -812, -814, -816, -817, -819, -821, -823, -825, -827, -829, -830, + -832, -834, -836, -838, -839, -841, -843, -845, -847, -848, -850, + -852, -854, -855, -857, -859, -860, -862, -864, -865, -867, -869, + -870, -872, -874, -875, -877, -879, -880, -882, -883, -885, -886, + -888, -890, -891, -893, -894, -896, -897, -899, -900, -902, -903, + -905, -906, -908, -909, -910, -912, -913, -915, -916, -917, -919, + -920, -922, -923, -924, -926, -927, -928, -930, -931, -932, -933, + -935, -936, -937, -939, -940, -941, -942, -943, -945, -946, -947, + -948, -949, -951, -952, -953, -954, -955, -956, -957, -958, -959, + -961, -962, -963, -964, -965, -966, -967, -968, -969, -970, -971, + -972, -973, -974, -975, -976, -977, -978, -978, -979, -980, -981, + -982, -983, -984, -985, -985, -986, -987, -988, -989, -990, -990, + -991, -992, -993, -993, -994, -995, -996, -996, -997, -998, -998, + -999, -1000, -1000, -1001, -1002, -1002, -1003, -1003, -1004, + -1005, -1005, -1006, -1006, -1007, -1007, -1008, -1008, -1009, + -1010, -1010, -1010, -1011, -1011, -1012, -1012, -1013, -1013, + -1014, -1014, -1014, -1015, -1015, -1016, -1016, -1016, -1017, + -1017, -1017, -1018, -1018, -1018, -1018, -1019, -1019, -1019, + -1019, -1020, -1020, -1020, -1020, -1021, -1021, -1021, -1021, + -1021, -1021, -1022, -1022, -1022, -1022, -1022, -1022, -1022, + -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1023, -1022, + -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1022, -1022, + -1022, -1022, -1022, -1022, -1021, -1021, -1021, -1021, -1021, + -1021, -1020, -1020, -1020, -1020, -1019, -1019, -1019, -1019, + -1018, -1018, -1018, -1018, -1017, -1017, -1017, -1016, -1016, + -1016, -1015, -1015, -1014, -1014, -1014, -1013, -1013, -1012, + -1012, -1011, -1011, -1010, -1010, -1010, -1009, -1008, -1008, + -1007, -1007, -1006, -1006, -1005, -1005, -1004, -1003, -1003, + -1002, -1002, -1001, -1000, -1000, -999, -998, -998, -997, -996, + -996, -995, -994, -993, -993, -992, -991, -990, -990, -989, -988, + -987, -986, -985, -985, -984, -983, -982, -981, -980, -979, -978, + -978, -977, -976, -975, -974, -973, -972, -971, -970, -969, -968, + -967, -966, -965, -964, -963, -962, -961, -959, -958, -957, -956, + -955, -954, -953, -952, -951, -949, -948, -947, -946, -945, -943, + -942, -941, -940, -939, -937, -936, -935, -933, -932, -931, -930, + -928, -927, -926, -924, -923, -922, -920, -919, -917, -916, -915, + -913, -912, -910, -909, -908, -906, -905, -903, -902, -900, -899, + -897, -896, -894, -893, -891, -890, -888, -886, -885, -883, -882, + -880, -879, -877, -875, -874, -872, -870, -869, -867, -865, -864, + -862, -860, -859, -857, -855, -854, -852, -850, -848, -847, -845, + -843, -841, -839, -838, -836, -834, -832, -830, -829, -827, -825, + -823, -821, -819, -817, -816, -814, -812, -810, -808, -806, -804, + -802, -800, -798, -796, -794, -792, -790, -788, -786, -784, -782, + -780, -778, -776, -774, -772, -770, -768, -766, -764, -762, -760, + -757, -755, -753, -751, -749, -747, -745, -743, -740, -738, -736, + -734, -732, -729, -727, -725, -723, -721, -718, -716, -714, -712, + -709, -707, -705, -703, -700, -698, -696, -693, -691, -689, -687, + -684, -682, -679, -677, -675, -672, -670, -668, -665, -663, -661, + -658, -656, -653, -651, -648, -646, -644, -641, -639, -636, -634, + -631, -629, -626, -624, -621, -619, -616, -614, -611, -609, -606, + -604, -601, -599, -596, -594, -591, -589, -586, -583, -581, -578, + -576, -573, -570, -568, -565, -563, -560, -557, -555, -552, -549, + -547, -544, -541, -539, -536, -533, -531, -528, -525, -523, -520, + -517, -515, -512, -509, -506, -504, -501, -498, -496, -493, -490, + -487, -485, -482, -479, -476, -473, -471, -468, -465, -462, -459, + -457, -454, -451, -448, -445, -443, -440, -437, -434, -431, -428, + -426, -423, -420, -417, -414, -411, -408, -405, -403, -400, -397, + -394, -391, -388, -385, -382, -379, -376, -374, -371, -368, -365, + -362, -359, -356, -353, -350, -347, -344, -341, -338, -335, -332, + -329, -326, -323, -320, -317, -314, -311, -308, -305, -302, -299, + -296, -293, -290, -287, -284, -281, -278, -275, -272, -269, -266, + -263, -260, -257, -254, -251, -248, -245, -242, -239, -236, -233, + -230, -227, -224, -221, -218, -214, -211, -208, -205, -202, -199, + -196, -193, -190, -187, -184, -181, -177, -174, -171, -168, -165, + -162, -159, -156, -153, -150, -147, -143, -140, -137, -134, -131, + -128, -125, -122, -118, -115, -112, -109, -106, -103, -100, -97, + -94, -90, -87, -84, -81, -78, -75, -72, -68, -65, -62, -59, -56, + -53, -50, -47, -43, -40, -37, -34, -31, -28, -25, -21, -18, -15, + -12, -9, -6, -3 }; int32_t fastsin(int32_t arg)