File: navigation.cpp

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (286 lines) | stat: -rw-r--r-- 8,698 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
 * @file: navigation.cpp 
 * Implemetation of navigation helper classes
 * @ingroup GUIGraph
 */
/*
 * GUI for ShowGraph tool.
 * Copyright (c) 2009, Boris Shurygin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "gview_impl.h"

/** Constructor */
NodeNav::NodeNav( GNode *curr_node, NavSector nav_sector):
    node_priv( curr_node),
    sector_priv( nav_sector)
{

}
    
/** Get edge to the bottom of current node */
GEdge *
NodeNav::firstEdgeInSector() const
{
    GEdge *res = NULL;
    qreal min_delta = 0;

    /** For each edge */   
    for ( Node::EdgeIter e_iter = node()->edgesBegin(),
                         e_end = node()->edgesEnd();
          e_iter != e_end;
          e_iter++ )
    {
        GEdge *e = static_cast<GEdge *>( e_iter.edge());
        if ( isEdgeInSector( e))
        {
            NodeItem *p_item = static_cast<GNode *>( e_iter.node())->item();
            QPointF center = node()->item()->boundingRect().center();
            QPointF point = node()->item()->mapFromItem( p_item, p_item->boundingRect().center());
            
            qreal delta;
            if ( sector() == TOP_SECTOR || sector() == BOTTOM_SECTOR)
            {
                delta = abs<qreal>( center.x() - point.x());
            } else
            {
                delta = abs<qreal>( center.y() - point.y());
            }

            if ( isNullP( res) 
                 || delta < min_delta) // for selection of closest edge
            {
                res = e;
                min_delta = delta;
            }
        }
    }
    return res;
}

/** Checks if navigation direction is applicable in current sector */
bool NodeNav::isDirApplicable( NavDirection dir, NavSector s)
{
    /* Applicable only for top and bottom sectors */
    if ( s == UNDEF_SECTOR)
        return false;

    if ( s == LEFT_SECTOR
         || s == RIGHT_SECTOR)
    {
        return dir == NAV_DIR_UP
               || dir == NAV_DIR_DOWN;
    } else if ( s == TOP_SECTOR
                || s == BOTTOM_SECTOR)
    {
        return dir == NAV_DIR_LEFT
               || dir == NAV_DIR_RIGHT;
    }
    GVIEW_ASSERTD( 0, "Invalid sector type");
    return false;
}

/**
 * Check that given point is in given direction from reference point
 */
bool NodeNav::isPointInDir( QPointF point, QPointF ref, NavDirection dir)
{
    switch ( dir)
    {
    case NAV_DIR_UP: 
        return point.y() <= ref.y();
    case NAV_DIR_DOWN: 
        return point.y() >= ref.y();
    case NAV_DIR_LEFT:
        return point.x() <= ref.x();
    case NAV_DIR_RIGHT:
        return point.x() >= ref.x();
    default:
        return false;
    }
    //return false;
}

/**
 * Compute coordinate difference in given direction 
 * between given point and reference point
 */
qreal NodeNav::deltaInDir( QPointF point, QPointF ref, NavDirection dir)
{
    switch ( dir)
    {
    case NAV_DIR_UP: 
       return ref.y() - point.y();
    case NAV_DIR_DOWN: 
        return point.y() - ref.y();
    case NAV_DIR_LEFT: 
        return ref.x() - point.x();
    case NAV_DIR_RIGHT:
        return point.x() - ref.x();
    default:
        return 0;
    }
    //return 0;
}
/** Get edge to the left of given edge */
GEdge *
NodeNav::edgeInDir( GEdge * edge, NavDirection dir) const
{
    /* Applicable only for top and bottom sectors */
    if ( !isDirApplicable( dir, sector()))
    {
        return NULL;
    }
    
    GNode *n = otherEnd( edge);
    /* item corresponding to other (than node_priv) node */
    if ( isNullP( n))
        return NULL;

    NodeItem *item = n->item();
    
    // in current node's coordinates
    QPointF edge_point = node()->item()->mapFromItem( item, item->boundingRect().center());

    GEdge *res = NULL;
    qreal min_delta = 0;
    /** For each edge */   
    for ( Node::EdgeIter e_iter = node()->edgesBegin(),
                         e_end = node()->edgesEnd();
          e_iter != e_end;
          e_iter++ )
    {
        GEdge *e = static_cast<GEdge *>( e_iter.edge());
        if ( isEdgeInSector( e) && areNotEqP( e, edge))
        {
            NodeItem *p_item = static_cast<GNode *>( e_iter.node())->item();
            QPointF point = node()->item()->mapFromItem( p_item, p_item->boundingRect().center());
            if ( isPointInDir( point, edge_point, dir))
            {
                qreal delta = deltaInDir( point, edge_point, dir);
                if ( isNullP( res) 
                     || delta < min_delta) // for selection of closest edge
                {
                    res = e;
                    min_delta = delta;
                }
            }
        }
    }

    return res;
}

/* Other edge's node */
GNode *
NodeNav::otherEnd( GEdge *edge) const
{
    if ( areEqP( node_priv, edge->pred()))
    {
        return edge->succ();
    } else if ( areEqP( node_priv, edge->succ()))
    {
        return edge->pred();
    }
    return NULL;
}

/** Check that given edge is in current sector */
bool NodeNav::isEdgeInSector( GEdge * edge) const
{
    GNode *n = otherEnd( edge);
    /* item corresponding to other (than node_priv) node */
    if ( isNullP( n) || sector() == UNDEF_SECTOR)
        return false;

    NodeItem *item = n->item();
    
    // Both points in current node's coordinates
    QPointF p_center = node_priv->item()->boundingRect().center();
    QPointF s_center = node_priv->item()->mapFromItem( item, item->boundingRect().center());

    qreal angle = QLineF( p_center, s_center).angle();
    
    qreal max_angle = sectorMaxAngle();
    qreal min_angle = sectorMinAngle();

    if ( angle > 270 && sector() == RIGHT_SECTOR)
    {
        max_angle +=360;
    }
    if ( angle < 90 && sector() == RIGHT_SECTOR)
    {
        min_angle -=360;
    }

    if ( angle <= max_angle && angle >= min_angle)
        return true;
    return false;
}

/** Compute max angle for sector */
qreal NodeNav::sectorMaxAngle() const
{
    QLineF line;
    QRectF rect = node_priv->item()->boundingRect();
    QPointF center = rect.center();
    
    switch ( sector_priv)
    {
        case TOP_SECTOR:
            line = QLineF( center, rect.topLeft());
            break;
        case BOTTOM_SECTOR:
            line = QLineF( center, rect.bottomRight());
            break;
        case LEFT_SECTOR:
            line = QLineF( center, rect.bottomLeft());
            break;
        case RIGHT_SECTOR:
            line = QLineF( center, rect.topRight());
            break;
        case UNDEF_SECTOR:
        default:
            return 0;
    }
    return line.angle();
}

/** Compute min angle for sector */
qreal NodeNav::sectorMinAngle() const
{
    QLineF line;
    QRectF rect = node_priv->item()->boundingRect();
    QPointF center = rect.center();
    
    switch ( sector_priv)
    {
        case TOP_SECTOR:
            line = QLineF( center, rect.topRight());
            break;
        case BOTTOM_SECTOR:
            line = QLineF( center, rect.bottomLeft());
            break;
        case LEFT_SECTOR:
            line = QLineF( center, rect.topLeft());
            break;
        case RIGHT_SECTOR:
            line = QLineF( center, rect.bottomRight());
            break;
        case UNDEF_SECTOR:
        default:
            return 0;
    }
    return line.angle();
}