[140] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[140] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
| 5 | #include "conv_fF.h" |
---|
| 6 | #include "fF_genotype.h" |
---|
| 7 | #include <frams/model/model.h> |
---|
| 8 | #include <common/nonstd_stl.h> |
---|
[185] | 9 | #include <common/Convert.h> |
---|
[140] | 10 | |
---|
[177] | 11 | GenoConv_fF0::GenoConv_fF0() |
---|
[176] | 12 | { |
---|
| 13 | name = "7-value Foraminifera encoding"; |
---|
| 14 | in_format = 'F'; |
---|
| 15 | out_format = '0'; |
---|
| 16 | mapsupport = 0; |
---|
| 17 | cosines = new double[fF_LATITUDE_NUM]; |
---|
| 18 | sines = new double[fF_LATITUDE_NUM]; |
---|
[178] | 19 | precompute_cos_and_sin(); |
---|
[140] | 20 | } |
---|
| 21 | |
---|
[177] | 22 | GenoConv_fF0::~GenoConv_fF0() |
---|
[176] | 23 | { |
---|
| 24 | delete[] cosines; |
---|
| 25 | delete[] sines; |
---|
[174] | 26 | } |
---|
[140] | 27 | |
---|
[176] | 28 | SString GenoConv_fF0::convert(SString &in, MultiMap *map) |
---|
| 29 | { |
---|
| 30 | fF_growth_params gp; |
---|
| 31 | if (!gp.load(in)) //invalid input genotype? |
---|
| 32 | return ""; //so we return an invalid f0 genotype |
---|
[140] | 33 | |
---|
[177] | 34 | double div_radius_length = 1; //div_radius_length=1 or kx=ky=kz=1 |
---|
[176] | 35 | double radius = 1; |
---|
[140] | 36 | |
---|
[176] | 37 | Model m; |
---|
| 38 | m.open(); |
---|
[177] | 39 | |
---|
| 40 | m.vis_style = "foram"; //dedicated visual look for Foraminifera |
---|
| 41 | |
---|
[176] | 42 | // subsequent parts (chambers) are placed relative to the previous part's orientation and location |
---|
| 43 | Part *p1, *p2; |
---|
[140] | 44 | |
---|
[176] | 45 | fF_chamber3d **chambers = new fF_chamber3d*[gp.number_of_chambers]; |
---|
[140] | 46 | |
---|
[177] | 47 | for (int i = 0; i < gp.number_of_chambers; i++) |
---|
[176] | 48 | createSphere(i, chambers, radius, div_radius_length, gp.translation, gp.angle1, gp.angle2, gp.scalex, gp.scaley, gp.scalez); |
---|
[174] | 49 | |
---|
[176] | 50 | p1 = m.addNewPart(Part::SHAPE_ELLIPSOID); |
---|
| 51 | p1->p = Pt3D(chambers[0]->centerX, chambers[0]->centerY, chambers[0]->centerZ); |
---|
[174] | 52 | |
---|
| 53 | |
---|
[177] | 54 | for (int i = 1; i < gp.number_of_chambers; i++, p1 = p2) |
---|
| 55 | { |
---|
[176] | 56 | p2 = m.addNewPart(Part::SHAPE_ELLIPSOID); |
---|
| 57 | p2->scale = p1->scale.entrywiseProduct(Pt3D(gp.scalex, gp.scaley, gp.scalez)); //each part's scale is its predecessor's scale * scaling |
---|
[174] | 58 | |
---|
[176] | 59 | p2->p = Pt3D(chambers[i]->centerX, chambers[i]->centerY, chambers[i]->centerZ); |
---|
[174] | 60 | |
---|
[176] | 61 | m.addNewJoint(p1, p2, Joint::SHAPE_SOLID); //all parts must be connected |
---|
| 62 | } |
---|
[174] | 63 | |
---|
[176] | 64 | for (int i = 0; i < gp.number_of_chambers; i++) |
---|
| 65 | delete chambers[i]; |
---|
| 66 | delete[]chambers; |
---|
[174] | 67 | |
---|
[176] | 68 | m.close(); |
---|
| 69 | return m.getF0Geno().getGene(); |
---|
[140] | 70 | } |
---|
[174] | 71 | |
---|
| 72 | void GenoConv_fF0::createSphere(int which, fF_chamber3d **chambers, double radius_, double div_radius_length_, double div_vector_length_, |
---|
[176] | 73 | double alpha_, double gamma_, double kx_, double ky_, double kz_) |
---|
| 74 | { |
---|
| 75 | chambers[which] = new fF_chamber3d(0.0f, 0.0f, 0.0f, |
---|
| 76 | (float)radius_, (float)radius_ * (float)kx_, 0.0f, 0.0f, |
---|
| 77 | (float)(radius_ * div_vector_length_), 0.0f, 0.0f, 0.0f, 0.0f); |
---|
| 78 | if (which == 0) |
---|
| 79 | chambers[which]->points = generate_points(chambers[which], which, kx_, ky_, kz_); |
---|
| 80 | if (which > 0) { |
---|
| 81 | /* old radius */ |
---|
| 82 | double radiusOld, radius; |
---|
| 83 | radiusOld = chambers[which - 1]->radius; |
---|
| 84 | radius = div_radius_length_ * radiusOld; |
---|
| 85 | /* new growth vector length */ |
---|
| 86 | double len = radius * div_vector_length_; |
---|
| 87 | if (radius < fF_TOO_LITTLE) { |
---|
| 88 | radius = fF_TOO_LITTLE; |
---|
| 89 | if (fabs(len) >(fF_TOO_MUCH * radius)) { |
---|
| 90 | len = ((len < 0) ? (-1) : 1) * fF_TOO_MUCH * radius; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | if (len == 0) { |
---|
| 94 | len = -0.0000001; |
---|
| 95 | } |
---|
[174] | 96 | |
---|
[176] | 97 | /* aperture of the previous chamber */ |
---|
| 98 | double pzx = chambers[which - 1]->holeX; |
---|
| 99 | double pzy = chambers[which - 1]->holeY; |
---|
| 100 | double pzz = chambers[which - 1]->holeZ; |
---|
[174] | 101 | |
---|
[176] | 102 | //center of the previous chamber |
---|
| 103 | double pcx = chambers[which - 1]->centerX; |
---|
| 104 | double pcy = chambers[which - 1]->centerY; |
---|
| 105 | double pcz = chambers[which - 1]->centerZ; |
---|
[174] | 106 | |
---|
[176] | 107 | /* aperture of the next to last chamber */ |
---|
| 108 | double ppx; |
---|
| 109 | double ppy; |
---|
| 110 | double ppz; |
---|
[174] | 111 | |
---|
[177] | 112 | if (which == 1) |
---|
| 113 | { |
---|
[176] | 114 | ppx = pcx; |
---|
| 115 | ppy = pcy; |
---|
| 116 | ppz = pcz; |
---|
| 117 | } |
---|
[177] | 118 | else |
---|
| 119 | { |
---|
[176] | 120 | ppx = chambers[which - 2]->holeX; |
---|
| 121 | ppy = chambers[which - 2]->holeY; |
---|
| 122 | ppz = chambers[which - 2]->holeZ; |
---|
| 123 | } |
---|
[174] | 124 | |
---|
[176] | 125 | double pzxprim = pzx - ppx; |
---|
| 126 | double pzyprim = pzy - ppy; |
---|
| 127 | double angle; |
---|
[174] | 128 | |
---|
[185] | 129 | angle = Convert::atan_2(pzyprim, pzxprim); |
---|
[176] | 130 | double alpha = angle - alpha_; |
---|
[174] | 131 | |
---|
| 132 | |
---|
[176] | 133 | double gamma = chambers[which - 1]->phi + gamma_; |
---|
[174] | 134 | |
---|
[176] | 135 | /* x */ |
---|
| 136 | double wx = len * cos(alpha); |
---|
| 137 | /* y */ |
---|
| 138 | double wy = len * sin(alpha); |
---|
| 139 | /* y */ |
---|
| 140 | double wz = len * sin(alpha) * sin(gamma); |
---|
[174] | 141 | |
---|
[176] | 142 | /*center of the new sphere*/ |
---|
| 143 | double x = pzx + wx; |
---|
| 144 | double y = pzy + wy; |
---|
| 145 | double z = pzz + wz; |
---|
[174] | 146 | |
---|
[176] | 147 | chambers[which]->centerX = (float)x; |
---|
| 148 | chambers[which]->centerY = (float)y; |
---|
| 149 | chambers[which]->centerZ = (float)z; |
---|
| 150 | chambers[which]->radius = (float)radius; |
---|
| 151 | chambers[which]->vectorTfX = (float)wx; |
---|
| 152 | chambers[which]->vectorTfY = (float)wy; |
---|
| 153 | chambers[which]->vectorTfZ = (float)wz; |
---|
| 154 | chambers[which]->beta = (float)alpha; |
---|
| 155 | chambers[which]->phi = (float)gamma; |
---|
[174] | 156 | |
---|
[176] | 157 | chambers[which]->points = generate_points(chambers[which], which, kx_, ky_, kz_); |
---|
| 158 | search_hid(which, chambers, kx_, ky_, kz_); |
---|
| 159 | int pun; |
---|
| 160 | pun = find_hole(which, pzx, pzy, pzz, chambers, kx_, ky_, kz_); |
---|
[174] | 161 | |
---|
[178] | 162 | chambers[which]->holeX = (float)chambers[which]->points[pun].x; |
---|
| 163 | chambers[which]->holeY = (float)chambers[which]->points[pun].y; |
---|
| 164 | chambers[which]->holeZ = (float)chambers[which]->points[pun].z; |
---|
[176] | 165 | } |
---|
[174] | 166 | } |
---|
| 167 | |
---|
[178] | 168 | void GenoConv_fF0::precompute_cos_and_sin() |
---|
[176] | 169 | { |
---|
| 170 | int i; |
---|
| 171 | double pi = acos(-1.0); |
---|
| 172 | double angle = pi / (((double)fF_LATITUDE_NUM)*0.5); |
---|
| 173 | for (i = 0; i < fF_LATITUDE_NUM; i++) |
---|
| 174 | { |
---|
| 175 | cosines[i] = cos((double)i * angle); |
---|
| 176 | sines[i] = sin((double)i * angle); |
---|
| 177 | } |
---|
[174] | 178 | } |
---|
| 179 | |
---|
[178] | 180 | fF_point* GenoConv_fF0::generate_points(fF_chamber3d *chamber, int which, double kx_, double ky_, double kz_) |
---|
[176] | 181 | { |
---|
| 182 | float radius = chamber->radius; |
---|
| 183 | float cenx = chamber->centerX; |
---|
| 184 | float ceny = chamber->centerY; |
---|
| 185 | float cenz = chamber->centerZ; |
---|
[174] | 186 | |
---|
[176] | 187 | double maxX = 0; |
---|
| 188 | double maxY = 0; |
---|
| 189 | double minX = 0; |
---|
| 190 | double minY = 0; |
---|
| 191 | double minZ = 0; |
---|
[174] | 192 | |
---|
[176] | 193 | double kx = 1; |
---|
| 194 | double ky = 1; |
---|
| 195 | double kz = 1; |
---|
[174] | 196 | |
---|
[177] | 197 | if (which > 0) |
---|
| 198 | { |
---|
| 199 | for (int kt = 1; kt < (which + 1); kt++) |
---|
| 200 | { |
---|
[176] | 201 | kx = kx * kx_; |
---|
| 202 | ky = ky * ky_; |
---|
| 203 | kz = kz * kz_; |
---|
| 204 | } |
---|
| 205 | } |
---|
[174] | 206 | |
---|
[177] | 207 | bool all_k_ones = kx_ == 1 && ky_ == 1 && kz_ == 1; |
---|
[174] | 208 | |
---|
[177] | 209 | double rx = all_k_ones ? radius : kx; |
---|
| 210 | double ry = all_k_ones ? radius : ky; |
---|
| 211 | double rz = all_k_ones ? radius : kz; |
---|
| 212 | |
---|
[178] | 213 | fF_point *points = new fF_point[fF_SIZE]; |
---|
[174] | 214 | |
---|
[177] | 215 | for (int i = 0; i < fF_LONGITUDE_NUM; i++) |
---|
| 216 | { |
---|
| 217 | double y = ceny + ry * cosines[i]; |
---|
[174] | 218 | |
---|
[177] | 219 | for (int j = 0; j < fF_LATITUDE_NUM; j++) |
---|
| 220 | { |
---|
| 221 | double x = cenx + rx * cosines[j] * sines[i]; |
---|
| 222 | double z = cenz + rz * sines[j] * sines[i]; |
---|
[178] | 223 | fF_point &p = points[(i * fF_LATITUDE_NUM) + j]; |
---|
| 224 | p.x = x; |
---|
| 225 | p.y = y; |
---|
| 226 | p.z = z; |
---|
| 227 | p.inside = false; |
---|
[177] | 228 | |
---|
[176] | 229 | if (x < minX) minX = x; |
---|
| 230 | if (x > maxX) maxX = x; |
---|
| 231 | if (y < minY) minY = y; |
---|
| 232 | if (y > maxY) maxY = y; |
---|
| 233 | |
---|
| 234 | if (z < minZ) minZ = z; |
---|
[177] | 235 | } |
---|
| 236 | } |
---|
[176] | 237 | return points; |
---|
| 238 | |
---|
[174] | 239 | } |
---|
| 240 | |
---|
[176] | 241 | double GenoConv_fF0::dist(double x1, double y1, double z1, double x2, double y2, double z2) |
---|
| 242 | { |
---|
| 243 | return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) + (z2 - z1)*(z2 - z1)); |
---|
[174] | 244 | } |
---|
| 245 | |
---|
[176] | 246 | void GenoConv_fF0::search_hid(int nr, fF_chamber3d **spheres, double kx_, double ky_, double kz_) |
---|
| 247 | { |
---|
[177] | 248 | double kxsq = kx_*kx_; |
---|
| 249 | double kysq = ky_*ky_; |
---|
| 250 | double kzsq = kz_*kz_; |
---|
[174] | 251 | |
---|
[177] | 252 | for (int i = 0; i < nr; i++) |
---|
| 253 | { |
---|
| 254 | double srX0 = spheres[i]->centerX; |
---|
| 255 | double srY0 = spheres[i]->centerY; |
---|
| 256 | double srZ0 = spheres[i]->centerZ; |
---|
[174] | 257 | |
---|
[177] | 258 | double radsq = spheres[i]->radius * spheres[i]->radius; |
---|
[174] | 259 | |
---|
[177] | 260 | double a2 = kx_ != 1 ? kxsq : radsq; |
---|
| 261 | double b2 = ky_ != 1 ? kysq : radsq; |
---|
| 262 | double c2 = kzsq * radsq; |
---|
[174] | 263 | |
---|
[177] | 264 | for (int j = 0; j < fF_AMOUNT; j++) |
---|
| 265 | { |
---|
[178] | 266 | fF_point &p = spheres[nr]->points[j]; |
---|
[174] | 267 | |
---|
[178] | 268 | double up1 = (p.x - srX0) * (p.x - srX0); |
---|
| 269 | double up2 = (p.y - srY0) * (p.y - srY0); |
---|
| 270 | double up3 = (p.z - srZ0) * (p.z - srZ0); |
---|
[174] | 271 | |
---|
[177] | 272 | double exp1 = up1 / a2; |
---|
| 273 | double exp2 = up2 / b2; |
---|
| 274 | double exp3 = up3 / c2; |
---|
[174] | 275 | |
---|
[177] | 276 | double result = exp1 + exp2 + exp3; |
---|
[174] | 277 | |
---|
[177] | 278 | if (result < fF_THICK_RATIO) |
---|
| 279 | { |
---|
[178] | 280 | p.inside = true; |
---|
[176] | 281 | } |
---|
| 282 | } |
---|
| 283 | } |
---|
[174] | 284 | } |
---|
| 285 | |
---|
[176] | 286 | int GenoConv_fF0::find_hole(int which, double x, double y, double z, fF_chamber3d **chambers, double kx_, double ky_, double kz_) |
---|
| 287 | { |
---|
[177] | 288 | int found = -1; |
---|
| 289 | double distsq_found; |
---|
[174] | 290 | |
---|
[177] | 291 | double kxsq = kx_*kx_; |
---|
| 292 | double kysq = ky_*ky_; |
---|
| 293 | double kzsq = kz_*kz_; |
---|
| 294 | |
---|
| 295 | for (int i = 0; i < fF_AMOUNT; i++) |
---|
| 296 | { |
---|
[178] | 297 | fF_point &p = chambers[which]->points[i]; |
---|
| 298 | if (!p.inside) //it is not inside another chamber |
---|
[176] | 299 | { |
---|
[178] | 300 | double distancesq = (p.x - x)*(p.x - x) + (p.y - y)*(p.y - y) + (p.z - z)*(p.z - z); |
---|
[177] | 301 | if (found < 0) |
---|
| 302 | { |
---|
[176] | 303 | found = i; |
---|
[177] | 304 | distsq_found = distancesq; |
---|
[176] | 305 | } |
---|
[177] | 306 | if (distancesq < distsq_found) |
---|
| 307 | { |
---|
| 308 | if (which != 0) |
---|
| 309 | { |
---|
[176] | 310 | bool good = true; |
---|
[177] | 311 | for (int j = 0; j < which && good; j++) |
---|
| 312 | { |
---|
| 313 | double srX0 = chambers[j]->centerX; |
---|
| 314 | double srY0 = chambers[j]->centerY; |
---|
| 315 | double srZ0 = chambers[j]->centerZ; |
---|
[174] | 316 | |
---|
[177] | 317 | double radsq = chambers[j]->radius * chambers[j]->radius; |
---|
[174] | 318 | |
---|
[177] | 319 | double a2 = kxsq * radsq; |
---|
| 320 | double b2 = kysq * radsq; |
---|
| 321 | double c2 = kzsq * radsq; |
---|
[174] | 322 | |
---|
[178] | 323 | double up1 = (p.x - srX0) * (p.x - srX0); |
---|
| 324 | double up2 = (p.y - srY0) * (p.y - srY0); |
---|
| 325 | double up3 = (p.z - srZ0) * (p.z - srZ0); |
---|
[174] | 326 | |
---|
[177] | 327 | double exp1 = up1 / a2; |
---|
| 328 | double exp2 = up2 / b2; |
---|
| 329 | double exp3 = up3 / c2; |
---|
[174] | 330 | |
---|
[177] | 331 | double result = exp1 + exp2 + exp3; |
---|
| 332 | if (result < 1.0) |
---|
| 333 | { |
---|
| 334 | good = false; |
---|
[176] | 335 | } |
---|
| 336 | } |
---|
[177] | 337 | if (good) |
---|
| 338 | { |
---|
[176] | 339 | found = i; |
---|
[177] | 340 | distsq_found = distancesq; |
---|
[176] | 341 | } |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
[174] | 346 | |
---|
[177] | 347 | return found; |
---|
[176] | 348 | } |
---|