source: tags/ms_r16q2/SECEDIT/SEC_paint.cxx

Last change on this file was 14487, checked in by westram, 8 years ago
  • erase commented code
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.9 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : SEC_paint.cxx                                     //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "SEC_root.hxx"
12#include "SEC_graphic.hxx"
13#include "SEC_iter.hxx"
14#include "SEC_drawn_pos.hxx"
15#include "SEC_bonddef.hxx"
16#include "SEC_toggle.hxx"
17
18#include <aw_msg.hxx>
19#include <iupac.h>
20
21#include <ed4_extern.hxx>
22
23#include <arb_defs.h>
24
25#include <iostream>
26#include <sstream>
27
28using namespace std;
29
30// -------------------
31//      Debugging
32
33#if defined(ASSERTION_USED)
34
35inline bool valid_cb_params(AW_CL cd1, AW_CL cd2) {
36    return cd1 == 0 || cd2 != -1;
37}
38inline bool valid_cb_params(AW_device *device) {
39    const AW_click_cd *cd = device->get_click_cd();
40    return valid_cb_params(cd->get_cd1(), cd->get_cd2());
41}
42
43#endif
44
45#if defined(DEBUG)
46// #define PAINT_REGION_INDEX // // paint region-internal index next to base
47
48static void paintDebugInfo(AW_device *device, int color, const Position& pos, const char *txt) {
49    sec_assert(valid_cb_params(device));
50    device->circle(color, AW::FillStyle::SOLID, pos.xpos(), pos.ypos(), 0.06, 0.06);
51    device->text(SEC_GC_DEFAULT, txt, pos.xpos(), pos.ypos(), 0, AW_SCREEN, 0);
52}
53static void paintStrandDebugInfo(AW_device *device, int color, SEC_helix_strand *strand) {
54    AW_click_cd cd(device, strand->self(), strand->rightAttachAbspos());        paintDebugInfo(device, color, strand->rightAttachPoint(), "RAP");
55    cd.set_cd2(strand->leftAttachAbspos());                                     paintDebugInfo(device, color, strand->leftAttachPoint(), "LAP");
56    cd.set_cd2(strand->startAttachAbspos());                                    paintDebugInfo(device, color, strand->get_fixpoint(), strand->isRootsideFixpoint() ? "RFP" : "FP");
57}
58
59#endif // DEBUG
60
61// -------------------
62//      PaintData
63
64class PaintData {
65    int gc_edit4_to_secedit[ED4_G_DRAG+1]; // GC translation table (EDIT4 -> SECEDIT)
66    int line_property_gc[SEC_GC_LAST_DATA+1][SEC_GC_LAST_DATA+1]; // of two GCs, which is responsible for line properties
67
68public:
69    PaintData() {
70        int gc;
71
72        // GC translation (EDIT4->SECEDIT)
73        for (gc = 0; gc <= ED4_G_DRAG; gc++) {
74            gc_edit4_to_secedit[gc] = -1; // invalid
75        }
76        for (gc = ED4_G_SBACK_0; gc <= ED4_G_SBACK_8; gc++) {
77            gc_edit4_to_secedit[gc] = gc-ED4_G_SBACK_0+SEC_GC_SBACK_0;
78        }
79        for (gc = ED4_G_CBACK_0; gc <= ED4_G_CBACK_9; gc++) {
80            gc_edit4_to_secedit[gc] = gc-ED4_G_CBACK_0+SEC_GC_CBACK_0;
81        }
82
83        // calc line property GCs
84        for (gc = SEC_GC_FIRST_DATA; gc <= SEC_GC_LAST_DATA; gc++) {
85            for (int gc2 = SEC_GC_FIRST_DATA; gc <= SEC_GC_LAST_DATA; gc++) {
86                int prop_gc;
87                if (gc == gc2) {
88                    prop_gc = gc;
89                }
90                else {
91                    if (gc == SEC_GC_LOOP || gc2 == SEC_GC_LOOP) {
92                        prop_gc = SEC_GC_LOOP; // use loop-properties in loop and at loop-helix-transition
93                    }
94                    else if (gc == SEC_GC_NHELIX || gc2 == SEC_GC_NHELIX) {
95                        prop_gc = SEC_GC_NHELIX; // use nhelix-properties in nhelix and at helix-nhelix-transition
96                    }
97                    else {
98                        prop_gc = SEC_GC_HELIX; // use helix-properties in helix
99                    }
100                }
101                line_property_gc[gc][gc2] = prop_gc;
102            }
103        }
104    }
105
106    int convert_BackgroundGC(int edit4_gc) const {
107        // returns -1 if edit4_gc is invalid
108        sec_assert(edit4_gc >= 0 && edit4_gc <= ED4_G_DRAG);
109        return gc_edit4_to_secedit[edit4_gc];
110    }
111
112    int get_linePropertyGC(int gc1, int gc2) {
113        // of the GCs of two positions, it returns the GC which is
114        // defining the properties for the background painted in-between the two positions
115        sec_assert(gc1 >= SEC_GC_FIRST_DATA && gc1 <= SEC_GC_LAST_DATA);
116        sec_assert(gc2 >= SEC_GC_FIRST_DATA && gc2 <= SEC_GC_LAST_DATA);
117        return line_property_gc[gc1][gc2];
118    }
119};
120
121static PaintData paintData;
122
123// ---------------------
124//      Annotations
125
126void SEC_root::paintAnnotation(AW_device *device, int gc,
127                               const Position& pos, const Position& left, const Position& right,
128                               double noteDistance, const char *text,
129                               bool lineToPos, bool linesToLeftRight, bool boxText)
130{
131    // draw annotation to explicit position 'pos' (annotation is drawn "above" the line left->right)
132    // The distance between pos and note is determined by
133    // * textsize (minimal half textsize/boxsize) and
134    // * the given 'noteDistance'
135    // lineToPos        == true -> draw a line from text to 'pos'
136    // linesToLeftRight == true -> draw lines from text to 'left' and 'right'
137    // boxText          == true -> draw a box around text
138
139    sec_assert(valid_cb_params(device));
140
141    Vector strand(left, right);
142    Angle pos2note(strand);
143    pos2note.rotate270deg();
144
145    int    fontgc        = gc <= SEC_GC_LAST_FONT ? gc : SEC_GC_DEFAULT;
146    double half_charSize = center_char[fontgc].length();
147    size_t text_len      = strlen(text);
148
149    // calculate textsize
150    AW_pos half_width  = 0.5 * device->rtransform_size(device->get_string_size(gc, text, text_len));
151    AW_pos half_height = center_char[fontgc].y();
152
153    double note_distance  = max(half_height, half_width) * (boxText ? 1.3 : 1.0);
154    note_distance         = max(note_distance, noteDistance);
155
156    Position note_center = pos + pos2note.normal()*note_distance;
157
158    if (device->get_filter() & AW_PRINTER) {
159        boxText = false; // don't print/xfig-export boxes
160    }
161
162    if (lineToPos || linesToLeftRight) {
163        device->set_line_attributes(gc, 1, AW_SOLID);
164
165        if (lineToPos) {
166            Vector dist = pos2note.normal()*half_charSize;
167            device->line(gc, boxText ? note_center : note_center-dist, pos+dist);
168        }
169        if (linesToLeftRight) {
170            Vector out(pos, note_center);
171
172            if (out.length()*2 >= strand.length()) { // short strands -> draw simple bracket
173                Vector toLeft(note_center, left);
174                Vector toRight(note_center, right);
175
176                device->line(gc, boxText ? note_center : note_center+toLeft*(half_width/toLeft.length()),
177                             left-toLeft*(half_charSize/toLeft.length()), AW_ALL_DEVICES_SCALED);
178                device->line(gc, boxText ? note_center : note_center+toRight*(half_width/toRight.length()),
179                             right-toRight*(half_charSize/toRight.length()), AW_ALL_DEVICES_SCALED);
180            }
181            else {
182                Vector rightIndent = out;
183                rightIndent.rotate270deg();
184
185                Position rightOut = right+out+rightIndent;
186                Position leftOut  = left+out-rightIndent;
187
188                Vector posPad = Vector(right, rightOut).set_length(half_charSize);
189                device->line(gc, right+posPad, rightOut);
190                posPad.rotate90deg();
191                device->line(gc, left+posPad, leftOut);
192
193                if (boxText) {
194                    device->line(gc, leftOut, rightOut);
195                }
196                else {
197                    Vector rightTextPad(note_center, rightOut);
198                    rightTextPad.set_length(half_width);
199
200                    device->line(gc, note_center+rightTextPad, rightOut);
201                    device->line(gc, note_center-rightTextPad, leftOut);
202                }
203            }
204        }
205    }
206
207    Vector   center_textcorner(-half_width, half_height); // from center to lower left corner
208    Position textcorner = note_center+center_textcorner;
209
210    if (boxText) {
211        Vector    center_corner(-half_width-half_height*0.3, half_height*1.3); // box is 25% bigger than text
212        Rectangle box(note_center+center_corner, -2*center_corner);
213
214        device->clear_part(box, -1);
215        device->box(gc, AW::FillStyle::EMPTY, box);
216    }
217
218    device->text(gc, text, textcorner);
219}
220
221void SEC_root::paintPosAnnotation(AW_device *device, int gc, size_t absPos, const char *text, bool lineToBase, bool boxText) {
222    // draw a annotation next to a base (only works after paint()).
223    // if nothing was drawn at absPos, annotate a position between previous and next drawn position.
224    // text       == NULL    -> draw absPos as number
225    // lineToBase == true    -> draw a line to the base itself
226    // boxText    == true    -> draw a box around text
227
228    size_t          abs1, abs2;
229    const Position& pos1 = drawnPositions->drawn_before(absPos, &abs1);
230    const Position& pos2 = drawnPositions->drawn_after (absPos, &abs2);
231
232    LineVector vec12(pos1, pos2);
233    Position   mid12 = vec12.centroid();
234    Position   pos;
235    {
236        const Position *posDrawn = drawnPositions->drawn_at(absPos);
237        if (posDrawn) { // absPos was drawn
238            pos = *posDrawn;
239        }
240        else { // absPos was not drawn -> use position in-between
241            pos = mid12;
242        }
243    }
244
245    if (!text) text = GBS_global_string("%zu", absPos);
246
247    AW_click_cd cd(device, 0, absPos);
248    paintAnnotation(device, gc, pos, pos1, pos2, vec12.length(), text, lineToBase, false, boxText);
249}
250
251void SEC_root::paintEcoliPositions(AW_device *device) {
252    long abspos = db->ecoli()->rel_2_abs(0);
253    paintPosAnnotation(device, SEC_GC_ECOLI, size_t(abspos), "1", true, true);
254
255    const BI_ecoli_ref *ecoli = db->ecoli();
256    for (size_t ep = bio2info(100); ep < (size_t)ecoli->base_count(); ep += 100) {
257        abspos = ecoli->rel_2_abs(ep);
258        paintPosAnnotation(device, SEC_GC_ECOLI, size_t(abspos), GBS_global_string("%i", info2bio(ep)), true, true);
259    }
260}
261
262void SEC_root::paintHelixNumbers(AW_device *device) {
263    for (SEC_base_iterator elem(this); elem; ++elem) {
264        if (elem->getType() == SEC_HELIX) {
265            SEC_helix& helix = static_cast<SEC_helix&>(*elem);
266
267            // paint helix number of right (3') helix strand
268            SEC_helix_strand *strand = helix.strandToRoot()->is3end() ? helix.strandToRoot() : helix.strandToOutside();
269
270            int         absPos  = strand->startAttachAbspos();
271            const char *helixNr = helixNrAt(absPos);
272
273            if (helixNr) {
274                if (helix.standardSize() == 0) { // helix with zero length (just one position on each strand)
275                    paintPosAnnotation(device, SEC_GC_HELIX_NO,
276                                       strand->startAttachAbspos(), helixNr, true, true);
277                }
278                else {
279                    const Position& start = strand->startAttachPoint();
280                    const Position& end   = strand->endAttachPoint();
281                    Position helixCenter           = centroid(start, end);
282
283                    AW_click_cd cd(device, strand->self(), absPos);
284                    paintAnnotation(device, SEC_GC_HELIX_NO,
285                                    helixCenter, start, end,
286                                    // displayParams.distance_between_strands*2,
287                                    displayParams.distance_between_strands,
288                                    helixNr, false, true, true);
289                }
290            }
291        }
292    }
293}
294
295
296#if defined(PAINT_ABSOLUTE_POSITION)
297void SEC_root::showSomeAbsolutePositions(AW_device *device) {
298    if (device->get_filter() != AW_SIZE) { // ignore for size calculation (@@@)
299        Rectangle screen = device->rtransform(Rectangle(device->get_area_size(), INCLUSIVE_OUTLINE));
300        Vector    diag3  = screen.diagonal()/3;
301        Rectangle showInside(screen.upper_left_corner()+diag3*1.85, diag3);
302
303        AW_click_cd cd(device, 0, -1);
304        device->box(SEC_GC_DEFAULT, AW::FillStyle::EMPTY, showInside);
305
306        PosMap::const_iterator end = drawnPositions->end();
307        for (PosMap::const_iterator pos = drawnPositions->begin(); pos != end; ++pos) {
308            if (showInside.contains(pos->second)) {
309                paintPosAnnotation(device, SEC_GC_DEFAULT, pos->first, NULL, true, true);
310            }
311        }
312    }
313}
314#endif // PAINT_ABSOLUTE_POSITION
315
316void SEC_root::announce_base_position(int base_pos, const Position& draw_pos) {
317    drawnPositions->announce(base_pos, draw_pos);
318}
319void SEC_root::clear_announced_positions() {
320    if (!drawnPositions) drawnPositions = new SEC_drawn_positions;
321    drawnPositions->clear();
322}
323
324void SEC_root::delete_announced_positions() {
325    delete drawnPositions;
326    drawnPositions = 0;
327}
328
329
330// ----------------------------
331//      Paints CONSTRAINTS
332
333void SEC_helix_strand::paint_constraints(AW_device *device) {
334    double minS = helix_info->minSize();
335    double maxS = helix_info->maxSize();
336
337    if (minS>0 || maxS>0) {
338        const Position& startP = startAttachPoint();
339        const Position& endP   = endAttachPoint();
340
341        bool drawMidLine = minS>0 && maxS>0;
342        Position minP = startP + Vector(startP, endP) * (drawMidLine ? minS/maxS : 0.5);
343
344        AW_click_cd cd(device, self(), startAttachAbspos());
345        get_root()->paintAnnotation(device, SEC_GC_DEFAULT,
346                                    minP, startP, endP,
347                                    get_root()->display_params().distance_between_strands*2,
348                                    GBS_global_string("%.1f-%.1f", minS, maxS),
349                                    drawMidLine, true, true);
350    }
351}
352
353void SEC_loop::paint_constraints(AW_device *device) {
354    int abspos = get_fixpoint_strand()->startAttachAbspos();
355
356    double minS = minSize();
357    double maxS = maxSize();
358
359    if (minS>0 || maxS>0) {
360        AW_click_cd cd(device, self(), abspos);
361       
362        if (minS>0) device->circle(SEC_GC_DEFAULT, AW::FillStyle::EMPTY, center, Vector(minS, minS));
363        if (maxS>0) device->circle(SEC_GC_DEFAULT, AW::FillStyle::EMPTY, center, Vector(maxS, maxS));
364
365        device->text(SEC_GC_DEFAULT, GBS_global_string("%.1f-%.1f", minS, maxS), center+Vector(0, max(minS, maxS)/2), 0.5, AW_ALL_DEVICES_UNSCALED);
366    }
367}
368
369// ---------------------------
370//      Background colors
371
372#if defined(WARN_TODO)
373#warning move to SEC_db_interface
374#endif
375void SEC_root::cacheBackgroundColor() {
376    freenull(bg_color);
377
378    int start = 0;
379    int len   = db->length();
380    int end   = len-1;
381
382    bg_color = (char*)malloc(len);
383
384    const char *bg_sai    = displayParams.display_sai    ? host().get_SAI_background(start, end)    : 0;
385    const char *bg_search = displayParams.display_search ? host().get_search_background(start, end) : 0;
386
387    if (bg_sai) {
388        if (bg_search) {
389            for (int i = start; i <= end; ++i) {
390                bg_color[i] = bg_search[i] ? bg_search[i] : bg_sai[i];
391            }
392        }
393        else memcpy(bg_color, bg_sai, len);
394    }
395    else {
396        if (bg_search) memcpy(bg_color, bg_search, len);
397        else memset(bg_color, 0, len);
398    }
399}
400
401void SEC_root::paintBackgroundColor(AW_device *device, SEC_bgpaint_mode mode, const Position& p1, int color1, int gc1, const Position& p2, int color2, int gc2, int skel_gc) {
402    // paints background colors for p2 and connection between p1 and p2.
403    // gc1/gc2 are foreground gc used to detect size of background regions
404    //
405    // Also paints skeleton
406
407    sec_assert(valid_cb_params(device));
408
409    color1 = paintData.convert_BackgroundGC(color1); // convert EDIT4-GCs into SECEDIT-GCs
410    color2 = paintData.convert_BackgroundGC(color2);
411
412    if (color1 >= 0 || color2 >= 0 || displayParams.show_strSkeleton) {
413        const double& radius1 = get_char_radius(gc1);
414        const double& radius2 = get_char_radius(gc2);
415
416        if (mode & BG_PAINT_FIRST && color1 >= 0) { // paint first circle ?
417            device->circle(color1, AW::FillStyle::SOLID, p1, Vector(radius1, radius1));
418        }
419
420        if (mode & BG_PAINT_SECOND && color2 >= 0) { // paint second circle ?
421            device->circle(color2, AW::FillStyle::SOLID, p2, Vector(radius1, radius1));
422        }
423
424        if (color1 == color2 && color1 >= 0) { // colors are equal -> paint background between points
425            device->set_line_attributes(color1, bg_linewidth[paintData.get_linePropertyGC(gc1, gc2)], AW_SOLID);
426            device->line(color1, p1, p2);
427        }
428
429        if (displayParams.show_strSkeleton) { // paint skeleton?
430            Position s1    = p1;
431            Position s2    = p2;
432            bool     space = false;
433
434            if (displayParams.hide_bases) {
435                space = true; // no base chars -> enough space to paint
436            }
437            else {
438                Vector v12(p1, p2);
439                double vlen = v12.length();
440
441                // Note: LINE_THICKNESS
442                //       Lines drawn with thickness != 1 differ between motif-version and gtk-version:
443                //       in motif thicker lines are also drawn longer than specified (half thickness on each side)
444#if defined(ARB_MOTIF)
445                const double CORR = skelThickWorld;
446#else // !defined(ARB_MOTIF)
447                const double CORR = 0.0;
448#endif
449
450                if ((radius1+radius2+CORR) < vlen) { // test if there is enough space between characters
451                    s1 = p1 + v12*((radius1+CORR/2)/vlen); // skeleton<->base attach-points
452                    s2 = p2 - v12*((radius2+CORR/2)/vlen);
453                    space = true;
454                }
455            }
456
457            if (space) {
458                device->set_line_attributes(skel_gc, displayParams.skeleton_thickness, AW_SOLID);
459#if defined(DEBUG)
460                if (displayParams.show_debug) { s1 = p1; s2 = p2; } // in debug mode always show full skeleton
461#endif // DEBUG
462                device->line(skel_gc, s1, s2);
463            }
464        }
465    }
466}
467
468void SEC_root::paintSearchPatternStrings(AW_device *device, int clickedPos, AW_pos xPos,  AW_pos yPos) {
469    int searchColor = getBackgroundColor(clickedPos);
470
471    if (searchColor >= SEC_GC_SBACK_0 && searchColor <= SEC_GC_SBACK_8) {
472        static const char *text[SEC_GC_SBACK_8-SEC_GC_SBACK_0+1] = {
473            "User 1",
474            "User 2",
475            "Probe",
476            "Primer (local)",
477            "Primer (region)",
478            "Primer (global)",
479            "Signature (local)",
480            "Signature (region)",
481            "Signature (global)",
482        };
483
484        AW_click_cd cd(device, 0, clickedPos);
485        device->text(searchColor, text[searchColor-SEC_GC_SBACK_0], xPos, yPos, 0, AW_SCREEN, 0);
486    }
487    else {
488        aw_message("Please click on a search result");
489    }
490}
491
492// ---------------
493//      Bonds
494
495void SEC_bond_def::paint(AW_device *device, char base1, char base2, const Position& p1, const Position& p2, const Vector& toNextBase, const double& char_radius) const {
496    if (base1 && base2) {
497        char Bond = get_bond(base1, base2);
498        if (Bond == ' ') {
499            // check IUPACs
500            const char *iupac1 = iupac::decode(base1, ali_type, 0);
501            const char *iupac2 = iupac::decode(base2, ali_type, 0);
502
503            bool useBond[SEC_BOND_PAIR_CHARS];
504            for (int i = 0; i<SEC_BOND_PAIR_CHARS; i++) useBond[i] = false;
505
506            int maxIdx = -1;
507            for (int i1 = 0; iupac1[i1]; ++i1) {
508                for (int i2 = 0; iupac2[i2]; ++i2) {
509                    char b = get_bond(iupac1[i1], iupac2[i2]);
510
511                    if (b != ' ') {
512                        const char *found = strchr(SEC_BOND_PAIR_CHAR, b);
513                        int         idx       = found-SEC_BOND_PAIR_CHAR;
514
515                        useBond[idx] = true;
516                        if (idx>maxIdx) maxIdx = idx;
517                    }
518                }
519            }
520
521            if (maxIdx >= 0) {
522                for (int i = 0; i<SEC_BOND_PAIR_CHARS; i++) {
523                    if (useBond[i]) {
524                        paint(device, i == maxIdx ? SEC_GC_BONDS : SEC_GC_ERROR, SEC_BOND_PAIR_CHAR[i], p1, p2, toNextBase, char_radius);
525                    }
526                }
527            }
528        }
529        else {
530            paint(device, SEC_GC_BONDS, Bond, p1, p2, toNextBase, char_radius);
531        }
532    }
533}
534
535void SEC_bond_def::paint(AW_device *device, int GC, char bondChar, const Position& p1, const Position& p2, const Vector& toNextBase, const double& char_radius) const {
536    Vector v12(p1, p2);
537    double oppoDist = v12.length();
538    double bondLen  = oppoDist-2*char_radius;
539
540    if (bondLen <= 0.0) return; // not enough space to draw bond
541
542    Vector pb = v12*(char_radius/oppoDist);
543
544    Position b1 = p1+pb; // start/end pos of bond
545    Position b2 = p2-pb;
546
547    Position center = centroid(b1, b2);
548
549    Vector aside = toNextBase;
550    {
551        // limit aside-size by strand-distance
552        double aside_len     = aside.length();
553        double max_aside_len = min(aside_len, Vector(b1, b2).length());
554        if (max_aside_len<aside_len) {
555            aside *= max_aside_len/aside_len;
556        }
557    }
558    aside *= 0.22; // max. 22% towards next base position (has to be less than 25%, because 'aside' is added twice for some bondtypes)
559
560    switch (bondChar) {
561        case '-':               // single line
562            device->line(GC, b1, b2);
563            break;
564
565        case '#':               // double cross
566        case '=':               // double line
567            device->line(GC, b1+aside, b2+aside);
568            device->line(GC, b1-aside, b2-aside);
569
570            if (bondChar == '#') {
571                Vector   outside = v12*(bondLen/oppoDist/4);
572                Position c1      = center+outside;
573                Position c2      = center-outside;
574
575                aside *= 2;
576
577                device->line(GC, c1-aside, c1+aside);
578                device->line(GC, c2-aside, c2+aside);
579            }
580            break;
581
582        case '~': {
583            double radius = aside.length();
584            {
585                double maxRadius = bondLen/4;
586                if (maxRadius<radius) radius = maxRadius;
587            }
588
589            Vector outside = v12*(radius/oppoDist);
590
591            Position c1 = center+outside;
592            Position c2 = center-outside;
593
594            aside *= 2;
595
596            Angle angle(outside);
597            int   deg = AW_INT(angle.degrees());
598
599            const int INSIDE  = 2;
600            const int OUTSIDE = 15;
601
602            Vector vRadius(radius, radius);
603            device->arc(GC, AW::FillStyle::EMPTY, c1, vRadius, deg+180+INSIDE, -(180+INSIDE+OUTSIDE));
604            device->arc(GC, AW::FillStyle::EMPTY, c2, vRadius, deg+INSIDE,     -(180+INSIDE+OUTSIDE));
605            break;
606        }
607
608        case '+':               // cross
609            aside *= 2;
610            device->line(GC, center-aside, center+aside);
611            if (2*aside.length() < bondLen) {
612                aside.rotate90deg();
613                device->line(GC, center-aside, center+aside);
614            }
615            else {
616                device->line(GC, b1, b2);
617            }
618            break;
619
620        case 'o':
621        case '.': {             // circles
622            double radius            = aside.length();
623            if (bondChar == 'o') radius *= 2;
624            device->circle(GC, AW::FillStyle::EMPTY, center, Vector(radius, radius));
625            break;
626        }
627
628        case '@':  // error in bonddef
629            device->text(GC, "Err", center+Vector(0, char_radius), 0.5, AW_ALL_DEVICES_UNSCALED);
630            break;
631
632        default:
633            sec_assert(0); // // illegal bond char
634            break;
635    }
636}
637
638// -----------------------
639//      Paint helices
640
641struct StrandPositionData {
642    int      abs[2];            // absolute sequence position
643    int      previous[2];       // previous drawn index
644    bool     drawn[2];          // draw position ?
645    bool     isPair;            // true if position is pairing
646    Position realpos[2];        // real position
647};
648
649void SEC_helix_strand::paint_strands(AW_device *device, const Vector& strand_vec, const double& strand_len) {
650    static StrandPositionData *data      = 0;
651    static int                 allocated = 0;
652
653    const SEC_region* Region[2] = { get_region(), other_strand->get_region() };
654    int base_count = Region[0]->get_base_count();
655
656    sec_assert(Region[1]->get_base_count() == base_count); // not aligned ?
657
658    if (base_count<1) {
659        return; // completely skip painting on strands w/o any base
660    }
661
662    if (allocated<base_count) {
663        delete [] data;
664        data      = new StrandPositionData[base_count];
665        allocated = base_count;
666    }
667
668    SEC_root       *root  = get_root();
669    const BI_helix *helix = root->get_helixDef();
670
671    double base_dist = base_count>1 ? strand_len / (base_count-1) : 1;
672    Vector vnext     = strand_vec * base_dist; // vector from base to next base (in strand)
673
674    // first calculate positions
675    {
676        StrandPositionData *curr = &data[0];
677
678        int idx[2] = { 0, base_count-1 };
679        Position pos[2] = { leftAttach, rightAttach };
680        Vector toNonBind[2]; // vectors from normal to non-binding positions
681        toNonBind[1] = (strand_vec*0.5).rotate90deg();
682        toNonBind[0] = -toNonBind[1];
683
684        for (int strand = 0; strand<2; ++strand) {
685            curr->abs[strand]      = Region[strand]->getBasePos(idx[strand]);
686            curr->previous[strand] = 0;
687            curr->drawn[strand]    = (curr->abs[strand] >= 0);
688        }
689
690        for (int dIdx = 1; ; ++dIdx) {
691            sec_assert(pos[0].valid());
692            sec_assert(pos[1].valid());
693
694            int oneAbs  = curr->drawn[0] ? curr->abs[0] : curr->abs[1];
695            sec_assert(oneAbs >= 0); // otherwise current position should have been eliminated by align_helix_strands
696            curr->isPair = (helix->pairtype(oneAbs) != HELIX_NONE);
697
698            for (int strand = 0; strand<2; ++strand) {
699                if (curr->isPair) {
700                    curr->realpos[strand] = pos[strand];
701                    curr->drawn[strand]   = true;
702                }
703                else {
704                    curr->realpos[strand] = pos[strand]+toNonBind[strand];
705                }
706
707                sec_assert(curr->realpos[strand].valid());
708            }
709
710            if (dIdx >= base_count) break;
711
712            ++idx[0];
713            --idx[1];
714
715            StrandPositionData *prev = curr;
716            curr                     = &data[dIdx];
717
718            for (int strand = 0; strand<2; ++strand) {
719                pos[strand]           += vnext;
720                curr->abs[strand]       = Region[strand]->getBasePos(idx[strand]);
721                curr->previous[strand]  = prev->drawn[strand] ? dIdx-1 : prev->previous[strand];
722                curr->drawn[strand]     = (curr->abs[strand] >= 0);
723            }
724        }
725    }
726
727    const int pair2helixGC[2] = { SEC_GC_NHELIX, SEC_GC_HELIX };
728    const int pair2skelGC[2] = { SEC_SKELE_NHELIX, SEC_SKELE_HELIX };
729
730    const SEC_db_interface   *db   = root->get_db();
731    const SEC_displayParams&  disp = root->display_params();
732
733    // draw background and skeleton
734    for (int pos = 1; pos<base_count; ++pos) {
735        StrandPositionData *curr = &data[pos];
736        for (int strand = 0; strand<2; ++strand) {
737            if (curr->drawn[strand]) {
738                StrandPositionData *prev = &data[curr->previous[strand]];
739
740                int backAbs = disp.edit_rightward
741                    ? max(prev->abs[strand], curr->abs[strand])
742                    : min(prev->abs[strand], curr->abs[strand]);
743
744                AW_click_cd cd(device, self(), backAbs);
745                root->paintBackgroundColor(device,
746                                           pos == base_count-1 ? BG_PAINT_NONE : BG_PAINT_SECOND,
747                                           prev->realpos[strand], root->getBackgroundColor(prev->abs[strand]), pair2helixGC[prev->isPair],
748                                           curr->realpos[strand], root->getBackgroundColor(curr->abs[strand]), pair2helixGC[curr->isPair],
749                                           pair2skelGC[curr->isPair && prev->isPair]);
750            }
751        }
752    }
753
754    // draw base characters and bonds
755    char baseBuf[20] = "x";
756    for (int pos = 0; pos<base_count; ++pos) {
757        StrandPositionData *curr = &data[pos];
758        char base[2] = { 0, 0 };
759
760        int    gc          = pair2helixGC[curr->isPair];
761        Vector center_char = root->get_center_char_vector(gc);
762
763        for (int strand = 0; strand<2; ++strand) {
764            if (curr->drawn[strand]) {
765                int             abs     = curr->abs[strand];
766                const Position& realPos = curr->realpos[strand];
767
768                sec_assert(abs >= 0);
769
770                base[strand] = db->baseAt(abs);
771                root->announce_base_position(abs, realPos);
772
773                if (!disp.hide_bases) {
774                    baseBuf[0]        = base[strand];
775                    Position base_pos = realPos + center_char; // center base at realpos
776                    AW_click_cd cd(device, self(), abs);
777#if defined(DEBUG)
778                    if (disp.show_debug) device->line(gc, realPos, base_pos);
779#endif // DEBUG
780
781                    device->text(gc, baseBuf, base_pos, 0.0, AW_ALL_DEVICES_SCALED);
782                }
783            }
784        }
785
786        if (disp.show_bonds == SHOW_NHELIX_BONDS || (disp.show_bonds == SHOW_HELIX_BONDS && curr->isPair)) {
787            AW_click_cd cd(device, self(), curr->abs[0]);
788            db->bonds()->paint(device, base[0], base[1], curr->realpos[0], curr->realpos[1], vnext,
789                               root->get_char_radius(pair2helixGC[curr->isPair])
790#if defined(ARB_MOTIF)
791                               +root->get_bondThickWorld()/2 // see .@LINE_THICKNESS
792#endif
793                               );
794        }
795    }
796}
797
798void SEC_helix_strand::paint(AW_device *device) {
799    sec_assert(isRootsideFixpoint());
800
801    Vector strand_vec(rightAttach, other_strand->leftAttach);
802    double strand_len     = strand_vec.length(); // length of strand
803
804    if (strand_len>0) {
805        strand_vec.normalize();     // normalize
806    }
807    else { // strand with zero length (contains only one base-pair)
808        strand_vec = Vector(rightAttach, leftAttach).rotate90deg();
809    }
810
811    other_strand->origin_loop->paint(device); // first paint next loop
812    paint_strands(device, strand_vec, strand_len); // then paint strand
813
814    SEC_root                 *root = get_root();
815    const SEC_displayParams&  disp = root->display_params();
816
817    if (disp.show_strSkeleton && !disp.show_bonds && disp.hide_bases) {
818        // display strand direction
819        LineVector strandArrow;
820        if (strand_len>0) {
821            strandArrow = LineVector(get_fixpoint(), strand_vec);
822        }
823        else {
824            Vector fix2arrowStart(get_fixpoint(), leftAttachPoint());
825            fix2arrowStart.rotate90deg();
826            strandArrow = LineVector(get_fixpoint()-fix2arrowStart, 2*fix2arrowStart);
827        }
828
829        AW_click_cd cd(device, get_helix()->self(), startAttachAbspos());
830        device->line(SEC_GC_HELIX, strandArrow);
831
832        Vector right = strandArrow.line_vector(); // left arrowhead vector
833        right        = (right * (disp.distance_between_strands*0.35/right.length())).rotate135deg();
834
835        Vector left = Vector(right).rotate90deg();
836
837        Position head = strandArrow.head();
838        device->line(SEC_GC_HELIX, LineVector(head, left));
839        device->line(SEC_GC_HELIX, LineVector(head, right));
840    }
841
842#if defined(DEBUG)
843    if (disp.show_debug) paintStrandDebugInfo(device, SEC_GC_HELIX, other_strand);
844#endif // DEBUG
845
846    if (root->get_show_constraints() & SEC_HELIX) paint_constraints(device);
847}
848
849
850// ---------------------
851//      Paint loops
852
853void SEC_segment::paint(AW_device *device, SEC_helix_strand *previous_strand_pointer) {
854    int base_count = get_region()->get_base_count(); // bases in segment
855
856    const Position& startP = previous_strand_pointer->rightAttachPoint();
857    const Position& endP   = next_helix_strand->leftAttachPoint();
858
859    Angle  current;             // start/current angle
860    Angle  end;                 // end angle
861    double radius1;             // start and..
862    double radius2;             // end radius of segment
863
864    {
865        Vector seg_start_radius(center1, startP);
866        radius1  = seg_start_radius.length();
867        current = seg_start_radius;
868
869        Vector seg_end_radius(center2, endP);
870        radius2  = seg_end_radius.length();
871        end = seg_end_radius;
872    }
873
874    int steps = base_count+1;
875
876    double step = ((end-current)/steps).radian();
877
878    // correct if we have to paint more than a full loop
879    if ((alpha - (step*steps)) > M_PI) {
880        step += (2*M_PI)/steps;
881    }
882
883    double radStep = (radius2-radius1)/steps;
884
885    Vector cstep(center1, center2);
886    cstep /= steps;
887
888    SEC_root                 *root = get_root();
889    const SEC_db_interface   *db   = root->get_db();
890    const SEC_displayParams&  disp = root->display_params();
891#if defined(DEBUG)
892    if (disp.show_debug) {
893        paintStrandDebugInfo(device, SEC_GC_LOOP, previous_strand_pointer);
894
895        int startAbsPos = previous_strand_pointer->rightAttachAbspos();
896        int endAbsPos   = next_helix_strand->leftAttachAbspos();
897
898        AW_click_cd cd(device, self(), startAbsPos);
899        paintDebugInfo(device, SEC_GC_LOOP, center1, GBS_global_string("SC1 (step=%5.3f)", step));
900        device->line(SEC_GC_LOOP, center1, startP);
901        device->line(SEC_GC_LOOP, center1, center2);
902
903        cd.set_cd2(endAbsPos);
904        paintDebugInfo(device, SEC_GC_LOOP, center2, "SC2");
905        device->line(SEC_GC_LOOP, center2, endP);
906    }
907#endif // DEBUG
908
909    char baseBuf[5]  = "?";      // contains base char during print
910    Position pos    = startP;
911    int      abs    = previous_strand_pointer->rightAttachAbspos();
912    int      back   = root->getBackgroundColor(abs);
913    int      gc     = root->getBondtype(abs) == HELIX_NONE ? SEC_GC_NHELIX : SEC_GC_HELIX;
914    int      nextGc = SEC_GC_LOOP;
915
916    Position currCenter = center1;
917    double   currRadius = radius1;
918
919    Angle step_angle(step);
920   
921    for (int i = -1; i<base_count; i++) { // for each segment position (plus one pre-loop)
922        current    += step_angle;     // iterate over angles
923        currCenter += cstep;
924        currRadius += radStep;
925
926        Position nextPos = currCenter + current.normal()*currRadius;
927        int      nextAbs;
928
929        if (i == (base_count-1)) { // last position (belongs to strand)
930            nextAbs    = next_helix_strand->leftAttachAbspos();
931            if (nextAbs<0) { // helix doesn't start with pair
932                nextAbs = next_helix_strand->getNextAbspos();
933            }
934            nextGc = root->getBondtype(nextAbs) == HELIX_NONE ? SEC_GC_NHELIX : SEC_GC_HELIX;
935        }
936        else {
937            nextAbs = get_region()->getBasePos(i+1);
938        }
939
940        int nextBack = root->getBackgroundColor(nextAbs);
941
942        // paint background (from pos to nextPos)
943        AW_click_cd cd(device, self(), disp.edit_rightward ? nextAbs : abs);
944        root->paintBackgroundColor(device, i == -1 ? BG_PAINT_BOTH : BG_PAINT_SECOND,
945                                   pos, back, gc, nextPos, nextBack, nextGc, SEC_SKELE_LOOP);
946
947        if (i >= 0) {
948            // paint base char at pos
949            baseBuf[0]           = abs>0 ? db->baseAt(abs) : '?';
950            Vector   center_char = root->get_center_char_vector(gc);
951            Position base_pos    = pos + center_char; // center base character at pos
952
953            cd.set_cd2(abs);
954            if (!disp.hide_bases) {
955#if defined(DEBUG)
956                // show line from base paint pos to calculated center of char
957                // (which is currently calculated wrong!)
958                if (disp.show_debug) device->line(SEC_GC_LOOP, pos, base_pos);
959#endif // DEBUG
960                device->text(SEC_GC_LOOP, baseBuf, base_pos, 0.0, AW_ALL_DEVICES_SCALED);
961            }
962            root->announce_base_position(abs, pos);
963        }
964
965        // prepare next loop
966        pos  = nextPos;
967        abs  = nextAbs;
968        back = nextBack;
969        gc   = nextGc;
970    }
971}
972
973void SEC_loop::paint(AW_device *device) {
974    for (SEC_segment_iterator seg(this); seg; ++seg) { // first paint all segments
975        seg->paint(device, seg->get_previous_strand());
976    }
977    for (SEC_strand_iterator strand(this); strand; ++strand) { // then paint all outgoing strands
978        if (strand->isRootsideFixpoint()) strand->paint(device);
979    }
980
981    SEC_root *sroot = get_root();
982#if defined(DEBUG)
983    if (sroot->display_params().show_debug) {
984        SEC_helix_strand *fixpoint_strand = get_fixpoint_strand();
985        int               abspos          = fixpoint_strand->startAttachAbspos();
986        AW_click_cd cd(device, self(), abspos);
987
988        device->set_line_attributes(SEC_GC_CURSOR, 1, AW_SOLID);
989        device->line(SEC_GC_CURSOR, get_center(), fixpoint_strand->get_fixpoint());
990
991        paintStrandDebugInfo(device, SEC_GC_CURSOR, fixpoint_strand);
992        paintDebugInfo(device, SEC_GC_CURSOR, get_center(), "LC");
993    }
994#endif // DEBUG
995    if (sroot->get_show_constraints() & SEC_LOOP) paint_constraints(device);
996}
997
998// ------------------------------------------------------------
999//      Paint the whole structure (starting with SEC_root)
1000
1001GB_ERROR SEC_root::paint(AW_device *device) {
1002    SEC_loop *rootLoop = get_root_loop();
1003    sec_assert(rootLoop);
1004    clear_announced_positions(); // reset positions next to cursor
1005
1006    const BI_helix *helix = get_helixDef();
1007    sec_assert(helix);
1008
1009    GB_ERROR error = helix->get_error();
1010
1011    if (!error) {
1012        sec_assert(SEC_GC_FIRST_FONT == 0);
1013        font_group.unregisterAll();
1014        for (int gc = SEC_GC_FIRST_FONT; gc <= SEC_GC_LAST_FONT; ++gc) {
1015            font_group.registerFont(device, gc, "ACGTU-.");
1016            center_char[gc] = device->rtransform(Vector(-0.5*font_group.get_width(gc), 0.5*font_group.get_ascent(gc)));
1017        }
1018
1019        // calculate size for background painting
1020        sec_assert(SEC_GC_FIRST_DATA == 0);
1021        for (int gc = SEC_GC_FIRST_DATA; gc <= SEC_GC_LAST_DATA; ++gc) {
1022            int maxSize = hypotenuse(font_group.get_width(gc), font_group.get_ascent(gc));
1023            bg_linewidth[gc] = maxSize*0.75;
1024
1025            maxSize        += 2;                                        // add 2 extra pixels
1026            charRadius[gc]  = device->rtransform_size(maxSize) * 0.5;   // was 0.75
1027        }
1028
1029#if defined(ARB_MOTIF)
1030        skelThickWorld = device->rtransform_size(displayParams.skeleton_thickness);
1031        bondThickWorld = device->rtransform_size(displayParams.bond_thickness);
1032#endif
1033
1034        cacheBackgroundColor();
1035
1036        device->set_line_attributes(SEC_SKELE_HELIX,  displayParams.skeleton_thickness, AW_SOLID);
1037        device->set_line_attributes(SEC_SKELE_NHELIX, displayParams.skeleton_thickness, AW_SOLID);
1038        device->set_line_attributes(SEC_SKELE_LOOP,   displayParams.skeleton_thickness, AW_SOLID);
1039        device->set_line_attributes(SEC_GC_BONDS,     displayParams.bond_thickness,     AW_SOLID);
1040
1041        // mark the rootLoop with a box and print structure number
1042        {
1043            const Position&  loop_center = rootLoop->get_center();
1044            const char      *structId    = db->structure()->name();
1045
1046            AW_click_cd cd(device, rootLoop->self(), -1);
1047
1048            Vector center2corner(-1, -1);
1049            center2corner.set_length(rootLoop->drawnSize()*0.33);
1050
1051            Position upperleft_corner = loop_center+center2corner;
1052            Vector   diagonal         = -2*center2corner;
1053
1054            Position textPos(loop_center.xpos(), upperleft_corner.ypos());
1055
1056            device->box(SEC_GC_DEFAULT, AW::FillStyle::EMPTY, upperleft_corner, diagonal, AW_ALL_DEVICES_UNSCALED);
1057            device->text(SEC_GC_DEFAULT, structId, textPos, 0.5, AW_ALL_DEVICES_UNSCALED, 0);
1058        }
1059
1060#if defined(CHECK_INTEGRITY)
1061        check_integrity(CHECK_ALL);
1062#endif // CHECK_INTEGRITY
1063
1064        rootLoop->paint(device);
1065
1066        // paint ecoli positions:
1067        if (displayParams.show_ecoli_pos) paintEcoliPositions(device);
1068
1069        if (displayParams.show_helixNrs) {
1070            paintHelixNumbers(device);
1071        }
1072
1073#if defined(PAINT_ABSOLUTE_POSITION)
1074        if (displayParams.show_debug) showSomeAbsolutePositions(device);
1075#endif // PAINT_ABSOLUTE_POSITION
1076
1077        // paint cursor:
1078        if (!drawnPositions->empty() &&
1079            (device->get_filter()&(AW_PRINTER|AW_PRINTER_EXT)) == 0) // don't print/xfig-export cursor
1080        {
1081            size_t   abs1, abs2;
1082            Position pos1, pos2;
1083            size_t curAbs;
1084
1085            if (displayParams.edit_rightward) {
1086                pos1   = drawnPositions->drawn_before(cursorAbsPos, &abs1);
1087                pos2   = drawnPositions->drawn_after(cursorAbsPos-1, &abs2);
1088                curAbs = abs2;
1089            }
1090            else {
1091                pos1   = drawnPositions->drawn_before(cursorAbsPos+1, &abs1);
1092                pos2   = drawnPositions->drawn_after(cursorAbsPos, &abs2);
1093                curAbs = abs1;
1094            }
1095
1096            AW_click_cd cd(device, 0, curAbs);
1097#if defined(DEBUG) && 1
1098            // draw a testline to see the baseline on that the cursor is positioned
1099            device->set_line_attributes(SEC_GC_CURSOR, 1, AW_DASHED);
1100            device->line(SEC_GC_CURSOR, pos1, pos2);
1101#endif
1102
1103            Position mid = centroid(pos1, pos2);
1104            Vector   v(pos1, pos2);
1105            {
1106                Vector v_drawn      = device->transform(v);
1107                double drawn_length = v_drawn.length();
1108
1109                sec_assert(drawn_length>0.0);
1110
1111                double cursor_size = 1.3 * max(font_group.get_max_width(), font_group.get_max_ascent()); // 30% bigger than max font size
1112                double stretch     = cursor_size*0.5/drawn_length; // stretch cursor (half fontsize in each direction)
1113
1114                v.rotate90deg() *= stretch;
1115            }
1116
1117            LineVector cursor(mid+v, mid-v);
1118            device->set_line_attributes(SEC_GC_CURSOR, 3, AW_SOLID);
1119            device->line(SEC_GC_CURSOR, cursor);
1120            set_last_drawed_cursor_position(cursor);
1121
1122            LineVector cursor_dir(cursor.head(), displayParams.edit_rightward ? v.rotate270deg() : v.rotate90deg());
1123            device->line(SEC_GC_CURSOR, cursor_dir);
1124
1125
1126            int cursor_gc  = -1;
1127            int disp_pos = -1;
1128
1129            switch (displayParams.show_curpos) {
1130                case SHOW_ABS_CURPOS:
1131                    cursor_gc = SEC_GC_CURSOR;
1132                    disp_pos  = info2bio(curAbs);
1133                    break;
1134                case SHOW_BASE_CURPOS:
1135                    cursor_gc = SEC_GC_DEFAULT;
1136                    disp_pos  = host().get_base_position(curAbs+1); // show bases up to cursorpos (inclusive)
1137                    break;
1138                case SHOW_ECOLI_CURPOS: {
1139                    cursor_gc = SEC_GC_ECOLI;
1140                    disp_pos  = db->ecoli()->abs_2_rel(curAbs+1); // show ecoli base position (inclusive cursorpos)
1141                    break;
1142                }
1143                case SHOW_NO_CURPOS:
1144                    cursor_gc = -1;
1145                    break;
1146            }
1147
1148            if (cursor_gc >= 0) {
1149                paintPosAnnotation(device, cursor_gc, curAbs, GBS_global_string("%u", disp_pos), true, true);
1150            }
1151        }
1152    }
1153    return error;
1154}
1155
1156void SEC_region::align_helix_strands(SEC_root *root, SEC_region *other_region) {
1157    if (abspos_array) {
1158        const BI_helix *helix = root->get_helixDef();
1159        if (helix && !helix->get_error()) {
1160            SEC_region *reg[2] = { this, other_region };
1161            int incr[2] = { 1, -1 }; // this is iterated forward, other_region backward
1162            int *absarr[2];
1163            int *new_absarr[2] = { 0, 0 };
1164
1165
1166            for (int r = 0; r<2; ++r) {
1167                absarr[r] = reg[r]->abspos_array;
1168            }
1169
1170            for (int write = 0; write < 2; ++write) {
1171                int curr[2] = { 0, reg[1]->baseCount-1 };
1172                int last[2] = { reg[0]->baseCount-1, 0 };
1173                int newp[2] = { 0, 0 };
1174
1175                while (curr[0] <= last[0] && curr[1] >= last[1]) {
1176                    int  abs[2];
1177                    bool ispair[2];
1178
1179                    for (int r = 0; r<2; ++r) {
1180                        abs[r]    = absarr[r][curr[r]];
1181                        ispair[r] = abs[r] >= 0 && (helix->pairtype(abs[r]) != HELIX_NONE);
1182                    }
1183
1184                    if (ispair[0] && ispair[1]) {
1185                        if (helix->opposite_position(abs[0]) != size_t(abs[1]) ||
1186                            helix->opposite_position(abs[1]) != size_t(abs[0]))
1187                        {
1188                            GB_ERROR error = GBS_global_string("Helix '%s/%s' folded at wrong position. Please refold.",
1189                                                               helix->helixNr(abs[0]), helix->helixNr(abs[1]));
1190                            aw_message(error);
1191                        }
1192
1193                        for (int r = 0; r<2; ++r) { // fill up to align binding positions
1194                            while (newp[r]<newp[1-r]) {
1195                                if (write) {
1196                                    new_absarr[r][newp[r]] = -1;
1197                                }
1198                                newp[r]++;
1199                            }
1200                        }
1201
1202                        sec_assert(newp[0] == newp[1]);
1203
1204                        for (int r = 0; r<2; ++r) { // copy binding positions
1205                            if (write) new_absarr[r][newp[r]] = abs[r];
1206                            newp[r]++; curr[r] += incr[r];
1207                        }
1208                    }
1209                    else {
1210                        bool collected = false;
1211                        for (int r = 0; r<2; ++r) {
1212                            if (abs[r] >= 0 && !ispair[r]) { // collect non-pairing bases
1213                                if (write) {
1214                                    new_absarr[r][newp[r]] = abs[r];
1215                                }
1216                                newp[r]++; curr[r] += incr[r];
1217                                collected           = true;
1218                            }
1219                        }
1220                        if (!collected) {
1221                            for (int r = 0; r<2; ++r) {
1222                                if (abs[r]<0) curr[r] += incr[r];
1223                            }
1224                        }
1225                    }
1226                }
1227
1228                sec_assert(newp[0] == newp[1]); // alignment failed
1229
1230                for (int r = 0; r<2; ++r) {
1231                    if (write) {
1232                        if (r == 1) { // reverse positions
1233                            int  p2  = newp[1]-1;
1234                            int *arr = new_absarr[1];
1235                            for (int p = 0; p<p2; ++p, --p2) {
1236                                swap(arr[p], arr[p2]);
1237                            }
1238                        }
1239
1240                        delete [] reg[r]->abspos_array;
1241                        reg[r]->abspos_array      = new_absarr[r];
1242#if defined(ASSERTION_USED)
1243                        reg[r]->abspos_array_size = newp[r];
1244#endif // ASSERTION_USED
1245                        reg[r]->set_base_count(newp[r]);
1246                    }
1247                    else {
1248                        // allocate buffers for second pass
1249                        new_absarr[r] = new int[newp[r]];
1250                    }
1251                }
1252            }
1253        }
1254    }
1255}
1256
Note: See TracBrowser for help on using the repository browser.