515 inT16 projection_offset;
519 inT16 best_left_x = 0;
520 inT16 best_right_x = 0;
531 FPSEGPT_IT seg_it = seg_list;
533 end = (end - start) % pitch;
536 if ((pitch - 3) / 2 < pitch_error)
537 pitch_error = (pitch - 3) / 2;
540 for (left_edge = projection_left; projection->
pile_count (left_edge) == 0
541 && left_edge < projection_right; left_edge++);
542 for (right_edge = projection_right; projection->
pile_count (right_edge) == 0
543 && right_edge > left_edge; right_edge--);
544 array_origin = left_edge - pitch;
548 for (x = array_origin; x < left_edge; x++)
550 cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, 0);
551 prev_zero = left_edge - 1;
552 for (offset = 0; offset <= pitch_error; offset++, x++)
554 cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, offset);
558 for (offset = -pitch_error, minindex = 0; offset < pitch_error;
559 offset++, minindex++)
560 mins[minindex] = projection->
local_min (x + offset);
561 next_zero = x + zero_offset + 1;
562 for (offset = next_zero - 1; offset >= x; offset--) {
563 if (projection->
pile_count (offset) <= zero_count) {
568 while (x < right_edge - pitch_error) {
569 mins[minindex] = projection->
local_min (x + pitch_error);
571 if (minindex > pitch_error * 2)
576 if (projection->
pile_count (x) <= zero_count) {
580 for (offset = 1; offset <= pitch_error; offset++)
581 if (projection->
pile_count (x + offset) <= zero_count
582 || projection->
pile_count (x - offset) <= zero_count)
585 if (offset > pitch_error) {
586 if (x - prev_zero > zero_offset && next_zero - x > zero_offset) {
587 for (offset = 0; offset <= pitch_error; offset++) {
588 test_index = minindex + pitch_error + offset;
589 if (test_index > pitch_error * 2)
590 test_index -= pitch_error * 2 + 1;
591 if (mins[test_index])
593 test_index = minindex + pitch_error - offset;
594 if (test_index > pitch_error * 2)
595 test_index -= pitch_error * 2 + 1;
596 if (mins[test_index])
600 if (offset > pitch_error) {
607 if (projection_offset > offset)
608 offset = projection_offset;
612 if ((start == 0 && end == 0)
614 || (x - projection_left - start) % pitch <= end)
615 cutpts[x - array_origin].
assign (cutpts, array_origin, x,
616 faking, mid_cut, offset, projection,
617 projection_scale, zero_count, pitch,
620 cutpts[x - array_origin].
assign_cheap (cutpts, array_origin, x,
621 faking, mid_cut, offset,
622 projection, projection_scale,
626 if (next_zero < x || next_zero == x + zero_offset)
627 next_zero = x + zero_offset + 1;
628 if (projection->
pile_count (x + zero_offset) <= zero_count)
629 next_zero = x + zero_offset;
635 while (x < right_edge + pitch) {
636 offset = x < right_edge ? right_edge - x : 0;
637 cutpts[x - array_origin].
assign (cutpts, array_origin, x,
639 projection_scale, zero_count, pitch,
642 if (cutpts[x - array_origin].index () +
643 cutpts[x - array_origin].fake_count <= best_count + best_fake) {
644 if (cutpts[x - array_origin].fake_count < best_fake
645 || (cutpts[x - array_origin].fake_count == best_fake
646 && cutpts[x - array_origin].cost_function () < best_cost)) {
647 best_fake = cutpts[x - array_origin].
fake_count;
651 best_count = cutpts[x - array_origin].
index ();
653 else if (cutpts[x - array_origin].fake_count == best_fake
654 && x == best_right_x + 1
655 && cutpts[x - array_origin].cost_function () == best_cost) {
664 best_end = &cutpts[(best_left_x + best_right_x) / 2 - array_origin];
673 occupation_count = -1;
675 for (x = best_end->
position () - pitch + pitch_error;
676 x < best_end->
position () - pitch_error
678 if (x < best_end->position () - pitch_error)
681 segpt =
new FPSEGPT (best_end);
682 seg_it.add_before_then_move (segpt);
685 while (best_end != NULL);
686 seg_it.move_to_last ();
687 mean_sum = seg_it.data ()->
sum ();
688 mean_sum = mean_sum * mean_sum / best_count;
689 if (seg_it.data ()->squares () - mean_sum < 0)
690 tprintf (
"Impossible sqsum=%g, mean=%g, total=%d\n",
691 seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
694 return seg_it.data ()->squares () - mean_sum;
bool local_min(inT32 x) const
void assign_cheap(FPCUTPT cutpts[], inT16 array_origin, inT16 x, BOOL8 faking, BOOL8 mid_cut, inT16 offset, STATS *projection, float projection_scale, inT16 zero_count, inT16 pitch, inT16 pitch_error)
EXTERN double pitsync_joined_edge
void * alloc_mem(inT32 count)
inT32 pile_count(inT32 value) const
void free_mem(void *oldchunk)
void assign(FPCUTPT cutpts[], inT16 array_origin, inT16 x, BOOL8 faking, BOOL8 mid_cut, inT16 offset, STATS *projection, float projection_scale, inT16 zero_count, inT16 pitch, inT16 pitch_error)
EXTERN bool textord_fast_pitch_test