BRL-CAD
rt_instance.cpp
Go to the documentation of this file.
1 /* R T _ I N S T A N C E . 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 rt_instance.cpp
21  *
22  * Brief description
23  *
24  */
25 
26 
27 #ifdef HAVE_BULLET
28 
29 
30 #include "common.h"
31 
32 #include "rt_instance.hpp"
33 
34 #include <stdexcept>
35 
36 
37 namespace
38 {
39 
40 
42 duplicate_comb_internal(const rt_db_internal &source)
43 {
44  if (source.idb_minor_type != ID_COMBINATION)
45  throw std::invalid_argument("source is not a combination");
46 
47  rt_db_internal dest = source;
49  BU_AVS_INIT(&dest.idb_avs);
50  bu_avs_merge(&dest.idb_avs, &source.idb_avs);
51 
52  {
53  const rt_comb_internal &source_comb = *static_cast<const rt_comb_internal *>
54  (source.idb_ptr);
55  rt_comb_internal &dest_comb = *static_cast<rt_comb_internal *>(dest.idb_ptr);
56 
57  dest_comb = source_comb;
58  BU_VLS_INIT(&dest_comb.shader);
59  BU_VLS_INIT(&dest_comb.material);
60  bu_vls_vlscat(&dest_comb.shader, &source_comb.shader);
61  bu_vls_vlscat(&dest_comb.material, &source_comb.material);
62  dest_comb.tree = db_dup_subtree(source_comb.tree, &rt_uniresource);
63  }
64 
65  return dest;
66 }
67 
68 
69 HIDDEN void
70 write_comb_internal(db_i &db_instance, directory &vdirectory,
71  const rt_db_internal &comb_internal)
72 {
73 
74  rt_db_internal temp_internal = duplicate_comb_internal(comb_internal);
75 
76  if (rt_db_put_internal(&vdirectory, &db_instance, &temp_internal,
77  &rt_uniresource) < 0)
78  throw std::runtime_error("rt_db_put_internal() failed");
79 }
80 
81 
82 }
83 
84 
85 namespace simulate
86 {
87 
88 
89 TreeUpdater::TreeUpdater(db_i &db_instance, directory &vdirectory) :
90  m_db_instance(db_instance),
91  m_directory(vdirectory),
92  m_comb_internal(),
93  m_is_modified(false),
94  m_rt_instance(NULL)
95 {
96  if (rt_db_get_internal(&m_comb_internal, &m_directory, &m_db_instance,
98  throw std::runtime_error("rt_db_get_internal() failed");
99 }
100 
101 
102 TreeUpdater::~TreeUpdater()
103 {
104  if (m_is_modified)
105  write_comb_internal(m_db_instance, m_directory, m_comb_internal);
106 
107  if (m_rt_instance)
108  rt_free_rti(m_rt_instance);
109 
110  rt_db_free_internal(&m_comb_internal);
111 }
112 
113 
114 void
115 TreeUpdater::mark_modified()
116 {
117  m_is_modified = true;
118 }
119 
120 
121 tree *
122 TreeUpdater::get_tree()
123 {
124  return static_cast<rt_comb_internal *>(m_comb_internal.idb_ptr)->tree;
125 }
126 
127 
128 rt_i &
129 TreeUpdater::get_rt_instance() const
130 {
131  if (m_is_modified)
132  write_comb_internal(m_db_instance, m_directory, m_comb_internal);
133  else if (m_rt_instance)
134  return *m_rt_instance;
135 
136  if (m_rt_instance)
137  rt_free_rti(m_rt_instance);
138 
139  m_rt_instance = rt_new_rti(&m_db_instance);
140 
141  if (!m_rt_instance)
142  throw std::runtime_error("rt_new_rti() failed");
143 
144  if (rt_gettree(m_rt_instance, m_directory.d_namep) != 0) {
145  rt_free_rti(m_rt_instance);
146  m_rt_instance = NULL;
147  throw std::runtime_error("rt_gettree() failed");
148  }
149 
150  rt_prep(m_rt_instance);
151  m_is_modified = false;
152  return *m_rt_instance;
153 }
154 
155 
156 }
157 
158 
159 #endif
160 
161 
162 // Local Variables:
163 // tab-width: 8
164 // mode: C++
165 // c-basic-offset: 4
166 // indent-tabs-mode: t
167 // c-file-style: "stroustrup"
168 // End:
169 // ex: shiftwidth=4 tabstop=8
Definition: raytrace.h:800
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
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
void rt_free_rti(struct rt_i *rtip)
Definition: prep.c:156
void bu_avs_merge(struct bu_attribute_value_set *dest, const struct bu_attribute_value_set *src)
Definition: avs.c:154
Header file for the BRL-CAD common definitions.
#define ID_COMBINATION
Combination Record.
Definition: raytrace.h:499
#define HIDDEN
Definition: common.h:86
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
struct bu_vls shader
Definition: raytrace.h:950
struct bu_attribute_value_set idb_avs
Definition: raytrace.h:196
struct bu_vls material
Definition: raytrace.h:951
struct rt_i * rt_new_rti(struct db_i *dbip)
Definition: prep.c:58
int rt_gettree(struct rt_i *rtip, const char *node)
Definition: tree.c:869
void * idb_ptr
Definition: raytrace.h:195
#define BU_AVS_INIT(_ap)
Definition: avs.h:102
union tree * db_dup_subtree(const union tree *tp, struct resource *resp)
Definition: db_tree.c:1190
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
void rt_prep(struct rt_i *rtip)
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
void bu_vls_vlscat(struct bu_vls *dest, const struct bu_vls *src)
Definition: vls.c:415
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_VLS_INIT(_vp)
Definition: vls.h:74