GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-55b2a2bccc
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
13  2008/2010)
14  */
15 
16 #include <grass/nviz.h>
17 
18 static void cp_draw(nv_data *, int, int, int);
19 static geoview Gv;
20 
21 /*!
22  \brief Creates a clip plane object
23 
24  The number of clip planes is fixed (MAX_CPLANES) and
25  we'll create them all ahead of time anyway we just let
26  the user decide on the id for each.
27 
28  \param data nviz data
29  \param id
30  */
31 int Nviz_new_cplane(nv_data *data, int id)
32 {
33  data->num_cplanes++;
34  /* Initialize internal attributes for this cutplane */
35  data->cp_rot[id][X] = data->cp_rot[id][Y] = data->cp_rot[id][Z] = 0.0;
36  data->cp_trans[id][X] = data->cp_trans[id][Y] = data->cp_trans[id][Z] = 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 [unused]
90  \param surf1 first surface id
91  \param surf2 second surface id
92  */
93 void cp_draw(nv_data *data, int current UNUSED, 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 /*!
143  \brief Return the number of clip planes objects currently allocated.
144 
145  \param data nviz data
146  */
148 {
149  return data->num_cplanes;
150 }
151 
152 /*!
153  \brief Get the current active cutplane.
154 
155  \param data nviz data
156  */
158 {
159  return data->cur_cplane;
160 }
161 
162 /*!
163  \brief Set the rotation for the current clip plane.
164 
165  \param data nviz data
166  \param id id of current clip plane
167  \param dx,dy,dz rotation parameters
168 
169  \return 1
170 
171  */
172 int Nviz_set_cplane_rotation(nv_data *data, int id, float dx, float dy,
173  float dz)
174 {
175  data->cp_rot[id][X] = dx;
176  data->cp_rot[id][Y] = dy;
177  data->cp_rot[id][Z] = dz;
178  GS_set_cplane_rot(id, data->cp_rot[id][X], data->cp_rot[id][Y],
179  data->cp_rot[id][Z]);
180 
181  cp_draw(data, data->cur_cplane, -1, -1);
182 
183  return 1;
184 }
185 
186 /*!
187  \brief Get the rotation values for the current clip plane.
188 
189  \param data nviz data
190  \param id id of current clip plane
191  \param dx,dy,dz rotation parameters
192 
193  \return 1
194 
195  */
196 int Nviz_get_cplane_rotation(nv_data *data, int id, float *dx, float *dy,
197  float *dz)
198 {
199  *dx = data->cp_rot[id][X];
200  *dy = data->cp_rot[id][Y];
201  *dz = data->cp_rot[id][Z];
202 
203  return 1;
204 }
205 
206 /*!
207  \brief Set the translation for the current clip plane.
208 
209  \param data nviz data
210  \param id id of current clip plane
211  \param dx,dy,dz values for setting translation
212 
213  \return 1
214  */
215 int Nviz_set_cplane_translation(nv_data *data, int id, float dx, float dy,
216  float dz)
217 {
218  data->cp_trans[id][X] = dx;
219  data->cp_trans[id][Y] = dy;
220  data->cp_trans[id][Z] = dz;
221  GS_set_cplane_trans(id, data->cp_trans[id][X], data->cp_trans[id][Y],
222  data->cp_trans[id][Z]);
223 
224  cp_draw(data, data->cur_cplane, -1, -1);
225 
226  return 1;
227 }
228 
229 /*!
230  \brief Get the translation values for the current clip plane.
231 
232  \param data nviz data
233  \param id id of current clip plane
234  \param dx,dy,dz translation parameters
235  */
236 int Nviz_get_cplane_translation(nv_data *data, int id, float *dx, float *dy,
237  float *dz)
238 {
239  *dx = data->cp_trans[id][X];
240  *dy = data->cp_trans[id][Y];
241  *dz = data->cp_trans[id][Z];
242 
243  return 1;
244 }
245 
246 /*!
247  \brief Set appropriate fence color
248 
249  \param type type of fence (FC_ABOVE, FC_BELOW, FC_BLEND, FC_GREY, FC_OFF)
250  */
251 int Nviz_set_fence_color(nv_data *data UNUSED, int type)
252 {
253  GS_set_fencecolor(type);
254 
255  return 1;
256 }
257 
258 int Nviz_set_cplane_here(nv_data *data, int cplane, float sx, float sy)
259 {
260  float x, y, z, len, los[2][3];
261  float dx, dy, dz;
262  float n, s, w, e;
263  Point3 realto, dir;
264  int id;
265  geosurf *gs;
266 
267  if (GS_get_selected_point_on_surface(sx, sy, &id, &x, &y, &z)) {
268  gs = gs_get_surf(id);
269  if (gs) {
270  realto[X] = x - gs->ox + gs->x_trans;
271  realto[Y] = y - gs->oy + gs->y_trans;
272  realto[Z] = z + gs->z_trans;
273  }
274  else
275  return 0;
276  }
277  else {
278  if (gsd_get_los(los, (short)sx, (short)sy)) {
279  len = GS_distance(Gv.from_to[FROM], Gv.real_to);
280  GS_v3dir(los[FROM], los[TO], dir);
281  GS_v3mult(dir, len);
282  realto[X] = Gv.from_to[FROM][X] + dir[X];
283  realto[Y] = Gv.from_to[FROM][Y] + dir[Y];
284  realto[Z] = Gv.from_to[FROM][Z] + dir[Z];
285  }
286  else
287  return 0;
288  }
289  Nviz_get_cplane_translation(data, cplane, &dx, &dy, &dz);
290 
291  GS_get_region(&n, &s, &w, &e);
292  dx = realto[X] - (e - w) / 2.;
293  dy = realto[Y] - (n - s) / 2.;
294 
295  Nviz_set_cplane_translation(data, cplane, dx, dy, dz);
296 
297  return 1;
298 }
int Nviz_set_fence_color(nv_data *data UNUSED, int type)
Set appropriate fence color.
Definition: cplanes_obj.c:251
int Nviz_set_cplane_here(nv_data *data, int cplane, float sx, float sy)
Definition: cplanes_obj.c:258
int Nviz_get_current_cplane(nv_data *data)
Get the current active cutplane.
Definition: cplanes_obj.c:157
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:236
int Nviz_draw_cplane(nv_data *data, int bound1, int bound2)
Draw the clip plane.
Definition: cplanes_obj.c:78
void cp_draw(nv_data *data, int current UNUSED, int surf1, int surf2)
Draw current clip plane.
Definition: cplanes_obj.c:93
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:172
int Nviz_off_cplane(nv_data *data, int id)
Turn off (make inactive) the given clip plane.
Definition: cplanes_obj.c:63
int Nviz_num_cplanes(nv_data *data)
Return the number of clip planes objects currently allocated.
Definition: cplanes_obj.c:147
int Nviz_new_cplane(nv_data *data, int id)
Creates a clip plane object.
Definition: cplanes_obj.c:31
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:215
int Nviz_on_cplane(nv_data *data, int id)
Turn on (make current) the given clip plane.
Definition: cplanes_obj.c:48
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:196
void GS_v3mult(float *, float)
Multiple vectors.
Definition: gs_util.c:229
int GS_get_region(float *, float *, float *, float *)
Get 2D region extent.
Definition: gs2.c:156
int * GS_get_surf_list(int *)
Get surface list.
Definition: gs2.c:1530
void GS_set_draw(int)
Sets which buffer to draw to.
Definition: gs2.c:2459
int GS_get_selected_point_on_surface(int, int, int *, float *, float *, float *)
Get selected point of surface.
Definition: gs2.c:3051
void GS_unset_cplane(int)
Unset clip place (turn off)
Definition: gs2.c:3223
int GS_draw_cplane_fence(int, int, int)
Draw cplace fence ?
Definition: gs2.c:3171
void GS_set_cplane(int)
Set cplace.
Definition: gs2.c:3211
void GS_ready_draw(void)
Definition: gs2.c:2485
void GS_set_cplane_rot(int, float, float, float)
Set cplace rotation.
Definition: gs2.c:3119
void GS_draw_wire(int)
Draw surface wire.
Definition: gs2.c:1897
void GS_clear(int)
Clear view.
Definition: gs2.c:3414
int GS_v3dir(float *, float *, float *)
Get a normalized direction from v1 to v2, store in v3.
Definition: gs_util.c:351
int gsd_get_los(float(*)[3], short, short)
ADD.
Definition: gsd_views.c:40
void GS_done_draw(void)
Draw done, swap buffers.
Definition: gs2.c:2498
void GS_set_fencecolor(int)
Set fence color.
Definition: gs2.c:3252
float GS_distance(float *, float *)
Calculate distance.
Definition: gs_util.c:141
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition: gs.c:63
void GS_set_cplane_trans(int, float, float, float)
Set cplace trans.
Definition: gs2.c:3132
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define MAX_CPLANES
Definition: ogsf.h:47
#define X
Definition: ogsf.h:140
float Point3[3]
Definition: ogsf.h:205
#define Z
Definition: ogsf.h:142
#define Y
Definition: ogsf.h:141
#define GSD_BACK
Definition: ogsf.h:105
#define TO
Definition: ogsf.h:145
@ FROM
Definition: sqlp.tab.h:67
Definition: ogsf.h:256
float x_trans
Definition: ogsf.h:266
double ox
Definition: ogsf.h:263
double oy
Definition: ogsf.h:263
float z_trans
Definition: ogsf.h:266
float y_trans
Definition: ogsf.h:266
Definition: ogsf.h:467
float real_to[4]
Definition: ogsf.h:474
float from_to[2][4]
Definition: ogsf.h:471
Definition: nviz.h:97
float cp_rot[MAX_CPLANES][3]
Definition: nviz.h:105
int cp_on[MAX_CPLANES]
Definition: nviz.h:103
int cur_cplane
Definition: nviz.h:103
int num_cplanes
Definition: nviz.h:102
float cp_trans[MAX_CPLANES][3]
Definition: nviz.h:104
int bgcolor
Definition: nviz.h:123
#define x