BRL-CAD
autoview.c
Go to the documentation of this file.
1 /* A U T O V I E W . 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/autoview.c
21  *
22  * The autoview command.
23  *
24  */
25 
26 #include "common.h"
27 
28 
29 #include "./ged_private.h"
30 
31 /*
32  * Auto-adjust the view so that all displayed geometry is in view
33  *
34  * Usage:
35  * autoview
36  *
37  */
38 int
39 ged_autoview(struct ged *gedp, int argc, const char *argv[])
40 {
41  int is_empty = 1;
42  vect_t min, max;
43  vect_t center;
44  vect_t radial;
45  vect_t sqrt_small;
46 
47  /* less than or near zero uses default, 0.5 model scale == 2.0 view factor */
48  fastf_t factor = -1.0;
49 
53  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
54 
55  /* initialize result */
56  bu_vls_trunc(gedp->ged_result_str, 0);
57 
58  if (argc > 2) {
59  bu_vls_printf(gedp->ged_result_str, "Usage: %s [scale]", argv[0]);
60  return GED_ERROR;
61  }
62 
63  /* parse the optional scale argument */
64  if (argc > 1) {
65  double scale = 0.0;
66  int ret = sscanf(argv[1], "%lf", &scale);
67  if (ret != 1) {
68  bu_vls_printf(gedp->ged_result_str, "ERROR: Expecting floating point scale value after %s\n", argv[0]);
69  return GED_ERROR;
70  }
71  if (scale > 0.0) {
72  factor = 1.0 / scale;
73  }
74  }
75 
76  /* set the default if unset or insane */
77  if (factor < SQRT_SMALL_FASTF) {
78  factor = 2.0; /* 2 is half the view */
79  }
80 
81  VSETALL(sqrt_small, SQRT_SMALL_FASTF);
82 
83  is_empty = dl_bounding_sph(gedp->ged_gdp->gd_headDisplay, &min, &max, 1);
84 
85  if (is_empty) {
86  /* Nothing is in view */
87  VSETALL(center, 0.0);
88  VSETALL(radial, 1000.0);
89  } else {
90  VADD2SCALE(center, max, min, 0.5);
91  VSUB2(radial, max, center);
92  }
93 
94  /* make sure it's not inverted */
95  VMAX(radial, sqrt_small);
96 
97  /* make sure it's not too small */
98  if (VNEAR_ZERO(radial, SQRT_SMALL_FASTF))
99  VSETALL(radial, 1.0);
100 
101  MAT_IDN(gedp->ged_gvp->gv_center);
102  MAT_DELTAS_VEC_NEG(gedp->ged_gvp->gv_center, center);
103  gedp->ged_gvp->gv_scale = radial[X];
104  V_MAX(gedp->ged_gvp->gv_scale, radial[Y]);
105  V_MAX(gedp->ged_gvp->gv_scale, radial[Z]);
106 
107  gedp->ged_gvp->gv_size = factor * gedp->ged_gvp->gv_scale;
108  gedp->ged_gvp->gv_isize = 1.0 / gedp->ged_gvp->gv_size;
109  ged_view_update(gedp->ged_gvp);
110 
111  return GED_OK;
112 }
113 
114 
115 /*
116  * Local Variables:
117  * tab-width: 8
118  * mode: C
119  * indent-tabs-mode: t
120  * c-file-style: "stroustrup"
121  * End:
122  * ex: shiftwidth=4 tabstop=8
123  */
#define GED_OK
Definition: ged.h:55
mat_t gv_center
Definition: bview.h:221
Definition: ged.h:338
#define VSETALL(a, s)
Definition: color.c:54
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
Header file for the BRL-CAD common definitions.
void ged_view_update(struct bview *gvp)
Definition: vutil.c:33
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#define GED_ERROR
Definition: ged.h:61
struct bview * ged_gvp
Definition: ged.h:361
Definition: color.c:49
#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
#define SQRT_SMALL_FASTF
Definition: defines.h:346
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
int dl_bounding_sph(struct bu_list *hdlp, vect_t *min, vect_t *max, int pflag)
Definition: display_list.c:157
int ged_autoview(struct ged *gedp, int argc, const char *argv[])
Definition: autoview.c:39
struct bu_vls * ged_result_str
Definition: ged.h:357
struct ged_drawable * ged_gdp
Definition: ged.h:360
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
Definition: color.c:51
double fastf_t
Definition: defines.h:300
Definition: color.c:50