File: affinematrix.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (537 lines) | stat: -rw-r--r-- 16,013 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/graphics/affinetransform.cpp
// Purpose:     Unit test for transformations implemented for wxAffineMatrix2D
// Author:      Catalin Raceanu
// Created:     2011-04-14
// Copyright:   (c) 2011 wxWidgets development team
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"


#include "wx/graphics.h"
#include "wx/dcmemory.h"
#include "wx/affinematrix2d.h"
#include "wx/math.h"
#if wxUSE_GRAPHICS_CONTEXT
#include "wx/dcgraph.h"
#endif // wxUSE_GRAPHICS_CONTEXT

#include "testimage.h"

// ----------------------------------------------------------------------------
// Affine transform test class
// ----------------------------------------------------------------------------

class AffineTransformTestCase : public CppUnit::TestCase
{
public:
    AffineTransformTestCase() {}

private:
    CPPUNIT_TEST_SUITE( AffineTransformTestCase );
        CPPUNIT_TEST( InvertMatrix );
        CPPUNIT_TEST( Concat );
    CPPUNIT_TEST_SUITE_END();

    void InvertMatrix();
    void Concat();

    wxDECLARE_NO_COPY_CLASS(AffineTransformTestCase);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( AffineTransformTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AffineTransformTestCase, "AffineTransformTestCase" );

void AffineTransformTestCase::InvertMatrix()
{
    wxAffineMatrix2D matrix1;
    matrix1.Set(wxMatrix2D(2, 1, 1, 1), wxPoint2DDouble(1, 1));

    wxAffineMatrix2D matrix2(matrix1);

    matrix2.Invert();

    wxMatrix2D m;
    wxPoint2DDouble p;
    matrix2.Get(&m, &p);
    CPPUNIT_ASSERT_EQUAL( 1, (int)m.m_11 );
    CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_12 );
    CPPUNIT_ASSERT_EQUAL( -1, (int)m.m_21 );
    CPPUNIT_ASSERT_EQUAL( 2, (int)m.m_22 );
    CPPUNIT_ASSERT_EQUAL( 0, (int)p.m_x );
    CPPUNIT_ASSERT_EQUAL( -1, (int)p.m_y );

    matrix2.Concat(matrix1);
    CPPUNIT_ASSERT( matrix2.IsIdentity() );
}

void AffineTransformTestCase::Concat()
{
    wxAffineMatrix2D m1;
    m1.Set(wxMatrix2D(0.9, 0.4, -0.4, 0.9), wxPoint2DDouble(0.0, 0.0));
    wxAffineMatrix2D m2;
    m2.Set(wxMatrix2D(1.0, 0.0, 0.0, 1.0), wxPoint2DDouble(3.0, 5.0));
    m1.Concat(m2);

    wxMatrix2D m;
    wxPoint2DDouble p;
    m1.Get(&m, &p);

    const double delta = 0.01;
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9, m.m_11, delta );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.4, m.m_12, delta );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( -0.4, m.m_21, delta );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.9, m.m_22, delta );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.7, p.m_x, delta );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 5.7, p.m_y, delta );
}

#if wxUSE_DC_TRANSFORM_MATRIX
// -------------------------------
//  Transform matrix test classes
// -------------------------------

// ====================
// wxDC / wxGCDC tests
// ====================

class TransformMatrixTestCaseDCBase : public CppUnit::TestCase
{
public:
    TransformMatrixTestCaseDCBase()
    {
        m_dc = NULL;
        wxImage::AddHandler(new wxJPEGHandler);
        m_imgOrig.LoadFile(wxS("horse.jpg"));
        CPPUNIT_ASSERT( m_imgOrig.IsOk() );
    }

    virtual ~TransformMatrixTestCaseDCBase()
    {
    }

    virtual void setUp() wxOVERRIDE
    {
        m_bmpOrig = wxBitmap(m_imgOrig);
        m_bmpUsingMatrix.Create(m_bmpOrig.GetSize(), m_bmpOrig.GetDepth());
    }

protected:
    virtual void FlushDC() = 0;

    void VMirrorAndTranslate();
    void Rotate90Clockwise();
#if wxUSE_GRAPHICS_CONTEXT
    void CompareToGraphicsContext();
#endif // wxUSE_GRAPHICS_CONTEXT

protected:
    wxImage m_imgOrig;
    wxBitmap m_bmpOrig;

    wxBitmap m_bmpUsingMatrix;
    wxDC* m_dc;

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseDCBase);
};

// ===========
// wxDC tests
// ===========

class TransformMatrixTestCaseDC : public TransformMatrixTestCaseDCBase
{
public:
    TransformMatrixTestCaseDC()
    {
        m_dc = &m_mdc;
    }

    virtual ~TransformMatrixTestCaseDC()
    {
    }

