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