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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
/**************************************************
* Classe Point
**************************************************/
class Point
{
public:
Point() : x1(0),x2(0),onGamma(false) {};
Point(const Point &P) : x1(P.x1),x2(P.x2),onGamma(P.onGamma) {};
Point(double x1,double x2, bool Gamma=false) : x1(x1),x2(x2),onGamma(Gamma) {};
double acces_x1() { return x1; };
double acces_x2() { return x2; };
bool isOnGamma() { return onGamma; };
void setOnGamma() { onGamma = true; };
void setNotOnGamma() { onGamma = false; };
Point& operator= (const Point & P) { x1=P.x1; x2=P.x2; onGamma=P.onGamma; return *this; };
friend bool operator< (Point P,Point Q);
friend bool operator> (Point P,Point Q);
friend bool operator<= (Point P,Point Q);
friend bool operator>= (Point P,Point Q);
friend ostream& operator<< (ostream& out, const Point P);
friend bool operator== (Point P,Point Q);
double operator[] (int i) { if(i==1) return x1;
if(i==2) return x2; };
private:
double x1,x2;
bool onGamma;
friend class Points;
friend class Triangle;
friend class Triangles;
};
bool operator==(Point P,Point Q)
{
return ( P.acces_x1()==Q.acces_x1() && P.acces_x2()==Q.acces_x2() );
}
ostream& operator<<(ostream& out, const Point P)
{
out << P.x1 << " " << P.x2;
return out;
}
bool operator<(Point P,Point Q)
{
if(P.acces_x1()<Q.acces_x1()) return true;
if(P.acces_x1()==Q.acces_x1() && P.acces_x2()<Q.acces_x2()) return true;
return false;
}
bool operator>(Point P,Point Q)
{
if(P.acces_x1()>Q.acces_x1()) return true;
if(P.acces_x1()==Q.acces_x1() && P.acces_x2()>Q.acces_x2()) return true;
return false;
}
bool operator<=(Point P,Point Q)
{
if(P.acces_x1()<Q.acces_x1()) return true;
if(P.acces_x1()==Q.acces_x1() && P.acces_x2()<=Q.acces_x2()) return true;
return false;
}
bool operator>=(Point P,Point Q)
{
if(P.acces_x1()>Q.acces_x1()) return true;
if(P.acces_x1()==Q.acces_x1() && P.acces_x2()>=Q.acces_x2()) return true;
return false;
}
/**************************************************
* Classe Points
**************************************************/
class Points
{
public:
Points() : nb_pts(0) {};
Points(Point P) : nb_pts(1) { vect_pts.push_back(P); };
Points(Points& P) { nb_pts = P.nb_points(); for(int i=0; i<P.nb_points(); ++i) vect_pts.push_back(P[i]); };
~Points() { nb_pts = 0; vect_pts.clear(); };
friend ostream& operator<< (ostream& out, Points P);
Point operator[] (int i) { return vect_pts[i]; };
void ajout(Point P);
void ajout_simple(Point P) { vect_pts.push_back(P); nb_pts++; };
int nb_points() { return nb_pts; };
private:
int nb_pts;
vector<Point> vect_pts;
};
ostream& operator<<(ostream& out, Points P)
{
if ( P.nb_pts > 0)
{
out << P.vect_pts[0];
for (int i=1; i<P.nb_pts; ++i)
out << endl << P.vect_pts[i];
}
else
out << "";
return out;
}
void Points::ajout(Point P)
{
int i = 0, erreur = 0;
if (nb_pts == 0)
{
vect_pts.push_back(P);
nb_pts++;
}
else
{
while (i<=nb_pts && erreur == 0)
{
if (vect_pts[i].acces_x1() == P.x1)
for (int j=i; vect_pts[j].x1 == P.x1; ++j)
if (vect_pts[j].x2 == P.x2)
erreur++;
i++;
}
if (erreur == 0)
{
vect_pts.push_back(P);
nb_pts++;
}
}
}
using namespace std;
/**************************************************
* Classe Triangle
**************************************************/
class Triangle
{
public:
Triangle(const Triangle& T) { P1=T.P1; P2=T.P2; P3=T.P3; };
Triangle(Point P, Point Q, Point R) : P1(P),P2(Q),P3(R) {};
Point acces_P1() { return P1; };
Point acces_P2() { return P2; };
Point acces_P3() { return P3; };
Triangle& operator= (const Triangle& T) { P1=T.P1; P2=T.P2; P3=T.P3; return *this; };
friend bool operator== (Triangle T1,Triangle T2);
friend bool operator< (Triangle T1,Triangle T2);
friend ostream& operator<< (ostream& out, Triangle T);
Point operator[] (int i) { if(i==1) return P1;
if(i==2) return P2;
if(i==3) return P3; };
Point min() { if(P1<=P2 && P1<=P3) return P1; if(P2<=P1 && P2<=P3) return P2; if(P3<=P2 && P3<=P1) return P3; }
Point max() { if(P1>=P2 && P1>=P3) return P1; if(P2>=P1 && P2>=P3) return P2; if(P3>=P2 && P3>=P1) return P3; }
Point mil() { if ((max()==P1 || max()==P2) && (min()==P1 || min()==P2) ) return P3;
if ((max()==P1 || max()==P3) && (min()==P1 || min()==P3) ) return P2;
if ((max()==P2 || max()==P3) && (min()==P2 || min()==P3) ) return P1; }
private:
Point P1,P2,P3;
friend class Triangles;
};
bool operator==(Triangle T1,Triangle T2)
{
return ( ( (T1.P1) == (T2.P1) || (T1.P1) == (T2.P2) || (T1.P1) == (T2.P3))
&& ( (T1.P2) == (T2.P1) || (T1.P2) == (T2.P2) || (T1.P2) == (T2.P3))
&& ( (T1.P3) == (T2.P1) || (T1.P3) == (T2.P2) || (T1.P3) == (T2.P3))
&& ( (T2.P1) == (T1.P1) || (T2.P1) == (T1.P2) || (T2.P1) == (T1.P3))
&& ( (T2.P2) == (T1.P1) || (T2.P2) == (T1.P2) || (T2.P2) == (T1.P3))
&& ( (T2.P3) == (T1.P1) || (T2.P3) == (T1.P2) || (T2.P3) == (T1.P3)) );
}
ostream& operator<<(ostream& out, Triangle T)
{
out << (T.P1) << endl << (T.P2) << endl << (T.P3);
return out;
}
bool operator< (Triangle T1,Triangle T2)
{
if( (T1.min()) < (T2.min()) ) return true;
if( (T1.min()) == (T2.min()) && (T1.max()) < (T2.max()) ) return true;
if( (T1.min()) == (T2.min()) && (T1.max()) == (T2.max()) && (T1.mil()) < (T2.mil()) ) return true;
return false;
}
/**************************************************
* Classe Triangles
**************************************************/
class Triangles
{
public:
Triangles() : nb_tri(0) {};
Triangles(Triangle T) : nb_tri(1) { vect_triangles.push_back(T); };
~Triangles() { nb_tri = 0; vect_triangles.clear(); };
friend ostream& operator<< (ostream& out, Triangles T);
Triangle operator[] (int i) { return vect_triangles[i]; };
void ajout(Triangle T);
void ajout_simple(Triangle T) { vect_triangles.push_back(T); nb_tri++; };
int nb_triangles() { return nb_tri; };
private:
int nb_tri;
vector<Triangle> vect_triangles;
};
ostream& operator<<(ostream& out, Triangles T)
{
if ( T.nb_tri > 0)
{
out << T.vect_triangles[0];
for (int i=1; i<T.nb_tri; ++i)
out << endl << T.vect_triangles[i];
}
else
out << "";
return out;
}
void Triangles::ajout(Triangle T)
{
int i = 0, erreur = 0;
if (nb_tri == 0)
{
vect_triangles.push_back(T);
nb_tri++;
}
else
{
while (i<nb_tri && erreur == 0)
{
if (vect_triangles[i] == T)
{
//cout << "T deja dans la liste" << endl;
erreur++;
}
i++;
}
if (erreur == 0)
{
vect_triangles.push_back(T);
nb_tri++;
}
}
}
/************************************************
definition du domaine de la marche
************************************************/
void marche ( Points& domaine,
double entree,
double longueur_marche,
double longueur_totale,
double sortie,
bool sortie_fichier=false,
bool sortie_gnu=true )
{
// if (entree < sortie) ...?
double hauteur_marche = sortie-entree;
Point A1(0,0,true);
Point A2(0,entree,true);
Point B1(longueur_marche,-hauteur_marche,true);
Point B2(longueur_marche,0,true);
Point C1(longueur_totale,-hauteur_marche,true);
Point C2(longueur_totale,entree,true);
domaine.ajout_simple(A1); // respect de l'ordre pour le tracer...
domaine.ajout_simple(A2);
domaine.ajout_simple(C2);
domaine.ajout_simple(C1);
domaine.ajout_simple(B1);
domaine.ajout_simple(B2);
if (sortie_fichier)
{
string nom_fichier = "data/domaine.dat";
ofstream fichier_ecrase(nom_fichier.c_str(), ios::out | ios::trunc); //déclaration du flux et ouverture du fichier (+creation si existe pas)
if(fichier_ecrase) // si l'ouverture a réussi
{
fichier_ecrase << domaine << endl;
fichier_ecrase << domaine[0] << endl; // pour fermer le dessin
fichier_ecrase.close(); // on referme le fichier
}
else
cout << "Erreur a l'ouverture du fichier DATA du domaine" << endl;
}
if (sortie_gnu)
{
string nom_fichier = "gnuplot/domaine.gnu";
ofstream fichier(nom_fichier.c_str(), ios::out | ios::trunc);
if(fichier) // si l'ouverture a réussi
{
fichier << "set xrange [-0.2:" << (longueur_totale+0.2) << "]\n";
fichier << "set yrange [-" << ((sortie-entree)+0.2) << ":" << (entree+0.2) << "]\n";
fichier << "set title \"Domaine\"\n";
fichier << "plot \"../data/domaine.dat\" w l\n";
fichier.close();
}
else
cout << "Erreur a l'ouverture du fichier GNUPLOT du domaine" << endl;
}
}
/************************************************
definition du maillage de la marche
************************************************/
void maillage_marche ( Points& maillage,
Triangles& triangles,
int nombre_de_points,
double entree,
double longueur_marche,
double longueur_totale,
double sortie,
bool sortie_gnu=false,
bool sortie_gnu_bord=false )
{
/* image du domaine de la marche
**********************************************
* 1 * 2 *
**********************************************
* 3 *
**********************************
*/
double aire_totale = longueur_totale*sortie + longueur_marche*(entree-sortie);
int nb_pts_1 = floor(longueur_marche*entree/aire_totale*nombre_de_points);
int nb_pts_2 = floor((longueur_totale-longueur_marche)*entree/aire_totale*nombre_de_points);
int nb_pts_3 = floor((longueur_totale-longueur_marche)*(sortie-entree)/aire_totale*nombre_de_points);
double coef_1 = longueur_marche/entree;
double coef_2 = (longueur_totale-longueur_marche)/entree;
double coef_3 = (longueur_totale-longueur_marche)/(sortie-entree);
int nb_colonnes_2 = floor(sqrt(nb_pts_2*4/3*coef_2));
int nb_colonnes_1 = floor(nb_colonnes_2*longueur_marche/(longueur_totale-longueur_marche));
int nb_colonnes_3 = nb_colonnes_2;
int nb_lignes_2 = floor(sqrt(nb_pts_2*2/coef_2));
while (nb_lignes_2*nb_colonnes_2 < nb_pts_2)
nb_lignes_2++;
int nb_lignes_1 = nb_lignes_2;
int nb_lignes_3 = floor(sqrt(nb_pts_3*2/coef_3));
while (nb_lignes_3*nb_colonnes_3 < nb_pts_3)
nb_lignes_3++;
double delta_x_1 = longueur_marche/(nb_colonnes_1-1);
double delta_y_1 = entree/(nb_lignes_1-1);
double delta_x_2 = (longueur_totale-longueur_marche)/(nb_colonnes_2-1);
double delta_y_2 = delta_y_1;
double delta_x_3 = delta_x_2;
double delta_y_3 = (sortie-entree)/(nb_lignes_3-1);
double x = 0;
double y = 0;
bool gamma = false;
for(int i=0; i<nb_colonnes_1-1; ++i)
{
for(int j=0; j<nb_lignes_1-1; ++j)
{
x = i*delta_x_1;
y = j*delta_y_1;
if (x==0 || y==0 || y==entree) gamma = true;
Point P(x,y,gamma);
x = (i+1)*delta_x_1;
y = j*delta_y_1;
if (x==0 || y==0 || y==entree) gamma = true;
Point Q(x,y,gamma);
x = i*delta_x_1;
y = (j+1)*delta_y_1;
if (x==0 || y==0 || y==entree) gamma = true;
Point R(x,y,gamma);
x = (i+1)*delta_x_1;
y = (j+1)*delta_y_1;
if (x==0 || y==0 || y==entree) gamma = true;
Point S(x,y,gamma);
maillage.ajout(P);
maillage.ajout(Q);
maillage.ajout(R);
Triangle T1( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T1);
maillage.ajout(S);
Triangle T2( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T2);
}
}
for(int i=0; i<nb_colonnes_2-1; ++i)
{
for(int j=0; j<nb_lignes_2-1; ++j)
{
x = i*delta_x_2+longueur_marche;
y = j*delta_y_2;
if (x==longueur_totale || y==entree) gamma = true;
Point P(x,y,gamma);
x = (i+1)*delta_x_2+longueur_marche;
y = j*delta_y_2;
if (x==longueur_totale || y==entree) gamma = true;
Point Q(x,y,gamma);
x = i*delta_x_2+longueur_marche;
y = (j+1)*delta_y_2;
if (x==longueur_totale || y==entree) gamma = true;
Point R (x,y,gamma);
x = (i+1)*delta_x_2+longueur_marche;
y = (j+1)*delta_y_2;
if (x==longueur_totale || y==entree) gamma = true;
Point S (x,y,gamma);
maillage.ajout(P);
maillage.ajout(Q);
maillage.ajout(R);
Triangle T1( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T1);
maillage.ajout(S);
Triangle T2( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T2);
}
}
for(int i=0; i<nb_colonnes_3-1; ++i)
{
for(int j=0; j<nb_lignes_3-1; ++j)
{
x = i*delta_x_3+longueur_marche;
y = j*delta_y_3*(-1);
if (x==longueur_totale || x==longueur_marche || y==-(sortie-entree)) gamma = true;
Point P(x,y,gamma);
x = (i+1)*delta_x_3+longueur_marche;
y = j*delta_y_3*(-1);
if (x==longueur_totale || x==longueur_marche || y==-(sortie-entree)) gamma = true;
Point Q(x,y,gamma);
x = i*delta_x_3+longueur_marche;
y = (j+1)*delta_y_3*(-1);
if (x==longueur_totale || x==longueur_marche || y==-(sortie-entree)) gamma = true;
Point R (x,y,gamma);
x = (i+1)*delta_x_3+longueur_marche;
y = (j+1)*delta_y_3*(-1);
if (x==longueur_totale || x==longueur_marche || y==-(sortie-entree)) gamma = true;
Point S (x,y,gamma);
maillage.ajout(P);
maillage.ajout(Q);
maillage.ajout(R);
Triangle T1( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T1);
maillage.ajout(S);
Triangle T2( maillage[maillage.nb_points()-1], maillage[maillage.nb_points()-2], maillage[maillage.nb_points()-3] );
triangles.ajout_simple(T2);
}
}
cout << "\tnombre de points : " << maillage.nb_points() << endl;
cout << "\tnombre de triangles : " << triangles.nb_triangles() << endl;
string nom_fichier = "data/maillage.dat";
string nom_fichier_tri = "data/triangles.dat";
ofstream fichier(nom_fichier.c_str(), ios::out | ios::trunc);
ofstream fichier_tri(nom_fichier_tri.c_str(), ios::out | ios::trunc);
if(fichier)
{
fichier << maillage;
fichier.close();
}
else
cout << "Erreur a l'ouverture du fichier DATA du maillage" << endl;
if(fichier_tri)
{
fichier_tri << triangles;
fichier_tri.close();
}
else
cout << "Erreur a l'ouverture du fichier DATA des triangles" << endl;
if (sortie_gnu)
{
string nom_fichier = "gnuplot/maillage.gnu";
string nom_fichier_tri = "gnuplot/triangles.gnu";
ofstream fichier(nom_fichier.c_str(), ios::out | ios::trunc);
ofstream fichier_tri(nom_fichier_tri.c_str(), ios::out | ios::trunc);
if(fichier)
{
fichier << "set xrange [-0.2:" << (longueur_totale+0.2) << "]\n";
fichier << "set yrange [-" << ((sortie-entree)+0.2) << ":" << (entree+0.2) << "]\n";
fichier << "set title \"Maillage\"\n";
fichier << "plot \"../data/maillage.dat\"\n";
fichier.close();
}
else
cout << "Erreur a l'ouverture du fichier GNUPLOT du maillage" << endl;
if(fichier_tri)
{
fichier_tri << "set xrange [-0.2:" << (longueur_totale+0.2) << "]\n";
fichier_tri << "set yrange [-" << ((sortie-entree)+0.2) << ":" << (entree+0.2) << "]\n";
fichier_tri << "set title \"Triangles\"\n";
fichier_tri << "plot \"../data/triangles.dat\"\n";
fichier_tri.close();
}
else
cout << "Erreur a l'ouverture du fichier GNUPLOT des triangles" << endl;
}
if (sortie_gnu_bord)
{
string nom_fichier2 = "gnuplot/bord.gnu";
string nom_fichier_bord = "data/bord.dat";
ofstream fichier2(nom_fichier2.c_str(), ios::out | ios::trunc);
ofstream fichier_bord(nom_fichier_bord.c_str(), ios::out | ios::trunc);
if(fichier2)
{
fichier2 << "set xrange [-0.2:" << (longueur_totale+0.2) << "]\n";
fichier2 << "set yrange [-" << ((sortie-entree)+0.2) << ":" << (entree+0.2) << "]\n";
fichier2 << "set title \"Bord\"\n";
fichier2 << "plot \"../data/bord.dat\"\n";
fichier2.close();
}
else
cout << "Erreur a l'ouverture du fichier GNUPLOT du maillage" << endl;
if(fichier_bord)
{
for(int i=0; i<maillage.nb_points(); ++i)
{
if(maillage[i].isOnGamma())
fichier_bord << maillage[i] << endl;
}
fichier_bord.close();
}
else
cout << "Erreur a l'ouverture du fichier DATA du bord" << endl;
}
cout << triangles << endl;
}
/************************************************
definition des variables
************************************************/
double entree = 1.;
double longueur_marche = 1.4;
double longueur_totale = 7.5;
double sortie = 2.;
Points Omega;
Points maillage;
Points sous_maillage;
Triangles triangles;
Triangles sous_triangles;
int nombre_de_points = 50; // = 75% du nb de points total... au moins 500 sinon c'est pas beau
bool sortie_fichier = true;
bool sortie_gnu = true;
bool sortie_gnu_bord = true;
/************************************************
main
************************************************/
int main()
{
//definition du domaine
cout << "Construction du domaine" << endl;
marche( Omega,
entree,
longueur_marche,
longueur_totale,
sortie ); // ne touche pas a maillage ni a triangles
//definition du maillage
cout << "Ecriture du maillage" << endl;
maillage_marche( maillage,
triangles,
nombre_de_points,
entree,
longueur_marche,
longueur_totale,
sortie,
sortie_gnu,
sortie_gnu_bord );
cout << maillage << endl;
cout << triangles << endl; // la ca bug...
return 0;
} |
Partager