File: hatching_brush.cpp

package info (click to toggle)
krita 1%3A3.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 430,944 kB
  • ctags: 68,203
  • sloc: cpp: 798,916; xml: 7,448; ansic: 3,844; perl: 434; sh: 140; makefile: 20
file content (299 lines) | stat: -rw-r--r-- 10,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 *  Copyright (c) 2008,2009,2010 Lukáš Tvrdý <lukast.dev@gmail.com>
 *  Copyright (c) 2010 José Luis Vergara <pentalis@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "hatching_brush.h"

#include <KoColor.h>
#include <KoColorTransformation.h>

#include <QVariant>
#include <QHash>

#include "kis_random_accessor_ng.h"
#include <cmath>
#include <time.h>


void inline myround(double *x)
{
    *x = ((*x - floor(*x)) >= 0.5) ? ceil(*x) : floor(*x);
}

HatchingBrush::HatchingBrush(KisHatchingPaintOpSettingsSP settings)
{
    m_settings = settings;

    // Initializing
    separation = m_settings->separation;
    origin_x = m_settings->origin_x;
    origin_y = m_settings->origin_y;
    cursorLineIntercept = 0;
    baseLineIntercept = 0;
    scanIntercept = 0;
    hotIntercept = 0;
    slope = 0;
    dx = 0;
    dy = 0;
}


HatchingBrush::~HatchingBrush()
{
}

void HatchingBrush::init()
{
}

void HatchingBrush::hatch(KisPaintDeviceSP dev, qreal x, qreal y, double width, double height, double givenangle, const KoColor &color, qreal additionalScale)
{
    m_painter.begin(dev);
    m_painter.setFillStyle(KisPainter::FillStyleForegroundColor);
    m_painter.setPaintColor(color);
    m_painter.setBackgroundColor(color);

    angle = givenangle;
    double tempthickness = m_settings->thickness * m_settings->thicknesssensorvalue;
    thickness = qMax(1, qRound(additionalScale * tempthickness));
    separation = additionalScale *
        (m_settings->enabledcurveseparation ?
         separationAsFunctionOfParameter(m_settings->separationsensorvalue, m_settings->separation, m_settings->separationintervals) :
         m_settings->separation);

    height_ = height;
    width_ = width;

    m_painter.setMaskImageSize(width_, height_);

    /*  dx and dy are the separation between lines in the x and y axis
    dx = separation / sin(angle*M_PI/180);     csc = 1/sin(angle)  */
    dy = fabs(separation / cos(angle * M_PI / 180)); // sec = 1/cos(angle)
    // I took the absolute value to avoid confusions with negative numbers

    if (!m_settings->subpixelprecision)
        modf(dy, &dy);

    // Exception for vertical lines, for which a tangent does not exist
    if ((angle == 90) || (angle == -90)) {
        verticalHotX = fmod((origin_x - x), separation);

        iterateVerticalLines(true, 1, false);    // Forward
        iterateVerticalLines(true, 0, true);     // In Between both
        iterateVerticalLines(false, 1, false);   // Backward
    }
    else {
        // Turn Angle + Point into Slope + Intercept
        slope = tan(angle * M_PI / 180);                // Angle into slope
        baseLineIntercept = origin_y - slope * origin_x; // Slope and Point of the Base Line into Intercept
        cursorLineIntercept = y - slope * x;
        hotIntercept = fmod((baseLineIntercept - cursorLineIntercept), dy);  // This hotIntercept belongs to a line that intersects with the hatching area

        iterateLines(true, 1, false);    // Forward
        iterateLines(true, 0, true);     // In Between both
        iterateLines(false, 1, false);   // Backward
        // I tried to make this cleaner but there's too many possibilities to be
        // worth the micromanagement to optimize
    }
}

