BSP_test.cc 18.5 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
/*
 * BSP_test.cc
 *
 *  Created on: 31.07.2012
 *      Author: moebius
 */

#include <gtest/gtest.h>

#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <ACG/Geometry/bsp/TriangleBSPT.hh>
53
#include <ACG/Geometry/Algorithms.hh>
Jan Möbius's avatar
Jan Möbius committed
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


struct CustomTraits : public OpenMesh::DefaultTraits {
};

typedef OpenMesh::TriMesh_ArrayKernelT<CustomTraits> Mesh;

typedef OpenMeshTriangleBSPT< Mesh > BSP;

class BSP_CUBE_BASE : public testing::Test {

    protected:

        // This function is called before each test is run
        virtual void SetUp() {

          mesh_.clear();

            // Add some vertices
            Mesh::VertexHandle vhandle[8];
            vhandle[0] = mesh_.add_vertex(Mesh::Point(-1, -1,  1));
            vhandle[1] = mesh_.add_vertex(Mesh::Point( 1, -1,  1));
            vhandle[2] = mesh_.add_vertex(Mesh::Point( 1,  1,  1));
            vhandle[3] = mesh_.add_vertex(Mesh::Point(-1,  1,  1));
            vhandle[4] = mesh_.add_vertex(Mesh::Point(-1, -1, -1));
            vhandle[5] = mesh_.add_vertex(Mesh::Point( 1, -1, -1));
            vhandle[6] = mesh_.add_vertex(Mesh::Point( 1,  1, -1));
            vhandle[7] = mesh_.add_vertex(Mesh::Point(-1,  1, -1));

            // Add six faces to form a cube
            std::vector<Mesh::VertexHandle> face_vhandles;

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[3]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[2]);
            face_vhandles.push_back(vhandle[3]);
            mesh_.add_face(face_vhandles);

            //=======================

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[7]);
            face_vhandles.push_back(vhandle[6]);
            face_vhandles.push_back(vhandle[5]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[7]);
            face_vhandles.push_back(vhandle[5]);
            face_vhandles.push_back(vhandle[4]);
            mesh_.add_face(face_vhandles);

            //=======================

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[4]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[4]);
            face_vhandles.push_back(vhandle[5]);
            mesh_.add_face(face_vhandles);

            //=======================

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[2]);
            face_vhandles.push_back(vhandle[1]);
            face_vhandles.push_back(vhandle[5]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[2]);
            face_vhandles.push_back(vhandle[5]);
            face_vhandles.push_back(vhandle[6]);
            mesh_.add_face(face_vhandles);


            //=======================

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[3]);
            face_vhandles.push_back(vhandle[2]);
            face_vhandles.push_back(vhandle[6]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[3]);
            face_vhandles.push_back(vhandle[6]);
            face_vhandles.push_back(vhandle[7]);
            mesh_.add_face(face_vhandles);

            //=======================

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[3]);
            face_vhandles.push_back(vhandle[7]);
            mesh_.add_face(face_vhandles);

            face_vhandles.clear();
            face_vhandles.push_back(vhandle[0]);
            face_vhandles.push_back(vhandle[7]);
            face_vhandles.push_back(vhandle[4]);
            mesh_.add_face(face_vhandles);


            // Test setup:
            //
            //
            //      3 ======== 2
            //     / \        /
            //    /   \      / |
            //   /     \    /  |
            //  /       \  /   |
            // 0 ======== 1    |
            // |       /  |    |
            // |      /   |    6    z
            // |     /    |   /     |   y
            // |    /     |  /      |  /
            // |   /      | /       | /
            // |  /       |/        |/
            // 4 ======== 5         -------> x
            //


            //      x ======== x
            //     / \        /
            //    /   \  1   / |
            //   /  0  \    /  |
            //  /       \  /   |
            // x ======== x    |
            // |       /  |    |
            // |  4   /   |    x    z
            // |     /    |   /     |   y
            // |    /  5  |  /      |  /
            // |   /      | /       | /
            // |  /       |/        |/
            // x ======== x         -------> x
            //

            // create Triangle BSP
            bsp_ = new BSP( mesh_ );

            // build Triangle BSP
            bsp_->reserve(mesh_.n_faces());

