BRL-CAD
interface.cpp
Go to the documentation of this file.
1 /* S I M U L A T E . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 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 simulate.cpp
21  *
22  * Brief description
23  *
24  */
25 
26 
27 #ifdef HAVE_BULLET
28 
29 
30 #include "common.h"
31 
32 #include "simulation.hpp"
33 
34 #include <sstream>
35 #include <stdexcept>
36 
37 #include "ged.h"
38 
39 
40 namespace
41 {
42 
43 
44 template<typename Target, typename Source>
45 Target lexical_cast(Source arg,
46  const std::string &description = "bad lexical_cast")
47 {
48  std::stringstream interpreter;
49  Target result;
50 
51  if (!(interpreter << arg) ||
52  !(interpreter >> result) ||
53  !(interpreter >> std::ws).eof())
54  throw std::invalid_argument(description);
55 
56  return result;
57 }
58 
59 
60 }
61 
62 
63 int
64 ged_simulate(ged *gedp, int argc, const char **argv)
65 {
67  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
68 
69  if (argc != 3) {
70  bu_vls_sprintf(gedp->ged_result_str, "%s: USAGE: %s <scene_comb> <seconds>",
71  argv[0], argv[0]);
72  return GED_ERROR;
73  }
74 
75  directory *dir = db_lookup(gedp->ged_wdbp->dbip, argv[1], LOOKUP_NOISY);
76 
77  if (!dir) return GED_ERROR;
78 
79  try {
80  btScalar seconds = lexical_cast<btScalar>(argv[2],
81  "invalid value for 'seconds'");
82 
83  if (seconds < 0.0) throw std::runtime_error("invalid value for 'seconds'");
84 
85  simulate::Simulation simulation(*gedp->ged_wdbp->dbip, *dir);
86  simulation.step(seconds);
87  } catch (const std::logic_error &e) {
88  bu_vls_sprintf(gedp->ged_result_str, "%s: %s", argv[0], e.what());
89  return GED_ERROR;
90  } catch (const std::runtime_error &e) {
91  bu_vls_sprintf(gedp->ged_result_str, "%s: %s", argv[0], e.what());
92  return GED_ERROR;
93  }
94 
95  return GED_OK;
96 }
97 
98 
99 #else
100 
101 
102 #include "common.h"
103 
104 #include "ged.h"
105 
106 
107 int
108 ged_simulate(ged *gedp, int argc, const char **argv)
109 {
111  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
112 
114  "%s: This build of BRL-CAD was not compiled with Bullet support", argv[0]);
115 
116  return GED_ERROR;
117 }
118 
119 
120 #endif
121 
122 
123 // Local Variables:
124 // tab-width: 8
125 // mode: C++
126 // c-basic-offset: 4
127 // indent-tabs-mode: t
128 // c-file-style: "stroustrup"
129 // End:
130 // ex: shiftwidth=4 tabstop=8
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
int ged_simulate(ged *gedp, int argc, const char **argv)
Definition: interface.cpp:108
#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_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
void bu_vls_sprintf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:707
struct bu_vls * ged_result_str
Definition: ged.h:357
#define LOOKUP_NOISY
Definition: raytrace.h:892