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