    virtual void setUp() wxOVERRIDE
    {
        TransformMatrixTestCaseDCBase::setUp();
        m_mdc.SelectObject(m_bmpUsingMatrix);
    }

    virtual void tearDown() wxOVERRIDE
    {
        m_mdc.SelectObject(wxNullBitmap);
        TransformMatrixTestCaseDCBase::tearDown();
    }

protected:
    virtual void FlushDC() wxOVERRIDE {}

private:
    CPPUNIT_TEST_SUITE( TransformMatrixTestCaseDC );
        CPPUNIT_TEST( VMirrorAndTranslate );
        CPPUNIT_TEST( Rotate90Clockwise );
#if wxUSE_GRAPHICS_CONTEXT
        CPPUNIT_TEST( CompareToGraphicsContext );
#endif // wxUSE_GRAPHICS_CONTEXT
    CPPUNIT_TEST_SUITE_END();

protected:
    wxMemoryDC m_mdc;

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseDC);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TransformMatrixTestCaseDC );

// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TransformMatrixTestCaseDC, "TransformMatrixTestCaseDC" );

#if wxUSE_GRAPHICS_CONTEXT
// =============
// wxGCDC tests
// =============

class TransformMatrixTestCaseGCDC : public TransformMatrixTestCaseDC
{
public:
    TransformMatrixTestCaseGCDC() {}

    virtual ~TransformMatrixTestCaseGCDC() {}

    virtual void setUp() wxOVERRIDE
    {
        TransformMatrixTestCaseDC::setUp();

        m_gcdc = new wxGCDC(m_mdc);
        m_dc = m_gcdc;

        wxGraphicsContext* ctx = m_gcdc->GetGraphicsContext();
        ctx->SetAntialiasMode(wxANTIALIAS_NONE);
    }

    virtual void tearDown() wxOVERRIDE
    {
        delete m_gcdc;
        TransformMatrixTestCaseDC::tearDown();
    }

protected:
    virtual void FlushDC() wxOVERRIDE
    {
        m_gcdc->GetGraphicsContext()->Flush();
    }

private:
    CPPUNIT_TEST_SUITE( TransformMatrixTestCaseGCDC );
        CPPUNIT_TEST( VMirrorAndTranslate );
        CPPUNIT_TEST( Rotate90Clockwise );
    CPPUNIT_TEST_SUITE_END();

protected:
    wxGCDC* m_gcdc;

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseGCDC);
};

// For MSW we have individual test cases for each graphics renderer
// so we don't need to test wxGCDC with default renderer.
#ifndef __WXMSW__
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TransformMatrixTestCaseGCDC );

// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TransformMatrixTestCaseGCDC, "TransformMatrixTestCaseGCDC" );
#endif // !__WXMSW__

#ifdef __WXMSW__
// GDI+ and Direct2D are available only under MSW.

#if wxUSE_GRAPHICS_GDIPLUS
class TransformMatrixTestCaseGCDCGDIPlus : public TransformMatrixTestCaseGCDC
{
public:
    TransformMatrixTestCaseGCDCGDIPlus() {}

    virtual ~TransformMatrixTestCaseGCDCGDIPlus() {}

    virtual void setUp() wxOVERRIDE
    {
        TransformMatrixTestCaseGCDC::setUp();

        wxGraphicsRenderer* rend = wxGraphicsRenderer::GetGDIPlusRenderer();
        wxGraphicsContext* ctx = rend->CreateContext(m_mdc);
        m_gcdc->SetGraphicsContext(ctx);
    }

private:
    CPPUNIT_TEST_SUITE( TransformMatrixTestCaseGCDCGDIPlus );
        CPPUNIT_TEST( VMirrorAndTranslate );
        CPPUNIT_TEST( Rotate90Clockwise );
    CPPUNIT_TEST_SUITE_END();

protected:

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseGCDCGDIPlus);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TransformMatrixTestCaseGCDCGDIPlus );

// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TransformMatrixTestCaseGCDCGDIPlus, "TransformMatrixTestCaseGCDCGDIPlus" );

#endif // wxUSE_GRAPHICS_GDIPLUS

#if wxUSE_GRAPHICS_DIRECT2D
class TransformMatrixTestCaseGCDCDirect2D : public TransformMatrixTestCaseGCDC
{
public:
    TransformMatrixTestCaseGCDCDirect2D() {}

    virtual ~TransformMatrixTestCaseGCDCDirect2D() {}

    virtual void setUp() wxOVERRIDE
    {
        TransformMatrixTestCaseGCDC::setUp();

        wxGraphicsRenderer* rend = wxGraphicsRenderer::GetDirect2DRenderer();
        wxGraphicsContext* ctx = rend->CreateContext(m_mdc);
        m_gcdc->SetGraphicsContext(ctx);
    }

