| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "geometrynode.h" |
| 7 |
|
| 8 |
#include "drawable.h" |
| 9 |
#include "visitor.h" |
| 10 |
|
| 11 |
#include <iostream> |
| 12 |
|
| 13 |
namespace Avogadro::Rendering { |
| 14 |
|
| 15 |
using Core::Array; |
| 16 |
|
| 17 |
GeometryNode::GeometryNode() {} |
| 18 |
|
| 19 |
GeometryNode::~GeometryNode() |
| 20 |
{ |
| 21 |
clearDrawables(); |
| 22 |
} |
| 23 |
|
| 24 |
void GeometryNode::accept(Visitor& visitor) |
| 25 |
{ |
| 26 |
visitor.visit(*this); |
| 27 |
for (auto& m_drawable : m_drawables) { |
| 28 |
m_drawable->accept(visitor); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
void GeometryNode::addDrawable(Drawable* object) |
| 33 |
{ |
| 34 |
for (auto& m_drawable : m_drawables) { |
| 35 |
if (m_drawable == object) |
| 36 |
return; |
| 37 |
} |
| 38 |
object->setParent(this); |
| 39 |
m_drawables.push_back(object); |
| 40 |
} |
| 41 |
|
| 42 |
bool GeometryNode::removeDrawable(Drawable* object) |
| 43 |
{ |
| 44 |
if (!object) |
| 45 |
return false; |
| 46 |
for (auto it = m_drawables.begin(); it != m_drawables.end(); ++it) { |
| 47 |
if (*it == object) { |
| 48 |
(*it)->setParent(nullptr); |
| 49 |
m_drawables.erase(it); |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
Drawable* GeometryNode::drawable(size_t index) |
| 57 |
{ |
| 58 |
if (index >= m_drawables.size()) |
| 59 |
return nullptr; |
| 60 |
else |
| 61 |
return m_drawables[index]; |
| 62 |
} |
| 63 |
|
| 64 |
void GeometryNode::clearDrawables() |
| 65 |
{ |
| 66 |
// Like all good parents, we destroy our children before we go... |
| 67 |
for (auto& m_drawable : m_drawables) { |
| 68 |
delete m_drawable; |
| 69 |
} |
| 70 |
m_drawables.clear(); |
| 71 |
} |
| 72 |
|
| 73 |
void GeometryNode::render(const Camera& camera) |
| 74 |
{ |
| 75 |
for (auto& m_drawable : m_drawables) { |
| 76 |
if (m_drawable->isVisible()) |
| 77 |
m_drawable->render(camera); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
std::multimap<float, Identifier> GeometryNode::hits( |
| 82 |
const Vector3f& rayOrigin, const Vector3f& rayEnd, |
| 83 |
const Vector3f& rayDirection) const |
| 84 |
{ |
| 85 |
std::multimap<float, Identifier> result; |
| 86 |
for (auto m_drawable : m_drawables) { |
| 87 |
std::multimap<float, Identifier> drawableHits; |
| 88 |
if (m_drawable->isVisible()) |
| 89 |
drawableHits = m_drawable->hits(rayOrigin, rayEnd, rayDirection); |
| 90 |
result.insert(drawableHits.begin(), drawableHits.end()); |
| 91 |
} |
| 92 |
|
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
Array<Identifier> GeometryNode::areaHits(const Frustrum& f) const |
| 97 |
{ |
| 98 |
Array<Identifier> result; |
| 99 |
for (auto m_drawable : m_drawables) { |
| 100 |
Array<Identifier> drawableHits; |
| 101 |
if (m_drawable->isVisible()) |
| 102 |
drawableHits = m_drawable->areaHits(f); |
| 103 |
result.insert(result.end(), drawableHits.begin(), drawableHits.end()); |
| 104 |
} |
| 105 |
|
| 106 |
return result; |
| 107 |
} |
| 108 |
|
| 109 |
} // namespace Avogadro::Rendering |