159 int16_t region_index;
160 int16_t best_region_index = 0;
163 int16_t right_best_x;
168 FPSEGPT_LIST *segpts;
176 FPSEGPT_IT outseg_it = seg_list;
177 FPSEGPT_LIST_CLIST lattice;
179 FPSEGPT_LIST_C_IT lattice_it = &lattice;
187 if ((pitch - 3) / 2 < pitch_error)
188 pitch_error = (pitch - 3) / 2;
196 left_edge = min_box.
left () + pitch_error;
197 for (min_index = 1; min_index < blob_count; min_index++) {
204 right_edge = min_box.
right ();
207 min_x = max_x - pitch + pitch_error * 2 + 1;
208 right_max = right_edge + pitch - pitch_error - 1;
209 segpts =
new FPSEGPT_LIST;
210 segpt_it.set_to_list (segpts);
211 for (x = min_x; x <= max_x; x++) {
214 segpt_it.add_after_then_move (segpt);
217 lattice_it.add_before_then_move (segpts);
227 segpts =
new FPSEGPT_LIST;
228 segpt_it.set_to_list (segpts);
229 min_x += pitch - pitch_error;
230 max_x += pitch + pitch_error;
231 while (min_box.
right () < min_x && min_index < blob_count) {
236 max_index = min_index;
239 for (x = min_x; x <= max_x && x <= right_max; x++) {
240 while (x < right_edge && max_index < blob_count
241 && x > max_box.
right ()) {
246 if (x <= max_box.
left () + pitch_error
247 || x >= max_box.
right () - pitch_error || x >= right_edge
248 || (max_index < blob_count - 1 && x >= next_box.
left ())
252 if (x - max_box.
left () > 0
253 && x - max_box.
left () <= pitch_error)
255 offset = x - max_box.
left ();
256 else if (max_box.
right () - x > 0
257 && max_box.
right () - x <= pitch_error
258 && (max_index >= blob_count - 1
259 || x < next_box.
left ()))
260 offset = max_box.
right () - x;
264 segpt =
new FPSEGPT (x,
false, offset, region_index,
265 pitch, pitch_error, lattice_it.data ());
269 segpt =
new FPSEGPT (x,
true, offset, region_index,
270 pitch, pitch_error, lattice_it.data ());
272 if (segpt->
previous () !=
nullptr) {
273 segpt_it.add_after_then_move (segpt);
274 if (x >= right_edge - pitch_error) {
280 best_region_index = region_index;
285 && right_best_x == x - 1)
293 if (segpts->empty ()) {
294 if (best_end !=
nullptr)
297 region_index, pitch, pitch_error, segpts);
300 if (right_best_x > left_best_x + 1) {
301 left_best_x = (left_best_x + right_best_x + 1) / 2;
302 for (segpt_it.mark_cycle_pt (); !segpt_it.cycled_list ()
303 && segpt_it.data ()->position () != left_best_x;
304 segpt_it.forward ());
305 if (segpt_it.data ()->position () == left_best_x)
307 best_end = segpt_it.data ();
311 lattice_it.add_before_then_move (segpts);
314 while (min_x < right_edge);
317 for (lattice_it.mark_cycle_pt (); !lattice_it.cycled_list ();
318 lattice_it.forward ()) {
319 segpts = lattice_it.data ();
320 segpt_it.set_to_list (segpts);
332 for (segpt_it.mark_cycle_pt (); !segpt_it.cycled_list ()
333 && segpt_it.data () != best_end; segpt_it.forward ());
334 if (segpt_it.data () == best_end) {
336 segpt = segpt_it.extract ();
337 outseg_it.add_before_then_move (segpt);
343 outseg_it.move_to_last ();
344 mean_sum = outseg_it.data ()->sum ();
345 mean_sum = mean_sum * mean_sum / best_region_index;
346 if (outseg_it.data ()->squares () - mean_sum < 0)
347 tprintf (
"Impossible sqsum=%g, mean=%g, total=%d\n",