BRL-CAD
ocenter.c
Go to the documentation of this file.
1 /* O C E N T E R . 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/ocenter.c
21  *
22  * The ocenter command.
23  *
24  */
25 
26 #include "common.h"
27 
28 
29 #include "./ged_private.h"
30 
31 
32 int
33 ged_ocenter(struct ged *gedp, int argc, const char *argv[])
34 {
35  struct directory *dp;
36  struct _ged_trace_data gtd;
37  struct rt_db_internal intern;
38  mat_t dmat;
39  mat_t emat;
40  mat_t tmpMat;
41  mat_t invXform;
42  point_t rpp_min;
43  point_t rpp_max;
44  point_t oldCenter;
45  point_t center;
46  point_t delta;
47  double scan[3];
48  static const char *usage = "object [x y z]";
49 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /* initialize result */
54  bu_vls_trunc(gedp->ged_result_str, 0);
55 
56  /* must be wanting help */
57  if (argc == 1) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_HELP;
60  }
61 
62  if (argc != 2 && argc != 5) {
63  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
64  return GED_ERROR;
65  }
66 
67  /*
68  * One of the get bounds routines needs to be fixed to
69  * work with all cases. In the meantime...
70  */
71  if (_ged_get_obj_bounds2(gedp, 1, argv+1, &gtd, rpp_min, rpp_max) == GED_ERROR)
72  return GED_ERROR;
73 
74  dp = gtd.gtd_obj[gtd.gtd_objpos-1];
75  if (!(dp->d_flags & RT_DIR_SOLID)) {
76  if (ged_get_obj_bounds(gedp, 1, argv+1, 1, rpp_min, rpp_max) == GED_ERROR)
77  return GED_ERROR;
78  }
79 
80  VADD2(oldCenter, rpp_min, rpp_max);
81  VSCALE(oldCenter, oldCenter, 0.5);
82 
83  if (argc == 2) {
84  VSCALE(center, oldCenter, gedp->ged_wdbp->dbip->dbi_base2local);
85  bn_encode_vect(gedp->ged_result_str, center);
86 
87  return GED_OK;
88  }
89 
91 
92  /* Read in the new center */
93  if (sscanf(argv[2], "%lf", &scan[X]) != 1) {
94  bu_vls_printf(gedp->ged_result_str, "%s: bad x value - %s", argv[0], argv[2]);
95  return GED_ERROR;
96  }
97 
98  if (sscanf(argv[3], "%lf", &scan[Y]) != 1) {
99  bu_vls_printf(gedp->ged_result_str, "%s: bad y value - %s", argv[0], argv[3]);
100  return GED_ERROR;
101  }
102 
103  if (sscanf(argv[4], "%lf", &scan[Z]) != 1) {
104  bu_vls_printf(gedp->ged_result_str, "%s: bad z value - %s", argv[0], argv[4]);
105  return GED_ERROR;
106  }
107 
108  VSCALE(center, scan, gedp->ged_wdbp->dbip->dbi_local2base);
109  VSUB2(delta, center, oldCenter);
110  MAT_IDN(dmat);
111  MAT_DELTAS_VEC(dmat, delta);
112 
113  bn_mat_inv(invXform, gtd.gtd_xform);
114  bn_mat_mul(tmpMat, invXform, dmat);
115  bn_mat_mul(emat, tmpMat, gtd.gtd_xform);
116 
117  GED_DB_GET_INTERNAL(gedp, &intern, dp, emat, &rt_uniresource, GED_ERROR);
118  RT_CK_DB_INTERNAL(&intern);
119  GED_DB_PUT_INTERNAL(gedp, dp, &intern, &rt_uniresource, GED_ERROR);
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  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
void bn_encode_vect(struct bu_vls *vp, const vect_t v)
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
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 RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
#define RT_DIR_SOLID
this name is a solid
Definition: raytrace.h:883
void bn_mat_inv(mat_t output, const mat_t input)
int ged_get_obj_bounds(struct ged *gedp, int argc, const char *argv[], int use_air, point_t rpp_min, point_t rpp_max)
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
struct bu_vls * ged_result_str
Definition: ged.h:357
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
int ged_ocenter(struct ged *gedp, int argc, const char *argv[])
Definition: ocenter.c:33
mat_t gtd_xform
Definition: ged.h:1917
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
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
Definition: color.c:51
double dbi_local2base
local2mm
Definition: raytrace.h:807
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
HIDDEN const point_t delta
Definition: sh_prj.c:618
struct directory * gtd_obj[_GED_MAX_LEVELS]
Definition: ged.h:1916
int _ged_get_obj_bounds2(struct ged *gedp, int argc, const char *argv[], struct _ged_trace_data *gtdp, point_t rpp_min, point_t rpp_max)
This version works if the last member of the path is a primitive.
Definition: color.c:50
int gtd_objpos
Definition: ged.h:1918