BRL-CAD
simulation.cpp
Go to the documentation of this file.
1 /* S I M U L A T I O N . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2015 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 simulation.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 <stdexcept>
35 
36 
37 namespace simulate
38 {
39 
40 
41 Simulation::Simulation(db_i &db_instance, directory &vdirectory) :
42  m_db_instance(db_instance),
43  m_directory(vdirectory),
44  m_tree_updater(db_instance, m_directory),
45  m_objects()
46 {
47  if (m_directory.d_minor_type != ID_COMBINATION)
48  throw std::runtime_error("object is not a combination");
49 
50  tree * const vtree = m_tree_updater.get_tree();
51 
52  try {
53  if (vtree) get_tree_objects(*vtree);
54  } catch (...) {
55  for (std::vector<WorldObject *>::iterator it = m_objects.begin();
56  it != m_objects.end(); ++it)
57  delete *it;
58 
59  throw;
60  }
61 
62  m_collision_dispatcher.registerCollisionCreateFunc(
63  RtCollisionShape::RT_SHAPE_TYPE, RtCollisionShape::RT_SHAPE_TYPE,
64  new RtCollisionAlgorithm::CreateFunc);
65 
66  m_world.setGravity(btVector3(0.0, 0.0, -9.8));
67 
68 }
69 
70 
71 Simulation::~Simulation()
72 {
73  for (std::vector<WorldObject *>::iterator it = m_objects.begin();
74  it != m_objects.end(); ++it)
75  delete *it;
76 }
77 
78 
79 void
80 Simulation::get_tree_objects(tree &vtree)
81 {
82  switch (vtree.tr_op) {
83  case OP_DB_LEAF: {
84  directory *dir = db_lookup(&m_db_instance, vtree.tr_l.tl_name, LOOKUP_NOISY);
85 
86  if (!dir) throw std::runtime_error("db_lookup() failed");
87 
88  if (!vtree.tr_l.tl_mat) {
89  vtree.tr_l.tl_mat = static_cast<matp_t>(bu_malloc(sizeof(mat_t), "tl_mat"));
90  MAT_IDN(vtree.tr_l.tl_mat);
91  }
92 
93  WorldObject * const object = WorldObject::create(m_db_instance, *dir,
94  vtree.tr_l.tl_mat, m_tree_updater);
95  m_objects.push_back(object);
96  object->add_to_world(m_world);
97  break;
98  }
99 
100  case OP_UNION:
101  get_tree_objects(*vtree.tr_b.tb_left);
102  get_tree_objects(*vtree.tr_b.tb_right);
103  break;
104 
105  default:
106  throw std::invalid_argument("unsupported operation in scene combination");
107  }
108 }
109 
110 
111 }
112 
113 
114 #endif
115 
116 
117 // Local Variables:
118 // tab-width: 8
119 // mode: C++
120 // c-basic-offset: 4
121 // indent-tabs-mode: t
122 // c-file-style: "stroustrup"
123 // End:
124 // ex: shiftwidth=4 tabstop=8
Definition: raytrace.h:800
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
union tree * tb_left
Definition: raytrace.h:1149
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
#define OP_DB_LEAF
Leaf of combination, db fmt.
Definition: raytrace.h:1139
matp_t tl_mat
xform matp, NULL ==> identity
Definition: raytrace.h:1173
char * tl_name
Name of this leaf (bu_strdup'ed)
Definition: raytrace.h:1174
struct tree::tree_node tr_b
struct tree::tree_db_leaf tr_l
union tree * tb_right
Definition: raytrace.h:1150
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define OP_UNION
Binary: L union R.
Definition: raytrace.h:1127