1 // Copyright (C) 2013-2015 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 char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath(fileName));
45 geom->setShapeString(txt);
48 void BrepGeometryTest::testGetIDs()
50 BrepGeometry* geom = new BrepGeometry("box");
51 readBrep(geom, "Box_1.brep");
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));
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));
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));
71 CPPUNIT_ASSERT_EQUAL(1, geom->countElements(XAO::SOLID));
72 CPPUNIT_ASSERT_EQUAL(1, geom->countSolids());
73 CPPUNIT_ASSERT_EQUAL(1, geom->getSolidID(0));
78 void BrepGeometryTest::testGetReferences()
80 BrepGeometry* geom = new BrepGeometry("box");
81 readBrep(geom, "Box_1.brep");
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));
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));
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));
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));
114 void BrepGeometryTest::testGetNames()
116 BrepGeometry* geom = new BrepGeometry("box");
117 readBrep(geom, "Box_1.brep");
121 // vertex of index 1 has 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);
129 // edge of index 1 has 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);
137 // face of index 1 has 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);
145 // solid of index 0 has 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);
156 void BrepGeometryTest::testGetEdgeVertices()
158 BrepGeometry* geom = new BrepGeometry("box");
159 readBrep(geom, "Box_2.brep");
161 // edge of index 23, id = #63
162 // vertex are 47 (#12), 59 (#15)
164 geom->getEdgeVertices(23, v1, v2);
165 CPPUNIT_ASSERT_EQUAL(12, v1);
166 CPPUNIT_ASSERT_EQUAL(15, v2);
171 void printVector(std::vector<int>& v)
174 for (unsigned int i = 0; i < v.size(); i++)
175 std::cout << v[i] << ", ";
176 std::cout << std::endl;
179 void BrepGeometryTest::testGetFaceEdges()
181 BrepGeometry* geom = new BrepGeometry("box");
182 readBrep(geom, "Box_2.brep");
184 CPPUNIT_ASSERT_EQUAL(2, geom->countFaceWires(1)); // face 13
185 CPPUNIT_ASSERT_EQUAL(1, geom->countFaceWires(2)); // face 29
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]);
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]);
204 void BrepGeometryTest::testSolidFaces()
206 BrepGeometry* geom = new BrepGeometry("box");
207 readBrep(geom, "Cut_2.brep");
209 CPPUNIT_ASSERT_EQUAL(5, geom->countSolidShells(0));
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]);
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]);
226 void BrepGeometryTest::testGetVertex()
228 BrepGeometry* geom = new BrepGeometry("box");
229 readBrep(geom, "Box_2.brep");
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);
240 void BrepGeometryTest::testGetEdgeLength()
242 BrepGeometry* geom = new BrepGeometry("box");
243 readBrep(geom, "Box_2.brep");
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);
252 void BrepGeometryTest::testGetFaceArea()
254 BrepGeometry* geom = new BrepGeometry("box");
255 readBrep(geom, "Box_2.brep");
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);
264 void BrepGeometryTest::testGetSolidVolume()
266 BrepGeometry* geom = new BrepGeometry("box");
267 readBrep(geom, "Box_2.brep");
269 CPPUNIT_ASSERT_DOUBLES_EQUAL(7488000., geom->getSolidVolume(0), 1e-9);
274 void BrepGeometryTest::testParse()
276 BrepGeometry* geom = new BrepGeometry("box");
277 readBrep(geom, "Box_2.brep");
278 std::cout << std::endl;
280 for (int solidIndex = 0; solidIndex < geom->countSolids(); ++solidIndex)
282 std::cout << "Solid #" << solidIndex << " : " << geom->getSolidReference(solidIndex) << std::endl;
283 int nbShells = geom->countSolidShells(solidIndex);
284 for (int shellIndex = 0; shellIndex < nbShells; ++shellIndex)
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)
290 int faceIndex = faces[indf];
291 std::cout << " Face #" << geom->getFaceReference(faceIndex) << std::endl;
293 int nbWires = geom->countFaceWires(faceIndex);
294 for (int wireIndex = 0; wireIndex < nbWires; ++wireIndex)
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)
300 int edgeIndex = edges[inde];
301 std::cout << " Edge #" << geom->getEdgeReference(edgeIndex) << " : ";
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);
311 std::cout << vaRef << " (" << ax << ", " << ay << ", " << az << ")";
313 std::cout << vbRef << " (" << bx << ", " << by << ", " << bz << ")";
314 std::cout << std::endl;