//+
SetFactory("OpenCASCADE");

// Raggi 5KW:
ri = 0.0525;
re = 0.0735;

Box(1) = {-1, -0.928, 0.317, 2, 1.856, 2.458};

box_l = 0.25;
box_w = 0.18;
box_h = 0.18;
angle = -0.82085; // angolo rotazione motore attorno asse z

Box(2) = {-box_l, -box_w/2.0, -box_h/2.0, box_l, box_w, box_h};

Rotate {{0, 0, 1}, {0, 0, 0}, angle} {
  Volume{2}; 
}

// Thruster center:
cx = 1.1484-0.06;
cy = -0.9719-0.06;
cz = 1.2555;

Translate {cx, cy, cz} {
  Volume{2}; 
}

// 
thruster_dz = 0.30; // distanza verticale tra i due motori
Translate {0, 0, thruster_dz} {
  Duplicata { Volume{2}; }
}

BooleanDifference{ Volume{2}; Volume{3}; Delete; }{ Volume{1}; }

Delete {
  Volume{1}; Volume{3}; Volume{2}; 
}

BooleanDifference{ Surface{3}; Delete; }{ Surface{18}; Surface{11}; Delete; }
BooleanDifference{ Surface{2}; Delete; }{ Surface{20}; Surface{13}; Delete; }

//               cx        cy         cz        ax         ay         az       r
Cylinder(1) = {2.518425, 0.112531, 0.552163, -0.001858, -0.000790, 0.011535, 0.901, 2*Pi};
Cylinder(2) = {-2.89143, 0.004023, 0.608102,  0.005097,  0.000011, 0.028430, 1.192, 2*Pi};

Delete {
  Volume{1}; Volume{2}; 
}

// Thruster exit:
np = newp;
Point(np)   = {0.0, 0.0, 0.0, 1};
Point(np+1) = {ri , 0.0, 0.0, 1};
Point(np+2) = {0.0, 0.0, ri , 1};
Point(np+3) = {-ri, 0.0, 0.0, 1};
Point(np+4) = {0.0, 0.0, -ri, 1};
Point(np+5) = {re , 0.0, 0.0, 1};
Point(np+6) = {0.0, 0.0, re , 1};
Point(np+7) = {-re, 0.0, 0.0, 1};
Point(np+8) = {0.0, 0.0, -re, 1};

// First solar panel

panel_l = 2.73;
panel_w = 2.216;
panel_h = 0.024;
panel_dl = 0.058;
Box(1) = {-panel_w/2.0, -4.8245, 1.4393, panel_w, panel_l, panel_h};

Translate {0, -panel_l-panel_dl, 0.0} {
  Duplicata { Volume{1}; }
}
Translate {0, -2.0*(panel_l+panel_dl), 0.0} {
  Duplicata { Volume{1}; }
}

Delete {
  Volume{1}; Volume{2}; Volume{3}; 
}

// Satellite lines:
Transfinite Line {10, 1, 9, 8, 6, 7, 12, 2, 11, 4} = 13 Using Progression 1;
Transfinite Line {3} = 10 Using Progression 1;
Transfinite Line {45, 43} = 7 Using Progression 1;

Circle(91) = {35, 33, 36};
Circle(92) = {36, 33, 37};
Circle(93) = {37, 33, 34};
Circle(94) = {34, 33, 35};
Circle(95) = {39, 33, 40};
Circle(96) = {40, 33, 41};
Circle(97) = {41, 33, 38};
Circle(98) = {38, 33, 39};

Delete {
  Surface{9}; Surface{16}; 
}

Line Loop(44) = {94, 91, 92, 93};
Plane Surface(44) = {44};
Line Loop(45) = {98, 95, 96, 97};
Plane Surface(45) = {45};
Rotate {{0, 0, 1}, {0, 0, 0}, -angle} {
  Surface{45}; Surface{44}; 
}
Translate {cx,cy, cz} {
  Surface{45}; Surface{44}; 
  }

Translate {0, 0, thruster_dz} {
  Duplicata { Surface{45}; Surface{44}; }
}

Line Loop(53) = {21, -22, -18, 14};
Line Loop(54) = {103, 104, 105, 106};
Plane Surface(47) = {53, 54};
Line Loop(57) = {36, -37, -33, 29};
Line Loop(58) = {119, 120, 121, 122};
Plane Surface(48) = {57, 58};

//Boom:
cl__1 = 1e+22;
np=newp;

Point(np) = {-1.10799754646, -1.963806952456, 1.466300040082, cl__1};
Point(np+1) = {-1.06799754646, -1.969417905651, 1.466300040082, cl__1};
Point(np+2) = {-1.06799754646, -2.066499987289, 1.466300040082, cl__1};
Point(np+3) = {-1.10799754646, -2.066499987289, 1.466300040082, cl__1};
Point(np+4) = {-1.06799754646, -2.066499987289, 1.438300040082, cl__1};
Point(np+5) = {-1.10799754646, -2.066499987289, 1.438300040082, cl__1};
Point(np+6) = {-1.10799754646, -1.963806952456, 1.438300040082, cl__1};
Point(np+7) = {-1.06799754646, -1.969417905651, 1.438300040082, cl__1};
Point(np+8) = {-0.007715396753268, -1.20190163003, 1.466300040082, cl__1};
Point(np+9) = {-0.007715396753268, -1.20190163003, 1.438300040082, cl__1};
Point(np+10) = {-0.02413382274289, -1.179220464098, 1.466300040082, cl__1};
Point(np+11) = {-0.02413382274289, -1.179220464098, 1.438300040082, cl__1};

