Graphviz 13.1.0~dev.20250626.0830
Loading...
Searching...
No Matches
glutils.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 <glcomp/glutils.h>
12#include <stdbool.h>
13#include <stdint.h>
14#include <stdlib.h>
15#include <string.h>
16#include <glcomp/glcompdefs.h>
17
18/*transforms 2d windows location to 3d gl coords but depth is calculated unlike the previous function*/
19void GetOGLPosRef(int x, int y, float *X, float *Y) {
20 double wwinZ;
21 double posX, posY;
22
23 int32_t viewport[4];
24 double modelview[16];
25 double projection[16];
26 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
27 glGetDoublev(GL_PROJECTION_MATRIX, projection);
28 glGetIntegerv(GL_VIEWPORT, viewport);
29
30 // draw a point to an unimportant location to get window coordinates
31 glColor4f(0.0f, 0.0f, 0.0f, 0.001f);
32
33 glBegin(GL_POINTS);
34 glVertex3f(-100.0f, -100.0f, 0.0f);
35 glEnd();
36 gluProject(-100.0, -100.0, 0.00, modelview, projection, viewport,
37 &(double){0}, &(double){0}, &wwinZ);
38 const double winX = x;
39 const double winY = (double)viewport[3] - y;
40 gluUnProject(winX, winY, wwinZ, modelview, projection, viewport, &posX,
41 &posY, &(double){0}); // ignore Z that the caller does not need
42
43 *X = (float) posX;
44 *Y = (float) posY;
45}
46
47float GetOGLDistance(float l) {
48 double wwinZ;
49 double posX;
50 double posXX;
51
52 int32_t viewport[4];
53 double modelview[16];
54 double projection[16];
55
56 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
57 glGetDoublev(GL_PROJECTION_MATRIX, projection);
58 glGetIntegerv(GL_VIEWPORT, viewport);
59
60 // draw a point to an unimportant location to get window coordinates
61 glColor4f(0.0f, 0.0f, 0.0f, 0.001f);
62
63 glBegin(GL_POINTS);
64 glVertex3f(10.0f, 10.0f, 1.0f);
65 glEnd();
66 gluProject(10.0, 10.0, 1.0, modelview, projection, viewport, &(double){0},
67 &(double){0}, &wwinZ);
68 float x = 50.0f;
69 const float y = 50.0f;
70 double winX = x;
71 double winY = (double)viewport[3] - y;
72 gluUnProject(winX, winY, wwinZ, modelview, projection, viewport, &posX,
73 &(double){0}, &(double){0});
74 x += l;
75 winX = x;
76 winY = (double)viewport[3] - y;
77 gluUnProject(winX, winY, wwinZ, modelview, projection, viewport,
78 &posXX, &(double){0}, &(double){0});
79 return (float)(posXX - posX);
80}
81
82/*
83 functions def: returns opengl coordinates of first hit object by using screen coordinates
84 x,y; 2D screen coordinates (usually received from mouse events
85 X,Y,Z; pointers to coordinates values to be calculated
86 return value: no return value
87
88
89*/
90
91void to3D(int x, int y, float *X, float *Y, float *Z) {
92 int const WIDTH = 20;
93
94 int32_t viewport[4];
95 double modelview[16];
96 double projection[16];
97 float winZ[400];
98 double posX, posY, posZ;
99 int idx;
100
101 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
102 glGetDoublev(GL_PROJECTION_MATRIX, projection);
103 glGetIntegerv(GL_VIEWPORT, viewport);
104
105 const double winX = x;
106 const double winY = (double)viewport[3] - y;
107
108 glReadPixels(x - WIDTH / 2, (int) winY - WIDTH / 2, WIDTH, WIDTH,
109 GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
110 float comp = -9999999;
111 for (idx = 0; idx < WIDTH * WIDTH; idx++) {
112 if (winZ[idx] > comp && winZ[idx] < 1)
113 comp = winZ[idx];
114 }
115
116 gluUnProject(winX, winY, comp, modelview, projection, viewport, &posX,
117 &posY, &posZ);
118
119 *X = (float)posX;
120 *Y = (float)posY;
121 *Z = (float)posZ;
122}
123
124#include <math.h>
125
127{
128 return (glCompPoint){.x = p.x - q.x, .y = p.y - q.y, .z = p.z - q.z};
129}
130
131static double dot(glCompPoint p, glCompPoint q)
132{
133 return p.x * q.x + p.y * q.y + p.z * q.z;
134}
135
136static double len(glCompPoint p)
137{
138 return sqrt(dot(p, p));
139}
140
142{
143 glCompPoint r;
144
145 r.x = p.x + m * (q.x - p.x);
146 r.y = p.y + m * (q.y - p.y);
147 r.z = p.z + m * (q.z - p.z);
148 return r;
149}
150
151static double dist(glCompPoint p, glCompPoint q)
152{
153 return len(sub(p, q));
154}
155
156/*
157 * Given a line segment determined by two points a and b, and a 3rd point p,
158 * return the distance between the point and the segment.
159 * If the perpendicular from p to the line a-b is outside of the segment,
160 * return the distance to the closer of a or b.
161 */
163{
164 float U;
165 glCompPoint q;
166 glCompPoint ba = sub(b, a);
167 glCompPoint pa = sub(p, a);
168
169 U = (float) (dot(pa, ba) / dot(ba, ba));
170
171 if (U > 1)
172 q = b;
173 else if (U < 0)
174 q = a;
175 else
176 q = blend(a, b, U);
177
178 return dist(p, q);
179}
180
183{
184 /*check alignments first , alignments overrides anchors */
185 float borderWidth;
186 ref->height = child->height;
187 ref->width = child->width;
188 if(!parent)
189 {
190 child->refPos.x = child->pos.x;
191 child->refPos.y = child->pos.y;
192 return;
193 }
194 borderWidth = parent->borderWidth;
195 if (child->align != glAlignNone) //if alignment, make sure width and height is no greater than parent
196 {
197 if (child->width > parent->width)
198 ref->width = parent->width - 2.0f *borderWidth;
199 if (child->height > parent->height)
200 ref->height = parent->height - 2.0f *borderWidth;;
201
202 }
203
204 ref->pos.x = parent->refPos.x + ref->pos.x + borderWidth;
205 ref->pos.y = parent->refPos.y + ref->pos.y + borderWidth;
206
207
208 switch (child->align) {
209 case glAlignLeft:
210 ref->pos.x = parent->refPos.x + borderWidth;
211 ref->pos.y = parent->refPos.y + borderWidth;
212 ref->height = parent->height - 2.0f * borderWidth;
213 break;
214 case glAlignRight:
215 ref->pos.x =
216 parent->refPos.x + parent->width - child->width - borderWidth;
217 ref->pos.y = parent->refPos.y + borderWidth;
218 ref->height = parent->height - 2.0f * borderWidth;
219 break;
220
221 case glAlignTop:
222 ref->pos.y =
223 parent->refPos.y + parent->height - child->height -
224 borderWidth;
225 ref->pos.x = parent->refPos.x;
226 ref->width = parent->width - 2.0f * borderWidth;
227 break;
228
229 case glAlignBottom:
230 ref->pos.y = parent->refPos.y + borderWidth;
231 ref->pos.x = parent->refPos.x + borderWidth;
232 ref->width = parent->width - 2.0f * borderWidth;
233 break;
234 case glAlignParent:
235 ref->pos.y = parent->refPos.y + borderWidth;
236 ref->pos.x = parent->refPos.x + borderWidth;;
237 ref->width = parent->width - 2.0f * borderWidth;;
238 ref->height = parent->height - 2.0f * borderWidth;
239 break;
240 case glAlignCenter:
241 case glAlignNone:
242 break;
243 }
244 if (child->align == glAlignNone) // No alignment, check anchors
245 {
246 ref->pos.x = parent->refPos.x + child->pos.x + borderWidth;
247 ref->pos.y = parent->refPos.y + child->pos.y + borderWidth;
248
249 if (child->anchor.leftAnchor)
250 ref->pos.x =
251 parent->refPos.x + child->anchor.left + borderWidth;
252 if (child->anchor.bottomAnchor)
253 ref->pos.y =
254 parent->refPos.y + child->anchor.bottom + borderWidth;
255
256 if (child->anchor.topAnchor)
257 ref->height =
258 parent->refPos.y + parent->height - ref->pos.y -
259 child->anchor.top - borderWidth;
260 if (child->anchor.rightAnchor)
261 ref->width =
262 parent->refPos.x + parent->width - ref->pos.x -
263 child->anchor.right - borderWidth;
264 }
265 child->refPos.x = ref->pos.x;
266 child->refPos.y = ref->pos.y;
267 child->width = ref->width;
268 child->height = ref->height;
269}
270
272 glCompPoint * p2, glCompPoint * p3)
273{
274 glVertex3f(p0->x, p0->y, p0->z);
275 glVertex3f(p1->x, p1->y, p1->z);
276 glVertex3f(p2->x, p2->y, p2->z);
277 glVertex3f(p3->x, p3->y, p3->z);
278}
279
280void glCompSetColor(glCompColor c) { glColor4f(c.R, c.G, c.B, c.A); }
281
283{
284 glBegin(GL_QUADS);
285 glVertex3f(r->pos.x, r->pos.y, r->pos.z);
286 glVertex3f(r->pos.x + r->w, r->pos.y, r->pos.z);
287 glVertex3f(r->pos.x + r->w, r->pos.y + r->h, r->pos.z);
288 glVertex3f(r->pos.x, r->pos.y + r->h, r->pos.z);
289 glEnd();
290}
291
292void glCompDrawRectPrism(glCompPoint *p, float w, float h, float b, float d,
293 glCompColor *c, bool bumped) {
294 float color_fac;
295 glCompPoint A, B, C, D, E, F, G, H;
296 float dim = 1;
297 if (!bumped) {
298 color_fac = 1.3f;
299 b = b - 2;
300 dim = 0.5;
301 } else
302 color_fac = 1.0f / 1.3f;
303
304
305 A.x = p->x;
306 A.y = p->y;
307 A.z = p->z;
308 B.x = p->x + w;
309 B.y = p->y;
310 B.z = p->z;
311 C.x = p->x + w;
312 C.y = p->y + h;
313 C.z = p->z;
314 D.x = p->x;
315 D.y = p->y + h;
316 D.z = p->z;
317 G.x = p->x + b;
318 G.y = p->y + b;
319 G.z = p->z + d;
320 H.x = p->x + w - b;
321 H.y = p->y + b;
322 H.z = p->z + d;
323 E.x = p->x + b;
324 E.y = p->y + h - b;
325 E.z = p->z + d;
326 F.x = p->x + w - b;
327 F.y = p->y + h - b;
328 F.z = p->z + d;
329 glBegin(GL_QUADS);
330 glColor4f(c->R * dim, c->G * dim, c->B * dim, c->A);
331 glCompQuadVertex(&G, &H, &F, &E);
332
333 glColor4f(c->R * color_fac * dim, c->G * color_fac * dim,
334 c->B * color_fac * dim, c->A);
335 glCompQuadVertex(&A, &B, &H, &G);
336 glCompQuadVertex(&B, &H, &F, &C);
337
338 glColor4f(c->R / color_fac * dim, c->G / color_fac * dim,
339 c->B / color_fac * dim, c->A);
340 glCompQuadVertex(&A, &G, &E, &D);
341 glCompQuadVertex(&E, &F, &C, &D);
342 glEnd();
343
344}
345
347 float rv = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z);
348 rv=sqrtf(rv);
349 if (rv <=R)
350 return 0;
351 return rv;
352}
353
354int is_point_in_rectangle(float X, float Y, float RX, float RY, float RW,float RH)
355{
356 if (X >= RX && X <= RX + RW && Y >= RY && Y <= RY + RH)
357 return 1;
358 else
359 return 0;
360}
361
#define parent(i)
Definition closest.c:80
#define sub(h, i)
Definition closest.c:67
#define A(n, t)
Definition expr.h:76
#define F
Definition expr.h:70
#define E
Definition gdefs.h:6
#define Y(i)
Definition gdefs.h:3
#define X(prefix, name, str, type, subtype,...)
Definition gdefs.h:14
#define G
Definition gdefs.h:7
#define dot(v, w)
Definition geom.c:228
@ glAlignTop
Definition glcompdefs.h:60
@ glAlignLeft
Definition glcompdefs.h:60
@ glAlignCenter
Definition glcompdefs.h:61
@ glAlignBottom
Definition glcompdefs.h:60
@ glAlignNone
Definition glcompdefs.h:60
@ glAlignParent
Definition glcompdefs.h:61
@ glAlignRight
Definition glcompdefs.h:61
float GetOGLDistance(float l)
Definition glutils.c:47
static double len(glCompPoint p)
Definition glutils.c:136
void glCompDrawRectangle(glCompRect *r)
Definition glutils.c:282
float distBetweenPts(glCompPoint A, glCompPoint B, float R)
Definition glutils.c:346
void glCompCalcWidget(glCompCommon *parent, glCompCommon *child, glCompCommon *ref)
Definition glutils.c:181
int is_point_in_rectangle(float X, float Y, float RX, float RY, float RW, float RH)
Definition glutils.c:354
void glCompDrawRectPrism(glCompPoint *p, float w, float h, float b, float d, glCompColor *c, bool bumped)
Definition glutils.c:292
void GetOGLPosRef(int x, int y, float *X, float *Y)
Definition glutils.c:19
static double dist(glCompPoint p, glCompPoint q)
Definition glutils.c:151
static void glCompQuadVertex(glCompPoint *p0, glCompPoint *p1, glCompPoint *p2, glCompPoint *p3)
Definition glutils.c:271
void to3D(int x, int y, float *X, float *Y, float *Z)
Definition glutils.c:91
double point_to_lineseg_dist(glCompPoint p, glCompPoint a, glCompPoint b)
Definition glutils.c:162
static glCompPoint blend(glCompPoint p, glCompPoint q, float m)
Definition glutils.c:141
void glCompSetColor(glCompColor c)
Definition glutils.c:280
#define WIDTH
Definition gmlparse.h:123
#define B
Definition hierarchy.c:118
#define D
Definition hierarchy.c:120
static const int dim
#define C
Definition pack.c:30
void ref(Site *v)
Definition site.c:59
glCompPoint pos
Definition glcompdefs.h:164
glCompAnchor anchor
Definition glcompdefs.h:176
glCompAlignment align
Definition glcompdefs.h:175
glCompPoint refPos
Definition glcompdefs.h:165
glCompPoint pos
Definition glcompdefs.h:118