Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Toolbox/DebugDrawing2D.cpp @ 2156:340bde744884
added DebugDrawing2D and GeometryToolbox::IntersectLineAndSegment()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 27 Sep 2024 22:14:36 +0200 |
parents | |
children | f14260c15151 |
comparison
equal
deleted
inserted
replaced
2155:71d6ad7036b7 | 2156:340bde744884 |
---|---|
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-2023 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #include "DebugDrawing2D.h" | |
25 | |
26 | |
27 namespace OrthancStone | |
28 { | |
29 class DebugDrawing2D::Segment | |
30 { | |
31 private: | |
32 double x1_; | |
33 double y1_; | |
34 double x2_; | |
35 double y2_; | |
36 std::string color_; | |
37 bool arrow_; | |
38 | |
39 public: | |
40 Segment(double x1, | |
41 double y1, | |
42 double x2, | |
43 double y2, | |
44 const std::string& color, | |
45 bool arrow) : | |
46 x1_(x1), | |
47 y1_(y1), | |
48 x2_(x2), | |
49 y2_(y2), | |
50 color_(color), | |
51 arrow_(arrow) | |
52 { | |
53 } | |
54 | |
55 double GetX1() const | |
56 { | |
57 return x1_; | |
58 } | |
59 | |
60 double GetY1() const | |
61 { | |
62 return y1_; | |
63 } | |
64 | |
65 double GetX2() const | |
66 { | |
67 return x2_; | |
68 } | |
69 | |
70 double GetY2() const | |
71 { | |
72 return y2_; | |
73 } | |
74 | |
75 const std::string& GetColor() const | |
76 { | |
77 return color_; | |
78 } | |
79 | |
80 bool IsArrow() const | |
81 { | |
82 return arrow_; | |
83 } | |
84 }; | |
85 | |
86 | |
87 void DebugDrawing2D::AddSegment(double x1, | |
88 double y1, | |
89 double x2, | |
90 double y2, | |
91 const std::string& color, | |
92 bool arrow, | |
93 bool addToExtent) | |
94 { | |
95 if (addToExtent) | |
96 { | |
97 extent_.AddPoint(x1, y1); | |
98 extent_.AddPoint(x2, y2); | |
99 } | |
100 | |
101 segments_.push_back(Segment(x1, y1, x2, y2, color, arrow)); | |
102 } | |
103 | |
104 | |
105 void DebugDrawing2D::SaveSvg(const std::string& path) | |
106 { | |
107 // Size in pixels | |
108 float ww, hh; | |
109 if (extent_.IsEmpty()) | |
110 { | |
111 ww = 2048.0f; | |
112 hh = 2048.0f; | |
113 } | |
114 else if (extent_.GetWidth() > extent_.GetHeight()) | |
115 { | |
116 ww = 2048.0f; | |
117 hh = ww * extent_.GetHeight() / extent_.GetWidth(); | |
118 } | |
119 else | |
120 { | |
121 hh = 2048.0f; | |
122 ww = hh * extent_.GetWidth() / extent_.GetHeight(); | |
123 } | |
124 | |
125 FILE* fp = fopen(path.c_str(), "w"); | |
126 fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"); | |
127 fprintf(fp, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"); | |
128 fprintf(fp, "<svg width=\"%f\" height=\"%f\" viewBox=\"0 0 %f %f\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", ww, hh, extent_.GetWidth(), extent_.GetHeight()); | |
129 | |
130 // http://thenewcode.com/1068/Making-Arrows-in-SVG | |
131 fprintf(fp, "<defs>\n"); | |
132 fprintf(fp, "<marker id=\"arrowhead\" markerWidth=\"2\" markerHeight=\"3\" \n"); | |
133 fprintf(fp, "refX=\"2\" refY=\"1.5\" orient=\"auto\">\n"); | |
134 fprintf(fp, "<polygon points=\"0 0, 2 1.5, 0 3\" />\n"); | |
135 fprintf(fp, "</marker>\n"); | |
136 fprintf(fp, "</defs>\n"); | |
137 | |
138 fprintf(fp, "<rect fill=\"#fff\" stroke=\"#000\" x=\"0\" y=\"0\" width=\"%f\" height=\"%f\"/>\n", extent_.GetWidth(), extent_.GetHeight()); | |
139 | |
140 for (std::list<Segment>::const_iterator it = segments_.begin(); it != segments_.end(); ++it) | |
141 { | |
142 float strokeWidth = 0.1; | |
143 | |
144 std::string s; | |
145 if (it->IsArrow()) | |
146 { | |
147 s = "marker-end=\"url(#arrowhead)\""; | |
148 } | |
149 | |
150 fprintf(fp, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"%s\" stroke-width=\"%f\" %s/>\n", | |
151 it->GetX1() - extent_.GetX1(), it->GetY1() - extent_.GetY1(), | |
152 it->GetX2() - extent_.GetX1(), it->GetY2() - extent_.GetY1(), | |
153 it->GetColor().c_str(), strokeWidth, s.c_str()); | |
154 } | |
155 | |
156 fprintf(fp, "</svg>\n"); | |
157 | |
158 fclose(fp); | |
159 } | |
160 } |