00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _CMGRAPHS_UTILS_H_
00034 #define _CMGRAPHS_UTILS_H_
00035
00036
00037
00038 #include <string>
00039 #include <sstream>
00040 #include <fstream>
00041 #include <cmath>
00042 #include <algorithm>
00043 #include <vector>
00044
00045
00046 #include "chomp/system/config.h"
00047 #include "chomp/system/textfile.h"
00048 #include "chomp/cubes/cube.h"
00049 #include "chomp/multiwork/mw.h"
00050
00051
00052 #include "config.h"
00053 #include "typedefs.h"
00054 #include "eigenval.h"
00055
00056
00057
00058
00059
00060
00061
00062 inline bool fileExists (const char *filename)
00063 {
00064 std::ifstream in (filename);
00065 return !!in;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 template <class T>
00079 class local_value
00080 {
00081 public:
00082
00083 local_value (T &_variable, const T &_value):
00084 variable (_variable), previous_value (_variable)
00085 {variable = _value; return;}
00086
00087
00088 ~local_value () {variable = previous_value; return;}
00089
00090 private:
00091
00092 T &variable;
00093
00094
00095 T previous_value;
00096
00097 };
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 template <class intType1, class intType2, class intType3>
00108 inline bool internalBoundaryPoint (const intType1 *coord,
00109 const intType2 *left, const intType2 *right,
00110 const intType3 *limit, int dim)
00111 {
00112 for (int i = 0; i < dim; ++ i)
00113 {
00114 if ((coord [i] == left [i]) && (left [i] != 0))
00115 {
00116 return true;
00117 }
00118 else if ((coord [i] == right [i] - 1) &&
00119 (right [i] != limit [i]))
00120 {
00121 return true;
00122 }
00123 }
00124 return false;
00125 }
00126
00127
00128
00129
00130 template <class intType>
00131 void computeParam (const intType *curCoord,
00132 double *leftMapParam, double *rightMapParam)
00133 {
00134 for (int i = 0; i < paramCount; ++ i)
00135 {
00136 leftMapParam [i] = paramLeft [i];
00137 rightMapParam [i] = paramRight [i];
00138 for (int j = 0; j < paramDim; ++ j)
00139 {
00140 if (i != paramSelect [j])
00141 continue;
00142 double paramWidth = paramRight [i] - paramLeft [i];
00143 if (curCoord [j] > 0)
00144 {
00145 leftMapParam [i] = paramLeft [i] +
00146 curCoord [j] * paramWidth /
00147 paramSubdiv [j];
00148 }
00149 if (curCoord [j] < paramSubdiv [j] - 1)
00150 {
00151 rightMapParam [i] = paramLeft [i] +
00152 (curCoord [j] + 1) * paramWidth /
00153 paramSubdiv [j];
00154 }
00155 break;
00156 }
00157 }
00158 return;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168 inline parRect *subRectangles (parCoord *zeroIter, parCoord *maxIter,
00169 std::vector<parCoord> *subCoords, int minCount)
00170 {
00171 using chomp::homology::sbug;
00172
00173
00174 if (minCount < 1)
00175 minCount = 1;
00176
00177
00178
00179 bool taken [paramDim];
00180 for (int i = 0; i < paramDim; ++ i)
00181 taken [i] = false;
00182 int increasing [paramDim];
00183 for (int i = 0; i < paramDim; ++ i)
00184 {
00185
00186 int minIndex = -1;
00187 for (int j = 0; j < paramDim; ++ j)
00188 {
00189 if (taken [j])
00190 continue;
00191 if ((minIndex < 0) ||
00192 (paramSubdiv [j] < paramSubdiv [minIndex]))
00193 {
00194 minIndex = j;
00195 }
00196 }
00197
00198
00199 increasing [i] = minIndex;
00200 taken [minIndex] = true;
00201 }
00202
00203
00204 double volume = 1;
00205 for (int i = 0; i < paramDim; ++ i)
00206 volume *= paramSubdiv [i];
00207 volume /= minCount;
00208
00209
00210
00211 for (int i = 0; i < paramDim; ++ i)
00212 {
00213
00214 int index = increasing [i];
00215 double side = std::exp (std::log (volume) / (paramDim - i));
00216 double delta = (i == paramDim - 1) ? 0.99999 : 0.5;
00217 maxIter [index] = static_cast<int>
00218 (paramSubdiv [index] / side + delta);
00219
00220
00221
00222 if (maxIter [index] > paramSubdiv [index])
00223 maxIter [index] = paramSubdiv [index];
00224 if (maxIter [index] <= 0)
00225 maxIter [index] = 1;
00226
00227
00228 volume /= paramSubdiv [index];
00229 volume *= maxIter [index];
00230
00231 }
00232
00233
00234 for (int i = 0; i < paramDim; ++ i)
00235 {
00236 subCoords [i]. push_back (0);
00237 for (int j = 1; j < maxIter [i]; ++ j)
00238 {
00239 subCoords [i]. push_back ((paramSubdiv [i] * j +
00240 maxIter [i] / 2) / maxIter [i]);
00241 }
00242 subCoords [i]. push_back (paramSubdiv [i]);
00243 }
00244
00245
00246 for (int i = 0; i < paramDim; ++ i)
00247 zeroIter [i] = 0;
00248
00249
00250 return new parRect (zeroIter, maxIter, paramDim);
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 template <class intType>
00260 inline int countDigits (intType x)
00261 {
00262 intType tenPower = 10;
00263 int digits = 1;
00264 while (1)
00265 {
00266 if (x < tenPower)
00267 return digits;
00268 intType nextPower = tenPower * 10;
00269 if (nextPower < tenPower)
00270 return digits;
00271 tenPower = nextPower;
00272 ++ digits;
00273 }
00274 }
00275
00276
00277
00278
00279 template <class intType1, class intType2>
00280 inline std::string coord2str (const intType1 *coords,
00281 const intType2 *maxCoords, int dim)
00282 {
00283 std::ostringstream str;
00284 for (int i = 0; i < dim; ++ i)
00285 {
00286 if (i)
00287 str << '_';
00288 int maxDigits = countDigits (maxCoords [i] - 1);
00289 int digits = countDigits (coords [i]);
00290 for (int d = digits; d < maxDigits; ++ d)
00291 str << '0';
00292 str << coords [i];
00293 }
00294 return str. str ();
00295 }
00296
00297
00298 template <class intType>
00299 inline std::string coord2str (const intType *coords, int dim)
00300 {
00301 std::ostringstream str;
00302 for (int i = 0; i < dim; ++ i)
00303 {
00304 if (i)
00305 str << '_';
00306 str << coords [i];
00307 }
00308 return str. str ();
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318 inline void coordMinMax (const theMorseDecompositionType &morseDec,
00319 spcCoord *coordLeftMin, spcCoord *coordRightMax)
00320 {
00321 spcCoord coord [spaceDim];
00322 int nSets = morseDec. count ();
00323 for (int n = 0; n < nSets; ++ n)
00324 {
00325 const spcCubes &theSet = morseDec [n];
00326 int nCubes = theSet. size ();
00327 for (int i = 0; i < nCubes; ++ i)
00328 {
00329 theSet [i]. coord (coord);
00330 for (int j = 0; j < spaceDim; ++ j)
00331 {
00332 if (coordLeftMin [j] > coord [j])
00333 coordLeftMin [j] = coord [j];
00334 if (coordRightMax [j] <= coord [j])
00335 coordRightMax [j] = coord [j] + 1;
00336 }
00337 }
00338 }
00339 return;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349 template <class typeCubes>
00350 void subdivideCubes (const typeCubes &X, typeCubes &result)
00351 {
00352 typedef typename typeCubes::value_type tCube;
00353 typedef typename typeCubes::value_type::CoordType tCoord;
00354
00355 if (X. empty ())
00356 return;
00357 int dim = X [0]. dim ();
00358
00359 tCoord cmin [tCube::MaxDim];
00360 tCoord cmax [tCube::MaxDim];
00361 int n = X. size ();
00362 for (int i = 0; i < n; ++ i)
00363 {
00364 X [i]. coord (cmin);
00365 for (int j = 0; j < dim; ++ j)
00366 {
00367 cmin [j] *= 2;
00368 cmax [j] = cmin [j] + 2;
00369 }
00370 chomp::homology::tRectangle<tCoord> r (cmin, cmax, dim);
00371 const tCoord *c;
00372 while ((c = r. get ()) != 0)
00373 result. add (tCube (c, dim));
00374 }
00375 return;
00376 }
00377
00378
00379 template <class typeCube, class typeCubes>
00380 void subdivideCube (const typeCube &q, typeCubes &result)
00381 {
00382 typedef typename typeCubes::value_type::CoordType tCoord;
00383
00384 int dim = q. dim ();
00385
00386 tCoord cmin [typeCube::MaxDim];
00387 tCoord cmax [typeCube::MaxDim];
00388 q. coord (cmin);
00389 for (int j = 0; j < dim; ++ j)
00390 {
00391 cmin [j] *= 2;
00392 cmax [j] = cmin [j] + 2;
00393 }
00394 chomp::homology::tRectangle<tCoord> r (cmin, cmax, dim);
00395 const tCoord *c;
00396 while ((c = r. get ()) != 0)
00397 result. add (typeCube (c, dim));
00398 return;
00399 }
00400
00401
00402
00403 template <class typeCubes>
00404 void embedCubes (const typeCubes &X, typeCubes &result)
00405 {
00406 typedef typename typeCubes::value_type tCube;
00407 typedef typename typeCubes::value_type::CoordType tCoord;
00408
00409 if (X. empty ())
00410 return;
00411 int dim = X [0]. dim ();
00412
00413 tCoord coord [tCube::MaxDim];
00414 int n = X. size ();
00415 for (int i = 0; i < n; ++ i)
00416 {
00417 X [i]. coord (coord);
00418 for (int j = 0; j < dim; ++ j)
00419 coord [j] /= 2;
00420 result. add (tCube (coord, dim));
00421 }
00422 return;
00423 }
00424
00425
00426 #endif // _CMGRAPHS_UTILS_H_
00427