BRL-CAD
comb_brep.cpp
Go to the documentation of this file.
1 /* C O M B _ B R E P . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-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 comb_brep.cpp
21  *
22  * Convert a combination to b-rep form (with NURBS boolean evaluations)
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "raytrace.h"
29 #include "rtgeom.h"
30 #include "nmg.h"
31 #include "brep.h"
32 
33 
34 // Declaration
35 extern "C" void rt_comb_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol, const struct db_i *dbip);
36 extern "C" int single_conversion(struct rt_db_internal* intern, ON_Brep** brep, const struct db_i *dbip);
37 
38 
39 HIDDEN int
40 conv_tree(ON_Brep **b, const union tree *t, const struct db_i *dbip)
41 {
42  ON_Brep *left = NULL, *right = NULL, *old = NULL;
43  int ret = 0;
44  switch (t->tr_op) {
45  case OP_UNION:
46  case OP_INTERSECT:
47  case OP_SUBTRACT:
48  case OP_XOR:
49  /* convert right */
50  old = right = ON_Brep::New();
51  ret = conv_tree(&right, t->tr_b.tb_right, dbip);
52  if (ret) {
53  delete old;
54  break;
55  }
56  /* fall through */
57  case OP_NOT:
58  case OP_GUARD:
59  case OP_XNOP:
60  /* convert left */
61  old = left = ON_Brep::New();
62  ret = conv_tree(&left, t->tr_b.tb_left, dbip);
63  if (!ret) {
64  // Perform NURBS evaluations
65  if (t->tr_op == OP_UNION)
66  ret = ON_Boolean(*b, left, right, BOOLEAN_UNION);
67  else if (t->tr_op == OP_INTERSECT)
68  ret = ON_Boolean(*b, left, right, BOOLEAN_INTERSECT);
69  else if (t->tr_op == OP_SUBTRACT)
70  ret = ON_Boolean(*b, left, right, BOOLEAN_DIFF);
71  else if (t->tr_op == OP_XOR)
72  ret = ON_Boolean(*b, left, right, BOOLEAN_XOR);
73  else {
74  bu_log("operation %d isn't supported yet.\n", t->tr_op);
75  ret = -1;
76  }
77  delete left;
78  delete right;
79  } else {
80  delete old;
81  delete right;
82  }
83  break;
84  case OP_DB_LEAF:
85  {
86  char *name = t->tr_l.tl_name;
87  directory *dir;
88  dir = db_lookup(dbip, name, LOOKUP_QUIET);
89  if (dir != RT_DIR_NULL) {
90  rt_db_internal intern;
91  rt_db_get_internal(&intern, dir, dbip, bn_mat_identity, &rt_uniresource);
92  RT_CK_DB_INTERNAL(&intern);
93  ret = single_conversion(&intern, b, dbip);
94  if (ret == 0 && *b != NULL) {
95  if (t->tr_l.tl_mat != NULL && !bn_mat_is_identity(t->tr_l.tl_mat)) {
96  ON_Xform xform(t->tr_l.tl_mat);
97  ret = -1;
98  if ((*b)->Transform(xform)) {
99  ret = 0;
100  }
101  }
102  }
103  rt_db_free_internal(&intern);
104  } else {
105  bu_log("Cannot find %s.\n", name);
106  ret = -1;
107  }
108  break;
109  }
110  default:
111  bu_log("OPCODE NOT IMPLEMENTED: %d\n", t->tr_op);
112  ret = -1;
113  }
114  if (ret) {
115  *b = NULL;
116  }
117 
118  return ret;
119 }
120 
121 
122 extern "C" void
123 rt_comb_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *UNUSED(tol), const struct db_i *dbip)
124 {
125  RT_CK_DB_INTERNAL(ip);
126  RT_CK_DBI(dbip);
127 
128  struct rt_comb_internal *cip;
129  cip = (struct rt_comb_internal *)ip->idb_ptr;
130  RT_CK_COMB(cip);
131 
132  if (cip->tree == NULL) {
133  *b = NULL;
134  return;
135  }
136 
137  conv_tree(b, cip->tree, dbip);
138 }
139 
140 
141 // Local Variables:
142 // tab-width: 8
143 // mode: C++
144 // c-basic-offset: 4
145 // indent-tabs-mode: t
146 // c-file-style: "stroustrup"
147 // End:
148 // ex: shiftwidth=4 tabstop=8
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
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 bn_mat_is_identity(const mat_t m)
Definition: mat.c:980
const mat_t bn_mat_identity
Matrix and vector functionality.
Definition: mat.c:46
Definition: clone.c:90
#define OP_XOR
Binary: L xor R, not both.
Definition: raytrace.h:1130
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
int single_conversion(struct rt_db_internal *intern, ON_Brep **brep, const struct db_i *dbip)
Header file for the BRL-CAD common definitions.
#define OP_XNOP
Unary: L, mark region.
Definition: raytrace.h:1136
#define HIDDEN
Definition: common.h:86
union tree * tb_left
Definition: raytrace.h:1149
#define OP_SUBTRACT
Binary: L subtract R.
Definition: raytrace.h:1129
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define OP_INTERSECT
Binary: L intersect R.
Definition: raytrace.h:1128
#define OP_DB_LEAF
Leaf of combination, db fmt.
Definition: raytrace.h:1139
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
#define LOOKUP_QUIET
Definition: raytrace.h:893
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
#define UNUSED(parameter)
Definition: common.h:239
#define OP_GUARD
Unary: not L, or else!
Definition: raytrace.h:1135
Support for uniform tolerances.
Definition: tol.h:71
HIDDEN int conv_tree(ON_Brep **b, const union tree *t, const struct db_i *dbip)
Definition: comb_brep.cpp:40
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
struct tree::tree_db_leaf tr_l
union tree * tb_right
Definition: raytrace.h:1150
void * idb_ptr
Definition: raytrace.h:195
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
#define RT_DIR_NULL
Definition: raytrace.h:875
int ON_Boolean(ON_Brep *evaluated_brep, const ON_Brep *brep1, const ON_Brep *brep2, op_type operation)
Definition: boolean.cpp:3891
#define OP_UNION
Binary: L union R.
Definition: raytrace.h:1127
#define OP_NOT
Unary: not L.
Definition: raytrace.h:1134
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
void rt_comb_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol, const struct db_i *dbip)
Definition: comb_brep.cpp:123