BRL-CAD
grid.c
Go to the documentation of this file.
1 /* G R I D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1998-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libdm/grid.c
21  *
22  * Routines to implement MGED's snap to grid capability.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <math.h>
29 
30 #include "vmath.h"
31 #include "dm.h"
32 #include "dm_private.h"
33 
34 void
35 dm_draw_grid(dm *dmp, struct bview_grid_state *ggsp, fastf_t scale, mat_t model2view, fastf_t base2local)
36 {
37  int i, j;
38  int nh, nv;
39  int nv_dots, nh_dots;
40  fastf_t fx, fy;
41  fastf_t sf;
42  fastf_t inv_sf;
43  point_t view_grid_anchor;
44  point_t view_lleft_corner;
45  point_t view_grid_anchor_local;
46  point_t view_lleft_corner_local;
47  point_t view_grid_start_pt_local;
48  fastf_t inv_grid_res_h;
49  fastf_t inv_grid_res_v;
50  fastf_t inv_aspect;
51 
52  if (ZERO(ggsp->res_h) ||
53  ZERO(ggsp->res_v))
54  return;
55 
56  inv_grid_res_h= 1.0 / (ggsp->res_h * base2local);
57  inv_grid_res_v= 1.0 / (ggsp->res_v * base2local);
58 
59  sf = scale*base2local;
60 
61  /* sanity - don't draw the grid if it would fill the screen */
62  {
63  fastf_t pixel_size = 2.0 * sf / dmp->dm_width;
64  if ( (ggsp->res_h*base2local) < pixel_size || (ggsp->res_v*base2local) < pixel_size )
65  return;
66  }
67 
68  inv_sf = 1.0 / sf;
69  inv_aspect = 1.0 / dmp->dm_aspect;
70 
71  nv_dots = 2.0 * inv_aspect * sf * inv_grid_res_v + (2 * ggsp->res_major_v);
72  nh_dots = 2.0 * sf * inv_grid_res_h + (2 * ggsp->res_major_h);
73  MAT4X3PNT(view_grid_anchor, model2view, ggsp->anchor);
74  VSCALE(view_grid_anchor_local, view_grid_anchor, sf);
75 
76  VSET(view_lleft_corner, -1.0, -inv_aspect, 0.0);
77  VSCALE(view_lleft_corner_local, view_lleft_corner, sf);
78  nh = (view_grid_anchor_local[X] - view_lleft_corner_local[X]) * inv_grid_res_h;
79  nv = (view_grid_anchor_local[Y] - view_lleft_corner_local[Y]) * inv_grid_res_v;
80 
81  {
82  int nmh, nmv;
83 
84  nmh = nh / ggsp->res_major_h + 1;
85  nmv = nv / ggsp->res_major_v + 1;
86  VSET(view_grid_start_pt_local,
87  view_grid_anchor_local[X] - (nmh * ggsp->res_h * ggsp->res_major_h * base2local),
88  view_grid_anchor_local[Y] - (nmv * ggsp->res_v * ggsp->res_major_v * base2local),
89  0.0);
90  }
91 
92  dm_set_fg(dmp,
93  ggsp->color[0],
94  ggsp->color[1],
95  ggsp->color[2], 1, 1.0);
96  dm_set_line_attr(dmp, 1, 0); /* solid lines */
97 
98  /* draw horizontal dots */
99  for (i = 0; i < nv_dots; i += ggsp->res_major_v) {
100  fy = (view_grid_start_pt_local[Y] + (i * ggsp->res_v * base2local)) * inv_sf;
101 
102  for (j = 0; j < nh_dots; ++j) {
103  fx = (view_grid_start_pt_local[X] + (j * ggsp->res_h * base2local)) * inv_sf;
104  dm_draw_point_2d(dmp, fx, fy * dmp->dm_aspect);
105  }
106  }
107 
108  /* draw vertical dots */
109  if (ggsp->res_major_v != 1) {
110  for (i = 0; i < nh_dots; i += ggsp->res_major_h) {
111  fx = (view_grid_start_pt_local[X] + (i * ggsp->res_h * base2local)) * inv_sf;
112 
113  for (j = 0; j < nv_dots; ++j) {
114  fy = (view_grid_start_pt_local[Y] + (j * ggsp->res_v * base2local)) * inv_sf;
115  dm_draw_point_2d(dmp, fx, fy * dmp->dm_aspect);
116  }
117  }
118  }
119 }
120 
121 
122 /*
123  * Local Variables:
124  * mode: C
125  * tab-width: 8
126  * indent-tabs-mode: t
127  * c-file-style: "stroustrup"
128  * End:
129  * ex: shiftwidth=4 tabstop=8
130  */
int color[3]
Definition: bview.h:114
int res_major_v
Definition: bview.h:113
int dm_width
Definition: dm_private.h:91
#define VSET(a, b, c, d)
Definition: color.c:53
fastf_t anchor[3]
Definition: bview.h:109
Header file for the BRL-CAD common definitions.
fastf_t dm_aspect
Definition: dm_private.h:97
int res_major_h
Definition: bview.h:112
Definition: color.c:49
fastf_t res_h
Definition: bview.h:110
fastf_t res_v
Definition: bview.h:111
int dm_set_fg(dm *dmp, unsigned char r, unsigned char g, unsigned char b, int strict, fastf_t transparency)
Definition: dm-generic.c:441
int dm_draw_point_2d(dm *dmp, fastf_t x, fastf_t y)
Definition: dm-generic.c:760
#define ZERO(val)
Definition: units.c:38
int dm_set_line_attr(dm *dmp, int width, int style)
Definition: dm-generic.c:617
double fastf_t
Definition: defines.h:300
void dm_draw_grid(dm *dmp, struct bview_grid_state *ggsp, fastf_t scale, mat_t model2view, fastf_t base2local)
Definition: grid.c:35
Definition: color.c:50