File indexing completed on 2025-02-02 04:54:38
0001 // SPDX-License-Identifier: GPL-2.0-or-later 0002 // SPDX-FileCopyrightText: 2018 Kevin Ottens <ervin@kde.org> 0003 0004 #include "pressureequation.h" 0005 0006 #include <QDebug> 0007 #include <cmath> 0008 0009 namespace 0010 { 0011 float sigmoid(float x) 0012 { 0013 const auto lambda = 5.0f; 0014 return 1.0f / (1.0f + std::exp(-lambda * x)); 0015 } 0016 0017 float computeWidth(float minWidth, float maxWidth, float pressure) 0018 { 0019 const auto alpha = sigmoid(2.0f * pressure - 1.0f); 0020 return (1.0f - alpha) * minWidth + alpha * maxWidth; 0021 } 0022 } 0023 0024 float PressureEquation::minWidth() const 0025 { 0026 return m_minWidth; 0027 } 0028 0029 float PressureEquation::maxWidth() const 0030 { 0031 return m_maxWidth; 0032 } 0033 0034 float PressureEquation::pressure() const 0035 { 0036 return m_pressure; 0037 } 0038 0039 float PressureEquation::width() const 0040 { 0041 return m_width; 0042 } 0043 0044 void PressureEquation::setMinWidth(float minWidth) 0045 { 0046 if (qFuzzyCompare(m_minWidth, minWidth)) 0047 return; 0048 0049 m_minWidth = minWidth; 0050 Q_EMIT minWidthChanged(minWidth); 0051 updateWidth(); 0052 } 0053 0054 void PressureEquation::setMaxWidth(float maxWidth) 0055 { 0056 if (qFuzzyCompare(m_maxWidth, maxWidth)) 0057 return; 0058 0059 m_maxWidth = maxWidth; 0060 Q_EMIT maxWidthChanged(maxWidth); 0061 updateWidth(); 0062 } 0063 0064 void PressureEquation::setPressure(float pressure) 0065 { 0066 if (qFuzzyCompare(m_pressure, pressure)) 0067 return; 0068 0069 m_pressure = pressure; 0070 Q_EMIT pressureChanged(pressure); 0071 updateWidth(); 0072 } 0073 0074 void PressureEquation::updateWidth() 0075 { 0076 m_width = computeWidth(m_minWidth, m_maxWidth, m_pressure); 0077 Q_EMIT widthChanged(m_width); 0078 }