~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to cmd/smyrna/selection.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: selection.c,v 1.3 2008/03/03 23:03:38 arif Exp $ $Revision: 1.3 $ */
 
2
/* vim:set shiftwidth=4 ts=8: */
 
3
 
 
4
/**********************************************************
 
5
*      This software is part of the graphviz package      *
 
6
*                http://www.graphviz.org/                 *
 
7
*                                                         *
 
8
*            Copyright (c) 1994-2004 AT&T Corp.           *
 
9
*                and is licensed under the                *
 
10
*            Common Public License, Version 1.0           *
 
11
*                      by AT&T Corp.                      *
 
12
*                                                         *
 
13
*        Information and Software Systems Research        *
 
14
*              AT&T Research, Florham Park NJ             *
 
15
**********************************************************/
 
16
 
 
17
#include "selection.h"
 
18
#include "viewport.h"
 
19
int rectintersects(float x,float y,float W,float H)
 
20
{
 
21
        //returns 1 if rect is completely in the clip rect
 
22
        //0 if they intersect
 
23
        //-1 if completely out
 
24
 
 
25
        int s[4];
 
26
        s[0]=( ((x >=view->Selection.X)&& (x <= (view->Selection.X + view->Selection.W) ))) ? 1:0;
 
27
        s[1]=( (((x+W) >=view->Selection.X)&& ((x+W) <= (view->Selection.X + view->Selection.W) ))) ? 1:0;
 
28
 
 
29
        s[2]=( ((y >=view->Selection.Y)&& (y <= (view->Selection.Y + view->Selection.H) ))) ? 1:0;
 
30
        s[3]=( (((y+H) >=view->Selection.Y)&& ((y+H) <= (view->Selection.Y + view->Selection.H) ))) ? 1:0;
 
31
 
 
32
        
 
33
        if (s[0] && s[1] && s[2] && s[3])
 
34
                return 1;
 
35
        if (s[0] || s[1] || s[2] || s[3])
 
36
                return 0;
 
37
        return -1;
 
38
 
 
39
}
 
40
int lineintersects(float X1,float X2,float Y1,float Y2)
 
41
{
 
42
        //line segment
 
43
        //X1,Y1 point 1
 
44
        //X2,Y3 point 2
 
45
        //rectangle
 
46
        //RX,RY lower left corner of rectangle
 
47
        //RW width of rectangle
 
48
        //RH height of ractangle
 
49
        //returns 1 if line segment is completely in the rect
 
50
        //0 if they intersect
 
51
        //-1 if completely out
 
52
        float x,y,m,iter;
 
53
        float RX,RY,RW,RH;
 
54
        int intersects,in;
 
55
        RX=view->Selection.X;
 
56
        RY=view->Selection.Y;
 
57
        RW=view->Selection.W;
 
58
        RH=view->Selection.H;
 
59
        if((is_point_in_rectangle(X1,Y1,RX,RY,RW,RH)) && (is_point_in_rectangle(X2,Y2,RX,RY,RW,RH)))
 
60
                return 1;
 
61
        if((is_point_in_rectangle(X1,Y1,RX,RY,RW,RH)) || (is_point_in_rectangle(X2,Y2,RX,RY,RW,RH)))
 
62
                return 0;
 
63
        //to be absolute or not to be one
 
64
        if(X1 > X2)
 
65
        {
 
66
                x=X2;
 
67
                y=Y2;
 
68
                X2=X1;
 
69
                Y2=Y1;
 
70
                X1=x;
 
71
                Y1=y;
 
72
        }
 
73
        x=X1;
 
74
        //iter
 
75
        iter=RW/(float)SELECTION_SEGMENT_DIVIDER;
 
76
        m=(Y2-Y1)/(X2-X1);
 
77
 
 
78
        in=1;
 
79
        intersects=0;
 
80
        while (x <= X2)
 
81
        {
 
82
                x=x+iter;
 
83
                y=Y1+m*(x-X1);          
 
84
                if ( !is_point_in_rectangle(x,y,RX,RY,RW,RH))
 
85
                        in=0;
 
86
                else
 
87
                        intersects=1;
 
88
 
 
89
        }
 
90
        if (in==1)
 
91
                return 1;
 
92
        if (intersects==1)
 
93
                return 0;
 
94
        return -1;
 
95
}
 
96
int is_point_in_rectangle(float X,float Y,float RX,float RY,float RW,float RH)
 
97
{
 
98
        if ((X >= RX) && (X <= (RX+RW) ) && (Y >= RY) && (Y <= (RY+RH)))
 
99
                return 1;
 
100
        else
 
101
                return 0;
 
102
 
 
103
 
 
104
}
 
105
 
 
106
int SelectBeziers(xdot_op* op)
 
