-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy patht8_cad.cxx
More file actions
302 lines (271 loc) · 10.5 KB
/
Copy patht8_cad.cxx
File metadata and controls
302 lines (271 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.
Copyright (C) 2025 the developers
t8code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
t8code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <t8.h>
#include <t8_cad/t8_cad.hxx>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_cad.hxx>
#include <TopoDS.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <Standard_Version.hxx>
t8_cad::t8_cad (std::string fileprefix)
{
BRep_Builder builder;
std::ifstream is (fileprefix + ".brep");
if (is.is_open () == false) {
SC_ABORTF ("Cannot find the file %s.brep.\n", fileprefix.c_str ());
}
BRepTools::Read (cad_shape, is, builder);
is.close ();
if (cad_shape.IsNull ()) {
SC_ABORTF ("Could not read brep file or brep file contains no shape. "
"The cad file may be written with a newer cad version. "
"Linked cad version: %s",
OCC_VERSION_COMPLETE);
}
TopExp::MapShapes (cad_shape, TopAbs_VERTEX, cad_shape_vertex_map);
TopExp::MapShapes (cad_shape, TopAbs_EDGE, cad_shape_edge_map);
TopExp::MapShapes (cad_shape, TopAbs_FACE, cad_shape_face_map);
TopExp::MapShapesAndUniqueAncestors (cad_shape, TopAbs_VERTEX, TopAbs_EDGE, cad_shape_vertex2edge_map);
TopExp::MapShapesAndUniqueAncestors (cad_shape, TopAbs_EDGE, TopAbs_FACE, cad_shape_edge2face_map);
}
t8_cad::t8_cad (const TopoDS_Shape cad_shape)
{
if (cad_shape.IsNull ()) {
SC_ABORTF ("Shape is null. \n");
}
TopExp::MapShapes (cad_shape, TopAbs_VERTEX, cad_shape_vertex_map);
TopExp::MapShapes (cad_shape, TopAbs_EDGE, cad_shape_edge_map);
TopExp::MapShapes (cad_shape, TopAbs_FACE, cad_shape_face_map);
TopExp::MapShapesAndUniqueAncestors (cad_shape, TopAbs_VERTEX, TopAbs_EDGE, cad_shape_vertex2edge_map);
TopExp::MapShapesAndUniqueAncestors (cad_shape, TopAbs_EDGE, TopAbs_FACE, cad_shape_edge2face_map);
}
t8_cad::t8_cad ()
{
cad_shape.Nullify ();
}
int
t8_cad::t8_geom_is_line (const int curve_index) const
{
const Handle_Geom_Curve curve = t8_geom_get_cad_curve (curve_index);
const GeomAdaptor_Curve curve_adaptor (curve);
return curve_adaptor.GetType () == GeomAbs_Line;
}
int
t8_cad::t8_geom_is_plane (const int surface_index) const
{
const Handle_Geom_Surface surface = t8_geom_get_cad_surface (surface_index);
const GeomAdaptor_Surface surface_adaptor (surface);
return surface_adaptor.GetType () == GeomAbs_Plane;
}
const gp_Pnt
t8_cad::t8_geom_get_cad_point (const int index) const
{
T8_ASSERT (index <= cad_shape_vertex_map.Size ());
return BRep_Tool::Pnt (TopoDS::Vertex (cad_shape_vertex_map.FindKey (index)));
}
const Handle_Geom_Curve
t8_cad::t8_geom_get_cad_curve (const int index) const
{
T8_ASSERT (index <= cad_shape_edge_map.Size ());
Standard_Real first, last;
return BRep_Tool::Curve (TopoDS::Edge (cad_shape_edge_map.FindKey (index)), first, last);
}
const Handle_Geom_Surface
t8_cad::t8_geom_get_cad_surface (const int index) const
{
T8_ASSERT (index <= cad_shape_face_map.Size ());
return BRep_Tool::Surface (TopoDS::Face (cad_shape_face_map.FindKey (index)));
}
const TopTools_IndexedMapOfShape
t8_cad::t8_geom_get_cad_shape_vertex_map () const
{
return cad_shape_vertex_map;
}
const TopTools_IndexedMapOfShape
t8_cad::t8_geom_get_cad_shape_edge_map () const
{
return cad_shape_edge_map;
}
const TopTools_IndexedMapOfShape
t8_cad::t8_geom_get_cad_shape_face_map () const
{
return cad_shape_face_map;
}
int
t8_cad::t8_geom_get_common_edge (const int vertex1_index, const int vertex2_index) const
{
const TopTools_ListOfShape collection1 = cad_shape_vertex2edge_map.FindFromIndex (vertex1_index);
const TopTools_ListOfShape collection2 = cad_shape_vertex2edge_map.FindFromIndex (vertex2_index);
for (auto edge1 = collection1.begin (); edge1 != collection1.end (); ++edge1) {
for (auto edge2 = collection2.begin (); edge2 != collection2.end (); ++edge2) {
if (edge1->IsEqual (*edge2)) {
return cad_shape_edge2face_map.FindIndex (*edge1);
}
}
}
return 0;
}
int
t8_cad::t8_geom_get_common_face (const int edge1_index, const int edge2_index) const
{
const TopTools_ListOfShape collection1 = cad_shape_edge2face_map.FindFromIndex (edge1_index);
const TopTools_ListOfShape collection2 = cad_shape_edge2face_map.FindFromIndex (edge2_index);
for (auto face1 = collection1.begin (); face1 != collection1.end (); ++face1) {
for (auto face2 = collection2.begin (); face2 != collection2.end (); ++face2) {
if (face1->IsEqual (*face2)) {
return cad_shape_face_map.FindIndex (*face1);
}
}
}
return 0;
}
int
t8_cad::t8_geom_is_vertex_on_edge (const int vertex_index, const int edge_index) const
{
const TopTools_ListOfShape collection = cad_shape_vertex2edge_map.FindFromIndex (vertex_index);
return collection.Contains (cad_shape_edge_map.FindKey (edge_index));
}
int
t8_cad::t8_geom_is_edge_on_face (const int edge_index, const int face_index) const
{
const TopTools_ListOfShape collection = cad_shape_edge2face_map.FindFromIndex (edge_index);
return collection.Contains (cad_shape_face_map.FindKey (face_index));
}
int
t8_cad::t8_geom_is_vertex_on_face (const int vertex_index, const int face_index) const
{
const TopTools_ListOfShape edge_collection = cad_shape_vertex2edge_map.FindFromIndex (vertex_index);
for (auto edge = edge_collection.begin (); edge != edge_collection.end (); ++edge) {
const TopTools_ListOfShape face_collection = cad_shape_edge2face_map.FindFromKey (*edge);
if (face_collection.Contains (cad_shape_face_map.FindKey (face_index))) {
return 1;
}
}
return 0;
}
void
t8_cad::t8_geom_get_parameter_of_vertex_on_edge (const int vertex_index, const int edge_index, double *edge_param) const
{
T8_ASSERT (t8_cad::t8_geom_is_vertex_on_edge (vertex_index, edge_index));
TopoDS_Vertex vertex = TopoDS::Vertex (cad_shape_vertex_map.FindKey (vertex_index));
TopoDS_Edge edge = TopoDS::Edge (cad_shape_edge_map.FindKey (edge_index));
*edge_param = BRep_Tool::Parameter (vertex, edge);
}
void
t8_cad::t8_geom_get_parameters_of_vertex_on_face (const int vertex_index, const int face_index,
double *face_params) const
{
T8_ASSERT (t8_cad::t8_geom_is_vertex_on_face (vertex_index, face_index));
gp_Pnt2d uv;
TopoDS_Vertex vertex = TopoDS::Vertex (cad_shape_vertex_map.FindKey (vertex_index));
TopoDS_Face face = TopoDS::Face (cad_shape_face_map.FindKey (face_index));
uv = BRep_Tool::Parameters (vertex, face);
face_params[0] = uv.X ();
face_params[1] = uv.Y ();
}
void
t8_cad::t8_geom_edge_parameter_to_face_parameters (const int edge_index, const int face_index, const int num_face_nodes,
const double edge_param, const double *surface_params,
double *face_params) const
{
T8_ASSERT (t8_cad::t8_geom_is_edge_on_face (edge_index, face_index));
Standard_Real first, last;
gp_Pnt2d uv;
TopoDS_Edge edge = TopoDS::Edge (cad_shape_edge_map.FindKey (edge_index));
TopoDS_Face face = TopoDS::Face (cad_shape_face_map.FindKey (face_index));
Handle_Geom2d_Curve curve_on_surface = BRep_Tool::CurveOnSurface (edge, face, first, last);
Handle_Geom_Surface surface = BRep_Tool::Surface (face);
curve_on_surface->D0 (edge_param, uv);
face_params[0] = uv.X ();
face_params[1] = uv.Y ();
/* Check for right conversion of edge to surface parameter and correct if needed */
/* Checking u parameter */
if (surface_params != NULL) {
double parametric_bounds[4];
surface->Bounds (parametric_bounds[0], parametric_bounds[1], parametric_bounds[2], parametric_bounds[3]);
if (surface->IsUClosed ()) {
for (int i_face_node = 0; i_face_node < num_face_nodes; ++i_face_node) {
if (surface_params[i_face_node * 2] == parametric_bounds[0]) {
if (face_params[0] == parametric_bounds[1]) {
face_params[0] = parametric_bounds[0];
}
}
else if (surface_params[i_face_node * 2] == parametric_bounds[1]) {
if (face_params[0] == parametric_bounds[0]) {
face_params[0] = parametric_bounds[1];
}
}
}
}
/* Checking v parameter */
if (surface->IsVClosed ()) {
for (int i_face_node = 0; i_face_node < num_face_nodes; ++i_face_node) {
if (surface_params[i_face_node * 2 + 1] == parametric_bounds[0]) {
if (face_params[1] == parametric_bounds[1]) {
face_params[1] = parametric_bounds[0];
}
}
else if (surface_params[i_face_node * 2 + 1] == parametric_bounds[1]) {
if (face_params[1] == parametric_bounds[0]) {
face_params[1] = parametric_bounds[1];
}
}
}
}
}
}
void
t8_cad::t8_geom_get_face_parametric_bounds (const int surface_index, double *bounds) const
{
const Handle_Geom_Surface cad_surface = t8_geom_get_cad_surface (surface_index);
cad_surface->Bounds (bounds[0], bounds[1], bounds[2], bounds[3]);
}
void
t8_cad::t8_geom_get_edge_parametric_bounds (const int edge_index, double *bounds) const
{
const Handle_Geom_Curve cad_edge = t8_geom_get_cad_curve (edge_index);
bounds[0] = cad_edge->FirstParameter ();
bounds[1] = cad_edge->LastParameter ();
}
int
t8_cad::t8_geom_is_edge_closed (int edge_index) const
{
const Handle_Geom_Curve cad_edge = t8_geom_get_cad_curve (edge_index);
return cad_edge->IsClosed ();
}
int
t8_cad::t8_geom_is_surface_closed (int geometry_index, int parameter) const
{
const Handle_Geom_Surface cad_surface = t8_geom_get_cad_surface (geometry_index);
switch (parameter) {
case 0:
return cad_surface->IsUClosed ();
break;
case 1:
return cad_surface->IsVClosed ();
break;
default:
SC_ABORT_NOT_REACHED ();
break;
}
}