-
Notifications
You must be signed in to change notification settings - Fork 299
/
BoxROI_test.cpp
418 lines (324 loc) · 16.8 KB
/
BoxROI_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <gtest/gtest.h>
using ::testing::Types;
#include <sofa/core/fwd.h>
#include <sofa/helper/BackTrace.h>
#include <sofa/core/objectmodel/ComponentState.h>
using sofa::core::objectmodel::ComponentState;
#include <sofa/component/engine/select/BoxROI.h>
using sofa::component::engine::select::BoxROI;
#include <sofa/simulation/graph/DAGSimulation.h>
using sofa::simulation::Simulation;
using sofa::simulation::graph::DAGSimulation;
#include <sofa/simulation/Node.h>
using sofa::simulation::Node;
using sofa::core::objectmodel::BaseObject;
using sofa::core::objectmodel::BaseData;
using sofa::core::objectmodel::New;
using sofa::defaulttype::Vec3Types;
using namespace sofa::type;
using namespace sofa::defaulttype;
#include <sofa/simulation/common/SceneLoaderXML.h>
using sofa::simulation::SceneLoaderXML;
#include <sofa/helper/logging/Message.h>
using sofa::helper::logging::MessageDispatcher;
#include <sofa/testing/TestMessageHandler.h>
#include <sofa/testing/BaseTest.h>
#include <sofa/simpleapi/SimpleApi.h>
template <typename TDataType>
struct BoxROITest : public sofa::testing::BaseTest
{
typedef BoxROI<TDataType> TheBoxROI;
Simulation* m_simu {nullptr};
Node::SPtr m_root;
Node::SPtr m_node;
typename TheBoxROI::SPtr m_boxroi;
void SetUp() override
{
sofa::simpleapi::importPlugin("Sofa.Component.StateContainer");
sofa::simpleapi::importPlugin("Sofa.Component.Topology.Container.Dynamic");
sofa::simpleapi::importPlugin("Sofa.Component.Engine.Select");
m_simu = sofa::simulation::getSimulation();
ASSERT_NE(m_simu, nullptr);
m_root = m_simu->createNewGraph("root");
m_node = m_root->createChild("node");
m_boxroi = New< TheBoxROI >();
m_node->addObject(m_boxroi);
}
void TearDown() override
{
if (m_root != nullptr){
sofa::simulation::node::unload(m_root);
}
}
/// It is important to freeze what are the available Data field
/// of a component and rise warning/errors when some one removed.
void attributesTests(){
/// List of the supported attributes the user expect to find
/// This list needs to be updated if you add an attribute.
std::vector<string> attrnames = {
"box", "orientedBox",
"position", "edges", "triangles", "tetrahedra", "hexahedra", "quad",
"computeEdges", "computeTriangles", "computeTetrahedra", "computeHexahedra", "computeQuad",
"indices", "edgeIndices", "triangleIndices", "tetrahedronIndices", "hexahedronIndices",
"quadIndices",
"pointsInROI", "edgesInROI", "trianglesInROI", "tetrahedraInROI", "hexahedraInROI", "quadInROI",
"nbIndices",
"drawBoxes", "drawPoints", "drawEdges", "drawTriangles", "drawTetrahedra", "drawHexahedra", "drawQuads",
"drawSize",
"doUpdate"
};
for(auto& attrname : attrnames)
EXPECT_NE( m_boxroi->findData(attrname), nullptr ) << "Missing attribute with name '" << attrname << "'.";
return;
}
void checkGracefullHandlingOfInvalidUsage(){
const string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 0 0' time='0' animate='0'> "
" <Node name='Level 1'> "
" <BoxROI name='myBoxROI'/> "
" </Node> "
"</Node> ";
EXPECT_MSG_EMIT(Error); // Unable to find a MechanicalObject for this component.
const Node::SPtr root = SceneLoaderXML::loadFromMemory("testscene", scene.c_str());
EXPECT_NE(root.get(), nullptr);
root->init(sofa::core::execparams::defaultInstance());
BaseObject* boxroi = root->getTreeNode("Level 1")->getObject("myBoxROI");
EXPECT_NE(boxroi, nullptr);
EXPECT_EQ(boxroi->getComponentState(), ComponentState::Invalid ) << "The component cannot be initialized because it is missing a MechanicalObject. "
"But it shouldn't crash or segfault. ";
boxroi->reinit();
EXPECT_EQ(boxroi->getComponentState(), ComponentState::Invalid ) << "Reinit shouln't crash or change the state because there is still no MechanicalObject. ";
}
void checkAutomaticSearchingOfMechanicalObject(){
const string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 0 0' time='0' animate='0'> "
" <Node name='Level 1'> "
" <TriangleSetTopologyContainer name='Container'/> "
" <MechanicalObject name='meca' position='0 0 0 1 1 1'/> "
" <BoxROI name='myBoxROI'/> "
" </Node> "
"</Node> ";
const Node::SPtr root = SceneLoaderXML::loadFromMemory("testscene", scene.c_str());
EXPECT_NE(root.get(), nullptr);
root->init(sofa::core::execparams::defaultInstance());
TheBoxROI* boxroi = root->getTreeObject<TheBoxROI>();
EXPECT_NE(boxroi, nullptr);
EXPECT_EQ(boxroi->getComponentState(), ComponentState::Valid ) << "The component should succeed in being initialized because there is a MechanicalObject in the current context. ";
}
void checkAutomaticSearchingOfMechanicalObjectParent(){
const string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 0 0' time='0' animate='0' > "
" <MechanicalObject name='meca' position='0 0 0 1 1 1'/> "
" <Node name='Level 1'> "
" <TriangleSetTopologyContainer name='Container' /> "
" <BoxROI name='myBoxROI'/> "
" </Node> "
"</Node> ";
const Node::SPtr root = SceneLoaderXML::loadFromMemory ("testscene", scene.c_str());
EXPECT_NE(root.get(), nullptr);
root->init(sofa::core::execparams::defaultInstance());
TheBoxROI* boxroi = root->getTreeObject<TheBoxROI>();
EXPECT_NE(boxroi, nullptr);
EXPECT_EQ(boxroi->getComponentState(), ComponentState::Valid ) << "The component should succeed in being initialized because there is a MechanicalObject in the current context. ";
}
void checkAutomaticSearchingOfMeshLoader(){
const string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 0 0' time='0' animate='0' > "
" <Node name='Level 1'> "
" <RequiredPlugin name='Sofa.Component.IO.Mesh' /> "
" <TriangleSetTopologyContainer name='Container' /> "
" <MeshOBJLoader filename='mesh/single_triangle.obj'/> "
" <BoxROI name='myBoxROI'/> "
" </Node> "
"</Node> ";
const Node::SPtr root = SceneLoaderXML::loadFromMemory("testscene", scene.c_str());
EXPECT_NE(root.get(), nullptr);
root->init(sofa::core::execparams::defaultInstance());
BaseObject* boxroi = root->getTreeNode("Level 1")->getObject("myBoxROI");
EXPECT_NE(boxroi, nullptr);
EXPECT_EQ(boxroi->getComponentState(), ComponentState::Valid ) << "The component should succeed in being initialized because there is a MeshLoader and a TopologyContainer in the current context. ";
}
/// Test isPointInBox computation with a simple example
void isPointInBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 1. 1. 1.");
m_boxroi->findData("position")->read("0. 0. 0. 1. 1. 1. 2. 2. 2.");
m_boxroi->update();
EXPECT_EQ(m_boxroi->findData("indices")->getValueString(),"0 1");
}
/// Test isEdgeInBox computation with a simple example
void isEdgeInBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 1. 1. 1.");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 0. 2. 0. 0.");
m_boxroi->findData("edges")->read("0 1 1 2");
m_boxroi->update();
EXPECT_EQ(m_boxroi->findData("edgeIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("edgesInROI")->getValueString(),"0 1");
}
/// Test isTriangleInBox computation with a simple example
void isTriangleInBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 1. 1. 1.");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 0. 1. 1. 0. 2. 0. 0.");
m_boxroi->findData("triangles")->read("0 1 2 1 3 2");
m_boxroi->update();
EXPECT_EQ(m_boxroi->findData("triangleIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("trianglesInROI")->getValueString(),"0 1 2");
}
/// Test isTetrahedraInBox computation with a simple example
void isTetrahedraInBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 1. 1. 1.");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 0. 1. 1. 0. 1. 0. 1. 2. 0. 0.");
m_boxroi->findData("tetrahedra")->read("0 1 2 3 1 2 4 3");
m_boxroi->update();
EXPECT_EQ(m_boxroi->findData("tetrahedronIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("tetrahedraInROI")->getValueString(),"0 1 2 3");
}
/// Test isPointInOrientedBox computation with a simple example
void isPointInOrientedBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 0. 0. 0.");
m_boxroi->findData("orientedBox")->read("2 0 0 0 0 0 2 2 2 2");
m_boxroi->findData("position")->read("1. 0. 0. 1. 0. 1. 0. 0. 1.");
m_boxroi->init();
EXPECT_EQ(m_boxroi->findData("indices")->getValueString(),"0 1");
}
/// Test isEdgeInOrientedBox computation with a simple example
void isEdgeInOrientedBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 0. 0. 0.");
m_boxroi->findData("orientedBox")->read("2 0 0 0 0 0 2 2 2 2");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 1. 0. 0. 1.");
m_boxroi->findData("edges")->read("0 1 0 2");
m_boxroi->init();
EXPECT_EQ(m_boxroi->findData("edgeIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("edgesInROI")->getValueString(),"0 1");
}
/// Test isTriangleInOrientedBox computation with a simple example
void isTriangleInOrientedBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 0. 0. 0.");
m_boxroi->findData("orientedBox")->read("2 0 0 0 0 0 2 2 2 2");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 0. 1. 1. 0. 0. 0. -1.");
m_boxroi->findData("triangles")->read("0 1 2 0 3 1");
m_boxroi->init();
EXPECT_EQ(m_boxroi->findData("triangleIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("trianglesInROI")->getValueString(),"0 1 2");
}
/// Test isTetrahedraInOrientedBox computation with a simple example
void isTetrahedraInOrientedBoxTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 0. 0. 0.");
m_boxroi->findData("orientedBox")->read("2 0 0 0 0 0 2 2 2 2");
m_boxroi->findData("position")->read("0. 0. 0. 1. 0. 0. 1. 1. 0. 1. 0. 1. 0. 0. -2.");
m_boxroi->findData("tetrahedra")->read("0 1 2 3 0 1 2 4");
m_boxroi->init();
EXPECT_EQ(m_boxroi->findData("tetrahedronIndices")->getValueString(),"0");
EXPECT_EQ(m_boxroi->findData("tetrahedraInROI")->getValueString(),"0 1 2 3");
}
/// Test isTetrahedraInOrientedBox computation with a simple example
void isPointInBoxesTest()
{
m_boxroi->findData("box")->read("0. 0. 0. 1. 1. 1. 0. 0. 0. -1 -1 -1");
m_boxroi->findData("orientedBox")->read("3 0 0 1 0 0 3 2 2 2 -3 0 0 -1 0 0 -3 -2 -2 2");
m_boxroi->findData("position")->read("1. 0. 0. -1. 0. 0. 2. 0. 0. -2. 0. 0. 1. -1. 0. -1. 1. 0.");
m_boxroi->init();
EXPECT_EQ(m_boxroi->findData("indices")->getValueString(),"0 1 2 3");
}
/// Test computeBBox computation with a simple example
void computeBBoxTest()
{
m_boxroi->findData("box")->read("-1. -1. -1. 0. 0. 0. 1. 1. 1. 2. 2. 2.");
m_boxroi->computeBBox(nullptr, false);
EXPECT_EQ(m_boxroi->f_bbox.getValue().minBBox(), Vec3(-1,-1,-1));
EXPECT_EQ(m_boxroi->f_bbox.getValue().maxBBox(), Vec3(2,2,2));
m_boxroi->findData("box")->read("-1. -1. -1. 0. 0. 0.");
m_boxroi->findData("orientedBox")->read("0 0 0 2 0 0 2 2 0 2");
m_boxroi->computeBBox(nullptr, false);
EXPECT_EQ(m_boxroi->f_bbox.getValue().minBBox(), Vec3(-1,-1,-1));
EXPECT_EQ(m_boxroi->f_bbox.getValue().maxBBox(), Vec3(2,2,1));
}
};
//TODO(dmarchal): There is a problem of segfault with the test & Rigid3 types.
//Please fix this either the tests or the BoxROI implementation
typedef Types<
Vec3Types
//,Rigid3dTypes
> DataTypes;
TYPED_TEST_SUITE(BoxROITest, DataTypes);
TYPED_TEST(BoxROITest, attributesTests) {
ASSERT_NO_THROW(this->attributesTests());
}
TYPED_TEST(BoxROITest, checkGracefullHandlingOfInvalidUsage) {
ASSERT_NO_THROW(this->checkGracefullHandlingOfInvalidUsage());
}
TYPED_TEST(BoxROITest, checkAutomaticSearchingOfMechanicalObject) {
ASSERT_NO_THROW(this->checkAutomaticSearchingOfMechanicalObject());
}
TYPED_TEST(BoxROITest, checkAutomaticSearchingOfMechanicalObjectParent) {
ASSERT_NO_THROW(this->checkAutomaticSearchingOfMechanicalObject());
}
TYPED_TEST(BoxROITest, checkAutomaticSearchingOfMeshLoader) {
ASSERT_NO_THROW(this->checkAutomaticSearchingOfMeshLoader());
}
TYPED_TEST(BoxROITest, isPointInBoxTest) {
ASSERT_NO_THROW(this->isPointInBoxTest());
}
TYPED_TEST(BoxROITest, isEdgeInBoxTest) {
ASSERT_NO_THROW(this->isEdgeInBoxTest());
}
TYPED_TEST(BoxROITest, isTriangleInBoxTest) {
ASSERT_NO_THROW(this->isTriangleInBoxTest());
}
TYPED_TEST(BoxROITest, isTetrahedraInBoxTest) {
ASSERT_NO_THROW(this->isTetrahedraInBoxTest());
}
TYPED_TEST(BoxROITest, isPointInOrientedBoxTest) {
ASSERT_NO_THROW(this->isPointInOrientedBoxTest());
}
TYPED_TEST(BoxROITest, isEdgeInOrientedBoxTest) {
ASSERT_NO_THROW(this->isEdgeInOrientedBoxTest());
}
TYPED_TEST(BoxROITest, isTriangleInOrientedBoxTest) {
ASSERT_NO_THROW(this->isTriangleInOrientedBoxTest());
}
TYPED_TEST(BoxROITest, isTetrahedraInOrientedBoxTest) {
ASSERT_NO_THROW(this->isTetrahedraInOrientedBoxTest());
}
TYPED_TEST(BoxROITest, isPointInBoxesTest) {
ASSERT_NO_THROW(this->isPointInBoxesTest());
}
TYPED_TEST(BoxROITest, computeBBoxTest) {
ASSERT_NO_THROW(this->computeBBoxTest());
}