comparison Framework/Scene2DViewport/MeasureToolsToolbox.cpp @ 860:238693c3bc51 am-dev

merge default -> am-dev
author Alain Mazy <alain@mazy.be>
date Mon, 24 Jun 2019 14:35:00 +0200
parents e42b491f1fb2
children a29c13497557
comparison
equal deleted inserted replaced
856:a6e17a5a39e7 860:238693c3bc51
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 "PredeclaredTypes.h"
23 #include <Framework/Scene2D/TextSceneLayer.h> 23 #include "LayerHolder.h"
24 #include "ViewportController.h"
25
26 #include "../Scene2D/TextSceneLayer.h"
27 #include "../Scene2D/Scene2D.h"
24 28
25 #include <boost/math/constants/constants.hpp> 29 #include <boost/math/constants/constants.hpp>
26 30
27 namespace 31 namespace
28 { 32 {
29 double g_pi = boost::math::constants::pi<double>(); 33 double g_pi = boost::math::constants::pi<double>();
30 } 34 }
31 35
32 namespace OrthancStone 36 namespace OrthancStone
33 { 37 {
38 void GetPositionOnBisectingLine(
39 ScenePoint2D& result
40 , const ScenePoint2D& p1
41 , const ScenePoint2D& c
42 , const ScenePoint2D& p2
43 , const double d)
44 {
45 // TODO: fix correct half-plane
46 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX());
47 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX());
48 double angle = 0.5 * (p1cAngle + p2cAngle);
49 double unitVectorX = cos(angle);
50 double unitVectorY = sin(angle);
51 double posX = c.GetX() + d * unitVectorX;
52 double posY = c.GetX() + d * unitVectorY;
53 result = ScenePoint2D(posX, posY);
54 }
55
34 double RadiansToDegrees(double angleRad) 56 double RadiansToDegrees(double angleRad)
35 { 57 {
36 static const double factor = 180.0 / g_pi; 58 static const double factor = 180.0 / g_pi;
37 return angleRad * factor; 59 return angleRad * factor;
38 } 60 }
39 61
40 void AddSquare(PolylineSceneLayer::Chain& chain, 62 void AddSquare(PolylineSceneLayer::Chain& chain,
41 const Scene2D& scene, 63 boost::shared_ptr<const Scene2D> scene,
42 const ScenePoint2D& centerS, 64 const ScenePoint2D& centerS,
43 const double& sideLength) 65 const double& sideLengthS)
44 { 66 {
67 // get the scaling factor
68 const double sceneToCanvas =
69 scene->GetSceneToCanvasTransform().ComputeZoom();
70
45 chain.clear(); 71 chain.clear();
46 chain.reserve(4); 72 chain.reserve(4);
47 ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); 73 ScenePoint2D centerC = centerS.Apply(scene->GetSceneToCanvasTransform());
48 //TODO: take DPI into account 74 //TODO: take DPI into account
49 double handleLX = centerC.GetX() - sideLength / 2; 75 double handleLX = centerC.GetX() - sideLengthS * sceneToCanvas * 0.5;
50 double handleTY = centerC.GetY() - sideLength / 2; 76 double handleTY = centerC.GetY() - sideLengthS * sceneToCanvas * 0.5;
51 double handleRX = centerC.GetX() + sideLength / 2; 77 double handleRX = centerC.GetX() + sideLengthS * sceneToCanvas * 0.5;
52 double handleBY = centerC.GetY() + sideLength / 2; 78 double handleBY = centerC.GetY() + sideLengthS * sceneToCanvas * 0.5;
53 ScenePoint2D LTC(handleLX, handleTY); 79 ScenePoint2D LTC(handleLX, handleTY);
54 ScenePoint2D RTC(handleRX, handleTY); 80 ScenePoint2D RTC(handleRX, handleTY);
55 ScenePoint2D RBC(handleRX, handleBY); 81 ScenePoint2D RBC(handleRX, handleBY);
56 ScenePoint2D LBC(handleLX, handleBY); 82 ScenePoint2D LBC(handleLX, handleBY);
57 83
58 ScenePoint2D startLT = LTC.Apply(scene.GetCanvasToSceneTransform()); 84 ScenePoint2D startLT = LTC.Apply(scene->GetCanvasToSceneTransform());
59 ScenePoint2D startRT = RTC.Apply(scene.GetCanvasToSceneTransform()); 85 ScenePoint2D startRT = RTC.Apply(scene->GetCanvasToSceneTransform());
60 ScenePoint2D startRB = RBC.Apply(scene.GetCanvasToSceneTransform()); 86 ScenePoint2D startRB = RBC.Apply(scene->GetCanvasToSceneTransform());
61 ScenePoint2D startLB = LBC.Apply(scene.GetCanvasToSceneTransform()); 87 ScenePoint2D startLB = LBC.Apply(scene->GetCanvasToSceneTransform());
62 88
63 chain.push_back(startLT); 89 chain.push_back(startLT);
64 chain.push_back(startRT); 90 chain.push_back(startRT);
65 chain.push_back(startRB); 91 chain.push_back(startRB);
66 chain.push_back(startLB); 92 chain.push_back(startLB);
84 } 110 }
85 #endif 111 #endif
86 112
87 void AddShortestArc( 113 void AddShortestArc(
88 PolylineSceneLayer::Chain& chain 114 PolylineSceneLayer::Chain& chain
89 , const Scene2D& scene
90 , const ScenePoint2D& p1 115 , const ScenePoint2D& p1
91 , const ScenePoint2D& c 116 , const ScenePoint2D& c
92 , const ScenePoint2D& p2 117 , const ScenePoint2D& p2
93 , const double& radiusS 118 , const double& radiusS
94 , const int subdivisionsCount) 119 , const int subdivisionsCount)
95 { 120 {
96 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX()); 121 double p1cAngle = atan2(p1.GetY() - c.GetY(), p1.GetX() - c.GetX());
97 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX()); 122 double p2cAngle = atan2(p2.GetY() - c.GetY(), p2.GetX() - c.GetX());
98 AddShortestArc( 123 AddShortestArc(
99 chain, scene, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount); 124 chain, c, radiusS, p1cAngle, p2cAngle, subdivisionsCount);
100 } 125 }
101 126
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( 127 void AddShortestArc(
121 PolylineSceneLayer::Chain& chain 128 PolylineSceneLayer::Chain& chain
122 , const Scene2D& scene
123 , const ScenePoint2D& centerS 129 , const ScenePoint2D& centerS
124 , const double& radiusS 130 , const double& radiusS
125 , const double startAngleRad 131 , const double startAngleRad
126 , const double endAngleRad 132 , const double endAngleRad
127 , const int subdivisionsCount) 133 , const int subdivisionsCount)
195 } 201 }
196 } 202 }
197 #endif 203 #endif
198 204
199 void AddCircle(PolylineSceneLayer::Chain& chain, 205 void AddCircle(PolylineSceneLayer::Chain& chain,
200 const Scene2D& scene,
201 const ScenePoint2D& centerS, 206 const ScenePoint2D& centerS,
202 const double& radiusS, 207 const double& radiusS,
203 const int numSubdivisions) 208 const int numSubdivisions)
204 { 209 {
205 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform()); 210 //ScenePoint2D centerC = centerS.Apply(scene.GetSceneToCanvasTransform());
275 chain.push_back(startRB); 280 chain.push_back(startRB);
276 chain.push_back(startLB); 281 chain.push_back(startLB);
277 } 282 }
278 #endif 283 #endif
279 284
280 285 /**
281 namespace 286 This utility function assumes that the layer holder contains 5 text layers
282 { 287 and will use the first four ones for the text background and the fifth one
283 /** 288 for the actual text
284 Helper function for outlined text rendering 289 */
285 */
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 boost::shared_ptr<Scene2D> scene, boost::shared_ptr<LayerHolder> 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 {
308 textLayer->SetColor(TEXT_COLOR_RED,
309 TEXT_COLOR_GREEN,
310 TEXT_COLOR_BLUE);
311 }
317 else 312 else
318 textLayer->SetColor(0, 56, 21); 313 {
314 textLayer->SetColor(TEXT_OUTLINE_COLOR_RED,
315 TEXT_OUTLINE_COLOR_GREEN,
316 TEXT_OUTLINE_COLOR_BLUE);
317 }
319 318
320 ScenePoint2D textAnchor; 319 ScenePoint2D textAnchor;
321 //GetPositionOnBisectingLine(
322 // textAnchor, side1End_, center_, side2End_, 40.0*pixelToScene);
323 textLayer->SetPosition( 320 textLayer->SetPosition(
324 p.GetX() + xoffsets[i] * pixelToScene, 321 p.GetX() + xoffsets[i] * pixelToScene,
325 p.GetY() + yoffsets[i] * pixelToScene); 322 p.GetY() + yoffsets[i] * pixelToScene);
326 } 323 }
327 } 324 }
328
329
330
331
332
333
334
335
336 } 325 }