Class Scene

Inheritance Relationships

Base Types

  • public mrpt::serialization::CSerializable

  • public std::enable_shared_from_this< Scene >

Class Documentation

class Scene : public mrpt::serialization::CSerializable, public std::enable_shared_from_this<Scene>

This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives. The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions, rendering primitives, etc.

It can contain from 1 up to any number of Viewports, each one associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.

An object of Scene always contains at least one viewport (mrpt::viz::Viewport), named “main”. Optionally, any number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of viewports so memory is not wasted replicating the same objects (see Viewport::setCloneView ).

The main rendering method, Scene::render(), assumes a viewport has been set-up for the entire target window. That method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate member in Viewport.

An object Scene can be saved to a “.3Dscene” file using CFileOutputStream or with the direct method Scene::saveToFile() for posterior visualization from the standalone application app_SceneViewer3D.

It can be also displayed in real-time using windows in mrpt::gui or serialized over a network socket, etc.

Public Types

using TListViewports = std::vector<Viewport::Ptr>

Public Functions

Scene()
~Scene() override
Scene &operator=(const Scene &obj)
Scene(const Scene &obj)
Scene &operator=(Scene &&obj) = default
Scene(Scene &&obj) = default
template<class T>
inline void insertCollection(const T &objs, const std::string &vpn = std::string("main"))

Inserts a set of objects into the scene, in the given viewport (“main” by default). Any iterable object will be accepted.

void insert(const CVisualObject::Ptr &newObject, const std::string &viewportName = std::string("main"))

Insert a new object into the scene, in the given viewport (by default, into the “main” viewport). The viewport must be created previously, an exception will be raised if the given name does not correspond to an existing viewport.

template<class T_it>
inline void insert(const T_it &begin, const T_it &end, const std::string &vpn = std::string("main"))

Inserts a set of objects into the scene, in the given viewport (“main” by default).

Viewport::Ptr createViewport(const std::string &viewportName)

Creates a new viewport, adding it to the scene and returning a pointer to the new object. Names (case-sensitive) cannot be duplicated: if the name provided coincides with an already existing viewport, a pointer to the existing object will be returned. The first, default viewport, is named “main”.

Viewport::Ptr getViewport(const std::string &viewportName = std::string("main"))

Returns the viewport with the given name, or nullptr if it does not exist; note that the default viewport is named “main” and initially occupies the entire rendering area.

Viewport::ConstPtr getViewport(const std::string &viewportName = std::string("main")) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

inline const TListViewports &viewports() const
inline size_t viewportsCount() const
void clear(bool createMainViewport = true)

Clear the list of objects and viewports in the scene, deleting objects’ memory, and leaving just the default viewport with the default values.

inline void enableFollowCamera(bool enabled)

If disabled (default), the SceneViewer application will ignore the camera of the “main” viewport and keep the viewport selected by the user by hand; otherwise, the camera in the “main” viewport prevails.

See also

followCamera

inline bool followCamera() const

Return the value of “followCamera”

CVisualObject::Ptr getByName(const std::string &str, const std::string &viewportName = std::string("main"))

Returns the first object with a given name, or nullptr (an empty smart pointer) if not found.

template<typename T>
inline T::Ptr getByClass(size_t ith = 0) const

Returns the i’th object of a given class (or of a descendant class), or nullptr (an empty smart pointer) if not found. Example:

CSphere::Ptr obs = myscene.getByClass<CSphere>();
By default (ith=0), the first observation is returned.

void removeObject(const CVisualObject::Ptr &obj, const std::string &viewportName = std::string("main"))

Removes the given object from the scene (it also deletes the object to free its memory).

void initializeTextures()

Initializes all textures in the scene (See opengl::CTexturedPlane::initializeTextures)

void dumpListOfObjects(std::vector<std::string> &lst) const

Retrieves a list of all objects in text form.

Deprecated:

Prefer asYAML() (since MRPT 2.1.3)

mrpt::containers::yaml asYAML() const

Prints all viewports and objects in human-readable YAML form. Note that not all objects data is serialized, so this method is not suitable for deserialization (for that, use saveToFile(), loadFromFile() instead).

Note

(New in MRPT 2.1.3)

bool saveToFile(const std::string &fil) const

Saves the scene to a “.3Dscene” file, loadable by: app_SceneViewer3D

See also

loadFromFile

Returns:

false on any error.

bool loadFromFile(const std::string &fil)

Loads the scene from a “.3Dscene” file.

See also

saveToFile

Returns:

false on any error.

bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const

Traces a ray

mrpt::math::TBoundingBox getBoundingBox(const std::string &vpn = std::string("main")) const

Evaluates the bounding box of the scene in the given viewport (default: “main”).

template<typename FUNCTOR>
inline void visitAllObjects(FUNCTOR functor) const

Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function The passed function must accept only one argument of type “const

mrpt::viz::CVisualObject::Ptr &”

Protected Attributes

bool m_followCamera = false
TListViewports m_viewports

The list of viewports, indexed by name.

Protected Static Functions

template<typename FUNCTOR>
static inline void internal_visitAllObjects(FUNCTOR functor, const CVisualObject::Ptr &o)