Krita Source Code Documentation
Loading...
Searching...
No Matches
CurvilinearPerspectiveAssistant.cc
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2008 Cyrille Berger <cberger@cberger.net>
3 * SPDX-FileCopyrightText: 2010 Geoffry Song <goffrie@gmail.com>
4 * SPDX-FileCopyrightText: 2014 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
5 * SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
6 *
7 * SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
11
12#include "kis_debug.h"
13#include <klocalizedstring.h>
14
15#include <QPainter>
16#include <QPainterPath>
17#include <QLinearGradient>
18#include <QTransform>
19
20#include <kis_canvas2.h>
22#include <kis_algebra_2d.h>
24
25#include <math.h>
26#include <limits>
27
30 , KisPaintingAssistant("curvilinear-perspective", i18n("Curvilinear Perspective assistant"))
31{
32}
33
34CurvilinearPerspectiveAssistant::CurvilinearPerspectiveAssistant(const CurvilinearPerspectiveAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
35 : KisAbstractPerspectiveGrid(rhs.parent())
36 , KisPaintingAssistant(rhs, handleMap)
37{
38}
39
40KisPaintingAssistantSP CurvilinearPerspectiveAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
41{
42 return KisPaintingAssistantSP(new CurvilinearPerspectiveAssistant(*this, handleMap));
43}
44
45void CurvilinearPerspectiveAssistant::adjustLine(QPointF &point, QPointF &strokeBegin)
46{
47 point = QPointF();
48 strokeBegin = QPointF();
49}
50
51void CurvilinearPerspectiveAssistant::drawAssistant(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter* converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool cached, KisCanvas2* canvas, bool assistantVisible, bool previewVisible)
52{
53 Q_UNUSED(cached);
54 Q_UNUSED(updateRect);
55
56 gc.save();
57 gc.resetTransform();
58
59 const QColor canvasAssistantColor = displayRenderInterface->convertColorToDisplayColorSpace(KoColor(effectiveAssistantColor(), KoColorSpaceRegistry::instance()->rgb8()));
60
61 if (isSnappingActive()) {
62
63 QTransform initialTransform = converter->documentToWidgetTransform();
64 QPainterPath baseGuidePath;
65 QPainterPath mouseGuidePath;
66
67 gc.setTransform(initialTransform);
68
69 /*
70 * Curvilinear perspective is created by circular arcs that intersect 2 vanishing points.
71 * As such, the center of the circle and the radius of the circle need to be determined.
72 *
73 * Create guidelines by selecting incremental multipliers for the assistant size between [-1, 1),
74 * and calculating the center location and radius of the circle to include the point
75 * at the location specified by the multiplier (the "arbitrary point").
76 *
77 * Formulas:
78 * Radius^2 = HalfHandleDist^2 + CenterDist^2 (b.c. The circle must include both vanishing points.)
79 * Radius^2 = (CenterDist + Multiplier * HalfHandleDist)^2 (b.c. The circle must include the arbitrary point)
80 *
81 * Solve for CenterDist and Radius:
82 * CenterDist = HalfHandleDist * (1 - Multiplier * Multiplier) / ( 2 * Multiplier)
83 * Radius = HalfHandleDist * (1 + Multiplier * Multiplier) / ( 2 * Multiplier)
84 *
85 */
86
87 QPointF p1 = *handles()[0];
88 QPointF p2 = *handles()[1];
89
90 double deltaX = p2.x() - p1.x();
91 double deltaY = p2.y() - p1.y();
92
93 // Copied from Two-Point Perspective's fading effect for approaching vanishing points.
94 // Set up the fading effect for the grid lines
95 // Needed so the grid density doesn't look distracting
96 QColor color = canvasAssistantColor;
97 QGradient fade = QLinearGradient(
98 QPointF(p1.x() - deltaY, p1.y() + deltaX),
99 QPointF(p1.x() + deltaY, p1.y() - deltaX));
100
101 color.setAlphaF(0.0);
102 fade.setColorAt(0.42, canvasAssistantColor);
103 fade.setColorAt(0.5, color);
104 fade.setColorAt(0.58, canvasAssistantColor);
105 const QPen pen = gc.pen();
106 const QBrush new_brush = QBrush(fade);
107 int width = 0;
108 const QPen new_pen = QPen(new_brush, width, pen.style());
109 gc.setPen(new_pen);
110
111 double handleDistance = KisAlgebra2D::norm(QPointF(deltaX, deltaY));
112 double halfHandleDist = handleDistance / 2.0;
113
114 double avgX = deltaX / 2.0 + p1.x();
115 double avgY = deltaY / 2.0 + p1.y();
116
117 // Rotate 90 degrees by formula: (-y, x)
118 // Then normalize vector.
119 double dirX = -deltaY / handleDistance;
120 double dirY = deltaX / handleDistance;
121
122 int resolution = halfHandleDist / 3;
123
124 if(assistantVisible) {
125
126 for(int i = -resolution; i < resolution; i++) {
127 // If i = 0, the circle would be infinitely far away with an infinite radius (aka a line)
128 if(i == 0) {
129 baseGuidePath.moveTo(QPointF(p1.x() - deltaX*2, p1.y() - deltaY*2));
130 baseGuidePath.lineTo(QPointF(p2.x() + deltaX*2, p2.y() + deltaY*2));
131 continue;
132 }
133 // Map loop iterator to multiplier. This line gives the depth-like effect.
134 double mult = 1.0 / i;
135 // Use formula to calculate CenterDist
136 double centerDist = halfHandleDist * (1 - pow2(mult)) / (2*mult);
137
138 // Use the distance to the center (from the average point) to calculate the center location.
139 double circleCenterX = centerDist * dirX + avgX;
140 double circleCenterY = centerDist * dirY + avgY;
141 // Use formula to calculate Radius
142 double radius = halfHandleDist * (1 + pow2(mult)) / (2*mult);
143
144 baseGuidePath.addEllipse(QPointF(circleCenterX, circleCenterY), radius, radius);
145
146 }
147 gc.drawPath(baseGuidePath);//drawPath(gc, baseGuidePath);
148 }
149
150 if(previewVisible) {
151 // Draw guideline for the mouse, based on mouse position.
152 QPointF mousePos = effectiveBrushPosition(converter, canvas);
153 // Get location on the screen of handles.
154 QPointF screenP1 = initialTransform.map(*handles()[0]);
155 QPointF screenP2 = initialTransform.map(*handles()[1]);
156 // Don't draw if mouse is too close to vanishing points (will flicker if not)
157 // Use distance squared to avoid expensive sqrt.
158 if(
159 kisSquareDistance(mousePos, screenP2) > 9 &&
160 kisSquareDistance(mousePos, screenP1) > 9
161 ) {
162 QLineF circle = identifyCircle(initialTransform.inverted().map(mousePos));
163 double radius = circle.length();
164 mouseGuidePath.addEllipse(circle.p1(), radius, radius);
165 }
166
167 gc.drawPath(mouseGuidePath);//drawPath(gc, mouseGuidePath);
168 }
169
170 }
171 gc.restore();
172
173 //KisPaintingAssistant::drawAssistant(gc, updateRect, converter, cached, canvas, assistantVisible, previewVisible);
174
175}
176
177void CurvilinearPerspectiveAssistant::drawCache(QPainter& gc, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool assistantVisible)
178{
179 Q_UNUSED(gc);
180 Q_UNUSED(converter);
181 Q_UNUSED(assistantVisible);
182 Q_UNUSED(displayRenderInterface);
183}
184
185QLineF CurvilinearPerspectiveAssistant::identifyCircle(const QPointF thirdPoint) {
186 /*
187 * Calculate center location and radius for an arbitrary point (usually the mouse location).
188 * Given Formulas:
189 * Radius^2 = HalfHandleDist^2 + CenterDist^2
190 * avgX + CenterDist * dirX = CenterX
191 * avgY + CenterDist * dirY = CenterY
192 *
193 * For ease of use, let BetaX = MouseX - AvgX, BetaY = MouseY - AvgY
194 * Calculated Formula for CenterDist:
195 * CenterDist = (BetaX^2 + BetaY^2 - HalfHandleDist^2) / (2 * DirY * BetaX + 2 * DirY * BetaY)
196 *
197 * Returns line from center to the arbitrary point.
198 *
199 */
200 QPointF p1 = *handles()[0];
201 QPointF p2 = *handles()[1];
202
203 double deltaX = p2.x() - p1.x();
204 double deltaY = p2.y() - p1.y();
205
206 double handleDistance = KisAlgebra2D::norm(QPointF(deltaX, deltaY));
207 double halfHandleDist = handleDistance / 2.0;
208
209 double avgX = deltaX / 2.0 + p1.x();
210 double avgY = deltaY / 2.0 + p1.y();
211
212 double dirX = -deltaY / handleDistance;
213 double dirY = deltaX / handleDistance;
214
215 double betaX = thirdPoint.x() - avgX;
216 double betaY = thirdPoint.y() - avgY;
217
218 double centerDist =
219 (pow2(betaX) + pow2(betaY) - pow2(halfHandleDist))
220 /
221 (2 * dirX * betaX + 2 * dirY * betaY);
222
223 double circleCenterX = centerDist*dirX + avgX;
224 double circleCenterY = centerDist*dirY + avgY;
225 return QLineF(QPointF(circleCenterX, circleCenterY), thirdPoint);
226}
227
228QPointF CurvilinearPerspectiveAssistant::adjustPosition(const QPointF& pt, const QPointF& strokeBegin, const bool /*snapToAny*/, qreal /*moveThresholdPt*/)
229{
230 // Get the center and radius for the given point
231 QLineF initialCircle = identifyCircle(strokeBegin);
232
233 // Set the new point onto the circle.
234 QLineF magnetizedCircle(initialCircle.p1(), pt);
235 magnetizedCircle.setLength(initialCircle.length());
236
237 return magnetizedCircle.p2();
238
239}
240
242{
243 return (*handles()[0] + *handles()[1]) * 0.5;
244}
245
247{
248 return handles().size() >= 2;
249}
250
251bool CurvilinearPerspectiveAssistant::perspectiveData(const QPointF &point, qreal *signedDepth) const
252{
253 if (!isAssistantComplete()) {
254 return false;
255 }
256
257 const QPointF p1 = *handles()[0];
258 const QPointF p2 = *handles()[1];
259 const QPointF delta = p2 - p1;
260 const qreal handleDistance = KisAlgebra2D::norm(delta);
261 const qreal halfHandleDist = handleDistance / 2.0;
262
263 if (!std::isfinite(halfHandleDist) || halfHandleDist <= 0.0) {
264 return false;
265 }
266
267 const QPointF midpoint = (p1 + p2) * 0.5;
268 const QPointF normal(-delta.y() / handleDistance, delta.x() / handleDistance);
269 const qreal depth = QPointF::dotProduct(point - midpoint, normal) / halfHandleDist;
270
271 if (!std::isfinite(depth)) {
272 return false;
273 }
274
275 if (signedDepth) {
276 *signedDepth = depth;
277 }
278
279 return true;
280}
281
282bool CurvilinearPerspectiveAssistant::contains(const QPointF &point) const
283{
284 qreal signedDepth = 0.0;
285 return perspectiveData(point, &signedDepth);
286}
287
288qreal CurvilinearPerspectiveAssistant::distance(const QPointF &point) const
289{
290 qreal signedDepth = 0.0;
291
292 if (!perspectiveData(point, &signedDepth)) {
293 return 0.0;
294 }
295
296 const qreal normalizedDepth = qAbs(signedDepth);
297 // The handle line is the "infinite distance" horizon. Moving away from it
298 // increases closeness monotonically, while remaining bounded in [0, 1).
299 return normalizedDepth / (1.0 + normalizedDepth);
300}
301
306
307
311
315
317{
318 return "curvilinear-perspective";
319}
320
322{
323 return i18n("Curvilinear Perspective");
324}
325
QPointF p2
QPointF p1
KisPaintingAssistant * createPaintingAssistant() const override
void drawCache(QPainter &gc, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool assistantVisible=true) override
performance layer where the graphics can be drawn from a cache instead of generated every render upda...
bool contains(const QPointF &point) const override
KisPaintingAssistantSP clone(QMap< KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP > &handleMap) const override
QLineF identifyCircle(const QPointF thirdPoint)
QPointF adjustPosition(const QPointF &point, const QPointF &strokeBegin, const bool snapToAny, qreal moveThresholdPt) override
bool perspectiveData(const QPointF &point, qreal *signedDepth) const
qreal distance(const QPointF &point) const override
void adjustLine(QPointF &point, QPointF &strokeBegin) override
void drawAssistant(QPainter &gc, const QRectF &updateRect, const KisCoordinatesConverter *converter, const KoColorDisplayRendererInterface *displayRenderInterface, bool cached=true, KisCanvas2 *canvas=0, bool assistantVisible=true, bool previewVisible=true) override
QPointF effectiveBrushPosition(const KisCoordinatesConverter *converter, KisCanvas2 *canvas) const
Query the effective brush position to be used for preview lines. This is intended to be used for pain...
const QList< KisPaintingAssistantHandleSP > & handles() const
virtual QColor convertColorToDisplayColorSpace(const KoColor color) const =0
convertColorToDisplayColorSpace
T pow2(const T &x)
Definition kis_global.h:166
qreal kisSquareDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:194
QSharedPointer< KisPaintingAssistant > KisPaintingAssistantSP
Definition kis_types.h:189
qreal norm(const T &a)
static KoColorSpaceRegistry * instance()