Debian Codegraph @

cmd/smyrna/smyrna_utils.c

raw
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 "smyrna_utils.h"
14 #include <assert.h>
15 #include <common/types.h>
16 #include <common/utils.h>
17 #include <limits.h>
18 #include <stddef.h>
19 #include <stdio.h>
20
21 int l_int(void *obj, Agsym_t * attr, int def)
22 {
23 return late_int(obj, attr, def, INT_MIN);
24 }
25
26 float l_float(void *obj, Agsym_t * attr, float def)
27 {
28 char *p;
29 if (!attr || !obj)
30 return def;
31 p = agxget(obj, attr);
32 if (!p || p[0] == '\0')
33 return def;
34 return atof(p);
35 }
36 int getAttrBool(Agraph_t* g,void* obj,char* attr_name,int def)
37 {
38 Agsym_t *const attr = agattr_text(g, AGTYPE(obj), attr_name, 0);
39 return late_bool(obj, attr,def);
40 }
41 int getAttrInt(Agraph_t* g,void* obj,char* attr_name,int def)
42 {
43 Agsym_t *const attr = agattr_text(g, AGTYPE(obj), attr_name, 0);
44 return l_int(obj,attr,def);
45 }
46 float getAttrFloat(Agraph_t* g,void* obj,char* attr_name,float def)
47 {
48 Agsym_t *const attr = agattr_text(g, AGTYPE(obj), attr_name, 0);
49 return l_float(obj,attr,def);
50 }
51 char* getAttrStr(Agraph_t* g,void* obj,char* attr_name,char* def)
52 {
53 Agsym_t *const attr = agattr_text(g, AGTYPE(obj), attr_name, 0);
54 return late_string(obj, attr,def);
55 }
56
57 glCompPoint getPointFromStr(const char *str) {
58 glCompPoint p = {0};
59 (void)sscanf(str, "%f,%f,%f", &p.x, &p.y, &p.z);
60 return p;
61 }
62
63 int point_in_polygon(glCompPoly_t *selPoly, glCompPoint p) {
64 const size_t npol = LIST_SIZE(selPoly);
65 assert(npol > 0);
66
67 int c = 0;
68 for (size_t i = 0, j = npol - 1; i < npol; j = i++) {
69 const glCompPoint pt_i = LIST_GET(selPoly, i);
70 const glCompPoint pt_j = LIST_GET(selPoly, j);
71 if (((pt_i.y <= p.y && p.y < pt_j.y) ||
72 (pt_j.y <= p.y && p.y < pt_i.y)) &&
73 p.x < (pt_j.x - pt_i.x) * (p.y - pt_i.y) / (pt_j.y - pt_i.y) + pt_i.x)
74 c = !c;
75 }
76 return c;
77 }