107
{
 
108
        if(!view->Selection.Active)
 
109
                return 0;
 
110
        switch (view->Selection.Type)
 
111
        {
 
112
        case 0:
 
113
                if (view->Selection.AlreadySelected)
 
114
                        return 0;
 
115
                if( spline_x_rect(op))
 
116
                {
 
117
                        if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
118
                        {
 
119
                                ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
120
                                select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
121
                                view->Selection.AlreadySelected=1;
 
122
                        }
 
123
                        else
 
124
                        {
 
125
                                ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
126
                                deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
127
                                view->Selection.AlreadySelected=1;
 
128
                        }
 
129
                }
 
130
                        break;
 
131
        case 1:
 
132
//              if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
133
//                      return 0;
 
134
                if(  ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag  != -1 )  &&       (spline_in_rect(op)) )
 
135
                {
 
136
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=1;
 
137
//                        select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
138
                          view->Selection.AlreadySelected=1;
 
139
                }
 
140
                else
 
141
                {
 
142
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=0;
 
143
//                        deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
144
                          view->Selection.AlreadySelected=1;
 
145
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag = -1;                      
 
146
                }
 
147
                break;
 
148
        case 2:
 
149
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
150
                        return 0;
 
151
                if(spline_x_rect(op))
 
152
                {
 
153
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
154
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
155
                          view->Selection.AlreadySelected=1;
 
156
                }
 
157
                break;
 
158
        default:
 
159
                return 0;
 
160
        }
 
161
        return 1;
 
162
 
 
163
}
 
164
 
 
165
int SelectPolygon(xdot_op* op)
 
166
{
 
167
 
 
168
        if(!view->Selection.Active)
 
169
                return 0;
 
170
        switch (view->Selection.Type)
 
171
        {
 
172
        case 0:
 
173
                if (view->Selection.AlreadySelected)
 
174
                        return 0;
 
175
 
 
176
                
 
177
                        if ((point_within_polygon( op)) ||     (polygon_x_rect(op)))
 
178
                        {
 
179
                                  
 
180
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
181
                                {
 
182
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
183
                                          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
184
                                          view->Selection.AlreadySelected=1;
 
185
 
 
186
                                }
 
187
                                else
 
188
                                {
 
189
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
190
                                          deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
191
                                          view->Selection.AlreadySelected=1;
 
192
 
 
193
                                }
 
194
                        }
 
195
                        break;
 
196
        case 1:
 
197
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
198
                        return 0;
 
199
                if(  ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag  != -1 )  &&       (polygon_in_rect(op)) )
 
200
                {
 
201
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=1;
 
202
//                        select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
203
//                        view->Selection.AlreadySelected=1;
 
204
 
 
205
                }
 
206
                else
 
207
                {
 
208
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=0;
 
209
//                      deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
210
                        view->Selection.AlreadySelected=1;
 
211
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag = -1;                      
 
212
                }
 
213
        
 
214
 
 
215
                break;
 
216
        case 2:
 
217
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
218
                        return 0;
 
219
                if(polygon_x_rect(op))
 
220
                {
 
221
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
222
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
223
                          view->Selection.AlreadySelected=1;
 
224
 
 
225
                }
 
226
                break;
 
227
        default:
 
228
                return 0;
 
229
        }
 
230
        return 1;
 
231
}
 
232
 
 
233
int SelectPolyline(xdot_op* op)
 
234
{
 
235
        if(!view->Selection.Active)
 
236
                return 0;
 
237
        switch (view->Selection.Type)
 
238
        {
 
239
        case 0:
 
240
                if (view->Selection.AlreadySelected)
 
241
                        return 0;
 
242
 
 
243
                
 
244
                        if ( polyline_x_rect(op)   )
 
245
                        {
 
246
                                  
 
247
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
248
                                {
 
249
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
250
                                          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
251
                                          view->Selection.AlreadySelected=1;
 
252
 
 
253
                                }
 
254
                                else
 
255
                                {
 
256
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
257
                                          deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
258
                                          view->Selection.AlreadySelected=1;
 
259
 
 
260
                                }
 
261
                        }
 
262
                        break;
 
263
        case 1:
 
264
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
265
                        return 0;
 
266
                if(  ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag  != -1 )  &&       (polyline_in_rect(op)) )
 
267
                {
 
268
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=1;
 
269
 
 
270
                }
 
271
                else
 
272
                {
 
273
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=0;
 
274
                        view->Selection.AlreadySelected=1;
 
275
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag = -1;                      
 
276
                }
 
277
        
 
278
 
 
279
                break;
 
280
        case 2:
 
281
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
282
                        return 0;
 
283
                if(polyline_x_rect(op))
 
284
                {
 
285
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
286
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
287
                          view->Selection.AlreadySelected=1;
 
288
 
 
289
                }
 
290
                break;
 
291
        default:
 
292
                return 0;
 
293
        }
 
294
 
 
295
        return 1;
 
296
 
 
297
}
 
298
int SelectEllipse(xdot_op* op)
 
