smyrna_utils.c 2.3 KB

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