Tesseract  3.02
tesseract-ocr/ccstruct/split.cpp
Go to the documentation of this file.
00001 /* -*-C-*-
00002  ********************************************************************************
00003  *
00004  * File:        split.c  (Formerly split.c)
00005  * Description:
00006  * Author:       Mark Seaman, OCR Technology
00007  * Created:      Fri Oct 16 14:37:00 1987
00008  * Modified:     Fri May 17 16:27:49 1991 (Mark Seaman) marks@hpgrlt
00009  * Language:     C
00010  * Package:      N/A
00011  * Status:       Reusable Software Component
00012  *
00013  * (c) Copyright 1987, Hewlett-Packard Company.
00014  ** Licensed under the Apache License, Version 2.0 (the "License");
00015  ** you may not use this file except in compliance with the License.
00016  ** You may obtain a copy of the License at
00017  ** http://www.apache.org/licenses/LICENSE-2.0
00018  ** Unless required by applicable law or agreed to in writing, software
00019  ** distributed under the License is distributed on an "AS IS" BASIS,
00020  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  ** See the License for the specific language governing permissions and
00022  ** limitations under the License.
00023  *
00024  *************************************************************************/
00025 /*----------------------------------------------------------------------
00026               I n c l u d e s
00027 ----------------------------------------------------------------------*/
00028 #include "split.h"
00029 #include "structures.h"
00030 #include "callcpp.h"
00031 
00032 #ifdef __UNIX__
00033 #include <assert.h>
00034 #endif
00035 
00036 /*----------------------------------------------------------------------
00037               V a r i a b l e s
00038 ----------------------------------------------------------------------*/
00039 BOOL_VAR(wordrec_display_splits, 0, "Display splits");
00040 
00041 makestructure(newsplit, free_split, SPLIT);
00042 
00043 /*----------------------------------------------------------------------
00044               F u n c t i o n s
00045 ----------------------------------------------------------------------*/
00046 
00047 /**********************************************************************
00048  * delete_split
00049  *
00050  * Remove this split from existance.  Take if off the display list and
00051  * deallocate its memory.
00052  **********************************************************************/
00053 void delete_split(SPLIT *split) { 
00054   if (split) {
00055     free_split(split); 
00056   }
00057 }
00058 
00059 
00060 /**********************************************************************
00061  * make_edgept
00062  *
00063  * Create an EDGEPT and hook it into an existing list of edge points.
00064  **********************************************************************/
00065 EDGEPT *make_edgept(int x, int y, EDGEPT *next, EDGEPT *prev) {
00066   EDGEPT *this_edgept;
00067   /* Create point */
00068   this_edgept = new EDGEPT;
00069   this_edgept->pos.x = x;
00070   this_edgept->pos.y = y;
00071   /* Hook it up */
00072   this_edgept->next = next;
00073   this_edgept->prev = prev;
00074   prev->next = this_edgept;
00075   next->prev = this_edgept;
00076   /* Set up vec entries */
00077   this_edgept->vec.x = this_edgept->next->pos.x - x;
00078   this_edgept->vec.y = this_edgept->next->pos.y - y;
00079   this_edgept->prev->vec.x = x - this_edgept->prev->pos.x;
00080   this_edgept->prev->vec.y = y - this_edgept->prev->pos.y;
00081 
00082   return (this_edgept);
00083 }
00084 
00085 /**********************************************************************
00086  * remove_edgept
00087  *
00088  * Remove a given EDGEPT from its list and delete it.
00089  **********************************************************************/
00090 void remove_edgept(EDGEPT *point) {
00091   EDGEPT *prev = point->prev;
00092   EDGEPT *next = point->next;
00093   prev->next = next;
00094   next->prev = prev;
00095   prev->vec.x = next->pos.x - prev->pos.x;
00096   prev->vec.y = next->pos.y - prev->pos.y;
00097   delete point;
00098 }
00099 
00100 /**********************************************************************
00101  * new_split
00102  *
00103  * Create a new split record and initialize it.  Put it on the display
00104  * list.
00105  **********************************************************************/
00106 SPLIT *new_split(EDGEPT *point1, EDGEPT *point2) { 
00107   SPLIT *s;
00108   s = (SPLIT *) newsplit ();
00109   s->point1 = point1;
00110   s->point2 = point2;
00111   return (s);
00112 }
00113 
00114 
00115 /**********************************************************************
00116  * print_split
00117  *
00118  * Print a list of splits.  Show the coordinates of both points in
00119  * each split.
00120  **********************************************************************/
00121 void print_split(SPLIT *split) { 
00122   if (split) {
00123     cprintf ("(%d,%d)--(%d,%d)",
00124       split->point1->pos.x, split->point1->pos.y,
00125       split->point2->pos.x, split->point2->pos.y);
00126   }
00127 }
00128 
00129 
00130 /**********************************************************************
00131  * split_outline
00132  *
00133  * Split between these two edge points. Apply a split and return a
00134  * pointer to the other side of the split.
00135  **********************************************************************/
00136 void split_outline(EDGEPT *join_point1, EDGEPT *join_point2) { 
00137   EDGEPT *join_point1a;
00138   EDGEPT *temp2;
00139   EDGEPT *temp1;
00140 
00141   assert (join_point1 != join_point2);
00142 
00143   temp2 = join_point2->next;
00144   temp1 = join_point1->next;
00145   /* Create two new points */
00146   join_point1a = make_edgept (join_point1->pos.x,
00147     join_point1->pos.y, temp1, join_point2);
00148 
00149   make_edgept (join_point2->pos.x, join_point2->pos.y, temp2, join_point1);
00150 }
00151 
00152 
00153 /**********************************************************************
00154  * unsplit_outlines
00155  *
00156  * Remove the split that was put between these two points.
00157  **********************************************************************/
00158 void unsplit_outlines(EDGEPT *p1, EDGEPT *p2) { 
00159   EDGEPT *tmp1 = p1->next;
00160   EDGEPT *tmp2 = p2->next;
00161 
00162   assert (p1 != p2);
00163 
00164   tmp1->next->prev = p2;
00165   tmp2->next->prev = p1;
00166 
00167   p1->next = tmp2->next;
00168   p2->next = tmp1->next;
00169 
00170   delete tmp1;
00171   delete tmp2;
00172 
00173   p1->vec.x = p1->next->pos.x - p1->pos.x;
00174   p1->vec.y = p1->next->pos.y - p1->pos.y;
00175 
00176   p2->vec.x = p2->next->pos.x - p2->pos.x;
00177   p2->vec.y = p2->next->pos.y - p2->pos.y;
00178 }