1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Viewport transformer - simple orthogonal conversions from world coordinates
17 // to screen (device) ones.
19 //----------------------------------------------------------------------------
21 #ifndef AGG_TRANS_VIEWPORT_INCLUDED
22 #define AGG_TRANS_VIEWPORT_INCLUDED
25 #include "agg_basics.h"
39 //----------------------------------------------------------trans_viewport
43 //-------------------------------------------------------------------
53 m_aspect(aspect_ratio_stretch
),
66 //-------------------------------------------------------------------
67 void preserve_aspect_ratio(double alignx
,
69 aspect_ratio_e aspect
)
77 //-------------------------------------------------------------------
78 void device_viewport(double x1
, double y1
, double x2
, double y2
)
87 //-------------------------------------------------------------------
88 void world_viewport(double x1
, double y1
, double x2
, double y2
)
97 //-------------------------------------------------------------------
98 void device_viewport(double* x1
, double* y1
, double* x2
, double* y2
) const
106 //-------------------------------------------------------------------
107 void world_viewport(double* x1
, double* y1
, double* x2
, double* y2
) const
115 //-------------------------------------------------------------------
116 void world_viewport_actual(double* x1
, double* y1
,
117 double* x2
, double* y2
) const
125 //-------------------------------------------------------------------
126 double align_x() const { return m_align_x
; }
127 double align_y() const { return m_align_y
; }
128 aspect_ratio_e
aspect_ratio() const { return m_aspect
; }
130 //-------------------------------------------------------------------
131 void transform(double* x
, double* y
) const
133 *x
= (*x
- m_wx1
) * m_kx
+ m_dx1
;
134 *y
= (*y
- m_wy1
) * m_ky
+ m_dy1
;
137 //-------------------------------------------------------------------
138 void inverse_transform(double* x
, double* y
) const
140 *x
= (*x
- m_dx1
) / m_kx
+ m_wx1
;
141 *y
= (*y
- m_dy1
) / m_ky
+ m_wy1
;
144 //-------------------------------------------------------------------
145 double scale_x() const
150 //-------------------------------------------------------------------
151 double scale_y() const
156 //-------------------------------------------------------------------
159 return (m_kx
+ m_ky
) * 0.5;
163 //-------------------------------------------------------------------
164 unsigned byte_size() const
171 sizeof(m_device_x1
) +
172 sizeof(m_device_y1
) +
173 sizeof(m_device_x2
) +
174 sizeof(m_device_y2
) +
188 void serialize(int8u
* ptr
) const
190 memcpy(ptr
, &m_world_x1
, sizeof(m_world_x1
)); ptr
+= sizeof(m_world_x1
);
191 memcpy(ptr
, &m_world_y1
, sizeof(m_world_y1
)); ptr
+= sizeof(m_world_y1
);
192 memcpy(ptr
, &m_world_x2
, sizeof(m_world_x2
)); ptr
+= sizeof(m_world_x2
);
193 memcpy(ptr
, &m_world_y2
, sizeof(m_world_y2
)); ptr
+= sizeof(m_world_y2
);
194 memcpy(ptr
, &m_device_x1
, sizeof(m_device_x1
)); ptr
+= sizeof(m_device_x1
);
195 memcpy(ptr
, &m_device_y1
, sizeof(m_device_y1
)); ptr
+= sizeof(m_device_y1
);
196 memcpy(ptr
, &m_device_x2
, sizeof(m_device_x2
)); ptr
+= sizeof(m_device_x2
);
197 memcpy(ptr
, &m_device_y2
, sizeof(m_device_y2
)); ptr
+= sizeof(m_device_y2
);
198 memcpy(ptr
, &m_aspect
, sizeof(m_aspect
)); ptr
+= sizeof(m_aspect
);
199 memcpy(ptr
, &m_align_x
, sizeof(m_align_x
)); ptr
+= sizeof(m_align_x
);
200 memcpy(ptr
, &m_align_y
, sizeof(m_align_y
)); ptr
+= sizeof(m_align_y
);
201 memcpy(ptr
, &m_wx1
, sizeof(m_wx1
)); ptr
+= sizeof(m_wx1
);
202 memcpy(ptr
, &m_wy1
, sizeof(m_wy1
)); ptr
+= sizeof(m_wy1
);
203 memcpy(ptr
, &m_wx2
, sizeof(m_wx2
)); ptr
+= sizeof(m_wx2
);
204 memcpy(ptr
, &m_wy2
, sizeof(m_wy2
)); ptr
+= sizeof(m_wy2
);
205 memcpy(ptr
, &m_dx1
, sizeof(m_dx1
)); ptr
+= sizeof(m_dx1
);
206 memcpy(ptr
, &m_dy1
, sizeof(m_dy1
)); ptr
+= sizeof(m_dy1
);
207 memcpy(ptr
, &m_kx
, sizeof(m_kx
)); ptr
+= sizeof(m_kx
);
208 memcpy(ptr
, &m_ky
, sizeof(m_ky
)); ptr
+= sizeof(m_ky
);
211 void deserialize(const int8u
* ptr
)
213 memcpy(&m_world_x1
, ptr
, sizeof(m_world_x1
)); ptr
+= sizeof(m_world_x1
);
214 memcpy(&m_world_y1
, ptr
, sizeof(m_world_y1
)); ptr
+= sizeof(m_world_y1
);
215 memcpy(&m_world_x2
, ptr
, sizeof(m_world_x2
)); ptr
+= sizeof(m_world_x2
);
216 memcpy(&m_world_y2
, ptr
, sizeof(m_world_y2
)); ptr
+= sizeof(m_world_y2
);
217 memcpy(&m_device_x1
, ptr
, sizeof(m_device_x1
)); ptr
+= sizeof(m_device_x1
);
218 memcpy(&m_device_y1
, ptr
, sizeof(m_device_y1
)); ptr
+= sizeof(m_device_y1
);
219 memcpy(&m_device_x2
, ptr
, sizeof(m_device_x2
)); ptr
+= sizeof(m_device_x2
);
220 memcpy(&m_device_y2
, ptr
, sizeof(m_device_y2
)); ptr
+= sizeof(m_device_y2
);
221 memcpy(&m_aspect
, ptr
, sizeof(m_aspect
)); ptr
+= sizeof(m_aspect
);
222 memcpy(&m_align_x
, ptr
, sizeof(m_align_x
)); ptr
+= sizeof(m_align_x
);
223 memcpy(&m_align_y
, ptr
, sizeof(m_align_y
)); ptr
+= sizeof(m_align_y
);
224 memcpy(&m_wx1
, ptr
, sizeof(m_wx1
)); ptr
+= sizeof(m_wx1
);
225 memcpy(&m_wy1
, ptr
, sizeof(m_wy1
)); ptr
+= sizeof(m_wy1
);
226 memcpy(&m_wx2
, ptr
, sizeof(m_wx2
)); ptr
+= sizeof(m_wx2
);
227 memcpy(&m_wy2
, ptr
, sizeof(m_wy2
)); ptr
+= sizeof(m_wy2
);
228 memcpy(&m_dx1
, ptr
, sizeof(m_dx1
)); ptr
+= sizeof(m_dx1
);
229 memcpy(&m_dy1
, ptr
, sizeof(m_dy1
)); ptr
+= sizeof(m_dy1
);
230 memcpy(&m_kx
, ptr
, sizeof(m_kx
)); ptr
+= sizeof(m_kx
);
231 memcpy(&m_ky
, ptr
, sizeof(m_ky
)); ptr
+= sizeof(m_ky
);
245 aspect_ratio_e m_aspect
;
260 //-----------------------------------------------------------------------
261 inline void trans_viewport::update()
263 double world_x1
= m_world_x1
;
264 double world_y1
= m_world_y1
;
265 double world_x2
= m_world_x2
;
266 double world_y2
= m_world_y2
;
267 double device_x1
= m_device_x1
;
268 double device_y1
= m_device_y1
;
269 double device_x2
= m_device_x2
;
270 double device_y2
= m_device_y2
;
271 if(m_aspect
!= aspect_ratio_stretch
)
274 m_kx
= (device_x2
- device_x1
) / (world_x2
- world_x1
);
275 m_ky
= (device_y2
- device_y1
) / (world_y2
- world_y1
);
277 if((m_aspect
== aspect_ratio_meet
) == (m_kx
< m_ky
))
279 d
= (world_y2
- world_y1
) * m_ky
/ m_kx
;
280 world_y1
+= (world_y2
- world_y1
- d
) * m_align_y
;
281 world_y2
= world_y1
+ d
;
285 d
= (world_x2
- world_x1
) * m_kx
/ m_ky
;
286 world_x1
+= (world_x2
- world_x1
- d
) * m_align_x
;
287 world_x2
= world_x1
+ d
;
296 m_kx
= (device_x2
- device_x1
) / (world_x2
- world_x1
);
297 m_ky
= (device_y2
- device_y1
) / (world_y2
- world_y1
);