📄 blkocc.cpp
字号:
&next_band, &min_x, &max_x, &increment, exit_pt); pt_it.mark_cycle_pt (); // Found the first real transition, so start walking the outline from here. do { prev_band = band; band = band + increment; while (band != next_band) { temp_pt = entry_pt; entry_pt = exit_pt; exit_pt = temp_pt; min_x = max_x = entry_pt->x (); find_trans_point (&pt_it, band, band + increment, exit_pt); maintain_limits (&min_x, &max_x, exit_pt->x ()); record_region (band, min_x, max_x, find_region_type (prev_band, band, band + increment, entry_pt->x (), exit_pt->x ()), region_occ_list); prev_band = band; band = band + increment; } temp_pt = entry_pt; entry_pt = exit_pt; exit_pt = temp_pt; min_x = max_x = entry_pt->x (); next_region(&pt_it, band, &next_band, &min_x, &max_x, &increment, exit_pt); record_region (band, min_x, max_x, find_region_type (prev_band, band, band + increment, entry_pt->x (), exit_pt->x ()), region_occ_list); } while (!pt_it.cycled_list ()); } } }}void record_region( //add region on list INT16 band, float new_min, float new_max, INT16 region_type, REGION_OCC_LIST *region_occ_list) { REGION_OCC_IT it (&(region_occ_list[band])); // if (wordocc_debug_on && blockocc_show_result) // fprintf( db_win, "\nBand %d, region type %d, from %f to %f", // band, region_type, new_min, new_max ); if ((region_type == REGION_TYPE_UPPER_UNBOUND) || (region_type == REGION_TYPE_LOWER_UNBOUND) || (region_type == REGION_TYPE_EMPTY)) return; if (it.empty ()) { it.add_after_stay_put (new REGION_OCC (new_min, new_max, region_type)); } else { /* Insert in sorted order of average limit */ while ((new_min + new_max > it.data ()->min_x + it.data ()->max_x) && (!it.at_last ())) it.forward (); if ((it.at_last ()) && //at the end (new_min + new_max > it.data ()->min_x + it.data ()->max_x)) //new range > current it.add_after_stay_put (new REGION_OCC (new_min, new_max, region_type)); else { it.add_before_stay_put (new REGION_OCC (new_min, new_max, region_type)); } }}INT16 find_containing_maximal_band( //find range's band float y1, float y2, BOOL8 *doubly_contained) { INT16 band; *doubly_contained = FALSE; for (band = 1; band <= blockocc_band_count; band++) { if (bands[band].range_in_maximal (y1, y2)) { if ((band < blockocc_band_count) && (bands[band + 1].range_in_maximal (y1, y2))) *doubly_contained = TRUE; return band; } } return UNDEFINED_BAND;}void find_significant_line(POLYPT_IT it, INT16 *band) { /* Look for a line which significantly occupies at least one band. I.e. part of the line is in the non-margin part of the band. */ *band = find_overlapping_minimal_band (it.data ()->pos.y (), it.data ()->pos.y () + it.data ()->vec.y ()); while (*band == UNDEFINED_BAND) { it.forward (); *band = find_overlapping_minimal_band (it.data ()->pos.y (), it.data ()->pos.y () + it.data ()->vec.y ()); }}INT16 find_overlapping_minimal_band( //find range's band float y1, float y2) { INT16 band; for (band = 1; band <= blockocc_band_count; band++) { if (bands[band].range_overlaps_minimal (y1, y2)) return band; } return UNDEFINED_BAND;}INT16 find_region_type(INT16 entry_band, INT16 current_band, INT16 exit_band, float entry_x, float exit_x) { if (entry_band > exit_band) return REGION_TYPE_OPEN_RIGHT; if (entry_band < exit_band) return REGION_TYPE_OPEN_LEFT; if (entry_x == exit_x) return REGION_TYPE_EMPTY; if (entry_band > current_band) { if (entry_x < exit_x) return REGION_TYPE_UPPER_BOUND; else return REGION_TYPE_UPPER_UNBOUND; } else { if (entry_x > exit_x) return REGION_TYPE_LOWER_BOUND; else return REGION_TYPE_LOWER_UNBOUND; }}void find_trans_point(POLYPT_IT *pt_it, INT16 current_band, INT16 next_band, FCOORD *transition_pt) { float x1, x2, y1, y2; // points of edge float gradient; // m in y = mx + c float offset; // c in y = mx + c if (current_band < next_band) transition_pt->set_y (bands[current_band].max); //going up else transition_pt->set_y (bands[current_band].min); //going down x1 = pt_it->data ()->pos.x (); x2 = x1 + pt_it->data ()->vec.x (); y1 = pt_it->data ()->pos.y (); y2 = y1 + pt_it->data ()->vec.y (); if (x1 == x2) transition_pt->set_x (x1); //avoid div by 0 else { if (y1 == y2) //avoid div by 0 transition_pt->set_x ((x1 + x2) / 2.0); else { gradient = (y1 - y2) / (float) (x1 - x2); offset = y1 - x1 * gradient; transition_pt->set_x ((transition_pt->y () - offset) / gradient); } }}void next_region(POLYPT_IT *start_pt_it, INT16 start_band, INT16 *to_band, float *min_x, float *max_x, INT16 *increment, FCOORD *exit_pt) { /* Given an edge and a band which the edge significantly occupies, find the significant end of the region containing the band. I.e. find an edge which points to another band such that the outline subsequetly moves significantly out of the starting band. Note that we can assume that we are significantly inside the current band to start with because the edges passed will be from previous calls to this routine apart from the first - the result of which is only used to establish the start of the first region. */ INT16 band; //band of current edge INT16 prev_band = start_band; //band of prev edge //edge crossing out POLYPT_IT last_transition_out_it; //band it pts to INT16 last_trans_out_to_band = 0; float ext_min_x = 0.0f; float ext_max_x = 0.0f; start_pt_it->forward (); band = find_band (start_pt_it->data ()->pos.y ()); while ((band == start_band) || bands[start_band].in_maximal (start_pt_it->data ()->pos.y ())) { if (band == start_band) { //Return to start band if (prev_band != start_band) { *min_x = ext_min_x; *max_x = ext_max_x; } maintain_limits (min_x, max_x, start_pt_it->data ()->pos.x ()); } else { if (prev_band == start_band) { //Exit from start band //so remember edge last_transition_out_it = *start_pt_it; //before we left last_transition_out_it.backward (); //and band it pts to last_trans_out_to_band = band; ext_min_x = *min_x; ext_max_x = *max_x; } maintain_limits (&ext_min_x, &ext_max_x, start_pt_it->data ()->pos.x ()); } prev_band = band; start_pt_it->forward (); band = find_band (start_pt_it->data ()->pos.y ()); } if (prev_band == start_band) { //exit from start band *to_band = band; //so remember edge last_transition_out_it = *start_pt_it; //before we left last_transition_out_it.backward (); } else { *to_band = last_trans_out_to_band; } if (*to_band > start_band) *increment = 1; else *increment = -1; find_trans_point (&last_transition_out_it, start_band, start_band + *increment, exit_pt); maintain_limits (min_x, max_x, exit_pt->x ()); *start_pt_it = last_transition_out_it;}INT16 find_band( // find POINT's band float y) { INT16 band; for (band = 1; band <= blockocc_band_count; band++) { if (bands[band].in_nominal (y)) return band; } BLOCKOCC.error ("find_band", ABORT, "Cant find band for %d", y); return 0;}void compress_region_list( // join open regions REGION_OCC_LIST *region_occ_list) { REGION_OCC_IT it (&(region_occ_list[0])); REGION_OCC *open_right = NULL; INT16 i = 0; for (i = 0; i <= blockocc_band_count; i++) { it.set_to_list (&(region_occ_list[i])); if (!it.empty ()) { /* First check for left right pairs. Merge them into the open right and delete the open left. */ open_right = NULL; for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) { switch (it.data ()->region_type) { case REGION_TYPE_OPEN_RIGHT: { if (open_right != NULL) BLOCKOCC.error ("compress_region_list", ABORT, "unmatched right"); else open_right = it.data (); break; } case REGION_TYPE_OPEN_LEFT: { if (open_right == NULL) BLOCKOCC.error ("compress_region_list", ABORT, "unmatched left"); else { open_right->max_x = it.data ()->max_x; open_right = NULL; delete it.extract (); } break; } default: break; } } if (open_right != NULL) BLOCKOCC.error ("compress_region_list", ABORT, "unmatched right remaining"); /* Now cycle the list again, merging and deleting any redundant regions */ it.move_to_first (); open_right = it.data (); while (!it.at_last ()) { it.forward (); if (it.data ()->min_x <= open_right->max_x) { // Overlaps if (it.data ()->max_x > open_right->max_x) open_right->max_x = it.data ()->max_x; // Extend delete it.extract (); } else open_right = it.data (); } } }}void find_fbox(OUTLINE_IT *out_it, float *min_x, float *min_y, float *max_x, float *max_y) { POLYPT_IT pt_it = out_it->data ()->polypts (); FCOORD pt; *min_x = 9999.0f; *min_y = 9999.0f; *max_x = 0.0f; *max_y = 0.0f; for (pt_it.mark_cycle_pt (); !pt_it.cycled_list (); pt_it.forward ()) { pt = pt_it.data ()->pos; maintain_limits (min_x, max_x, pt.x ()); maintain_limits (min_y, max_y, pt.y ()); }}void maintain_limits(float *min_x, float *max_x, float x) { if (x > *max_x) *max_x = x; if (x < *min_x) *min_x = x;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -