Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
selectionfuncs.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <limits.h>
12#include <stddef.h>
13#include <util/alloc.h>
14#include "selectionfuncs.h"
15#include "topviewfuncs.h"
16#include "smyrna_utils.h"
17
18static void select_node(Agraph_t* g,Agnode_t* obj,int reverse)
19{
20 Agsym_t* sel_attr = GN_selected(g);
21
22 if(!sel_attr)
23 sel_attr = GN_selected(g) = agattr(g, AGNODE,"selected","0");
24 if(!reverse)
25 {
26 agxset(obj,sel_attr,"1");
27 ND_selected(obj) = 1;
28 }
29 else
30 {
31 if(ND_selected(obj)==1)
32 {
33 agxset(obj,sel_attr,"0");
34 ND_selected(obj) = 0;
35 ND_printLabel(obj) = 0;
36 }
37 else
38 {
39 agxset(obj,sel_attr,"1");
40 ND_selected(obj) = 1;
41 }
42
43 }
44
45
46}
47static void select_edge(Agraph_t* g,Agedge_t* obj,int reverse)
48{
49 Agsym_t* sel_attr = GE_selected(g);
50
51 if (!sel_attr)
52 sel_attr = GE_selected(g) = agattr(g, AGEDGE,"selected","0");
53 if (!reverse)
54 {
55 agxset(obj,sel_attr,"1");
56 ED_selected(obj) = 1;
57 }
58 else
59 {
60 if (ED_selected(obj) == 1)
61 {
62 agxset(obj,sel_attr,"0");
63 ED_selected(obj) = 0;
64 }
65 else
66 {
67 agxset(obj,sel_attr,"1");
68 ED_selected(obj) = 1;
69 }
70 }
71
72
73}
74
75static void pick_objects_in_rect(Agraph_t *g, float x1, float y1, float x2,
76 float y2) {
77 Agnode_t *v;
78 Agedge_t *e;
79 glCompPoint posT;
80 glCompPoint posH;
81 glCompPoint posN;
82
83 for (v = agfstnode(g); v; v = agnxtnode(g, v))
84 {
86 posN = ND_A(v);
87 if(!ND_visible(v))
88 continue;
89 if(is_point_in_rectangle(posN.x,posN.y,x1,y1,x2-x1,y2-y1) )
90 select_node(g,v,0);
91 }
93 for (e = agfstout(g, v); e; e = agnxtout(g, e))
94 {
95 posT = ED_posTail(e);
96 posH = ED_posHead(e);
97 if(is_point_in_rectangle(posT.x,posT.y,x1,y1,x2-x1,y2-y1))
98 if(is_point_in_rectangle(posH.x,posH.y,x1,y1,x2-x1,y2-y1))
99 select_edge(g,e,0);
100 }
101 }
102 }
103}
104
105
106
108{
109 Agnode_t *v;
110 Agedge_t *e;
111 glCompPoint posT;
112 glCompPoint posH;
113 glCompPoint posN;
114 int defaultNodeShape;
115 float dist = FLT_MAX;
116 float nd; // node distance to point
117 float ed; // edge distance to point
118 float nodeSize=0;
119 void *rv = NULL;
120
121 defaultNodeShape=getAttrBool(g,g,"defaultnodeshape",0);
122
123 if(defaultNodeShape==0)
125
126 for (v = agfstnode(g); v; v = agnxtnode(g, v))
127 {
128 if(!ND_visible(v))
129 continue;
130 posN = ND_A(v);
131 if(defaultNodeShape==1)
132 {
133 nodeSize = ND_size(v);
134 }
135
136 nd=distBetweenPts(posN,p,nodeSize);
137 if( nd < dist )
138 {
139 rv=v;dist=nd;
140 }
141
142 for (e = agfstout(g, v); e; e = agnxtout(g, e))
143 {
144 posT = ED_posTail(e);
145 posH = ED_posHead(e);
146 ed=point_to_lineseg_dist(p, posT,posH);
147 if( ed < dist ) {rv=e;dist=ed;}
148 }
149 }
150 return rv;
151}
152
153void pick_object_xyz(Agraph_t *g, topview *t, float x, float y, float z) {
154 glCompPoint p;
155 void* a;
156 p.x=x;p.y=y;p.z=z;
157 a=pick_object(g,p);
158 if (!a)
159 return;
160 if(agobjkind(a)==AGNODE)
161 {
162 select_node(g,a,1);
163 ND_printLabel((Agnode_t*)a) = 1;
164
166
167 }
168 if(agobjkind(a)==AGEDGE)
169 {
170 select_edge(g,a,1);
172 }
173}
175{
176 float x1;
177 float y1;
178 float x2;
179 float y2;
181 {
182 x1=view->mouse.GLinitPos.x;
184 }
185 else
186 {
187 x2=view->mouse.GLinitPos.x;
189
190 }
192 {
193 y1=view->mouse.GLinitPos.y;
195 }
196 else
197 {
198 y2=view->mouse.GLinitPos.y;
200 }
201 pick_objects_in_rect(g,x1,y1,x2,y2);
204}
205
206
207
209{
210 Agnode_t *v;
211 Agedge_t *e;
212 Agsym_t* nsel_attr = GN_selected(g);
213 Agsym_t* esel_attr = GE_selected(g);
214 if(!nsel_attr)
215 nsel_attr = GN_selected(g) = agattr(g, AGNODE,"selected","0");
216 if(!esel_attr)
217 esel_attr = GE_selected(g) = agattr(g, AGEDGE,"selected","0");
218 for (v = agfstnode(g); v; v = agnxtnode(g, v))
219 {
220 agxset(v,nsel_attr,"0");
221 ND_selected(v) = 0;
222 ND_printLabel(v) = 0;
223
224 for (e = agfstout(g, v); e; e = agnxtout(g, e))
225 {
226 agxset(e,esel_attr,"0");
227 ED_selected(e) = 0;
228 }
229 }
232}
233
234static int close_poly(glCompPoly_t *selPoly, glCompPoint pt) {
235 /* int i=0; */
236 const float EPS = GetOGLDistance(3.0f);
237 if (glCompPoly_size(selPoly) < 2)
238 return 0;
239 if (glCompPoly_front(selPoly)->x - pt.x < EPS &&
240 glCompPoly_front(selPoly)->y - pt.y < EPS)
241 return 1;
242 return 0;
243}
244
245static void select_polygon(Agraph_t *g, glCompPoly_t *selPoly) {
246 Agnode_t *v;
247 glCompPoint posN;
248
249 for (v = agfstnode(g); v; v = agnxtnode(g, v))
250 {
251 posN = ND_A(v);
252 if(point_in_polygon(selPoly,posN))
253 select_node(g,v,0);
254 }
256}
257
258void add_selpoly(Agraph_t *g, glCompPoly_t *selPoly, glCompPoint pt) {
259 if(!close_poly(selPoly,pt))
260 {
261 glCompPoly_append(selPoly, (glCompPoint){.x = pt.x, .y = pt.y});
262 }
263 else
264 {
265 select_polygon (g,selPoly);
266 glCompPoly_free(selPoly);
267 }
268}
269
Memory allocation wrappers that exit on failure.
#define EPS
Definition emit.c:443
static double dist(int dim, double *x, double *y)
float GetOGLDistance(float l)
Definition glutils.c:53
float distBetweenPts(glCompPoint A, glCompPoint B, float R)
Definition glutils.c:360
int is_point_in_rectangle(float X, float Y, float RX, float RY, float RW, float RH)
Definition glutils.c:368
double point_to_lineseg_dist(glCompPoint p, glCompPoint a, glCompPoint b)
Definition glutils.c:176
node NULL
Definition grammar.y:163
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:338
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:478
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
int agobjkind(void *obj)
Definition obj.c:252
@ AGEDGE
Definition cgraph.h:207
@ AGNODE
Definition cgraph.h:207
static int z
void deselect_all(Agraph_t *g)
static void * pick_object(Agraph_t *g, glCompPoint p)
void add_selpoly(Agraph_t *g, glCompPoly_t *selPoly, glCompPoint pt)
static void pick_objects_in_rect(Agraph_t *g, float x1, float y1, float x2, float y2)
void pick_objects_rect(Agraph_t *g)
static void select_node(Agraph_t *g, Agnode_t *obj, int reverse)
static void select_edge(Agraph_t *g, Agedge_t *obj, int reverse)
static int close_poly(glCompPoly_t *selPoly, glCompPoint pt)
static void select_polygon(Agraph_t *g, glCompPoly_t *selPoly)
void pick_object_xyz(Agraph_t *g, topview *t, float x, float y, float z)
int getAttrBool(Agraph_t *g, void *obj, char *attr_name, int def)
int point_in_polygon(glCompPoly_t *selPoly, glCompPoint p)
ViewInfo * view
Definition viewport.c:37
#define ED_posHead(e)
Definition smyrnadefs.h:177
#define GN_selected(g)
Definition smyrnadefs.h:199
#define ED_posTail(e)
Definition smyrnadefs.h:176
#define ND_size(n)
Definition smyrnadefs.h:165
#define ND_printLabel(n)
Definition smyrnadefs.h:163
#define GE_selected(g)
Definition smyrnadefs.h:205
#define ED_selected(e)
Definition smyrnadefs.h:175
#define ND_selected(n)
Definition smyrnadefs.h:162
#define ND_A(n)
Definition smyrnadefs.h:164
#define ND_visible(n)
Definition smyrnadefs.h:161
graph or subgraph
Definition cgraph.h:425
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:637
topview * Topview
Definition smyrnadefs.h:313
float nodeScale
Definition smyrnadefs.h:347
float zoom
Definition smyrnadefs.h:258
glCompMouse mouse
Definition smyrnadefs.h:303
bool selectEdges
Definition smyrnadefs.h:216
bool selectNodes
Definition smyrnadefs.h:215
glCompPoint GLfinalPos
Definition glcompdefs.h:243
glCompPoint GLinitPos
Definition glcompdefs.h:242
float fitin_zoom
Definition smyrnadefs.h:236
selection sel
Definition smyrnadefs.h:242
void cacheSelectedEdges(Agraph_t *g, topview *t)
void cacheSelectedNodes(Agraph_t *g, topview *t)