[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[667] | 2 | // Copyright (C) 1999-2017 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[140] | 4 | |
---|
| 5 | #include "conv_fF.h" |
---|
| 6 | #include "fF_genotype.h" |
---|
| 7 | #include <common/nonstd_stl.h> |
---|
[185] | 8 | #include <common/Convert.h> |
---|
[140] | 9 | |
---|
[177] | 10 | GenoConv_fF0::GenoConv_fF0() |
---|
[176] | 11 | { |
---|
[667] | 12 | name = "10-parameter Foraminifera encoding"; |
---|
[176] | 13 | in_format = 'F'; |
---|
| 14 | out_format = '0'; |
---|
| 15 | mapsupport = 0; |
---|
| 16 | cosines = new double[fF_LATITUDE_NUM]; |
---|
| 17 | sines = new double[fF_LATITUDE_NUM]; |
---|
[178] | 18 | precompute_cos_and_sin(); |
---|
[140] | 19 | } |
---|
| 20 | |
---|
[177] | 21 | GenoConv_fF0::~GenoConv_fF0() |
---|
[176] | 22 | { |
---|
| 23 | delete[] cosines; |
---|
| 24 | delete[] sines; |
---|
[174] | 25 | } |
---|
[140] | 26 | |
---|
[256] | 27 | Part *GenoConv_fF0::addNewPart(Model *m, const fF_chamber3d* c) |
---|
| 28 | { |
---|
| 29 | Part *part = m->addNewPart(Part::SHAPE_ELLIPSOID); |
---|
| 30 | part->p = Pt3D(c->centerX, c->centerY, c->centerZ); |
---|
| 31 | Pt3D hole = Pt3D(c->holeX, c->holeY, c->holeZ); |
---|
| 32 | Orient o; |
---|
| 33 | o.lookAt(part->p - hole); |
---|
| 34 | part->setOrient(o); |
---|
| 35 | return part; |
---|
| 36 | } |
---|
| 37 | |
---|
[176] | 38 | SString GenoConv_fF0::convert(SString &in, MultiMap *map) |
---|
| 39 | { |
---|
| 40 | fF_growth_params gp; |
---|
[348] | 41 | if (!gp.load(in.c_str())) //invalid input genotype? |
---|
[176] | 42 | return ""; //so we return an invalid f0 genotype |
---|
[140] | 43 | |
---|
[176] | 44 | Model m; |
---|
| 45 | m.open(); |
---|
[177] | 46 | |
---|
| 47 | m.vis_style = "foram"; //dedicated visual look for Foraminifera |
---|
| 48 | |
---|
[176] | 49 | fF_chamber3d **chambers = new fF_chamber3d*[gp.number_of_chambers]; |
---|
[177] | 50 | for (int i = 0; i < gp.number_of_chambers; i++) |
---|
[667] | 51 | createSphere(i, chambers, gp.radius0x, gp.radius0y, gp.radius0z, gp.translation, gp.angle1, gp.angle2, gp.scalex, gp.scaley, gp.scalez); |
---|
[174] | 52 | |
---|
[256] | 53 | Part *p1 = addNewPart(&m, chambers[0]); |
---|
[667] | 54 | p1->scale = Pt3D(gp.radius0x, gp.radius0y, gp.radius0z); //size of the initial chamber |
---|
[256] | 55 | for (int i = 1; i < gp.number_of_chambers; i++) |
---|
[177] | 56 | { |
---|
[256] | 57 | Part *p2 = addNewPart(&m, chambers[i]); |
---|
[176] | 58 | p2->scale = p1->scale.entrywiseProduct(Pt3D(gp.scalex, gp.scaley, gp.scalez)); //each part's scale is its predecessor's scale * scaling |
---|
[544] | 59 | m.addNewJoint(p1, p2, Joint::SHAPE_FIXED); //all parts must be connected |
---|
[256] | 60 | p1 = p2; |
---|
[176] | 61 | } |
---|
[174] | 62 | |
---|
[176] | 63 | for (int i = 0; i < gp.number_of_chambers; i++) |
---|
| 64 | delete chambers[i]; |
---|
[256] | 65 | delete[] chambers; |
---|
[174] | 66 | |
---|
[176] | 67 | m.close(); |
---|
[544] | 68 | return m.getF0Geno().getGenes(); |
---|
[140] | 69 | } |
---|
[174] | 70 | |
---|
[667] | 71 | void GenoConv_fF0::createSphere(int which, fF_chamber3d **chambers, double radius0x, double radius0y, double radius0z, double translation, double alpha_, double gamma_, double kx_, double ky_, double kz_) |
---|
[176] | 72 | { |
---|
[667] | 73 | chambers[which] = new fF_chamber3d(0.0, 0.0, 0.0, |
---|
| 74 | radius0x, radius0y, radius0z, radius0x * kx_, 0.0, 0.0, |
---|
| 75 | radius0x * translation, 0.0, 0.0, 0.0, 0.0); |
---|
[176] | 76 | if (which == 0) |
---|
[667] | 77 | chambers[which]->points = generate_points(chambers[which]); |
---|
| 78 | if (which > 0) |
---|
| 79 | { |
---|
| 80 | chambers[which]->radius_x = get_radius(chambers[which - 1]->radius_x, kx_, chambers[0]->radius_x); |
---|
| 81 | chambers[which]->radius_y = get_radius(chambers[which - 1]->radius_y, ky_, chambers[0]->radius_y); |
---|
| 82 | chambers[which]->radius_z = get_radius(chambers[which - 1]->radius_z, kz_, chambers[0]->radius_z); |
---|
| 83 | |
---|
[176] | 84 | /* new growth vector length */ |
---|
[667] | 85 | double len = chambers[which]->radius_y * translation; |
---|
| 86 | double max_radius = fF_TOO_MUCH * chambers[which]->radius_y; |
---|
| 87 | if (fabs(len) > (max_radius)) |
---|
| 88 | len = ((len < 0) ? (-1) : 1) * max_radius; |
---|
| 89 | if (len == 0) |
---|
[176] | 90 | len = -0.0000001; |
---|
[174] | 91 | |
---|
[176] | 92 | /* aperture of the previous chamber */ |
---|
| 93 | double pzx = chambers[which - 1]->holeX; |
---|
| 94 | double pzy = chambers[which - 1]->holeY; |
---|
| 95 | double pzz = chambers[which - 1]->holeZ; |
---|
[174] | 96 | |
---|
[176] | 97 | //center of the previous chamber |
---|
| 98 | double pcx = chambers[which - 1]->centerX; |
---|
| 99 | double pcy = chambers[which - 1]->centerY; |
---|
[319] | 100 | //double pcz = chambers[which - 1]->centerZ; //not used |
---|
[174] | 101 | |
---|
[176] | 102 | /* aperture of the next to last chamber */ |
---|
| 103 | double ppx; |
---|
| 104 | double ppy; |
---|
[319] | 105 | //double ppz; //not used |
---|
[174] | 106 | |
---|
[177] | 107 | if (which == 1) |
---|
| 108 | { |
---|
[176] | 109 | ppx = pcx; |
---|
| 110 | ppy = pcy; |
---|
[319] | 111 | //ppz = pcz; |
---|
[176] | 112 | } |
---|
[177] | 113 | else |
---|
| 114 | { |
---|
[176] | 115 | ppx = chambers[which - 2]->holeX; |
---|
| 116 | ppy = chambers[which - 2]->holeY; |
---|
[319] | 117 | //ppz = chambers[which - 2]->holeZ; |
---|
[176] | 118 | } |
---|
[174] | 119 | |
---|
[176] | 120 | double pzxprim = pzx - ppx; |
---|
| 121 | double pzyprim = pzy - ppy; |
---|
| 122 | double angle; |
---|
[174] | 123 | |
---|
[185] | 124 | angle = Convert::atan_2(pzyprim, pzxprim); |
---|
[176] | 125 | double alpha = angle - alpha_; |
---|
[174] | 126 | |
---|
[176] | 127 | double gamma = chambers[which - 1]->phi + gamma_; |
---|
[174] | 128 | |
---|
[176] | 129 | double wx = len * cos(alpha); |
---|
| 130 | double wy = len * sin(alpha); |
---|
| 131 | double wz = len * sin(alpha) * sin(gamma); |
---|
[174] | 132 | |
---|
[176] | 133 | /*center of the new sphere*/ |
---|
| 134 | double x = pzx + wx; |
---|
| 135 | double y = pzy + wy; |
---|
| 136 | double z = pzz + wz; |
---|
[174] | 137 | |
---|
[667] | 138 | chambers[which]->centerX = x; |
---|
| 139 | chambers[which]->centerY = y; |
---|
| 140 | chambers[which]->centerZ = z; |
---|
| 141 | chambers[which]->vectorTfX = wx; |
---|
| 142 | chambers[which]->vectorTfY = wy; |
---|
| 143 | chambers[which]->vectorTfZ = wz; |
---|
| 144 | chambers[which]->beta = alpha; |
---|
| 145 | chambers[which]->phi = gamma; |
---|
[174] | 146 | |
---|
[667] | 147 | chambers[which]->points = generate_points(chambers[which]); |
---|
| 148 | search_hid(which, chambers); |
---|
[176] | 149 | int pun; |
---|
[667] | 150 | pun = find_hole(which, pzx, pzy, pzz, chambers); |
---|
| 151 | if (pun < 0) //should never happen |
---|
| 152 | { |
---|
| 153 | logPrintf("GenoConv_fF0", "createSphere", LOG_ERROR, "find_hole(%d) returned %d", which, pun); |
---|
| 154 | pun = 0; |
---|
| 155 | } |
---|
| 156 | chambers[which]->holeX = chambers[which]->points[pun].x; |
---|
| 157 | chambers[which]->holeY = chambers[which]->points[pun].y; |
---|
| 158 | chambers[which]->holeZ = chambers[which]->points[pun].z; |
---|
| 159 | } |
---|
| 160 | } |
---|
[174] | 161 | |
---|
[667] | 162 | double GenoConv_fF0::get_radius(double prev_radius, double scale, double start_radius) |
---|
| 163 | { |
---|
| 164 | double radius = prev_radius * scale; |
---|
| 165 | double min_radius = fF_TOO_LITTLE*start_radius; |
---|
| 166 | if (radius < min_radius) { |
---|
| 167 | radius = min_radius; |
---|
[176] | 168 | } |
---|
[667] | 169 | |
---|
| 170 | return radius; |
---|
[174] | 171 | } |
---|
| 172 | |
---|
[178] | 173 | void GenoConv_fF0::precompute_cos_and_sin() |
---|
[176] | 174 | { |
---|
[667] | 175 | double angle = M_PI * 2 / fF_LATITUDE_NUM; |
---|
| 176 | for (int i = 0; i < fF_LATITUDE_NUM; i++) |
---|
[176] | 177 | { |
---|
[667] | 178 | cosines[i] = cos(i * angle); |
---|
| 179 | sines[i] = sin(i * angle); |
---|
[176] | 180 | } |
---|
[174] | 181 | } |
---|
| 182 | |
---|
[667] | 183 | fF_point* GenoConv_fF0::generate_points(fF_chamber3d *chamber) |
---|
[176] | 184 | { |
---|
[667] | 185 | double cenx = chamber->centerX; |
---|
| 186 | double ceny = chamber->centerY; |
---|
| 187 | double cenz = chamber->centerZ; |
---|
[174] | 188 | |
---|
[667] | 189 | double rx = chamber->radius_x; |
---|
| 190 | double ry = chamber->radius_y; |
---|
| 191 | double rz = chamber->radius_z; |
---|
[174] | 192 | |
---|
[178] | 193 | fF_point *points = new fF_point[fF_SIZE]; |
---|
[174] | 194 | |
---|
[177] | 195 | for (int i = 0; i < fF_LONGITUDE_NUM; i++) |
---|
| 196 | { |
---|
| 197 | double y = ceny + ry * cosines[i]; |
---|
[174] | 198 | |
---|
[177] | 199 | for (int j = 0; j < fF_LATITUDE_NUM; j++) |
---|
| 200 | { |
---|
| 201 | double x = cenx + rx * cosines[j] * sines[i]; |
---|
| 202 | double z = cenz + rz * sines[j] * sines[i]; |
---|
[178] | 203 | fF_point &p = points[(i * fF_LATITUDE_NUM) + j]; |
---|
| 204 | p.x = x; |
---|
| 205 | p.y = y; |
---|
| 206 | p.z = z; |
---|
| 207 | p.inside = false; |
---|
[177] | 208 | } |
---|
| 209 | } |
---|
[176] | 210 | return points; |
---|
| 211 | |
---|
[174] | 212 | } |
---|
| 213 | |
---|
[667] | 214 | template<typename T> T Square(T x) { return x * x; } |
---|
| 215 | |
---|
[176] | 216 | double GenoConv_fF0::dist(double x1, double y1, double z1, double x2, double y2, double z2) |
---|
| 217 | { |
---|
[667] | 218 | return sqrt(Square(x2 - x1) + Square(y2 - y1) + Square(z2 - z1)); |
---|
[174] | 219 | } |
---|
| 220 | |
---|
[667] | 221 | void GenoConv_fF0::search_hid(int nr, fF_chamber3d **chambers) |
---|
[176] | 222 | { |
---|
[177] | 223 | for (int i = 0; i < nr; i++) |
---|
| 224 | { |
---|
[667] | 225 | fF_chamber3d *chamber = chambers[i]; |
---|
[174] | 226 | |
---|
[667] | 227 | double rx_sq = Square(chamber->radius_x); |
---|
| 228 | double ry_sq = Square(chamber->radius_y); |
---|
| 229 | double rz_sq = Square(chamber->radius_z); |
---|
[174] | 230 | |
---|
[177] | 231 | for (int j = 0; j < fF_AMOUNT; j++) |
---|
| 232 | { |
---|
[667] | 233 | fF_point &p = chambers[nr]->points[j]; |
---|
[174] | 234 | |
---|
[667] | 235 | double upx = Square(p.x - chamber->centerX); |
---|
| 236 | double upy = Square(p.y - chamber->centerY); |
---|
| 237 | double upz = Square(p.z - chamber->centerZ); |
---|
[174] | 238 | |
---|
[667] | 239 | double expx = upx / rx_sq; |
---|
| 240 | double expy = upy / ry_sq; |
---|
| 241 | double expz = upz / rz_sq; |
---|
[174] | 242 | |
---|
[667] | 243 | double result = expx + expy + expz; |
---|
[174] | 244 | |
---|
[177] | 245 | if (result < fF_THICK_RATIO) |
---|
| 246 | { |
---|
[178] | 247 | p.inside = true; |
---|
[176] | 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|
[174] | 251 | } |
---|
| 252 | |
---|
[667] | 253 | int GenoConv_fF0::find_hole(int which, double x, double y, double z, fF_chamber3d **chambers) |
---|
[176] | 254 | { |
---|
[177] | 255 | int found = -1; |
---|
| 256 | double distsq_found; |
---|
[174] | 257 | |
---|
[177] | 258 | for (int i = 0; i < fF_AMOUNT; i++) |
---|
| 259 | { |
---|
[178] | 260 | fF_point &p = chambers[which]->points[i]; |
---|
| 261 | if (!p.inside) //it is not inside another chamber |
---|
[176] | 262 | { |
---|
[667] | 263 | double distancesq = Square(p.x - x) + Square(p.y - y) + Square(p.z - z); |
---|
[177] | 264 | if (found < 0) |
---|
| 265 | { |
---|
[176] | 266 | found = i; |
---|
[177] | 267 | distsq_found = distancesq; |
---|
[176] | 268 | } |
---|
[177] | 269 | if (distancesq < distsq_found) |
---|
| 270 | { |
---|
| 271 | if (which != 0) |
---|
| 272 | { |
---|
[176] | 273 | bool good = true; |
---|
[177] | 274 | for (int j = 0; j < which && good; j++) |
---|
| 275 | { |
---|
[667] | 276 | fF_chamber3d *chamber = chambers[j]; |
---|
[174] | 277 | |
---|
[667] | 278 | double rx_sq = Square(chamber->radius_x); |
---|
| 279 | double ry_sq = Square(chamber->radius_y); |
---|
| 280 | double rz_sq = Square(chamber->radius_z); |
---|
[174] | 281 | |
---|
[667] | 282 | double upx = Square(p.x - chamber->centerX); |
---|
| 283 | double upy = Square(p.y - chamber->centerY); |
---|
| 284 | double upz = Square(p.z - chamber->centerZ); |
---|
[174] | 285 | |
---|
[667] | 286 | double expx = upx / rx_sq; |
---|
| 287 | double expy = upy / ry_sq; |
---|
| 288 | double expz = upz / rz_sq; |
---|
[174] | 289 | |
---|
[667] | 290 | double result = expx + expy + expz; |
---|
[177] | 291 | if (result < 1.0) |
---|
| 292 | { |
---|
| 293 | good = false; |
---|
[176] | 294 | } |
---|
| 295 | } |
---|
[177] | 296 | if (good) |
---|
| 297 | { |
---|
[176] | 298 | found = i; |
---|
[177] | 299 | distsq_found = distancesq; |
---|
[176] | 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
[174] | 305 | |
---|
[177] | 306 | return found; |
---|
[176] | 307 | } |
---|