299
{
 
300
        if(!view->Selection.Active)
 
301
                return 0;
 
302
        switch (view->Selection.Type)
 
303
        {
 
304
        case 0:
 
305
                if (view->Selection.AlreadySelected)
 
306
                        return 0;
 
307
 
 
308
                        if( point_within_ellipse(op))           
 
309
                        {
 
310
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
311
                                {
 
312
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
313
                                          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
314
                                          view->Selection.AlreadySelected=1;
 
315
 
 
316
                                }
 
317
                                else
 
318
                                {
 
319
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
320
                                          deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
321
                                          view->Selection.AlreadySelected=1;
 
322
 
 
323
                                }
 
324
                        }
 
325
                        else if( ellipse_x_rect(op))
 
326
                        {
 
327
                                  
 
328
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
329
                                {
 
330
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
331
                                          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
332
                                          view->Selection.AlreadySelected=1;
 
333
 
 
334
                                }
 
335
                                else
 
336
                                {
 
337
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
338
                                          deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
339
                                          view->Selection.AlreadySelected=1;
 
340
 
 
341
                                }
 
342
                        }
 
343
                        break;
 
344
        case 1:
 
345
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
346
                        return 0;
 
347
                if(  ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag  != -1 )  &&  (ellipse_in_rect(op)) )
 
348
                {
 
349
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=1;
 
350
                          view->Selection.AlreadySelected=1;
 
351
 
 
352
                }
 
353
                else
 
354
                {
 
355
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Preselected=0;
 
356
                        view->Selection.AlreadySelected=1;
 
357
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->selectionflag = -1;                      
 
358
                }
 
359
 
 
360
                break;
 
361
        case 2:
 
362
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
363
                        return 0;
 
364
                if(ellipse_x_rect(op))
 
365
                {
 
366
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
367
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
368
                          view->Selection.AlreadySelected=1;
 
369
 
 
370
                }
 
371
                break;
 
372
        default:
 
373
                return 0;
 
374
        }
 
375
        return 1;
 
376
}
 
377
 
 
378
 
 
379
int SelectText(xdot_op* op)
 
380
{
 
381
        if(!view->Selection.Active)
 
382
                return 0;
 
383
        switch (view->Selection.Type)
 
384
        {
 
385
        case 0:
 
386
                if (view->Selection.AlreadySelected)
 
387
                        return 0;
 
388
                        if( text_x_rect(op))
 
389
                        {
 
390
                                  
 
391
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
392
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
393
                                else
 
394
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
395
                        }
 
396
                        break;
 
397
        case 1:
 
398
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
399
                        return 0;
 
400
                if(text_in_rect(op))
 
401
                {
 
402
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
403
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
404
                          view->Selection.AlreadySelected=1;
 
405
 
 
406
                }
 
407
                break;
 
408
        case 2:
 
409
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
410
                        return 0;
 
411
                if(text_x_rect(op))
 
412
                {
 
413
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
414
                  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
415
                          view->Selection.AlreadySelected=1;
 
416
                }
 
417
                break;
 
418
        default:
 
419
                return 0;
 
420
        }
 
421
        return 1;
 
422
 
 
423
}
 
424
 
 
425
int SelectImage(xdot_op* op)
 
426
{
 
427
        if(!view->Selection.Active)
 
428
                return 0;
 
429
        switch (view->Selection.Type)
 
430
        {
 
431
        case 0:
 
432
                if (view->Selection.AlreadySelected)
 
433
                        return 0;
 
434
                        if( image_x_rect(op))
 
435
                        {
 
436
                                  
 
437
                                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==0)
 
438
                                {
 
439
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
440
                          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
 
441
                                        view->Selection.AlreadySelected=1;
 
442
                                }
 
443
                                else
 
444
                                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=0;
 
445
                        }
 
446
                        break;
 
447
        case 1:
 
448
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
449
                        return 0;
 
450
                if(image_in_rect(op))
 
451
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
452
                break;
 
453
        case 2:
 
454
                if ( ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected==1)
 
455
                        return 0;
 
456
                if(image_x_rect(op))
 
457
                        ((custom_object_data*)AGDATA(((xdot*)(op->parentxdot))->obj))->Selected=1;
 
458
                break;
 
459
        default:
 
460
                return 0;
 
461
        }
 
462
        return 1;
 
463
 
 
464
}
 
465
 
 
466
 
 
467
 
 
468
 
 
469
int spline_in_rect(xdot_op* op)
 
470
{
 
471
        //JUST SEND ALL CONTROL POINTS IN 3D ARRAYS
 
472
        float tempX[4];
 
473
        float tempY[4];
 
474
        float tempZ[4];
 
475
        int temp=0;
 
476
        int i=0;
 
477
        for (i=0;i < op->u.bezier.cnt ; i= i + 1)
 
478
        {
 
479
                if (temp==4)
 
480
                {
 
481
                        if(!within_bezier(tempX,tempY,tempZ,0))
 
482
                                return 0;
 
483
                        tempX[0]=(float)op->u.bezier.pts[i-1].x;
 
484
                        tempY[0]=(float)op->u.bezier.pts[i-1].y;
 
485
                        tempZ[0]=(float)op->u.bezier.pts[i-1].z;
 
486
                        temp=1;
 
487
                        tempX[temp]=(float)op->u.bezier.pts[i].x;                       
 
488
                        tempY[temp]=(float)op->u.bezier.pts[i].y;                       
 
489
                        tempZ[temp]=(float)op->u.bezier.pts[i].z;                       
 
490
                        temp=temp+1;
 
491
                }
 
492
                else
 
493
                {
 
494
                        tempX[temp]=(float)op->u.bezier.pts[i].x;
 
495
                        tempY[temp]=(float)op->u.bezier.pts[i].y;
 
496
                        tempZ[temp]=(float)op->u.bezier.pts[i].z;
 
497
                        temp=temp+1;
 
498
                }
 
499
        }
 
500
        if(!within_bezier(tempX,tempY,tempZ,0))
 
501
                        return 0;
 
502
        else
 
503
                        return 1;
 
504
}
 
