GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/nodes.cpp Lines: 40 48 83.3 %
Date: 2024-04-14 11:13:22 Branches: 127 300 42.3 %

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/leaf-node-box.h>
27
#include <gepetto/viewer/node.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;                                    \
36
    double angle;                                       \
37
    qres.getRotate(angle, axis);                        \
38
    if (std::fabs(angle - 2 * M_PI) < std::fabs(angle)) \
39
      BOOST_CHECK_CLOSE(angle, 2 * M_PI, (double)tol);  \
40
    else                                                \
41
      BOOST_CHECK_SMALL(angle, (double)tol);            \
42
  }
43
44
namespace osg {
45
std::ostream& operator<<(std::ostream& os, const Matrix& m) {
46
  const char* s = ", ";
47
  const char* l = "\n";
48
  os << m(0, 0) << s << m(0, 1) << s << m(0, 2) << s << m(0, 3) << l << m(1, 0)
49
     << s << m(1, 1) << s << m(1, 2) << s << m(1, 3) << l << m(2, 0) << s
50
     << m(2, 1) << s << m(2, 2) << s << m(2, 3) << l << m(3, 0) << s << m(3, 1)
51
     << s << m(3, 2) << s << m(3, 3);
52
  return os;
53
}
54
}  // namespace osg
55
56
namespace gepetto {
57
namespace viewer {
58
struct NodeTest {
59
1
  static void checkAbstractClass(NodePtr_t node) {
60
1
    osgQuat q, so;
61
1
    osgVector3 t, s;
62
63
    // Check transforms
64
1
    osgVector3 _ts = node->getStaticPosition();
65
1
    osgQuat _qs = node->getStaticRotation();
66
1
    osgVector3 ts(1.f, 1.f, 1.f);
67
1
    osgQuat qs(1.f, 0.f, 0.f, 0.f);
68
69
1
    node->setDirty(false);
70
1
    node->setStaticTransform(ts, qs);
71



1
    BOOST_CHECK(node->isDirty());
72
73



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







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



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



1
    BOOST_CHECK(node->isDirty());
84
2
    node->applyConfiguration(osgVector3(0.f, 0.f, 0.f),
85
1
                             osgQuat(0.f, 0.f, 0.f, 1.f));
86
87
    const osg::MatrixTransform* matrixTransform =
88

1
        dynamic_cast<osg::MatrixTransform*>(node->asQueue().get());
89



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


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






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



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







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


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






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
















4
BOOST_AUTO_TEST_CASE(box) {
118
  LeafNodeBoxPtr_t box =
119

4
      LeafNodeBox::create("box", osgVector3(0.1f, 0.2f, 0.3f));
120
2
  NodeTest::checkAbstractClass(box);
121
2
}
122
123
BOOST_AUTO_TEST_SUITE_END()