Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2019 CNRS |
2 |
|
|
// Authors: Joseph Mirabel |
3 |
|
|
// |
4 |
|
|
// |
5 |
|
|
// This file is part of hpp-gui |
6 |
|
|
// hpp-gui is free software: you can redistribute it |
7 |
|
|
// and/or modify it under the terms of the GNU Lesser General Public |
8 |
|
|
// License as published by the Free Software Foundation, either version |
9 |
|
|
// 3 of the License, or (at your option) any later version. |
10 |
|
|
// |
11 |
|
|
// hpp-gui is distributed in the hope that it will be |
12 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
13 |
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
// General Lesser Public License for more details. You should have |
15 |
|
|
// received a copy of the GNU Lesser General Public License along with |
16 |
|
|
// hpp-gui If not, see |
17 |
|
|
// <http://www.gnu.org/licenses/>. |
18 |
|
|
|
19 |
|
|
#include <coal/mesh_loader/assimp.h> |
20 |
|
|
|
21 |
|
|
#include <node.hh> |
22 |
|
|
|
23 |
|
|
namespace hpp { |
24 |
|
|
namespace gui { |
25 |
|
✗ |
BVHDisplay::BVHDisplay(const std::string& filename, const std::string& name) |
26 |
|
✗ |
: Node(name), filename_(filename) { |
27 |
|
✗ |
setWireFrameMode(gepetto::viewer::WIREFRAME); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
✗ |
void BVHDisplay::setColor(const osgVector4&) { |
31 |
|
|
// TODO |
32 |
|
|
} |
33 |
|
|
|
34 |
|
✗ |
void BVHDisplay::setLevel(const int& level) { |
35 |
|
✗ |
if (level >= (int)levels_.size()) |
36 |
|
✗ |
throw std::invalid_argument("level out of range"); |
37 |
|
✗ |
this->asQueue()->replaceChild(levels_[level_].geode, levels_[level].geode); |
38 |
|
✗ |
level_ = level; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
✗ |
void BVHDisplay::init(coal::SplitMethodType splitMethod) { |
42 |
|
|
using namespace coal; |
43 |
|
|
|
44 |
|
✗ |
BVHPtr_t bvh(new BVH_t); |
45 |
|
✗ |
bvh->bv_splitter.reset(new BVSplitter<BoundingVolume>(splitMethod)); |
46 |
|
✗ |
loadPolyhedronFromResource(filename_, Vec3f(1, 1, 1), bvh); |
47 |
|
|
|
48 |
|
✗ |
recursiveBuildTree(*bvh, 0, 0); |
49 |
|
|
|
50 |
|
✗ |
level_ = 0; |
51 |
|
✗ |
this->asQueue()->addChild(levels_[0].geode); |
52 |
|
|
|
53 |
|
|
using gepetto::viewer::RangedIntProperty; |
54 |
|
|
RangedIntProperty::Ptr_t levelProp = RangedIntProperty::create( |
55 |
|
✗ |
"Level", this, &BVHDisplay::getLevel, &BVHDisplay::setLevel); |
56 |
|
✗ |
levelProp->min = 0; |
57 |
|
✗ |
levelProp->max = (int)(levels_.size() - 1); |
58 |
|
✗ |
addProperty(levelProp); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
✗ |
void BVHDisplay::recursiveBuildTree(const BVH_t& bvh, int ibv, |
62 |
|
|
std::size_t level) { |
63 |
|
✗ |
if (levels_.size() <= level) levels_.resize(level + 1); |
64 |
|
✗ |
BVLevel& bvlevel = levels_[level]; |
65 |
|
|
|
66 |
|
✗ |
const BoundingVolume& bv = bvh.getBV(ibv).bv; |
67 |
|
✗ |
::osg::BoxRefPtr box = new ::osg::Box(); |
68 |
|
✗ |
box->setCenter(osg::Vec3((float)bv.To[0], (float)bv.To[1], (float)bv.To[2])); |
69 |
|
✗ |
box->setHalfLengths( |
70 |
|
✗ |
osg::Vec3((float)bv.extent[0], (float)bv.extent[1], (float)bv.extent[2])); |
71 |
|
✗ |
Eigen::Quaterniond q(bv.axes); |
72 |
|
✗ |
box->setRotation(::osg::Quat(q.x(), q.y(), q.z(), q.w())); |
73 |
|
|
|
74 |
|
✗ |
::osg::ShapeDrawableRefPtr drawable = new ::osg::ShapeDrawable(box); |
75 |
|
|
|
76 |
|
✗ |
bvlevel.boxes.push_back(box); |
77 |
|
✗ |
if (!bvlevel.geode) bvlevel.geode = new ::osg::Geode; |
78 |
|
✗ |
bvlevel.geode->addDrawable(drawable); |
79 |
|
|
|
80 |
|
✗ |
if (bvh.getBV(ibv).isLeaf()) return; |
81 |
|
✗ |
recursiveBuildTree(bvh, bvh.getBV(ibv).leftChild(), level + 1); |
82 |
|
✗ |
recursiveBuildTree(bvh, bvh.getBV(ibv).rightChild(), level + 1); |
83 |
|
|
} |
84 |
|
|
} // namespace gui |
85 |
|
|
} // namespace hpp |
86 |
|
|
|