BRL-CAD
3ptarb.c
Go to the documentation of this file.
1 /* 3 P T A R B . 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/3ptarb.c
21  *
22  * The 3ptarb command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "rtgeom.h"
33 
34 #include "./ged_private.h"
35 
36 
37 static char *p_arb3pt[] = {
38  "Enter X, Y, Z for point 1: ",
39  "Enter Y, Z: ",
40  "Enter Z: ",
41  "Enter X, Y, Z for point 2: ",
42  "Enter Y, Z: ",
43  "Enter Z: ",
44  "Enter X, Y, Z for point 3: ",
45  "Enter Y, Z: ",
46  "Enter Z: "
47 };
48 
49 
50 int
51 ged_3ptarb(struct ged *gedp, int argc, const char *argv[])
52 {
53  int i, solve;
54  vect_t vec1;
55  vect_t vec2;
56  fastf_t pt4[2], length, thick;
57  vect_t norm;
58  fastf_t ndotv;
59  char **prompts;
60  struct directory *dp;
61  struct rt_db_internal internal;
62  struct rt_arb_internal *aip;
63  static const char *usage = "name x1 y1 z1 x2 y2 z2 x3 y3 z3 coord c1 c2 th";
64 
67  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
68 
69  /* initialize result */
70  bu_vls_trunc(gedp->ged_result_str, 0);
71 
72  if (argc > 15) {
73  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
74  return GED_ERROR;
75  }
76 
77  if (argc < 2) {
78  bu_vls_printf(gedp->ged_result_str, "Enter name for this arb: ");
79  return GED_MORE;
80  }
81 
82  GED_CHECK_EXISTS(gedp, argv[1], LOOKUP_QUIET, GED_ERROR);
83 
84  /* read the three points */
85  prompts = &p_arb3pt[0];
86  if (argc < 11) {
87  bu_vls_printf(gedp->ged_result_str, "%s", prompts[argc-2]);
88  return GED_MORE;
89  }
90 
91  /* preliminary calculations to check input so far */
92  for (i = 0; i < 3; i++) {
93  vec1[i] = atof(argv[i+2]) - atof(argv[i+5]);
94  vec2[i] = atof(argv[i+2]) - atof(argv[i+8]);
95  }
96  VCROSS(norm, vec1, vec2);
97  length = MAGNITUDE(norm);
98  if (ZERO(length)) {
99  bu_vls_printf(gedp->ged_result_str, "%s: points are collinear\n", argv[0]);
100  return GED_ERROR;
101  }
102  VSCALE(norm, norm, 1.0/length);
103 
104  if (argc < 12) {
105  bu_vls_printf(gedp->ged_result_str, "Enter coordinate to solve for (x, y, or z): ");
106  return GED_MORE;
107  }
108 
109  switch (argv[11][0]) {
110  case 'x':
111  if (ZERO(norm[0])) {
112  bu_vls_printf(gedp->ged_result_str, "%s: X not unique in this face\n", argv[0]);
113  return GED_ERROR;
114  }
115  solve = X;
116 
117  if (argc < 13) {
118  bu_vls_printf(gedp->ged_result_str, "Enter the Y, Z coordinate values: ");
119  return GED_MORE;
120  }
121  if (argc < 14) {
122  bu_vls_printf(gedp->ged_result_str, "Enter the Z coordinate value: ");
123  return GED_MORE;
124  }
125 
126  pt4[0] = atof(argv[12]) * gedp->ged_wdbp->dbip->dbi_local2base;
127  pt4[1] = atof(argv[13]) * gedp->ged_wdbp->dbip->dbi_local2base;
128  break;
129 
130  case 'y':
131  if (ZERO(norm[1])) {
132  bu_vls_printf(gedp->ged_result_str, "%s: Y not unique in this face\n", argv[0]);
133  return GED_ERROR;
134  }
135  solve = Y;
136 
137  if (argc < 13) {
138  bu_vls_printf(gedp->ged_result_str, "Enter the X, Z coordinate values: ");
139  return GED_MORE;
140  }
141  if (argc < 14) {
142  bu_vls_printf(gedp->ged_result_str, "Enter the Z coordinate value: ");
143  return GED_MORE;
144  }
145 
146  pt4[0] = atof(argv[12]) * gedp->ged_wdbp->dbip->dbi_local2base;
147  pt4[1] = atof(argv[13]) * gedp->ged_wdbp->dbip->dbi_local2base;
148  break;
149 
150  case 'z':
151  if (ZERO(norm[2])) {
152  bu_vls_printf(gedp->ged_result_str, "%s: Z not unique in this face\n", argv[0]);
153  return GED_ERROR;
154  }
155  solve = Z;
156 
157  if (argc < 13) {
158  bu_vls_printf(gedp->ged_result_str, "Enter the X, Y coordinate values: ");
159  return GED_MORE;
160  }
161  if (argc < 14) {
162  bu_vls_printf(gedp->ged_result_str, "Enter the Y coordinate value: ");
163  return GED_MORE;
164  }
165 
166  pt4[0] = atof(argv[12]) * gedp->ged_wdbp->dbip->dbi_local2base;
167  pt4[1] = atof(argv[13]) * gedp->ged_wdbp->dbip->dbi_local2base;
168  break;
169 
170  default:
171  bu_vls_printf(gedp->ged_result_str, "%s: coordinate must be x, y, or z\n", argv[0]);
172  return GED_ERROR;
173  }
174 
175  if (argc < 15) {
176  bu_vls_printf(gedp->ged_result_str, "Enter thickness for this arb: ");
177  return GED_MORE;
178  }
179 
180  thick = atof(argv[14]);
181  if (ZERO(thick)) {
182  bu_vls_printf(gedp->ged_result_str, "%s: thickness = 0.0\n", argv[0]);
183  return GED_ERROR;
184  }
185 
186  RT_DB_INTERNAL_INIT(&internal);
187  internal.idb_major_type = DB5_MAJORTYPE_BRLCAD;
188  internal.idb_type = ID_ARB8;
189  internal.idb_meth = &OBJ[ID_ARB8];
190  BU_ALLOC(internal.idb_ptr, struct rt_arb_internal);
191  aip = (struct rt_arb_internal *)internal.idb_ptr;
192  aip->magic = RT_ARB_INTERNAL_MAGIC;
193 
194  for (i = 0; i < 8; i++) {
195  VSET(aip->pt[i], 0.0, 0.0, 0.0);
196  }
197 
198  for (i = 0; i < 3; i++) {
199  /* the three given vertices */
200  VSET(aip->pt[i], atof(argv[i*3+2])*gedp->ged_wdbp->dbip->dbi_local2base, atof(argv[i*3+3])*gedp->ged_wdbp->dbip->dbi_local2base, atof(argv[i*3+4])*gedp->ged_wdbp->dbip->dbi_local2base);
201  }
202 
203  thick *= gedp->ged_wdbp->dbip->dbi_local2base;
204 
205  ndotv = VDOT(aip->pt[0], norm);
206 
207  switch (solve) {
208 
209  case X:
210  /* solve for x-coord of 4th point */
211  aip->pt[3][Y] = pt4[0]; /* y-coord */
212  aip->pt[3][Z] = pt4[1]; /* z-coord */
213  aip->pt[3][X] = (ndotv
214  - norm[Y] * aip->pt[3][Y]
215  - norm[Z] * aip->pt[3][Z])
216  / norm[X]; /* x-coord */
217  break;
218 
219  case Y:
220  /* solve for y-coord of 4th point */
221  aip->pt[3][X] = pt4[0]; /* x-coord */
222  aip->pt[3][Z] = pt4[1]; /* z-coord */
223  aip->pt[3][Y] = (ndotv
224  - norm[X] * aip->pt[3][X]
225  - norm[Z] * aip->pt[3][Z])
226  / norm[Y]; /* y-coord */
227  break;
228 
229  case Z:
230  /* solve for z-coord of 4th point */
231  aip->pt[3][X] = pt4[0]; /* x-coord */
232  aip->pt[3][Y] = pt4[1]; /* y-coord */
233  aip->pt[3][Z] = (ndotv
234  - norm[X] * aip->pt[3][X]
235  - norm[Y] * aip->pt[3][Y])
236  / norm[Z]; /* z-coord */
237  break;
238 
239  default:
240  bu_free((void *)internal.idb_ptr, "rt_arb_internal");
241  bu_vls_printf(gedp->ged_result_str, "%s: bad coordinate to solve for\n", argv[0]);
242  return GED_ERROR;
243  }
244 
245  /* calculate the remaining 4 vertices */
246  for (i = 0; i < 4; i++) {
247  VJOIN1(aip->pt[i+4], aip->pt[i], thick, norm);
248  }
249 
250  GED_DB_DIRADD(gedp, dp, argv[1], RT_DIR_PHONY_ADDR, 0, RT_DIR_SOLID, (void *)&internal.idb_type, GED_ERROR);
251 
252  GED_DB_PUT_INTERNAL(gedp, dp, &internal, &rt_uniresource, GED_ERROR);
253 
254  return GED_OK;
255 }
256 
257 
258 /*
259  * Local Variables:
260  * tab-width: 8
261  * mode: C
262  * indent-tabs-mode: t
263  * c-file-style: "stroustrup"
264  * End:
265  * ex: shiftwidth=4 tabstop=8
266  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_DB_DIRADD(_gedp, _dp, _name, _laddr, _len, _dirflags, _ptr, _flags)
Definition: ged.h:213
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
#define ID_ARB8
Generalized ARB. V + 7 vectors.
Definition: raytrace.h:462
#define VSET(a, b, c, d)
Definition: color.c:53
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
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 GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
Definition: color.c:49
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
#define RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
int ged_3ptarb(struct ged *gedp, int argc, const char *argv[])
Definition: 3ptarb.c:51
#define GED_CHECK_EXISTS(_gedp, _name, _noisy, _flags)
Definition: ged.h:171
struct bu_vls * ged_result_str
Definition: ged.h:357
#define ZERO(val)
Definition: units.c:38
const struct rt_functab OBJ[]
Definition: table.c:159
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
Definition: color.c:51
#define idb_type
Definition: raytrace.h:198
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
double dbi_local2base
local2mm
Definition: raytrace.h:807
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
double fastf_t
Definition: defines.h:300
#define RT_ARB_INTERNAL_MAGIC
Definition: magic.h:82
Definition: color.c:50
#define GED_MORE
Definition: ged.h:63