| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.4 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (McSeem) |
| 4 |
// |
| 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. |
| 9 |
// |
| 10 |
//---------------------------------------------------------------------------- |
| 11 |
// Contact: [email protected] |
| 12 |
// [email protected] |
| 13 |
// http://www.antigrain.com |
| 14 |
//---------------------------------------------------------------------------- |
| 15 |
|
| 16 |
IMPORTANT! |
| 17 |
Copy spheres.bmp from ..\..\art before running this example. |
| 18 |
|
| 19 |
|
| 20 |
Using affine transformations for images looks tricky, but it's not, especially |
| 21 |
when you understand the main idea. You can apply any affine transformations |
| 22 |
to images of any size and draw any part of the image. |
| 23 |
|
| 24 |
This example demonstrates the ideas of constructing affine matrices for |
| 25 |
images. The example contains 6 variants of scaling/rotation/translation |
| 26 |
matrices. Also, there are the following important variables: |
| 27 |
|
| 28 |
m_polygon_angle; |
| 29 |
m_polygon_scale; |
| 30 |
|
| 31 |
m_image_angle; |
| 32 |
m_image_scale; |
| 33 |
|
| 34 |
m_image_center_x; |
| 35 |
m_image_center_y; |
| 36 |
|
| 37 |
m_polygon_cx; |
| 38 |
m_polygon_cy; |
| 39 |
|
| 40 |
m_image_cx; |
| 41 |
m_image_cy; |
| 42 |
|
| 43 |
|
| 44 |
Variables m_polygon_... refer to the source path (star) that will be used to |
| 45 |
transform the image, variables m_image_... refer to the image parametres. |
| 46 |
Actually, different variants of transformations use different variables |
| 47 |
(in some cases m_polygon_... is used to create the image affine matrix). |
| 48 |
The meaning of the variables is the following: |
| 49 |
|
| 50 |
m_polygon_angle; |
| 51 |
m_polygon_scale; |
| 52 |
|
| 53 |
Are actually two slider controls on the left. |
| 54 |
|
| 55 |
m_polygon_cx; |
| 56 |
m_polygon_cy; |
| 57 |
|
| 58 |
Are the center of the "star", that is the geometric center of the source path. |
| 59 |
You can drag the "star" with the left mouse button. |
| 60 |
|
| 61 |
m_image_angle; |
| 62 |
m_image_scale; |
| 63 |
|
| 64 |
are two respective sliders on the right. |
| 65 |
|
| 66 |
m_image_cx; |
| 67 |
m_image_cy; |
| 68 |
|
| 69 |
are the coordinates of the green marker. The marker can also be dragged. |
| 70 |
|
| 71 |
m_image_center_x; |
| 72 |
m_image_center_y; |
| 73 |
|
| 74 |
are the center of the image. You can consider them as constants. |
| 75 |
In certain cases it's easier to understand the idea when we have some |
| 76 |
"reference point", like the center or the origin of the axes. |
| 77 |
|
| 78 |
The image transformation matrix doesn't depend on anything else. |
| 79 |
It means that the code above the line "// --------------(Example 0)": |
| 80 |
|
| 81 |
polygon_mtx *= agg::trans_affine_translation(-m_polygon_cx, -m_polygon_cy); |
| 82 |
polygon_mtx *= agg::trans_affine_rotation(m_polygon_angle.value() * agg::pi / 180.0); |
| 83 |
polygon_mtx *= agg::trans_affine_scaling(m_polygon_scale.value()); |
| 84 |
polygon_mtx *= agg::trans_affine_translation(m_polygon_cx, m_polygon_cy); |
| 85 |
|
| 86 |
affects only the drawn path. So, the only way to shift, rotate, or scale the |
| 87 |
image is to use the image affine matrix (image_mtx in this example). |
| 88 |
|
| 89 |
Another important thing is that due to the nature of the algorithm you |
| 90 |
have to use the inverse transformations. The algorithm takes the |
| 91 |
coordinates of every destination pixel, applies the transformations and |
| 92 |
obtains the coordinates of the pixel to pick it up from the source image. |
| 93 |
The coordinates of the destination pixels are always known and they have |
| 94 |
regular, pixel accuracy. After transforming they usually fall somewhere " |
| 95 |
between" pixels. This fact allows us to apply anti-aliasing filters like |
| 96 |
bilinear, bicubic, sinc, blackman, etc. After filtering we know the value of the |
| 97 |
destination pixel and we can put it in its place. This is why the algorithm |
| 98 |
uses the inverse affine matrix. In other words you prepare the transformation |
| 99 |
matrix as usual and then invret it before using: |
| 100 |
|
| 101 |
image_mtx *= agg::trans_affine_translation(-m_image_center_x, -m_image_center_y); |
| 102 |
image_mtx *= agg::trans_affine_rotation(m_polygon_angle.value() * agg::pi / 180.0); |
| 103 |
image_mtx *= agg::trans_affine_scaling(m_polygon_scale.value()); |
| 104 |
image_mtx *= agg::trans_affine_translation(m_polygon_cx, m_polygon_cy); |
| 105 |
image_mtx.invert(); |
| 106 |
|
| 107 |
This code illustrates the behaviour when we have synchronous transformations |
| 108 |
of the source path and the image. |
| 109 |
|
| 110 |
Well, let us return to example 0. Here we have a "star" that can be dragged with |
| 111 |
the left mouse button, a small round green marker, and four sliders. |
| 112 |
The marker and the sliders on the right don't affect anything. As it was said, |
| 113 |
this example simply copies pixels from the source image to the destination canvas. |
| 114 |
|
| 115 |
Example 1. The marker and the image sliders on the right still |
| 116 |
don't work, but now the image is moved, scaled, and rotated syncronously with |
| 117 |
the "star". We simply take the reference point, which is m_image_center_x(y), |
| 118 |
rotate and scale the image around it, and then, translate the image to |
| 119 |
m_polygon_cx(cy). |
| 120 |
|
| 121 |
|
| 122 |
Example 2 is the same as 1 but instead of using "m_polygon_..." parameters we |
| 123 |
use "m_image_..." ones, so the marker and the sliders on the right now work |
| 124 |
independently. In other words you can control the image and the "star" separately. |
| 125 |
image_mtx *= agg::trans_affine_translation(-m_image_center_x, -m_image_center_y); |
| 126 |
image_mtx *= agg::trans_affine_rotation(m_image_angle.value() * agg::pi / 180.0); |
| 127 |
image_mtx *= agg::trans_affine_scaling(m_image_scale.value()); |
| 128 |
image_mtx *= agg::trans_affine_translation(m_image_cx, m_image_cy); |
| 129 |
image_mtx.invert(); |
| 130 |
|
| 131 |
|
| 132 |
Example 3 is the same as the above but instead of using "m_image_cx(cy)" we use |
| 133 |
m_polygon_cx(cy). So that, the image is rotated and scaled around its center, |
| 134 |
but the marker doesn't have any effect. |
| 135 |
image_mtx *= agg::trans_affine_translation(-m_image_center_x, -m_image_center_y); |
| 136 |
image_mtx *= agg::trans_affine_rotation(m_image_angle.value() * agg::pi / 180.0); |
| 137 |
image_mtx *= agg::trans_affine_scaling(m_image_scale.value()); |
| 138 |
image_mtx *= agg::trans_affine_translation(m_polygon_cx, m_polygon_cy); |
| 139 |
image_mtx.invert(); |
| 140 |
|
| 141 |
|
| 142 |
Example 4 is the same as 1, but we use m_image_cx(cy) as the source point |
| 143 |
in the image. So, the image sliders don't work, the image is transformed |
| 144 |
synchronously with the path, but we are able to choose the source point for |
| 145 |
image transformations. That is the idea: we take the source point in the |
| 146 |
image, perform the transformations around it and then move this source |
| 147 |
point to the center of the "star". |
| 148 |
image_mtx *= agg::trans_affine_translation(-m_image_cx, -m_image_cy); |
| 149 |
image_mtx *= agg::trans_affine_rotation(m_polygon_angle.value() * agg::pi / 180.0); |
| 150 |
image_mtx *= agg::trans_affine_scaling(m_polygon_scale.value()); |
| 151 |
image_mtx *= agg::trans_affine_translation(m_polygon_cx, m_polygon_cy); |
| 152 |
image_mtx.invert(); |
| 153 |
|
| 154 |
|
| 155 |
Example 5 is the same as 2, but there we have a combination of the scaling |
| 156 |
and rotation of the "star" and the image. |
| 157 |
image_mtx *= agg::trans_affine_translation(-m_image_center_x, -m_image_center_y); |
| 158 |
image_mtx *= agg::trans_affine_rotation(m_image_angle.value() * agg::pi / 180.0); |
| 159 |
image_mtx *= agg::trans_affine_rotation(m_polygon_angle.value() * agg::pi / 180.0); |
| 160 |
image_mtx *= agg::trans_affine_scaling(m_image_scale.value()); |
| 161 |
image_mtx *= agg::trans_affine_scaling(m_polygon_scale.value()); |
| 162 |
image_mtx *= agg::trans_affine_translation(m_image_cx, m_image_cy); |
| 163 |
image_mtx.invert(); |
| 164 |
BTW, code above can be simplified like this: |
| 165 |
image_mtx *= agg::trans_affine_translation(-m_image_center_x, -m_image_center_y); |
| 166 |
image_mtx *= agg::trans_affine_rotation(m_image_angle.value() * agg::pi / 180.0 + |
| 167 |
m_polygon_angle.value() * agg::pi / 180.0); |
| 168 |
image_mtx *= agg::trans_affine_scaling(m_image_scale.value() * m_polygon_scale.value()); |
| 169 |
image_mtx *= agg::trans_affine_translation(m_image_cx, m_image_cy); |
| 170 |
image_mtx.invert(); |
| 171 |
|
| 172 |
|
| 173 |
Finally, example 5 is probably not very interesting in practice, but still, it |
| 174 |
can simplify understanding of the idea. This example uses only m_image_... |
| 175 |
parameters. It shifts the image from m_image_cx(cy) to the origin (0,0), then |
| 176 |
applies rotation and scaling, and finally, shifts the image back. So that, |
| 177 |
point m_image_cx(cy) is simply the center of the transformations. When the |
| 178 |
image angle is 0.0 and the scale is 1.0 dragging this point doesn't have any |
| 179 |
effect. |
| 180 |
image_mtx *= agg::trans_affine_translation(-m_image_cx, -m_image_cy); |
| 181 |
image_mtx *= agg::trans_affine_rotation(m_image_angle.value() * agg::pi / 180.0); |
| 182 |
image_mtx *= agg::trans_affine_scaling(m_image_scale.value()); |
| 183 |
image_mtx *= agg::trans_affine_translation(m_image_cx, m_image_cy); |
| 184 |
image_mtx.invert(); |
| 185 |
|
| 186 |
|
| 187 |
Many thank to you if you read it and many special thanks if you could understand |
| 188 |
something. :-) |
| 189 |
|