Jan Möbius's avatar
Jan Möbius committed
210
211
            Mesh::FIter f_it  = mesh_.faces_begin();
            Mesh::FIter f_end = mesh_.faces_end();
Jan Möbius's avatar
Jan Möbius committed
212
213

            for (; f_it!=f_end; ++f_it)
Jan Möbius's avatar
OM3    
Jan Möbius committed
214
              bsp_->push_back(*f_it);
Jan Möbius's avatar
Jan Möbius committed
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

            bsp_->build(10, 100); //max vertices per leaf 10, max depth 100

        }

        // This function is called after all tests are through
        virtual void TearDown() {

            // Do some final stuff with the member data here...
        }

    // This member will be accessible in all tests
    Mesh mesh_;

    //This will be the tree
    BSP* bsp_;
};


/* Basic check if the test mesh is setup correctly
 */
TEST_F(BSP_CUBE_BASE, CheckCubeConstruction) {

  // Check setup of the mesh
  EXPECT_EQ(8u, mesh_.n_vertices() ) << "Wrong number of vertices";
  EXPECT_EQ(12u, mesh_.n_faces() )   << "Wrong number of faces";

}

/* Basic check if the bsp can be setup
 */
TEST_F(BSP_CUBE_BASE, SetupBSPfromCube ) {

  EXPECT_FALSE(bsp_->empty()) << "BSP should not be empty !";
  EXPECT_EQ(12u, bsp_->size()) << "Wrong number of triangles in BSP after construction";

}

/* Nearest neighbor check
 */
TEST_F(BSP_CUBE_BASE, NearestNeighbour_OnSurface_1 ) {

  // (-1,1)      (1,1)
  //    x ======== x
  //    |       /  |             y = -1 plane
  //    |  4   /   |        z
  //    |     /    |        |
  //    |    /  5  |        |
  //    |   /      |        |
  //    |  /       |        |
  //    x ======== x         -------> x
  // (-1,-1)     (1,-1)


  // Point close to face 5
  // (-1,1)      (1,1)
  //    x ======== x
  //    |       /  |
  //    |      /   |
  //    |     /    |
  //    |    /  p  |
  //    |   /      |
  //    |  /       |
  //    x ======== x
  // (-1,-1)     (1,-1)
  Mesh::Point p1(0.75,-1.0,0.0);
  BSP::NearestNeighbor nn = bsp_->nearest(p1);

  EXPECT_EQ(5, nn.handle.idx()) << "Wrong Handle on closest face";
}

TEST_F(BSP_CUBE_BASE, NearestNeighbour_OnSurface_2 ) {
  // Point close to face 4
  // (-1,1)      (1,1)
  //    x ======== x
  //    |       /  |
  //    |      /   |
  //    |     /    |
  //    | p  /     |
  //    |   /      |
  //    |  /       |
  //    x ======== x
  // (-1,-1)     (1,-1)
  Mesh::Point p1(-0.75,-1.0,0.0);
  BSP::NearestNeighbor nn = bsp_->nearest(p1);
  EXPECT_EQ(4, nn.handle.idx()) << "Wrong Handle on closest face";
}

TEST_F(BSP_CUBE_BASE, NearestNeighbour_OnSurface_3 ) {
  // Point close to face 4
  // (-1,1)      (1,1)
  //    x ======== x
  //    | p     /  |
  //    |      /   |
  //    |     /    |
  //    |    /     |
  //    |   /      |
  //    |  /       |
  //    x ======== x
  // (-1,-1)     (1,-1)
  Mesh::Point p1(-0.75,-1.0,0.75);
  BSP::NearestNeighbor nn = bsp_->nearest(p1);
  EXPECT_EQ(4, nn.handle.idx()) << "Wrong Handle on closest face";
}

