init.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. \file nviz/init.cpp
  3. \brief wxNviz extension (3D view mode) - initialization
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. (C) 2008-2009 by Martin Landa, and the GRASS development team
  8. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008)
  9. */
  10. #include "nviz.h"
  11. #define MSG 0
  12. #define WARN 1
  13. #define ERR 2
  14. static void swap_gl();
  15. static int print_error(const char *, const int);
  16. static int print_percent(int);
  17. static void print_sentence (PyObject*, const int, const char *);
  18. static PyObject *logStream;
  19. static int message_id = 1;
  20. /*!
  21. \brief Initialize Nviz class instance
  22. */
  23. Nviz::Nviz(PyObject *log)
  24. {
  25. G_gisinit(""); /* GRASS functions */
  26. logStream = log;
  27. G_set_error_routine(&print_error);
  28. G_set_percent_routine(&print_percent);
  29. GS_libinit();
  30. GVL_libinit();
  31. GS_set_swap_func(swap_gl);
  32. data = (nv_data*) G_malloc(sizeof (nv_data));
  33. G_debug(1, "Nviz::Nviz()");
  34. }
  35. /*!
  36. \brief Destroy Nviz class instance
  37. */
  38. Nviz::~Nviz()
  39. {
  40. G_unset_error_routine();
  41. G_unset_percent_routine();
  42. G_free((void *) data);
  43. data = NULL;
  44. logStream = NULL;
  45. }
  46. void Nviz::InitView()
  47. {
  48. /* initialize nviz data */
  49. Nviz_init_data(data);
  50. /* define default attributes for map objects */
  51. Nviz_set_surface_attr_default();
  52. /* set background color */
  53. Nviz_set_bgcolor(data, Nviz_color_from_str("white")); /* TODO */
  54. /* initialize view */
  55. Nviz_init_view();
  56. /* set default lighting model */
  57. SetLightsDefault();
  58. /* clear window */
  59. GS_clear(data->bgcolor);
  60. G_debug(1, "Nviz::InitView()");
  61. return;
  62. }
  63. void swap_gl()
  64. {
  65. return;
  66. }
  67. /*!
  68. \brief Set background color
  69. \param color_str color string
  70. */
  71. void Nviz::SetBgColor(const char *color_str)
  72. {
  73. data->bgcolor = Nviz_color_from_str(color_str);
  74. return;
  75. }
  76. /*
  77. \brief Print one message, prefix inserted before each new line
  78. From lib/gis/error.c
  79. */
  80. void print_sentence (PyObject *pyFd, const int type, const char *msg)
  81. {
  82. char prefix[256];
  83. const char *start;
  84. char* sentence;
  85. switch (type) {
  86. case MSG:
  87. sprintf (prefix, "GRASS_INFO_MESSAGE(%d,%d): Nviz: ", getpid(), message_id);
  88. break;
  89. case WARN:
  90. sprintf (prefix, "GRASS_INFO_WARNING(%d,%d): Nviz: ", getpid(), message_id);
  91. break;
  92. case ERR:
  93. sprintf (prefix, "GRASS_INFO_ERROR(%d,%d): Nviz: ", getpid(), message_id);
  94. break;
  95. }
  96. start = msg;
  97. PyFile_WriteString("\n", pyFd);
  98. while (*start != '\0') {
  99. const char *next = start;
  100. PyFile_WriteString(prefix, pyFd);
  101. while ( *next != '\0' ) {
  102. next++;
  103. if ( *next == '\n' ) {
  104. next++;
  105. break;
  106. }
  107. }
  108. sentence = (char *) G_malloc((next - start + 1) * sizeof (char));
  109. strncpy(sentence, start, next - start + 1);
  110. sentence[next-start] = '\0';
  111. PyFile_WriteString(sentence, pyFd);
  112. G_free((void *)sentence);
  113. PyFile_WriteString("\n", pyFd);
  114. start = next;
  115. }
  116. PyFile_WriteString("\n", pyFd);
  117. sprintf(prefix, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
  118. PyFile_WriteString(prefix, pyFd);
  119. message_id++;
  120. }
  121. /*!
  122. \brief Print error/warning/message
  123. \param msg message buffer
  124. \param type message type
  125. \return 0
  126. */
  127. int print_error(const char *msg, const int type)
  128. {
  129. if (logStream) {
  130. print_sentence(logStream, type, msg);
  131. }
  132. else {
  133. fprintf(stderr, "Nviz: %s\n", msg);
  134. }
  135. return 0;
  136. }
  137. /*!
  138. \brief Print percentage information
  139. \param x value
  140. \return 0
  141. */
  142. int print_percent(int x)
  143. {
  144. char msg[256];
  145. if (logStream) {
  146. sprintf(msg, "GRASS_INFO_PERCENT: %d\n", x);
  147. PyFile_WriteString(msg, logStream);
  148. }
  149. else {
  150. fprintf(stderr, "GRASS_INFO_PERCENT: %d\n", x);
  151. }
  152. return 0;
  153. }