GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
cplanes_obj.c
Go to the documentation of this file.
1 /*!
2  \file lib/nviz/cplanes_obj.c
3 
4  \brief Nviz library -- Clip planes manipulation
5 
6  Based on visualization/nviz/src/cutplanes_obj.c
7 
8  (C) 2008, 2010 by the GRASS Development Team
9  This program is free software under the GNU General Public License
10  (>=v2). Read the file COPYING that comes with GRASS for details.
11 
12  \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
13  */
14 
15 #include <grass/nviz.h>
16 
17 static void cp_draw(nv_data *, int, int, int);
18 static geoview Gv;
19 
20 /*!
21  \brief Creates a clip plane object
22 
23  The number of clip planes is fixed (MAX_CPLANES) and
24  we'll create them all ahead of time anyway we just let
25  the user decide on the id for each.
26 
27  \param data nviz data
28  \param id
29  */
30 int Nviz_new_cplane(nv_data * data, int id)
31 {
32  data->num_cplanes++;
33  /* Initialize internal attributes for this cutplane */
34  data->cp_rot[id][X] = data->cp_rot[id][Y] = data->cp_rot[id][Z] = 0.0;
35  data->cp_trans[id][X] = data->cp_trans[id][Y] = data->cp_trans[id][Z] =
36  0.0;
37  data->cp_on[id] = 0;
38 
39  return 1;
40 }
41 
42 /*!
43  \brief Turn on (make current) the given clip plane.
44 
45  \param data nviz data
46  \param cplane id
47  */
48 int Nviz_on_cplane(nv_data * data, int id)
49 {
50  data->cur_cplane = id;
51  data->cp_on[id] = 1;
52  GS_set_cplane(id);
53 
54  return 1;
55 }
56 
57 /*!
58  \brief Turn off (make inactive) the given clip plane
59 
60  \param data nviz data
61  \param cplane id
62  */
63 int Nviz_off_cplane(nv_data * data, int id)
64 {
65  data->cp_on[id] = 0;
66  GS_unset_cplane(id);
67 
68  return 1;
69 }
70 
71 /*!
72  \brief Draw the clip plane
73 
74  \param data nviz data
75  \param bound1
76  \param bound2
77  */
78 int Nviz_draw_cplane(nv_data * data, int bound1, int bound2)
79 {
80  cp_draw(data, data->cur_cplane, bound1, bound2);
81 
82  return 1;
83 }
84 
85 /*!
86  \brief Draw current clip plane
87 
88  \param data nviz data
89  \param current id of current clip plane
90  \param surf1 first surface id
91  \param surf2 second surface id
92  */
93 void cp_draw(nv_data * data, int current, int surf1, int surf2)
94 {
95  int i, nsurfs;
96  int surf_min = 0, surf_max = 0, temp;
97  int *surf_list;
98 
100  GS_clear(data->bgcolor);
101  GS_ready_draw();
102 
103  /* If surf boundaries present then find them */
104  surf_list = GS_get_surf_list(&nsurfs);
105  if ((surf1 != -1) && (surf2 != -1)) {
106  for (i = 0; i < nsurfs; i++) {
107  if (surf_list[i] == surf1)
108  surf_min = i;
109  if (surf_list[i] == surf2)
110  surf_max = i;
111  }
112 
113  if (surf_max < surf_min) {
114  temp = surf_min;
115  surf_min = surf_max;
116  surf_max = temp;
117  }
118 
119  surf_max++;
120  }
121  else {
122  surf_min = 0;
123  surf_max = nsurfs;
124  }
125 
126  if (nsurfs > 1) {
127  for (i = 0; i < MAX_CPLANES; i++) {
128  if (data->cp_on[i])
129  GS_draw_cplane_fence(surf_list[0], surf_list[1], i);
130  }
131  }
132 
133  for (i = surf_min; i < surf_max; i++) {
134  GS_draw_wire(surf_list[i]);
135  }
136 
137  GS_done_draw();
138 
139  return;
140 }
141 /*!
142  \brief Return the number of clip planes objects currently allocated.
143 
144  \param data nviz data
145  */
147 {
148  return data->num_cplanes;
149 }
150 
151 /*!
152  \brief Get the current active cutplane.
153 
154  \param data nviz data
155  */
157 {
158  return data->cur_cplane;
159 }
160 
161 /*!
162  \brief Set the rotation for the current clip plane.
163 
164  \param data nviz data
165  \param id id of current clip plane
166  \param dx,dy,dz rotation parameters
167 
168  \return 1
169 
170  */
171 int Nviz_set_cplane_rotation(nv_data * data, int id, float dx, float dy, float dz)
172 {
173  data->cp_rot[id][X] = dx;
174  data->cp_rot[id][Y] = dy;
175  data->cp_rot[id][Z] = dz;
176  GS_set_cplane_rot(id, data->cp_rot[id][X], data->cp_rot[id][Y],
177  data->cp_rot[id][Z]);
178 
179  cp_draw(data, data->cur_cplane, -1, -1);
180 
181  return 1;
182 }
183 /*!
184  \brief Get the rotation values for the current clip plane.
185 
186  \param data nviz data
187  \param id id of current clip plane
188  \param dx,dy,dz rotation parameters
189 
190  \return 1
191 
192  */
193 int Nviz_get_cplane_rotation(nv_data * data, int id, float *dx, float *dy, float *dz)
194 {
195  *dx = data->cp_rot[id][X];
196  *dy = data->cp_rot[id][Y];
197  *dz = data->cp_rot[id][Z];
198 
199  return 1;
200 }
201 
202 /*!
203  \brief Set the translation for the current clip plane.
204 
205  \param data nviz data
206  \param id id of current clip plane
207  \param dx,dy,dz values for setting translation
208 
209  \return 1
210  */
211 int Nviz_set_cplane_translation(nv_data * data, int id, float dx, float dy, float dz)
212 {
213  data->cp_trans[id][X] = dx;
214  data->cp_trans[id][Y] = dy;
215  data->cp_trans[id][Z] = dz;
216  GS_set_cplane_trans(id, data->cp_trans[id][X], data->cp_trans[id][Y],
217  data->cp_trans[id][Z]);
218 
219  cp_draw(data, data->cur_cplane, -1, -1);
220 
221  return 1;
222 }
223 /*!
224  \brief Get the translation values for the current clip plane.
225 
226  \param data nviz data
227  \param id id of current clip plane
228  \param dx,dy,dz translation parameters
229  */
230 int Nviz_get_cplane_translation(nv_data * data, int id, float *dx, float *dy, float *dz)
231 {
232  *dx = data->cp_trans[id][X];
233  *dy = data->cp_trans[id][Y];
234  *dz = data->cp_trans[id][Z];
235 
236  return 1;
237 }
238 /*!
239  \brief Set appropriate fence color
240 
241  \param type type of fence (FC_ABOVE, FC_BELOW, FC_BLEND, FC_GREY, FC_OFF)
242  */
243 int Nviz_set_fence_color(nv_data * data, int type)
244 {
245  GS_set_fencecolor(type);
246 
247  return 1;
248 
249 }
250 int Nviz_set_cplane_here(nv_data *data, int cplane, float sx, float sy)
251 {
252  float x, y, z, len, los[2][3];
253  float dx, dy, dz;
254  float n, s, w, e;
255  Point3 realto, dir;
256  int id;
257  geosurf *gs;
258 
259  if (GS_get_selected_point_on_surface(sx, sy, &id, &x, &y, &z)) {
260  gs = gs_get_surf(id);
261  if (gs) {
262  realto[X] = x - gs->ox + gs->x_trans;
263  realto[Y] = y - gs->oy + gs->y_trans;
264  realto[Z] = z + gs->z_trans;
265  }
266  else
267  return 0;
268  }
269  else {
270  if (gsd_get_los(los, (short)sx, (short)sy)) {
271  len = GS_distance(Gv.from_to[FROM], Gv.real_to);
272  GS_v3dir(los[FROM], los[TO], dir);
273  GS_v3mult(dir, len);
274  realto[X] = Gv.from_to[FROM][X] + dir[X];
275  realto[Y] = Gv.from_to[FROM][Y] + dir[Y];
276  realto[Z] = Gv.from_to[FROM][Z] + dir[Z];
277  }
278  else
279  return 0;
280  }
281  Nviz_get_cplane_translation(data, cplane, &dx, &dy, &dz);
282 
283  GS_get_region(&n, &s, &w, &e);
284  dx = realto[X] - (e - w) / 2.;
285  dy = realto[Y] - (n - s) / 2.;
286 
287  Nviz_set_cplane_translation(data, cplane, dx, dy, dz);
288 
289  return 1;
290 }
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition: gs.c:62
int cur_cplane
Definition: nviz.h:108
#define MAX_CPLANES
Definition: ogsf.h:45
double ox
Definition: ogsf.h:264
void GS_ready_draw(void)
Definition: gs2.c:2488
int gsd_get_los(float(*)[3], short, short)
ADD.
Definition: gsd_views.c:40
int Nviz_draw_cplane(nv_data *data, int bound1, int bound2)
Draw the clip plane.
Definition: cplanes_obj.c:78
void GS_draw_wire(int)
Draw surface wire.
Definition: gs2.c:1899
int Nviz_set_fence_color(nv_data *data, int type)
Set appropriate fence color.
Definition: cplanes_obj.c:243
float Point3[3]
Definition: ogsf.h:201
void GS_set_cplane(int)
Set cplace.
Definition: gs2.c:3215
#define x
int Nviz_set_cplane_rotation(nv_data *data, int id, float dx, float dy, float dz)
Set the rotation for the current clip plane.
Definition: cplanes_obj.c:171
void GS_clear(int)
Clear view.
Definition: gs2.c:3418
int num_cplanes
Definition: nviz.h:107
int Nviz_get_cplane_rotation(nv_data *data, int id, float *dx, float *dy, float *dz)
Get the rotation values for the current clip plane.
Definition: cplanes_obj.c:193
void GS_set_cplane_rot(int, float, float, float)
Set cplace rotation.
Definition: gs2.c:3122
float cp_trans[MAX_CPLANES][3]
Definition: nviz.h:109
float GS_distance(float *, float *)
Calculate distance.
Definition: gs_util.c:141
int GS_get_selected_point_on_surface(int, int, int *, float *, float *, float *)
Get selected point of surface.
Definition: gs2.c:3054
void GS_set_cplane_trans(int, float, float, float)
Set cplace trans.
Definition: gs2.c:3135
int GS_get_region(float *, float *, float *, float *)
Get 2D region extent.
Definition: gs2.c:156
int Nviz_off_cplane(nv_data *data, int id)
Turn off (make inactive) the given clip plane.
Definition: cplanes_obj.c:63
float x_trans
Definition: ogsf.h:267
Definition: ogsf.h:478
void GS_unset_cplane(int)
Unset clip place (turn off)
Definition: gs2.c:3227
int Nviz_set_cplane_translation(nv_data *data, int id, float dx, float dy, float dz)
Set the translation for the current clip plane.
Definition: cplanes_obj.c:211
int Nviz_get_current_cplane(nv_data *data)
Get the current active cutplane.
Definition: cplanes_obj.c:156
int * GS_get_surf_list(int *)
Get surface list.
Definition: gs2.c:1539
#define FROM
Definition: ogsf.h:141
void GS_done_draw(void)
Draw done, swap buffers.
Definition: gs2.c:2501
#define Z
Definition: ogsf.h:139
int GS_draw_cplane_fence(int, int, int)
Draw cplace fence ?
Definition: gs2.c:3175
double oy
Definition: ogsf.h:264
int Nviz_on_cplane(nv_data *data, int id)
Turn on (make current) the given clip plane.
Definition: cplanes_obj.c:48
int cp_on[MAX_CPLANES]
Definition: nviz.h:108
int Nviz_num_cplanes(nv_data *data)
Return the number of clip planes objects currently allocated.
Definition: cplanes_obj.c:146
#define GSD_BACK
Definition: ogsf.h:102
float y_trans
Definition: ogsf.h:267
#define Y
Definition: ogsf.h:138
Definition: nviz.h:101
float real_to[4]
Definition: ogsf.h:486
int Nviz_set_cplane_here(nv_data *data, int cplane, float sx, float sy)
Definition: cplanes_obj.c:250
#define TO
Definition: ogsf.h:142
int Nviz_new_cplane(nv_data *data, int id)
Creates a clip plane object.
Definition: cplanes_obj.c:30
void GS_set_fencecolor(int)
Set fence color.
Definition: gs2.c:3256
int GS_v3dir(float *, float *, float *)
Get a normalized direction from v1 to v2, store in v3.
Definition: gs_util.c:353
#define X
Definition: ogsf.h:137
float cp_rot[MAX_CPLANES][3]
Definition: nviz.h:110
float z_trans
Definition: ogsf.h:267
void GS_set_draw(int)
Sets which buffer to draw to.
Definition: gs2.c:2462
int Nviz_get_cplane_translation(nv_data *data, int id, float *dx, float *dy, float *dz)
Get the translation values for the current clip plane.
Definition: cplanes_obj.c:230
Definition: ogsf.h:257
int bgcolor
Definition: nviz.h:128
float from_to[2][4]
Definition: ogsf.h:483
void GS_v3mult(float *, float)
Multiple vectors.
Definition: gs_util.c:229