summaryrefslogtreecommitdiff
path: root/src/player/testcalibrator.cpp
blob: a9898f54add43f82a911a12d507c2c2333d6ffac (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "TrackerCalibrator.h"

#include "../base/GLMHelper.h"

#include "../base/TestSuite.h"
#include "../base/Exception.h"
#include "../base/Logger.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace avg;
using namespace std;

class CalibratorTest: public Test {
public:
    CalibratorTest()
        : Test("CalibratorTest", 2)
    {
    }

    void runTests() 
    {
        DeDistortPtr pTrafo;
        {
            TrackerCalibrator calibrator(IntPoint(640, 480), IntPoint(640,480));
            bool bDone = false;
            while (!bDone) {
                IntPoint displayPoint(calibrator.getDisplayPoint());
                calibrator.setCamPoint(glm::vec2(displayPoint));
                bDone = !calibrator.nextPoint();
            }
            pTrafo = calibrator.makeTransformer();
            TEST(glm::length(pTrafo->transformBlobToScreen(glm::dvec2(1.00,1.00)) - 
                    glm::dvec2(1.00,1.00)) < 1);
            TEST(checkTransform(pTrafo, glm::dvec2(0,0), glm::dvec2(0,0)));
            TEST(checkTransform(pTrafo, glm::dvec2(640, 480), glm::dvec2(640, 480)));
        }
        {
            TrackerCalibrator calibrator(IntPoint(640, 480), IntPoint(1280,720));
            bool bDone = false;
            while (!bDone) {
                IntPoint displayPoint(calibrator.getDisplayPoint());
                calibrator.setCamPoint(glm::vec2(displayPoint.x/2, displayPoint.y/1.5));
                bDone = !calibrator.nextPoint();
            }
            pTrafo = calibrator.makeTransformer();
            TEST(glm::length(pTrafo->transformBlobToScreen(glm::dvec2(1.00,1.00)) -
                    glm::dvec2(2.00,1.50)) < 1);
            TEST(checkTransform(pTrafo, glm::dvec2(0,0), glm::dvec2(0,0)));
            TEST(checkTransform(pTrafo, glm::dvec2(640, 480), glm::dvec2(640, 480)));
            TEST(checkBlobToScreen(pTrafo, glm::dvec2(0,0), glm::dvec2(0,0)));
            TEST(checkBlobToScreen(pTrafo, glm::dvec2(640, 480), glm::dvec2(1280, 720)));
        }
    }

    bool checkTransform(CoordTransformerPtr pTrafo, const glm::dvec2& srcPt, 
            const glm::dvec2& destPt) 
    {
        glm::dvec2 ResultPt = pTrafo->transform_point(srcPt);
//        cerr << srcPt << " -> " << ResultPt << ", expected " << destPt << endl;
        return ((fabs(ResultPt.x-destPt.x) < 0.1) && (fabs(ResultPt.y-destPt.y) < 0.1));
    }

    bool checkBlobToScreen(DeDistortPtr pTrafo, 
            const glm::dvec2& srcPt, const glm::dvec2& destPt)
    {
        glm::dvec2 ResultPt = 
                pTrafo->transformBlobToScreen(pTrafo->transform_point(srcPt));
//        cerr << srcPt << " -> " << ResultPt << ", expected " << destPt << endl;
        return ((fabs(ResultPt.x-destPt.x) < 1) && (fabs(ResultPt.y-destPt.y) < 1));
    }
   
};

class CalibratorTestSuite: public TestSuite {
public:
    CalibratorTestSuite() 
        : TestSuite("CalibratorTestSuite")
    {
        addTest(TestPtr(new CalibratorTest));
    }
};


int main(int nargs, char** args)
{
    CalibratorTestSuite suite;
    suite.runTests();
    bool bOK = suite.isOk();

    if (bOK) {
        return 0;
    } else {
        return 1;
    }
}