comparison Framework/Scene2D/Scene2D.cpp @ 582:e36e69a380a5

Scene2D
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 16:43:08 +0200
parents
children 6129b1e5ba42
comparison
equal deleted inserted replaced
581:d933fc19214a 582:e36e69a380a5
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 "Scene2D.h"
23
24 #include <Core/OrthancException.h>
25
26
27 namespace OrthancStone
28 {
29 Scene2D::Scene2D(const Scene2D& other) :
30 sceneToCanvas_(other.sceneToCanvas_),
31 canvasToScene_(other.canvasToScene_)
32 {
33 for (Content::const_iterator it = other.content_.begin();
34 it != other.content_.end(); ++it)
35 {
36 content_[it->first] = it->second->Clone();
37 }
38 }
39
40
41 Scene2D::~Scene2D()
42 {
43 for (Content::iterator it = content_.begin();
44 it != content_.end(); ++it)
45 {
46 assert(it->second != NULL);
47 delete it->second;
48 }
49 }
50
51
52 void Scene2D::SetLayer(int depth,
53 ISceneLayer* layer) // Takes ownership
54 {
55 std::auto_ptr<ISceneLayer> protection(layer);
56
57 if (layer == NULL)
58 {
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
60 }
61
62 Content::iterator found = content_.find(depth);
63
64 if (found == content_.end())
65 {
66 content_[depth] = protection.release();
67 }
68 else
69 {
70 assert(found->second != NULL);
71 delete found->second;
72 found->second = protection.release();
73 }
74 }
75
76
77 void Scene2D::DeleteLayer(int depth)
78 {
79 Content::iterator found = content_.find(depth);
80
81 if (found != content_.end())
82 {
83 assert(found->second != NULL);
84 delete found->second;
85 content_.erase(found);
86 }
87 }
88
89
90 void Scene2D::Apply(IVisitor& visitor)
91 {
92 for (Content::const_iterator it = content_.begin();
93 it != content_.end(); ++it)
94 {
95 assert(it->second != NULL);
96 visitor.Visit(*it->second, it->first);
97 }
98 }
99
100
101 void Scene2D::SetSceneToCanvasTransform(const AffineTransform2D& transform)
102 {
103 // Make sure the transform is invertible before making any change
104 AffineTransform2D inverse = AffineTransform2D::Invert(transform);
105
106 sceneToCanvas_ = transform;
107 canvasToScene_ = inverse;
108 }
109
110
111 void Scene2D::FitContent(unsigned int canvasWidth,
112 unsigned int canvasHeight)
113 {
114 Extent2D extent;
115
116 for (Content::const_iterator it = content_.begin();
117 it != content_.end(); ++it)
118 {
119 assert(it->second != NULL);
120
121 Extent2D tmp;
122 if (it->second->GetBoundingBox(tmp))
123 {
124 extent.Union(tmp);
125 }
126 }
127
128 if (!extent.IsEmpty())
129 {
130 double zoomX = static_cast<double>(canvasWidth) / extent.GetWidth();
131 double zoomY = static_cast<double>(canvasHeight) / extent.GetHeight();
132
133 double zoom = std::min(zoomX, zoomY);
134 if (LinearAlgebra::IsCloseToZero(zoom))
135 {
136 zoom = 1;
137 }
138
139 double panX = extent.GetCenterX();
140 double panY = extent.GetCenterY();
141
142 // Bring the center of the scene to (0,0)
143 AffineTransform2D t1 = AffineTransform2D::CreateOffset(-panX, -panY);
144
145 // Scale the scene
146 AffineTransform2D t2 = AffineTransform2D::CreateScaling(zoom, zoom);
147
148 SetSceneToCanvasTransform(AffineTransform2D::Combine(t2, t1));
149 }
150 }
151 }