]> git.saurik.com Git - wxWidgets.git/blob - tests/graphics/affinematrix.cpp
c6a0696241f0f0b8d52a3cf9ab43f07263dd0c5c
[wxWidgets.git] / tests / graphics / affinematrix.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/affinetransform.cpp
3 // Purpose: Unit test for transformations implemented for wxAffineMatrix2D
4 // Author: Catalin Raceanu
5 // Created: 2011-04-14
6 // Copyright: (c) 2011 wxWidgets development team
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/dcmemory.h"
20 #include "wx/affinematrix2d.h"
21 #include "wx/math.h"
22
23 #include "testimage.h"
24
25 // ----------------------------------------------------------------------------
26 // test class
27 // ----------------------------------------------------------------------------
28
29 class AffineTransformTestCase : public CppUnit::TestCase
30 {
31 public:
32 AffineTransformTestCase()
33 {
34 wxImage::AddHandler(new wxJPEGHandler);
35 }
36
37 virtual void setUp();
38
39 private:
40 CPPUNIT_TEST_SUITE( AffineTransformTestCase );
41 CPPUNIT_TEST( InvertMatrix );
42 #if wxUSE_DC_TRANSFORM_MATRIX
43 CPPUNIT_TEST( VMirrorAndTranslate );
44 CPPUNIT_TEST( Rotate90Clockwise );
45 #endif // wxUSE_DC_TRANSFORM_MATRIX
46 CPPUNIT_TEST_SUITE_END();
47
48 void InvertMatrix();
49 #if wxUSE_DC_TRANSFORM_MATRIX
50 void VMirrorAndTranslate();
51 void Rotate90Clockwise();
52
53 wxImage m_imgOrig;
54 wxBitmap m_bmpOrig;
55 #endif // wxUSE_DC_TRANSFORM_MATRIX
56
57 DECLARE_NO_COPY_CLASS(AffineTransformTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( AffineTransformTestCase );
62
63 // also include in it's own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AffineTransformTestCase, "AffineTransformTestCase" );
65
66 void AffineTransformTestCase::setUp()
67 {
68 #if wxUSE_DC_TRANSFORM_MATRIX
69 m_imgOrig.LoadFile("horse.jpg");
70
71 CPPUNIT_ASSERT( m_imgOrig.IsOk() );
72
73 m_bmpOrig = wxBitmap(m_imgOrig);
74 #endif // wxUSE_DC_TRANSFORM_MATRIX
75 }
76
77 void AffineTransformTestCase::InvertMatrix()
78 {
79 wxAffineMatrix2D matrix1;
80 matrix1.Set(wxMatrix2D(2, 1, 1, 1), wxPoint2DDouble(1, 1));
81
82 wxAffineMatrix2D matrix2(matrix1);
83
84 matrix2.Invert();
85
86 wxMatrix2D m;
87 wxPoint2DDouble p;
88 matrix2.Get(&m, &p);
89 CPPUNIT_ASSERT_EQUAL( 1, (int)m.m_11 );
90 CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_12 );
91 CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_21 );
92 CPPUNIT_ASSERT_EQUAL( 2, (int)m.m_22 );
93 CPPUNIT_ASSERT_EQUAL( 0, (int)p.m_x );
94 CPPUNIT_ASSERT_EQUAL( -1, (int)p.m_y );
95
96 matrix2.Concat(matrix1);
97 CPPUNIT_ASSERT( matrix2.IsIdentity() );
98 }
99
100 #if wxUSE_DC_TRANSFORM_MATRIX
101
102 void AffineTransformTestCase::VMirrorAndTranslate()
103 {
104 wxBitmap bmpUsingMatrix(m_bmpOrig.GetWidth(), m_bmpOrig.GetHeight());
105
106 // build the mirrored image using the transformation matrix
107 {
108 wxMemoryDC dc(bmpUsingMatrix);
109
110 if ( !dc.CanUseTransformMatrix() )
111 return;
112
113 wxAffineMatrix2D matrix;
114 matrix.Mirror(wxVERTICAL);
115 matrix.Translate(0, m_bmpOrig.GetHeight() - 1);
116 dc.SetTransformMatrix(matrix);
117 dc.DrawBitmap(m_bmpOrig, 0, 0);
118 }
119
120 CPPUNIT_ASSERT_EQUAL( bmpUsingMatrix.ConvertToImage(),
121 m_imgOrig.Mirror(false) );
122 }
123
124 void AffineTransformTestCase::Rotate90Clockwise()
125 {
126 wxBitmap bmpUsingMatrix(m_bmpOrig.GetHeight(), m_bmpOrig.GetWidth());
127
128 // build the rotated image using the transformation matrix
129 {
130 wxMemoryDC dc(bmpUsingMatrix);
131
132 if ( !dc.CanUseTransformMatrix() )
133 return;
134
135 wxAffineMatrix2D matrix;
136 matrix.Rotate(-0.5 * M_PI);
137 matrix.Translate(m_bmpOrig.GetHeight(), 0);
138 dc.SetTransformMatrix(matrix);
139 dc.DrawBitmap(m_bmpOrig, 0, 0);
140 }
141
142 CPPUNIT_ASSERT_EQUAL( bmpUsingMatrix.ConvertToImage(),
143 m_imgOrig.Rotate90(true) );
144 }
145
146 #endif // wxUSE_DC_TRANSFORM_MATRIX