comparison OrthancStone/Sources/Scene2D/Internals/CairoArrowRenderer.cpp @ 1614:ad9b425f27ae

new class: ArrowSceneLayer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Oct 2020 16:26:39 +0100
parents
children 9ac2a65d4172
comparison
equal deleted inserted replaced
1613:5f0660fe06c3 1614:ad9b425f27ae
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "CairoArrowRenderer.h"
24
25 #include "../ArrowSceneLayer.h"
26
27 namespace OrthancStone
28 {
29 namespace Internals
30 {
31 void CairoArrowRenderer::Render(const AffineTransform2D& transform,
32 unsigned int canvasWidth,
33 unsigned int canvasHeight)
34 {
35 const ArrowSceneLayer& layer = GetLayer<ArrowSceneLayer>();
36
37 cairo_t* cr = GetCairoContext();
38
39 cairo_set_line_width(cr, layer.GetThickness());
40
41 const Color& color = layer.GetColor();
42 cairo_set_source_rgb(cr, color.GetRedAsFloat(),
43 color.GetGreenAsFloat(),
44 color.GetBlueAsFloat());
45
46 const double zoom = transform.ComputeZoom();
47
48 ScenePoint2D axis = layer.GetB() - layer.GetA();
49 double n = ScenePoint2D::SquaredMagnitude(axis);
50
51 if (!LinearAlgebra::IsCloseToZero(n))
52 {
53 axis = axis * layer.GetArrowLength() / (sqrt(n) * zoom);
54
55 const ScenePoint2D a = layer.GetA().Apply(transform);
56 const ScenePoint2D b = layer.GetB().Apply(transform);
57
58 cairo_move_to(cr, a.GetX(), a.GetY());
59 cairo_line_to(cr, b.GetX(), b.GetY());
60
61 AffineTransform2D r1 = AffineTransform2D::CreateRotation(layer.GetArrowAngle());
62 const ScenePoint2D c = (layer.GetA() + axis.Apply(r1)).Apply(transform);
63
64 AffineTransform2D r2 = AffineTransform2D::CreateRotation(-layer.GetArrowAngle());
65 const ScenePoint2D d = (layer.GetA() + axis.Apply(r2)).Apply(transform);
66
67 cairo_move_to(cr, a.GetX(), a.GetY());
68 cairo_line_to(cr, c.GetX(), c.GetY());
69
70 cairo_move_to(cr, a.GetX(), a.GetY());
71 cairo_line_to(cr, d.GetX(), d.GetY());
72
73 cairo_stroke(cr);
74 }
75 }
76 }
77 }