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