BRL-CAD
tol.c
Go to the documentation of this file.
1 /* T O L . 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/tol.c
21  *
22  * The tol command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_tol(struct ged *gedp, int argc, const char *argv[])
37 {
38  double f;
39  static const char *usage = "([abs|rel|norm|dist|perp] [tolerance]) ...";
40 
42  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
43 
44  /* initialize result */
45  bu_vls_trunc(gedp->ged_result_str, 0);
46 
47  /* print all tolerance settings */
48  if (argc == 1) {
49  bu_vls_printf(gedp->ged_result_str, "Current tolerance settings are:\n");
50  bu_vls_printf(gedp->ged_result_str, "Tessellation tolerances:\n");
51 
52  if (gedp->ged_wdbp->wdb_ttol.abs > 0.0) {
53  bu_vls_printf(gedp->ged_result_str, "\tabs %g mm\n", gedp->ged_wdbp->wdb_ttol.abs);
54  } else {
55  bu_vls_printf(gedp->ged_result_str, "\tabs None\n");
56  }
57 
58  if (gedp->ged_wdbp->wdb_ttol.rel > 0.0) {
59  bu_vls_printf(gedp->ged_result_str, "\trel %g (%g%%)\n",
60  gedp->ged_wdbp->wdb_ttol.rel, gedp->ged_wdbp->wdb_ttol.rel * 100.0);
61  } else {
62  bu_vls_printf(gedp->ged_result_str, "\trel None\n");
63  }
64 
65  if (gedp->ged_wdbp->wdb_ttol.norm > 0.0) {
66  int deg, min;
67  double sec;
68 
69  sec = gedp->ged_wdbp->wdb_ttol.norm * RAD2DEG;
70  deg = (int)(sec);
71  sec = (sec - (double)deg) * 60;
72  min = (int)(sec);
73  sec = (sec - (double)min) * 60;
74 
75  bu_vls_printf(gedp->ged_result_str, "\tnorm %g degrees (%d deg %d min %g sec)\n",
76  gedp->ged_wdbp->wdb_ttol.norm * RAD2DEG, deg, min, sec);
77  } else {
78  bu_vls_printf(gedp->ged_result_str, "\tnorm None\n");
79  }
80 
81  bu_vls_printf(gedp->ged_result_str, "Calculational tolerances:\n");
83  "\tdistance = %g mm\n\tperpendicularity = %g (cosine of %g degrees)",
84  gedp->ged_wdbp->wdb_tol.dist, gedp->ged_wdbp->wdb_tol.perp,
85  acos(gedp->ged_wdbp->wdb_tol.perp)*RAD2DEG);
86 
87  return GED_OK;
88  }
89 
90  /* get the specified tolerance */
91  if (argc == 2) {
92  int status = GED_OK;
93 
94  switch (argv[1][0]) {
95  case 'a':
96  if (gedp->ged_wdbp->wdb_ttol.abs > 0.0)
97  bu_vls_printf(gedp->ged_result_str, "%g", gedp->ged_wdbp->wdb_ttol.abs);
98  else
99  bu_vls_printf(gedp->ged_result_str, "None");
100  break;
101  case 'r':
102  if (gedp->ged_wdbp->wdb_ttol.rel > 0.0)
103  bu_vls_printf(gedp->ged_result_str, "%g", gedp->ged_wdbp->wdb_ttol.rel);
104  else
105  bu_vls_printf(gedp->ged_result_str, "None");
106  break;
107  case 'n':
108  if (gedp->ged_wdbp->wdb_ttol.norm > 0.0)
109  bu_vls_printf(gedp->ged_result_str, "%g", gedp->ged_wdbp->wdb_ttol.norm);
110  else
111  bu_vls_printf(gedp->ged_result_str, "None");
112  break;
113  case 'd':
114  bu_vls_printf(gedp->ged_result_str, "%g", gedp->ged_wdbp->wdb_tol.dist);
115  break;
116  case 'p':
117  bu_vls_printf(gedp->ged_result_str, "%g", gedp->ged_wdbp->wdb_tol.perp);
118  break;
119  default:
120  bu_vls_printf(gedp->ged_result_str, "unrecognized tolerance type - %s", argv[1]);
121  status = GED_ERROR;
122  break;
123  }
124 
125  return status;
126  }
127 
128  if ((argc-1)%2 != 0) {
129  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
130  return GED_ERROR;
131  }
132 
133  /* skip the command name */
134  argc--;
135  argv++;
136 
137  /* iterate over the pairs of tolerance values */
138  while (argc > 0) {
139 
140  /* set the specified tolerance(s) */
141  if (sscanf(argv[1], "%lf", &f) != 1) {
142  bu_vls_printf(gedp->ged_result_str, "bad tolerance - %s", argv[1]);
143 
144  return GED_ERROR;
145  }
146 
147  /* clamp negative to zero */
148  if (f < 0.0) {
149  bu_vls_printf(gedp->ged_result_str, "negative tolerance clamped to 0.0\n");
150  f = 0.0;
151  }
152 
153  switch (argv[0][0]) {
154  case 'a':
155  /* Absolute tol */
156  if (f < gedp->ged_wdbp->wdb_tol.dist) {
158  "absolute tolerance cannot be less than distance tolerance, clamped to %f\n", gedp->ged_wdbp->wdb_tol.dist);
159  }
160  gedp->ged_wdbp->wdb_ttol.abs = f;
161  break;
162  case 'r':
163  if (f >= 1.0) {
165  "relative tolerance must be between 0 and 1, not changed\n");
166  return GED_ERROR;
167  }
168  /* Note that a value of 0.0 will disable relative tolerance */
169  gedp->ged_wdbp->wdb_ttol.rel = f;
170  break;
171  case 'n':
172  /* Normal tolerance, in degrees */
173  if (f > 90.0) {
175  "Normal tolerance must be less than 90.0 degrees\n");
176  return GED_ERROR;
177  }
178  /* Note that a value of 0.0 or 360.0 will disable this tol */
179  gedp->ged_wdbp->wdb_ttol.norm = f * DEG2RAD;
180  break;
181  case 'd':
182  /* Calculational distance tolerance */
183  gedp->ged_wdbp->wdb_tol.dist = f;
184  gedp->ged_wdbp->wdb_tol.dist_sq = gedp->ged_wdbp->wdb_tol.dist * gedp->ged_wdbp->wdb_tol.dist;
185  break;
186  case 'p':
187  /* Calculational perpendicularity tolerance */
188  if (f > 1.0) {
190  "Calculational perpendicular tolerance must be from 0 to 1\n");
191  return GED_ERROR;
192  }
193  gedp->ged_wdbp->wdb_tol.perp = f;
194  gedp->ged_wdbp->wdb_tol.para = 1.0 - f;
195  break;
196  default:
197  bu_vls_printf(gedp->ged_result_str, "unrecognized tolerance type - %s", argv[0]);
198 
199  return GED_ERROR;
200  }
201 
202  argc-=2;
203  argv+=2;
204  }
205 
206  return GED_OK;
207 }
208 
209 
210 /*
211  * Local Variables:
212  * tab-width: 8
213  * mode: C
214  * indent-tabs-mode: t
215  * c-file-style: "stroustrup"
216  * End:
217  * ex: shiftwidth=4 tabstop=8
218  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
double dist
>= 0
Definition: tol.h:73
Definition: ged.h:338
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
double dist_sq
dist * dist
Definition: tol.h:74
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
struct rt_tess_tol wdb_ttol
Definition: raytrace.h:1268
#define GED_ERROR
Definition: ged.h:61
double rel
rel dist tol
Definition: raytrace.h:181
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
int ged_tol(struct ged *gedp, int argc, const char *argv[])
Definition: tol.c:36
struct bu_vls * ged_result_str
Definition: ged.h:357
double perp
nearly 0
Definition: tol.h:75
struct bn_tol wdb_tol
Definition: raytrace.h:1269
double abs
absolute dist tol
Definition: raytrace.h:180
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
double norm
normal tol
Definition: raytrace.h:182
double para
nearly 1
Definition: tol.h:76