BRL-CAD
sphgroup.c
Go to the documentation of this file.
1 /* S P H G R O U P . 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/group.c
21  *
22  * The group command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "wdb.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_sphgroup(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct directory *dp, *sphdp;
40  point_t obj_min, obj_max;
41  point_t rpp_min, rpp_max;
42  point_t centerpt;
43  int i;
44  int inside_flag = 0;
45  struct rt_db_internal sph_intern;
46  struct rt_ell_internal *bsph;
47  static const char *usage = "gname target_sphere.s";
48 
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 != 3) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_ERROR;
60  }
61 
62  if ((sphdp = db_lookup(gedp->ged_wdbp->dbip, argv[argc-1], LOOKUP_NOISY)) == RT_DIR_NULL) {
63  bu_vls_printf(gedp->ged_result_str, "Specified bounding sphere %s not found\n", argv[argc-1]);
64  return GED_ERROR;
65  } else {
66  if (rt_db_get_internal(&sph_intern, sphdp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0)
67  return GED_ERROR;
68  if ((sph_intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_ELL) && (sph_intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_SPH)) {
69  bu_vls_printf(gedp->ged_result_str, "Specified bounding object %s not a sphere\n", argv[argc-1]);
70  return GED_ERROR;
71  }
72  bsph = (struct rt_ell_internal *)sph_intern.idb_ptr;
73  }
74 
75  /* get objects to add to group - at the moment, only gets regions*/
76  for (i = 0; i < RT_DBNHASH; i++)
77  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
78  if (dp->d_nref == 0 && !(dp->d_flags & RT_DIR_HIDDEN) && (dp->d_addr != RT_DIR_PHONY_ADDR)) continue;
79  if (BU_STR_EQUAL(dp->d_namep, sphdp->d_namep)) continue;
80  if (!(dp->d_flags & RT_DIR_REGION)) continue;
81  inside_flag = 0;
82  if (ged_get_obj_bounds(gedp, 1, (const char **)&(dp->d_namep), 0, obj_min, obj_max) != GED_ERROR) {
83  VSETALL(rpp_min, INFINITY);
84  VSETALL(rpp_max, -INFINITY);
85  VMINMAX(rpp_min, rpp_max, (double *)obj_min);
86  VMINMAX(rpp_min, rpp_max, (double *)obj_max);
87  /*
88  VMOVE(testpts[0], rpp_min);
89  VSET(testpts[1], rpp_min[X], rpp_min[Y], rpp_max[Z]);
90  VSET(testpts[2], rpp_min[X], rpp_max[Y], rpp_max[Z]);
91  VSET(testpts[3], rpp_min[X], rpp_max[Y], rpp_min[Z]);
92  VSET(testpts[4], rpp_max[X], rpp_min[Y], rpp_min[Z]);
93  VSET(testpts[5], rpp_max[X], rpp_min[Y], rpp_max[Z]);
94  VMOVE(testpts[6], rpp_max);
95  VSET(testpts[7], rpp_max[X], rpp_max[Y], rpp_min[Z]);
96  for (j = 0; j < 8; j++) {
97  if (DIST_PT_PT(testpts[j], bsph->v) <= MAGNITUDE(bsph->a)) inside_flag = 1;
98  }*/
99  VSET(centerpt, (rpp_min[0] + rpp_max[0])*0.5, (rpp_min[1] + rpp_max[1])*0.5, (rpp_min[2] + rpp_max[2])*0.5);
100  if (DIST_PT_PT(centerpt, bsph->v) <= MAGNITUDE(bsph->a)) inside_flag = 1;
101  if (inside_flag == 1) {
102  if (_ged_combadd(gedp, dp, (char *)argv[1], 0, WMOP_UNION, 0, 0) == RT_DIR_NULL) return GED_ERROR;
103  inside_flag = 0;
104  }
105  }
106  }
107  return GED_OK;
108 }
109 
110 
111 /*
112  * Local Variables:
113  * tab-width: 8
114  * mode: C
115  * indent-tabs-mode: t
116  * c-file-style: "stroustrup"
117  * End:
118  * ex: shiftwidth=4 tabstop=8
119  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition: raytrace.h:755
long d_nref
times ref'ed by COMBs
Definition: raytrace.h:868
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
#define VSET(a, b, c, d)
Definition: color.c:53
#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
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
#define RT_DIR_REGION
region
Definition: raytrace.h:885
#define WMOP_UNION
Definition: wdb.h:887
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
#define GED_ERROR
Definition: ged.h:61
int ged_sphgroup(struct ged *gedp, int argc, const char *argv[])
Definition: sphgroup.c:37
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_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
#define RT_DIR_HIDDEN
object name is hidden
Definition: raytrace.h:886
int ged_get_obj_bounds(struct ged *gedp, int argc, const char *argv[], int use_air, point_t rpp_min, point_t rpp_max)
struct bu_vls * ged_result_str
Definition: ged.h:357
void * idb_ptr
Definition: raytrace.h:195
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
struct directory * _ged_combadd(struct ged *gedp, struct directory *objp, char *combname, int region_flag, db_op_t relation, int ident, int air)
Definition: comb.c:680
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126