TEST_F(BSP_CUBE_BASE, NearestNeighbour_OnSurface_4 ) {

  // Point close to face 5
  // (-1,1)      (1,1)
  //    x ======== x
  //    |       /  |
  //    |      /   |
  //    |     /    |
  //    |    /     |
  //    |   /      |
  //    |  /     p |
  //    x ======== x
  // (-1,-1)     (1,-1)
  Mesh::Point p1(0.75,-1.0,-0.75);
  BSP::NearestNeighbor nn = bsp_->nearest(p1);
  EXPECT_EQ(5, nn.handle.idx()) << "Wrong Handle on closest face";

}

339
TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_NonDirectionalFunction_1 ) {
Jan Möbius's avatar
Jan Möbius committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363


  // ==============================================
  // Test some Rays
  // ==============================================

  // Shoot through face 4 in y direction
  // Start point is not on face 4
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point yDirection(0.0,1.0,0.0);
  Mesh::Point p1(-0.5,-2.0,0.0);
Jan Möbius's avatar
Jan Möbius committed
364
365
  BSP::RayCollision rc;
  rc = bsp_->raycollision(p1,yDirection);
Jan Möbius's avatar
Jan Möbius committed
366

Jan Möbius's avatar
Jan Möbius committed
367
368
369
370
  EXPECT_EQ(2u, rc.size() ) << "Wrong number of hit faces in ray collision test 1";
  if ( rc.size() == 2u ) { // Don't crash on wrong size
    EXPECT_EQ(4, rc[0].first.idx() )         << "Wrong handle of first face in ray collision test 1";
    EXPECT_EQ(9, rc[1].first.idx() )         << "Wrong handle of second face in ray collision test 1";
Jan Möbius's avatar
Jan Möbius committed
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
TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_NonDirectionalFunction_NegativeDirection_1 ) {
  // ==============================================
  // Test some Rays
  // ==============================================

  // Shoot through face 4 in negative y direction
  // Start point is not on face 4
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point nyDirection(0.0,-1.0,0.0);
  Mesh::Point p1(-0.5,-2.0,0.0);
398
399
  BSP::RayCollision rc;
  rc = bsp_->raycollision(p1,nyDirection);
400

Jan Möbius's avatar
Jan Möbius committed
401
402
403
404
  EXPECT_EQ(2u, rc.size() )       << "Wrong number of hit faces in ray collision test 1";
  if ( rc.size() == 2u ) { // Don't crash on wrong size
    EXPECT_EQ(4, rc[0].first.idx() )    << "Wrong handle of first face in ray collision test 1";
    EXPECT_EQ(9, rc[1].first.idx() )    << "Wrong handle of second face in ray collision test 1";
405
406
407
408
  }
}

TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_NonDirectionalFunction_2 ) {
Jan Möbius's avatar
Jan Möbius committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  // Shoot through face 5 in y direction
  // Start point is not on face 5
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point yDirection(0.0,1.0,0.0);
  Mesh::Point p1(0.5,-2.0,0.0);
427
428
  BSP::RayCollision rc;
  rc = bsp_->raycollision(p1,yDirection);
Jan Möbius's avatar
Jan Möbius committed
429

Jan Möbius's avatar
Jan Möbius committed
430
431
432
433
  EXPECT_EQ(2u, rc.size() )     << "Wrong number of hit faces in ray collision test 2";
  if ( rc.size() == 2u ) { // Don't crash on wrong size
    EXPECT_EQ(5, rc[0].first.idx() )  << "Wrong handle of first face in ray collision test 2";
    EXPECT_EQ(8, rc[1].first.idx() )  << "Wrong handle of second face in ray collision test 2";
Jan Möbius's avatar
Jan Möbius committed
434
435
436
437
  }

}

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_NonDirectionalFunction_NegativeDirection_2 ) {

  // Shoot through face 5 in negative y direction
  // Start point is not on face 5
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point nyDirection(0.0,-1.0,0.0);
  Mesh::Point p1(0.5,-2.0,0.0);
458
459
  BSP::RayCollision rc;
  rc = bsp_->raycollision(p1,nyDirection);
460

Jan Möbius's avatar
Jan Möbius committed
461
462
463
464
  EXPECT_EQ(2u, rc.size() )      << "Wrong number of hit faces in ray collision test 2";
  if ( rc.size() == 2u ) { // Don't crash on wrong size
    EXPECT_EQ(5, rc[0].first.idx() )   << "Wrong handle of first face in ray collision test 2";
    EXPECT_EQ(8, rc[1].first.idx() )   << "Wrong handle of second face in ray collision test 2";
465
466
467
468
469
470
471
  }

}



TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_DirectionalFunction_1 ) {
Jan Möbius's avatar
Jan Möbius committed
472
473


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
  // ==============================================
  // Test some Rays
  // ==============================================

  // Shoot through face 4 in y direction
  // Start point is not on face 4
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point yDirection(0.0,1.0,0.0);
495
  Mesh::Point origin(-0.5,-2.0,0.0);
496
497
  BSP::RayCollision rc;
  rc = bsp_->directionalRaycollision(origin,yDirection);
498

Jan Möbius's avatar
Jan Möbius committed
499
500
501
502
  EXPECT_EQ(2u, rc.size() )      << "Wrong number of hit faces in ray collision test 1";
  if ( rc.size() == 2u ) { // Don't crash on wrong size
    EXPECT_EQ(4, rc[0].first.idx() )   << "Wrong handle of first face in ray collision test 1";
    EXPECT_EQ(9, rc[1].first.idx() )   << "Wrong handle of second face in ray collision test 1";
503
504
505


    // Some intersection test to see, if we really have something usefull here:
Jan Möbius's avatar
Jan Möbius committed
506
    Mesh::FaceVertexIter fv_it =  Mesh::FaceVertexIter(mesh_, rc[0].first);
507
508

    float distance,u,v;
Jan Möbius's avatar
OM3    
Jan Möbius committed
509
510
511
    Mesh::Point p1 = mesh_.point(*fv_it);
    Mesh::Point p2 = mesh_.point(*(++fv_it));
    Mesh::Point p3 = mesh_.point(*(++fv_it));
512
513
514
515
516
517
518
519
520
521
522
523
524
525

    ACG::Geometry::triangleIntersection(origin,
                                        yDirection,
                                        p1,
                                        p2,
                                        p3,
                                        distance,
                                        u,
                                        v);

    EXPECT_EQ(1.0f, distance ) << "Wrong distance";
    EXPECT_EQ(0.25f, u )       << "Wrong u";
    EXPECT_EQ(0.5f , v )       << "Wrong v";

526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
  }

}

TEST_F(BSP_CUBE_BASE, RayIntersectionAboveSurface_DirectionalFunction_NegativeDirection_1 ) {
  // ==============================================
  // Test some Rays
  // ==============================================

  // Shoot through face 4 in negative y direction
  // Start point is not on face 4
  //      x ======== x
  //     / \        /
  //    /   \  1   / |
  //   /  0  \    /  |
  //  /       \  /   |
  // x ======== x    |
  // |       /  |    |
  // |  4   /   |    x    z
  // |     /    |   /     |   y
  // | p  /  5  |  /      |  /
  // |   /      | /       | /
  // |  /       |/        |/
  // x ======== x         -------> x

  Mesh::Point nyDirection(0.0,-1.0,0.0);
  Mesh::Point p1(-0.5,-2.0,0.0);
553
554
  BSP::RayCollision rc;
  rc = bsp_->directionalRaycollision(p1,nyDirection);
555

Jan Möbius's avatar
Jan Möbius committed
556
  EXPECT_EQ(0u, rc.size() ) << "Wrong number of hit faces in ray collision test 1";
557

558
}
Jan Möbius's avatar
Jan Möbius committed
559