init.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 void print_sentence (PyObject*, const int, const char *);
  19. static PyObject *logStream;
  20. static int message_id = 1;
  21. /*!
  22. \brief Initialize Nviz class instance
  23. */
  24. Nviz::Nviz(PyObject *log)
  25. {
  26. G_gisinit(""); /* GRASS functions */
  27. logStream = log;
  28. G_set_error_routine(&print_error);
  29. GS_libinit();
  30. /* GVL_libinit(); TODO */
  31. GS_set_swap_func(swap_gl);
  32. data = (nv_data*) G_malloc(sizeof (nv_data));
  33. /* GLCanvas */
  34. glCanvas = NULL;
  35. G_debug(1, "Nviz::Nviz()");
  36. }
  37. /*!
  38. \brief Destroy Nviz class instance
  39. */
  40. Nviz::~Nviz()
  41. {
  42. G_free((void *) data);
  43. data = NULL;
  44. glCanvas = NULL;
  45. logStream = NULL;
  46. }
  47. /*!
  48. \brief Associate display with render window
  49. \return 1 on success
  50. \return 0 on failure
  51. */
  52. int Nviz::SetDisplay(void *display)
  53. {
  54. glCanvas = (wxGLCanvas *) display;
  55. // glCanvas->SetCurrent();
  56. G_debug(1, "Nviz::SetDisplay()");
  57. return 1;
  58. }
  59. void Nviz::InitView()
  60. {
  61. /* initialize nviz data */
  62. Nviz_init_data(data);
  63. /* define default attributes for map objects */
  64. Nviz_set_surface_attr_default();
  65. /* set background color */
  66. Nviz_set_bgcolor(data, Nviz_color_from_str("white")); /* TODO */
  67. /* initialize view */
  68. Nviz_init_view();
  69. /* set default lighting model */
  70. SetLightsDefault();
  71. /* clear window */
  72. GS_clear(data->bgcolor);
  73. G_debug(1, "Nviz::InitView()");
  74. return;
  75. }
  76. /*!
  77. \brief Reset session
  78. Unload all data layers
  79. @todo vector, volume
  80. */
  81. void Nviz::Reset()
  82. {
  83. int i;
  84. int *surf_list, nsurfs;
  85. surf_list = GS_get_surf_list(&nsurfs);
  86. for (i = 0; i < nsurfs; i++) {
  87. GS_delete_surface(surf_list[i]);
  88. }
  89. }
  90. void swap_gl()
  91. {
  92. return;
  93. }
  94. /*!
  95. \brief Set background color
  96. \param color_str color string
  97. */
  98. void Nviz::SetBgColor(const char *color_str)
  99. {
  100. data->bgcolor = Nviz_color_from_str(color_str);
  101. return;
  102. }
  103. int print_error(const char *msg, const int type)
  104. {
  105. if (logStream) {
  106. print_sentence(logStream, type, msg);
  107. }
  108. else {
  109. fprintf(stderr, "Nviz: %s\n", msg);
  110. }
  111. return 0;
  112. }
  113. /*
  114. \brief Print one message, prefix inserted before each new line
  115. From lib/gis/error.c
  116. */
  117. void print_sentence (PyObject *pyFd, const int type, const char *msg)
  118. {
  119. char prefix[256];
  120. const char *start;
  121. char* sentence;
  122. switch ( type ) {
  123. case MSG:
  124. sprintf (prefix, "GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
  125. break;
  126. case WARN:
  127. sprintf (prefix, "GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
  128. break;
  129. case ERR:
  130. sprintf (prefix, "GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
  131. break;
  132. }
  133. start = msg;
  134. PyFile_WriteString("\n", pyFd);
  135. while ( *start != '\0' ) {
  136. const char *next = start;
  137. PyFile_WriteString(prefix, pyFd);
  138. while ( *next != '\0' ) {
  139. next++;
  140. if ( *next == '\n' ) {
  141. next++;
  142. break;
  143. }
  144. }
  145. sentence = (char *) G_malloc ((next - start + 1) * sizeof (char));
  146. strncpy(sentence, start, next - start + 1);
  147. sentence[next-start] = '\0';
  148. PyFile_WriteString(sentence, pyFd);
  149. G_free((void *)sentence);
  150. PyFile_WriteString("\n", pyFd);
  151. start = next;
  152. }
  153. PyFile_WriteString("\n", pyFd);
  154. sprintf(prefix, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
  155. PyFile_WriteString(prefix, pyFd);
  156. message_id++;
  157. }