Salome HOME
ccda532194e91fac40b5404f3245c17cc5975433
[modules/geom.git] / src / XAO / tests / BrepGeometryTest.cxx
1 // Copyright (C) 2013-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <vector>
21
22 #include "TestUtils.hxx"
23 #include "BrepGeometryTest.hxx"
24 #include "../XAO_XaoUtils.hxx"
25 #include "../XAO_Xao.hxx"
26 #include "../XAO_BrepGeometry.hxx"
27
28 using namespace XAO;
29
30 void BrepGeometryTest::setUp()
31 {
32 }
33
34 void BrepGeometryTest::tearDown()
35 {
36 }
37
38 void BrepGeometryTest::cleanUp()
39 {
40 }
41
42 void readBrep(Geometry* geom, const std::string& fileName)
43 {
44     char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath(fileName));
45     geom->setShapeString(txt);
46 }
47
48 void BrepGeometryTest::testGetIDs()
49 {
50     BrepGeometry* geom = new BrepGeometry("box");
51     readBrep(geom, "Box_1.brep");
52
53     CPPUNIT_ASSERT_EQUAL(8, geom->countElements(XAO::VERTEX));
54     CPPUNIT_ASSERT_EQUAL(8, geom->countVertices());
55     int vertices[8] = { 6,7,9,11,16,17,19,21 };
56     for (int i = 0; i < 8; ++i)
57         CPPUNIT_ASSERT_EQUAL(vertices[i], geom->getVertexID(i));
58
59     CPPUNIT_ASSERT_EQUAL(12, geom->countElements(XAO::EDGE));
60     CPPUNIT_ASSERT_EQUAL(12, geom->countEdges());
61     int edges[12] = { 5,8,10,12,15,18,20,22,25,26,29,30 };
62     for (int i = 0; i < 12; ++i)
63         CPPUNIT_ASSERT_EQUAL(edges[i], geom->getEdgeID(i));
64
65     CPPUNIT_ASSERT_EQUAL(6, geom->countElements(XAO::FACE));
66     CPPUNIT_ASSERT_EQUAL(6, geom->countFaces());
67     int faces[6] = { 3,13,23,27,31,33 };
68     for (int i = 0; i < 6; ++i)
69         CPPUNIT_ASSERT_EQUAL(faces[i], geom->getFaceID(i));
70
71     CPPUNIT_ASSERT_EQUAL(1, geom->countElements(XAO::SOLID));
72     CPPUNIT_ASSERT_EQUAL(1, geom->countSolids());
73     CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
74
75     delete geom;
76 }
77
78 void BrepGeometryTest::testGetReferences()
79 {
80     BrepGeometry* geom = new BrepGeometry("box");
81     readBrep(geom, "Box_1.brep");
82
83     // vertex of index 1 has id = 7
84     CPPUNIT_ASSERT_EQUAL(std::string("7"), geom->getElementReference(XAO::VERTEX, 1));
85     CPPUNIT_ASSERT_EQUAL(std::string("7"), geom->getVertexReference(1));
86     CPPUNIT_ASSERT_EQUAL(7, geom->getVertexID(1));
87     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::VERTEX, "7"));
88     CPPUNIT_ASSERT_EQUAL(1, geom->findVertex(7));
89
90     // edge of index 1 has id = 8
91     CPPUNIT_ASSERT_EQUAL(std::string("8"), geom->getElementReference(XAO::EDGE, 1));
92     CPPUNIT_ASSERT_EQUAL(std::string("8"), geom->getEdgeReference(1));
93     CPPUNIT_ASSERT_EQUAL(8, geom->getEdgeID(1));
94     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::EDGE, "8"));
95     CPPUNIT_ASSERT_EQUAL(1, geom->findEdge(8));
96
97     // face of index 1 has id = 13
98     CPPUNIT_ASSERT_EQUAL(std::string("13"), geom->getElementReference(XAO::FACE, 1));
99     CPPUNIT_ASSERT_EQUAL(std::string("13"), geom->getFaceReference(1));
100     CPPUNIT_ASSERT_EQUAL(13, geom->getFaceID(1));
101     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::FACE, "13"));
102     CPPUNIT_ASSERT_EQUAL(1, geom->findFace(13));
103
104     // solid of index 0 has id = 1
105     CPPUNIT_ASSERT_EQUAL(std::string("1"), geom->getElementReference(XAO::SOLID, 0));
106     CPPUNIT_ASSERT_EQUAL(std::string("1"), geom->getSolidReference(0));
107     CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
108     CPPUNIT_ASSERT_EQUAL(0, geom->getElementIndexByReference(XAO::SOLID, "1"));
109     CPPUNIT_ASSERT_EQUAL(0, geom->findSolid(1));
110
111     delete geom;
112 }
113
114 void BrepGeometryTest::testGetNames()
115 {
116     BrepGeometry* geom = new BrepGeometry("box");
117     readBrep(geom, "Box_1.brep");
118
119     int id;
120
121     // vertex of index 1 has id = 7
122     id = 7;
123     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findVertexName(id));
124     geom->changeVertexName(id, std::string("va"));
125     CPPUNIT_ASSERT_EQUAL(std::string("va"), geom->findVertexName(id));
126     CPPUNIT_ASSERT_THROW(geom->changeVertexName(100, "a"), XAO_Exception);
127     CPPUNIT_ASSERT_THROW(geom->findVertexName(100), XAO_Exception);
128
129     // edge of index 1 has id = 8
130     id = 8;
131     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findEdgeName(id));
132     geom->changeEdgeName(id, std::string("ea"));
133     CPPUNIT_ASSERT_EQUAL(std::string("ea"), geom->findEdgeName(id));
134     CPPUNIT_ASSERT_THROW(geom->changeEdgeName(100, "a"), XAO_Exception);
135     CPPUNIT_ASSERT_THROW(geom->findEdgeName(100), XAO_Exception);
136
137     // face of index 1 has id = 13
138     id = 13;
139     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findFaceName(id));
140     geom->changeFaceName(id, std::string("fa"));
141     CPPUNIT_ASSERT_EQUAL(std::string("fa"), geom->findFaceName(id));
142     CPPUNIT_ASSERT_THROW(geom->changeFaceName(100, "a"), XAO_Exception);
143     CPPUNIT_ASSERT_THROW(geom->findFaceName(100), XAO_Exception);
144
145     // solid of index 0 has id = 1
146     id = 1;
147     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findSolidName(id));
148     geom->changeSolidName(id, std::string("sa"));
149     CPPUNIT_ASSERT_EQUAL(std::string("sa"), geom->findSolidName(id));
150     CPPUNIT_ASSERT_THROW(geom->changeSolidName(100, "a"), XAO_Exception);
151     CPPUNIT_ASSERT_THROW(geom->findSolidName(100), XAO_Exception);
152
153     delete geom;
154 }
155
156 void BrepGeometryTest::testGetEdgeVertices()
157 {
158     BrepGeometry* geom = new BrepGeometry("box");
159     readBrep(geom, "Box_2.brep");
160
161     // edge of index 23, id = #63
162     // vertex are 47 (#12), 59 (#15)
163     int v1, v2;
164     geom->getEdgeVertices(23, v1, v2);
165     CPPUNIT_ASSERT_EQUAL(12, v1);
166     CPPUNIT_ASSERT_EQUAL(15, v2);
167
168     delete geom;
169 }
170
171 void printVector(std::vector<int>& v)
172 {
173     std::cout << "# ";
174     for (unsigned int i = 0; i < v.size(); i++)
175         std::cout << v[i] << ", ";
176     std::cout << std::endl;
177 }
178
179 void BrepGeometryTest::testGetFaceEdges()
180 {
181     BrepGeometry* geom = new BrepGeometry("box");
182     readBrep(geom, "Box_2.brep");
183
184     CPPUNIT_ASSERT_EQUAL(2, geom->countFaceWires(1)); // face 13
185     CPPUNIT_ASSERT_EQUAL(1, geom->countFaceWires(2)); // face 29
186
187     // wire 0 of face 1 (#13) => edge 4 (#15), 5 (#17), 0 (#5), 6 (#19)
188     std::vector<int> edges = geom->getFaceEdges(1, 0);
189     CPPUNIT_ASSERT_EQUAL(4, (int)edges.size());
190     int ids1[4] = { 4,5,0,6 };
191     for (int i = 0; i < 4; ++i)
192         CPPUNIT_ASSERT_EQUAL(ids1[i], edges[i]);
193
194     // wire 1 of face 13 => edge 7 (#21) ,8 (#24), 9 (#26), 10 (#28)
195     edges = geom->getFaceEdges(1, 1);
196     CPPUNIT_ASSERT_EQUAL(4, (int)edges.size());
197     int ids2[4] = { 7,8,9,10 };
198     for (int i = 0; i < 4; ++i)
199         CPPUNIT_ASSERT_EQUAL(ids2[i], edges[i]);
200
201     delete geom;
202 }
203
204 void BrepGeometryTest::testSolidFaces()
205 {
206     BrepGeometry* geom = new BrepGeometry("box");
207     readBrep(geom, "Cut_2.brep");
208
209     CPPUNIT_ASSERT_EQUAL(5, geom->countSolidShells(0));
210
211     std::vector<int> faces = geom->getSolidFaces(0, 0);
212     CPPUNIT_ASSERT_EQUAL(6, (int)faces.size());
213     int ids[6] = { 0, 1, 2, 3, 4, 5 };
214     for (int i = 0; i < 6; ++i)
215         CPPUNIT_ASSERT_EQUAL(ids[i], faces[i]);
216
217     faces = geom->getSolidFaces(0, 1);
218     CPPUNIT_ASSERT_EQUAL(6, (int)faces.size());
219     int ids2[6] = { 6, 7, 8, 9, 10, 11 };
220     for (int i = 0; i < 6; ++i)
221         CPPUNIT_ASSERT_EQUAL(ids2[i], faces[i]);
222
223     delete geom;
224 }
225
226 void BrepGeometryTest::testGetVertex()
227 {
228     BrepGeometry* geom = new BrepGeometry("box");
229     readBrep(geom, "Box_2.brep");
230
231     double x, y, z;
232     geom->getVertexXYZ(15, x, y, z);
233     CPPUNIT_ASSERT_DOUBLES_EQUAL(60., x, 1e-6);
234     CPPUNIT_ASSERT_DOUBLES_EQUAL(80., y, 1e-6);
235     CPPUNIT_ASSERT_DOUBLES_EQUAL(60., z, 1e-6);
236
237     delete geom;
238 }
239
240 void BrepGeometryTest::testGetEdgeLength()
241 {
242     BrepGeometry* geom = new BrepGeometry("box");
243     readBrep(geom, "Box_2.brep");
244
245     // edges 0 (#5), 7 (#21)
246     CPPUNIT_ASSERT_DOUBLES_EQUAL(200., geom->getEdgeLength(0), 0);
247     CPPUNIT_ASSERT_DOUBLES_EQUAL(80., geom->getEdgeLength(7), 0);
248
249     delete geom;
250 }
251
252 void BrepGeometryTest::testGetFaceArea()
253 {
254     BrepGeometry* geom = new BrepGeometry("box");
255     readBrep(geom, "Box_2.brep");
256
257     // faces 0 (#3), 1 (#13)
258     CPPUNIT_ASSERT_DOUBLES_EQUAL(40000., geom->getFaceArea(0), 1e-9);
259     CPPUNIT_ASSERT_DOUBLES_EQUAL(33600., geom->getFaceArea(1), 1e-9);
260
261     delete geom;
262 }
263
264 void BrepGeometryTest::testGetSolidVolume()
265 {
266     BrepGeometry* geom = new BrepGeometry("box");
267     readBrep(geom, "Box_2.brep");
268
269     CPPUNIT_ASSERT_DOUBLES_EQUAL(7488000., geom->getSolidVolume(0), 1e-9);
270
271     delete geom;
272 }
273
274 void BrepGeometryTest::testParse()
275 {
276     BrepGeometry* geom = new BrepGeometry("box");
277     readBrep(geom, "Box_2.brep");
278     std::cout << std::endl;
279
280     for (int solidIndex = 0; solidIndex < geom->countSolids(); ++solidIndex)
281     {
282         std::cout << "Solid #" << solidIndex << " : " << geom->getSolidReference(solidIndex) << std::endl;
283         int nbShells = geom->countSolidShells(solidIndex);
284         for (int shellIndex = 0; shellIndex < nbShells; ++shellIndex)
285         {
286             std::cout << "  Shell #" << shellIndex << std::endl;
287             std::vector<int> faces = geom->getSolidFaces(solidIndex, shellIndex);
288             for (unsigned int indf = 0; indf < faces.size(); ++indf)
289             {
290                 int faceIndex = faces[indf];
291                 std::cout  << "    Face #" << geom->getFaceReference(faceIndex) << std::endl;
292
293                 int nbWires = geom->countFaceWires(faceIndex);
294                 for (int wireIndex = 0; wireIndex < nbWires; ++wireIndex)
295                 {
296                     std::cout << "      Wire #" << wireIndex << std::endl;
297                     std::vector<int> edges = geom->getFaceEdges(faceIndex, wireIndex);
298                     for (unsigned int inde = 0; inde < edges.size(); ++inde)
299                     {
300                         int edgeIndex = edges[inde];
301                         std::cout << "        Edge #" << geom->getEdgeReference(edgeIndex) << " : ";
302
303                         int va = 0, vb = 0;
304                         geom->getEdgeVertices(edgeIndex, va, vb);
305                         int vaRef = geom->getVertexID(va);
306                         int vbRef = geom->getVertexID(vb);
307                         double ax, ay, az, bx, by, bz;
308                         geom->getVertexXYZ(va, ax, ay, az);
309                         geom->getVertexXYZ(vb, bx, by, bz);
310
311                         std::cout << vaRef << " (" << ax << ", " << ay << ", " << az << ")";
312                         std::cout << " - ";
313                         std::cout << vbRef << " (" << bx << ", " << by << ", " << bz << ")";
314                         std::cout << std::endl;
315                     }
316                 }
317
318
319             }
320         }
321     }
322
323     delete geom;
324 }