surface.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. \file surface.cpp
  3. \brief Experimental C++ wxWidgets Nviz prototype -- surface attributes
  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. /*!
  14. \brief Set surface topography
  15. \param id surface id
  16. \param map if true use map otherwise constant
  17. \param value map name of value
  18. \return 1 on success
  19. \return 0 on failure
  20. */
  21. int Nviz::SetSurfaceTopo(int id, bool map, const char *value)
  22. {
  23. return SetSurfaceAttr(id, ATT_TOPO, map, value);
  24. }
  25. /*!
  26. \brief Set surface color
  27. \param id surface id
  28. \param map if true use map otherwise constant
  29. \param value map name of value
  30. \return 1 on success
  31. \return 0 on failure
  32. */
  33. int Nviz::SetSurfaceColor(int id, bool map, const char *value)
  34. {
  35. return SetSurfaceAttr(id, ATT_COLOR, map, value);
  36. }
  37. /*!
  38. \brief Set surface mask
  39. @todo invert
  40. \param id surface id
  41. \param invert if true invert mask
  42. \param value map name of value
  43. \return 1 on success
  44. \return 0 on failure
  45. */
  46. int Nviz::SetSurfaceMask(int id, bool invert, const char *value)
  47. {
  48. return SetSurfaceAttr(id, ATT_MASK, true, value);
  49. }
  50. /*!
  51. \brief Set surface mask
  52. @todo invert
  53. \param id surface id
  54. \param map if true use map otherwise constant
  55. \param value map name of value
  56. \return 1 on success
  57. \return 0 on failure
  58. */
  59. int Nviz::SetSurfaceTransp(int id, bool map, const char *value)
  60. {
  61. return SetSurfaceAttr(id, ATT_TRANSP, map, value);
  62. }
  63. /*!
  64. \brief Set surface shininess
  65. \param id surface id
  66. \param map if true use map otherwise constant
  67. \param value map name of value
  68. \return 1 on success
  69. \return 0 on failure
  70. */
  71. int Nviz::SetSurfaceShine(int id, bool map, const char *value)
  72. {
  73. return SetSurfaceAttr(id, ATT_SHINE, map, value);
  74. }
  75. /*!
  76. \brief Set surface emission
  77. \param id surface id
  78. \param map if true use map otherwise constant
  79. \param value map name of value
  80. \return 1 on success
  81. \return 0 on failure
  82. */
  83. int Nviz::SetSurfaceEmit(int id, bool map, const char *value)
  84. {
  85. return SetSurfaceAttr(id, ATT_EMIT, map, value);
  86. }
  87. /*!
  88. \brief Set surface attribute
  89. \param id surface id
  90. \param attr attribute desc
  91. \param map if true use map otherwise constant
  92. \param value map name of value
  93. \return 1 on success
  94. \return 0 on failure
  95. */
  96. int Nviz::SetSurfaceAttr(int id, int attr, bool map, const char *value)
  97. {
  98. int ret;
  99. if (!GS_surf_exists(id)) {
  100. return 0;
  101. }
  102. if (map) {
  103. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  104. value, -1.0,
  105. data);
  106. }
  107. else {
  108. float val;
  109. if (attr == ATT_COLOR) {
  110. val = Nviz_color_from_str(value);
  111. }
  112. else {
  113. val = atof(value);
  114. }
  115. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  116. NULL, val,
  117. data);
  118. }
  119. G_debug(1, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  120. id, attr, map, value);
  121. return ret;
  122. }
  123. /*!
  124. \brief Unset surface mask
  125. \param id surface id
  126. \return 1 on success
  127. \return 0 on failure
  128. */
  129. int Nviz::UnsetSurfaceMask(int id)
  130. {
  131. return UnsetSurfaceAttr(id, ATT_MASK);
  132. }
  133. /*!
  134. \brief Unset surface transparency
  135. \param id surface id
  136. \return 1 on success
  137. \return 0 on failure
  138. */
  139. int Nviz::UnsetSurfaceTransp(int id)
  140. {
  141. return UnsetSurfaceAttr(id, ATT_TRANSP);
  142. }
  143. /*!
  144. \brief Unset surface emission
  145. \param id surface id
  146. \return 1 on success
  147. \return 0 on failure
  148. */
  149. int Nviz::UnsetSurfaceEmit(int id)
  150. {
  151. return UnsetSurfaceAttr(id, ATT_EMIT);
  152. }
  153. /*!
  154. \brief Unset surface attribute
  155. \param id surface id
  156. \param attr attribute descriptor
  157. \return 1 on success
  158. \return 0 on failure
  159. */
  160. int Nviz::UnsetSurfaceAttr(int id, int attr)
  161. {
  162. if (!GS_surf_exists(id)) {
  163. return 0;
  164. }
  165. G_debug(1, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  166. id, attr);
  167. return Nviz_unset_attr(id, MAP_OBJ_SURF, attr);
  168. }
  169. /*!
  170. \brief Set surface resolution
  171. \param id surface id
  172. \param fine x/y fine resolution
  173. \param coarse x/y coarse resolution
  174. \return 0 on error
  175. \return 1 on success
  176. */
  177. int Nviz::SetSurfaceRes(int id, int fine, int coarse)
  178. {
  179. G_debug(1, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  180. id, fine, coarse);
  181. if (id > 0) {
  182. if (!GS_surf_exists(id)) {
  183. return 0;
  184. }
  185. if (GS_set_drawres(id, fine, fine, coarse, coarse) < 0) {
  186. return 0;
  187. }
  188. }
  189. else {
  190. GS_setall_drawres(fine, fine, coarse, coarse);
  191. }
  192. return 1;
  193. }
  194. /*!
  195. \brief Set draw style
  196. Draw styles:
  197. - DM_GOURAUD
  198. - DM_FLAT
  199. - DM_FRINGE
  200. - DM_WIRE
  201. - DM_COL_WIRE
  202. - DM_POLY
  203. - DM_WIRE_POLY
  204. - DM_GRID_WIRE
  205. - DM_GRID_SURF
  206. \param id surface id (<= 0 for all)
  207. \param style draw style
  208. \return 1 on success
  209. \return 0 on error
  210. */
  211. int Nviz::SetSurfaceStyle(int id, int style)
  212. {
  213. G_debug(1, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  214. id, style);
  215. if (id > 0) {
  216. if (!GS_surf_exists(id)) {
  217. return 0;
  218. }
  219. if (GS_set_drawmode(id, style) < 0) {
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. if (GS_setall_drawmode(style) < 0) {
  225. return 0;
  226. }
  227. return 1;
  228. }
  229. /*!
  230. \brief Set color of wire
  231. \todo all
  232. \param surface id (< 0 for all)
  233. \param color color string (R:G:B)
  234. \return 1 on success
  235. \return 0 on failure
  236. */
  237. int Nviz::SetWireColor(int id, const char* color_str)
  238. {
  239. int color;
  240. G_debug(1, "Nviz::SetWireColor(): id=%d, color=%s",
  241. id, color_str);
  242. color = Nviz_color_from_str(color_str);
  243. if (id > 0) {
  244. if (!GS_surf_exists(id)) {
  245. return 0;
  246. }
  247. GS_set_wire_color(id, color);
  248. }
  249. else {
  250. int *surf_list, nsurfs, id;
  251. surf_list = GS_get_surf_list(&nsurfs);
  252. for (int i = 0; i < nsurfs; i++) {
  253. id = surf_list[i];
  254. GS_set_wire_color(id, color);
  255. }
  256. G_free(surf_list);
  257. }
  258. return 1;
  259. }
  260. /*!
  261. \brief Get surface position
  262. \param id surface id
  263. \return x,y,z
  264. \return zero-length vector on error
  265. */
  266. std::vector<double> Nviz::GetSurfacePosition(int id)
  267. {
  268. std::vector<double> vals;
  269. float x, y, z;
  270. if (!GS_surf_exists(id)) {
  271. return vals;
  272. }
  273. GS_get_trans(id, &x, &y, &z);
  274. G_debug(1, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  275. id, x, y, z);
  276. vals.push_back(double (x));
  277. vals.push_back(double (y));
  278. vals.push_back(double (z));
  279. return vals;
  280. }
  281. /*!
  282. \brief Set surface position
  283. \param id surface id
  284. \param x,y,z translation values
  285. \return 1 on success
  286. \return 0 on failure
  287. */
  288. int Nviz::SetSurfacePosition(int id, float x, float y, float z)
  289. {
  290. if (!GS_surf_exists(id)) {
  291. return 0;
  292. }
  293. G_debug(1, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  294. id, x, y, z);
  295. GS_set_trans(id, x, y, z);
  296. return 1;
  297. }