BRL-CAD
adc.c
Go to the documentation of this file.
1 /* A D C . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-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/adc.c
21  *
22  */
23 
24 #include "common.h"
25 
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29 
30 #include "bn.h"
31 #include "vmath.h"
32 #include "dm.h"
33 #include "dm/bview.h"
34 #include "dm_private.h"
35 
36 static void
37 dm_draw_ticks(dm *dmp, struct bview_adc_state *adcp, fastf_t angle)
38 {
39  fastf_t c_tdist;
40  fastf_t d1, d2;
41  fastf_t t1, t2;
42  fastf_t x1, Y1; /* not "y1", due to conflict with math lib */
43  fastf_t x2, y2;
44 
45  /*
46  * Position tic marks from dial 9.
47  */
48  /* map -2048 - 2047 into 0 - 2048 * sqrt (2) */
49  /* Tick distance */
50  c_tdist = ((fastf_t)(adcp->dv_dist) + GED_MAX) * M_SQRT1_2;
51 
52  d1 = c_tdist * cos (angle);
53  d2 = c_tdist * sin (angle);
54  t1 = 20.0 * sin (angle);
55  t2 = 20.0 * cos (angle);
56 
57  /* Quadrant 1 */
58  x1 = adcp->dv_x + d1 + t1;
59  Y1 = adcp->dv_y + d2 - t2;
60  x2 = adcp->dv_x + d1 -t1;
61  y2 = adcp->dv_y + d2 + t2;
62  if (bn_lseg_clip(&x1, &Y1, &x2, &y2, DM_MIN, DM_MAX) == 0) {
63  dm_draw_line_2d(dmp,
64  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
65  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
66  }
67 
68  /* Quadrant 2 */
69  x1 = adcp->dv_x - d2 + t2;
70  Y1 = adcp->dv_y + d1 + t1;
71  x2 = adcp->dv_x - d2 - t2;
72  y2 = adcp->dv_y + d1 - t1;
73  if (bn_lseg_clip(&x1, &Y1, &x2, &y2, DM_MIN, DM_MAX) == 0) {
74  dm_draw_line_2d(dmp,
75  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
76  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
77  }
78 
79  /* Quadrant 3 */
80  x1 = adcp->dv_x - d1 - t1;
81  Y1 = adcp->dv_y - d2 + t2;
82  x2 = adcp->dv_x - d1 + t1;
83  y2 = adcp->dv_y - d2 - t2;
84  if (bn_lseg_clip(&x1, &Y1, &x2, &y2, DM_MIN, DM_MAX) == 0) {
85  dm_draw_line_2d(dmp,
86  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
87  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
88  }
89 
90  /* Quadrant 4 */
91  x1 = adcp->dv_x + d2 - t2;
92  Y1 = adcp->dv_y - d1 - t1;
93  x2 = adcp->dv_x + d2 + t2;
94  y2 = adcp->dv_y - d1 + t1;
95  if (bn_lseg_clip(&x1, &Y1, &x2, &y2, DM_MIN, DM_MAX) == 0) {
96  dm_draw_line_2d(dmp,
97  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
98  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
99  }
100 }
101 
102 
103 /**
104  * Compute and display the angle/distance cursor.
105  */
106 void
107 dm_draw_adc(dm *dmp, struct bview_adc_state *adcp, mat_t view2model, mat_t model2view)
108 {
109  fastf_t x1, Y1; /* not "y1", due to conflict with math lib */
110  fastf_t x2, y2;
111  fastf_t x3, y3;
112  fastf_t x4, y4;
113  fastf_t d1, d2;
114  fastf_t angle1, angle2;
115 
116  if (adcp->anchor_pos == 1) {
117  adc_model_to_adc_view(adcp, model2view, GED_MAX);
118  adc_view_to_adc_grid(adcp, model2view);
119  } else if (adcp->anchor_pos == 2) {
120  adc_grid_to_adc_view(adcp, view2model, GED_MAX);
121  MAT4X3PNT(adcp->pos_model, view2model, adcp->pos_view);
122  } else {
123  adc_view_to_adc_grid(adcp, model2view);
124  MAT4X3PNT(adcp->pos_model, view2model, adcp->pos_view);
125  }
126 
127  if (adcp->anchor_a1) {
128  fastf_t dx, dy;
129  point_t view_pt;
130 
131  MAT4X3PNT(view_pt, model2view, adcp->anchor_pt_a1);
132  dx = view_pt[X] * GED_MAX - adcp->dv_x;
133  dy = view_pt[Y] * GED_MAX - adcp->dv_y;
134 
135  if (!ZERO(dx) || !ZERO(dy)) {
136  adcp->a1 = RAD2DEG*atan2(dy, dx);
137  adcp->dv_a1 = (1.0 - (adcp->a1 / 45.0)) * GED_MAX;
138  }
139  }
140 
141  if (adcp->anchor_a2) {
142  fastf_t dx, dy;
143  point_t view_pt;
144 
145  MAT4X3PNT(view_pt, model2view, adcp->anchor_pt_a2);
146  dx = view_pt[X] * GED_MAX - adcp->dv_x;
147  dy = view_pt[Y] * GED_MAX - adcp->dv_y;
148 
149  if (!ZERO(dx) || !ZERO(dy)) {
150  adcp->a2 = RAD2DEG*atan2(dy, dx);
151  adcp->dv_a2 = (1.0 - (adcp->a2 / 45.0)) * GED_MAX;
152  }
153  }
154 
155  if (adcp->anchor_dst) {
156  fastf_t dist;
157  fastf_t dx, dy;
158  point_t view_pt;
159 
160  MAT4X3PNT(view_pt, model2view, adcp->anchor_pt_dst);
161 
162  dx = view_pt[X] * GED_MAX - adcp->dv_x;
163  dy = view_pt[Y] * GED_MAX - adcp->dv_y;
164  dist = sqrt(dx * dx + dy * dy);
165  adcp->dst = dist * INV_GED;
166  adcp->dv_dist = (dist / M_SQRT1_2) - GED_MAX;
167  } else
168  adcp->dst = (adcp->dv_dist * INV_GED + 1.0) * M_SQRT1_2;
169 
170  dm_set_fg(dmp,
171  adcp->line_color[0],
172  adcp->line_color[1],
173  adcp->line_color[2], 1, 1.0);
174  dm_set_line_attr(dmp, adcp->line_width, 0);
175 
176  /* Horizontal */
177  dm_draw_line_2d(dmp,
178  GED_TO_PM1(GED_MIN), GED_TO_PM1(adcp->dv_y) * dmp->dm_aspect,
179  GED_TO_PM1(GED_MAX), GED_TO_PM1(adcp->dv_y) * dmp->dm_aspect);
180 
181  /* Vertical */
182  dm_draw_line_2d(dmp,
184  GED_TO_PM1(adcp->dv_x), GED_TO_PM1(GED_MIN));
185 
186  angle1 = adcp->a1 * DEG2RAD;
187  angle2 = adcp->a2 * DEG2RAD;
188 
189  /* sin for X and cos for Y to reverse sense of knob */
190  d1 = cos (angle1) * 8000.0;
191  d2 = sin (angle1) * 8000.0;
192  x1 = adcp->dv_x + d1;
193  Y1 = adcp->dv_y + d2;
194  x2 = adcp->dv_x - d1;
195  y2 = adcp->dv_y - d2;
196 
197  x3 = adcp->dv_x + d2;
198  y3 = adcp->dv_y - d1;
199  x4 = adcp->dv_x - d2;
200  y4 = adcp->dv_y + d1;
201 
202  dm_draw_line_2d(dmp,
203  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
204  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
205  dm_draw_line_2d(dmp,
206  GED_TO_PM1(x3), GED_TO_PM1(y3) * dmp->dm_aspect,
207  GED_TO_PM1(x4), GED_TO_PM1(y4) * dmp->dm_aspect);
208 
209  d1 = cos(angle2) * 8000.0;
210  d2 = sin(angle2) * 8000.0;
211  x1 = adcp->dv_x + d1;
212  Y1 = adcp->dv_y + d2;
213  x2 = adcp->dv_x - d1;
214  y2 = adcp->dv_y - d2;
215 
216  x3 = adcp->dv_x + d2;
217  y3 = adcp->dv_y - d1;
218  x4 = adcp->dv_x - d2;
219  y4 = adcp->dv_y + d1;
220 
221  dm_set_line_attr(dmp, adcp->line_width, 1);
222  dm_draw_line_2d(dmp,
223  GED_TO_PM1(x1), GED_TO_PM1(Y1) * dmp->dm_aspect,
224  GED_TO_PM1(x2), GED_TO_PM1(y2) * dmp->dm_aspect);
225  dm_draw_line_2d(dmp,
226  GED_TO_PM1(x3), GED_TO_PM1(y3) * dmp->dm_aspect,
227  GED_TO_PM1(x4), GED_TO_PM1(y4) * dmp->dm_aspect);
228  dm_set_line_attr(dmp, adcp->line_width, 0);
229 
230  dm_set_fg(dmp,
231  adcp->tick_color[0],
232  adcp->tick_color[1],
233  adcp->tick_color[2], 1, 1.0);
234  dm_draw_ticks(dmp, adcp, 0.0);
235  dm_draw_ticks(dmp, adcp, angle1);
236  dm_draw_ticks(dmp, adcp, angle2);
237 }
238 
239 
240 /*
241  * Local Variables:
242  * mode: C
243  * tab-width: 8
244  * indent-tabs-mode: t
245  * c-file-style: "stroustrup"
246  * End:
247  * ex: shiftwidth=4 tabstop=8
248  */
#define GED_MAX
Definition: ged.h:253
int anchor_a1
Definition: bview.h:67
void adc_model_to_adc_view(struct bview_adc_state *adcs, mat_t model2view, fastf_t amax)
Definition: clip.c:192
fastf_t a2
Definition: bview.h:64
void adc_grid_to_adc_view(struct bview_adc_state *adcs, mat_t model2view, fastf_t amax)
Definition: clip.c:201
Header file for the BRL-CAD common definitions.
int dv_dist
Definition: bview.h:59
int anchor_pos
Definition: bview.h:66
fastf_t dm_aspect
Definition: dm_private.h:97
#define GED_TO_PM1(x)
Definition: dm.h:67
fastf_t a1
Definition: bview.h:63
Definition: color.c:49
fastf_t anchor_pt_a1[3]
Definition: bview.h:70
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
fastf_t anchor_pt_dst[3]
Definition: bview.h:72
fastf_t dst
Definition: bview.h:65
void dm_draw_adc(dm *dmp, struct bview_adc_state *adcp, mat_t view2model, mat_t model2view)
Definition: adc.c:107
int tick_color[3]
Definition: bview.h:74
int line_width
Definition: bview.h:75
int line_color[3]
Definition: bview.h:73
#define GED_MIN
Definition: ged.h:254
int bn_lseg_clip(fastf_t *xp1, fastf_t *yp1, fastf_t *xp2, fastf_t *yp2, fastf_t clip_min, fastf_t clip_max)
Clipping functions.
Definition: clip.c:61
#define DM_MAX
Definition: dm.h:45
fastf_t anchor_pt_a2[3]
Definition: bview.h:71
#define ZERO(val)
Definition: units.c:38
fastf_t pos_view[3]
Definition: bview.h:61
#define DM_MIN
Definition: dm.h:44
int dm_set_line_attr(dm *dmp, int width, int style)
Definition: dm-generic.c:617
#define INV_GED
Definition: clip.c:226
fastf_t pos_model[3]
Definition: bview.h:60
int dm_draw_line_2d(dm *dmp, fastf_t x1, fastf_t y1_2d, fastf_t x2, fastf_t y2)
Definition: dm-generic.c:741
int anchor_dst
Definition: bview.h:69
#define M_SQRT1_2
Definition: fft.h:38
double fastf_t
Definition: defines.h:300
void adc_view_to_adc_grid(struct bview_adc_state *adcs, mat_t model2view)
Definition: clip.c:215
int anchor_a2
Definition: bview.h:68
Definition: color.c:50