505
int spline_x_rect(xdot_op* op)
 
506
{
 
507
        //JUST SEND ALL CONTROL POINTS IN 3D ARRAYS
 
508
        float tempX[4];
 
509
        float tempY[4];
 
510
        float tempZ[4];
 
511
        int temp=0;
 
512
        int i=0;
 
513
        for (i=0;i < op->u.bezier.cnt ; i= i + 1)
 
514
        {
 
515
                if (temp==4)
 
516
                {
 
517
                        if(within_bezier(tempX,tempY,tempZ,1))
 
518
                                return 1;
 
519
                        tempX[0]=(float)op->u.bezier.pts[i-1].x;
 
520
                        tempY[0]=(float)op->u.bezier.pts[i-1].y;
 
521
                        tempZ[0]=(float)op->u.bezier.pts[i-1].z;
 
522
                        temp=1;
 
523
                        tempX[temp]=(float)op->u.bezier.pts[i].x;                       
 
524
                        tempY[temp]=(float)op->u.bezier.pts[i].y;                       
 
525
                        tempZ[temp]=(float)op->u.bezier.pts[i].z;                       
 
526
                        temp=temp+1;
 
527
                }
 
528
                else
 
529
                {
 
530
                        tempX[temp]=(float)op->u.bezier.pts[i].x;
 
531
                        tempY[temp]=(float)op->u.bezier.pts[i].y;
 
532
                        tempZ[temp]=(float)op->u.bezier.pts[i].z;
 
533
                        temp=temp+1;
 
534
                }
 
535
        }
 
536
        if(within_bezier(tempX,tempY,tempZ,1))
 
537
                        return 1;
 
538
        else
 
539
                        return 0;
 
540
}
 
541
int polygon_in_rect(xdot_op* op)
 
542
{
 
543
        int ind=0;
 
544
        for (ind=0;ind < op->u.polygon.cnt-1;ind ++)
 
545
        {
 
546
                if( lineintersects((float)op->u.polygon.pts[ind].x,(float)op->u.polygon.pts[ind+1].x,(float)op->u.polygon.pts[ind].y,(float)op->u.polygon.pts[ind+1].y)!=1)
 
547
                        return 0;
 
548
        };
 
549
        return 1;
 
550
}
 
551
int polygon_x_rect(xdot_op* op)
 
552
{
 
553
        int ind=0;
 
554
        for (ind=0;ind < op->u.polygon.cnt-1;ind ++)
 
555
        {
 
556
                if( lineintersects((float)op->u.polygon.pts[ind].x,(float)op->u.polygon.pts[ind+1].x,(float)op->u.polygon.pts[ind].y,(float)op->u.polygon.pts[ind+1].y)>=0)
 
557
                        return 1;
 
558
        };
 
559
        return 0;
 
560
}
 
561
int polyline_in_rect(xdot_op* op)
 
562
{
 
563
        int ind=0;
 
564
        for (ind=0;ind < op->u.polygon.cnt-1;ind ++)
 
565
        {
 
566
                if( lineintersects((float)op->u.polygon.pts[ind].x,(float)op->u.polygon.pts[ind+1].x,(float)op->u.polygon.pts[ind].y,(float)op->u.polygon.pts[ind+1].y)!=1)
 
567
                        return 0;
 
568
        };
 
569
        return 1;
 
570
}
 
571
int polyline_x_rect(xdot_op* op)
 
572
{
 
573
        int ind=0;
 
574
        for (ind=0;ind < op->u.polygon.cnt-1;ind ++)
 
575
        {
 
576
                if( lineintersects((float)op->u.polygon.pts[ind].x,(float)op->u.polygon.pts[ind+1].x,(float)op->u.polygon.pts[ind].y,(float)op->u.polygon.pts[ind+1].y)>=1)
 
577
                        return 1;
 
578
        };
 
579
        return 0;
 
580
}
 
581
int text_in_rect(xdot_op* op)
 
582
{
 
583
        if( rectintersects((float)op->u.text.x,(float)op->u.text.y,(float)op->u.text.x+op->u.text.width,(float)op->u.text.y+(float)op->u.text.size)==1 )
 
584
                return 1;
 
585
        return 0;
 
586
 
 
587
}
 
588
int text_x_rect(xdot_op* op)
 
589
{
 
590
        if( rectintersects((float)op->u.text.x,(float)op->u.text.y,(float)op->u.text.x+op->u.text.width,(float)op->u.text.y+(float)op->u.text.size)>=1 )
 
591
                return 1;
 
592
        return 0;
 
593
 
 
594
}
 