Line(127) = {103, 102};
Line(128) = {102, 100};
Line(129) = {100, 101};
Line(130) = {101, 103};
Line(131) = {101, 98};
Line(132) = {103, 104};
Line(133) = {100, 99};
Line(134) = {102, 105};
Line(135) = {105, 104};
Line(136) = {104, 98};
Line(137) = {98, 99};
Line(138) = {99, 105};
Line(139) = {105, 107};
Line(140) = {107, 106};
Line(141) = {106, 99};
Line(142) = {98, 108};
Line(143) = {108, 106};
Line(144) = {108, 109};
Line(145) = {109, 107};
Line(146) = {109, 104};
Line Loop(61) = {127, 128, 129, 130};
Plane Surface(49) = {61};
Line Loop(62) = {131, -136, -132, -130};
Plane Surface(50) = {62};
Line Loop(63) = {137, -133, 129, 131};
Plane Surface(51) = {63};
Line Loop(64) = {132, -135, -134, -127};
Plane Surface(52) = {64};
Line Loop(65) = {138, -134, 128, 133};
Plane Surface(53) = {65};
Line Loop(66) = {142, 143, 141, -137};
Plane Surface(54) = {66};
Line Loop(67) = {141, 138, 139, 140};
Plane Surface(55) = {67};
Line Loop(68) = {135, -146, 145, -139};
Plane Surface(56) = {68};
Line Loop(69) = {136, 142, 144, 146};
Plane Surface(57) = {69};
Line Loop(70) = {145, 140, -143, 144};
Plane Surface(58) = {70};
Surface Loop(7) = {55, 54, 57, 50, 51, 53, 52, 56, 58, 49};

Volume(1) = {7};

Rotate {{0, 1, 0}, {0, 0, 1.4523}, Pi} {
  Duplicata { Volume{1}; }
}

Delete {
  Volume{2}; Volume{1}; 
}

// End of Boom

Delete {
  Surface{67}; Surface{58}; 
}

Line(167) = {108, 117};
Line(168) = {106, 113};
Line(169) = {107, 110};
Line(170) = {109, 115};
Line Loop(81) = {168, 150, -169, 140};
Plane Surface(69) = {81};
Line Loop(82) = {143, 168, -166, -167};
Plane Surface(70) = {82};
Line Loop(83) = {167, -155, -170, -144};
Plane Surface(71) = {83};
Line Loop(84) = {170, 152, -169, -145};
Plane Surface(72) = {84};
Delete {
  Surface{44}; Surface{45}; 
}
//+
Delete {
  Surface{46}; 
}

Line Loop(85) = {108, 109, 110, 107};
Plane Surface(73) = {85};
Line Loop(88) = {124, 125, 126, 123};
Plane Surface(75) = {88};

Delete {
  Surface{2}; 
}

Line Loop(91) = {6, -7, -8, 43, 20, -27, -25, 44, 35, -42, -40, 45};
Plane Surface(77) = {91};

Line(171) = {79, 75};
Line(172) = {80, 76};
Line(173) = {81, 77};
Line(174) = {78, 74};
Line(175) = {95, 91};
Line(176) = {96, 92};
Line(177) = {97, 93};
Line(178) = {94, 90};

Line Loop(93) = {107, 171, -103, -174};
Plane Surface(78) = {93};
Line Loop(94) = {171, 104, -172, -108};
Plane Surface(79) = {94};
Line Loop(95) = {172, 105, -173, -109};
Plane Surface(80) = {95};
Line Loop(96) = {173, 106, -174, -110};
Plane Surface(81) = {96};
Line Loop(97) = {178, 119, -175, -123};
Plane Surface(82) = {97};
Line Loop(98) = {175, 120, -176, -124};
Plane Surface(83) = {98};
Line Loop(99) = {176, 121, -177, -125};
Plane Surface(84) = {99};
Line Loop(100) = {177, 122, -178, -126};
Plane Surface(85) = {100};

SetFactory("Built-in");

// Thrusters exit:
Transfinite Line {171, 172, 173, 174, 175, 176, 177, 178} = 3 Using Progression 1;
Transfinite Line {110, 107, 108, 109, 123, 124, 125, 126} = 7 Using Progression 1;
Transfinite Line {119, 120, 121, 122} = 7 Using Progression 1;
Transfinite Line {103, 104, 105, 106} = 11 Using Progression 1;
Transfinite Line {36, 29, 33, 37, 21, 14, 18, 22} = 7 Using Progression 1;
Transfinite Line {15, 23, 13, 28, 30, 38, 17, 32, 42, 27, 31, 16} = 3 Using Progression 1;

// Antenna piccola:
Transfinite Line {49} = 31 Using Progression 1;
Transfinite Line {51} = 25 Using Progression 1;

// antenna grande:
Transfinite Line {52} = 35 Using Progression 1;
Transfinite Line {54} = 27 Using Progression 1;

// Solar panels:
Transfinite Line {65, 66, 64, 63, 78, 77, 76, 75, 90, 89, 88, 87} = 13 Using Progression 1;
Transfinite Line {58, 56, 62, 60, 70, 68, 74, 72, 86, 84, 82, 80} = 17 Using Progression 1;

// Boom:
Transfinite Line {139, 146, 141, 142, 147, 149, 156, 151} = 9 Using Progression 1;

///////////////////////// FAR FIELD /////////////////////////////////////////////

//Coherence;
//SyncModel;

Merge "FarField.geo";
