Graphviz 12.0.1~dev.20240715.2254
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
295{
296 glColor4f(c->R, c->G, c->B, c->A);
297}
298
300{
301 glBegin(GL_QUADS);
302 glVertex3f(r->pos.x, r->pos.y, r->pos.z);
303 glVertex3f(r->pos.x + r->w, r->pos.y, r->pos.z);
304 glVertex3f(r->pos.x + r->w, r->pos.y + r->h, r->pos.z);
305 glVertex3f(r->pos.x, r->pos.y + r->h, r->pos.z);
306 glEnd();
307}
308
309void glCompDrawRectPrism(glCompPoint *p, float w, float h, float b, float d,
310 glCompColor *c, bool bumped) {
311 float color_fac;
312 glCompPoint A, B, C, D, E, F, G, H;
313 float dim = 1;
314 if (!bumped) {
315 color_fac = 1.3f;
316 b = b - 2;
317 dim = 0.5;
318 } else
319 color_fac = 1.0f / 1.3f;
320
321
322 A.x = p->x;
323 A.y = p->y;
324 A.z = p->z;
325 B.x = p->x + w;
326 B.y = p->y;
327 B.z = p->z;
328 C.x = p->x + w;
329 C.y = p->y + h;
330 C.z = p->z;
331 D.x = p->x;
332 D.y = p->y + h;
333 D.z = p->z;
334 G.x = p->x + b;
335 G.y = p->y + b;
336 G.z = p->z + d;
337 H.x = p->x + w - b;
338 H.y = p->y + b;
339 H.z = p->z + d;
340 E.x = p->x + b;
341 E.y = p->y + h - b;
342 E.z = p->z + d;
343 F.x = p->x + w - b;
344 F.y = p->y + h - b;
345 F.z = p->z + d;
346 glBegin(GL_QUADS);
347 glColor4f(c->R * dim, c->G * dim, c->B * dim, c->A);
348 glCompQuadVertex(&G, &H, &F, &E);
349
350 glColor4f(c->R * color_fac * dim, c->G * color_fac * dim,
351 c->B * color_fac * dim, c->A);
352 glCompQuadVertex(&A, &B, &H, &G);
353 glCompQuadVertex(&B, &H, &F, &C);
354
355 glColor4f(c->R / color_fac * dim, c->G / color_fac * dim,
356 c->B / color_fac * dim, c->A);
357 glCompQuadVertex(&A, &G, &E, &D);
358 glCompQuadVertex(&E, &F, &C, &D);
359 glEnd();
360
361}
362
364 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);
365 rv=sqrtf(rv);
366 if (rv <=R)
367 return 0;
368 return rv;
369}
370
371int is_point_in_rectangle(float X, float Y, float RX, float RY, float RW,float RH)
372{
373 if (X >= RX && X <= RX + RW && Y >= RY && Y <= RY + RH)
374 return 1;
375 else
376 return 0;
377}
378
#define parent(i)
Definition closest.c:78
#define sub(h, i)
Definition closest.c:65
#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:80
@ glAlignLeft
Definition glcompdefs.h:80
@ glAlignCenter
Definition glcompdefs.h:81
@ glAlignBottom
Definition glcompdefs.h:80
@ glAlignNone
Definition glcompdefs.h:80
@ glAlignParent
Definition glcompdefs.h:81
@ glAlignRight
Definition glcompdefs.h:81
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:299
float distBetweenPts(glCompPoint A, glCompPoint B, float R)
Definition glutils.c:363
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:371
void glCompSetColor(glCompColor *c)
Definition glutils.c:294
void glCompDrawRectPrism(glCompPoint *p, float w, float h, float b, float d, glCompColor *c, bool bumped)
Definition glutils.c:309
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
#define WIDTH
Definition gmlparse.c:404
#define B
Definition hierarchy.c:117
#define D
Definition hierarchy.c:119
#define C
Definition pack.c:30
void ref(Site *v)
Definition site.c:59
glCompPoint pos
Definition glcompdefs.h:208
glCompAnchor anchor
Definition glcompdefs.h:220
glCompAlignment align
Definition glcompdefs.h:219
glCompPoint refPos
Definition glcompdefs.h:209
glCompPoint pos
Definition glcompdefs.h:156