BRL-CAD
scale.c
Go to the documentation of this file.
1 /* S C A L E . 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.c
21  *
22  * The scale command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_scale_args(struct ged *gedp, int argc, const char *argv[], fastf_t *sf1, fastf_t *sf2, fastf_t *sf3)
37 {
38  static const char *usage = "sf (or) sfx sfy sfz";
39  int ret = GED_OK, args_read;
40  double scan;
41 
44  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
45 
46  /* initialize result */
47  bu_vls_trunc(gedp->ged_result_str, 0);
48 
49  /* must be wanting help */
50  if (argc == 1) {
51  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
52  return GED_HELP;
53  }
54 
55  if (argc != 2 && argc != 4) {
56  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
57  return GED_ERROR;
58  }
59 
60  if (argc == 2) {
61  if (!sf1 || sscanf(argv[1], "%lf", &scan) != 1) {
62  bu_vls_printf(gedp->ged_result_str, "\nbad scale factor '%s'", argv[1]);
63  return GED_ERROR;
64  }
65  *sf1 = scan;
66  } else {
67  args_read = sscanf(argv[1], "%lf", &scan);
68  if (!sf1 || args_read != 1) {
69  bu_vls_printf(gedp->ged_result_str, "\nbad x scale factor '%s'", argv[1]);
70  ret = GED_ERROR;
71  }
72  *sf1 = scan;
73 
74  args_read = sscanf(argv[2], "%lf", &scan);
75  if (!sf2 || args_read != 1) {
76  bu_vls_printf(gedp->ged_result_str, "\nbad y scale factor '%s'", argv[2]);
77  ret = GED_ERROR;
78  }
79  *sf2 = scan;
80 
81  args_read = sscanf(argv[3], "%lf", &scan);
82  if (!sf3 || args_read != 1) {
83  bu_vls_printf(gedp->ged_result_str, "\nbad z scale factor '%s'", argv[3]);
84  ret = GED_ERROR;
85  }
86  *sf3 = scan;
87  }
88  return ret;
89 }
90 
91 
92 int
93 ged_scale(struct ged *gedp, int argc, const char *argv[])
94 {
95  int ret;
96  fastf_t sf1;
97  fastf_t sf2;
98  fastf_t sf3;
99 
100  if ((ret = ged_scale_args(gedp, argc, argv, &sf1, &sf2, &sf3)) != GED_OK)
101  return ret;
102 
103  if (argc != 2) {
104  bu_vls_printf(gedp->ged_result_str, "Can not scale xyz independently on a view.");
105  return GED_ERROR;
106  }
107 
108  if (sf1 <= SMALL_FASTF || INFINITY < sf1)
109  return GED_OK;
110 
111  /* scale the view */
112  gedp->ged_gvp->gv_scale *= sf1;
113 
114  if (gedp->ged_gvp->gv_scale < RT_MINVIEWSIZE)
116  gedp->ged_gvp->gv_size = 2.0 * gedp->ged_gvp->gv_scale;
117  gedp->ged_gvp->gv_isize = 1.0 / gedp->ged_gvp->gv_size;
118  ged_view_update(gedp->ged_gvp);
119 
120  return GED_OK;
121 }
122 
123 
124 /*
125  * Local Variables:
126  * tab-width: 8
127  * mode: C
128  * indent-tabs-mode: t
129  * c-file-style: "stroustrup"
130  * End:
131  * ex: shiftwidth=4 tabstop=8
132  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
#define RT_MINVIEWSIZE
Definition: raytrace.h:1301
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
#define SMALL_FASTF
Definition: defines.h:342
Header file for the BRL-CAD common definitions.
void ged_view_update(struct bview *gvp)
Definition: vutil.c:33
#define GED_ERROR
Definition: ged.h:61
struct bview * ged_gvp
Definition: ged.h:361
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
fastf_t gv_isize
1.0 / size
Definition: bview.h:213
fastf_t gv_scale
Definition: bview.h:211
fastf_t gv_size
2.0 * scale
Definition: bview.h:212
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_scale_args(struct ged *gedp, int argc, const char *argv[], fastf_t *sf1, fastf_t *sf2, fastf_t *sf3)
Definition: scale.c:36
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
int ged_scale(struct ged *gedp, int argc, const char *argv[])
Definition: scale.c:93
double fastf_t
Definition: defines.h:300