Selects the best plane to partition with, returning the pointer to polygon to split with
| 454 | |
| 455 | // Selects the best plane to partition with, returning the pointer to polygon to split with |
| 456 | bsppolygon *SelectPlane(listnode **polylist) { |
| 457 | listnode *outer, *inner; |
| 458 | bsppolygon *outerpoly, *innerpoly, *bestpoly; |
| 459 | int splits, front, back, planar, result, balance; |
| 460 | float score, bestscore; |
| 461 | float g_kbalance = 1.0; |
| 462 | float g_ksplit = 0.0; |
| 463 | float g_kplanar = 0.0; |
| 464 | int checked = 0; |
| 465 | |
| 466 | bestscore = 9999999.0; |
| 467 | bestpoly = NULL; |
| 468 | |
| 469 | /* Iterate through every polygon, checking its plane against all |
| 470 | * the other polygons. But don't check it against itself |
| 471 | */ |
| 472 | |
| 473 | mprintf_at(2, 4, 0, "Plane = %c", Twirly[(Plane_twirl++) % 4]); |
| 474 | |
| 475 | for (outer = *polylist; outer != NULL; outer = outer->next) { |
| 476 | outerpoly = (bsppolygon *)outer->data; |
| 477 | splits = front = back = planar = 0; |
| 478 | score = 0.0; |
| 479 | |
| 480 | if (outerpoly->plane.used) |
| 481 | continue; |
| 482 | |
| 483 | // Only check 300 times |
| 484 | if (checked >= 300) |
| 485 | return bestpoly; |
| 486 | |
| 487 | for (inner = *polylist; inner != NULL; inner = inner->next) { |
| 488 | innerpoly = (bsppolygon *)inner->data; |
| 489 | if (innerpoly != outerpoly) { |
| 490 | result = ClassifyPolygon(&outerpoly->plane, innerpoly); |
| 491 | switch (result) { |
| 492 | case BSP_SPANNING: |
| 493 | splits++; |
| 494 | break; |
| 495 | case BSP_IN_FRONT: |
| 496 | front++; |
| 497 | break; |
| 498 | case BSP_BEHIND: |
| 499 | back++; |
| 500 | break; |
| 501 | case BSP_COINCIDENT: |
| 502 | planar++; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Calculate the score. If we have a "perfect" tree, ie |
| 509 | * it has either no splits, or is perfectly balanced, |
| 510 | * return the plane to the caller |
| 511 | */ |
| 512 | |
| 513 | balance = abs(front - back); |
no test coverage detected