    virtual void FlushDC() wxOVERRIDE
    {
        // Apparently, flushing native Direct2D renderer
        // is not enough to update underlying DC (bitmap)
        // and therefore we have to destroy the renderer
        // to do so.
        TransformMatrixTestCaseGCDC::FlushDC();
        m_gcdc->SetGraphicsContext(NULL);
    }

private:
    CPPUNIT_TEST_SUITE( TransformMatrixTestCaseGCDCDirect2D );
        CPPUNIT_TEST( VMirrorAndTranslate );
        CPPUNIT_TEST( Rotate90Clockwise );
    CPPUNIT_TEST_SUITE_END();

protected:

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseGCDCDirect2D);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TransformMatrixTestCaseGCDCDirect2D );

// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TransformMatrixTestCaseGCDCDirect2D, "TransformMatrixTestCaseGCDCDirect2D" );

#endif // wxUSE_GRAPHICS_DIRECT2D

#endif // __WXMSW__

#if wxUSE_CAIRO
class TransformMatrixTestCaseGCDCCairo : public TransformMatrixTestCaseGCDC
{
public:
    TransformMatrixTestCaseGCDCCairo() {}

    virtual ~TransformMatrixTestCaseGCDCCairo() {}

    virtual void setUp() wxOVERRIDE
    {
        TransformMatrixTestCaseGCDC::setUp();

        wxGraphicsRenderer* rend = wxGraphicsRenderer::GetCairoRenderer();
        wxGraphicsContext* ctx = rend->CreateContext(m_mdc);
        m_gcdc->SetGraphicsContext(ctx);
    }

private:
    CPPUNIT_TEST_SUITE( TransformMatrixTestCaseGCDCCairo );
        CPPUNIT_TEST( VMirrorAndTranslate );
        CPPUNIT_TEST( Rotate90Clockwise );
    CPPUNIT_TEST_SUITE_END();

protected:

    wxDECLARE_NO_COPY_CLASS(TransformMatrixTestCaseGCDCCairo);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TransformMatrixTestCaseGCDCCairo );

// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TransformMatrixTestCaseGCDCCairo, "TransformMatrixTestCaseGCDCCairo" );

#endif // wxUSE_CAIRO

#endif // wxUSE_GRAPHICS_CONTEXT

void TransformMatrixTestCaseDCBase::VMirrorAndTranslate()
{
    // build the mirrored image using the transformation matrix
    if ( !m_dc->CanUseTransformMatrix() )
        return;

    wxAffineMatrix2D matrix;
    matrix.Mirror(wxVERTICAL);
    // For wxDC pixel center is at (0, 0) so row 0 of the bitmap is the axis
    // of mirroring and it is left intact by the transformation. In this case
    // mirrored bitmap needs to be shifted by dim-1 pixels.
    // For wxGCDC pixel center of underlying wxGraphicsContext is at (0.5, 0.5)
    // so the axis of mirroring is above row 0 of the bitmap and this row
    // is affected by the transformation. In this case mirrored bitmap
    // needs to be shifthed by dim pixels.
    int ty;
#if wxUSE_GRAPHICS_CONTEXT
    if ( m_dc->GetGraphicsContext() )
        ty = m_bmpOrig.GetHeight();
    else
#endif // wxUSE_GRAPHICS_CONTEXT
        ty = m_bmpOrig.GetHeight() - 1;
    matrix.Translate(0, -ty);
    m_dc->SetTransformMatrix(matrix);
    m_dc->DrawBitmap(m_bmpOrig, 0, 0);
    FlushDC();

    CHECK_THAT( m_bmpUsingMatrix.ConvertToImage(),
                RGBSameAs(m_imgOrig.Mirror(false)) );
}

void TransformMatrixTestCaseDCBase::Rotate90Clockwise()
{
    // build the rotated image using the transformation matrix
    if ( !m_dc->CanUseTransformMatrix() )
        return;

    wxAffineMatrix2D matrix;
    matrix.Rotate(0.5 * M_PI);
    matrix.Translate(0, -m_bmpOrig.GetHeight());
    m_dc->SetTransformMatrix(matrix);
    m_dc->DrawBitmap(m_bmpOrig, 0, 0);
    FlushDC();

    CHECK_THAT( m_bmpUsingMatrix.ConvertToImage(),
                RGBSameAs(m_imgOrig.Rotate90(true)) );
}

