summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/hotelling.cc
blob: ac490d0c1793c3c5fd29f1ade06c104af09b9478 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// hotelling.cc - Classe Hotelling
// robert - Programme du robot APBteam
// Copyright (C) 2005 Olivier Gaillard

#include <iostream>

#include "hotelling.hh"
    
/// Constructeur
Hotelling::Hotelling (const std::vector<Hpoint> &dataList)
{
    oconfig_ = OConfig::getInstance (); 
    dataSize_ = dataList.size ();
    if (dataSize_ == 0)
	meanX_ = meanY_ = 0;
    else
      {
	// Calcul de la moyenne
	mean (dataList);
	// Initialisation des donn�es
	data_ = new Hpoint[dataSize_];
	// Changement de rep�re des donn�es
	int i = 0;
	for (std::vector<Hpoint>::const_iterator iter = dataList.begin ();
	     iter != dataList.end (); ++iter)
	  {
	    data_[i].set ((*iter)[0] - meanX_, (*iter)[1] - meanY_);
	    ++i;
	  }
      }
}

/// Destructeur
Hotelling::~Hotelling (void)
{
    delete [] data_;
}

/// Covariance
double
Hotelling::cov (const unsigned i, const unsigned j)
{
    double c = 0;
    for (unsigned k = 0; k < dataSize_; ++k)
	c += data_[k][i] * data_[k][j]; 
    c /= (dataSize_-1);
    return c;
}

/// Calcul de la matrice de covariance
void
Hotelling::covMatrix ()
{
    /// Calcul de la covariance
    covMatrix_[0][0] = cov (0, 0);
    covMatrix_[0][1] = cov (0, 1);
    covMatrix_[1][0] = covMatrix_[0][1];
    covMatrix_[1][1] = cov (1, 1);
}

/// Calcul de la moyenne des deux composantes
void
Hotelling::mean (const std::vector<Hpoint> &dataList)
{
    meanX_ = meanY_ = 0;
    for (std::vector<Hpoint>::const_iterator iter = dataList.begin ();
	 iter != dataList.end (); ++iter)
      {
	meanX_ += (*iter)[0];
	meanY_ += (*iter)[1];
      }
    meanX_ /= dataSize_;
    meanY_ /= dataSize_;
}

/// Calcul des eigenvalues
void
Hotelling::eigenValues ()
{
    double apd = covMatrix_[0][0] + covMatrix_[1][1];
    double sqrtdelta = sqrt (apd*apd - 4* (covMatrix_[0][0]*covMatrix_[1][1] - covMatrix_[1][0]*covMatrix_[1][0]));
    val1_ = (apd - sqrtdelta)/(2);
    val2_ = (apd + sqrtdelta)/(2);
}


/// Calcul des eigenvectors
void 
Hotelling::eigenVectors ()
{
    covMatrix ();
    eigenValues ();
    // Calcul des vecteurs
    e2_.set (1, covMatrix_[1][0] / (val2_ - covMatrix_[1][1]));
    e1_.set (1, covMatrix_[1][0] / (val1_ - covMatrix_[1][1]));
    // Normalisation
    e1_.norm ();
    e2_.norm ();
}
    
/// Accessors
void 
Hotelling::getPC (Hpoint &pca)
{
    if (val1_>val2_) pca = e1_;
    else pca = e2_;
}
void 
Hotelling::getPC (double &x, double &y)
{
    Hpoint h;
    getPC (h);
    x = h[0]; y = h[1];
}

/// Affiche la matrice de covariance
void 
Hotelling::showCovMatrix () const
{
    std::cout << "Matrice de covariance :\n"
	      << "\t" << covMatrix_[0][0] << "\n"
	      << "\t" << covMatrix_[0][1] << "\n"
	      << "\t" << covMatrix_[1][0] << "\n"
	      << "\t" << covMatrix_[1][1] << std::endl;
}

/// Affiche les eigenvectors
void 
Hotelling::showEigenVectors () const
{
    std::cout << "EigenVectors :\n"
	      << "\t" << e1_[0] << ", " << e1_[1] << "\n"
	      << "\t" << e2_[0] << ", " << e2_[1] << std::endl;
}