00001
00002
00004
00005 #if !defined(AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_)
00006 #define AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_
00007
00008 #if _MSC_VER > 1000
00009 #pragma once
00010 #endif // _MSC_VER > 1000
00011
00012 #include <fstream>
00013
00014 #include "math.h"
00015 #include "../Utility_hm/TMatrix.h"
00016 using namespace std;
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 struct objvector
00027 {
00028 int Type;
00029
00030
00031
00032 double FontHeight;
00033 double Length;
00034 double Centroid_X;
00035 double Centroid_Y;
00036 double Angle;
00037
00041 objvector()
00042 {
00043 Type = 0;
00044 FontHeight = 0;
00045 Length = 0;
00046 Centroid_X = 0;
00047 Centroid_Y = 0;
00048 Angle = 0;
00049 }
00050
00054 objvector(int type, double font, double length, double x, double y, double angle)
00055 {
00056 Type = type;
00057 FontHeight = font;
00058 Length = length;
00059 Centroid_X = x;
00060 Centroid_Y = y;
00061 Angle = angle;
00062 }
00063
00068 void write_stream( ofstream & f )
00069 {
00070 f << Type << endl;
00071
00072
00073
00074 f << FontHeight << endl;
00075 f << Length << endl;
00076 f << Centroid_X << endl;
00077 f << Centroid_Y << endl;
00078 f << Angle << endl;
00079 }
00080
00085 void read_stream( ifstream &f )
00086 {
00087 f >> Type;
00088
00089
00090
00091 f >> FontHeight;
00092 f >> Length;
00093 f >> Centroid_X ;
00094 f >> Centroid_Y ;
00095 f >> Angle;
00096
00097 }
00098
00099 objvector operator - (objvector a)
00100 {
00101 objvector b;
00102
00103 b.Type = Type;
00104 b.FontHeight = FontHeight - a.FontHeight;
00105 b.Length = Length - a.Length;
00106 b.Centroid_X = Centroid_X - a.Centroid_X;
00107 b.Centroid_Y = Centroid_Y - a.Centroid_Y;
00108 b.Angle = Angle - a.Angle;
00109
00110 return b;
00111 }
00112 objvector operator + (objvector a)
00113 {
00114 objvector b;
00115
00116 b.Type = Type;
00117 b.FontHeight = FontHeight + a.FontHeight;
00118 b.Length = Length + a.Length;
00119 b.Centroid_X = Centroid_X + a.Centroid_X;
00120 b.Centroid_Y = Centroid_Y + a.Centroid_Y;
00121 b.Angle = Angle + a.Angle;
00122
00123 return b;
00124 }
00125
00126
00127 objvector operator * (double a)
00128 {
00129 objvector b;
00130 b.Type = Type;
00131 b.FontHeight = FontHeight * a;
00132 b.Length = Length * a;
00133 b.Centroid_X = Centroid_X * a;
00134 b.Centroid_Y = Centroid_Y * a;
00135 b.Angle = Angle * a;
00136 return b;
00137 }
00138
00139 objvector operator / (double a)
00140 {
00141
00142
00143 objvector b;
00144 b.Type = Type;
00145 b.FontHeight = FontHeight / a;
00146 b.Length = Length / a;
00147 b.Centroid_X = Centroid_X / a;
00148 b.Centroid_Y = Centroid_Y / a;
00149 b.Angle = Angle / a;
00150 return b;
00151 }
00152
00153 objvector dotproduct(objvector a)
00154 {
00155
00156
00157 objvector b = *this;
00158 b.Angle *= a.Angle;
00159 b.Centroid_X *= a.Centroid_X;
00160 b.Centroid_Y *= a.Centroid_Y;
00161 b.Length *= a.Length;
00162 b.FontHeight *= a.FontHeight;
00163 return b;
00164 }
00165
00166 objvector dotsqrt()
00167 {
00168
00169
00170 objvector b = *this;
00171 b.Angle = sqrt(Angle);
00172 b.Centroid_X = sqrt(Centroid_X);
00173 b.Centroid_Y = sqrt(Centroid_Y);
00174 b.Length = sqrt(Length);
00175 b.FontHeight = sqrt(FontHeight);
00176 return b;
00177 }
00178
00179 objvector dotdivide(objvector a, bool flag = true)
00180 {
00181
00182
00183 objvector b = *this;
00184 b.Angle /= a.Angle;
00185 if(flag)
00186 {
00187 b.Centroid_X /= a.Centroid_X;
00188 b.Centroid_Y /= a.Centroid_Y;
00189 }
00190 b.Length /= a.Length;
00191 b.FontHeight /= a.FontHeight;
00192 return b;
00193 }
00194
00195 objvector objsqrt()
00196 {
00197 objvector b;
00198 b.Type = Type;
00199 b.Angle = sqrt(Angle);
00200 b.Centroid_X = sqrt(Centroid_X);
00201 b.Centroid_Y = sqrt(Centroid_Y);
00202 b.FontHeight = sqrt(FontHeight);
00203 b.Length = sqrt(Length);
00204
00205 return b;
00206 }
00207
00208 objvector& operator = (objvector a)
00209 {
00210 Type = a.Type;
00211 FontHeight = a.FontHeight;
00212 Length = a.Length;
00213 Centroid_X = a.Centroid_X;
00214 Centroid_Y = a.Centroid_Y;
00215 Angle = a.Angle;
00216
00217 return *this;
00218 }
00219
00220 bool operator == (objvector a)
00221 {
00222 if (Type != a.Type) return false;
00223 if (FontHeight != a.FontHeight ) return false;
00224 if (Length != a.Length) return false;
00225 if (Centroid_X != a.Centroid_X) return false;
00226 if (Centroid_Y != a.Centroid_Y) return false;
00227 if (Angle != a.Angle) return false;
00228
00229 return true;
00230 }
00231
00232 objvector dotabs()
00233 {
00234 objvector b = *this;
00235 if(b.Angle <0) b.Angle = -b.Angle;
00236 if(b.Centroid_X <0) b.Centroid_X = -b.Centroid_X;
00237 if(b.Centroid_Y <0) b.Centroid_Y = -b.Centroid_Y;
00238 if(b.FontHeight <0) b.FontHeight = -b.FontHeight;
00239 if(b.Length <0) b.Length = -b.Length;
00240
00241 return b;
00242 }
00243
00244 double norm()
00245 {
00246 return sqrt(normsquare());
00247 }
00248
00249 double normsquare()
00250 {
00251 return FontHeight*FontHeight + Length*Length+Centroid_X*Centroid_X + Centroid_Y*Centroid_Y + Angle*Angle;
00252 }
00253
00254
00255 double norm_2(double f_r=1, double a_r=1, double l_r=1, double x_r=1, double y_r=1)
00256 {
00257 double ratio = f_r + a_r+ l_r + x_r + y_r;
00258 double dist = FontHeight*FontHeight * f_r + Length*Length * l_r
00259 + Centroid_X*Centroid_X * x_r + Centroid_Y*Centroid_Y * y_r
00260 + Angle*Angle * a_r;
00261
00262 return sqrt(dist/ratio);
00263 }
00264
00265 public:
00266 void clear()
00267 {
00268 Type = 0;
00269 FontHeight = 0;
00270 Length = 0;
00271 Centroid_X = 0;
00272 Centroid_Y = 0;
00273 Angle = 0;
00274 }
00275 };
00276
00277 struct objcluster
00278 {
00279 public:
00280 objvector m_centroid;
00281 objvector m_sigma;
00282 int m_nbObjects;
00283
00284 double m_weight;
00285 double m_avgnbObjperdoc;
00286 double m_sigmaObjperdoc;
00287
00288 objcluster();
00289 virtual ~objcluster();
00290
00291 double Probability(objvector& obj);
00292
00293
00294
00295
00296 void write_stream(ofstream & of, bool saveweights);
00297 void read_stream(ifstream &f);
00298 };
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 #endif // !defined(AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_)