BRL-CAD
translate_tgc.c
Go to the documentation of this file.
1 /* T R A N S L A T 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/translate_tgc.c
21  *
22  * The translate_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_translate_tgc(struct ged *gedp, struct rt_tgc_internal *tgc, const char *attribute, vect_t tvec, int rflag)
39 {
40  fastf_t la, lb, lc, ld;
41  vect_t hvec;
42 
43  RT_TGC_CK_MAGIC(tgc);
44 
45  VSCALE(tvec, tvec, gedp->ged_wdbp->dbip->dbi_local2base);
46 
47  switch (attribute[0]) {
48  case 'h':
49  case 'H':
50  switch (attribute[1]) {
51  case '\0':
52  if (rflag) {
53  VADD2(hvec, tgc->h, tvec);
54  } else {
55  VSUB2(hvec, tvec, tgc->v);
56  }
57 
58  /* check for zero H vector */
59  if (MAGNITUDE(hvec) <= SQRT_SMALL_FASTF) {
60  bu_vls_printf(gedp->ged_result_str, "Zero H vector not allowed.");
61  return GED_ERROR;
62  }
63 
64  VMOVE(tgc->h, hvec);
65 
66  break;
67  case 'r':
68  case 'R':
69  if (attribute[2] != '\0') {
70  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
71  return GED_ERROR;
72  }
73 
74  if (rflag) {
75  VADD2(hvec, tgc->h, tvec);
76  } else {
77  VSUB2(hvec, tvec, tgc->v);
78  }
79 
80  /* check for zero H vector */
81  if (MAGNITUDE(hvec) <= SQRT_SMALL_FASTF) {
82  bu_vls_printf(gedp->ged_result_str, "Zero H vector not allowed.");
83  return GED_ERROR;
84  }
85 
86  VMOVE(tgc->h, hvec);
87 
88  /* have new height vector -- redefine rest of tgc */
89  la = MAGNITUDE(tgc->a);
90  lb = MAGNITUDE(tgc->b);
91  lc = MAGNITUDE(tgc->c);
92  ld = MAGNITUDE(tgc->d);
93 
94  /* find 2 perpendicular vectors normal to H for new A, B */
95  VCROSS(tgc->b, tgc->h, tgc->a);
96  VCROSS(tgc->a, tgc->b, tgc->h);
97 
98  VUNITIZE(tgc->a);
99  VUNITIZE(tgc->b);
100 
101  /* Create new C, D from unit length A, B, with previous len */
102  VSCALE(tgc->c, tgc->a, lc);
103  VSCALE(tgc->d, tgc->b, ld);
104 
105  /* Restore original vector lengths to A, B */
106  VSCALE(tgc->a, tgc->a, la);
107  VSCALE(tgc->b, tgc->b, lb);
108 
109  break;
110  default:
111  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
112  return GED_ERROR;
113  }
114 
115  break;
116  default:
117  bu_vls_printf(gedp->ged_result_str, "bad tgc attribute - %s", attribute);
118  return GED_ERROR;
119  }
120 
121  return GED_OK;
122 }
123 
124 
125 /*
126  * Local Variables:
127  * tab-width: 8
128  * mode: C
129  * indent-tabs-mode: t
130  * c-file-style: "stroustrup"
131  * End:
132  * ex: shiftwidth=4 tabstop=8
133  */
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define GED_ERROR
Definition: ged.h:61
#define SQRT_SMALL_FASTF
Definition: defines.h:346
struct bu_vls * ged_result_str
Definition: ged.h:357
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
double dbi_local2base
local2mm
Definition: raytrace.h:807
double fastf_t
Definition: defines.h:300
int _ged_translate_tgc(struct ged *gedp, struct rt_tgc_internal *tgc, const char *attribute, vect_t tvec, int rflag)
Definition: translate_tgc.c:38