#if wxUSE_GRAPHICS_CONTEXT
void TransformMatrixTestCaseDCBase::CompareToGraphicsContext()
{
    wxPoint2DDouble pointA1(1.0, 3.0), pointA2(60.0, 50.0),
                    pointG1(1.0, 3.0), pointG2(60.0, 50.0);

    // Create affine matrix and transform it
    wxAffineMatrix2D matrixA1, matrixA2;
    matrixA2.Rotate(M_PI / 3);
    matrixA1.Translate(-m_bmpOrig.GetWidth()/2, -m_bmpOrig.GetHeight()/2);
    matrixA1.Rotate(-M_PI *2/ 6);
    matrixA1.Translate(m_bmpOrig.GetWidth()/2, m_bmpOrig.GetHeight()/2);
    matrixA1.Mirror(wxHORIZONTAL);
    matrixA1.Concat(matrixA2);
    matrixA1.Mirror(wxVERTICAL);
    matrixA1.Translate(m_bmpOrig.GetWidth()/2, -m_bmpOrig.GetHeight()/2);
    matrixA1.Scale(0.9, 0.9);
    matrixA1.Invert();

    // Create image using first matrix
    wxBitmap bmpUsingMatrixA1(m_bmpOrig.GetSize(), m_bmpOrig.GetDepth());

    // Build the transformed image using the transformation matrix
    {
        wxMemoryDC dc(bmpUsingMatrixA1);

        if ( !dc.CanUseTransformMatrix() )
            return;

        // Draw the bitmap
        dc.SetTransformMatrix(matrixA1);
        dc.DrawBitmap(m_bmpOrig, 0, 0);

        // Draw a line
        matrixA1.TransformPoint(&pointA1.m_x, &pointA1.m_y);
        matrixA1.TransformDistance(&pointA2.m_x, &pointA2.m_y);

        dc.DrawLine(wxRound(pointA1.m_x), wxRound(pointA1.m_y),
            wxRound(pointA1.m_x + pointA2.m_x), wxRound(pointA1.m_x + pointA2.m_y));
    }


    // Create graphics matrix and transform it
    wxGraphicsRenderer* r;
    if ( m_dc->GetGraphicsContext() )
    {
        r = m_dc->GetGraphicsContext()->GetRenderer();
    }
    else
    {
        r = wxGraphicsRenderer::GetDefaultRenderer();
    }

    wxBitmap bmp(10, 10);
    wxMemoryDC mDc(bmp);
    wxGraphicsContext* gDc = r->CreateContext(mDc);
    wxGraphicsMatrix matrixG1 = gDc->CreateMatrix();
    wxGraphicsMatrix matrixG2 = gDc->CreateMatrix();
    matrixG2.Rotate(M_PI / 3);
    matrixG1.Translate(-m_bmpOrig.GetWidth()/2, -m_bmpOrig.GetHeight()/2);
    matrixG1.Rotate(-M_PI*2 / 6);
    matrixG1.Translate(m_bmpOrig.GetWidth()/2, m_bmpOrig.GetHeight()/2);
    matrixG1.Scale(-1, 1);
    matrixG1.Concat(matrixG2);
    matrixG1.Scale(1, -1);
    matrixG1.Translate(m_bmpOrig.GetWidth()/2, -m_bmpOrig.GetHeight()/2);
    matrixG1.Scale(0.9, 0.9);
    matrixG1.Invert();
    // Create affine matrix from the graphics matrix
    wxMatrix2D mat2D;
    wxPoint2DDouble tr;
    matrixG1.Get(&mat2D.m_11, &mat2D.m_12, &mat2D.m_21, &mat2D.m_22, &tr.m_x, &tr.m_y);
    wxAffineMatrix2D matrixAG;
    matrixAG.Set(mat2D, tr);

    delete gDc;

    // Create image using last matrix
    wxBitmap bmpUsingMatrixAG(m_bmpOrig.GetHeight(), m_bmpOrig.GetWidth());

    // Build the transformed image using the transformation matrix
    {
        wxMemoryDC dc(bmpUsingMatrixAG);

        if ( !dc.CanUseTransformMatrix() )
            return;

        // Draw the bitmap
        dc.SetTransformMatrix(matrixAG);
        dc.DrawBitmap(m_bmpOrig, 0, 0);

        // Draw a line
        matrixG1.TransformPoint(&pointG1.m_x, &pointG1.m_y);
        matrixG1.TransformDistance(&pointG2.m_x, &pointG2.m_y);

        dc.DrawLine(wxRound(pointG1.m_x), wxRound(pointG1.m_y),
            wxRound(pointG1.m_x + pointG2.m_x), wxRound(pointG1.m_x + pointG2.m_y));
    }


    CHECK_THAT( bmpUsingMatrixA1.ConvertToImage(),
                RGBSameAs(bmpUsingMatrixAG.ConvertToImage()) );

    // Save the images to check that something _is_ inside the visible area.
    //bmpUsingMatrixA1.SaveFile("matrixA1.jpg", wxBITMAP_TYPE_JPEG);
    //bmpUsingMatrixAG.SaveFile("matrixAG.jpg", wxBITMAP_TYPE_JPEG);
}
#endif // wxUSE_GRAPHICS_CONTEXT

#endif // wxUSE_DC_TRANSFORM_MATRIX