DGtal 1.4.0
Loading...
Searching...
No Matches
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
21namespace {
22 const float ppmm = 720.0f / 254.0f;
23 const float fig_ppmm = 1143 / 25.4f;
24}
25
26namespace LibBoard {
27
28//
29// Transform
30//
31
32double
33Transform::rounded( double x ) const
34{
35 return Transform::round( 1000000*x ) / 1000000;
36}
37
38double
39Transform::mapX( double x ) const
40{
41 return rounded( x * _scale + _deltaX );
42}
43
44double
45Transform::scale( double x ) const
46{
47 return rounded( x * _scale );
48}
49
50void
51Transform::apply( double & x, double & y ) const
52{
53 x = mapX( x );
54 y = mapY( y );
55}
56
57//
58// TransformEPS
59//
60
61double
62TransformEPS::mapY( double y ) const
63{
64 return rounded( y * _scale + _deltaY );
65}
66
67void
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
98double
99TransformFIG::rounded( double x ) const
100{
101 return Transform::round( x );
102}
103
104double
105TransformFIG::mapY( double y ) const
106{
107 return rounded( _height - ( y * _scale + _deltaY ) );
108}
109
110int
111TransformFIG::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
120void
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
150void
152{
153 _maxDepth = shapes.maxDepth();
154 _minDepth = shapes.minDepth();
155}
156
157int
158TransformFIG::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
175double
176TransformSVG::rounded( double x ) const
177{
178 return Transform::round( 100*x ) / 100.0f;
179}
180
181double
182TransformSVG::mapY( double y ) const
183{
184 return rounded( _height - ( y * _scale + _deltaY ) );
185}
186
187double
188TransformSVG::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
194void
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
227double
228TransformCairo::rounded( double x ) const
229{
230 return Transform::round( 100*x ) / 100.0f;
231}
232
233double
234TransformCairo::mapY( double y ) const
235{
236 return rounded( _height - ( y * _scale + _deltaY ) );
237}
238
239double
240TransformCairo::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
246void
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
virtual int maxDepth() const
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
double mapY(double y) const
double mapWidth(double width) const
double rounded(double x) const
double mapY(double y) const
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
int mapWidth(double width) const
int mapDepth(int depth) const
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
double rounded(double x) const
double mapY(double y) const
void setDepthRange(const ShapeList &shapes)
double mapWidth(double width) const
void setBoundingBox(const Rect &rect, const double pageWidth, const double pageHeight, const double margin)
double rounded(double x) const
double mapY(double y) const
virtual double mapX(double x) const
virtual void apply(double &x, double &y) const
virtual double mapY(double y) const =0
virtual double scale(double x) const
virtual double rounded(double x) const
static double round(const double &x)