annotate OrthancStone/Sources/Scene2D/Internals/OpenGLArrowRenderer.cpp @ 1652:fa9e6bf84958

integrating pdf.js into Stone Web viewer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Nov 2020 20:47:53 +0100
parents ad9b425f27ae
children 9ac2a65d4172
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1614
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Lesser General Public License for more details.
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * License along with this program. If not, see
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * <http://www.gnu.org/licenses/>.
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "OpenGLArrowRenderer.h"
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <OrthancException.h>
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <math.h>
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 namespace OrthancStone
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 namespace Internals
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 void OpenGLArrowRenderer::LoadLayer(const ArrowSceneLayer& layer)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 // "dataBody_" contains the "body" of the arrow, in scene coordinates
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 PolylineSceneLayer l;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 l.SetThickness(layer.GetThickness());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 PolylineSceneLayer::Chain chain;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 chain.push_back(layer.GetA());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 chain.push_back(layer.GetB());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 l.AddChain(chain, false, layer.GetColor());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 dataBody_.reset(new OpenGLLinesProgram::Data(context_, l));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 // "dataHead_" contains the "head" of the arrow, properly scaled in
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 // pixel coordinates, but with center at (0,0)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 PolylineSceneLayer l;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 l.SetThickness(layer.GetThickness());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 const double c = cos(layer.GetArrowAngle());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 const double s = sin(layer.GetArrowAngle());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 PolylineSceneLayer::Chain chain;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 chain.push_back(ScenePoint2D(c, s) * layer.GetArrowLength());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 chain.push_back(ScenePoint2D(0, 0));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 chain.push_back(ScenePoint2D(c, -s) * layer.GetArrowLength());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 l.AddChain(chain, false, layer.GetColor());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 dataHead_.reset(new OpenGLLinesProgram::Data(context_, l));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 // Compute a unit vector encoding the direction of the body of the arrow
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 ScenePoint2D direction = layer.GetB() - layer.GetA();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 double n = ScenePoint2D::SquaredMagnitude(direction);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if (LinearAlgebra::IsCloseToZero(n))
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 direction = ScenePoint2D(1, 0);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 else
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 direction = direction / sqrt(n);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 // Compute a rotation matrix, to bring the "head" in the axis of the "body"
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 // https://math.stackexchange.com/a/3565068
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 Matrix rotation = LinearAlgebra::ZeroMatrix(3, 3);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 rotation(0, 0) = direction.GetX();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 rotation(1, 0) = direction.GetY();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 rotation(0, 1) = -direction.GetY();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 rotation(1, 1) = direction.GetX();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 rotation(2, 2) = 1;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 transformHead_ = AffineTransform2D::Combine(
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 AffineTransform2D::CreateOffset(layer.GetA().GetX(), layer.GetA().GetY()),
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 AffineTransform2D(rotation));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if (dataBody_.get() == NULL ||
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 dataHead_.get() == NULL)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 OpenGLArrowRenderer::OpenGLArrowRenderer(OpenGL::IOpenGLContext& context,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 OpenGLLinesProgram& program,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 const ArrowSceneLayer& layer) :
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 context_(context),
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 program_(program)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 LoadLayer(layer);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 void OpenGLArrowRenderer::Render(const AffineTransform2D& transform,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 unsigned int canvasWidth,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 unsigned int canvasHeight)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 if (!context_.IsContextLost())
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 program_.Apply(*dataBody_, transform, canvasWidth, canvasHeight, true, true);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 const double z = 1.0 / transform.ComputeZoom();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 const AffineTransform2D t2 = AffineTransform2D::Combine(
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 transform, // 3. Apply the original transform
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 transformHead_, // 2. Bring the "head" of the arrow at the proper position/angle
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 AffineTransform2D::CreateScaling(z, z)); // 1. Neutralize the zoom level
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 program_.Apply(*dataHead_, t2, canvasWidth, canvasHeight, true, true);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 void OpenGLArrowRenderer::Update(const ISceneLayer& layer)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 LoadLayer(dynamic_cast<const ArrowSceneLayer&>(layer));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }