BRL-CAD
bot_face_sort.c
Go to the documentation of this file.
1 /* B O T _ F A C E _ S O R 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_face_sort.c
21  *
22  * The bot_face_sort command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "rtgeom.h"
33 #include "ged.h"
34 
35 
36 int
37 ged_bot_face_sort(struct ged *gedp, int argc, const char *argv[])
38 {
39  int i;
40  int tris_per_piece=0;
41  static const char *usage = "triangles_per_piece bot_solid1 [bot_solid2 bot_solid3 ...]";
42 
45  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
46 
47  /* initialize result */
48  bu_vls_trunc(gedp->ged_result_str, 0);
49 
50  /* must be wanting help */
51  if (argc == 1) {
52  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
53  return GED_HELP;
54  }
55 
56  if (argc < 3) {
57  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
58  return GED_ERROR;
59  }
60 
61  tris_per_piece = atoi(argv[1]);
62  if (tris_per_piece < 1) {
64  "Illegal value for triangle per piece (%s)\n",
65  argv[1]);
66  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
67  return GED_ERROR;
68  }
69 
70  for (i = 2; i < argc; i++) {
71  struct directory *dp;
72  struct rt_db_internal intern;
73  struct rt_bot_internal *bot;
74 
75  if ((dp=db_lookup(gedp->ged_wdbp->dbip, argv[i], LOOKUP_NOISY)) == RT_DIR_NULL) {
76  continue;
77  }
78 
79  GED_DB_GET_INTERNAL(gedp, &intern, dp, bn_mat_identity, gedp->ged_wdbp->wdb_resp, GED_ERROR);
80 
81  if (intern.idb_major_type != DB5_MAJORTYPE_BRLCAD || intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
82  rt_db_free_internal(&intern);
84  "%s is not a BOT primitive, skipped\n",
85  dp->d_namep);
86  continue;
87  }
88 
89  bot = (struct rt_bot_internal *)intern.idb_ptr;
90  RT_BOT_CK_MAGIC(bot);
91 
92  bu_log("processing %s (%zu triangles)\n", dp->d_namep, bot->num_faces);
93 
94  if (rt_bot_sort_faces(bot, tris_per_piece)) {
95  rt_db_free_internal(&intern);
97  "Face sort failed for %s, this BOT not sorted\n",
98  dp->d_namep);
99  continue;
100  }
101 
102  GED_DB_PUT_INTERNAL(gedp, dp, &intern, gedp->ged_wdbp->wdb_resp, GED_ERROR);
103  }
104 
105  return GED_OK;
106 }
107 
108 
109 /*
110  * Local Variables:
111  * tab-width: 8
112  * mode: C
113  * indent-tabs-mode: t
114  * c-file-style: "stroustrup"
115  * End:
116  * ex: shiftwidth=4 tabstop=8
117  */
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
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Definition: ged.h:338
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
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 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 GED_ERROR
Definition: ged.h:61
#define GED_DB_PUT_INTERNAL(_gedp, _dp, _intern, _resource, _flags)
Definition: ged.h:243
int idb_major_type
Definition: raytrace.h:192
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
struct bu_vls * ged_result_str
Definition: ged.h:357
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 RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
int ged_bot_face_sort(struct ged *gedp, int argc, const char *argv[])
Definition: bot_face_sort.c:37
#define GED_HELP
Definition: ged.h:62
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
struct resource * wdb_resp
Definition: raytrace.h:1270
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
int rt_bot_sort_faces(struct rt_bot_internal *bot, size_t tris_per_piece)
Definition: bot.c:3531