annotate OrthancStone/Sources/Scene2D/Internals/OpenGLArrowRenderer.cpp @ 2068:22a83fb9dd23 deep-learning

added AlignedMatrix and TimerLogger
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 May 2023 17:30:52 +0200
parents 7053b8a0aaec
children 07964689cb0b
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
1871
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
7053b8a0aaec upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1870
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1614
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * 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
10 * as published by the Free Software Foundation, either version 3 of
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * the License, or (at your option) any later version.
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * 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
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * Lesser General Public License for more details.
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * 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
19 * License along with this program. If not, see
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * <http://www.gnu.org/licenses/>.
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "OpenGLArrowRenderer.h"
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <OrthancException.h>
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <math.h>
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancStone
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace Internals
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 void OpenGLArrowRenderer::LoadLayer(const ArrowSceneLayer& layer)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 // "dataBody_" contains the "body" of the arrow, in scene coordinates
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 PolylineSceneLayer l;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 l.SetThickness(layer.GetThickness());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 PolylineSceneLayer::Chain chain;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 chain.push_back(layer.GetA());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 chain.push_back(layer.GetB());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 l.AddChain(chain, false, layer.GetColor());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 dataBody_.reset(new OpenGLLinesProgram::Data(context_, l));
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 // "dataHead_" contains the "head" of the arrow, properly scaled in
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 // pixel coordinates, but with center at (0,0)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 PolylineSceneLayer l;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 l.SetThickness(layer.GetThickness());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 const double c = cos(layer.GetArrowAngle());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 const double s = sin(layer.GetArrowAngle());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 PolylineSceneLayer::Chain chain;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 chain.push_back(ScenePoint2D(c, s) * layer.GetArrowLength());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 chain.push_back(ScenePoint2D(0, 0));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 chain.push_back(ScenePoint2D(c, -s) * layer.GetArrowLength());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 l.AddChain(chain, false, layer.GetColor());
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 dataHead_.reset(new OpenGLLinesProgram::Data(context_, l));
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 // 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
69 ScenePoint2D direction = layer.GetB() - layer.GetA();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 double n = ScenePoint2D::SquaredMagnitude(direction);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (LinearAlgebra::IsCloseToZero(n))
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 direction = ScenePoint2D(1, 0);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 else
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 direction = direction / sqrt(n);
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 // 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
81 // https://math.stackexchange.com/a/3565068
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 Matrix rotation = LinearAlgebra::ZeroMatrix(3, 3);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 rotation(0, 0) = direction.GetX();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 rotation(1, 0) = direction.GetY();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 rotation(0, 1) = -direction.GetY();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 rotation(1, 1) = direction.GetX();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 rotation(2, 2) = 1;
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 transformHead_ = AffineTransform2D::Combine(
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 AffineTransform2D::CreateOffset(layer.GetA().GetX(), layer.GetA().GetY()),
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 AffineTransform2D(rotation));
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (dataBody_.get() == NULL ||
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 dataHead_.get() == NULL)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 OpenGLArrowRenderer::OpenGLArrowRenderer(OpenGL::IOpenGLContext& context,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 OpenGLLinesProgram& program,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 const ArrowSceneLayer& layer) :
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 context_(context),
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 program_(program)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 LoadLayer(layer);
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 void OpenGLArrowRenderer::Render(const AffineTransform2D& transform,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 unsigned int canvasWidth,
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 unsigned int canvasHeight)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 if (!context_.IsContextLost())
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 program_.Apply(*dataBody_, transform, canvasWidth, canvasHeight, true, true);
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 const double z = 1.0 / transform.ComputeZoom();
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 const AffineTransform2D t2 = AffineTransform2D::Combine(
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 transform, // 3. Apply the original transform
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 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
123 AffineTransform2D::CreateScaling(z, z)); // 1. Neutralize the zoom level
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 program_.Apply(*dataHead_, t2, canvasWidth, canvasHeight, true, true);
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
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 void OpenGLArrowRenderer::Update(const ISceneLayer& layer)
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 LoadLayer(dynamic_cast<const ArrowSceneLayer&>(layer));
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 }
ad9b425f27ae new class: ArrowSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }