00001 #include <Polygon.h>
00002
00003
00004 int Polygon:: no_vertices() {
00005 return p.size();
00006 }
00007
00008 ex Polygon::vertex (int i) {
00009 return p[i];
00010 }
00011
00012 string Polygon:: str() {
00013
00014 return "Polygon";
00015 }
00016
00017
00018
00019
00020 Line:: Line(ex x0, ex x1, string subscript_){
00021 subscript = subscript_;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 if ( (is_a<lst>(x0) && is_a<lst>(x1)) && (x0.nops() == x1.nops()) ) {
00032
00033 lst aa;
00034 lst bb;
00035
00036
00037
00038 for (int i=0; i<= x0.nops()-1; i++) {
00039 bb.append(x0.op(i));
00040 aa.append(x1.op(i) - x0.op(i));
00041 }
00042
00043
00044
00045 p.insert(p.end(), x0); ;
00046 p.insert(p.end(), x1); ;
00047 a_ = aa;
00048 b_ = bb;
00049
00050 } else {
00051
00052
00053 cout <<"THIS SHOULD NOT HAPPEN "<<endl;
00054 }
00055 }
00056
00057 int Line:: no_vertices() { return 2; }
00058
00059 ex Line:: vertex(int i) {
00060 return p[i];
00061 }
00062
00063
00064
00065 ex Line:: repr(ex t, Repr_format format) {
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 lst r;
00078
00079
00080 if ( format == SUBS_PERFORMED ) {
00081 if ( a_.nops() == 2) {
00082 r = lst( x == b_.op(0) + a_.op(0)*t,
00083 y == b_.op(1) + a_.op(1)*t);
00084
00085 } else if ( a_.nops() == 3) {
00086 r = lst( x == b_.op(0) + a_.op(0)*t,
00087 y == b_.op(1) + a_.op(1)*t,
00088 z == b_.op(2) + a_.op(2)*t);
00089 }
00090 r.append(lst(t,0,1));
00091 }
00092 else if ( format == SUBS_NOT_PERFORMED ) {
00093
00094
00095 symbol a1("A"+subscript);
00096 symbol a2("C"+subscript);
00097 symbol a3("E"+subscript);
00098 symbol b1("B"+subscript);
00099 symbol b2("D"+subscript);
00100 symbol b3("F"+subscript);
00101
00102
00103
00104 if ( a_.nops() == 2) {
00105 r = lst( x == b1 + a1*t,
00106 y == b2 + a2*t);
00107
00108 r.append( a1 == a_.op(0));
00109 r.append( b1 == b_.op(0));
00110 r.append( a2 == a_.op(1));
00111 r.append( b2 == b_.op(1));
00112
00113 } else if ( a_.nops() == 3) {
00114 r = lst( x == b1 + a1*t,
00115 y == b2 + a2*t,
00116 z == b3 + a3*t);
00117
00118
00119 r.append( a1 == a_.op(0));
00120 r.append( b1 == b_.op(0));
00121 r.append( a2 == a_.op(1));
00122 r.append( b2 == b_.op(1));
00123 r.append( a3 == a_.op(2));
00124 r.append( b3 == b_.op(2));
00125
00126
00127 }
00128
00129 r.append(lst(t,0,1));
00130 }
00131
00132 return r;
00133 }
00134
00135 ex Line:: integrate(ex f, Repr_format format) {
00136 symbol t("t");
00137 ex t_repr = repr(t);
00138 lst sub;
00139 int counter;
00140
00141 if ( p[0].nops() == 3) {
00142 sub = lst(t_repr.op(0), t_repr.op(1), t_repr.op(2));
00143 counter = 3;
00144 } else if ( p[0].nops() == 2) {
00145 sub = lst(t_repr.op(0), t_repr.op(1));
00146 counter = 2;
00147 }
00148
00149
00150
00151 ex D;
00152 if ( p[0].nops() == 3) {
00153 D = pow(t_repr.op(0).rhs().coeff(t), 2) +
00154 pow(t_repr.op(1).rhs().coeff(t), 2) +
00155 pow(t_repr.op(2).rhs().coeff(t), 2);
00156 } else if ( p[0].nops() == 2) {
00157 D = pow(t_repr.op(0).rhs().coeff(t), 2) +
00158 pow(t_repr.op(1).rhs().coeff(t), 2);
00159 }
00160
00161 D = sqrt(D);
00162
00163 ex intf = f.subs(sub)*D;
00164
00165 intf = integral(t_repr.op(counter).op(0), t_repr.op(counter).op(1), t_repr.op(counter).op(2), intf);
00166 intf = eval_integ(intf);
00167
00168 return intf;
00169
00170
00171 }
00172
00173 string Line:: str(){
00174 std::ostringstream s;
00175 s <<"Line("<<p[0]<<","<<p[1]<<")";
00176 return s.str();
00177 }
00178
00179
00180
00181
00182 ReferenceLine:: ReferenceLine(string subscript_) {
00183 subscript = subscript_;
00184 ex x0 = lst(0,0,0);
00185 ex x1 = lst(1,0,0);
00186
00187
00188 }
00189
00190 int ReferenceLine:: no_vertices() { return 2; }
00191
00192 ex ReferenceLine:: vertex(int i) {
00193 return p[i];
00194 }
00195
00196 ex ReferenceLine:: a() {
00197 return lst(1,0,0);
00198 }
00199
00200 ex ReferenceLine:: b() {
00201 return lst(0,0,0);
00202 }
00203
00204 ex ReferenceLine:: repr(ex t, Repr_format format) {
00205 lst r;
00206 r = lst( x == t, y == 0, z == 0, t, 0, 1);
00207 return r;
00208 }
00209
00210 string ReferenceLine:: str() {
00211 std::ostringstream s;
00212
00213 s <<"ReferenceLine";
00214 return s.str();
00215 }
00216
00217 ex ReferenceLine:: integrate(ex f, Repr_format formt) {
00218 ex intf = integral(x,0,1,f);
00219 intf = eval_integ(intf);
00220 return intf;
00221 }
00222
00223
00224
00225
00226 Triangle:: Triangle(ex x0, ex x1, ex x2, string subscript_) {
00227 subscript = subscript_;
00228 p.insert(p.end(), x0);
00229 p.insert(p.end(), x1);
00230 p.insert(p.end(), x2);
00231
00232 }
00233
00234 int Triangle:: no_vertices() { return 3; }
00235
00236 ex Triangle:: vertex(int i) {
00237 return p[i];
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 Line Triangle:: line(int i) {
00269
00270
00271 if ( i == 1) return Line(p[0],p[1], istr(subscript,i));
00272 else if ( i == 2) return Line(p[0],p[2], istr(subscript,i));
00273 else if ( i == 3) return Line(p[1],p[2], istr(subscript,i));
00274 }
00275
00276 ex Triangle:: repr(Repr_format format) {
00277 symbol r("r"), s("s");
00278 symbol G("G"), H("H");
00279 ex l1_repr = line(1).repr(r);
00280 ex l2_repr = line(2).repr(s);
00281 lst ret;
00282
00283 if ( p[0].nops() == 2) {
00284 ret = lst( x ==l1_repr.op(0).rhs().coeff(r,0) + l1_repr.op(0).rhs().coeff(r,1)*r + l2_repr.op(0).rhs().coeff(s,1)*s,
00285 y ==l1_repr.op(1).rhs().coeff(r,0) + l1_repr.op(1).rhs().coeff(r,1)*r + l2_repr.op(1).rhs().coeff(s,1)*s);
00286
00287
00288 }
00289 else if ( p[0].nops() == 3) {
00290
00291 ret = lst( x == l1_repr.op(0).rhs().coeff(r,0) + l1_repr.op(0).rhs().coeff(r,1)*r + l2_repr.op(0).rhs().coeff(s,1)*s,
00292 y == l1_repr.op(1).rhs().coeff(r,0) + l1_repr.op(1).rhs().coeff(r,1)*r + l2_repr.op(1).rhs().coeff(s,1)*s,
00293 z == l1_repr.op(2).rhs().coeff(r,0) + l1_repr.op(2).rhs().coeff(r,1)*r + l2_repr.op(2).rhs().coeff(s,1)*s);
00294
00295 }
00296
00297 ret.append(lst(r, 0, 1));
00298 ret.append(lst(s, 0, 1 - r));
00299
00300 return ret;
00301
00302 }
00303
00304
00305 string Triangle:: str() {
00306 std::ostringstream s;
00307
00308 s <<"Triangle";
00309 return s.str();
00310 }
00311
00312 ex Triangle:: integrate(ex func, Repr_format format) {
00313
00314 ex t_repr = repr();
00315 lst sub;
00316 int counter;
00317
00318
00319
00320 if ( p[0].nops() == 3) {
00321 sub = lst(t_repr.op(0), t_repr.op(1), t_repr.op(2));
00322 counter = 3;
00323 } else if ( p[0].nops() == 2) {
00324 sub = lst(t_repr.op(0), t_repr.op(1));
00325 counter = 2;
00326 }
00327 ex intf = func.subs(sub);
00328
00329
00330 ex D;
00331 if ( p[0].nops() == 3) {
00332 ex r = t_repr.op(3).op(0);
00333 ex s = t_repr.op(4).op(0);
00334 ex a = t_repr.op(0).rhs().coeff(r,1);
00335 ex b = t_repr.op(0).rhs().coeff(s,1);
00336 ex c = t_repr.op(1).rhs().coeff(r,1);
00337 ex d = t_repr.op(1).rhs().coeff(s,1);
00338 ex e = t_repr.op(2).rhs().coeff(r,1);
00339 ex f = t_repr.op(2).rhs().coeff(s,1);
00340 D = pow(c*f-d*e,2) + pow(a*f - b*e,2) + pow(a*d - b*c,2);
00341 D = sqrt(D);
00342 } else if ( p[0].nops() == 2) {
00343 ex r = t_repr.op(2).op(0);
00344 ex s = t_repr.op(3).op(0);
00345 ex a = t_repr.op(0).rhs().coeff(r,1);
00346 ex b = t_repr.op(0).rhs().coeff(s,1);
00347 ex c = t_repr.op(1).rhs().coeff(r,1);
00348 ex d = t_repr.op(1).rhs().coeff(s,1);
00349 D = a*d-b*c;
00350 }
00351
00352 intf = intf*D;
00353
00354
00355 counter++;
00356 intf = integral(t_repr.op(counter).op(0), t_repr.op(counter).op(1), t_repr.op(counter).op(2), intf);
00357 intf = eval_integ(intf);
00358
00359 counter--;
00360 intf = integral(t_repr.op(counter).op(0), t_repr.op(counter).op(1), t_repr.op(counter).op(2), intf);
00361 intf = eval_integ(intf);
00362 return intf;
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 }
00413
00414
00415
00416
00417
00418
00419 ReferenceTriangle:: ReferenceTriangle(string subscript_) {
00420 subscript = subscript_;
00421 ex x0 = lst(0,0,0);
00422 ex x1 = lst(1,0,0);
00423 ex x2 = lst(0,1,0);
00424 p.insert(p.end(), x0);
00425 p.insert(p.end(), x1);
00426 p.insert(p.end(), x2);
00427 }
00428
00429 int ReferenceTriangle:: no_vertices() { return 3; }
00430
00431 ex ReferenceTriangle:: vertex (int i) {
00432 return Triangle::vertex(i);
00433 }
00434
00435
00436
00437
00438
00439 Line ReferenceTriangle:: line(int i) {
00440 return Triangle:: line(i);
00441 }
00442
00443 ex ReferenceTriangle:: repr(Repr_format format) {
00444 return Triangle:: repr();
00445 }
00446
00447 string ReferenceTriangle:: str() {
00448 std::ostringstream s;
00449
00450 s <<"ReferenceTriangle";
00451 return s.str();
00452 }
00453
00454 ex ReferenceTriangle:: integrate(ex f) {
00455
00456 ex ret = eval_integ(integral(y,0,1, eval_integ(integral(x,0,1-y,f))));
00457 return ret;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466 Tetrahedron:: Tetrahedron(ex x0, ex x1, ex x2, ex x3, string subscript_) {
00467 subscript = subscript_;
00468 p.insert(p.end(), x0);
00469 p.insert(p.end(), x1);
00470 p.insert(p.end(), x2);
00471 p.insert(p.end(), x3);
00472
00473 }
00474
00475 int Tetrahedron:: no_vertices() {return 4; }
00476
00477 ex Tetrahedron:: vertex(int i) {
00478 return p[i];
00479 }
00480
00481 Line Tetrahedron:: line(int i) {
00482 int i0, i1;
00483 if ( i == 1 ) {
00484 i0 = 0; i1 = 1;
00485 } else if ( i == 2 ) {
00486 i0 = 0; i1 = 2;
00487 } else if ( i == 3 ) {
00488 i0 = 0; i1 = 3;
00489 } else if ( i == 4 ) {
00490 i0 = 1; i1 = 2;
00491 } else if ( i == 5 ) {
00492 i0 = 1; i1 = 3;
00493 } else if ( i == 6 ) {
00494 i0 = 2; i1 = 3;
00495 }
00496 Line l = Line(p[i0], p[i1], istr(subscript,i));
00497 return l;
00498 }
00499
00500 Triangle Tetrahedron :: triangle(int i) {
00501 Triangle t = Triangle(p[i%4], p[(i+1)%4], p[(i+2)%4], istr(subscript,i));
00502 return t;
00503 }
00504
00505 string Tetrahedron:: str() {
00506 std::ostringstream s;
00507
00508 s <<"Tetrahedron";
00509 return s.str();
00510 }
00511
00512
00513 ex Tetrahedron:: repr(Repr_format format) {
00514 symbol r("r"), s("s"), t("t");
00515 ex l1_repr = line(1).repr(r);
00516 ex l2_repr = line(2).repr(s);
00517 ex l3_repr = line(3).repr(t);
00518 lst ret;
00519
00520
00521 ret = lst(
00522 x == l1_repr.op(0).rhs().coeff(r,0) + l1_repr.op(0).rhs().coeff(r,1)*r
00523 + l2_repr.op(0).rhs().coeff(s,1)*s + l3_repr.op(0).rhs().coeff(t,1)*t,
00524 y == l1_repr.op(1).rhs().coeff(r,0) + l1_repr.op(1).rhs().coeff(r,1)*r
00525 + l2_repr.op(1).rhs().coeff(s,1)*s + l3_repr.op(1).rhs().coeff(t,1)*t,
00526 z == l1_repr.op(2).rhs().coeff(r,0) + l1_repr.op(1).rhs().coeff(r,1)*r
00527 + l2_repr.op(2).rhs().coeff(s,1)*s + l3_repr.op(2).rhs().coeff(t,1)*t);
00528
00529
00530 ret.append(lst(r, 0, 1));
00531 ret.append(lst(s, 0, 1 - r));
00532 ret.append(lst(t, 0, 1 - r - s));
00533
00534 return ret;
00535 }
00536
00537 ex Tetrahedron:: integrate(ex func, Repr_format format) {
00538 ex t_repr = repr();
00539
00540
00541 lst sub = lst(t_repr.op(0), t_repr.op(1), t_repr.op(2));
00542 ex intf = func.subs(sub);
00543
00544
00545 ex D;
00546 ex r = t_repr.op(3).op(0);
00547 ex s = t_repr.op(4).op(0);
00548 ex t = t_repr.op(5).op(0);
00549 ex a = t_repr.op(0).rhs().coeff(r,1);
00550 ex b = t_repr.op(0).rhs().coeff(s,1);
00551 ex c = t_repr.op(0).rhs().coeff(t,1);
00552 ex d = t_repr.op(1).rhs().coeff(r,1);
00553 ex e = t_repr.op(1).rhs().coeff(s,1);
00554 ex f = t_repr.op(1).rhs().coeff(t,1);
00555 ex g = t_repr.op(2).rhs().coeff(r,1);
00556 ex h = t_repr.op(2).rhs().coeff(s,1);
00557 ex k = t_repr.op(2).rhs().coeff(t,1);
00558
00559 D = a*(e*k-f*h) - b*(d*k-f*g) + c*(d*h - g*e);
00560
00561 intf = intf*D;
00562
00563 intf = integral(t_repr.op(5).op(0), t_repr.op(5).op(1), t_repr.op(5).op(2), intf);
00564 intf = eval_integ(intf);
00565
00566 intf = integral(t_repr.op(4).op(0), t_repr.op(4).op(1), t_repr.op(4).op(2), intf);
00567 intf = eval_integ(intf);
00568
00569 intf = integral(t_repr.op(3).op(0), t_repr.op(3).op(1), t_repr.op(3).op(2), intf);
00570 intf = eval_integ(intf);
00571
00572 return intf;
00573 }
00574
00575
00576
00577 ReferenceTetrahedron:: ReferenceTetrahedron(string subscript_) : Tetrahedron(subscript_) {
00578 subscript = subscript_;
00579 ex x0 = lst(0, 0, 0);
00580 ex x1 = lst(1, 0, 0);
00581 ex x2 = lst(0, 1, 0);
00582 ex x3 = lst(0, 0, 1);
00583
00584 p.insert(p.end(), x0);
00585 p.insert(p.end(), x1);
00586 p.insert(p.end(), x2);
00587 p.insert(p.end(), x3);
00588 }
00589
00590 int ReferenceTetrahedron:: no_vertices () {
00591 return 4;
00592 }
00593
00594 ex ReferenceTetrahedron:: vertex(int i) {
00595 return Tetrahedron:: vertex(i);
00596 }
00597
00598 Line ReferenceTetrahedron:: line(int i) {
00599 return Tetrahedron:: line(i);
00600 }
00601
00602 Triangle ReferenceTetrahedron :: triangle(int i) {
00603 return Tetrahedron:: triangle(i);
00604 }
00605
00606 string ReferenceTetrahedron:: str() {
00607 std::ostringstream s;
00608
00609 s <<"ReferenceTetrahedron";
00610 return s.str();
00611 }
00612
00613 ex ReferenceTetrahedron:: integrate(ex f, Repr_format format) {
00614
00615
00616
00617 ex intf = integral(x,0,1-y-z,f);
00618 intf = eval_integ(intf);
00619
00620 intf = integral(y,0,1-z, intf);
00621 intf = eval_integ(intf);
00622
00623 intf = integral(z,0,1, intf);
00624 intf = eval_integ(intf);
00625
00626 return intf;
00627 }
00628
00629
00630
00631
00632 lst bezier_ordinates(Tetrahedron& tetrahedra, int d) {
00633
00634
00635
00636 lst ret;
00637 ex V1 = tetrahedra.vertex(0);
00638 ex V2 = tetrahedra.vertex(1);
00639 ex V3 = tetrahedra.vertex(2);
00640 ex V4 = tetrahedra.vertex(3);
00641
00642 lst V1l = ex_to<lst>(V1);
00643 lst V2l = ex_to<lst>(V2);
00644 lst V3l = ex_to<lst>(V3);
00645 lst V4l = ex_to<lst>(V4);
00646
00647
00648 ex V1m = lst_to_matrix2(V1l);
00649 ex V2m = lst_to_matrix2(V2l);
00650 ex V3m = lst_to_matrix2(V3l);
00651 ex V4m = lst_to_matrix2(V4l);
00652
00653 int l;
00654 for (int i=0; i<= d; i++) {
00655 for (int j=0; j<= d; j++) {
00656 for (int k=0; k<= d; k++) {
00657 if ( d - i - j -k >= 0 ) {
00658 l= d - i - j -k;
00659 ex sum = (i*V1m + j*V2m + k*V3m + l*V4m)/d;
00660 ret.append(matrix_to_lst2(sum.evalm()));
00661 }
00662 }
00663 }
00664 }
00665
00666
00667 return ret;
00668 }
00669
00670
00671
00672 lst bezier_ordinates(Triangle& triangle, int d) {
00673
00674
00675
00676 lst ret;
00677 ex V1 = triangle.vertex(0);
00678 ex V2 = triangle.vertex(1);
00679 ex V3 = triangle.vertex(2);
00680
00681 lst V1l = ex_to<lst>(V1);
00682 lst V2l = ex_to<lst>(V2);
00683 lst V3l = ex_to<lst>(V3);
00684
00685 ex V1m = lst_to_matrix2(V1l);
00686 ex V2m = lst_to_matrix2(V2l);
00687 ex V3m = lst_to_matrix2(V3l);
00688
00689 int k;
00690 for (int i=0; i<= d; i++) {
00691 for (int j=0; j<= d-i; j++) {
00692 k= d - i - j;
00693 ex sum = (i*V1m + j*V2m + k*V3m)/d;
00694 ret.append(matrix_to_lst2(sum.evalm()));
00695 }
00696 }
00697
00698
00699 return ret;
00700 }
00701
00702 ex barycenter_triangle(ex p0, ex p1, ex p2) {
00703
00704 symbol b0("b0"), b1("b1"), b2("b2");
00705 ex eq1 = x == b0*p0.op(0) + b1*p1.op(0) + b2*p2.op(0);
00706 ex eq2 = y == b0*p0.op(1) + b1*p1.op(1) + b2*p2.op(1);
00707 ex eq3 = 1 == b0 + b1 + b2;
00708
00709 ex sol = lsolve(lst(eq1, eq2, eq3), lst(b0, b1, b2));
00710
00711 return sol;
00712 }
00713
00714 ex barycenter_tetrahedron(ex p0, ex p1, ex p2, ex p3) {
00715 symbol b0("b0"), b1("b1"), b2("b2"), b3("b3");
00716
00717
00718 ex eq1 = x == b0*p0.op(0) + b1*p1.op(0) + b2*p2.op(0) + b3*p3.op(0);
00719 ex eq2 = y == b0*p0.op(1) + b1*p1.op(1) + b2*p2.op(1) + b3*p3.op(1);
00720 ex eq3 = z == b0*p0.op(2) + b1*p1.op(2) + b2*p2.op(2) + b3*p3.op(2);
00721 ex eq4 = 1 == b0 + b1 + b2 +b3;
00722
00723 ex sol = lsolve(lst(eq1, eq2, eq3, eq4), lst(b0, b1, b2, b3));
00724
00725 return sol;
00726
00727 }
00728
00729
00730
00731