Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Scene2D/Internals/OpenGLArrowRenderer.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 "OpenGLArrowRenderer.h" | |
24 | |
25 #include <OrthancException.h> | |
26 | |
27 #include <math.h> | |
28 | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 namespace Internals | |
33 { | |
34 void OpenGLArrowRenderer::LoadLayer(const ArrowSceneLayer& layer) | |
35 { | |
36 // "dataBody_" contains the "body" of the arrow, in scene coordinates | |
37 { | |
38 PolylineSceneLayer l; | |
39 l.SetThickness(layer.GetThickness()); | |
40 | |
41 PolylineSceneLayer::Chain chain; | |
42 chain.push_back(layer.GetA()); | |
43 chain.push_back(layer.GetB()); | |
44 l.AddChain(chain, false, layer.GetColor()); | |
45 | |
46 dataBody_.reset(new OpenGLLinesProgram::Data(context_, l)); | |
47 } | |
48 | |
49 // "dataHead_" contains the "head" of the arrow, properly scaled in | |
50 // pixel coordinates, but with center at (0,0) | |
51 { | |
52 PolylineSceneLayer l; | |
53 l.SetThickness(layer.GetThickness()); | |
54 | |
55 const double c = cos(layer.GetArrowAngle()); | |
56 const double s = sin(layer.GetArrowAngle()); | |
57 | |
58 PolylineSceneLayer::Chain chain; | |
59 chain.push_back(ScenePoint2D(c, s) * layer.GetArrowLength()); | |
60 chain.push_back(ScenePoint2D(0, 0)); | |
61 chain.push_back(ScenePoint2D(c, -s) * layer.GetArrowLength()); | |
62 l.AddChain(chain, false, layer.GetColor()); | |
63 | |
64 dataHead_.reset(new OpenGLLinesProgram::Data(context_, l)); | |
65 } | |
66 | |
67 // Compute a unit vector encoding the direction of the body of the arrow | |
68 ScenePoint2D direction = layer.GetB() - layer.GetA(); | |
69 double n = ScenePoint2D::SquaredMagnitude(direction); | |
70 if (LinearAlgebra::IsCloseToZero(n)) | |
71 { | |
72 direction = ScenePoint2D(1, 0); | |
73 } | |
74 else | |
75 { | |
76 direction = direction / sqrt(n); | |
77 } | |
78 | |
79 // Compute a rotation matrix, to bring the "head" in the axis of the "body" | |
80 // https://math.stackexchange.com/a/3565068 | |
81 Matrix rotation = LinearAlgebra::ZeroMatrix(3, 3); | |
82 rotation(0, 0) = direction.GetX(); | |
83 rotation(1, 0) = direction.GetY(); | |
84 rotation(0, 1) = -direction.GetY(); | |
85 rotation(1, 1) = direction.GetX(); | |
86 rotation(2, 2) = 1; | |
87 | |
88 transformHead_ = AffineTransform2D::Combine( | |
89 AffineTransform2D::CreateOffset(layer.GetA().GetX(), layer.GetA().GetY()), | |
90 AffineTransform2D(rotation)); | |
91 | |
92 if (dataBody_.get() == NULL || | |
93 dataHead_.get() == NULL) | |
94 { | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
96 } | |
97 } | |
98 | |
99 | |
100 OpenGLArrowRenderer::OpenGLArrowRenderer(OpenGL::IOpenGLContext& context, | |
101 OpenGLLinesProgram& program, | |
102 const ArrowSceneLayer& layer) : | |
103 context_(context), | |
104 program_(program) | |
105 { | |
106 LoadLayer(layer); | |
107 } | |
108 | |
109 | |
110 void OpenGLArrowRenderer::Render(const AffineTransform2D& transform, | |
111 unsigned int canvasWidth, | |
112 unsigned int canvasHeight) | |
113 { | |
114 if (!context_.IsContextLost()) | |
115 { | |
116 program_.Apply(*dataBody_, transform, canvasWidth, canvasHeight, true, true); | |
117 | |
118 const double z = 1.0 / transform.ComputeZoom(); | |
119 const AffineTransform2D t2 = AffineTransform2D::Combine( | |
120 transform, // 3. Apply the original transform | |
121 transformHead_, // 2. Bring the "head" of the arrow at the proper position/angle | |
122 AffineTransform2D::CreateScaling(z, z)); // 1. Neutralize the zoom level | |
123 | |
124 program_.Apply(*dataHead_, t2, canvasWidth, canvasHeight, true, true); | |
125 } | |
126 } | |
127 | |
128 | |
129 void OpenGLArrowRenderer::Update(const ISceneLayer& layer) | |
130 { | |
131 LoadLayer(dynamic_cast<const ArrowSceneLayer&>(layer)); | |
132 } | |
133 } | |
134 } |