Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/AnnotationsSceneLayer.cpp @ 1821:36430d73e36c
introducing measure units in AnnotationsSceneLayer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 26 May 2021 14:02:12 +0200 |
parents | fe402c678d18 |
children | 781e9fc8925e |
rev | line source |
---|---|
1804 | 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-2021 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 "AnnotationsSceneLayer.h" | |
24 | |
25 #include "MacroSceneLayer.h" | |
26 #include "PolylineSceneLayer.h" | |
27 #include "TextSceneLayer.h" | |
28 | |
29 #include <OrthancException.h> | |
30 | |
31 #include <boost/math/constants/constants.hpp> | |
32 #include <list> | |
33 | |
34 static const double HANDLE_SIZE = 10.0; | |
35 static const double PI = boost::math::constants::pi<double>(); | |
36 | |
37 static const char* const KEY_ANNOTATIONS = "annotations"; | |
38 static const char* const KEY_TYPE = "type"; | |
39 static const char* const KEY_X1 = "x1"; | |
40 static const char* const KEY_Y1 = "y1"; | |
41 static const char* const KEY_X2 = "x2"; | |
42 static const char* const KEY_Y2 = "y2"; | |
43 static const char* const KEY_X3 = "x3"; | |
44 static const char* const KEY_Y3 = "y3"; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
45 static const char* const KEY_UNITS = "units"; |
1804 | 46 |
47 static const char* const VALUE_ANGLE = "angle"; | |
48 static const char* const VALUE_CIRCLE = "circle"; | |
49 static const char* const VALUE_SEGMENT = "segment"; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
50 static const char* const VALUE_MILLIMETERS = "millimeters"; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
51 static const char* const VALUE_PIXELS = "pixels"; |
1804 | 52 |
1819
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
53 #if 0 |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
54 static OrthancStone::Color COLOR_PRIMITIVES(192, 192, 192); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
55 static OrthancStone::Color COLOR_HOVER(0, 255, 0); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
56 static OrthancStone::Color COLOR_TEXT(255, 0, 0); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
57 #else |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
58 static OrthancStone::Color COLOR_PRIMITIVES(0x40, 0x82, 0xad); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
59 static OrthancStone::Color COLOR_HOVER(0x40, 0xad, 0x79); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
60 static OrthancStone::Color COLOR_TEXT(0x4e, 0xde, 0x99); |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
61 #endif |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
62 |
1804 | 63 |
64 namespace OrthancStone | |
65 { | |
66 class AnnotationsSceneLayer::GeometricPrimitive : public boost::noncopyable | |
67 { | |
68 private: | |
69 bool modified_; | |
70 Annotation& parentAnnotation_; | |
71 Color color_; | |
72 Color hoverColor_; | |
73 bool isHover_; | |
74 int depth_; | |
75 | |
76 public: | |
77 GeometricPrimitive(Annotation& parentAnnotation, | |
78 int depth) : | |
79 modified_(true), | |
80 parentAnnotation_(parentAnnotation), | |
1819
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
81 color_(COLOR_PRIMITIVES), |
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
82 hoverColor_(COLOR_HOVER), |
1804 | 83 isHover_(false), |
84 depth_(depth) | |
85 { | |
86 } | |
87 | |
88 virtual ~GeometricPrimitive() | |
89 { | |
90 } | |
91 | |
92 Annotation& GetParentAnnotation() const | |
93 { | |
94 return parentAnnotation_; | |
95 } | |
96 | |
97 int GetDepth() const | |
98 { | |
99 return depth_; | |
100 } | |
101 | |
102 void SetHover(bool hover) | |
103 { | |
104 if (hover != isHover_) | |
105 { | |
106 isHover_ = hover; | |
107 modified_ = true; | |
108 } | |
109 } | |
110 | |
111 bool IsHover() const | |
112 { | |
113 return isHover_; | |
114 } | |
115 | |
116 void SetModified(bool modified) | |
117 { | |
118 modified_ = modified; | |
119 } | |
120 | |
121 bool IsModified() const | |
122 { | |
123 return modified_; | |
124 } | |
125 | |
126 void SetColor(const Color& color) | |
127 { | |
128 SetModified(true); | |
129 color_ = color; | |
130 } | |
131 | |
132 void SetHoverColor(const Color& color) | |
133 { | |
134 SetModified(true); | |
135 hoverColor_ = color; | |
136 } | |
137 | |
138 const Color& GetColor() const | |
139 { | |
140 return color_; | |
141 } | |
142 | |
143 const Color& GetHoverColor() const | |
144 { | |
145 return hoverColor_; | |
146 } | |
147 | |
148 virtual bool IsHit(const ScenePoint2D& p, | |
149 const Scene2D& scene) const = 0; | |
150 | |
151 // Always called, even if not modified | |
152 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
153 const Scene2D& scene) = 0; | |
154 | |
155 // Only called if modified | |
156 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
157 const Scene2D& scene) = 0; | |
158 | |
159 virtual void MovePreview(const ScenePoint2D& delta) = 0; | |
160 | |
161 virtual void MoveDone(const ScenePoint2D& delta) = 0; | |
162 }; | |
163 | |
164 | |
165 class AnnotationsSceneLayer::Annotation : public boost::noncopyable | |
166 { | |
167 private: | |
168 typedef std::list<GeometricPrimitive*> GeometricPrimitives; | |
169 | |
170 AnnotationsSceneLayer& that_; | |
171 GeometricPrimitives primitives_; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
172 Units units_; |
1804 | 173 |
174 public: | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
175 explicit Annotation(AnnotationsSceneLayer& that, |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
176 Units units) : |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
177 that_(that), |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
178 units_(units) |
1804 | 179 { |
180 that.AddAnnotation(this); | |
181 } | |
182 | |
183 virtual ~Annotation() | |
184 { | |
185 for (GeometricPrimitives::iterator it = primitives_.begin(); it != primitives_.end(); ++it) | |
186 { | |
187 that_.DeletePrimitive(*it); | |
188 } | |
189 } | |
190 | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
191 Units GetUnits() const |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
192 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
193 return units_; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
194 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
195 |
1804 | 196 GeometricPrimitive* AddPrimitive(GeometricPrimitive* primitive) |
197 { | |
198 if (primitive == NULL) | |
199 { | |
200 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
201 } | |
202 else | |
203 { | |
204 assert(that_.primitives_.find(primitive) == that_.primitives_.end()); | |
205 primitives_.push_back(primitive); // For automated deallocation | |
206 that_.primitives_.insert(primitive); | |
207 return primitive; | |
208 } | |
209 } | |
210 | |
211 template <typename T> | |
212 T& AddTypedPrimitive(T* primitive) | |
213 { | |
214 AddPrimitive(primitive); | |
215 return *primitive; | |
216 } | |
217 | |
218 virtual void SignalMove(GeometricPrimitive& primitive) = 0; | |
219 | |
220 virtual void Serialize(Json::Value& target) = 0; | |
221 }; | |
222 | |
223 | |
224 class AnnotationsSceneLayer::Handle : public GeometricPrimitive | |
225 { | |
226 private: | |
227 ScenePoint2D center_; | |
228 ScenePoint2D delta_; | |
229 | |
230 public: | |
231 explicit Handle(Annotation& parentAnnotation, | |
232 const ScenePoint2D& center) : | |
233 GeometricPrimitive(parentAnnotation, 0), // Highest priority | |
234 center_(center), | |
235 delta_(0, 0) | |
236 { | |
237 } | |
238 | |
239 void SetSize(unsigned int size) | |
240 { | |
241 SetModified(true); | |
242 } | |
243 | |
244 void SetCenter(const ScenePoint2D& center) | |
245 { | |
246 SetModified(true); | |
247 center_ = center; | |
248 delta_ = ScenePoint2D(0, 0); | |
249 } | |
250 | |
251 ScenePoint2D GetCenter() const | |
252 { | |
253 return center_ + delta_; | |
254 } | |
255 | |
256 virtual bool IsHit(const ScenePoint2D& p, | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
257 const Scene2D& scene) const ORTHANC_OVERRIDE |
1804 | 258 { |
259 const double zoom = scene.GetSceneToCanvasTransform().ComputeZoom(); | |
260 | |
261 double dx = (center_.GetX() + delta_.GetX() - p.GetX()) * zoom; | |
262 double dy = (center_.GetY() + delta_.GetY() - p.GetY()) * zoom; | |
263 | |
264 return (std::abs(dx) <= HANDLE_SIZE / 2.0 && | |
265 std::abs(dy) <= HANDLE_SIZE / 2.0); | |
266 } | |
267 | |
268 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
269 const Scene2D& scene) ORTHANC_OVERRIDE | |
270 { | |
271 const double zoom = scene.GetSceneToCanvasTransform().ComputeZoom(); | |
272 | |
273 // TODO: take DPI into account | |
274 double x1 = center_.GetX() + delta_.GetX() - (HANDLE_SIZE / 2.0) / zoom; | |
275 double y1 = center_.GetY() + delta_.GetY() - (HANDLE_SIZE / 2.0) / zoom; | |
276 double x2 = center_.GetX() + delta_.GetX() + (HANDLE_SIZE / 2.0) / zoom; | |
277 double y2 = center_.GetY() + delta_.GetY() + (HANDLE_SIZE / 2.0) / zoom; | |
278 | |
279 PolylineSceneLayer::Chain chain; | |
280 chain.reserve(4); | |
281 chain.push_back(ScenePoint2D(x1, y1)); | |
282 chain.push_back(ScenePoint2D(x2, y1)); | |
283 chain.push_back(ScenePoint2D(x2, y2)); | |
284 chain.push_back(ScenePoint2D(x1, y2)); | |
285 | |
286 if (IsHover()) | |
287 { | |
288 polyline.AddChain(chain, true /* closed */, GetHoverColor()); | |
289 } | |
290 else | |
291 { | |
292 polyline.AddChain(chain, true /* closed */, GetColor()); | |
293 } | |
294 } | |
295 | |
296 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
297 const Scene2D& scene) ORTHANC_OVERRIDE | |
298 { | |
299 } | |
300 | |
301 virtual void MovePreview(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
302 { | |
303 SetModified(true); | |
304 delta_ = delta; | |
305 GetParentAnnotation().SignalMove(*this); | |
306 } | |
307 | |
308 virtual void MoveDone(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
309 { | |
310 SetModified(true); | |
311 center_ = center_ + delta; | |
312 delta_ = ScenePoint2D(0, 0); | |
313 GetParentAnnotation().SignalMove(*this); | |
314 } | |
315 }; | |
316 | |
317 | |
318 class AnnotationsSceneLayer::Segment : public GeometricPrimitive | |
319 { | |
320 private: | |
321 ScenePoint2D p1_; | |
322 ScenePoint2D p2_; | |
323 ScenePoint2D delta_; | |
324 | |
325 public: | |
326 Segment(Annotation& parentAnnotation, | |
327 const ScenePoint2D& p1, | |
328 const ScenePoint2D& p2) : | |
329 GeometricPrimitive(parentAnnotation, 1), // Can only be selected if no handle matches | |
330 p1_(p1), | |
331 p2_(p2), | |
332 delta_(0, 0) | |
333 { | |
334 } | |
335 | |
336 void SetPosition(const ScenePoint2D& p1, | |
337 const ScenePoint2D& p2) | |
338 { | |
339 SetModified(true); | |
340 p1_ = p1; | |
341 p2_ = p2; | |
342 delta_ = ScenePoint2D(0, 0); | |
343 } | |
344 | |
345 ScenePoint2D GetPosition1() const | |
346 { | |
347 return p1_ + delta_; | |
348 } | |
349 | |
350 ScenePoint2D GetPosition2() const | |
351 { | |
352 return p2_ + delta_; | |
353 } | |
354 | |
355 virtual bool IsHit(const ScenePoint2D& p, | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
356 const Scene2D& scene) const ORTHANC_OVERRIDE |
1804 | 357 { |
358 const double zoom = scene.GetSceneToCanvasTransform().ComputeZoom(); | |
359 return (ScenePoint2D::SquaredDistancePtSegment(p1_ + delta_, p2_ + delta_, p) * zoom * zoom <= | |
360 (HANDLE_SIZE / 2.0) * (HANDLE_SIZE / 2.0)); | |
361 } | |
362 | |
363 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
364 const Scene2D& scene) ORTHANC_OVERRIDE | |
365 { | |
366 PolylineSceneLayer::Chain chain; | |
367 chain.reserve(2); | |
368 chain.push_back(p1_ + delta_); | |
369 chain.push_back(p2_ + delta_); | |
370 | |
371 if (IsHover()) | |
372 { | |
373 polyline.AddChain(chain, false /* closed */, GetHoverColor()); | |
374 } | |
375 else | |
376 { | |
377 polyline.AddChain(chain, false /* closed */, GetColor()); | |
378 } | |
379 } | |
380 | |
381 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
382 const Scene2D& scene) ORTHANC_OVERRIDE | |
383 { | |
384 } | |
385 | |
386 virtual void MovePreview(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
387 { | |
388 SetModified(true); | |
389 delta_ = delta; | |
390 GetParentAnnotation().SignalMove(*this); | |
391 } | |
392 | |
393 virtual void MoveDone(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
394 { | |
395 SetModified(true); | |
396 p1_ = p1_ + delta; | |
397 p2_ = p2_ + delta; | |
398 delta_ = ScenePoint2D(0, 0); | |
399 GetParentAnnotation().SignalMove(*this); | |
400 } | |
401 }; | |
402 | |
403 | |
404 class AnnotationsSceneLayer::Circle : public GeometricPrimitive | |
405 { | |
406 private: | |
407 ScenePoint2D p1_; | |
408 ScenePoint2D p2_; | |
409 | |
410 public: | |
411 Circle(Annotation& parentAnnotation, | |
412 const ScenePoint2D& p1, | |
413 const ScenePoint2D& p2) : | |
414 GeometricPrimitive(parentAnnotation, 2), | |
415 p1_(p1), | |
416 p2_(p2) | |
417 { | |
418 } | |
419 | |
420 void SetPosition(const ScenePoint2D& p1, | |
421 const ScenePoint2D& p2) | |
422 { | |
423 SetModified(true); | |
424 p1_ = p1; | |
425 p2_ = p2; | |
426 } | |
427 | |
428 ScenePoint2D GetPosition1() const | |
429 { | |
430 return p1_; | |
431 } | |
432 | |
433 ScenePoint2D GetPosition2() const | |
434 { | |
435 return p2_; | |
436 } | |
437 | |
438 virtual bool IsHit(const ScenePoint2D& p, | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
439 const Scene2D& scene) const ORTHANC_OVERRIDE |
1804 | 440 { |
441 return false; | |
442 } | |
443 | |
444 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
445 const Scene2D& scene) ORTHANC_OVERRIDE | |
446 { | |
447 static unsigned int NUM_SEGMENTS = 128; | |
448 | |
449 ScenePoint2D middle((p1_.GetX() + p2_.GetX()) / 2.0, | |
450 (p1_.GetY() + p2_.GetY()) / 2.0); | |
451 | |
452 const double radius = ScenePoint2D::DistancePtPt(middle, p1_); | |
453 | |
454 double increment = 2.0 * PI / static_cast<double>(NUM_SEGMENTS - 1); | |
455 | |
456 PolylineSceneLayer::Chain chain; | |
457 chain.reserve(NUM_SEGMENTS); | |
458 | |
459 double theta = 0; | |
460 for (unsigned int i = 0; i < NUM_SEGMENTS; i++) | |
461 { | |
462 chain.push_back(ScenePoint2D(middle.GetX() + radius * cos(theta), | |
463 middle.GetY() + radius * sin(theta))); | |
464 theta += increment; | |
465 } | |
466 | |
467 if (IsHover()) | |
468 { | |
469 polyline.AddChain(chain, false /* closed */, GetHoverColor()); | |
470 } | |
471 else | |
472 { | |
473 polyline.AddChain(chain, false /* closed */, GetColor()); | |
474 } | |
475 } | |
476 | |
477 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
478 const Scene2D& scene) ORTHANC_OVERRIDE | |
479 { | |
480 } | |
481 | |
482 virtual void MovePreview(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
483 { | |
484 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
485 } | |
486 | |
487 virtual void MoveDone(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
488 { | |
489 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
490 } | |
491 }; | |
492 | |
493 | |
494 class AnnotationsSceneLayer::Arc : public GeometricPrimitive | |
495 { | |
496 private: | |
497 ScenePoint2D start_; | |
498 ScenePoint2D middle_; | |
499 ScenePoint2D end_; | |
500 double radius_; // in pixels | |
501 | |
502 void ComputeAngles(double& fullAngle, | |
503 double& startAngle, | |
504 double& endAngle) const | |
505 { | |
506 const double x1 = start_.GetX(); | |
507 const double y1 = start_.GetY(); | |
508 const double xc = middle_.GetX(); | |
509 const double yc = middle_.GetY(); | |
510 const double x2 = end_.GetX(); | |
511 const double y2 = end_.GetY(); | |
512 | |
513 startAngle = atan2(y1 - yc, x1 - xc); | |
514 endAngle = atan2(y2 - yc, x2 - xc); | |
515 | |
516 fullAngle = endAngle - startAngle; | |
517 | |
518 while (fullAngle < -PI) | |
519 { | |
520 fullAngle += 2.0 * PI; | |
521 } | |
522 | |
523 while (fullAngle >= PI) | |
524 { | |
525 fullAngle -= 2.0 * PI; | |
526 } | |
527 } | |
528 | |
529 public: | |
530 Arc(Annotation& parentAnnotation, | |
531 const ScenePoint2D& start, | |
532 const ScenePoint2D& middle, | |
533 const ScenePoint2D& end) : | |
534 GeometricPrimitive(parentAnnotation, 2), | |
535 start_(start), | |
536 middle_(middle), | |
537 end_(end), | |
538 radius_(20) | |
539 { | |
540 } | |
541 | |
542 double GetAngle() const | |
543 { | |
544 double fullAngle, startAngle, endAngle; | |
545 ComputeAngles(fullAngle, startAngle, endAngle); | |
546 return fullAngle; | |
547 } | |
548 | |
549 void SetStart(const ScenePoint2D& p) | |
550 { | |
551 SetModified(true); | |
552 start_ = p; | |
553 } | |
554 | |
555 void SetMiddle(const ScenePoint2D& p) | |
556 { | |
557 SetModified(true); | |
558 middle_ = p; | |
559 } | |
560 | |
561 void SetEnd(const ScenePoint2D& p) | |
562 { | |
563 SetModified(true); | |
564 end_ = p; | |
565 } | |
566 | |
567 virtual bool IsHit(const ScenePoint2D& p, | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
568 const Scene2D& scene) const ORTHANC_OVERRIDE |
1804 | 569 { |
570 return false; | |
571 } | |
572 | |
573 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
574 const Scene2D& scene) ORTHANC_OVERRIDE | |
575 { | |
576 static unsigned int NUM_SEGMENTS = 64; | |
577 | |
578 const double radius = radius_ / scene.GetSceneToCanvasTransform().ComputeZoom(); | |
579 | |
580 double fullAngle, startAngle, endAngle; | |
581 ComputeAngles(fullAngle, startAngle, endAngle); | |
582 | |
583 double increment = fullAngle / static_cast<double>(NUM_SEGMENTS - 1); | |
584 | |
585 PolylineSceneLayer::Chain chain; | |
586 chain.reserve(NUM_SEGMENTS); | |
587 | |
588 double theta = startAngle; | |
589 for (unsigned int i = 0; i < NUM_SEGMENTS; i++) | |
590 { | |
591 chain.push_back(ScenePoint2D(middle_.GetX() + radius * cos(theta), | |
592 middle_.GetY() + radius * sin(theta))); | |
593 theta += increment; | |
594 } | |
595 | |
596 if (IsHover()) | |
597 { | |
598 polyline.AddChain(chain, false /* closed */, GetHoverColor()); | |
599 } | |
600 else | |
601 { | |
602 polyline.AddChain(chain, false /* closed */, GetColor()); | |
603 } | |
604 } | |
605 | |
606 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
607 const Scene2D& scene) ORTHANC_OVERRIDE | |
608 { | |
609 } | |
610 | |
611 virtual void MovePreview(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
612 { | |
613 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
614 } | |
615 | |
616 virtual void MoveDone(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
617 { | |
618 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
619 } | |
620 }; | |
621 | |
622 | |
623 class AnnotationsSceneLayer::Text : public GeometricPrimitive | |
624 { | |
625 private: | |
626 AnnotationsSceneLayer& that_; | |
627 bool first_; | |
628 size_t subLayer_; | |
629 std::unique_ptr<TextSceneLayer> content_; | |
630 | |
631 public: | |
632 Text(AnnotationsSceneLayer& that, | |
633 Annotation& parentAnnotation) : | |
634 GeometricPrimitive(parentAnnotation, 2), | |
635 that_(that), | |
1817 | 636 first_(true), |
637 subLayer_(0) // dummy initialization | |
1804 | 638 { |
639 } | |
640 | |
641 virtual ~Text() | |
642 { | |
643 if (!first_) | |
644 { | |
645 that_.TagSubLayerToRemove(subLayer_); | |
646 } | |
647 } | |
648 | |
649 void SetContent(const TextSceneLayer& content) | |
650 { | |
651 SetModified(true); | |
652 content_.reset(dynamic_cast<TextSceneLayer*>(content.Clone())); | |
653 } | |
654 | |
655 virtual bool IsHit(const ScenePoint2D& p, | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
656 const Scene2D& scene) const ORTHANC_OVERRIDE |
1804 | 657 { |
658 return false; | |
659 } | |
660 | |
661 virtual void RenderPolylineLayer(PolylineSceneLayer& polyline, | |
662 const Scene2D& scene) ORTHANC_OVERRIDE | |
663 { | |
664 } | |
665 | |
666 virtual void RenderOtherLayers(MacroSceneLayer& macro, | |
667 const Scene2D& scene) ORTHANC_OVERRIDE | |
668 { | |
669 if (content_.get() != NULL) | |
670 { | |
671 std::unique_ptr<TextSceneLayer> layer(reinterpret_cast<TextSceneLayer*>(content_->Clone())); | |
672 | |
673 layer->SetColor(IsHover() ? GetHoverColor() : GetColor()); | |
674 | |
675 if (first_) | |
676 { | |
677 subLayer_ = macro.AddLayer(layer.release()); | |
678 first_ = false; | |
679 } | |
680 else | |
681 { | |
682 macro.UpdateLayer(subLayer_, layer.release()); | |
683 } | |
684 } | |
685 } | |
686 | |
687 virtual void MovePreview(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
688 { | |
689 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
690 } | |
691 | |
692 virtual void MoveDone(const ScenePoint2D& delta) ORTHANC_OVERRIDE | |
693 { | |
694 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); // No hit is possible | |
695 } | |
696 }; | |
697 | |
698 | |
699 class AnnotationsSceneLayer::EditPrimitiveTracker : public IFlexiblePointerTracker | |
700 { | |
701 private: | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
702 AnnotationsSceneLayer& that_; |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
703 GeometricPrimitive& primitive_; |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
704 ScenePoint2D sceneClick_; |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
705 AffineTransform2D canvasToScene_; |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
706 bool alive_; |
1804 | 707 |
708 public: | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
709 EditPrimitiveTracker(AnnotationsSceneLayer& that, |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
710 GeometricPrimitive& primitive, |
1804 | 711 const ScenePoint2D& sceneClick, |
712 const AffineTransform2D& canvasToScene) : | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
713 that_(that), |
1804 | 714 primitive_(primitive), |
715 sceneClick_(sceneClick), | |
716 canvasToScene_(canvasToScene), | |
717 alive_(true) | |
718 { | |
719 } | |
720 | |
721 virtual void PointerMove(const PointerEvent& event) ORTHANC_OVERRIDE | |
722 { | |
723 primitive_.MovePreview(event.GetMainPosition().Apply(canvasToScene_) - sceneClick_); | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
724 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 725 } |
726 | |
727 virtual void PointerUp(const PointerEvent& event) ORTHANC_OVERRIDE | |
728 { | |
729 primitive_.MoveDone(event.GetMainPosition().Apply(canvasToScene_) - sceneClick_); | |
730 alive_ = false; | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
731 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 732 } |
733 | |
734 virtual void PointerDown(const PointerEvent& event) ORTHANC_OVERRIDE | |
735 { | |
736 } | |
737 | |
738 virtual bool IsAlive() const ORTHANC_OVERRIDE | |
739 { | |
740 return alive_; | |
741 } | |
742 | |
743 virtual void Cancel() ORTHANC_OVERRIDE | |
744 { | |
745 primitive_.MoveDone(ScenePoint2D(0, 0)); | |
746 } | |
747 }; | |
748 | |
749 | |
750 class AnnotationsSceneLayer::SegmentAnnotation : public Annotation | |
751 { | |
752 private: | |
753 bool showLabel_; | |
754 Handle& handle1_; | |
755 Handle& handle2_; | |
756 Segment& segment_; | |
757 Text& label_; | |
758 | |
759 void UpdateLabel() | |
760 { | |
761 if (showLabel_) | |
762 { | |
763 TextSceneLayer content; | |
764 | |
765 double x1 = handle1_.GetCenter().GetX(); | |
766 double y1 = handle1_.GetCenter().GetY(); | |
767 double x2 = handle2_.GetCenter().GetX(); | |
768 double y2 = handle2_.GetCenter().GetY(); | |
769 | |
770 // Put the label to the right of the right-most handle | |
771 if (x1 < x2) | |
772 { | |
773 content.SetPosition(x2, y2); | |
774 } | |
775 else | |
776 { | |
777 content.SetPosition(x1, y1); | |
778 } | |
779 | |
780 content.SetAnchor(BitmapAnchor_CenterLeft); | |
781 content.SetBorder(10); | |
782 | |
783 double dx = x1 - x2; | |
784 double dy = y1 - y2; | |
785 char buf[32]; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
786 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
787 switch (GetUnits()) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
788 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
789 case Units_Millimeters: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
790 sprintf(buf, "%0.2f cm", sqrt(dx * dx + dy * dy) / 10.0); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
791 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
792 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
793 case Units_Pixels: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
794 sprintf(buf, "%0.1f px", sqrt(dx * dx + dy * dy)); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
795 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
796 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
797 default: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
798 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
799 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
800 |
1804 | 801 content.SetText(buf); |
802 | |
803 label_.SetContent(content); | |
804 } | |
805 } | |
806 | |
807 public: | |
808 SegmentAnnotation(AnnotationsSceneLayer& that, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
809 Units units, |
1804 | 810 bool showLabel, |
811 const ScenePoint2D& p1, | |
812 const ScenePoint2D& p2) : | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
813 Annotation(that, units), |
1804 | 814 showLabel_(showLabel), |
815 handle1_(AddTypedPrimitive<Handle>(new Handle(*this, p1))), | |
816 handle2_(AddTypedPrimitive<Handle>(new Handle(*this, p2))), | |
817 segment_(AddTypedPrimitive<Segment>(new Segment(*this, p1, p2))), | |
818 label_(AddTypedPrimitive<Text>(new Text(that, *this))) | |
819 { | |
1819
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
820 label_.SetColor(COLOR_TEXT); |
1804 | 821 UpdateLabel(); |
822 } | |
823 | |
824 Handle& GetHandle1() const | |
825 { | |
826 return handle1_; | |
827 } | |
828 | |
829 Handle& GetHandle2() const | |
830 { | |
831 return handle2_; | |
832 } | |
833 | |
834 virtual void SignalMove(GeometricPrimitive& primitive) ORTHANC_OVERRIDE | |
835 { | |
836 if (&primitive == &handle1_ || | |
837 &primitive == &handle2_) | |
838 { | |
839 segment_.SetPosition(handle1_.GetCenter(), handle2_.GetCenter()); | |
840 } | |
841 else if (&primitive == &segment_) | |
842 { | |
843 handle1_.SetCenter(segment_.GetPosition1()); | |
844 handle2_.SetCenter(segment_.GetPosition2()); | |
845 } | |
846 | |
847 UpdateLabel(); | |
848 } | |
849 | |
850 virtual void Serialize(Json::Value& target) ORTHANC_OVERRIDE | |
851 { | |
852 target = Json::objectValue; | |
853 target[KEY_TYPE] = VALUE_SEGMENT; | |
854 target[KEY_X1] = handle1_.GetCenter().GetX(); | |
855 target[KEY_Y1] = handle1_.GetCenter().GetY(); | |
856 target[KEY_X2] = handle2_.GetCenter().GetX(); | |
857 target[KEY_Y2] = handle2_.GetCenter().GetY(); | |
858 } | |
859 | |
860 static void Unserialize(AnnotationsSceneLayer& target, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
861 Units units, |
1804 | 862 const Json::Value& source) |
863 { | |
864 if (source.isMember(KEY_X1) && | |
865 source.isMember(KEY_Y1) && | |
866 source.isMember(KEY_X2) && | |
867 source.isMember(KEY_Y2) && | |
868 source[KEY_X1].isNumeric() && | |
869 source[KEY_Y1].isNumeric() && | |
870 source[KEY_X2].isNumeric() && | |
871 source[KEY_Y2].isNumeric()) | |
872 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
873 new SegmentAnnotation(target, units, true, |
1804 | 874 ScenePoint2D(source[KEY_X1].asDouble(), source[KEY_Y1].asDouble()), |
875 ScenePoint2D(source[KEY_X2].asDouble(), source[KEY_Y2].asDouble())); | |
876 } | |
877 else | |
878 { | |
879 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, "Cannot unserialize an segment annotation"); | |
880 } | |
881 } | |
882 }; | |
883 | |
884 | |
885 class AnnotationsSceneLayer::AngleAnnotation : public Annotation | |
886 { | |
887 private: | |
888 Handle& startHandle_; | |
889 Handle& middleHandle_; | |
890 Handle& endHandle_; | |
891 Segment& segment1_; | |
892 Segment& segment2_; | |
893 Arc& arc_; | |
894 Text& label_; | |
895 | |
896 void UpdateLabel() | |
897 { | |
898 TextSceneLayer content; | |
899 | |
900 const double x1 = startHandle_.GetCenter().GetX(); | |
901 const double x2 = middleHandle_.GetCenter().GetX(); | |
902 const double y2 = middleHandle_.GetCenter().GetY(); | |
903 const double x3 = endHandle_.GetCenter().GetX(); | |
904 | |
905 if (x2 < x1 && | |
906 x2 < x3) | |
907 { | |
908 content.SetAnchor(BitmapAnchor_CenterRight); | |
909 } | |
910 else | |
911 { | |
912 content.SetAnchor(BitmapAnchor_CenterLeft); | |
913 } | |
914 | |
915 content.SetPosition(x2, y2); | |
916 content.SetBorder(10); | |
917 | |
918 char buf[32]; | |
919 sprintf(buf, "%.01f%c%c", std::abs(arc_.GetAngle()) / PI * 180.0, | |
920 0xc2, 0xb0 /* two bytes corresponding to degree symbol in UTF-8 */); | |
921 content.SetText(buf); | |
922 | |
923 label_.SetContent(content); | |
924 } | |
925 | |
926 public: | |
927 AngleAnnotation(AnnotationsSceneLayer& that, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
928 Units units, |
1804 | 929 const ScenePoint2D& start, |
930 const ScenePoint2D& middle, | |
931 const ScenePoint2D& end) : | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
932 Annotation(that, units), |
1804 | 933 startHandle_(AddTypedPrimitive<Handle>(new Handle(*this, start))), |
934 middleHandle_(AddTypedPrimitive<Handle>(new Handle(*this, middle))), | |
935 endHandle_(AddTypedPrimitive<Handle>(new Handle(*this, end))), | |
936 segment1_(AddTypedPrimitive<Segment>(new Segment(*this, start, middle))), | |
937 segment2_(AddTypedPrimitive<Segment>(new Segment(*this, middle, end))), | |
938 arc_(AddTypedPrimitive<Arc>(new Arc(*this, start, middle, end))), | |
939 label_(AddTypedPrimitive<Text>(new Text(that, *this))) | |
940 { | |
1819
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
941 label_.SetColor(COLOR_TEXT); |
1804 | 942 UpdateLabel(); |
943 } | |
944 | |
945 Handle& GetEndHandle() const | |
946 { | |
947 return endHandle_; | |
948 } | |
949 | |
950 virtual void SignalMove(GeometricPrimitive& primitive) ORTHANC_OVERRIDE | |
951 { | |
952 if (&primitive == &startHandle_) | |
953 { | |
954 segment1_.SetPosition(startHandle_.GetCenter(), middleHandle_.GetCenter()); | |
955 arc_.SetStart(startHandle_.GetCenter()); | |
956 } | |
957 else if (&primitive == &middleHandle_) | |
958 { | |
959 segment1_.SetPosition(startHandle_.GetCenter(), middleHandle_.GetCenter()); | |
960 segment2_.SetPosition(middleHandle_.GetCenter(), endHandle_.GetCenter()); | |
961 arc_.SetMiddle(middleHandle_.GetCenter()); | |
962 } | |
963 else if (&primitive == &endHandle_) | |
964 { | |
965 segment2_.SetPosition(middleHandle_.GetCenter(), endHandle_.GetCenter()); | |
966 arc_.SetEnd(endHandle_.GetCenter()); | |
967 } | |
968 else if (&primitive == &segment1_) | |
969 { | |
970 startHandle_.SetCenter(segment1_.GetPosition1()); | |
971 middleHandle_.SetCenter(segment1_.GetPosition2()); | |
972 segment2_.SetPosition(segment1_.GetPosition2(), segment2_.GetPosition2()); | |
973 arc_.SetStart(segment1_.GetPosition1()); | |
974 arc_.SetMiddle(segment1_.GetPosition2()); | |
975 } | |
976 else if (&primitive == &segment2_) | |
977 { | |
978 middleHandle_.SetCenter(segment2_.GetPosition1()); | |
979 endHandle_.SetCenter(segment2_.GetPosition2()); | |
980 segment1_.SetPosition(segment1_.GetPosition1(), segment2_.GetPosition1()); | |
981 arc_.SetMiddle(segment2_.GetPosition1()); | |
982 arc_.SetEnd(segment2_.GetPosition2()); | |
983 } | |
984 | |
985 UpdateLabel(); | |
986 } | |
987 | |
988 virtual void Serialize(Json::Value& target) ORTHANC_OVERRIDE | |
989 { | |
990 target = Json::objectValue; | |
991 target[KEY_TYPE] = VALUE_ANGLE; | |
992 target[KEY_X1] = startHandle_.GetCenter().GetX(); | |
993 target[KEY_Y1] = startHandle_.GetCenter().GetY(); | |
994 target[KEY_X2] = middleHandle_.GetCenter().GetX(); | |
995 target[KEY_Y2] = middleHandle_.GetCenter().GetY(); | |
996 target[KEY_X3] = endHandle_.GetCenter().GetX(); | |
997 target[KEY_Y3] = endHandle_.GetCenter().GetY(); | |
998 } | |
999 | |
1000 static void Unserialize(AnnotationsSceneLayer& target, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1001 Units units, |
1804 | 1002 const Json::Value& source) |
1003 { | |
1004 if (source.isMember(KEY_X1) && | |
1005 source.isMember(KEY_Y1) && | |
1006 source.isMember(KEY_X2) && | |
1007 source.isMember(KEY_Y2) && | |
1008 source.isMember(KEY_X3) && | |
1009 source.isMember(KEY_Y3) && | |
1010 source[KEY_X1].isNumeric() && | |
1011 source[KEY_Y1].isNumeric() && | |
1012 source[KEY_X2].isNumeric() && | |
1013 source[KEY_Y2].isNumeric() && | |
1014 source[KEY_X3].isNumeric() && | |
1015 source[KEY_Y3].isNumeric()) | |
1016 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1017 new AngleAnnotation(target, units, |
1804 | 1018 ScenePoint2D(source[KEY_X1].asDouble(), source[KEY_Y1].asDouble()), |
1019 ScenePoint2D(source[KEY_X2].asDouble(), source[KEY_Y2].asDouble()), | |
1020 ScenePoint2D(source[KEY_X3].asDouble(), source[KEY_Y3].asDouble())); | |
1021 } | |
1022 else | |
1023 { | |
1024 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, "Cannot unserialize an angle annotation"); | |
1025 } | |
1026 } | |
1027 }; | |
1028 | |
1029 | |
1030 class AnnotationsSceneLayer::CircleAnnotation : public Annotation | |
1031 { | |
1032 private: | |
1033 Handle& handle1_; | |
1034 Handle& handle2_; | |
1035 Segment& segment_; | |
1036 Circle& circle_; | |
1037 Text& label_; | |
1038 | |
1039 void UpdateLabel() | |
1040 { | |
1041 TextSceneLayer content; | |
1042 | |
1043 double x1 = handle1_.GetCenter().GetX(); | |
1044 double y1 = handle1_.GetCenter().GetY(); | |
1045 double x2 = handle2_.GetCenter().GetX(); | |
1046 double y2 = handle2_.GetCenter().GetY(); | |
1047 | |
1048 // Put the label to the right of the right-most handle | |
1049 if (x1 < x2) | |
1050 { | |
1051 content.SetPosition(x2, y2); | |
1052 } | |
1053 else | |
1054 { | |
1055 content.SetPosition(x1, y1); | |
1056 } | |
1057 | |
1058 content.SetAnchor(BitmapAnchor_CenterLeft); | |
1059 content.SetBorder(10); | |
1060 | |
1061 double dx = x1 - x2; | |
1062 double dy = y1 - y2; | |
1063 double diameter = sqrt(dx * dx + dy * dy); // in millimeters | |
1064 | |
1065 double area = PI * diameter * diameter / 4.0; | |
1066 | |
1067 char buf[32]; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1068 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1069 switch (GetUnits()) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1070 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1071 case Units_Millimeters: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1072 sprintf(buf, "%0.2f cm\n%0.2f cm%c%c", |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1073 diameter / 10.0, |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1074 area / 100.0, |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1075 0xc2, 0xb2 /* two bytes corresponding to two power in UTF-8 */); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1076 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1077 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1078 case Units_Pixels: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1079 // Don't report area (pixel-times-pixel is a strange unit) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1080 sprintf(buf, "%0.1f px", diameter); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1081 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1082 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1083 default: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1084 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1085 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1086 |
1804 | 1087 content.SetText(buf); |
1088 | |
1089 label_.SetContent(content); | |
1090 } | |
1091 | |
1092 public: | |
1093 CircleAnnotation(AnnotationsSceneLayer& that, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1094 Units units, |
1804 | 1095 const ScenePoint2D& p1, |
1096 const ScenePoint2D& p2) : | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1097 Annotation(that, units), |
1804 | 1098 handle1_(AddTypedPrimitive<Handle>(new Handle(*this, p1))), |
1099 handle2_(AddTypedPrimitive<Handle>(new Handle(*this, p2))), | |
1100 segment_(AddTypedPrimitive<Segment>(new Segment(*this, p1, p2))), | |
1101 circle_(AddTypedPrimitive<Circle>(new Circle(*this, p1, p2))), | |
1102 label_(AddTypedPrimitive<Text>(new Text(that, *this))) | |
1103 { | |
1819
fe402c678d18
improved color theme for annotations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1817
diff
changeset
|
1104 label_.SetColor(COLOR_TEXT); |
1804 | 1105 UpdateLabel(); |
1106 } | |
1107 | |
1108 Handle& GetHandle2() const | |
1109 { | |
1110 return handle2_; | |
1111 } | |
1112 | |
1113 virtual void SignalMove(GeometricPrimitive& primitive) ORTHANC_OVERRIDE | |
1114 { | |
1115 if (&primitive == &handle1_ || | |
1116 &primitive == &handle2_) | |
1117 { | |
1118 segment_.SetPosition(handle1_.GetCenter(), handle2_.GetCenter()); | |
1119 circle_.SetPosition(handle1_.GetCenter(), handle2_.GetCenter()); | |
1120 } | |
1121 else if (&primitive == &segment_) | |
1122 { | |
1123 handle1_.SetCenter(segment_.GetPosition1()); | |
1124 handle2_.SetCenter(segment_.GetPosition2()); | |
1125 circle_.SetPosition(segment_.GetPosition1(), segment_.GetPosition2()); | |
1126 } | |
1127 | |
1128 UpdateLabel(); | |
1129 } | |
1130 | |
1131 virtual void Serialize(Json::Value& target) ORTHANC_OVERRIDE | |
1132 { | |
1133 target = Json::objectValue; | |
1134 target[KEY_TYPE] = VALUE_CIRCLE; | |
1135 target[KEY_X1] = handle1_.GetCenter().GetX(); | |
1136 target[KEY_Y1] = handle1_.GetCenter().GetY(); | |
1137 target[KEY_X2] = handle2_.GetCenter().GetX(); | |
1138 target[KEY_Y2] = handle2_.GetCenter().GetY(); | |
1139 } | |
1140 | |
1141 static void Unserialize(AnnotationsSceneLayer& target, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1142 Units units, |
1804 | 1143 const Json::Value& source) |
1144 { | |
1145 if (source.isMember(KEY_X1) && | |
1146 source.isMember(KEY_Y1) && | |
1147 source.isMember(KEY_X2) && | |
1148 source.isMember(KEY_Y2) && | |
1149 source[KEY_X1].isNumeric() && | |
1150 source[KEY_Y1].isNumeric() && | |
1151 source[KEY_X2].isNumeric() && | |
1152 source[KEY_Y2].isNumeric()) | |
1153 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1154 new CircleAnnotation(target, units, |
1804 | 1155 ScenePoint2D(source[KEY_X1].asDouble(), source[KEY_Y1].asDouble()), |
1156 ScenePoint2D(source[KEY_X2].asDouble(), source[KEY_Y2].asDouble())); | |
1157 } | |
1158 else | |
1159 { | |
1160 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, "Cannot unserialize an circle annotation"); | |
1161 } | |
1162 } | |
1163 }; | |
1164 | |
1165 | |
1166 class AnnotationsSceneLayer::CreateSegmentOrCircleTracker : public IFlexiblePointerTracker | |
1167 { | |
1168 private: | |
1169 AnnotationsSceneLayer& that_; | |
1170 Annotation* annotation_; | |
1171 AffineTransform2D canvasToScene_; | |
1172 Handle* handle2_; | |
1173 | |
1174 public: | |
1175 CreateSegmentOrCircleTracker(AnnotationsSceneLayer& that, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1176 Units units, |
1804 | 1177 bool isCircle, |
1178 const ScenePoint2D& sceneClick, | |
1179 const AffineTransform2D& canvasToScene) : | |
1180 that_(that), | |
1181 annotation_(NULL), | |
1182 canvasToScene_(canvasToScene), | |
1183 handle2_(NULL) | |
1184 { | |
1185 if (isCircle) | |
1186 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1187 annotation_ = new CircleAnnotation(that, units, sceneClick, sceneClick); |
1804 | 1188 handle2_ = &dynamic_cast<CircleAnnotation*>(annotation_)->GetHandle2(); |
1189 } | |
1190 else | |
1191 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1192 annotation_ = new SegmentAnnotation(that, units, true /* show label */, sceneClick, sceneClick); |
1804 | 1193 handle2_ = &dynamic_cast<SegmentAnnotation*>(annotation_)->GetHandle2(); |
1194 } | |
1195 | |
1196 assert(annotation_ != NULL && | |
1197 handle2_ != NULL); | |
1198 } | |
1199 | |
1200 virtual void PointerMove(const PointerEvent& event) ORTHANC_OVERRIDE | |
1201 { | |
1202 if (annotation_ != NULL) | |
1203 { | |
1204 assert(handle2_ != NULL); | |
1205 handle2_->SetCenter(event.GetMainPosition().Apply(canvasToScene_)); | |
1206 annotation_->SignalMove(*handle2_); | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1207 |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1208 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 1209 } |
1210 } | |
1211 | |
1212 virtual void PointerUp(const PointerEvent& event) ORTHANC_OVERRIDE | |
1213 { | |
1214 annotation_ = NULL; // IsAlive() becomes false | |
1215 | |
1216 that_.BroadcastMessage(AnnotationAddedMessage(that_)); | |
1217 } | |
1218 | |
1219 virtual void PointerDown(const PointerEvent& event) ORTHANC_OVERRIDE | |
1220 { | |
1221 } | |
1222 | |
1223 virtual bool IsAlive() const ORTHANC_OVERRIDE | |
1224 { | |
1225 return (annotation_ != NULL); | |
1226 } | |
1227 | |
1228 virtual void Cancel() ORTHANC_OVERRIDE | |
1229 { | |
1230 if (annotation_ != NULL) | |
1231 { | |
1232 that_.DeleteAnnotation(annotation_); | |
1233 annotation_ = NULL; | |
1234 } | |
1235 } | |
1236 }; | |
1237 | |
1238 | |
1239 class AnnotationsSceneLayer::CreateAngleTracker : public IFlexiblePointerTracker | |
1240 { | |
1241 private: | |
1242 AnnotationsSceneLayer& that_; | |
1243 SegmentAnnotation* segment_; | |
1244 AngleAnnotation* angle_; | |
1245 AffineTransform2D canvasToScene_; | |
1246 | |
1247 public: | |
1248 CreateAngleTracker(AnnotationsSceneLayer& that, | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1249 Units units, |
1804 | 1250 const ScenePoint2D& sceneClick, |
1251 const AffineTransform2D& canvasToScene) : | |
1252 that_(that), | |
1253 segment_(NULL), | |
1254 angle_(NULL), | |
1255 canvasToScene_(canvasToScene) | |
1256 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1257 segment_ = new SegmentAnnotation(that, units, false /* no length label */, sceneClick, sceneClick); |
1804 | 1258 } |
1259 | |
1260 virtual void PointerMove(const PointerEvent& event) ORTHANC_OVERRIDE | |
1261 { | |
1262 if (segment_ != NULL) | |
1263 { | |
1264 segment_->GetHandle2().SetCenter(event.GetMainPosition().Apply(canvasToScene_)); | |
1265 segment_->SignalMove(segment_->GetHandle2()); | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1266 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 1267 } |
1268 | |
1269 if (angle_ != NULL) | |
1270 { | |
1271 angle_->GetEndHandle().SetCenter(event.GetMainPosition().Apply(canvasToScene_)); | |
1272 angle_->SignalMove(angle_->GetEndHandle()); | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1273 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 1274 } |
1275 } | |
1276 | |
1277 virtual void PointerUp(const PointerEvent& event) ORTHANC_OVERRIDE | |
1278 { | |
1279 if (segment_ != NULL) | |
1280 { | |
1281 // End of first step: The first segment is available, now create the angle | |
1282 | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1283 angle_ = new AngleAnnotation(that_, segment_->GetUnits(), segment_->GetHandle1().GetCenter(), |
1804 | 1284 segment_->GetHandle2().GetCenter(), |
1285 segment_->GetHandle2().GetCenter()); | |
1286 | |
1287 that_.DeleteAnnotation(segment_); | |
1288 segment_ = NULL; | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1289 |
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1290 that_.BroadcastMessage(AnnotationChangedMessage(that_)); |
1804 | 1291 } |
1292 else | |
1293 { | |
1294 angle_ = NULL; // IsAlive() becomes false | |
1295 | |
1296 that_.BroadcastMessage(AnnotationAddedMessage(that_)); | |
1297 } | |
1298 } | |
1299 | |
1300 virtual void PointerDown(const PointerEvent& event) ORTHANC_OVERRIDE | |
1301 { | |
1302 } | |
1303 | |
1304 virtual bool IsAlive() const ORTHANC_OVERRIDE | |
1305 { | |
1306 return (segment_ != NULL || | |
1307 angle_ != NULL); | |
1308 } | |
1309 | |
1310 virtual void Cancel() ORTHANC_OVERRIDE | |
1311 { | |
1312 if (segment_ != NULL) | |
1313 { | |
1314 that_.DeleteAnnotation(segment_); | |
1315 segment_ = NULL; | |
1316 } | |
1317 | |
1318 if (angle_ != NULL) | |
1319 { | |
1320 that_.DeleteAnnotation(angle_); | |
1321 angle_ = NULL; | |
1322 } | |
1323 } | |
1324 }; | |
1325 | |
1326 | |
1327 // Dummy tracker that is only used for deletion, in order to warn | |
1328 // the caller that the mouse action was taken into consideration | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1329 class AnnotationsSceneLayer::RemoveTracker : public IFlexiblePointerTracker |
1804 | 1330 { |
1331 public: | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1332 RemoveTracker() |
1804 | 1333 { |
1334 } | |
1335 | |
1336 virtual void PointerMove(const PointerEvent& event) ORTHANC_OVERRIDE | |
1337 { | |
1338 } | |
1339 | |
1340 virtual void PointerUp(const PointerEvent& event) ORTHANC_OVERRIDE | |
1341 { | |
1342 } | |
1343 | |
1344 virtual void PointerDown(const PointerEvent& event) ORTHANC_OVERRIDE | |
1345 { | |
1346 } | |
1347 | |
1348 virtual bool IsAlive() const ORTHANC_OVERRIDE | |
1349 { | |
1350 return false; | |
1351 } | |
1352 | |
1353 virtual void Cancel() ORTHANC_OVERRIDE | |
1354 { | |
1355 } | |
1356 }; | |
1357 | |
1358 | |
1359 void AnnotationsSceneLayer::AddAnnotation(Annotation* annotation) | |
1360 { | |
1361 assert(annotation != NULL); | |
1362 assert(annotations_.find(annotation) == annotations_.end()); | |
1363 annotations_.insert(annotation); | |
1364 } | |
1365 | |
1366 | |
1367 void AnnotationsSceneLayer::DeleteAnnotation(Annotation* annotation) | |
1368 { | |
1369 if (annotation != NULL) | |
1370 { | |
1371 assert(annotations_.find(annotation) != annotations_.end()); | |
1372 annotations_.erase(annotation); | |
1373 delete annotation; | |
1374 } | |
1375 } | |
1376 | |
1377 | |
1378 void AnnotationsSceneLayer::DeletePrimitive(GeometricPrimitive* primitive) | |
1379 { | |
1380 if (primitive != NULL) | |
1381 { | |
1382 assert(primitives_.find(primitive) != primitives_.end()); | |
1383 primitives_.erase(primitive); | |
1384 delete primitive; | |
1385 } | |
1386 } | |
1387 | |
1388 | |
1389 void AnnotationsSceneLayer::TagSubLayerToRemove(size_t subLayerIndex) | |
1390 { | |
1391 assert(subLayersToRemove_.find(subLayerIndex) == subLayersToRemove_.end()); | |
1392 subLayersToRemove_.insert(subLayerIndex); | |
1393 } | |
1394 | |
1395 | |
1396 AnnotationsSceneLayer::AnnotationsSceneLayer(size_t macroLayerIndex) : | |
1397 activeTool_(Tool_Edit), | |
1398 macroLayerIndex_(macroLayerIndex), | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1399 polylineSubLayer_(0), // dummy initialization |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1400 units_(Units_Pixels) |
1804 | 1401 { |
1402 } | |
1403 | |
1404 | |
1405 void AnnotationsSceneLayer::Clear() | |
1406 { | |
1407 for (Annotations::iterator it = annotations_.begin(); it != annotations_.end(); ++it) | |
1408 { | |
1409 assert(*it != NULL); | |
1410 delete *it; | |
1411 } | |
1412 | |
1413 annotations_.clear(); | |
1812
db341679dc9f
ViewerViewport::StoneAnnotationsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1809
diff
changeset
|
1414 |
db341679dc9f
ViewerViewport::StoneAnnotationsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1809
diff
changeset
|
1415 ClearHover(); |
1804 | 1416 } |
1417 | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1418 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1419 void AnnotationsSceneLayer::SetUnits(Units units) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1420 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1421 if (units_ != units) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1422 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1423 Clear(); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1424 units_ = units; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1425 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1426 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1427 |
1804 | 1428 |
1806
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1429 void AnnotationsSceneLayer::AddSegmentAnnotation(const ScenePoint2D& p1, |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1430 const ScenePoint2D& p2) |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1431 { |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1432 annotations_.insert(new SegmentAnnotation(*this, units_, true /* show label */, p1, p2)); |
1806
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1433 } |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1434 |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1435 |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1436 void AnnotationsSceneLayer::AddCircleAnnotation(const ScenePoint2D& p1, |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1437 const ScenePoint2D& p2) |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1438 { |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1439 annotations_.insert(new CircleAnnotation(*this, units_, p1, p2)); |
1806
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1440 } |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1441 |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1442 |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1443 void AnnotationsSceneLayer::AddAngleAnnotation(const ScenePoint2D& p1, |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1444 const ScenePoint2D& p2, |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1445 const ScenePoint2D& p3) |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1446 { |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1447 annotations_.insert(new AngleAnnotation(*this, units_, p1, p2, p3)); |
1806
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1448 } |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1449 |
0840a25c6d41
removed hard-coded test in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1804
diff
changeset
|
1450 |
1804 | 1451 void AnnotationsSceneLayer::Render(Scene2D& scene) |
1452 { | |
1453 MacroSceneLayer* macro = NULL; | |
1454 | |
1455 if (scene.HasLayer(macroLayerIndex_)) | |
1456 { | |
1457 macro = &dynamic_cast<MacroSceneLayer&>(scene.GetLayer(macroLayerIndex_)); | |
1458 } | |
1459 else | |
1460 { | |
1461 macro = &dynamic_cast<MacroSceneLayer&>(scene.SetLayer(macroLayerIndex_, new MacroSceneLayer)); | |
1462 polylineSubLayer_ = macro->AddLayer(new PolylineSceneLayer); | |
1463 } | |
1464 | |
1465 for (SubLayers::const_iterator it = subLayersToRemove_.begin(); it != subLayersToRemove_.end(); ++it) | |
1466 { | |
1467 assert(macro->HasLayer(*it)); | |
1468 macro->DeleteLayer(*it); | |
1469 } | |
1470 | |
1471 subLayersToRemove_.clear(); | |
1472 | |
1473 std::unique_ptr<PolylineSceneLayer> polyline(new PolylineSceneLayer); | |
1474 | |
1475 for (GeometricPrimitives::iterator it = primitives_.begin(); it != primitives_.end(); ++it) | |
1476 { | |
1477 assert(*it != NULL); | |
1478 GeometricPrimitive& primitive = **it; | |
1479 | |
1480 primitive.RenderPolylineLayer(*polyline, scene); | |
1481 | |
1482 if (primitive.IsModified()) | |
1483 { | |
1484 primitive.RenderOtherLayers(*macro, scene); | |
1485 primitive.SetModified(false); | |
1486 } | |
1487 } | |
1488 | |
1489 macro->UpdateLayer(polylineSubLayer_, polyline.release()); | |
1490 } | |
1491 | |
1492 | |
1493 bool AnnotationsSceneLayer::ClearHover() | |
1494 { | |
1495 bool needsRefresh = false; | |
1496 | |
1497 for (GeometricPrimitives::iterator it = primitives_.begin(); it != primitives_.end(); ++it) | |
1498 { | |
1499 assert(*it != NULL); | |
1500 if ((*it)->IsHover()) | |
1501 { | |
1502 (*it)->SetHover(false); | |
1503 needsRefresh = true; | |
1504 } | |
1505 } | |
1506 | |
1507 return needsRefresh; | |
1508 } | |
1509 | |
1510 | |
1511 bool AnnotationsSceneLayer::SetMouseHover(const ScenePoint2D& p, | |
1512 const Scene2D& scene) | |
1513 { | |
1514 if (activeTool_ == Tool_None) | |
1515 { | |
1516 return ClearHover(); | |
1517 } | |
1518 else | |
1519 { | |
1520 bool needsRefresh = false; | |
1521 | |
1522 const ScenePoint2D s = p.Apply(scene.GetCanvasToSceneTransform()); | |
1523 | |
1524 for (GeometricPrimitives::iterator it = primitives_.begin(); it != primitives_.end(); ++it) | |
1525 { | |
1526 assert(*it != NULL); | |
1527 bool hover = (*it)->IsHit(s, scene); | |
1528 | |
1529 if ((*it)->IsHover() != hover) | |
1530 { | |
1531 needsRefresh = true; | |
1532 } | |
1533 | |
1534 (*it)->SetHover(hover); | |
1535 } | |
1536 | |
1537 return needsRefresh; | |
1538 } | |
1539 } | |
1540 | |
1541 | |
1542 IFlexiblePointerTracker* AnnotationsSceneLayer::CreateTracker(const ScenePoint2D& p, | |
1543 const Scene2D& scene) | |
1544 { | |
1545 if (activeTool_ == Tool_None) | |
1546 { | |
1547 return NULL; | |
1548 } | |
1549 else | |
1550 { | |
1551 const ScenePoint2D s = p.Apply(scene.GetCanvasToSceneTransform()); | |
1552 | |
1553 GeometricPrimitive* bestHit = NULL; | |
1554 | |
1555 for (GeometricPrimitives::iterator it = primitives_.begin(); it != primitives_.end(); ++it) | |
1556 { | |
1557 assert(*it != NULL); | |
1558 if ((*it)->IsHit(s, scene)) | |
1559 { | |
1560 if (bestHit == NULL || | |
1561 bestHit->GetDepth() > (*it)->GetDepth()) | |
1562 { | |
1563 bestHit = *it; | |
1564 } | |
1565 } | |
1566 } | |
1567 | |
1568 if (bestHit != NULL) | |
1569 { | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1570 if (activeTool_ == Tool_Remove) |
1804 | 1571 { |
1572 DeleteAnnotation(&bestHit->GetParentAnnotation()); | |
1573 BroadcastMessage(AnnotationRemovedMessage(*this)); | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1574 return new RemoveTracker; |
1804 | 1575 } |
1576 else | |
1577 { | |
1809
79a5838739a6
starting the integration of AnnotationsSceneLayer into Stone Web viewer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1806
diff
changeset
|
1578 return new EditPrimitiveTracker(*this, *bestHit, s, scene.GetCanvasToSceneTransform()); |
1804 | 1579 } |
1580 } | |
1581 else | |
1582 { | |
1583 switch (activeTool_) | |
1584 { | |
1585 case Tool_Segment: | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1586 return new CreateSegmentOrCircleTracker(*this, units_, false /* segment */, s, scene.GetCanvasToSceneTransform()); |
1804 | 1587 |
1588 case Tool_Circle: | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1589 return new CreateSegmentOrCircleTracker(*this, units_, true /* circle */, s, scene.GetCanvasToSceneTransform()); |
1804 | 1590 |
1591 case Tool_Angle: | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1592 return new CreateAngleTracker(*this, units_, s, scene.GetCanvasToSceneTransform()); |
1804 | 1593 |
1594 default: | |
1595 return NULL; | |
1596 } | |
1597 } | |
1598 } | |
1599 } | |
1600 | |
1601 | |
1602 void AnnotationsSceneLayer::Serialize(Json::Value& target) const | |
1603 { | |
1604 Json::Value annotations = Json::arrayValue; | |
1605 | |
1606 for (Annotations::const_iterator it = annotations_.begin(); it != annotations_.end(); ++it) | |
1607 { | |
1608 assert(*it != NULL); | |
1609 | |
1610 Json::Value item; | |
1611 (*it)->Serialize(item); | |
1612 annotations.append(item); | |
1613 } | |
1614 | |
1615 target = Json::objectValue; | |
1616 target[KEY_ANNOTATIONS] = annotations; | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1617 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1618 switch (units_) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1619 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1620 case Units_Millimeters: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1621 target[KEY_UNITS] = VALUE_MILLIMETERS; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1622 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1623 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1624 case Units_Pixels: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1625 target[KEY_UNITS] = VALUE_PIXELS; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1626 break; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1627 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1628 default: |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1629 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1630 } |
1804 | 1631 } |
1632 | |
1633 | |
1634 void AnnotationsSceneLayer::Unserialize(const Json::Value& serialized) | |
1635 { | |
1636 Clear(); | |
1637 | |
1638 if (serialized.type() != Json::objectValue || | |
1639 !serialized.isMember(KEY_ANNOTATIONS) || | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1640 !serialized.isMember(KEY_UNITS) || |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1641 serialized[KEY_ANNOTATIONS].type() != Json::arrayValue || |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1642 serialized[KEY_UNITS].type() != Json::stringValue) |
1804 | 1643 { |
1644 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, "Cannot unserialize a set of annotations"); | |
1645 } | |
1646 | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1647 const std::string& u = serialized[KEY_UNITS].asString(); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1648 |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1649 if (u == VALUE_MILLIMETERS) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1650 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1651 units_ = Units_Millimeters; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1652 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1653 else if (u == VALUE_PIXELS) |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1654 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1655 units_ = Units_Pixels; |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1656 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1657 else |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1658 { |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1659 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, "Unknown units: " + u); |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1660 } |
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1661 |
1804 | 1662 const Json::Value& annotations = serialized[KEY_ANNOTATIONS]; |
1663 | |
1664 for (Json::Value::ArrayIndex i = 0; i < annotations.size(); i++) | |
1665 { | |
1666 if (annotations[i].type() != Json::objectValue || | |
1667 !annotations[i].isMember(KEY_TYPE) || | |
1668 annotations[i][KEY_TYPE].type() != Json::stringValue) | |
1669 { | |
1670 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
1671 } | |
1672 | |
1673 const std::string& type = annotations[i][KEY_TYPE].asString(); | |
1674 | |
1675 if (type == VALUE_ANGLE) | |
1676 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1677 AngleAnnotation::Unserialize(*this, units_, annotations[i]); |
1804 | 1678 } |
1679 else if (type == VALUE_CIRCLE) | |
1680 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1681 CircleAnnotation::Unserialize(*this, units_, annotations[i]); |
1804 | 1682 } |
1683 else if (type == VALUE_SEGMENT) | |
1684 { | |
1821
36430d73e36c
introducing measure units in AnnotationsSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1819
diff
changeset
|
1685 SegmentAnnotation::Unserialize(*this, units_, annotations[i]); |
1804 | 1686 } |
1687 else | |
1688 { | |
1689 LOG(ERROR) << "Cannot unserialize unknown type of annotation: " << type; | |
1690 } | |
1691 } | |
1692 } | |
1693 } |