GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/nodes.cpp Lines: 39 47 83.0 %
Date: 2020-05-14 11:23:33 Branches: 147 348 42.2 %

Line Branch Exec Source
1
//
2
// Copyright (c) 2019 CNRS
3
// Authors: Joseph Mirabel
4
//
5
//
6
// This file is part of gepetto-viewer
7
// gepetto-viewer is free software: you can redistribute it
8
// and/or modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation, either version
10
// 3 of the License, or (at your option) any later version.
11
//
12
// gepetto-viewer is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
// General Lesser Public License for more details.  You should have
16
// received a copy of the GNU Lesser General Public License along with
17
// gepetto-viewer  If not, see
18
// <http://www.gnu.org/licenses/>.
19
20
#define BOOST_TEST_MODULE nodes
21
#ifndef Q_MOC_RUN
22
#include <boost/test/unit_test.hpp>
23
#include <boost/utility/binary.hpp>
24
#endif
25
26
#include <gepetto/viewer/node.h>
27
#include <gepetto/viewer/leaf-node-box.h>
28
29
#define CHECK_VECT_CLOSE(a, b, tol) \
30
  BOOST_CHECK_SMALL ((a-b).length2(), float(tol));
31
32
#define CHECK_QUAT_CLOSE(a, b, tol)                     \
33
{                                                       \
34
  osgQuat qres (a.inverse() * b);                       \
35
  osgVector3 axis; double angle;                        \
36
  qres.getRotate (angle, axis);                         \
37
  if (std::fabs(angle - 2*M_PI) < std::fabs(angle))     \
38
        BOOST_CHECK_CLOSE (angle, 2*M_PI, (double)tol); \
39
  else                                                  \
40
        BOOST_CHECK_SMALL (angle, (double)tol);         \
41
}
42
43
namespace osg {
44
std::ostream& operator<< (std::ostream& os, const Matrix& m)
45
{
46
  const char* s = ", ";
47
  const char* l = "\n";
48
  os
49
    << m(0,0) << s << m(0,1) << s << m(0,2) << s << m(0,3) << l
50
    << m(1,0) << s << m(1,1) << s << m(1,2) << s << m(1,3) << l
51
    << m(2,0) << s << m(2,1) << s << m(2,2) << s << m(2,3) << l
52
    << m(3,0) << s << m(3,1) << s << m(3,2) << s << m(3,3)
53
    ;
54
  return os;
55
}
56
}
57
58
namespace gepetto {
59
namespace viewer {
60
  struct NodeTest {
61
1
    static void checkAbstractClass (NodePtr_t node)
62
    {
63
1
      osgQuat q, so;
64
1
      osgVector3 t, s;
65
66
      // Check transforms
67
1
      osgVector3 _ts = node->getStaticPosition();
68
1
      osgQuat    _qs = node->getStaticRotation();
69
1
      osgVector3 ts (1.f,1.f,1.f);
70
1
      osgQuat    qs (1.f,0.f,0.f,0.f);
71
72
1
      node->setDirty (false);
73
1
      node->setStaticTransform (ts, qs);
74



1
      BOOST_CHECK (node->isDirty ());
75
76



1
      CHECK_VECT_CLOSE (node->getStaticPosition(), ts, 1e-4);
77








1
      CHECK_QUAT_CLOSE (node->getStaticRotation(), qs, 1e-4);
78
79
1
      node->setDirty (false);
80
1
      node->applyConfiguration (osgVector3(0.f,0.f,0.f), osgQuat (0.f,0.f,0.f,1.f));
81
      // Not dirty since configuration did not change.
82



1
      BOOST_CHECK (!node->isDirty ());
83
1
      node->applyConfiguration (osgVector3(0.f,0.f,1.f), osgQuat (0.f,0.f,0.f,1.f));
84



1
      BOOST_CHECK (node->isDirty ());
85
1
      node->applyConfiguration (osgVector3(0.f,0.f,0.f), osgQuat (0.f,0.f,0.f,1.f));
86
87

1
      const osg::MatrixTransform* matrixTransform = dynamic_cast<osg::MatrixTransform*>(node->asQueue ().get());
88



1
      BOOST_REQUIRE (matrixTransform != NULL);
89
90
1
      matrixTransform->getMatrix().decompose(t, q, s, so);
91
92



1
      CHECK_VECT_CLOSE (t, ts, 1e-4);
93







1
      CHECK_QUAT_CLOSE (q, qs, 1e-4);
94
95
1
      osgVector3 t1 (0.f,0.f,1.f);
96
1
      osgQuat q1 (0.5f,0.5f,0.5f,0.5f);
97
1
      node->applyConfiguration (t1, q1);
98



1
      CHECK_VECT_CLOSE (node->getGlobalTransform().position, t1, 1e-4);
99








1
      CHECK_QUAT_CLOSE (node->getGlobalTransform().quat    , q1, 1e-4);
100
101
1
      matrixTransform->getMatrix().decompose(t, q, s, so);
102
      // This is very counter intuitive...
103



1
      CHECK_VECT_CLOSE (t, (t1 + q1*ts), 1e-4);
104







1
      CHECK_QUAT_CLOSE (q, (  qs*q1   ), 1e-4);
105
106
1
      node->setStaticTransform (_ts, _qs);
107
1
    }
108
  };
109
} /* namespace viewer */
110
}
111
112
using namespace gepetto::viewer;
113
114

1
BOOST_AUTO_TEST_SUITE( node )
115
116


















3
BOOST_AUTO_TEST_CASE (box) {
117

2
  LeafNodeBoxPtr_t box = LeafNodeBox::create("box", osgVector3(0.1f,0.2f,0.3f));
118
1
  NodeTest::checkAbstractClass (box);
119
1
}
120
121

3
BOOST_AUTO_TEST_SUITE_END()