BRL-CAD
bot.c
Go to the documentation of this file.
1 /* B O T . 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/bot.c
21  *
22  * Bot command for simple bot primitive operations.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "bn/chull.h"
33 #include "rtgeom.h"
34 #include "wdb.h"
35 #include "./ged_private.h"
36 
37 
38 int
39 ged_bot(struct ged *gedp, int argc, const char *argv[])
40 {
41  struct directory *bot_dp;
42  struct rt_db_internal intern;
43  struct rt_bot_internal *bot;
44  const char *cmd = argv[0];
45  const char *sub = NULL;
46  const char *arg = NULL;
47  const char *primitive = NULL;
48  size_t len;
49  fastf_t tmp;
50  fastf_t propVal;
51  static const char *usage = "get (faces|minEdge|maxEdge|orientation|type|vertices) bot\nchull bot_in bot_out\n";
52 
55  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
56 
57  /* initialize result */
58  bu_vls_trunc(gedp->ged_result_str, 0);
59 
60  /* must be wanting help */
61  if (argc < 3) {
62  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
63  return GED_ERROR;
64  }
65 
66  /* determine subcommand */
67  sub = argv[1];
68  len = strlen(sub);
69  if (bu_strncmp(sub, "get", len) == 0) {
70  primitive = argv[argc - 1];
71  }
72  if (bu_strncmp(sub, "chull", len) == 0) {
73  primitive = argv[2];
74  }
75  if (primitive == NULL) {
76  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a known subcommand!", cmd, sub);
77  return GED_ERROR;
78  }
79 
80  /* get bot */
81  GED_DB_LOOKUP(gedp, bot_dp, primitive, LOOKUP_NOISY, GED_ERROR & GED_QUIET);
83 
84  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
85  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a BOT solid!", cmd, primitive);
86  return GED_ERROR;
87  }
88 
89  bot = (struct rt_bot_internal *)intern.idb_ptr;
90  RT_BOT_CK_MAGIC(bot);
91 
92  if (bu_strncmp(sub, "get", len) == 0) {
93 
94  arg = argv[2];
95  propVal = rt_bot_propget(bot, arg);
96 
97  /* print result string */
98  if (!EQUAL(propVal, -1.0)) {
99 
100  tmp = (int) propVal;
101 
102  /* int result */
103  if (EQUAL(propVal, tmp)) {
104  bu_vls_printf(gedp->ged_result_str, "%d", (int) propVal);
105  }
106 
107  /* float result */
108  else {
109  bu_vls_printf(gedp->ged_result_str, "%f", propVal);
110  }
111  } else {
112  bu_vls_printf(gedp->ged_result_str, "%s: %s is not a valid argument!", sub, arg);
113  return GED_ERROR;
114  }
115  }
116  if (bu_strncmp(sub, "chull", len) == 0) {
117  int retval = 0;
118  int fc = 0;
119  int vc = 0;
120  point_t *vert_array;
121  int *faces;
122  unsigned char err = 0;
123 
124  /* must be wanting help */
125  if (argc < 4) {
126  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", cmd, usage);
127  return GED_ERROR;
128  }
129 
130  retval = bn_3d_chull(&faces, &fc, &vert_array, &vc, (const point_t *)bot->vertices, (int)bot->num_vertices);
131 
132  if (retval != 3) return GED_ERROR;
133 
134  retval = mk_bot(gedp->ged_wdbp, argv[3], RT_BOT_SOLID, RT_BOT_CCW, err, vc, fc, (fastf_t *)vert_array, faces, NULL, NULL);
135 
136  if (retval) return GED_ERROR;
137  }
138 
139  return GED_OK;
140 }
141 
142 
143 /*
144  * Local Variables:
145  * tab-width: 8
146  * mode: C
147  * indent-tabs-mode: t
148  * c-file-style: "stroustrup"
149  * End:
150  * ex: shiftwidth=4 tabstop=8
151  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
int mk_bot(struct rt_wdb *fp, const char *name, unsigned char mode, unsigned char orientation, unsigned char flags, size_t num_vertices, size_t num_faces, fastf_t *vertices, int *faces, fastf_t *thickness, struct bu_bitv *face_mode)
Definition: bot.c:121
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
#define GED_DB_LOOKUP(_gedp, _dp, _name, _noisy, _flags)
Definition: ged.h:223
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
int idb_major_type
Definition: raytrace.h:192
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
int ged_bot(struct ged *gedp, int argc, const char *argv[])
Definition: bot.c:39
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
int bn_3d_chull(int **faces, int *num_faces, point_t **vertices, int *num_vertices, const point_t *input_points_3d, int num_input_pnts)
Find 3D point convex hull for unordered point sets.
Definition: chull3d.cpp:1265
#define GED_QUIET
Definition: ged.h:64
struct bu_vls * ged_result_str
Definition: ged.h:357
fastf_t rt_bot_propget(struct rt_bot_internal *bot, const char *property)
Definition: bot.c:3015
void * idb_ptr
Definition: raytrace.h:195
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define LOOKUP_NOISY
Definition: raytrace.h:892
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
#define GED_DB_GET_INTERNAL(_gedp, _intern, _dp, _mat, _resource, _flags)
Definition: ged.h:233
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
double fastf_t
Definition: defines.h:300