595
int image_in_rect(xdot_op* op)
 
596
{
 
597
        if( rectintersects((float)op->u.image.pos.x  ,(float)op->u.image.pos.y,(float)op->u.image.pos.w,(float)op->u.image.pos.h)==1)
 
598
                return 1;
 
599
        return 0;
 
600
}
 
601
int image_x_rect(xdot_op* op)
 
602
{
 
603
        if( rectintersects((float)op->u.image.pos.x  ,(float)op->u.image.pos.y,(float)op->u.image.pos.w,(float)op->u.image.pos.h)>=0)
 
604
                return 1;
 
605
        return 0;
 
606
}
 
607
 
 
608
int within_bezier(GLfloat* xp,GLfloat* yp,GLfloat* zp,int isx)
 
609
{
 
610
        double Ax = xp[0]; double Ay = yp[0]; double Az = zp[0];
 
611
        double Bx = xp[1]; double By = yp[1]; double Bz = zp[1];
 
612
        double Cx = xp[2]; double Cy = yp[2]; double Cz = zp[2];
 
613
        double Dx = xp[3]; double Dy = yp[3]; double Dz = zp[3];
 
614
        double X;
 
615
        double Y;
 
616
        double Z;
 
617
        double Xprev;
 
618
        double Yprev;
 
619
        double Zprev;
 
620
        int i = 0;//loop index
 
621
        // Variable
 
622
        double a = 1.0;
 
623
        double b = 1.0 - a;
 
624
        int in=1;
 
625
        int inter=0;
 
626
        for(i = 0; i <= 20; i++)
 
627
        {
 
628
          // Get a point on the curve
 
629
                X = Ax*a*a*a + Bx*3*a*a*b + Cx*3*a*b*b + Dx*b*b*b;
 
630
                Y = Ay*a*a*a + By*3*a*a*b + Cy*3*a*b*b + Dy*b*b*b;
 
631
                Z = Az*a*a*a + Bz*3*a*a*b + Cz*3*a*b*b + Dz*b*b*b;
 
632
                if (i>0)
 
633
                {
 
634
                        if(lineintersects((float)Xprev,(float)X,(float)Yprev,(float)Y) >=0)     //intersection
 
635
                        {
 
636
                                if(isx)
 
637
                                        return 1;
 
638
                        }
 
639
                        if(!(lineintersects((float)Xprev,(float)X,(float)Yprev,(float)Y)==1))   //withing the rect
 
640
                        {
 
641
                                if(!isx)
 
642
                                        return 0;
 
643
                        }
 
644
                }
 
645
                Xprev=X;
 
646
                Yprev=Y;
 
647
                Zprev=Z;
 
648
 
 
649
                a -= 0.05;
 
650
                b = 1.0 - a;
 
651
        }
 
652
        if(isx)
 
653
                return 0;
 
654
        else
 
655
                return 1;
 
656
 
 
657
}
 
658
 
 
659
 
 
660
int ellipse_x_rect(xdot_op* op)
 
661
{
 
662
        float x,y,xradius,yradius;
 
663
 
 
664
        double Xprev;
 
665
        double Yprev;
 
666
        double Zprev;
 
667
 
 
668
        int i=0;
 
669
        x=(float)op->u.ellipse.x;
 
670
        y=(float)op->u.ellipse.y;
 
671
        xradius=(float)op->u.ellipse.w;
 
672
        yradius=(float)op->u.ellipse.h;
 
673
        for (i=0; i < 360; i=i+1)
 
674
        {
 
675
      //convert degrees into radians
 
676
                float degInRad = i*(float)DEG2RAD;
 
677
                if (i>0)
 
678
                {
 
679
                        if(lineintersects((float)Xprev,x+(float)cos(degInRad)*xradius,(float)Yprev,y+(float)sin(degInRad)*yradius) >=0) //intersection
 
680
                                        return 1;
 
681
                }
 
682
 
 
683
                Xprev=x+cos(degInRad)*xradius;
 
684
                Yprev=y+sin(degInRad)*yradius;
 
685
                Zprev=0;
 
686
   }
 
687
   return 0;
 
688
}
 
689
 
 
690
int ellipse_in_rect(xdot_op* op)
 
691
{
 
692
        float x,y,xradius,yradius;
 
693
 
 
694
        double Xprev;
 
695
        double Yprev;
 
696
        double Zprev;
 
697
 
 
698
        int i=0;
 
699
        x=(float)op->u.ellipse.x;
 
700
        y=(float)op->u.ellipse.y;
 
701
        xradius=(float)op->u.ellipse.w;
 
702
        yradius=(float)op->u.ellipse.h;
 
703
        for (i=0; i < 360; i=i+1)
 
704
        {
 
705
      //convert degrees into radians
 
706
                float degInRad = (float)i*(float)DEG2RAD;
 
707
                if (i>0)
 
708
                {
 
709
                        if(!(lineintersects((float)Xprev,(float)x+(float)cos(degInRad)*xradius,(float)Yprev,y+(float)sin(degInRad)*yradius)==1))        //withing the rect
 
710
                                        return 0;
 
711
                }
 
712
 
 
713
                Xprev=x+cos(degInRad)*xradius;
 
714
                Yprev=y+sin(degInRad)*yradius;
 
715
                Zprev=0;
 
716
   }
 
717
   return 1;
 
718
}
 
