keep aspect ratio of simulator image
Stefan Schuermans

Stefan Schuermans commited on 2017-06-07 21:38:03
Showing 7 changed files, with 27 additions and 9 deletions.

... ...
@@ -118,3 +118,13 @@ void Box::getRelative(const Point &abs, Point &rel) const
118 118
   }
119 119
 }
120 120
 
121
+/// expand box (centered) in order to make it quadratic
122
+Box Box::expandQuadratic() const
123
+{
124
+  Point delta = mTR - mBL;
125
+  double max = delta.mX > delta.mY ? delta.mX : delta.mY;
126
+  Point fix(max - delta.mX, max - delta.mY);
127
+  fix *= 0.5;
128
+  return Box(mTR + fix, mBL - fix);
129
+}
130
+
... ...
@@ -33,6 +33,8 @@ public:
33 33
   bool isIntersecting(const Box *pBox) const;
34 34
   bool getIntersection(const Box *pBox, Box *pIntersect) const;
35 35
   void getRelative(const Point &abs, Point &rel) const;
36
+  /// expand box (centered) in order to make it quadratic
37
+  Box expandQuadratic() const;
36 38
   Point mBL, mTR;
37 39
 };
38 40
 
... ...
@@ -88,13 +88,14 @@ void Distri::writePixels(std::ostream & strm) const
88 88
   }
89 89
 }
90 90
 
91
-void Distri::writeSimPixels(std::ostream & strm, const Box & boundsVideo) const
91
+void Distri::writeSimPixels(std::ostream & strm,
92
+                            const Box & boundsVideoQuad) const
92 93
 {
93 94
   std::vector<Chain>::const_iterator itC;
94 95
   unsigned int c;
95 96
   for (itC = mChains.begin(), c = 0; itC != mChains.end(); ++itC, ++c) {
96 97
     strm << "output " << mNo << "," << c << " =";
97
-    itC->writeSimPixels(strm, boundsVideo);
98
+    itC->writeSimPixels(strm, boundsVideoQuad);
98 99
     strm << std::endl;
99 100
   }
100 101
 }
... ...
@@ -45,7 +45,7 @@ public:
45 45
   void writeDistri(std::ostream & strm) const;
46 46
   void writeMapping(std::ostream & strm) const;
47 47
   void writePixels(std::ostream & strm) const;
48
-  void writeSimPixels(std::ostream & strm, const Box & boundsVideo) const;
48
+  void writeSimPixels(std::ostream & strm, const Box & boundsVideoQuad) const;
49 49
   size_t countPixels() const;
50 50
 private:
51 51
   unsigned int mNo, mChainCnt, mPixelCnt;
... ...
@@ -534,6 +534,10 @@ static int writeSimCfg(const std::vector<Distri> &distris,
534 534
                        const Box & boundsVideo,
535 535
                        const std::string & strSimCfgFileName)
536 536
 {
537
+  /* make video bounds quadratic
538
+     in order to keep aspect ratio of simulator image */
539
+  Box boundsVideoQuad = boundsVideo.expandQuadratic();
540
+
537 541
   // open output file
538 542
   std::ofstream strm(strSimCfgFileName.c_str(), std::ios::out);
539 543
   if (!strm.is_open()) {
... ...
@@ -555,7 +559,7 @@ static int writeSimCfg(const std::vector<Distri> &distris,
555 559
 
556 560
   // simulated pixels
557 561
   for (itD = distris.begin(); itD != distris.end(); ++itD)
558
-    itD->writeSimPixels(strm, boundsVideo);
562
+    itD->writeSimPixels(strm, boundsVideoQuad);
559 563
   strm << std::endl;
560 564
 
561 565
   strm.close();
... ...
@@ -58,12 +58,13 @@ void Pixel::writePixel(std::ostream & strm) const
58 58
   strm << " " << mX << "," << mY;
59 59
 }
60 60
 
61
-void Pixel::writeSimPixel(std::ostream & strm, const Box & boundsVideo) const
61
+void Pixel::writeSimPixel(std::ostream & strm,
62
+                          const Box & boundsVideoQuad) const
62 63
 {
63 64
   Point c, bl, tr;
64
-  boundsVideo.getRelative(mCenter, c);
65
-  boundsVideo.getRelative(mBounds.mBL, bl);
66
-  boundsVideo.getRelative(mBounds.mTR, tr);
65
+  boundsVideoQuad.getRelative(mCenter, c);
66
+  boundsVideoQuad.getRelative(mBounds.mBL, bl);
67
+  boundsVideoQuad.getRelative(mBounds.mTR, tr);
67 68
   double r1 = (c - bl).abs();
68 69
   double r2 = (c - tr).abs();
69 70
   double r = r1 > r2 ? r1 : r2;
... ...
@@ -40,7 +40,7 @@ public:
40 40
   int pixCoord(const Point &pix0, const Point &pixSz,
41 41
                unsigned int width, unsigned int height);
42 42
   void writePixel(std::ostream & strm) const;
43
-  void writeSimPixel(std::ostream & strm, const Box & boundsVideo) const;
43
+  void writeSimPixel(std::ostream & strm, const Box & boundsVideoQuad) const;
44 44
 private:
45 45
   const Object *mpObjPixel;
46 46
   Box mBounds;
47 47