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