719
 
 
720
int point_within_ellipse( xdot_op* op)
 
721
{
 
722
 
 
723
        float dx,dy,ex,ey,ea,eb,px,py ;
 
724
        float a;
 
725
        
 
726
        ex=(float)op->u.ellipse.x;
 
727
        ey=(float)op->u.ellipse.y;
 
728
        ea=(float)op->u.ellipse.w;
 
729
        eb=(float)op->u.ellipse.h;
 
730
        px=view->Selection.X+(float)SINGLE_SELECTION_WIDTH/(float)2;
 
731
        py=view->Selection.Y+(float)SINGLE_SELECTION_WIDTH/(float)2;
 
732
        dx = px - ex;
 
733
        dy = py - ey;
 
734
        a=(dx*dx)/(ea*ea) + (dy*dy)/(eb*eb);
 
735
        return (a <= 1);
 
736
}
 
737
 
 
738
int point_within_polygon(xdot_op* op)
 
739
//int pnpoly(int npol, float *xp, float *yp, float x, float y)
 
740
{
 
741
        int i, j, c = 0;
 
742
        int npol=op->u.polygon.cnt;
 
743
        float x,y;
 
744
        op->u.polygon.pts[i].y;
 
745
        op->u.polygon.pts[i].x;
 
746
        x=view->Selection.X+SINGLE_SELECTION_WIDTH/2;
 
747
        y=view->Selection.Y+SINGLE_SELECTION_WIDTH/2;
 
748
 
 
749
        for (i = 0, j = npol-1; i < npol; j = i++) {
 
750
                
 
751
                
 
752
                if ((((op->u.polygon.pts[i].y <= y) && (y < op->u.polygon.pts[j].y)) ||
 
753
                ((op->u.polygon.pts[j].y <= y) && (y < op->u.polygon.pts[i].y))) &&
 
754
                        (x < (op->u.polygon.pts[j].x - op->u.polygon.pts[i].x) * (y - op->u.polygon.pts[i].y) / (op->u.polygon.pts[j].y - op->u.polygon.pts[i].y) + op->u.polygon.pts[i].x))
 
755
                c = !c;
 
756
        }
 
757
        return c;
 
758
}
 
759
 
 
760
 
 
761
//select functions
 
762
int select_node(Agraph_t* g,Agnode_t* N)
 
763
{
 
764
        int ind=0;
 
765
        //check if in the list
 
766
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedNodesCount ; ind ++ )
 
767
        {       
 
768
                if(  ((custom_graph_data*)AGDATA(g))->selectedNodes[ind] == N)
 
769
                        return 0;
 
770
        }
 
771
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
772
        ((custom_graph_data*)AGDATA(g))->selectedNodes=realloc(((custom_graph_data*)AGDATA(g))->selectedNodes,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedNodesCount+1));
 
773
        ((custom_graph_data*)AGDATA(g))->selectedNodes[((custom_graph_data*)AGDATA(g))->selectedNodesCount]=N;
 
774
        ((custom_graph_data*)AGDATA(g))->selectedNodesCount++;
 
775
        ((custom_object_data*)AGDATA(N))->Selected=1;
 
776
        return 1;
 
777
}
 
778
int select_edge(Agraph_t* g,Agedge_t* E)
 
779
{
 
780
        int ind=0;
 
781
        //check if in the list
 
782
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedEdgesCount ; ind ++ )
 
783
        {       
 
784
                if(((custom_graph_data*)AGDATA(g))->selectedEdges[ind] == E)
 
785
                        return 0;
 
786
        }
 
787
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
788
        ((custom_graph_data*)AGDATA(g))->selectedEdges=realloc(((custom_graph_data*)AGDATA(g))->selectedEdges,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedEdgesCount+1));
 
789
        ((custom_graph_data*)AGDATA(g))->selectedEdges[((custom_graph_data*)AGDATA(g))->selectedEdgesCount]=E;
 
790
        ((custom_graph_data*)AGDATA(g))->selectedEdgesCount++;
 
791
        ((custom_object_data*)AGDATA(E))->Selected=1;
 
792
        return 1;
 
793
 
 
794
}
 
795
int select_object (Agraph_t* g,void* obj)
 
796
{
 
797
        switch(AGTYPE(obj))
 
798
        {
 
799
        case AGNODE:
 
800
                select_node(g,obj); 
 
801
                break;
 
802
        case AGEDGE:
 
803
                select_edge(g,obj); 
 
804
                break;
 
805
        case AGRAPH:
 
806
                select_graph(g,obj); 
 
807
                break;
 
808
        default:
 
809
                break;
 
810
        }
 
811
        return 1;
 
812
}
 
