Mercurial > hg > orthanc-stone
comparison Framework/Scene2D/CompositorHelper.cpp @ 585:b9ce24c606ae
TextSceneLayer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 19 Apr 2019 17:57:58 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
584:434ceeb0bcab | 585:b9ce24c606ae |
---|---|
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-2019 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 Affero 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 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "CompositorHelper.h" | |
23 | |
24 #include <Core/OrthancException.h> | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 class CompositorHelper::Item : public boost::noncopyable | |
29 { | |
30 private: | |
31 std::auto_ptr<ILayerRenderer> renderer_; | |
32 const ISceneLayer& layer_; | |
33 uint64_t lastRevision_; | |
34 | |
35 public: | |
36 Item(ILayerRenderer* renderer, // Takes ownership | |
37 const ISceneLayer& layer) : | |
38 renderer_(renderer), | |
39 layer_(layer), | |
40 lastRevision_(layer.GetRevision()) | |
41 { | |
42 if (renderer == NULL) | |
43 { | |
44 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
45 } | |
46 } | |
47 | |
48 ILayerRenderer& GetRenderer() const | |
49 { | |
50 assert(renderer_.get() != NULL); | |
51 return *renderer_; | |
52 } | |
53 | |
54 const ISceneLayer& GetLayer() const | |
55 { | |
56 return layer_; | |
57 } | |
58 | |
59 uint64_t GetLastRevision() const | |
60 { | |
61 return lastRevision_; | |
62 } | |
63 | |
64 void UpdateRenderer() | |
65 { | |
66 assert(renderer_.get() != NULL); | |
67 renderer_->Update(layer_); | |
68 lastRevision_ = layer_.GetRevision(); | |
69 } | |
70 }; | |
71 | |
72 | |
73 void CompositorHelper::Visit(const ISceneLayer& layer, | |
74 int depth) | |
75 { | |
76 Content::iterator found = content_.find(depth); | |
77 | |
78 assert(found == content_.end() || | |
79 found->second != NULL); | |
80 | |
81 if (found == content_.end() || | |
82 &found->second->GetLayer() != &layer) | |
83 { | |
84 // This is the first time this layer is rendered, or the layer | |
85 // is not the same as before | |
86 if (found != content_.end()) | |
87 { | |
88 delete found->second; | |
89 content_.erase(found); | |
90 } | |
91 | |
92 std::auto_ptr<ILayerRenderer> renderer(factory_.Create(layer)); | |
93 | |
94 if (renderer.get() != NULL) | |
95 { | |
96 renderer->Render(sceneTransform_); | |
97 content_[depth] = new Item(renderer.release(), layer); | |
98 } | |
99 } | |
100 else | |
101 { | |
102 // This layer has already been rendered | |
103 if (found->second->GetLastRevision() < layer.GetRevision()) | |
104 { | |
105 found->second->UpdateRenderer(); | |
106 } | |
107 | |
108 found->second->GetRenderer().Render(sceneTransform_); | |
109 } | |
110 | |
111 // Check invariants | |
112 assert(content_.find(depth) == content_.end() || | |
113 (&content_[depth]->GetLayer() == &layer && | |
114 content_[depth]->GetLastRevision() == layer.GetRevision())); | |
115 } | |
116 | |
117 | |
118 CompositorHelper::~CompositorHelper() | |
119 { | |
120 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
121 { | |
122 assert(it->second != NULL); | |
123 delete it->second; | |
124 } | |
125 } | |
126 | |
127 | |
128 void CompositorHelper::Refresh(unsigned int canvasWidth, | |
129 unsigned int canvasHeight) | |
130 { | |
131 // Bring coordinate (0,0) to the center of the canvas | |
132 AffineTransform2D offset = AffineTransform2D::CreateOffset( | |
133 static_cast<double>(canvasWidth) / 2.0, | |
134 static_cast<double>(canvasHeight) / 2.0); | |
135 | |
136 sceneTransform_ = AffineTransform2D::Combine(offset, scene_.GetSceneToCanvasTransform()); | |
137 scene_.Apply(*this); | |
138 } | |
139 } |