Mercurial > hg > orthanc-stone
comparison Framework/Scene2DViewport/MeasureToolsToolbox.cpp @ 901:240359ab1651
Added Dot + magnitude + couple helpers in ScenePoint2D + Added ability to target several sets of 5-layer text packs stored in LayerHolder
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 17 Jul 2019 09:39:51 +0200 |
parents | 0c5201499af8 |
children | 52b1c6ff10c5 |
comparison
equal
deleted
inserted
replaced
899:a8e3d686b43e | 901:240359ab1651 |
---|---|
33 double g_pi = boost::math::constants::pi<double>(); | 33 double g_pi = boost::math::constants::pi<double>(); |
34 } | 34 } |
35 | 35 |
36 namespace OrthancStone | 36 namespace OrthancStone |
37 { | 37 { |
38 void GetPositionOnBisectingLine( | 38 void GetPositionOnBisectingLine( |
39 ScenePoint2D& result | 39 ScenePoint2D& result |
40 , const ScenePoint2D& p1 | 40 , const ScenePoint2D& p1 |
41 , const ScenePoint2D& c | 41 , const ScenePoint2D& c |
42 , const ScenePoint2D& p2 | 42 , const ScenePoint2D& p2 |
43 , const double d) | 43 , const double d) |
44 { | 44 { |
45 // TODO: fix correct half-plane | 45 // TODO: fix correct half-plane |
46 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); | 46 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); |
47 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); | 47 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); |
48 double angle = 0.5 * (p1cAngle + p2cAngle); | 48 double angle = 0.5 * (p1cAngle + p2cAngle); |
49 double unitVectorX = cos(angle); | 49 double unitVectorX = cos(angle); |
50 double unitVectorY = sin(angle); | 50 double unitVectorY = sin(angle); |
51 double posX = c.GetX() + d * unitVectorX; | 51 double posX = c.GetX() + d * unitVectorX; |
52 double posY = c.GetX() + d * unitVectorY; | 52 double posY = c.GetX() + d * unitVectorY; |
53 result = ScenePoint2D(posX, posY); | 53 result = ScenePoint2D(posX, posY); |
54 } | 54 } |
55 | 55 |
56 double RadiansToDegrees(double angleRad) | 56 double RadiansToDegrees(double angleRad) |
57 { | 57 { |
58 static const double factor = 180.0 / g_pi; | 58 static const double factor = 180.0 / g_pi; |
59 return angleRad * factor; | 59 return angleRad * factor; |
60 } | 60 } |
61 | 61 |
62 void AddSquare(PolylineSceneLayer::Chain& chain, | 62 void AddSquare(PolylineSceneLayer::Chain& chain, |
63 const Scene2D& scene, | 63 const Scene2D& scene, |
64 const ScenePoint2D& centerS, | 64 const ScenePoint2D& centerS, |
65 const double& sideLengthS) | 65 const double& sideLengthS) |
66 { | 66 { |
67 // get the scaling factor | 67 // get the scaling factor |
68 const double sceneToCanvas = | 68 const double sceneToCanvas = |
69 scene.GetSceneToCanvasTransform().ComputeZoom(); | 69 scene.GetSceneToCanvasTransform().ComputeZoom(); |
70 | 70 |
71 chain.clear(); | 71 chain.clear(); |
72 chain.reserve(4); | 72 chain.reserve(4); |
73 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); | 73 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); |
91 chain.push_back(startRB); | 91 chain.push_back(startRB); |
92 chain.push_back(startLB); | 92 chain.push_back(startLB); |
93 } | 93 } |
94 #if 0 | 94 #if 0 |
95 void AddArc( | 95 void AddArc( |
96 PolylineSceneLayer::Chain& chain | 96 PolylineSceneLayer::Chain & chain |
97 , const Scene2D& scene | 97 , const Scene2D & scene |
98 , const ScenePoint2D & p1 | |
99 , const ScenePoint2D & c | |
100 , const ScenePoint2D & p2 | |
101 , const double& radiusS | |
102 , const bool clockwise | |
103 , const int subdivisionsCount) | |
104 { | |
105 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); | |
106 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); | |
107 AddArc( | |
108 chain, scene, c, radiusS, p1cAngle, p2cAngle, | |
109 clockwise, subdivisionsCount); | |
110 } | |
111 #endif | |
112 | |
113 void AddShortestArc( | |
114 PolylineSceneLayer::Chain& chain | |
98 , const ScenePoint2D& p1 | 115 , const ScenePoint2D& p1 |
99 , const ScenePoint2D& c | 116 , const ScenePoint2D& c |
100 , const ScenePoint2D& p2 | 117 , const ScenePoint2D& p2 |
101 , const double& radiusS | 118 , const double& radiusS |
102 , const bool clockwise | |
103 , const int subdivisionsCount) | |
104 { | |
105 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); | |
106 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); | |
107 AddArc( | |
108 chain, scene, c, radiusS, p1cAngle, p2cAngle, | |
109 clockwise, subdivisionsCount); | |
110 } | |
111 #endif | |
112 | |
113 void AddShortestArc( | |
114 PolylineSceneLayer::Chain& chain | |
115 , const ScenePoint2D& p1 | |
116 , const ScenePoint2D& c | |
117 , const ScenePoint2D& p2 | |
118 , const double& radiusS | |
119 , const int subdivisionsCount) | 119 , const int subdivisionsCount) |
120 { | 120 { |
121 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); | 121 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); |
122 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); | 122 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); |
123 AddShortestArc( | 123 AddShortestArc( |
124 chain, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount); | 124 chain, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount); |
125 } | 125 } |
126 | 126 |
127 void AddShortestArc( | 127 void AddShortestArc( |
128 PolylineSceneLayer::Chain& chain | 128 PolylineSceneLayer::Chain& chain |
129 , const ScenePoint2D& centerS | 129 , const ScenePoint2D& centerS |
130 , const double& radiusS | 130 , const double& radiusS |
131 , const double startAngleRad | 131 , const double startAngleRad |
132 , const double endAngleRad | 132 , const double endAngleRad |
133 , const int subdivisionsCount) | 133 , const int subdivisionsCount) |
134 { | 134 { |
135 // this gives a signed difference between angle which | 135 // this gives a signed difference between angle which |
136 // is the smallest difference (in magnitude) between | 136 // is the smallest difference (in magnitude) between |
137 // the angles | 137 // the angles |
138 double delta = NormalizeAngle(endAngleRad-startAngleRad); | 138 double delta = NormalizeAngle(endAngleRad - startAngleRad); |
139 | 139 |
140 chain.clear(); | 140 chain.clear(); |
141 chain.reserve(subdivisionsCount + 1); | 141 chain.reserve(subdivisionsCount + 1); |
142 | 142 |
143 double angleIncr = delta/static_cast<double>(subdivisionsCount); | 143 double angleIncr = delta / static_cast<double>(subdivisionsCount); |
144 | 144 |
145 double theta = startAngleRad; | 145 double theta = startAngleRad; |
146 for (int i = 0; i < subdivisionsCount + 1; ++i) | 146 for (int i = 0; i < subdivisionsCount + 1; ++i) |
147 { | 147 { |
148 double offsetX = radiusS * cos(theta); | 148 double offsetX = radiusS * cos(theta); |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 #if 0 | 157 #if 0 |
158 void AddArc( | 158 void AddArc( |
159 PolylineSceneLayer::Chain& chain | 159 PolylineSceneLayer::Chain & chain |
160 , const Scene2D& scene | 160 , const Scene2D & scene |
161 , const ScenePoint2D& centerS | 161 , const ScenePoint2D & centerS |
162 , const double& radiusS | 162 , const double& radiusS |
163 , const double startAngleRad | 163 , const double startAngleRad |
164 , const double endAngleRad | 164 , const double endAngleRad |
165 , const bool clockwise | 165 , const bool clockwise |
166 , const int subdivisionsCount) | 166 , const int subdivisionsCount) |
167 { | 167 { |
202 } | 202 } |
203 #endif | 203 #endif |
204 | 204 |
205 void AddCircle(PolylineSceneLayer::Chain& chain, | 205 void AddCircle(PolylineSceneLayer::Chain& chain, |
206 const ScenePoint2D& centerS, | 206 const ScenePoint2D& centerS, |
207 const double& radiusS, | 207 const double& radiusS, |
208 const int numSubdivisions) | 208 const int numSubdivisions) |
209 { | 209 { |
210 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); | 210 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); |
211 //TODO: take DPI into account | 211 //TODO: take DPI into account |
212 | 212 |
232 } | 232 } |
233 | 233 |
234 double NormalizeAngle(double angle) | 234 double NormalizeAngle(double angle) |
235 { | 235 { |
236 double retAngle = angle; | 236 double retAngle = angle; |
237 while (retAngle < -1.0*g_pi) | 237 while (retAngle < -1.0 * g_pi) |
238 retAngle += 2 * g_pi; | 238 retAngle += 2 * g_pi; |
239 while (retAngle >= g_pi) | 239 while (retAngle >= g_pi) |
240 retAngle -= 2 * g_pi; | 240 retAngle -= 2 * g_pi; |
241 return retAngle; | 241 return retAngle; |
242 } | 242 } |
249 return NormalizeAngle(delta); | 249 return NormalizeAngle(delta); |
250 } | 250 } |
251 | 251 |
252 | 252 |
253 #if 0 | 253 #if 0 |
254 void AddEllipse(PolylineSceneLayer::Chain& chain, | 254 void AddEllipse(PolylineSceneLayer::Chain & chain, |
255 const Scene2D& scene, | 255 const Scene2D & scene, |
256 const ScenePoint2D& centerS, | 256 const ScenePoint2D & centerS, |
257 const double& halfHAxis, | 257 const double& halfHAxis, |
258 const double& halfVAxis) | 258 const double& halfVAxis) |
259 { | 259 { |
260 chain.clear(); | 260 chain.clear(); |
261 chain.reserve(4); | 261 chain.reserve(4); |
262 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); | 262 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); |
263 //TODO: take DPI into account | 263 //TODO: take DPI into account |
277 | 277 |
278 chain.push_back(startLT); | 278 chain.push_back(startLT); |
279 chain.push_back(startRT); | 279 chain.push_back(startRT); |
280 chain.push_back(startRB); | 280 chain.push_back(startRB); |
281 chain.push_back(startLB); | 281 chain.push_back(startLB); |
282 } | 282 } |
283 #endif | 283 #endif |
284 | 284 |
285 /** | 285 /** |
286 This utility function assumes that the layer holder contains 5 text layers | 286 This utility function assumes that the layer holder contains 5 text layers |
287 and will use the first four ones for the text background and the fifth one | 287 and will use the first four ones for the text background and the fifth one |
288 for the actual text | 288 for the actual text |
289 */ | 289 */ |
290 void SetTextLayerOutlineProperties( | 290 void SetTextLayerOutlineProperties( |
291 Scene2D& scene, boost::shared_ptr<LayerHolder> layerHolder, | 291 Scene2D& scene |
292 const char* text, ScenePoint2D p) | 292 , boost::shared_ptr<LayerHolder> layerHolder |
293 , const char* text | |
294 , ScenePoint2D p | |
295 , int startingLayerIndex) | |
293 { | 296 { |
294 double xoffsets[5] = { 2, 0, -2, 0, 0 }; | 297 double xoffsets[5] = { 2, 0, -2, 0, 0 }; |
295 double yoffsets[5] = { 0, -2, 0, 2, 0 }; | 298 double yoffsets[5] = { 0, -2, 0, 2, 0 }; |
296 | 299 |
297 // get the scaling factor | 300 // get the scaling factor |
298 const double pixelToScene = | 301 const double pixelToScene = |
299 scene.GetCanvasToSceneTransform().ComputeZoom(); | 302 scene.GetCanvasToSceneTransform().ComputeZoom(); |
300 | 303 |
301 for (int i = 0; i < 5; ++i) | 304 for (int i = startingLayerIndex; i < startingLayerIndex + 5; ++i) |
302 { | 305 { |
303 TextSceneLayer* textLayer = layerHolder->GetTextLayer(i); | 306 TextSceneLayer* textLayer = layerHolder->GetTextLayer(i); |
307 ORTHANC_ASSERT(textLayer != NULL); | |
304 textLayer->SetText(text); | 308 textLayer->SetText(text); |
305 | 309 |
306 if (i == 4) | 310 if (i == 4) |
307 { | 311 { |
308 textLayer->SetColor(TEXT_COLOR_RED, | 312 textLayer->SetColor(TEXT_COLOR_RED, |
309 TEXT_COLOR_GREEN, | 313 TEXT_COLOR_GREEN, |
310 TEXT_COLOR_BLUE); | 314 TEXT_COLOR_BLUE); |
311 } | 315 } |
312 else | 316 else |
313 { | 317 { |
314 textLayer->SetColor(TEXT_OUTLINE_COLOR_RED, | 318 textLayer->SetColor(TEXT_OUTLINE_COLOR_RED, |
315 TEXT_OUTLINE_COLOR_GREEN, | 319 TEXT_OUTLINE_COLOR_GREEN, |
316 TEXT_OUTLINE_COLOR_BLUE); | 320 TEXT_OUTLINE_COLOR_BLUE); |
317 } | 321 } |
318 | 322 |
319 ScenePoint2D textAnchor; | 323 ScenePoint2D textAnchor; |
320 textLayer->SetPosition( | 324 textLayer->SetPosition( |
321 p.GetX() + xoffsets[i] * pixelToScene, | 325 p.GetX() + xoffsets[i] * pixelToScene, |