813
int deselect_object (Agraph_t* g,void* obj)
 
814
{
 
815
        switch(AGTYPE(obj))
 
816
        {
 
817
        case AGNODE:
 
818
                deselect_node(g,obj); 
 
819
                break;
 
820
        case AGEDGE:
 
821
                deselect_edge(g,obj); 
 
822
                break;
 
823
        case AGRAPH:
 
824
                deselect_graph(g,obj); 
 
825
                break;
 
826
        default:
 
827
                break;
 
828
        }
 
829
        return 1;
 
830
}
 
831
 
 
832
//select functions
 
833
int select_graph(Agraph_t* g,Agraph_t* G)
 
834
{
 
835
        int ind=0;
 
836
        //check if in the list
 
837
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedGraphsCount ; ind ++ )
 
838
        {       
 
839
                if(((custom_graph_data*)AGDATA(g))->selectedGraphs[ind] == G)
 
840
                        return 0;
 
841
        }
 
842
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
843
        ((custom_graph_data*)AGDATA(g))->selectedGraphs=realloc(((custom_graph_data*)AGDATA(g))->selectedGraphs,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedGraphsCount+1));
 
844
        ((custom_graph_data*)AGDATA(g))->selectedGraphs[((custom_graph_data*)AGDATA(g))->selectedGraphsCount]=G;
 
845
        ((custom_graph_data*)AGDATA(g))->selectedGraphsCount++;
 
846
        ((custom_object_data*)AGDATA(G))->Selected=1;
 
847
        return 1;
 
848
}
 
849
int deselect_node(Agraph_t* g,Agnode_t* N)
 
850
{
 
851
        int ind=0;
 
852
        int valid=0;
 
853
        //check if in the list
 
854
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedNodesCount ; ind ++ )
 
855
        {       
 
856
                if(valid)
 
857
                        ((custom_graph_data*)AGDATA(g))->selectedNodes[ind-1]=((custom_graph_data*)AGDATA(g))->selectedNodes[ind];
 
858
                if( ((custom_graph_data*)AGDATA(g))->selectedNodes[ind] == N)
 
859
                        valid=1;
 
860
        }
 
861
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
862
        if(valid)
 
863
        {
 
864
                ((custom_graph_data*)AGDATA(g))->selectedNodes=realloc(((custom_graph_data*)AGDATA(g))->selectedNodes,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedNodesCount-1));
 
865
                ((custom_graph_data*)AGDATA(g))->selectedNodesCount--;
 
866
                ((custom_object_data*)AGDATA(N))->Selected=0;
 
867
                ((custom_object_data*)AGDATA(N))->selectionflag=0;
 
868
        }
 
869
        return 1;       
 
870
 
 
871
}
 
872
int deselect_edge(Agraph_t* g,Agedge_t* E)
 
873
{
 
874
        int ind=0;
 
875
        int valid=0;
 
876
        //check if in the list
 
877
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedEdgesCount ; ind ++ )
 
878
        {       
 
879
                if(valid)
 
880
                        ((custom_graph_data*)AGDATA(g))->selectedEdges[ind-1]=((custom_graph_data*)AGDATA(g))->selectedEdges[ind];
 
881
                if( ((custom_graph_data*)AGDATA(g))->selectedEdges[ind] == E)
 
882
                        valid=1;
 
883
        }
 
884
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
885
        if(valid)
 
886
        {
 
887
                ((custom_graph_data*)AGDATA(g))->selectedEdges=realloc(((custom_graph_data*)AGDATA(g))->selectedEdges,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedEdgesCount-1));
 
888
                ((custom_graph_data*)AGDATA(g))->selectedEdgesCount--;
 
889
                ((custom_object_data*)AGDATA(E))->Selected=0;
 
890
                ((custom_object_data*)AGDATA(E))->selectionflag=0;
 
891
 
 
892
        }
 
893
        return 1;
 
894
}
 
895
int deselect_graph(Agraph_t* g,Agraph_t* G)
 
896
{
 
897
        int ind=0;
 
898
        int valid=0;
 
899
        //check if in the list
 
900
        for (ind=0;ind < ((custom_graph_data*)AGDATA(g))->selectedGraphsCount ; ind ++ )
 
901
        {       
 
902
                if(valid)
 
903
                        ((custom_graph_data*)AGDATA(g))->selectedGraphs[ind-1]=((custom_graph_data*)AGDATA(g))->selectedGraphs[ind];
 
904
                if( ((custom_graph_data*)AGDATA(g))->selectedGraphs[ind] == G)
 
905
                        valid=1;
 
906
        }
 
907
        //for single selections i think realloc is ok, for mass selections i ll figure out something else
 
908
        if(valid)
 
909
        {
 
910
                ((custom_graph_data*)AGDATA(g))->selectedGraphs=realloc(((custom_graph_data*)AGDATA(g))->selectedGraphs,sizeof(Agnode_t*)*(((custom_graph_data*)AGDATA(g))->selectedGraphsCount-1));
 
911
                ((custom_graph_data*)AGDATA(g))->selectedGraphsCount--;
 
912
                ((custom_object_data*)AGDATA(G))->Selected=0;
 
913
                ((custom_object_data*)AGDATA(G))->selectionflag=0;
 
914
 
 
915
        }
 
916
        return 1;
 
917
}
 
