1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2013 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #ifndef AVOGADRO_RENDERING_GEOMETRYNODE_H
18 #define AVOGADRO_RENDERING_GEOMETRYNODE_H
19 
20 #include "node.h"
21 
22 #include "primitive.h"
23 
24 #include <avogadro/core/array.h>
25 #include <avogadro/core/vector.h>
26 
27 #include <map>
28 #include <vector>
29 
30 namespace Avogadro {
31 namespace Rendering {
32 
33 class Camera;
34 class Drawable;
35 
36 /**
37  * @class GeometryNode geometrynode.h <avogadro/rendering/geometrynode.h>
38  * @brief The GeometryNode class is the common base of all geometry nodes.
39  * @author Marcus D. Hanwell
40  *
41  * The GeometryNode contains any Drawable objects, and is the only node type
42  * that results in anything being rendered to the screen.
43  */
44 
45 class AVOGADRORENDERING_EXPORT GeometryNode : public Node
46 {
47 public:
48   GeometryNode();
49   ~GeometryNode() override;
50 
51   /**
52    * Accept a visit from our friendly visitor.
53    */
54   void accept(Visitor&) override;
55 
56   /**
57    * @brief Add a drawable object to the geometry node.
58    * @param object Drawable object to be added.
59    */
60   void addDrawable(Drawable* object);
61 
62   /**
63    * @brief Remove child node, this node will no longer be deleted.
64    * @param node Node to be removed.
65    * @return True if the node was removed, false if it was not found.
66    */
67   bool removeDrawable(Drawable* node);
68 
69   /**
70    * @brief Get the child Node at the specified index.
71    * @param index The index of the child.
72    * @return A pointer to the child node, or nullptr if the index is out of
73    * range.
74    */
75   Drawable* drawable(size_t index);
76 
77   /**
78    * @brief Get a reference to the child nodes list.
79    */
drawables()80   std::vector<Drawable*>& drawables() { return m_drawables; }
drawables()81   const std::vector<Drawable*> drawables() const { return m_drawables; }
82 
83   /**
84    * @brief Remove all drawable objects.
85    */
86   void clearDrawables();
87 
88   /**
89    * @brief Render the drawables in the geometry node.
90    */
91   void render(const Camera& camera);
92 
93   /**
94    * Return the primitives that are hit by the ray.
95    * @param rayOrigin Origin of the ray.
96    * @param rayEnd End point of the ray.
97    * @param rayDirection Normalized direction of the ray.
98    * @return Sorted collection of primitives that were hit.
99    */
100   std::multimap<float, Identifier> hits(const Vector3f& rayOrigin,
101                                         const Vector3f& rayEnd,
102                                         const Vector3f& rayDirection) const;
103 
104   /**
105    * Return the primitives within the supplied frustrum.
106    */
107   Core::Array<Identifier> areaHits(const Frustrum& frustrum) const;
108 
109 protected:
110   std::vector<Drawable*> m_drawables;
111 };
112 
113 } // End namespace Rendering
114 } // End namespace Avogadro
115 
116 #endif // AVOGADRO_RENDERING_GEOMETRYNODE_H
117