comparison Framework/Scene2DViewport/MeasureToolsToolbox.cpp @ 751:712ff6ff3c19

- undo redo now works fine for both measure tool creation commands - added LayerHolder to streamline layer index management - added overloads for ORTHANC_ASSERT with no string message (some heavy preprocessor wizardry in there) - fixing wasm BasicScene is *not* finished.
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 22 May 2019 11:55:52 +0200
parents 8b6adfb62a2f
children 92c400a09f1b
comparison
equal deleted inserted replaced
750:284f37dc1c66 751:712ff6ff3c19
17 * You should have received a copy of the GNU Affero General Public License 17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 #include "MeasureToolsToolbox.h" 21 #include "MeasureToolsToolbox.h"
22 22 #include "PointerTypes.h"
23 #include <Framework/Scene2D/TextSceneLayer.h> 23 #include "LayerHolder.h"
24
25 #include "../Scene2D/TextSceneLayer.h"
26 #include "../Scene2D/Scene2D.h"
24 27
25 #include <boost/math/constants/constants.hpp> 28 #include <boost/math/constants/constants.hpp>
26 29
27 namespace 30 namespace
28 { 31 {
29 double g_pi = boost::math::constants::pi<double>(); 32 double g_pi = boost::math::constants::pi<double>();
30 } 33 }
31 34
32 namespace OrthancStone 35 namespace OrthancStone
33 { 36 {
37 void GetPositionOnBisectingLine(
38 ScenePoint2D& result
39 , const ScenePoint2D& p1
40 , const ScenePoint2D& c
41 , const ScenePoint2D& p2
42 , const double d)
43 {
44 // TODO: fix correct half-plane
45 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX());
46 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX());
47 double angle = 0.5 * (p1cAngle + p2cAngle);
48 double unitVectorX = cos(angle);
49 double unitVectorY = sin(angle);
50 double posX = c.GetX() + d * unitVectorX;
51 double posY = c.GetX() + d * unitVectorY;
52 result = ScenePoint2D(posX, posY);
53 }
54
34 double RadiansToDegrees(double angleRad) 55 double RadiansToDegrees(double angleRad)
35 { 56 {
36 static const double factor = 180.0 / g_pi; 57 static const double factor = 180.0 / g_pi;
37 return angleRad * factor; 58 return angleRad * factor;
38 } 59 }
39 60
40 void AddSquare(PolylineSceneLayer::Chain& chain, 61 void AddSquare(PolylineSceneLayer::Chain& chain,
41 const Scene2D& scene, 62 Scene2DConstPtr scene,
42 const ScenePoint2D& centerS, 63 const ScenePoint2D& centerS,
43 const double& sideLength) 64 const double& sideLengthS)
44 { 65 {
66 // get the scaling factor
67 const double sceneToCanvas =
68 scene->GetSceneToCanvasTransform().ComputeZoom();
69
45 chain.clear(); 70 chain.clear();
46 chain.reserve(4); 71 chain.reserve(4);
47 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); 72 ScenePoint2D centerC = centerS.Apply(scene->GetSceneToCanvasTransform());
48 //TODO: take DPI into account 73 //TODO: take DPI into account
49 double handleLX = centerC.GetX() - sideLength / 2; 74 double handleLX = centerC.GetX() - sideLengthS * sceneToCanvas * 0.5;
50 double handleTY = centerC.GetY() - sideLength / 2; 75 double handleTY = centerC.GetY() - sideLengthS * sceneToCanvas * 0.5;
51 double handleRX = centerC.GetX() + sideLength / 2; 76 double handleRX = centerC.GetX() + sideLengthS * sceneToCanvas * 0.5;
52 double handleBY = centerC.GetY() + sideLength / 2; 77 double handleBY = centerC.GetY() + sideLengthS * sceneToCanvas * 0.5;
53 ScenePoint2D LTC(handleLX, handleTY); 78 ScenePoint2D LTC(handleLX, handleTY);
54 ScenePoint2D RTC(handleRX, handleTY); 79 ScenePoint2D RTC(handleRX, handleTY);
55 ScenePoint2D RBC(handleRX, handleBY); 80 ScenePoint2D RBC(handleRX, handleBY);
56 ScenePoint2D LBC(handleLX, handleBY); 81 ScenePoint2D LBC(handleLX, handleBY);
57 82
58 ScenePoint2D startLT = LTC.Apply(scene.GetCanvasToSceneTransform()); 83 ScenePoint2D startLT = LTC.Apply(scene->GetCanvasToSceneTransform());
59 ScenePoint2D startRT = RTC.Apply(scene.GetCanvasToSceneTransform()); 84 ScenePoint2D startRT = RTC.Apply(scene->GetCanvasToSceneTransform());
60 ScenePoint2D startRB = RBC.Apply(scene.GetCanvasToSceneTransform()); 85 ScenePoint2D startRB = RBC.Apply(scene->GetCanvasToSceneTransform());
61 ScenePoint2D startLB = LBC.Apply(scene.GetCanvasToSceneTransform()); 86 ScenePoint2D startLB = LBC.Apply(scene->GetCanvasToSceneTransform());
62 87
63 chain.push_back(startLT); 88 chain.push_back(startLT);
64 chain.push_back(startRT); 89 chain.push_back(startRT);
65 chain.push_back(startRB); 90 chain.push_back(startRB);
66 chain.push_back(startLB); 91 chain.push_back(startLB);
84 } 109 }
85 #endif 110 #endif
86 111
87 void AddShortestArc( 112 void AddShortestArc(
88 PolylineSceneLayer::Chain& chain 113 PolylineSceneLayer::Chain& chain
89 , const Scene2D& scene
90 , const ScenePoint2D& p1 114 , const ScenePoint2D& p1
91 , const ScenePoint2D& c 115 , const ScenePoint2D& c
92 , const ScenePoint2D& p2 116 , const ScenePoint2D& p2
93 , const double& radiusS 117 , const double& radiusS
94 , const int subdivisionsCount) 118 , const int subdivisionsCount)
95 { 119 {
96 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); 120 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX());
97 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); 121 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX());
98 AddShortestArc( 122 AddShortestArc(
99 chain, scene, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount); 123 chain, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount);
100 } 124 }
101 125
102 void GetPositionOnBisectingLine(
103 ScenePoint2D& result
104 , const ScenePoint2D& p1
105 , const ScenePoint2D& c
106 , const ScenePoint2D& p2
107 , const double d)
108 {
109 // TODO: fix correct half-plane
110 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX());
111 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX());
112 double angle = 0.5*(p1cAngle + p2cAngle);
113 double unitVectorX = cos(angle);
114 double unitVectorY = sin(angle);
115 double posX = c.GetX() + d * unitVectorX;
116 double posY = c.GetX() + d * unitVectorY;
117 result = ScenePoint2D(posX, posY);
118 }
119
120 void AddShortestArc( 126 void AddShortestArc(
121 PolylineSceneLayer::Chain& chain 127 PolylineSceneLayer::Chain& chain
122 , const Scene2D& scene
123 , const ScenePoint2D& centerS 128 , const ScenePoint2D& centerS
124 , const double& radiusS 129 , const double& radiusS
125 , const double startAngleRad 130 , const double startAngleRad
126 , const double endAngleRad 131 , const double endAngleRad
127 , const int subdivisionsCount) 132 , const int subdivisionsCount)
195 } 200 }
196 } 201 }
197 #endif 202 #endif
198 203
199 void AddCircle(PolylineSceneLayer::Chain& chain, 204 void AddCircle(PolylineSceneLayer::Chain& chain,
200 const Scene2D& scene,
201 const ScenePoint2D& centerS, 205 const ScenePoint2D& centerS,
202 const double& radiusS, 206 const double& radiusS,
203 const int numSubdivisions) 207 const int numSubdivisions)
204 { 208 {
205 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); 209 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform());
276 chain.push_back(startLB); 280 chain.push_back(startLB);
277 } 281 }
278 #endif 282 #endif
279 283
280 284
281 namespace 285 /**
282 { 286 This utility function assumes that the layer holder contains 5 text layers
283 /** 287 and will use the first four ones for the text background and the fifth one
284 Helper function for outlined text rendering 288 for the actual text
285 */ 289 */
286 TextSceneLayer* GetOutlineTextLayer(
287 Scene2D& scene, int baseLayerIndex, int index)
288 {
289 assert(scene.HasLayer(baseLayerIndex));
290 assert(index >= 0);
291 assert(index < 5);
292
293 ISceneLayer * layer = &(scene.GetLayer(baseLayerIndex + index));
294 TextSceneLayer * concreteLayer = dynamic_cast<TextSceneLayer*>(layer);
295 assert(concreteLayer != NULL);
296 return concreteLayer;
297 }
298 }
299
300 void SetTextLayerOutlineProperties( 290 void SetTextLayerOutlineProperties(
301 Scene2D& scene, int baseLayerIndex, const char* text, ScenePoint2D p) 291 Scene2DPtr scene, LayerHolderPtr layerHolder,
292 const char* text, ScenePoint2D p)
302 { 293 {
303 double xoffsets[5] = { 2, 0, -2, 0, 0 }; 294 double xoffsets[5] = { 2, 0, -2, 0, 0 };
304 double yoffsets[5] = { 0, -2, 0, 2, 0 }; 295 double yoffsets[5] = { 0, -2, 0, 2, 0 };
305 296
306 // get the scaling factor 297 // get the scaling factor
307 const double pixelToScene = 298 const double pixelToScene =
308 scene.GetCanvasToSceneTransform().ComputeZoom(); 299 scene->GetCanvasToSceneTransform().ComputeZoom();
309 300
310 for (int i = 0; i < 5; ++i) 301 for (int i = 0; i < 5; ++i)
311 { 302 {
312 TextSceneLayer* textLayer = GetOutlineTextLayer(scene, baseLayerIndex, i); 303 TextSceneLayer* textLayer = layerHolder->GetTextLayer(i);
313 textLayer->SetText(text); 304 textLayer->SetText(text);
314 305
315 if (i == 4) 306 if (i == 4)
316 textLayer->SetColor(0, 223, 81); 307 textLayer->SetColor(0, 223, 81);
317 else 308 else