-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexplore-shape.cpp
248 lines (204 loc) · 6.72 KB
/
explore-shape.cpp
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
/*
* Copyright 2019 Assaf Gordon <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*/
#include <BRepBuilderAPI_MakeSolid.hxx>
#include <TopoDS_Shell.hxx>
#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <ShapeFix_Shape.hxx>
#include <BRepGProp.hxx>
#include <GProp_GProps.hxx>
#include <BRepTools.hxx>
#include <BRep_Builder.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>
#include <Bnd_Box.hxx>
#include <BRepBndLib.hxx>
#include <gp_Circ.hxx>
#include <TopExp_Explorer.hxx>
#include <gp_GTrsf.hxx>
#include <TColgp_HArray1OfPnt.hxx>
#include <BRepTools_WireExplorer.hxx>
#include <math.hxx>
/* good resources:
https://github.com/miho/OCC-CSG
https://github.com/lvk88/OccTutorial
https://dev.opencascade.org/doc/overview/html/index.html#OCCT_OVW_SECTION_7
*/
std::string SurfaceTypeName ( enum GeomAbs_SurfaceType t )
{
switch (t)
{
case GeomAbs_Plane:
return "Plane";
case GeomAbs_Cylinder:
return "Cylinder";
case GeomAbs_Cone:
return "Cone";
case GeomAbs_Sphere:
return "Sphere";
case GeomAbs_Torus:
return "Torus";
case GeomAbs_BezierSurface:
return "Bezier";
case GeomAbs_BSplineSurface:
return "BSplineSurface";
case GeomAbs_SurfaceOfRevolution:
return "SurfaceOfRevolution";
case GeomAbs_SurfaceOfExtrusion:
return "SurfaceOfExtrusion";
case GeomAbs_OffsetSurface:
return "OffsetSurface";
case GeomAbs_OtherSurface:
return "OtherSurface";
}
return "UNKNOWN";
}
std::string OrientationName (enum TopAbs_Orientation o)
{
switch (o)
{
case TopAbs_FORWARD:
return "FORWARD";
case TopAbs_REVERSED:
return "REVERSED";
case TopAbs_INTERNAL:
return "INTERNAL";
case TopAbs_EXTERNAL:
return "EXTERNAL";
}
return "UNKNOWN";
}
void get_vertex_points_test1 (const TopoDS_Shape& shape)
{
// from: https://www.opencascade.com/content/extracting-boundary-topodsshape
TopTools_IndexedMapOfShape vertices;
TopExp::MapShapes(shape,TopAbs_VERTEX,vertices);
Handle_TColgp_HArray1OfPnt result = new TColgp_HArray1OfPnt(1,vertices.Extent());
for(Standard_Integer i = 1;i<=vertices.Extent();i++)
{
TopoDS_Vertex vertex = TopoDS::Vertex(vertices.FindKey(i));
gp_Pnt currentGeometricPoint = BRep_Tool::Pnt(vertex);
result->SetValue(i,currentGeometricPoint);
}
//Convert to array
//See: https://github.com/lvk88/OccTutorial/blob/master/Chapter1_Basics/src/WriteCoordinatesToFile.cpp
const TColgp_Array1OfPnt& points = result->Array1();
for(Standard_Integer i = points.Lower();i <= points.Upper();i++)
{
std::cout << " vertex " << i << ": " << points.Value(i).X() << " " << points.Value(i).Y() << " " << points.Value(i).Z() << std::endl;
auto &p = points.Value(i);
}
}
void get_vertex_points_test2 (const TopoDS_Shape& shape)
{
//from: https://www.opencascade.com/content/how-get-wires-single-face
int i = 0;
for (TopExp_Explorer anExp(shape, TopAbs_EDGE); anExp.More(); anExp.Next())
{
// Iterate over edges in input shape.
const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
std::cout << "Edge " << i << " orientatino = " << OrientationName(anEdge.Orientation()) << std::endl;
// Take the first and the last vertices from edge
TopoDS_Vertex aVFirst = TopExp::FirstVertex(anEdge);
TopoDS_Vertex aVLast = TopExp::LastVertex(anEdge);
// Take geometrical information from vertices.
gp_Pnt aPntFirst = BRep_Tool::Pnt(aVFirst);
gp_Pnt aPntLast = BRep_Tool::Pnt(aVLast);
if (anEdge.Orientation() == TopAbs_FORWARD)
{
}
else
{
}
++i;
}
}
void get_vertex_points_test3 (const TopoDS_Shape& shape)
{
//from: https://www.opencascade.com/content/how-get-wires-single-face
int i = 0;
for (TopExp_Explorer anExp(shape, TopAbs_VERTEX); anExp.More(); anExp.Next())
{
const TopoDS_Vertex& anVertex = TopoDS::Vertex(anExp.Current());
std::cout << "Vertex " << i << " orientatino = " << OrientationName(anVertex.Orientation()) << std::endl;
// Take geometrical information from vertices.
gp_Pnt aPnt = BRep_Tool::Pnt(anVertex);
++i;
}
}
void get_wire_edge_points_test4(const TopoDS_Shape& shape)
{
int wire_idx = 0;
for (TopExp_Explorer WireExp(shape, TopAbs_WIRE); WireExp.More(); WireExp.Next())
{
const TopoDS_Wire& aWire = TopoDS::Wire(WireExp.Current());
std::cout << " Wire " << wire_idx << std::endl;
int edge_idx = 0;
for (TopExp_Explorer EdgeExp(aWire, TopAbs_EDGE); EdgeExp.More(); EdgeExp.Next())
{
std::cout << " edge " << edge_idx << std::endl;
const TopoDS_Edge& anEdge = TopoDS::Edge(EdgeExp.Current());
// Take the first and the last vertices from edge
TopoDS_Vertex aVFirst = TopExp::FirstVertex(anEdge);
TopoDS_Vertex aVLast = TopExp::LastVertex(anEdge);
// Take geometrical information from vertices.
gp_Pnt aPntFirst = BRep_Tool::Pnt(aVFirst);
gp_Pnt aPntLast = BRep_Tool::Pnt(aVLast);
std::cout << " VertexFirst " << aPntFirst.X() << ", "
<< aPntFirst.Y() << ", "
<< aPntFirst.Z() << std::endl;
std::cout << " VertexLast " << aPntLast.X() << ", "
<< aPntLast.Y() << ", "
<< aPntLast.Z() << std::endl;
++edge_idx;
}
++wire_idx;
}
}
TopoDS_Shape make_solid(const TopoDS_Shape& shape)
{
BRepBuilderAPI_MakeSolid solidmaker;
int i = 0;
for (TopExp_Explorer fExpl(shape, TopAbs_SHELL); fExpl.More(); fExpl.Next())
{
const TopoDS_Shell &curShell = TopoDS::Shell(fExpl.Current());
++i;
//debugGetVerticesOfShape(curShell);
solidmaker.Add(curShell);
}
std::cout << std::endl;
TopoDS_Shape solid = solidmaker.Solid();
return solid;
}
void explore_shape(const TopoDS_Shape& shape)
{
int shell_i = 0;
for (TopExp_Explorer ShellExp(shape, TopAbs_SHELL); ShellExp.More(); ShellExp.Next())
{
const TopoDS_Shell &curShell = TopoDS::Shell(ShellExp.Current());
std::cout << "Shell " << shell_i << std::endl;
int i = 0;
for (TopExp_Explorer FaceExp(curShell, TopAbs_FACE); FaceExp.More(); FaceExp.Next())
{
const TopoDS_Face &aFace = TopoDS::Face(FaceExp.Current());
//from: https://www.opencascade.com/content/how-determine-whether-topodsface-flat-or-volumetric
BRepAdaptor_Surface anAdaptor(aFace);
std::cout << " Face " << i << " Type = " << anAdaptor.GetType() << ":" << SurfaceTypeName(anAdaptor.GetType()) << std::endl;
get_wire_edge_points_test4(aFace);
}
}
}