1 // Copyright (C) 2013-2019 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "TestUtils.hxx"
23 #include "BrepGeometryTest.hxx"
24 #include "../XAO_XaoUtils.hxx"
25 #include "../XAO_Xao.hxx"
26 #include "../XAO_BrepGeometry.hxx"
30 void BrepGeometryTest::setUp()
34 void BrepGeometryTest::tearDown()
38 void BrepGeometryTest::cleanUp()
42 void readBrep(Geometry* geom, const std::string& fileName)
44 std::string filePath = "Shapes/Brep/" + fileName;
45 char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath(filePath));
46 geom->setShapeString(txt);
49 void BrepGeometryTest::testGetIDs()
51 BrepGeometry* geom = new BrepGeometry("box");
52 readBrep(geom, "box1.brep");
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));
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));
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));
72 CPPUNIT_ASSERT_EQUAL(1, geom->countElements(XAO::SOLID));
73 CPPUNIT_ASSERT_EQUAL(1, geom->countSolids());
74 CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
79 void BrepGeometryTest::testGetReferences()
81 BrepGeometry* geom = new BrepGeometry("box");
82 readBrep(geom, "box1.brep");
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));
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));
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));
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));
115 void BrepGeometryTest::testGetNames()
117 BrepGeometry* geom = new BrepGeometry("box");
118 readBrep(geom, "box1.brep");
122 // vertex of index 1 has 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);
130 // edge of index 1 has 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);
138 // face of index 1 has 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);
146 // solid of index 0 has 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);
157 void BrepGeometryTest::testGetEdgeVertices()
159 BrepGeometry* geom = new BrepGeometry("box");
160 readBrep(geom, "box2.brep");
162 // edge of index 23, id = #63
163 // vertex are 47 (#12), 59 (#15)
165 geom->getEdgeVertices(23, v1, v2);
166 CPPUNIT_ASSERT_EQUAL(12, v1);
167 CPPUNIT_ASSERT_EQUAL(15, v2);
172 void printVector(std::vector<int>& v)
175 for (unsigned int i = 0; i < v.size(); i++)
176 std::cout << v[i] << ", ";
177 std::cout << std::endl;
180 void BrepGeometryTest::testGetFaceEdges()
182 BrepGeometry* geom = new BrepGeometry("box");
183 readBrep(geom, "box2.brep");
185 CPPUNIT_ASSERT_EQUAL(2, geom->countFaceWires(1)); // face 13
186 CPPUNIT_ASSERT_EQUAL(1, geom->countFaceWires(2)); // face 29
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]);
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]);
205 void BrepGeometryTest::testSolidFaces()
207 BrepGeometry* geom = new BrepGeometry("box");
208 readBrep(geom, "cut.brep");
210 CPPUNIT_ASSERT_EQUAL(5, geom->countSolidShells(0));
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]);
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]);
227 void BrepGeometryTest::testGetVertex()
229 BrepGeometry* geom = new BrepGeometry("box");
230 readBrep(geom, "box2.brep");
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);
241 void BrepGeometryTest::testGetEdgeLength()
243 BrepGeometry* geom = new BrepGeometry("box");
244 readBrep(geom, "box2.brep");
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);
253 void BrepGeometryTest::testGetFaceArea()
255 BrepGeometry* geom = new BrepGeometry("box");
256 readBrep(geom, "box2.brep");
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);
265 void BrepGeometryTest::testGetSolidVolume()
267 BrepGeometry* geom = new BrepGeometry("box");
268 readBrep(geom, "box2.brep");
270 CPPUNIT_ASSERT_DOUBLES_EQUAL(7488000., geom->getSolidVolume(0), 1e-9);
275 void BrepGeometryTest::testParse()
277 BrepGeometry* geom = new BrepGeometry("box");
278 readBrep(geom, "box2.brep");
279 std::cout << std::endl;
281 for (int solidIndex = 0; solidIndex < geom->countSolids(); ++solidIndex)
283 std::cout << "Solid #" << solidIndex << " : " << geom->getSolidReference(solidIndex) << std::endl;
284 int nbShells = geom->countSolidShells(solidIndex);
285 for (int shellIndex = 0; shellIndex < nbShells; ++shellIndex)
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)
291 int faceIndex = faces[indf];
292 std::cout << " Face #" << geom->getFaceReference(faceIndex) << std::endl;
294 int nbWires = geom->countFaceWires(faceIndex);
295 for (int wireIndex = 0; wireIndex < nbWires; ++wireIndex)
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)
301 int edgeIndex = edges[inde];
302 std::cout << " Edge #" << geom->getEdgeReference(edgeIndex) << " : ";
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);
312 std::cout << vaRef << " (" << ax << ", " << ay << ", " << az << ")";
314 std::cout << vbRef << " (" << bx << ", " << by << ", " << bz << ")";
315 std::cout << std::endl;