File indexing completed on 2024-06-16 04:17:31

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Lukáš Tvrdý <lukast.dev@gmail.com>
0003  *  SPDX-FileCopyrightText: 2011 Silvio Heinrich <plassy@web.de>
0004  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 #include "KisScatterOption.h"
0009 
0010 #include <QVector2D>
0011 
0012 #include <kis_properties_configuration.h>
0013 #include <kis_paint_information.h>
0014 #include <KisScatterOptionData.h>
0015 
0016 #include <KisPaintOpOptionUtils.h>
0017 namespace kpou = KisPaintOpOptionUtils;
0018 
0019 
0020 KisScatterOption::KisScatterOption(const KisPropertiesConfiguration *setting)
0021     : KisScatterOption(kpou::loadOptionData<KisScatterOptionData>(setting))
0022 {
0023 }
0024 
0025 KisScatterOption::KisScatterOption(const KisScatterOptionData &data)
0026     : KisCurveOption(data)
0027     , m_axisX(data.axisX)
0028     , m_axisY(data.axisY)
0029 {
0030 }
0031 
0032 QPointF KisScatterOption::apply(const KisPaintInformation& info, qreal width, qreal height) const
0033 {
0034     if ((!m_axisX && !m_axisY) || (!isChecked())) {
0035         return info.pos();
0036     }
0037 
0038     // just use the most significant dimension for calculations
0039     qreal diameter = qMax(width, height);
0040     qreal sensorValue = computeSizeLikeValue(info);
0041 
0042     qreal jitter = (2.0 * info.randomSource()->generateNormalized() - 1.0) * diameter * sensorValue;
0043     QPointF result(0.0, 0.0);
0044 
0045     if (m_axisX && m_axisY) {
0046         qreal jitterY = (2.0 * info.randomSource()->generateNormalized() - 1.0) * diameter * sensorValue;
0047         result = QPointF(jitter, jitterY);
0048         return info.pos() + result;
0049     }
0050 
0051     qreal drawingAngle = info.drawingAngle();
0052     QVector2D movement(cos(drawingAngle), sin(drawingAngle));
0053     if (m_axisX) {
0054         movement *= jitter;
0055         result = movement.toPointF();
0056     }
0057     else if (m_axisY) {
0058         QVector2D movementNormal(-movement.y(), movement.x());
0059         movementNormal *= jitter;
0060         result = movementNormal.toPointF();
0061     }
0062 
0063     return info.pos() + result;
0064 }