init.cpp 4.1 KB

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