BRL-CAD
scale_tgc.c
Go to the documentation of this file.
1 /* S C A L E _ T G C . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library 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 library 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 libged/scale_tgc.c
21  *
22  * The scale_tgc command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "rtgeom.h"
32 #include "raytrace.h"
33 
34 #include "./ged_private.h"
35 
36 
37 int
38 _ged_scale_tgc(struct ged *gedp, struct rt_tgc_internal *tgc, const char *attribute, fastf_t sf, int rflag)
39 {
40  fastf_t ma, mb;
41 
42  RT_TGC_CK_MAGIC(tgc);
43 
44  switch (attribute[0]) {
45  case 'a':
46  case 'A':
47  if (!rflag)
48  sf /= MAGNITUDE(tgc->a);
49 
50  switch (attribute[1]) {
51  case '\0':
52  VSCALE(tgc->a, tgc->a, sf);
53  break;
54  case 'b':
55  case 'B':
56  switch (attribute[2]) {
57  case '\0':
58  VSCALE(tgc->a, tgc->a, sf);
59  ma = MAGNITUDE(tgc->a);
60  mb = MAGNITUDE(tgc->b);
61  VSCALE(tgc->b, tgc->b, ma/mb);
62 
63  break;
64  case 'c':
65  case 'C':
66  if ((attribute[3] == 'd' || attribute[3] == 'D') &&
67  attribute[4] == '\0') {
68  VSCALE(tgc->a, tgc->a, sf);
69  ma = MAGNITUDE(tgc->a);
70  mb = MAGNITUDE(tgc->b);
71  VSCALE(tgc->b, tgc->b, ma/mb);
72  mb = MAGNITUDE(tgc->c);
73  VSCALE(tgc->c, tgc->c, ma/mb);
74  mb = MAGNITUDE(tgc->d);
75  VSCALE(tgc->d, tgc->d, ma/mb);
76  } else {
77  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
78  return GED_ERROR;
79  }
80 
81  break;
82  default:
83  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
84  return GED_ERROR;
85  }
86 
87  break;
88  default:
89  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
90  return GED_ERROR;
91  }
92 
93  break;
94  case 'b':
95  case 'B':
96  if (!rflag)
97  sf /= MAGNITUDE(tgc->b);
98 
99  VSCALE(tgc->b, tgc->b, sf);
100  break;
101  case 'c':
102  case 'C':
103  if (!rflag)
104  sf /= MAGNITUDE(tgc->c);
105 
106  switch (attribute[1]) {
107  case '\0':
108  VSCALE(tgc->c, tgc->c, sf);
109  break;
110  case 'd':
111  case 'D':
112  VSCALE(tgc->c, tgc->c, sf);
113  ma = MAGNITUDE(tgc->c);
114  mb = MAGNITUDE(tgc->d);
115  VSCALE(tgc->d, tgc->d, ma/mb);
116  break;
117  default:
118  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
119  return GED_ERROR;
120  }
121 
122  break;
123  case 'd':
124  case 'D':
125  if (!rflag)
126  sf /= MAGNITUDE(tgc->d);
127 
128  VSCALE(tgc->d, tgc->d, sf);
129  break;
130  case 'h':
131  case 'H':
132  if (!rflag)
133  sf /= MAGNITUDE(tgc->h);
134 
135  switch (attribute[1]) {
136  case '\0':
137  VSCALE(tgc->h, tgc->h, sf);
138  break;
139  case 'c':
140  case 'C':
141  if ((attribute[2] == 'd' || attribute[2] == 'D') &&
142  attribute[3] == '\0') {
143  vect_t vec1, vec2;
144  vect_t c, d;
145 
146  /* calculate new c */
147  VSUB2(vec1, tgc->a, tgc->c);
148  VSCALE(vec2, vec1, 1-sf);
149  VADD2(c, tgc->c, vec2);
150 
151  /* calculate new d */
152  VSUB2(vec1, tgc->b, tgc->d);
153  VSCALE(vec2, vec1, 1-sf);
154  VADD2(d, tgc->d, vec2);
155 
156  if (0 <= VDOT(tgc->c, c) &&
157  0 <= VDOT(tgc->d, d) &&
158  !ZERO(MAGNITUDE(c)) &&
159  !ZERO(MAGNITUDE(d))) {
160  /* adjust c, d and h */
161  VMOVE(tgc->c, c);
162  VMOVE(tgc->d, d);
163  VSCALE(tgc->h, tgc->h, sf);
164  }
165  } else {
166  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
167  return GED_ERROR;
168  }
169 
170  break;
171  case 'v':
172  case 'V':
173  switch (attribute[2]) {
174  case '\0': {
175  point_t old_top;
176 
177  VADD2(old_top, tgc->v, tgc->h);
178  VSCALE(tgc->h, tgc->h, sf);
179  VSUB2(tgc->v, old_top, tgc->h);
180  }
181 
182  break;
183  case 'a':
184  case 'A':
185  if ((attribute[3] == 'b' || attribute[3] == 'B') &&
186  attribute[4] == '\0') {
187  vect_t vec1, vec2;
188  vect_t a, b;
189  point_t old_top;
190 
191  /* calculate new a */
192  VSUB2(vec1, tgc->c, tgc->a);
193  VSCALE(vec2, vec1, 1-sf);
194  VADD2(a, tgc->a, vec2);
195 
196  /* calculate new b */
197  VSUB2(vec1, tgc->d, tgc->b);
198  VSCALE(vec2, vec1, 1-sf);
199  VADD2(b, tgc->b, vec2);
200 
201  if (0 <= VDOT(tgc->a, a) &&
202  0 <= VDOT(tgc->b, b) &&
203  !ZERO(MAGNITUDE(a)) &&
204  !ZERO(MAGNITUDE(b))) {
205  /* adjust a, b, v and h */
206  VMOVE(tgc->a, a);
207  VMOVE(tgc->b, b);
208  VADD2(old_top, tgc->v, tgc->h);
209  VSCALE(tgc->h, tgc->h, sf);
210  VSUB2(tgc->v, old_top, tgc->h);
211  }
212  } else {
213  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
214  return GED_ERROR;
215  }
216 
217  break;
218  default:
219  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
220  return GED_ERROR;
221  } /* switch (attribute[2]) */
222 
223  break;
224  default:
225  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
226  return GED_ERROR;
227  } /* switch (attribute[1]) */
228 
229  break;
230  default:
231  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
232  return GED_ERROR;
233  } /* switch (attribute[0]) */
234 
235  return GED_OK;
236 }
237 
238 
239 /*
240  * Local Variables:
241  * tab-width: 8
242  * mode: C
243  * indent-tabs-mode: t
244  * c-file-style: "stroustrup"
245  * End:
246  * ex: shiftwidth=4 tabstop=8
247  */
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
struct bu_vls * ged_result_str
Definition: ged.h:357
#define ZERO(val)
Definition: units.c:38
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
double fastf_t
Definition: defines.h:300
int _ged_scale_tgc(struct ged *gedp, struct rt_tgc_internal *tgc, const char *attribute, fastf_t sf, int rflag)
Definition: scale_tgc.c:38