DGtal  1.5.beta
Transforms.cpp
1 /* -*- mode: c++ -*- */
9 /*
10  * \@copyright This File is part of the Board library which is
11  * licensed under the terms of the GNU Lesser General Public Licence.
12  * See the LICENCE file for further details.
13  */
14 
15 #include "Board/Rect.h"
16 #include "Board/Shapes.h"
17 #include "Board/ShapeList.h"
18 #include "Board/Transforms.h"
19 #include <cmath>
20 
21 namespace {
22  const float ppmm = 720.0f / 254.0f;
23  const float fig_ppmm = 1143 / 25.4f;
24 }
25 
26 namespace LibBoard {
27 
28 //
29 // Transform
30 //
31 
32 double
33 Transform::rounded( double x ) const
34 {
35  return Transform::round( 1000000*x ) / 1000000;
36 }
37 
38 double
39 Transform::mapX( double x ) const
40 {
41  return rounded( x * _scale + _deltaX );
42 }
43 
44 double
45 Transform::scale( double x ) const
46 {
47  return rounded( x * _scale );
48 }
49 
50 void
51 Transform::apply( double & x, double & y ) const
52 {
53  x = mapX( x );
54  y = mapY( y );
55 }
56 
57 //
58 // TransformEPS
59 //
60 
61 double
62 TransformEPS::mapY( double y ) const
63 {
64  return rounded( y * _scale + _deltaY );
65 }
66 
67 void
69  const double pageWidth,
70  const double pageHeight,
71  const double margin )
72 {
73  if ( pageWidth <= 0 || pageHeight <= 0 ) {
74  _scale = 1.0f;
75  // _deltaX = - rect.left;
76  _deltaX = 0.5 * 210 * ppmm - ( rect.left + 0.5 * rect.width );
77  // _deltaY = - ( rect.top - rect.height );
78  _deltaY = 0.5 * 297 * ppmm - ( rect.top - 0.5 * rect.height );
79  _height = rect.height;
80  } else {
81  const double w = pageWidth - 2 * margin;
82  const double h = pageHeight - 2 * margin;
83  if ( ( rect.height / rect.width ) > ( h / w ) ) {
84  _scale = h * ppmm / rect.height;
85  } else {
86  _scale = w * ppmm / rect.width;
87  }
88  _deltaX = 0.5 * pageWidth * ppmm - _scale * ( rect.left + 0.5 * rect.width );
89  _deltaY = 0.5 * pageHeight * ppmm - _scale * ( rect.top - 0.5 * rect.height );
90  _height = pageHeight * ppmm;
91  }
92 }
93 
94 //
95 // TransformFIG
96 //
97 
98 double
99 TransformFIG::rounded( double x ) const
100 {
101  return Transform::round( x );
102 }
103 
104 double
105 TransformFIG::mapY( double y ) const
106 {
107  return rounded( _height - ( y * _scale + _deltaY ) );
108 }
109 
110 int
111 TransformFIG::mapWidth( double width ) const
112 {
113  // FIG width unit is 1/160 inch
114  // Postscript points are 1/72 inch
115  if ( width == 0.0 ) return 0;
116  int result = static_cast<int>( Transform::round( 160 * ( width / 72.0 ) ) );
117  return result>0?result:1;
118 }
119 
120 void
122  const double pageWidth,
123  const double pageHeight,
124  const double margin )
125 {
126  if ( pageWidth <= 0 || pageHeight <= 0 ) {
127  _scale = fig_ppmm / ppmm;
128  _deltaX = 0.5 * 210 * fig_ppmm - _scale * ( rect.left + 0.5 * rect.width );
129  //_deltaX = - rect.left;
130  _deltaY = 0.5 * 297 * fig_ppmm - _scale * ( rect.top - 0.5 * rect.height );
131  // _deltaY = - rect.top;
132  // _deltaY = - ( rect.top - rect.height );
133  //_height = rect.height;
134  _height = 297 * fig_ppmm;
135  } else {
136  const double w = pageWidth - 2 * margin;
137  const double h = pageHeight - 2 * margin;
138  if ( rect.height / rect.width > ( h / w ) ) {
139  _scale = ( h * fig_ppmm ) / rect.height;
140  } else {
141  _scale = ( w * fig_ppmm ) / rect.width;
142  }
143  _deltaX = 0.5 * pageWidth * fig_ppmm - _scale * ( rect.left + 0.5 * rect.width );
144  _deltaY = 0.5 * pageHeight * fig_ppmm - _scale * ( rect.top - 0.5 * rect.height );
145  _height = pageHeight * fig_ppmm;
146  }
147  // float ppmm = (1200/25.4);
148 }
149 
150 void
152 {
153  _maxDepth = shapes.maxDepth();
154  _minDepth = shapes.minDepth();
155 }
156 
157 int
158 TransformFIG::mapDepth( int depth ) const
159 {
160  if ( depth > _maxDepth ) return 999;
161  if ( _maxDepth - _minDepth > 998 ) {
162  double range = _maxDepth - _minDepth;
163  int r = static_cast<int>( 1 + Transform::round( ( ( depth - _minDepth ) / range ) * 998 ) );
164  return r>=0?r:0;
165  } else {
166  int r = 1 + depth - _minDepth;
167  return r>=0?r:0;
168  }
169 }
170 
171 //
172 // TransformSVG
173 //
174 
175 double
176 TransformSVG::rounded( double x ) const
177 {
178  return Transform::round( 100*x ) / 100.0f;
179 }
180 
181 double
182 TransformSVG::mapY( double y ) const
183 {
184  return rounded( _height - ( y * _scale + _deltaY ) );
185 }
186 
187 double
188 TransformSVG::mapWidth( double width ) const
189 {
190  // return Transform::round( 1000 * width / ppmm ) / 1000.0;
191  return Transform::round( 1000 * width / ppmm ) / 1000.0;
192 }
193 
194 void
196  const double pageWidth,
197  const double pageHeight,
198  const double margin )
199 {
200  if ( pageWidth <= 0 || pageHeight <= 0 ) {
201  _scale = 1.0f;
202  // _deltaX = 0.5 * 210 * ppmm - ( rect.left + 0.5 * rect.width );
203  _deltaX = - rect.left;
204  // _deltaY = 0.5 * 297 * ppmm - ( rect.top - 0.5 * rect.height );
205  _deltaY = - ( rect.top - rect.height );
206  // _height = 297 * fig_ppmm;
207  _height = rect.height;
208  } else {
209  const double w = pageWidth - 2 * margin;
210  const double h = pageHeight - 2 * margin;
211  if ( rect.height / rect.width > ( h / w ) ) {
212  _scale = h * ppmm / rect.height;
213  } else {
214  _scale = w * ppmm / rect.width;
215  }
216  _deltaX = 0.5 * pageWidth * ppmm - _scale * ( rect.left + 0.5 * rect.width );
217  _deltaY = 0.5 * pageHeight * ppmm - _scale * ( rect.top - 0.5 * rect.height );
218  _height = pageHeight * ppmm;
219  }
220 }
221 
222 #ifdef WITH_CAIRO
223 //
224 // TransformCairo
225 //
226 
227 double
228 TransformCairo::rounded( double x ) const
229 {
230  return Transform::round( 100*x ) / 100.0f;
231 }
232 
233 double
234 TransformCairo::mapY( double y ) const
235 {
236  return rounded( _height - ( y * _scale + _deltaY ) );
237 }
238 
239 double
240 TransformCairo::mapWidth( double width ) const
241 {
242  // return Transform::round( 1000 * width / ppmm ) / 1000.0;
243  return Transform::round( 1000 * width / ppmm ) / 1000.0;
244 }
245 
246 void
248  const double pageWidth,
249  const double pageHeight,
250  const double margin )
251 {
252  if ( pageWidth <= 0 || pageHeight <= 0 ) {
253  _scale = 1.0f;
254  // _deltaX = 0.5 * 210 * ppmm - ( rect.left + 0.5 * rect.width );
255  _deltaX = - rect.left;
256  // _deltaY = 0.5 * 297 * ppmm - ( rect.top - 0.5 * rect.height );
257  _deltaY = - ( rect.top - rect.height );
258  // _height = 297 * fig_ppmm;
259  _height = rect.height;
260  } else {
261  const double w = pageWidth - 2 * margin;
262  const double h = pageHeight - 2 * margin;
263  if ( rect.height / rect.width > ( h / w ) ) {
264  _scale = h * ppmm / rect.height;
265  } else {
266  _scale = w * ppmm / rect.width;
267  }
268  _deltaX = 0.5 * pageWidth * ppmm - _scale * ( rect.left + 0.5 * rect.width );
269  _deltaY = 0.5 * pageHeight * ppmm - _scale * ( rect.top - 0.5 * rect.height );
270  _height = pageHeight * ppmm;
271  }
272 }
273 #endif
274 
275 } // namespace LibBoard
Struct representing a rectangle on the plane.
Definition: Rect.h:26
double height
Definition: Rect.h:31
double width
Definition: Rect.h:30
double left
Definition: Rect.h:28
double top
Definition: Rect.h:29
A group of shapes.
Definition: ShapeList.h:28
virtual int minDepth() const
Definition: ShapeList.cpp:395
virtual int maxDepth() const
Definition: ShapeList.cpp:421
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
Definition: Transforms.cpp:247
double mapY(double y) const
Definition: Transforms.cpp:234
double mapWidth(double width) const
Definition: Transforms.cpp:240
double rounded(double x) const
Definition: Transforms.cpp:228
double mapY(double y) const
Definition: Transforms.cpp:62
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
Definition: Transforms.cpp:68
int mapWidth(double width) const
Definition: Transforms.cpp:111
int mapDepth(int depth) const
Definition: Transforms.cpp:158
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
Definition: Transforms.cpp:121
double rounded(double x) const
Definition: Transforms.cpp:99
double mapY(double y) const
Definition: Transforms.cpp:105
void setDepthRange(const ShapeList &shapes)
Definition: Transforms.cpp:151
double mapWidth(double width) const
Definition: Transforms.cpp:188
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
Definition: Transforms.cpp:195
double rounded(double x) const
Definition: Transforms.cpp:176
double mapY(double y) const
Definition: Transforms.cpp:182
virtual double mapX(double x) const
Definition: Transforms.cpp:39
virtual void apply(double &x, double &y) const
Definition: Transforms.cpp:51
virtual double mapY(double y) const =0
virtual double scale(double x) const
Definition: Transforms.cpp:45
virtual double rounded(double x) const
Definition: Transforms.cpp:33
static double round(const double &x)