void HatchingBrush::iterateLines(bool forward, int lineindex, bool oneline)
{
    //---Preparations before the loop---

    double xdraw[2] = {0, 0};
    double ydraw[2] = {0, 0};
    //points A and B of the segments to trace
    QPointF A, B;
    int append_index = 0;
    bool remaininginnerlines = true;

    while (remaininginnerlines) {

        //---------START INTERSECTION POINT VERIFICATION--------

        append_index = 0;
        remaininginnerlines = false; // We assume there's no more lines unless proven contrary
        if (forward)
            scanIntercept = hotIntercept + dy * lineindex; // scanIntercept will represent the Intercept of the current line
        else
            scanIntercept = hotIntercept - dy * lineindex;  // scanIntercept will represent the Intercept of the current line

        lineindex++; // We are descending vertically out of convenience, see blog entry at pentalis.org/kritablog

        /*
        Explanation: only 2 out of the 4 segments can include limit values
        to verify intersections, otherwise we could encounter a situation where
        our new lines intersect with all 4 segments and is still considered an
        inner line (for example, a line that goes from corner to corner), thus
        triggering an error. The idea is of the algorithm is that only 2 intersections
        at most are considered at a time. Graphically this is indistinguishable, it's
        just there to avoid making unnecesary control structures (like additional "ifs").
        */

        if ((scanIntercept >= 0) && (scanIntercept <= height_)) {
            xdraw[append_index] = 0;
            ydraw[append_index] = scanIntercept;       //interseccion at left
            remaininginnerlines = true;
            append_index++;
        }

        if ((slope * width_ + scanIntercept <= height_) && (slope * width_ + scanIntercept >= 0)) {
            xdraw[append_index] = width_;
            ydraw[append_index] = scanIntercept + slope * width_; //interseccion at right
            remaininginnerlines = true;
            append_index++;
        }

        if ((-scanIntercept / slope > 0) && (-scanIntercept / slope < width_)) {
            xdraw[append_index] = -scanIntercept / slope;
            ydraw[append_index] = 0;       //interseccion at top
            remaininginnerlines = true;
            append_index++;
        }

        if (((height_ - scanIntercept) / slope > 0) && ((height_ - scanIntercept) / slope < width_)) {
            xdraw[append_index] = (height_ - scanIntercept) / slope;
            ydraw[append_index] = height_;       //interseccion at bottom
            remaininginnerlines = true;
            append_index++;
        }
        //--------END INTERSECTION POINT VERIFICATION---------

        if (!remaininginnerlines)
            break;

        if (!m_settings->subpixelprecision) {
            myround(&xdraw[0]);
            myround(&xdraw[1]);
            myround(&ydraw[0]);
            myround(&ydraw[1]);
        }

        A.setX(xdraw[0]);
        A.setY(ydraw[0]);

        // If 2 lines intersect with the dab square
        if (append_index == 2) {
            B.setX(xdraw[1]);
            B.setY(ydraw[1]);

            if (m_settings->antialias)
                m_painter.drawThickLine(A, B, thickness, thickness);
            else
                m_painter.drawLine(A, B, thickness, false);    //testing no subpixel;

            if (oneline)
                break;
        }
        else {
            continue;
            /*Drawing points at the vertices causes incosistent results due to
            floating point calculations not being quite in sync with algebra,
            therefore if I have only 1 intersection (= corner = this case),
            don't draw*/
        }
    }
}

void HatchingBrush::iterateVerticalLines(bool forward, int lineindex, bool oneline)
{
    //---Preparations before the loop---

    double xdraw = 0;
    double ydraw[2] = {0, height_};
    //points A and B of the segments to trace
    QPointF A, B;
    bool remaininginnerlines = true;

    while (remaininginnerlines) {

        //---------START INTERSECTION POINT VERIFICATION--------
        remaininginnerlines = false;     // We assume there's no more lines unless proven contrary
        if (forward)
            verticalScanX = verticalHotX + separation * lineindex;
        else
            verticalScanX = verticalHotX - separation * lineindex;

        lineindex++;

        /*Read the explanation in HatchingBrush::iterateLines for more information*/

        if ((verticalScanX >= 0) && (verticalScanX <= width_)) {
            xdraw = verticalScanX;
            remaininginnerlines = true;
        }
        //--------END INTERSECTION POINT VERIFICATION---------

        if (!remaininginnerlines)
            break;

        if (!m_settings->subpixelprecision) {
            myround(&xdraw);
            myround(&ydraw[1]);
        }

        A.setX(xdraw);
        A.setY(ydraw[0]);
        B.setX(xdraw);
        B.setY(ydraw[1]);

        if (m_settings->antialias)
            m_painter.drawThickLine(A, B, thickness, thickness);
        else
            m_painter.drawLine(A, B, thickness, false);    //testing no subpixel;

        if (oneline)
            break;
        else
            continue;
    }
}

double HatchingBrush::separationAsFunctionOfParameter(double parameter, double separation, int numintervals)
{
    if ((numintervals < 2) || (numintervals > 7)) {
        dbgKrita << "Fix your function" << numintervals << "<> 2-7" ;
        return separation;
    }

    double sizeinterval = 1 / double(numintervals);
    double lowerlimit = 0;
    double upperlimit = 0;
    double factor = 0;

    int basefactor = numintervals / 2;
    // Make the base separation factor tend to greater instead of lesser numbers when numintervals is even
    if ((numintervals % 2) == 0)
        basefactor--;

    for (quint8 currentinterval = 0; currentinterval < numintervals; currentinterval++) {
        lowerlimit = upperlimit;
        upperlimit += sizeinterval;
        if (currentinterval == (numintervals - 1))
            upperlimit = 1;
        if ((parameter >= lowerlimit) && (parameter <= upperlimit)) {
            factor = pow(2.0, (basefactor - currentinterval));
            //dbgKrita << factor;
            return (separation * factor);
        }
    }

    dbgKrita << "Fix your function" << parameter << ">" << upperlimit ;
    return separation;
}