Debian Codegraph @

cuneiform_src/Kern/lns32/src/lbambuk.cpp

raw
1 /*
2 Copyright (c) 1993-2008, Cognitive Technologies
3 All rights reserved.
4
5 Разрешается повторное распространение и использование как в виде исходного кода,
6 так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:
7
8 * При повторном распространении исходного кода должны оставаться указанное
9 выше уведомление об авторском праве, этот список условий и последующий
10 отказ от гарантий.
11 * При повторном распространении двоичного кода в документации и/или в
12 других материалах, поставляемых при распространении, должны сохраняться
13 указанная выше информация об авторском праве, этот список условий и
14 последующий отказ от гарантий.
15 * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
16 быть использованы в качестве средства поддержки и/или продвижения
17 продуктов, основанных на этом ПО, без предварительного письменного
18 разрешения.
19
20 ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
21 ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
22 ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
23 ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
24 МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
25 НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
26 ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
27 НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
28 СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
29 ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
30 НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
31 ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.
32
33 Redistribution and use in source and binary forms, with or without modification,
34 are permitted provided that the following conditions are met:
35
36 * Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright notice,
39 this list of conditions and the following disclaimer in the documentation
40 and/or other materials provided with the distribution.
41 * Neither the name of the Cognitive Technologies nor the names of its
42 contributors may be used to endorse or promote products derived from this
43 software without specific prior written permission.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
46 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
49 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 //#include "lnslang.h"
58
59 #ifndef __PUANSO_H
60 # include "puanso.h"
61 #endif
62
63 #ifndef __RBAMBUK_H
64 # include "rbambuk.h"
65 #endif
66
67 #ifndef __LBAMBUK_H
68 # include "lbambuk.h"
69 #endif
70
71 # ifndef __STDLIB_H
72 # include <stdlib.h>
73 # endif
74
75 #include "hliner.h"
76 #include "decl.h"
77
78 int cdecl compFragLevels( const void *frag1, const void *frag2 ); //used by qsort()
79
80 void RegisterBlackSquare(int left, int top, int right, int bottom );
81 void RegisterCheckBox( Rect16& rcbox );
82
83 static Bool has_H_linked( PLine line, PLine frag );
84 static Bool has_V_linked( PLine line, PLine frag );
85 static inline int line_length( PLine line );
86
87 static TRasterBambuk* rb_ = NULL;
88 static TSegBambuk* sb_ = NULL;
89
90 Bool TLinesBambuk::makeIt( TRasterBambuk* rb, TSegBambuk* sb, Bool isVertical ){
91
92 *((TXYDim*)this) = *((TXYDim*)sb); // 11-04-93 07:56pm
93
94 if (isVertical){
95 computeVRasters( rb, sb );
96 }
97 else {
98 computeHRasters( rb, sb );
99 };
100 return TRUE;
101 }
102
103 #include "frag.h"
104
105 void TLinesBambuk::computeHRasters( TRasterBambuk* rb, TSegBambuk* sb ){
106
107 rb_ = rb;
108 sb_ = sb;
109 assert(rb_);
110 assert(sb_);
111
112 BEntry last_comp = rb_->lastEntry();
113 BHandle dash, seg;
114 BEntry row;
115 BHandle frag_handle = 0;
116 PTLineFragment fragment;
117 TPuanso puanso;
118 double sum_phi = 0.0;
119 int32_t sum_len = 0;
120
121 int down, up;
122 int i(0);
123 Frag_HAlloc( rb->totalRasterCount());
124 int nfrag =0;
125 /// --------------------
126
127 for ( BEntry comp = 0; comp <= last_comp; comp++ )
128 {
129 dash = rb_->firstEntryMember( comp );
130 if (dash ==NULLBHandle)
131 continue;
132 // compute tenzor of inertia...
133 puanso.flashPuanso();
134 down = 0x7FF0;
135 up = 0;
136 do {
137 TDash& dd = (*rb_)[ dash ];
138 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
139 row = dd.firstSegEntry;
140 down = MIN( down, (int)row );
141 do {
142 TBlackSeg& bs = (*sb_)[ seg ];
143 puanso.increase( bs.left,
144 bs.right,
145 row++
146 );
147 seg = bs.nLower;
148 } while ( seg != NULLBHandle );
149 up = MAX( up, (int)row );
150 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
151
152 puanso.atOwnCoords();
153
154 /////////////// VP: 15 jul 98 : cutting a line
155 Bool isLine = puanso.isALine();
156
157 LnsFrag* frg = Frag_HGet( nfrag ); nfrag++;
158 {
159 if (frg)
160 {
161 if (isLine)
162 frg->flags = LF_LINE;
163 else
164 frg->frag_handle = -1;
165 frg->rc.left = puanso.left;
166 frg->rc.right = puanso.right-2;
167 frg->rc.top = down;
168 frg->rc.bottom = up-1;
169 frg->mass =(long)puanso.mass ;
170 frg->Xc =puanso.Xc ;
171 frg->Yc =puanso.Yc ;
172 frg->Phi =puanso.Phi ;
173 frg->aveW =puanso.aveW ;
174 frg->mainMxx =puanso.mainMxx;
175 frg->mainMyy =puanso.mainMyy;
176
177 frg->A.x = puanso.left;
178 frg->B.x = puanso.right - 1;
179 frg->A.y =(int16_t)( puanso.Yc + (puanso.left-puanso.Xc)*puanso.Phi );
180 frg->B.y =(int16_t)( puanso.Yc + (puanso.right-puanso.Xc)*puanso.Phi );
181 }
182 }
183 ///-----------------------
184
185 if (!isLine && (puanso.right-puanso.left) > 600)
186 { // try to recover long line with glued dust
187 /////////////////////////////////////////////////////////////////////////////////
188 {
189 // 1. find zone with bad line width
190 int wdt[512]={0};
191 dash = rb_->firstEntryMember( comp );
192 do
193 {
194 TDash& dd = (*rb_)[ dash ];
195 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
196 row = dd.firstSegEntry;
197 do
198 {
199 TBlackSeg& bs = (*sb_)[ seg ];
200 if (bs.right < 4000) // 4096, if more presize
201 {
202 wdt[bs.left >> 3]++;
203 wdt[(bs.right+1) >> 3]--;
204 }
205 seg = bs.nLower;
206 } while ( seg != NULLBHandle );
207 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
208
209 for (i=1; i<512; i++)
210 wdt[i] += wdt[i-1];
211
212 int sum=0; int max_width = 40;
213
214 for (int pass=0; pass<2; pass++)
215 { sum=0; int sumsq=0; int len=0;
216 for (int i=1; i<512; i++)
217 {
218 if (wdt[i] > 0 && wdt[i] < max_width)
219 {
220
221 sum += wdt[i];
222 sumsq += wdt[i]*wdt[i];
223 len ++;
224 }
225 }
226
227 if (len==0)
228 len=1;
229
230 double ave_width = ((double)sum) / len;
231 double dispers = ( double(sumsq)/len) - ave_width*ave_width;
232
233 max_width = (int)(ave_width + sqrt(dispers) + 1);
234 }// pass
235
236 int last_good = 0;
237 int good_right[512]={0}; // index [i]=> right end of good good segment with i
238 int good_left[512]={0}; // index [i]=> left end of good good segment with i
239 Bool outside = TRUE;
240 for (i=511; i>=0; i--) // compute good_right[]
241 {
242 if (wdt[i] && wdt[i] <= max_width) // good point
243 {
244 if (outside) // good segment started
245 { good_right[i] = i;
246 last_good = i;
247 outside = FALSE;
248 }
249 else // inside
250 good_right[i] = last_good;
251 }
252 else // bad point
253 {
254 if (!outside) // prev point was start of good segm
255 {
256 outside = TRUE;
257 }
258 }
259 }
260 outside = TRUE;
261 for (i=0; i < 512; i++) // compute good_left[]
262 {
263 if (wdt[i] && wdt[i] <= max_width) // good point
264 {
265 if (outside) // good segment started
266 { good_left[i] = i;
267 last_good = i;
268 outside = FALSE;
269 }
270 else // inside
271 good_left[i] = last_good;
272 }
273 else // bad point
274 {
275 if (!outside) // prev point was start of good segm
276 {
277 outside = TRUE;
278 }
279 }
280 }
281 // 04 oct ////////////////
282 { // find longmost zone
283 int maxzonelen=0; int maxzonestart = 0;
284 for (i=0; i < 512; i++) // compute good_left[]
285 {
286 if (!good_right[i])
287 continue;
288 // reach start of zone
289 if (good_right[i]-i > maxzonelen)
290 {
291 maxzonelen = good_right[i]-i;
292 maxzonestart = i;
293 i+= maxzonelen+1;
294 }
295 }
296 if ( maxzonelen > 30 ) // we demand 240 pels of line
297 {
298 for (i=0; i<maxzonestart; i++)
299 {
300 good_left[i]=good_right[i]=0;
301 }
302 for (i=maxzonestart+maxzonelen+1; i<512; i++)
303 {
304 good_left[i]=good_right[i]=0;
305 }
306 }
307 else
308 {
309 for (i=0; i<512; i++)
310 {
311 good_left[i]=good_right[i]=0;
312 }
313 }
314 }
315 //////////////////////////
316 // 2. compute line parms without this zone(s)
317
318 dash = rb_->firstEntryMember( comp );
319 if (dash ==NULLBHandle)
320 continue;
321 // compute tenzor of inertia...
322 puanso.flashPuanso();
323 do
324 {
325 TDash& dd = (*rb_)[ dash ];
326 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
327 row = dd.firstSegEntry;
328 do
329 {
330 TBlackSeg& bs = (*sb_)[ seg ];
331 int n_goodright = good_right[bs.left>>3];
332 if ( (bs.right>>3) <= n_goodright) // entire segment inside good zone
333 {
334 puanso.increase( bs.left,bs.right,row);
335 }
336 else // segment broken
337 { int halflen = (bs.right-bs.left) >> 1;
338 if (halflen > 10)
339 {
340 if ( (n_goodright<<3) - bs.left > halflen) // more than half of segment on left side is good
341 puanso.increase( bs.left,n_goodright<<3,row);
342 else // let's try right part of segment
343 {
344 int n_goodleft = good_left[bs.right>>3];
345 if (n_goodleft && (bs.right>>3) - n_goodleft > halflen) // some of right part is good
346 puanso.increase( n_goodleft<<3, bs.right,row);
347 }
348 }
349 }
350 row++;seg = bs.nLower;
351 } while ( seg != NULLBHandle );
352 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
353
354 puanso.atOwnCoords();
355 // 3. compute full line cutting in angle of previous estimation
356 if (puanso.isALine()) // wow - one of parts treated as line...
357 {
358 double phi = puanso.Phi;
359 int xx = (int)puanso.Xc;
360 int yy = (int)puanso.Yc;
361 int ww = max_width + 10; //puanso.aveW * 3;
362 int prj0 = (int)(yy + xx*phi);
363
364 dash = rb_->firstEntryMember( comp );
365 if (dash ==NULLBHandle)
366 continue;
367 // compute tenzor of inertia...
368 puanso.flashPuanso();
369 do
370 {
371 TDash& dd = (*rb_)[ dash ];
372 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
373 row = dd.firstSegEntry;
374 do
375 {
376
377 TBlackSeg& bs = (*sb_)[ seg ];
378 int prj = (int)(row + ((bs.right+bs.left)>>1)*phi);
379 if (abs( prj - prj0 ) < ww)
380 {
381 puanso.increase( bs.left,
382 bs.right,
383 row
384 );
385 }
386 row++;seg = bs.nLower;
387 } while ( seg != NULLBHandle );
388 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
389
390 puanso.atOwnCoords();
391 isLine = puanso.isALine();
392 }
393 } // if (!isLine && (puanso.right-puanso.left) > 600)
394 }
395
396 /////////////////////////////////////////////////////////////////////////////////
397 #if 0
398 int center = (puanso.right+puanso.left)>>1;
399
400 // test left part of line
401 dash = rb_->firstEntryMember( comp );
402 if (dash ==NULLBHandle)
403 continue;
404 // compute tenzor of inertia...
405 puanso.flashPuanso();
406 do {
407 TDash& dd = (*rb_)[ dash ];
408 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
409 row = dd.firstSegEntry;
410 do {
411 TBlackSeg& bs = (*sb_)[ seg ];
412 if (bs.left < center)
413 {
414 puanso.increase( bs.left,
415 bs.right < center ? bs.right : center,
416 row
417 );
418 }
419 row++;seg = bs.nLower;
420 } while ( seg != NULLBHandle );
421 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
422
423 puanso.atOwnCoords();
424
425 if (!puanso.isALine())
426 { // try right part...
427 dash = rb_->firstEntryMember( comp );
428 if (dash ==NULLBHandle)
429 continue;
430 // compute tenzor of inertia...
431 puanso.flashPuanso();
432 do {
433 TDash& dd = (*rb_)[ dash ];
434 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
435 row = dd.firstSegEntry;
436 do {
437 TBlackSeg& bs = (*sb_)[ seg ];
438 if (bs.right > center)
439 {
440 puanso.increase( bs.left > center ? bs.left : center,
441 bs.right,
442 row
443 );
444 }
445 row++;seg = bs.nLower;
446 } while ( seg != NULLBHandle );
447 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
448
449 puanso.atOwnCoords();
450 }
451
452 if (puanso.isALine()) // wow - one of parts treated as line...
453 {
454 double phi = puanso.Phi;
455 int xx = puanso.Xc;
456 int yy = puanso.Yc;
457 int ww = puanso.aveW * 3;
458 int prj0 = yy + xx*phi;
459
460 dash = rb_->firstEntryMember( comp );
461 if (dash ==NULLBHandle)
462 continue;
463 // compute tenzor of inertia...
464 puanso.flashPuanso();
465 do {
466 TDash& dd = (*rb_)[ dash ];
467 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
468 row = dd.firstSegEntry;
469 do {
470 TBlackSeg& bs = (*sb_)[ seg ];
471 int prj = row + ((bs.right+bs.left)>>1)*phi;
472 if (abs( prj - prj0 ) < ww)
473 {
474 puanso.increase( bs.left,
475 bs.right,
476 row
477 );
478 }
479 row++;seg = bs.nLower;
480 } while ( seg != NULLBHandle );
481 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
482
483 puanso.atOwnCoords();
484 isLine = puanso.isALine();
485 }
486 }
487
488 // end of 15 jul 98 (cutting a line)
489 /////////////////////////////////////////////////////
490 #endif
491
492 // now tenzor of inertia has computed. Make line now...
493 if (isLine)
494 {
495 fragment = &((*this)[frag_handle]);
496 if (frg)
497 frg->frag_handle = (short)frag_handle;
498 fragment->fragmentAsIs.start.x = puanso.left;
499 fragment->fragmentAsIs.end.x = puanso.right - 1;
500 fragment->fragmentAsIs.start.y =(int32_t)( puanso.Yc + (puanso.left-puanso.Xc)*puanso.Phi );
501 fragment->fragmentAsIs.end.y =(int32_t)( puanso.Yc + (puanso.right-puanso.Xc)*puanso.Phi );
502 fragment->fragmentAsIs.width10 = (short)(puanso.aveW * 10.);
503 fragment->fragmentAsIs.flags = 0;
504 if (puanso.right - puanso.left > 64)
505 {
506 int len = puanso.right - puanso.left;
507 int factor = 1; // len >> 7; // div 128
508 sum_phi += factor * len * puanso.Phi;
509 sum_len += factor * len;
510 }
511 fragment->rasterBambukEntry = comp;
512 frag_handle++;
513 }; // is a line
514
515 /////////////////////////////////////////////////
516 /// 4 dec 98, VP
517 /// Check for black squares:
518 int height = up - down + 1;
519 int width = puanso.right-puanso.left;
520 if ( height > 24 && abs(height + 2 - width) < 4 )
521 // box is about square - diff between height and width less than 4 pels!
522 // note: two pels addition for for height slightly compensates horisontal filtration
523 { double square = height* (double)width;
524 if ( fabs(square - puanso.mass) < (square + puanso.mass) * 0.1 )
525 { // 80 percents of square is black
526 RegisterBlackSquare(puanso.left, down, puanso.right-1, up);
527 };
528 };
529 //////////////////////////////////////////////////
530 }; // for
531 ////////////////////
532 // add fragments from HLiner
533 int nhl = HLiner_GetCount();
534 for (i=0; i<nhl; i++)
535 {
536 // array is allocated of count_of_rasters + count_of_hliner_lines,
537 // so we can add them without test
538
539 Line16& li = *HLiner_GetLine(i);
540 fragment = &((*this)[frag_handle]);
541 fragment->fragmentAsIs.start.x = li.A.x;
542 fragment->fragmentAsIs.start.y = li.A.y;
543 fragment->fragmentAsIs.end.x = li.B.x;
544 fragment->fragmentAsIs.end.y = li.B.y;
545 fragment->fragmentAsIs.width10 = 20; //(short)(puanso.aveW * 10.);
546 fragment->fragmentAsIs.flags = LF_HLINER;
547 fragment->rasterBambukEntry = NULLBHandle;
548 frag_handle++;
549 }
550 ///////////////////
551
552 fragmentsCount = (uint16_t)frag_handle; // count of line fragments
553 if (fragmentsCount > 0 && sum_len > 0)
554 {
555 averagePhi = (sum_phi) / (double)(sum_len);
556 }
557 else
558 averagePhi = 1.; // sign off
559 }
560
561 void TLinesBambuk::computeVRasters( TRasterBambuk* rb, TSegBambuk* sb ){
562
563 rb_ = rb;
564 sb_ = sb;
565 assert(rb_);
566 assert(sb_);
567
568 BEntry last_comp = rb_->lastEntry();
569 BHandle dash, seg;
570 BEntry col;
571 BHandle frag_handle = 0;
572 // PTLineFragment fragment;
573 TPuanso puanso;
574 double sum_phi = 0.0;
575 int32_t sum_len = 0;
576
577 /// 10.02.99, VP ------- registering fragments for external usage
578 Frag_VAlloc( rb->totalRasterCount() );
579 int nfrag=0;
580 /// --------------------
581
582 for ( BEntry comp = 0; comp <= last_comp; comp++ )
583 { dash = rb_->firstEntryMember( comp );
584
585 if (dash ==NULLBHandle)
586 continue;
587
588 // compute tenzor of inertia...
589 puanso.flashPuanso();
590 int left = 0x7ff0;
591 int right= 0;
592 do {
593 TDash& dd = (*rb_)[ dash ];
594 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
595 col = dd.firstSegEntry;
596 left = MIN( left, (int)col );
597 do {
598 TBlackSeg& bs = (*sb_)[ seg ];
599 puanso.increase( bs.left, // down
600 bs.right, // up
601 col
602 );
603 col++; seg = bs.nLower;
604 } while ( seg != NULLBHandle );
605 right = MAX( right, (int)col );
606 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
607
608 puanso.atOwnCoords();
609
610 /////////////// VP: 15 jul 98 : cutting a line
611 Bool isLine = puanso.isALine();
612
613 /// 10.02.99, VP ------- registering fragments for external usage
614 LnsFrag* frg = Frag_VGet( nfrag ); nfrag++;
615 {
616 if (frg)
617 {
618 if (isLine)
619 frg->flags = LF_LINE;
620 else
621 frg->frag_handle = -1; // else will set below
622
623 frg->rc.left = left;
624 frg->rc.right = right-1;
625 frg->rc.top = puanso.left;
626 frg->rc.bottom = puanso.right+1;
627
628 frg->mass = (long)puanso.mass ;
629 frg->Xc =puanso.Yc ;
630 frg->Yc =puanso.Xc ;
631 frg->Phi =puanso.Phi ;//??? transp?
632 frg->aveW =puanso.aveW ;
633 frg->mainMxx =puanso.mainMxx;//??? transp?
634 frg->mainMyy =puanso.mainMyy;//???
635 // frg->relMxx =puanso.relMxx ;//???
636 // frg->relMxy =puanso.relMxy ;//???
637 // frg->relMyy =puanso.relMyy ;//???
638 frg->A.y = puanso.left;
639 frg->B.y = puanso.right + 1;
640 frg->A.x = (int16_t)( puanso.Yc + (puanso.left-puanso.Xc)*puanso.Phi );
641 frg->B.x = (int16_t)( puanso.Yc + (puanso.right-puanso.Xc)*puanso.Phi );
642 }
643 }
644 ///-----------------------
645
646 if (!isLine && (puanso.right-puanso.left) > 600)
647 { // try to recover long line with glued dust
648 int center = (puanso.right+puanso.left)>>1;
649
650 // test left part of line
651 dash = rb_->firstEntryMember( comp );
652 if (dash ==NULLBHandle)
653 continue;
654 // compute tenzor of inertia...
655 puanso.flashPuanso();
656 do {
657 TDash& dd = (*rb_)[ dash ];
658 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
659 col = dd.firstSegEntry;
660 do {
661 TBlackSeg& bs = (*sb_)[ seg ];
662 if (bs.left < center)
663 {
664 puanso.increase( bs.left,
665 bs.right < center ? bs.right : center,
666 col
667 );
668 }
669 col++;seg = bs.nLower;
670 } while ( seg != NULLBHandle );
671 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
672
673 puanso.atOwnCoords();
674
675 if (!puanso.isALine())
676 { // try right part...
677 dash = rb_->firstEntryMember( comp );
678 if (dash ==NULLBHandle)
679 continue;
680 // compute tenzor of inertia...
681 puanso.flashPuanso();
682 do {
683 TDash& dd = (*rb_)[ dash ];
684 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
685 col = dd.firstSegEntry;
686 do {
687 TBlackSeg& bs = (*sb_)[ seg ];
688 if (bs.right > center)
689 {
690 puanso.increase( bs.left > center ? bs.left : center,
691 bs.right,
692 col
693 );
694 }
695 col++;seg = bs.nLower;
696 } while ( seg != NULLBHandle );
697 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
698
699 puanso.atOwnCoords();
700 }
701
702 if (puanso.isALine()) // wow - one of parts treated as line...
703 {
704 double phi = puanso.Phi;
705 int yy = (int)puanso.Xc;
706 int xx = (int)puanso.Yc;
707 int ww = (int)puanso.aveW * 3;
708 int prj0 = (int)(xx - yy*phi);
709
710 dash = rb_->firstEntryMember( comp );
711 if (dash ==NULLBHandle)
712 continue;
713 // compute tenzor of inertia...
714 puanso.flashPuanso();
715 do {
716 TDash& dd = (*rb_)[ dash ];
717 seg = dd.firstSegHandle; assert( seg != NULLBHandle );
718 col = dd.firstSegEntry;
719 do {
720 TBlackSeg& bs = (*sb_)[ seg ];
721 int prj = (int)(col - ((bs.right+bs.left)>>1)*phi);
722 if (abs( prj - prj0 ) < ww)
723 {
724 puanso.increase( bs.left,
725 bs.right,
726 col
727 );
728 }
729 col++;seg = bs.nLower;
730 } while ( seg != NULLBHandle );
731 } while ((dash = rb_->nextMember( dash ))!=NULLBHandle);
732
733 puanso.atOwnCoords();
734 isLine = puanso.isALine();
735 }
736 }
737
738 // end of 15 jul 98 (cutting a line)
739 /////////////////////////////////////////////////////
740
741 // now tenzor of inertia has computed. Make line now...
742 if (isLine){
743 TLineFragment& frag = (*this)[frag_handle];
744 //////// 13 may 99
745 if (frg)
746 frg->frag_handle = (short)frag_handle;
747 ////////
748 frag.fragmentAsIs.start.y = puanso.left;
749 frag.fragmentAsIs.end.y = puanso.right + 1;
750 frag.fragmentAsIs.start.x = (int32_t)( puanso.Yc + (puanso.left-puanso.Xc)*puanso.Phi );
751 frag.fragmentAsIs.end.x = (int32_t)( puanso.Yc + (puanso.right-puanso.Xc)*puanso.Phi );
752 frag.fragmentAsIs.width10 = (short)(puanso.aveW * 10.);
753 frag.fragmentAsIs.flags = 0;
754 if (puanso.right - puanso.left > 64)
755 {
756 int len = puanso.right - puanso.left;
757 int factor = 1; // len >> 7; // div 128
758 sum_phi -= factor * len * puanso.Phi;
759 sum_len += factor * len;
760 }
761 frag.rasterBambukEntry = comp;
762 frag_handle++;
763 }; // is a line
764
765 }; // for
766 fragmentsCount = (int32_t)frag_handle; // count of line fragments
767 if (fragmentsCount > 0 && sum_len > 0)
768 {
769 averagePhi = (sum_phi) / (double)(sum_len);
770 }
771 else
772 averagePhi = 1.; // sign off
773 }
774
775 Bool TLinesBambuk::linkHFragments( void ){
776 // compute fragments levels
777 double _averagePhi = averagePhi == 1. ? 0 : averagePhi;
778 for( BHandle frag = 0; frag < fragmentsCount; frag++)
779 { TLineFragment& frg =(*this)[frag];
780 frg.level = frg.fragmentAsIs.start.y -
781 (int)
782 ( (double)( frg.fragmentAsIs.start.x ) * _averagePhi
783 );
784 };
785 // sort fragments by level
786 if (fragmentsCount > 0)
787 qsort( &((*this)[0]), fragmentsCount, sizeof(TLineFragment), compFragLevels );
788
789 // cleanLinks();
790 // first pass: make initial lines by fragments
791 Bool not_linked[ MAX_H_RASTERS_COUNT ];
792 for (int i = 0; i < MAX_H_RASTERS_COUNT; i++) not_linked[i] = TRUE;
793
794 int cur_line = 0;
795 BHandle base_frag = 0; // first unlinked fragment
796 BHandle next_frag = 0; // current fragment to try link
797 while( base_frag < fragmentsCount ){
798 // copy and link first
799 appendMember( cur_line, base_frag );
800 not_linked[ base_frag ] = FALSE;
801 linesRoot[ cur_line ].lineAsIs = (*this)[base_frag].fragmentAsIs; //??
802 linesRoot[ cur_line ].level = (*this)[base_frag].level;
803 linesRoot[ cur_line ].linesBambukEntry = cur_line;
804 linesCount++;
805 // link next fragments in near levels (if can)
806 next_frag = base_frag+1;
807 while ( (next_frag < fragmentsCount)
808 &&
809 ( abs(
810 linesRoot[cur_line].level - (*this)[next_frag].level
811 ) < CHECK_FRAGMENTS_DELTA
812 )
813 )
814 { // try to link...
815 if ( not_linked[next_frag]
816 &&
817 has_H_linked( &(linesRoot[ cur_line ].lineAsIs),
818 &((*this)[ next_frag ].fragmentAsIs)
819 )
820 ){ appendMember( cur_line, next_frag );
821 not_linked[ next_frag ] = FALSE;
822 next_frag = base_frag; // lets repeat trying
823 };
824 next_frag++;
825 };
826 // seek next base_frag
827 base_frag++;
828 while ( (base_frag < fragmentsCount)
829 &&
830 !not_linked[ base_frag ]
831 )
832 { base_frag ++;
833 };
834 cur_line++;
835 if (cur_line > lastEntry())
836 {
837 assert(cur_line <= lastEntry());
838 return FALSE;
839 }
840 #ifndef NDEBUG
841 //if ( (cur_line % 10) == 0 )
842 // printf(".");
843 #endif
844 }; // while bas_frag...
845 // second pass - try link lines ... (???)
846 // <...>
847 return TRUE;
848 }
849
850 Bool TLinesBambuk::linkVFragments( void ){
851 // compute fragments levels
852 double _averagePhi = averagePhi == 1. ? 0 : averagePhi;
853 for( BHandle frag = 0; frag < fragmentsCount; frag++)
854 { TLineFragment& frg =(*this)[frag];
855 frg.level = frg.fragmentAsIs.start.x +
856 (int)
857 ( (double)( frg.fragmentAsIs.start.y ) * _averagePhi
858 );
859 };
860 // sort fragments by level
861 if (fragmentsCount > 0)
862 qsort( &((*this)[0]), fragmentsCount, sizeof(TLineFragment), compFragLevels );
863
864 // cleanLinks();
865
866 // first pass: make initial lines by fragments
867 Bool not_linked[ MAX_V_RASTERS_COUNT ];
868 for (int i = 0; i < MAX_V_RASTERS_COUNT; i++) not_linked[i] = TRUE;
869
870 int cur_line = 0;
871 BHandle base_frag = 0; // first unlinked fragment
872 BHandle next_frag = 0; // current fragment to try link
873 while( base_frag < fragmentsCount ){
874 // copy and link first
875 appendMember( cur_line, base_frag );
876 not_linked[ base_frag ] = FALSE;
877 linesRoot[ cur_line ].lineAsIs = (*this)[base_frag].fragmentAsIs; //??
878 linesRoot[ cur_line ].level = (*this)[base_frag].level;
879 linesRoot[ cur_line ].linesBambukEntry = cur_line;
880 linesCount++;
881 // link next fragments in near levels (if can)
882 next_frag = base_frag+1;
883 while ( (next_frag < fragmentsCount)
884 &&
885 ( abs(
886 linesRoot[cur_line].level - (*this)[next_frag].level
887 ) < CHECK_FRAGMENTS_DELTA
888 )
889 )
890 { // try to link...
891 if ( not_linked[next_frag]
892 &&
893 has_V_linked( &(linesRoot[ cur_line ].lineAsIs),
894 &((*this)[ next_frag ].fragmentAsIs)
895 )
896 ){ appendMember( cur_line, next_frag );
897 not_linked[ next_frag ] = FALSE;
898 next_frag = base_frag; // lets repeat trying
899 };
900 next_frag++;
901 };
902 // seek next base_frag
903 base_frag++;
904 while ( (base_frag < fragmentsCount)
905 &&
906 !not_linked[ base_frag ]
907 ) { base_frag ++;
908 };
909 cur_line++;
910 if (cur_line > lastEntry())
911 {
912 assert(cur_line <= lastEntry());
913 return FALSE;
914 }
915 #ifndef NDEBUG
916 //if ( (cur_line % 10) == 0 )
917 // printf(".");
918 #endif
919 };
920 // second pass - try link lines ... (???)
921 // <...>
922 return TRUE;
923 }
924
925 /********************************************************/
926 int cdecl compFragLevels( const void *frag1, const void *frag2 ){
927 return ( (*(TLineFragment*)frag1).level -
928 (*(TLineFragment*)frag2).level
929 );
930 }
931
932 #if 0 // old, before 04 jun 99
933 Bool has_H_linked( PLine line, PLine frag ){
934
935 if (line->flags & LF_DONTLINK || frag->flags & LF_DONTLINK)
936 return FALSE;
937
938 int32_t sum_len = (line->end.x - line->start.x) + (frag->end.x - frag->start.x);
939 int32_t gap = 0;
940
941 if ( abs( line->width10 - frag->width10 ) > 15 ) // more than 1.5 pels width diff!
942 return FALSE;
943
944 if ( ( (gap = abs(frag->start.x - line->end.x)) < LINK_FRAGMENTS_GAP )
945 &&
946 ( (gap * LINK_FRAGMENTS_RATIO) < sum_len )
947 &&
948 ( abs(frag->start.y - line->end.y) < LINK_FRAGMENTS_DELTA )
949 ) {
950 line->end = frag->end;
951 return (TRUE);
952 };
953
954 if ( ( (gap = abs(line->start.x - frag->end.x)) < LINK_FRAGMENTS_GAP )
955 &&
956 ( (gap * LINK_FRAGMENTS_RATIO) < sum_len )
957 &&
958 ( abs(line->start.y - frag->end.y) < LINK_FRAGMENTS_DELTA )
959 ) {
960 line->start = frag->start;
961 return (TRUE);
962 };
963
964 return (FALSE);
965 }
966 #else // 04 jun 99 - join overlapped too
967 Bool has_H_linked( PLine line, PLine frag )
968 {
969 if (line->flags & LF_DONTLINK || frag->flags & LF_DONTLINK)
970 return FALSE;
971
972 int32_t sum_len = (line->end.x - line->start.x) + (frag->end.x - frag->start.x);
973 int32_t gap = 0;
974
975 if ( abs( line->width10 - frag->width10 ) > 15 ) // more than 1.5 pels width diff!
976 {
977 if (!(line->flags & LF_HLINER || frag->flags & LF_HLINER)) // currently, 04 jun 99, HLiner reports not correct width
978 return FALSE;
979 }
980
981 if (frag->start.x > line->end.x) // fragment is on right side of the line
982 {
983 gap = frag->start.x - line->end.x;
984 if ( gap < LINK_FRAGMENTS_GAP
985 &&
986 (gap * LINK_FRAGMENTS_RATIO < sum_len )
987 &&
988 ( abs(frag->start.y - line->end.y) < LINK_FRAGMENTS_DELTA )
989 )
990 {
991 line->end = frag->end;
992 return (TRUE);
993 };
994 return (FALSE); // else not linked
995 }
996
997 if (line->start.x > frag->end.x) // fragment is on left side of the line
998 {
999 gap = line->start.x - frag->end.x;
1000 if ( gap < LINK_FRAGMENTS_GAP
1001 &&
1002 gap * LINK_FRAGMENTS_RATIO < sum_len
1003 &&
1004 abs(line->start.y - frag->end.y) < LINK_FRAGMENTS_DELTA
1005 )
1006 {
1007 line->start = frag->start;
1008 return (TRUE);
1009 };
1010 return (FALSE); // else not linked
1011 }
1012
1013 // here we have two overlapped lines...
1014 Point32 *p_left, *p_right; // leftmost and rightmost points
1015 Point32 *p_rest1,*p_rest2; // the rest points
1016 if (line->start.x < frag->start.x)
1017 {
1018 p_left = &line->start;
1019 p_rest1= &frag->start;
1020 }
1021 else
1022 {
1023 p_left = &frag->start;
1024 p_rest1= &line->start;
1025 }
1026
1027 if (line->end.x > frag->end.x)
1028 {
1029 p_right= &line->end;
1030 p_rest2= &frag->end;
1031 }
1032 else
1033 {
1034 p_right= &frag->end;
1035 p_rest2= &line->end;
1036 }
1037
1038 { // test rest1
1039 int x1= p_rest1->x - p_left->x;
1040 int x2= p_right->x - p_rest1->x;
1041
1042 if (x1 + x2 == 0)
1043 return (FALSE);
1044 int y = ( p_left->y * x2 + p_right->y * x1 ) / (x1 + x2);
1045 if (abs(p_rest1->y - y) > 4)
1046 return (FALSE); // more than 4 pixels diff
1047 }
1048 { // test rest2
1049 int x1= p_rest2->x - p_left->x;
1050 int x2= p_right->x - p_rest2->x;
1051
1052 if (x1 + x2 == 0)
1053 return (FALSE);
1054 int y = ( p_left->y * x2 + p_right->y * x1 ) / (x1 + x2);
1055 if (abs(p_rest2->y - y) > 4)
1056 return (FALSE); // more than 4 pixels diff
1057 }
1058
1059 if (line->flags & LF_HLINER || frag->flags & LF_HLINER)
1060 {
1061 if ( abs(line->start.x - frag->start.x) >= 16 ) // diff is big
1062 line->start = *p_left; // change if diff more than 16 pels, else keep old
1063 else // diff small - get from not HLiner
1064 {
1065 if (line->flags & LF_HLINER && !(frag->flags & LF_HLINER)) // frag more precise
1066 line->start = frag->start;
1067 }
1068
1069 if ( abs(line->end.x - frag->end.x) >= 16 ) // diff is big
1070 line->end = *p_right; // change if diff more than 16 pels, else keep old
1071 else // diff small - get from not HLiner
1072 {
1073 if (line->flags & LF_HLINER && !(frag->flags & LF_HLINER)) // frag more precise
1074 line->end = frag->end;
1075 }
1076 }
1077 else // both lines not from HLINER
1078 {
1079 line->start = *p_left;
1080 line->end = *p_right;
1081 }
1082
1083 return (TRUE);
1084 }
1085
1086 #endif
1087
1088 inline int line_length( PLine line )
1089 {
1090 return MAX(abs(line->end.x-line->start.x), abs(line->end.y-line->start.y));
1091 }
1092
1093
1094 Bool has_V_linked( PLine line, PLine frag ){
1095
1096 if (line->flags & LF_DONTLINK || frag->flags & LF_DONTLINK)
1097 return FALSE;
1098
1099 if ( abs( line->width10 - frag->width10 ) > 15 ) // more than 1.5 pels width diff!
1100 return FALSE;
1101
1102
1103 int32_t sum_len = (line->end.y - line->start.y) + (frag->end.y - frag->start.y);
1104
1105 PLine fst = frag->start.y < line->start.y ? frag : line;
1106 PLine sec = frag->start.y < line->start.y ? line : frag;
1107
1108 int min_len = MIN( line_length(fst), line_length(sec) );
1109 // test delta
1110 int32_t delta = abs(fst->end.x - sec->start.x);
1111 int delta_limit = (min_len < 50) ? 5 : 10;
1112 if (delta > delta_limit)
1113 return FALSE;
1114
1115 // test gap
1116 int32_t gap = abs(fst->end.y - sec->start.y);
1117 int gap_limit = (min_len < 50) ? 15 : 30;
1118
1119 if (gap > gap_limit)
1120 return FALSE;
1121 if ( gap * LINK_FRAGMENTS_RATIO > sum_len )
1122 return FALSE;
1123
1124 // make connection
1125 if (frag->start.y < line->start.y)
1126 line->start = frag->start;
1127 else
1128 line->end = frag->end;
1129
1130 return (TRUE);
1131 }
1132
1133 /////////////////////////////////////
1134 #define CORNER_DELTA 3 // <= | ver...start.x - hor...start.x | ; y also
1135 #define HV_LENGTH_DELTA 6 // <= | checkbox height-width |
1136 #define VV_LENGTH_DELTA 3 // <= | left_len - right_len |
1137
1138 #define MAX_CHBOX_SIZE 80
1139 #define MIN_CHBOX_SIZE 20
1140
1141 static TLineFragment* _curr = NULL;
1142 int cdecl byStartY( const void *int1, const void *int2)
1143 {
1144 return _curr[*(int*)int1].fragmentAsIs.start.y -
1145 _curr[*(int*)int2].fragmentAsIs.start.y;
1146 }
1147
1148 static
1149 void _RegisterCheckBox( Line& left,
1150 Line& top,
1151 Line& right,
1152 Line& bottom
1153 )
1154 {
1155 Rect16 rcb;
1156 rcb.left = (short)(( left.start.x + left.end.x ) >> 1);
1157 rcb.right = (short)(( right.start.x + right.end.x ) >> 1);
1158 rcb.top = (short)(( top.start.y + top.end.y ) >> 1);
1159 rcb.bottom = (short)(( bottom.start.y + bottom.end.y ) >> 1);
1160
1161 left.flags |= LF_DONTLINK;
1162 right.flags |= LF_DONTLINK;
1163 top.flags |= LF_DONTLINK;
1164 bottom.flags |= LF_DONTLINK;
1165
1166 RegisterCheckBox(rcb);
1167 }
1168
1169 inline Bool OutRect( Point32& pt, Rect32& rc )
1170 {
1171 return (pt.x < rc.left) || (pt.x > rc.right) || (pt.y < rc.top) || (pt.y > rc.bottom);
1172 }
1173
1174 inline Bool OutRange( int x, int minim, int maxim )
1175 {
1176 return (x < minim) || (x > maxim);
1177 }
1178
1179 inline void Point2Rect( Rect32& rc, Point32& pt, int delta )
1180 {
1181 rc.left = pt.x - delta;
1182 rc.right = pt.x + delta;
1183 rc.top = pt.y - delta;
1184 rc.bottom= pt.y + delta;
1185 }
1186
1187 Bool AnalyzeFragments( TLinesBambuk& hLB, TLinesBambuk& vLB)
1188 {
1189 #if 1
1190 /////////////////////////////
1191 // prepeare data
1192 int nv = vLB.fragmentsCount;
1193 int nh = hLB.fragmentsCount;
1194
1195 if (nv==0 || nh==0)
1196 return TRUE;
1197
1198 if (nv > MAX_V_RASTERS_COUNT)
1199 return WRONG();
1200 if (nh > MAX_H_RASTERS_COUNT)
1201 return WRONG();
1202
1203 int vv[ MAX_V_RASTERS_COUNT ] = {0};
1204 int hh[ MAX_H_RASTERS_COUNT ] = {0};
1205
1206 int i(0);
1207 for (i=0; i < nv; i++ ) vv[i]=i;
1208 for (i=0; i < nh; i++ ) hh[i]=i;
1209
1210 TLineFragment* _lf_hor = &(hLB[0]);
1211 TLineFragment* _lf_ver = &(vLB[0]);
1212
1213 _curr = _lf_ver; qsort(&vv, nv, sizeof(vv[0]), byStartY);
1214 _curr = _lf_hor; qsort(&hh, nh, sizeof(hh[0]), byStartY);
1215
1216 /////////////////////////////
1217 // start checkbox search
1218
1219 int prev_ver = 0;
1220 for ( int top_hor=0; top_hor < nh; top_hor++ )
1221 { // by all horisontal fragments,
1222 // in order up to down (start.y increases)
1223 Line& top = _lf_hor[hh[top_hor]].fragmentAsIs; // potential top of checkbox
1224 int top_len = top.end.x - top.start.x;
1225 if (OutRange( top_len, MIN_CHBOX_SIZE, MAX_CHBOX_SIZE ))
1226 continue;
1227
1228 Rect32 corner0; Point2Rect(corner0, top.start, CORNER_DELTA);
1229 Rect32 corner1; Point2Rect(corner1, top.end, CORNER_DELTA);
1230 int zone_start = MIN(corner0.top, corner1.top);
1231 int zone_finish = MAX(corner0.bottom, corner1.bottom);
1232
1233 //// skip to start of test zone
1234 while ( prev_ver < nv && _lf_ver[vv[prev_ver]].fragmentAsIs.start.y < zone_start )
1235 prev_ver++;
1236
1237 if (!(prev_ver < nv))
1238 break; // finished
1239
1240 // we at start of test zone (prev_ver - is first vertical segment)
1241
1242 for ( int left_ver = prev_ver; left_ver < nv; left_ver++ )
1243 {
1244 Line& left = _lf_ver[vv[left_ver]].fragmentAsIs;
1245 if (left.start.y > zone_finish)
1246 break; // out of zone
1247
1248 if ( OutRect(left.start, corner0 ) )
1249 continue; // no connection
1250
1251 int left_len = left.end.y - left.start.y;
1252 if ( abs(left_len - top_len) > HV_LENGTH_DELTA )
1253 continue; // diff length!
1254 /////////////////////////////////////////////////////////////////////
1255 // now we have left-top corner; try to make bottom and right parts
1256
1257 for ( int right_ver = prev_ver; right_ver < nv; right_ver++ )
1258 {
1259 Line& right = _lf_ver[vv[right_ver]].fragmentAsIs;
1260 if (right.start.y > zone_finish)
1261 break; // out of zone
1262
1263 if ( OutRect( right.start, corner1 ) )
1264 continue; // no connection
1265
1266 int right_len = right.end.y - right.start.y;
1267 if ( abs(right_len - left_len) > VV_LENGTH_DELTA )
1268 continue; // diff length!
1269 /////////////////////////////////////////////////////////////////////
1270 // now we have 'П'- connection; test for bottom
1271 Rect32 corner2; Point2Rect(corner2, left.end, CORNER_DELTA);
1272 Rect32 corner3; Point2Rect(corner3, right.end, CORNER_DELTA);
1273 int bottom_zone_finish = MAX(left.end.y, right.end.y) + CORNER_DELTA;
1274 for (int bottom_hor = top_hor; bottom_hor < nh; bottom_hor++ )
1275 {
1276 Line& bottom = _lf_hor[hh[bottom_hor]].fragmentAsIs;
1277 if (bottom.start.y > bottom_zone_finish)
1278 break; // out of zone
1279
1280 int bottom_len = bottom.end.x - bottom.start.x;
1281 if ( abs(bottom_len - top_len) > VV_LENGTH_DELTA )
1282 continue; // diff length!
1283
1284 if ( OutRect( bottom.start, corner2 ) )
1285 continue; // no connection
1286
1287 if ( OutRect( bottom.end, corner3 ) )
1288 continue; // no connection
1289
1290 /////////////////////////////////////////////////////////////////////
1291 // now we have connected rect - register checkbox
1292 _RegisterCheckBox(left, top, right, bottom);
1293 }
1294 /////////////////////////////////////////////////////////////////////
1295 }
1296 /////////////////////////////////////////////////////////////////////
1297 }
1298 }
1299
1300
1301 #endif //0
1302 return TRUE;
1303 }
1304
1305 ///////////////////////////////////////////
1306 // search vertical lines for letter sticks
1307 /*#include <windows.h> // OutputDebugString*/
1308 #include "lns.h"
1309 static LnsFrag* vfrags = NULL;
1310 static LnsFrag* hfrags = NULL;
1311
1312 inline int xlevel( LnsFrag& lf1) { return (lf1.A.x + lf1.B.x) >> 1; }
1313 inline int ylevel( LnsFrag& lf1) { return (lf1.A.y + lf1.B.y) >> 1; }
1314
1315 inline int yheight( LnsFrag& lf1) { return (lf1.B.y - lf1.A.y); }
1316 inline int xheight( LnsFrag& lf1) { return (lf1.B.x - lf1.A.x); }
1317
1318 inline int yproj_pt( Point16& pt, int phi ) // computes projection of point by angle phi to Y-axis
1319 {
1320 return pt.y - ((phi * pt.x) >> 10);
1321 }
1322
1323 inline int yproj_frag( LnsFrag& hor_lf, int* phi ) // computes angle of fragment and it's projection to Y-axis
1324 {
1325 *phi = ( (hor_lf.B.y - hor_lf.A.y) << 10 ) / (hor_lf.B.x - hor_lf.A.x);
1326 return yproj_pt( hor_lf.B, *phi );
1327 }
1328
1329 static int yproject( LnsFrag& lf1, LnsFrag& lf2 )
1330 {
1331 assert(lf1.B.y > lf1.A.y);
1332 assert(lf2.B.y > lf2.A.y); // end point has larger y coord
1333
1334 if (lf1.B.y < lf2.A.y || lf1.A.y > lf2.B.y) // not intersected
1335 return 0;
1336
1337 int h1 = yheight(lf1);
1338 int h2 = yheight(lf2);
1339 int minh = MIN(h1, h2);
1340 assert(minh > 0);
1341
1342 int a = MAX(lf1.A.y,lf2.A.y);
1343 int b = MIN(lf1.B.y,lf2.B.y);
1344
1345 assert(b >= a);
1346
1347 return (100*(b-a))/minh;
1348 }
1349
1350 int cdecl comp_vfrags_byXLevel( const void *int1, const void *int2)
1351 {
1352 LnsFrag & lf1 = vfrags[*(int*)int1];
1353 LnsFrag & lf2 = vfrags[*(int*)int2];
1354 return xlevel(lf1) - xlevel(lf2);
1355 }
1356
1357 int cdecl comp_vfrags_byYLevel( const void *int1, const void *int2)
1358 {
1359 LnsFrag & lf1 = vfrags[*(int*)int1];
1360 LnsFrag & lf2 = vfrags[*(int*)int2];
1361 return ylevel(lf1) - ylevel(lf2);
1362 }
1363 int cdecl comp_hfrags_byYLevel( const void *int1, const void *int2)
1364 {
1365 LnsFrag & lf1 = hfrags[*(int*)int1];
1366 LnsFrag & lf2 = hfrags[*(int*)int2];
1367 return ylevel(lf1) - ylevel(lf2);
1368 }
1369
1370
1371 #define CONSOLE(str) OutputDebugString(str)
1372 #include "xtimer.h"
1373 Bool AnalyzeFragmentsII( TLinesBambuk& hLB, TLinesBambuk& vLB)
1374 {
1375 XTimer t("AnalyzeFragmentsII");
1376 /////////////////////////////
1377 // prepeare data
1378 int nv = LnsGetFragCount(FALSE); // get vertical fragments count
1379 if (nv==0)
1380 return TRUE;
1381
1382 if (nv > MAX_V_RASTERS_COUNT)
1383 return WRONG();
1384 int vv[ MAX_V_RASTERS_COUNT ] = {0};
1385
1386
1387 int i(0);
1388 for (i=0; i < nv; i++ ) vv[i]=i;
1389
1390 vfrags = LnsGetFragments(FALSE);
1391 qsort(&vv, nv, sizeof(vv[0]), comp_vfrags_byXLevel);
1392
1393 /////////////////////
1394 // start text linkage
1395
1396 for ( int cur =0; cur < nv; cur++ )
1397 { // by all vertical fragments
1398 // in order left to right (start.x + end.x increases)
1399 LnsFrag& lf1 = vfrags[vv[cur]];
1400
1401 #define MAX_STICK_HEIGHT 60 // == 60 pels
1402 #define MAX_STICK_DELTA 40 // == 40 pels
1403
1404 if (yheight( lf1 ) > MAX_STICK_HEIGHT)
1405 continue;
1406
1407 int lev1 = xlevel(lf1);
1408 for (int nxt = cur+1; nxt < nv; nxt++)
1409 {
1410 LnsFrag& lf2 = vfrags[vv[nxt]];
1411 int lev2 = xlevel(lf2);
1412 if (lev2 > lev1 + MAX_STICK_DELTA)
1413 break; // out of proximity zone
1414 if (yheight( lf2 ) > MAX_STICK_HEIGHT)
1415 continue;
1416 if (yproject( lf1, lf2 ) > 50) // more than 50% of shortest stick projected to another
1417 {
1418 lf1.flags |= LF_STICK_RIGHT;
1419 lf2.flags |= LF_STICK_LEFT;
1420 }
1421 }
1422
1423 if (lf1.flags & (LF_STICK_RIGHT | LF_STICK_LEFT) && lf1.frag_handle != -1)
1424 {
1425 TLineFragment& lfrg = vLB[lf1.frag_handle];
1426 lfrg.fragmentAsIs.flags |= LF_DONTLINK;
1427 }
1428
1429 }
1430
1431 ///////////////////////////////////
1432 // now vertical sticks are marked -
1433 // let test horisontal near sticks
1434 // to prevent extracting false line
1435 // from text fragments
1436
1437 /////////////////////////////////////////////////////////
1438 // sort both vertical and horisontal fragments up to down
1439
1440 int nh = LnsGetFragCount(TRUE); // get horisontal fragments count
1441 if (nh==0)
1442 return TRUE;
1443
1444 if (nh > MAX_H_RASTERS_COUNT)
1445 return WRONG();
1446 int hh[ MAX_H_RASTERS_COUNT ] = {0};
1447
1448
1449 for (i=0; i < nh; i++ ) hh[i]=i;
1450
1451 hfrags = LnsGetFragments(TRUE);
1452
1453 qsort(&hh, nh, sizeof(hh[0]), comp_hfrags_byYLevel);
1454 qsort(&vv, nv, sizeof(vv[0]), comp_vfrags_byYLevel);
1455
1456
1457 /////////////////////////////////////////////////////////
1458 // for each horisontal fragment test count of vertical
1459 // sticks touching it
1460
1461 int vcur_start = 0; // current start of zone for vertical segments
1462 for ( int hcur =0; hcur < nh; hcur++ )
1463 { // by all horisontal fragments
1464 // in order up to down (start.y + end.y increases)
1465 LnsFrag& hor_lf = hfrags[hh[hcur]];
1466
1467 #define MAX_WORD_LENGTH 300 // 300 pels
1468 if (xheight( hor_lf ) > MAX_WORD_LENGTH) // line too long, don't test
1469 continue;
1470
1471 int ylev_hor = ylevel( hor_lf );
1472
1473 int phi=0;
1474 int yproj_hor = yproj_frag( hor_lf, &phi );
1475
1476 //////////////////////////////
1477 // update vcur_start position
1478 for ( ; vcur_start < nv; vcur_start++)
1479 {
1480 LnsFrag& vlf = vfrags[vv[vcur_start]];
1481 int ylev = ylevel( vlf );
1482 if (ylev > ylev_hor - MAX_STICK_HEIGHT) // reach zone
1483 break;
1484 }
1485
1486 // test inside [ylev_hor - MAX_STICK_HEIGHT, ylev_hor + MAX_STICK_HEIGHT] strip
1487 // <=> from vcur_start until level not too big
1488 int xa = hor_lf.A.x;
1489 int xb = hor_lf.B.x;
1490 xa -= 5; xb += 5;
1491
1492 int nstick_len = 0;
1493 int nsticks_near = 0; // count of sticks intersected this line
1494 int nsticks_intersect = 0; // count of sticks intersected this line
1495 int left = 10000;
1496 int right = -10000;
1497
1498 for ( int vcur = vcur_start; vcur < nv; vcur++)
1499 {
1500 LnsFrag& vlf = vfrags[vv[vcur]];
1501
1502 int ylev = ylevel( vlf );
1503 if (ylev > ylev_hor + MAX_STICK_HEIGHT) // out of zone (by Y coord)
1504 break;
1505
1506 int xlev = xlevel( vlf );
1507 if (xlev < xa || xlev > xb) // out of range (by X coord)
1508 continue;
1509
1510 if (yheight(vlf) > MAX_STICK_HEIGHT) // not a stick - continue
1511 continue;
1512
1513 // we have vertical segment near if (ylev > ylev_hor - MAX_STICK_HEIGHT) // reach zone
1514 int ya = yproj_pt( vlf.A, phi );
1515 int yb = yproj_pt( vlf.B, phi );
1516 if (ya > yproj_hor +5 || yb < yproj_hor-5)
1517 // stick not intersectes nor touches horisontal fragment
1518 continue;
1519
1520 nstick_len += yheight(vlf);
1521 nsticks_near ++;
1522 left = MIN(left, xlev);
1523 right = MAX(right, xlev);
1524
1525 if (ya > yproj_hor || yb < yproj_hor)
1526 // stick not intersectes nor touches horisontal fragment
1527 nsticks_intersect++;
1528 };
1529
1530 #define MIN_FREE_LINE_END 30 // 30 pels should be free from vertical sticks at the end of line
1531
1532 if (nsticks_near > 0)
1533 {
1534 if (abs(hor_lf.A.x - left) < MIN_FREE_LINE_END && // both ends are not free
1535 abs(hor_lf.B.x - right) < MIN_FREE_LINE_END &&
1536 nstick_len > hor_lf.B.x - hor_lf.A.x // sum length of intersected frags > hor len
1537 )
1538 {
1539 if (hor_lf.frag_handle != -1)
1540 {
1541 TLineFragment& lfrg = hLB[hor_lf.frag_handle];
1542 lfrg.fragmentAsIs.flags |= LF_DONTLINK;
1543 lfrg.fragmentAsIs.flags |= LF_TEXT;
1544 }
1545
1546 hor_lf.flags |= LF_TEXT;
1547 }
1548 }
1549 }
1550
1551 return TRUE;
1552 }