918
 
 
919
int select_all_nodes(Agraph_t* g)
 
920
{
 
921
        Agnode_t *n;
 
922
        for (n = agfstnode(g); n; n = agnxtnode(g, n))
 
923
        {
 
924
                        select_node(g,n);
 
925
        }
 
926
        return 1;       
 
927
 
 
928
}
 
929
int select_all_edges(Agraph_t* g)
 
930
{
 
931
   Agnode_t *n;
 
932
        Agedge_t *e;
 
933
 
 
934
        
 
935
        n = agfstnode(g);
 
936
        
 
937
        for (n = agfstnode(g); n; n = agnxtnode(g, n))
 
938
        {
 
939
                for (e = agfstout(g,n) ; e ; e = agnxtout (g,e))
 
940
                {
 
941
                        select_edge(g,e);
 
942
                }
 
943
        }
 
944
        return 1;       
 
945
 
 
946
}
 
947
int select_all_graphs(Agraph_t* g)
 
948
{
 
949
        Agraph_t *s;
 
950
        for (s = agfstsubg(g); s; s = agnxtsubg(s))
 
951
                select_graph(g,s);
 
952
        return 1;       
 
953
}
 
954
 
 
955
int deselect_all_nodes(Agraph_t* g)
 
956
{
 
957
   Agnode_t *n;
 
958
 
 
959
        
 
960
        n = agfstnode(g);
 
961
        
 
962
 
 
963
        for (n = agfstnode(g); n; n = agnxtnode(g, n))
 
964
        {
 
965
                        deselect_node(g,n);
 
966
        }
 
967
        return 1;       
 
968
 
 
969
 
 
970
}
 
971
int deselect_all_edges(Agraph_t* g)
 
972
{
 
973
{
 
974
   Agnode_t *n;
 
975
        Agedge_t *e;
 
976
        n = agfstnode(g);
 
977
        for (n = agfstnode(g); n; n = agnxtnode(g, n))
 
978
        {
 
979
                for (e = agfstout(g,n) ; e ; e = agnxtout (g,e))
 
980
                {
 
981
                        deselect_edge(g,e);
 
982
                }
 
983
        }
 
984
        return 1;       
 
985
}
 
986
 
 
987
 
 
988
}
 
989
int deselect_all_graphs(Agraph_t* g)
 
990
{
 
991
        Agraph_t *s;
 
992
        for (s = agfstsubg(g); s; s = agnxtsubg(s))
 
993
                deselect_graph(g,s);
 
994
        return 1;
 
995
}
 
996
int select_all(Agraph_t* g)
 
997
{
 
998
        select_all_nodes(g);
 
999
        select_all_edges(g);
 
1000
        select_all_graphs(g);
 
1001
        return 1;
 
1002
 
 
1003
}
 
1004
int deselect_all(Agraph_t* g)
 
1005
{
 
1006
        deselect_all_nodes(g);
 
1007
        deselect_all_edges(g);
 
1008
        deselect_all_graphs(g);
 
1009
        return 1;
 
1010
}
 
1011
int line_intersects (float* x,float* y,float* X,float* Y)
 
1012
{
 
1013
        //x,y are arrayf of float for two lines parameters theyt hold 4 points with x and y
 
1014
        //limitx and limity are float arrays with two points, thse points are the end points of the second line
 
1015
        //X,Y are the variables to put intersection point coordinates in
 
1016
        pointf pA,pB,pC,pD,pX;
 
1017
        pA.x=x[0];pA.y=y[0];
 
1018
        pB.x=x[1];pB.y=y[1];
 
1019
        pC.x=x[2];pC.y=y[2];
 
1020
        pD.x=x[3];pD.y=y[3];
 
1021
 
 
1022
        if(intersect (pA, pB, pC, pD, &pX))
 
1023
        {
 
1024
                *X=(float)pX.x;
 
1025
                *Y=(float)pX.y;
 
1026
                if ( (pX.x >=x[2]-0.01) &&      (pX.x <x[3]+0.01) &&
 
1027
                        (pX.y >=y[2]-0.01)      &&      (pX.x <y[3]+0.01))
 
1028
                {
 
1029
                        return 1;
 
1030
 
 
1031
                }
 
1032
                return 0;
 
1033
 
 
1034
        }
 
1035
        else
 
1036
                return 0;
 
1037
 
 
1038
}
 
1039
 
 
1040
int point_within_ellips_with_coords(float ex,float ey,float ea,float eb,float px,float py)
 
1041
{
 
1042
 
 
1043
        float dx,dy;
 
1044
        float a;
 
1045
        dx = px - ex;
 
1046
        dy = py - ey;
 
1047
        a=(dx*dx)/(ea*ea) + (dy*dy)/(eb*eb);
 
1048
        return (a <= 1);
 
1049
}
 
1050
 
 
1051