Salome HOME
Update copyrights
[modules/shaper.git] / src / XAO / tests / BrepGeometryTest.cxx
1 // Copyright (C) 2013-2019  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     std::string filePath = "Shapes/Brep/" + fileName;
45     char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath(filePath));
46     geom->setShapeString(txt);
47 }
48
49 void BrepGeometryTest::testGetIDs()
50 {
51     BrepGeometry* geom = new BrepGeometry("box");
52     readBrep(geom, "box1.brep");
53
54     CPPUNIT_ASSERT_EQUAL(8, geom->countElements(XAO::VERTEX));
55     CPPUNIT_ASSERT_EQUAL(8, geom->countVertices());
56     int vertices[8] = { 6,7,9,11,16,17,19,21 };
57     for (int i = 0; i < 8; ++i)
58         CPPUNIT_ASSERT_EQUAL(vertices[i], geom->getVertexID(i));
59
60     CPPUNIT_ASSERT_EQUAL(12, geom->countElements(XAO::EDGE));
61     CPPUNIT_ASSERT_EQUAL(12, geom->countEdges());
62     int edges[12] = { 5,8,10,12,15,18,20,22,25,26,29,30 };
63     for (int i = 0; i < 12; ++i)
64         CPPUNIT_ASSERT_EQUAL(edges[i], geom->getEdgeID(i));
65
66     CPPUNIT_ASSERT_EQUAL(6, geom->countElements(XAO::FACE));
67     CPPUNIT_ASSERT_EQUAL(6, geom->countFaces());
68     int faces[6] = { 3,13,23,27,31,33 };
69     for (int i = 0; i < 6; ++i)
70         CPPUNIT_ASSERT_EQUAL(faces[i], geom->getFaceID(i));
71
72     CPPUNIT_ASSERT_EQUAL(1, geom->countElements(XAO::SOLID));
73     CPPUNIT_ASSERT_EQUAL(1, geom->countSolids());
74     CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
75
76     delete geom;
77 }
78
79 void BrepGeometryTest::testGetReferences()
80 {
81     BrepGeometry* geom = new BrepGeometry("box");
82     readBrep(geom, "box1.brep");
83
84     // vertex of index 1 has id = 7
85     CPPUNIT_ASSERT_EQUAL(std::string("7"), geom->getElementReference(XAO::VERTEX, 1));
86     CPPUNIT_ASSERT_EQUAL(std::string("7"), geom->getVertexReference(1));
87     CPPUNIT_ASSERT_EQUAL(7, geom->getVertexID(1));
88     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::VERTEX, "7"));
89     CPPUNIT_ASSERT_EQUAL(1, geom->findVertex(7));
90
91     // edge of index 1 has id = 8
92     CPPUNIT_ASSERT_EQUAL(std::string("8"), geom->getElementReference(XAO::EDGE, 1));
93     CPPUNIT_ASSERT_EQUAL(std::string("8"), geom->getEdgeReference(1));
94     CPPUNIT_ASSERT_EQUAL(8, geom->getEdgeID(1));
95     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::EDGE, "8"));
96     CPPUNIT_ASSERT_EQUAL(1, geom->findEdge(8));
97
98     // face of index 1 has id = 13
99     CPPUNIT_ASSERT_EQUAL(std::string("13"), geom->getElementReference(XAO::FACE, 1));
100     CPPUNIT_ASSERT_EQUAL(std::string("13"), geom->getFaceReference(1));
101     CPPUNIT_ASSERT_EQUAL(13, geom->getFaceID(1));
102     CPPUNIT_ASSERT_EQUAL(1, geom->getElementIndexByReference(XAO::FACE, "13"));
103     CPPUNIT_ASSERT_EQUAL(1, geom->findFace(13));
104
105     // solid of index 0 has id = 1
106     CPPUNIT_ASSERT_EQUAL(std::string("1"), geom->getElementReference(XAO::SOLID, 0));
107     CPPUNIT_ASSERT_EQUAL(std::string("1"), geom->getSolidReference(0));
108     CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
109     CPPUNIT_ASSERT_EQUAL(0, geom->getElementIndexByReference(XAO::SOLID, "1"));
110     CPPUNIT_ASSERT_EQUAL(0, geom->findSolid(1));
111
112     delete geom;
113 }
114
115 void BrepGeometryTest::testGetNames()
116 {
117     BrepGeometry* geom = new BrepGeometry("box");
118     readBrep(geom, "box1.brep");
119
120     int id;
121
122     // vertex of index 1 has id = 7
123     id = 7;
124     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findVertexName(id));
125     geom->changeVertexName(id, std::string("va"));
126     CPPUNIT_ASSERT_EQUAL(std::string("va"), geom->findVertexName(id));
127     CPPUNIT_ASSERT_THROW(geom->changeVertexName(100, "a"), XAO_Exception);
128     CPPUNIT_ASSERT_THROW(geom->findVertexName(100), XAO_Exception);
129
130     // edge of index 1 has id = 8
131     id = 8;
132     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findEdgeName(id));
133     geom->changeEdgeName(id, std::string("ea"));
134     CPPUNIT_ASSERT_EQUAL(std::string("ea"), geom->findEdgeName(id));
135     CPPUNIT_ASSERT_THROW(geom->changeEdgeName(100, "a"), XAO_Exception);
136     CPPUNIT_ASSERT_THROW(geom->findEdgeName(100), XAO_Exception);
137
138     // face of index 1 has id = 13
139     id = 13;
140     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findFaceName(id));
141     geom->changeFaceName(id, std::string("fa"));
142     CPPUNIT_ASSERT_EQUAL(std::string("fa"), geom->findFaceName(id));
143     CPPUNIT_ASSERT_THROW(geom->changeFaceName(100, "a"), XAO_Exception);
144     CPPUNIT_ASSERT_THROW(geom->findFaceName(100), XAO_Exception);
145
146     // solid of index 0 has id = 1
147     id = 1;
148     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->findSolidName(id));
149     geom->changeSolidName(id, std::string("sa"));
150     CPPUNIT_ASSERT_EQUAL(std::string("sa"), geom->findSolidName(id));
151     CPPUNIT_ASSERT_THROW(geom->changeSolidName(100, "a"), XAO_Exception);
152     CPPUNIT_ASSERT_THROW(geom->findSolidName(100), XAO_Exception);
153
154     delete geom;
155 }
156
157 void BrepGeometryTest::testGetEdgeVertices()
158 {
159     BrepGeometry* geom = new BrepGeometry("box");
160     readBrep(geom, "box2.brep");
161
162     // edge of index 23, id = #63
163     // vertex are 47 (#12), 59 (#15)
164     int v1, v2;
165     geom->getEdgeVertices(23, v1, v2);
166     CPPUNIT_ASSERT_EQUAL(12, v1);
167     CPPUNIT_ASSERT_EQUAL(15, v2);
168
169     delete geom;
170 }
171
172 void printVector(std::vector<int>& v)
173 {
174     std::cout << "# ";
175     for (unsigned int i = 0; i < v.size(); i++)
176         std::cout << v[i] << ", ";
177     std::cout << std::endl;
178 }
179
180 void BrepGeometryTest::testGetFaceEdges()
181 {
182     BrepGeometry* geom = new BrepGeometry("box");
183     readBrep(geom, "box2.brep");
184
185     CPPUNIT_ASSERT_EQUAL(2, geom->countFaceWires(1)); // face 13
186     CPPUNIT_ASSERT_EQUAL(1, geom->countFaceWires(2)); // face 29
187
188     // wire 0 of face 1 (#13) => edge 4 (#15), 5 (#17), 0 (#5), 6 (#19)
189     std::vector<int> edges = geom->getFaceEdges(1, 0);
190     CPPUNIT_ASSERT_EQUAL(4, (int)edges.size());
191     int ids1[4] = { 4,5,0,6 };
192     for (int i = 0; i < 4; ++i)
193         CPPUNIT_ASSERT_EQUAL(ids1[i], edges[i]);
194
195     // wire 1 of face 13 => edge 7 (#21) ,8 (#24), 9 (#26), 10 (#28)
196     edges = geom->getFaceEdges(1, 1);
197     CPPUNIT_ASSERT_EQUAL(4, (int)edges.size());
198     int ids2[4] = { 7,8,9,10 };
199     for (int i = 0; i < 4; ++i)
200         CPPUNIT_ASSERT_EQUAL(ids2[i], edges[i]);
201
202     delete geom;
203 }
204
205 void BrepGeometryTest::testSolidFaces()
206 {
207     BrepGeometry* geom = new BrepGeometry("box");
208     readBrep(geom, "cut.brep");
209
210     CPPUNIT_ASSERT_EQUAL(5, geom->countSolidShells(0));
211
212     std::vector<int> faces = geom->getSolidFaces(0, 0);
213     CPPUNIT_ASSERT_EQUAL(6, (int)faces.size());
214     int ids[6] = { 0, 1, 2, 3, 4, 5 };
215     for (int i = 0; i < 6; ++i)
216         CPPUNIT_ASSERT_EQUAL(ids[i], faces[i]);
217
218     faces = geom->getSolidFaces(0, 1);
219     CPPUNIT_ASSERT_EQUAL(6, (int)faces.size());
220     int ids2[6] = { 6, 7, 8, 9, 10, 11 };
221     for (int i = 0; i < 6; ++i)
222         CPPUNIT_ASSERT_EQUAL(ids2[i], faces[i]);
223
224     delete geom;
225 }
226
227 void BrepGeometryTest::testGetVertex()
228 {
229     BrepGeometry* geom = new BrepGeometry("box");
230     readBrep(geom, "box2.brep");
231
232     double x, y, z;
233     geom->getVertexXYZ(15, x, y, z);
234     CPPUNIT_ASSERT_DOUBLES_EQUAL(60., x, 1e-6);
235     CPPUNIT_ASSERT_DOUBLES_EQUAL(80., y, 1e-6);
236     CPPUNIT_ASSERT_DOUBLES_EQUAL(60., z, 1e-6);
237
238     delete geom;
239 }
240
241 void BrepGeometryTest::testGetEdgeLength()
242 {
243     BrepGeometry* geom = new BrepGeometry("box");
244     readBrep(geom, "box2.brep");
245
246     // edges 0 (#5), 7 (#21)
247     CPPUNIT_ASSERT_DOUBLES_EQUAL(200., geom->getEdgeLength(0), 0);
248     CPPUNIT_ASSERT_DOUBLES_EQUAL(80., geom->getEdgeLength(7), 0);
249
250     delete geom;
251 }
252
253 void BrepGeometryTest::testGetFaceArea()
254 {
255     BrepGeometry* geom = new BrepGeometry("box");
256     readBrep(geom, "box2.brep");
257
258     // faces 0 (#3), 1 (#13)
259     CPPUNIT_ASSERT_DOUBLES_EQUAL(40000., geom->getFaceArea(0), 1e-9);
260     CPPUNIT_ASSERT_DOUBLES_EQUAL(33600., geom->getFaceArea(1), 1e-9);
261
262     delete geom;
263 }
264
265 void BrepGeometryTest::testGetSolidVolume()
266 {
267     BrepGeometry* geom = new BrepGeometry("box");
268     readBrep(geom, "box2.brep");
269
270     CPPUNIT_ASSERT_DOUBLES_EQUAL(7488000., geom->getSolidVolume(0), 1e-9);
271
272     delete geom;
273 }
274
275 void BrepGeometryTest::testParse()
276 {
277     BrepGeometry* geom = new BrepGeometry("box");
278     readBrep(geom, "box2.brep");
279     std::cout << std::endl;
280
281     for (int solidIndex = 0; solidIndex < geom->countSolids(); ++solidIndex)
282     {
283         std::cout << "Solid #" << solidIndex << " : " << geom->getSolidReference(solidIndex) << std::endl;
284         int nbShells = geom->countSolidShells(solidIndex);
285         for (int shellIndex = 0; shellIndex < nbShells; ++shellIndex)
286         {
287             std::cout << "  Shell #" << shellIndex << std::endl;
288             std::vector<int> faces = geom->getSolidFaces(solidIndex, shellIndex);
289             for (unsigned int indf = 0; indf < faces.size(); ++indf)
290             {
291                 int faceIndex = faces[indf];
292                 std::cout  << "    Face #" << geom->getFaceReference(faceIndex) << std::endl;
293
294                 int nbWires = geom->countFaceWires(faceIndex);
295                 for (int wireIndex = 0; wireIndex < nbWires; ++wireIndex)
296                 {
297                     std::cout << "      Wire #" << wireIndex << std::endl;
298                     std::vector<int> edges = geom->getFaceEdges(faceIndex, wireIndex);
299                     for (unsigned int inde = 0; inde < edges.size(); ++inde)
300                     {
301                         int edgeIndex = edges[inde];
302                         std::cout << "        Edge #" << geom->getEdgeReference(edgeIndex) << " : ";
303
304                         int va = 0, vb = 0;
305                         geom->getEdgeVertices(edgeIndex, va, vb);
306                         int vaRef = geom->getVertexID(va);
307                         int vbRef = geom->getVertexID(vb);
308                         double ax, ay, az, bx, by, bz;
309                         geom->getVertexXYZ(va, ax, ay, az);
310                         geom->getVertexXYZ(vb, bx, by, bz);
311
312                         std::cout << vaRef << " (" << ax << ", " << ay << ", " << az << ")";
313                         std::cout << " - ";
314                         std::cout << vbRef << " (" << bx << ", " << by << ", " << bz << ")";
315                         std::cout << std::endl;
316                     }
317                 }
318
319
320             }
321         }
322     }
323
324     delete geom;
325 }