• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

src/SceneObject.hh

00001 /*
00002  * Copyright Staffan Gimåker 2006-2010.
00003  * Copyright Anders Boberg 2006-2007.
00004  *
00005  * ---
00006  *
00007  * This file is part of peekabot.
00008  *
00009  * peekabot is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * peekabot is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #ifndef PEEKABOT_SCENE_OBJECT_HH_INCLUDED
00024 #define PEEKABOT_SCENE_OBJECT_HH_INCLUDED
00025 
00026 
00027 #include <utility>
00028 #include <string>
00029 #include <stdexcept>
00030 #include <set>
00031 #include <boost/signals2.hpp>
00032 #include <Eigen/Core>
00033 #include <Eigen/Geometry>
00034 #include <boost/bimap.hpp>
00035 #include <boost/cstdint.hpp>
00036 
00037 #include "Types.hh"
00038 #include "XMLHandler.hh"
00039 #include "Any.hh"
00040 #include "PropMap.hh"
00041 
00042 
00043 namespace peekabot
00044 {
00045     class SceneObject;
00046     class ObjectVisitor;
00047     class ScopedHandler;
00048     class Prop;
00049 
00079     class SceneObject
00080     {
00081     public:
00082         class ChildNotFound : public std::runtime_error
00083         {
00084         public:
00085             ChildNotFound(const std::string &name)
00086                 : std::runtime_error("Child '" + name + "' not found") {}
00087         };
00088 
00089 
00090 
00091         SceneObject(const std::string &default_name);
00092 
00095         SceneObject(const std::string &default_name, ScopedHandler *handler);
00096 
00097         virtual ~SceneObject();
00098 
00105         inline const Eigen::Transform3f &get_mtow() const throw()
00106         {
00107             return m_mtow;
00108         }
00109 
00117         inline const Eigen::Transform3f &get_parent_mtow() const throw()
00118         {
00119             return m_parent ? m_parent->get_mtow() : get_mtow();
00120         }
00121 
00122         void detach();
00123 
00124         void rearrange(SceneObject *new_parent, bool retain_mtow);
00125 
00129         bool is_descendant(SceneObject *object) const throw();
00130 
00136         SceneObject *get_parent() throw();
00137 
00141         const SceneObject *get_parent() const throw();
00142 
00143         typedef std::set<SceneObject *> Children;
00144         typedef Children::iterator ChildIterator;
00145         typedef Children::const_iterator ConstChildIterator;
00146 
00153         Children &get_children() throw();
00154 
00155         const Children &get_children() const throw();
00156 
00157         ChildIterator begin();
00158 
00159         ChildIterator end();
00160 
00161         ConstChildIterator begin() const;
00162 
00163         ConstChildIterator end() const;
00164 
00165         void clear();
00166 
00167         SceneObject *get_child(const std::string &name)
00168             throw(ChildNotFound);
00169 
00170         const SceneObject *get_child(const std::string &name)
00171             const throw(ChildNotFound);
00172 
00173         bool has_child(const std::string &name) const throw();
00174 
00183         bool has_descendent(const std::string &rel_path) const throw();
00184 
00194         SceneObject *get_descendent(const std::string &rel_path)
00195             throw(std::runtime_error);
00196 
00200         const SceneObject *get_descendent(const std::string &rel_path)
00201             const throw(std::runtime_error);
00202 
00205         std::size_t child_count() const throw();
00206 
00213         ObjectID get_object_id() const throw();
00214 
00227         bool attach(SceneObject *subtree,
00228                     NameConflictPolicy conflict_policy = FAIL_ON_CONFLICT)
00229             throw(std::runtime_error);
00230 
00240         boost::uint8_t get_layer() const throw();
00241 
00252         void set_layer(boost::uint8_t layer);
00253 
00265         const std::string &get_name() const throw();
00266 
00275         void set_name(const std::string &name);
00276 
00283         float get_opacity() const throw();
00284 
00290         void set_opacity(float opacity);
00291 
00300         void set_hidden(bool hidden);
00301 
00307         bool is_hidden() const throw();
00308 
00313         bool is_visible() const throw();
00314 
00320         float get_accumulated_opacity() const throw();
00321 
00322         virtual void accept(ObjectVisitor *visitor) throw() = 0;
00323 
00327         virtual ObjectType get_object_type() const = 0;
00328 
00332         inline const Eigen::Transform3f &get_transformation() const throw()
00333         {
00334             return m_mtop;
00335         }
00336 
00340         void set_transformation(
00341             const Eigen::Transform3f &m,
00342             CoordinateSystem system = PARENT_COORDINATES);
00343 
00351         void apply_transformation(const Eigen::Transform3f &transform) throw();
00352 
00356         void set_position(const Eigen::Vector3f &pos) throw();
00357 
00361         void set_world_position(const Eigen::Vector3f &pos) throw();
00362 
00371         void set_rpy(
00372             float roll, float pitch, float yaw,
00373             CoordinateSystem system);
00374 
00380         void rotate(const Eigen::Vector3f &axis, float angle);
00381 
00386         void rotate_x(float angle) throw();
00387 
00392         void rotate_y(float angle) throw();
00393 
00398         void rotate_z(float angle) throw();
00399 
00400         void set_orientation(
00401             const Eigen::Vector3f &v, CoordinateSystem system);
00402 
00409         void set_color(const RGBColor &color, bool recursive = false);
00410 
00413         RGBColor get_color() const throw();
00414 
00415         void set_selected(bool select);
00416 
00417         bool is_selected() const throw();
00418 
00419         bool has_selected_ancestor() const throw();
00420 
00423 
00424         typedef PropMap::iterator PropIterator;
00425         typedef PropMap::const_iterator ConstPropIterator;
00426 
00427         Prop *get_prop(PropKey key);
00428 
00429         const Prop *get_prop(PropKey key) const;
00430 
00431         Any get_prop_value(PropKey key) const;
00432 
00433         void set_prop_value(PropKey key, const Any &val);
00434 
00435         template<typename T> inline
00436         T get_prop_value(PropKey key) const
00437         {
00438             return any_cast<T>(get_prop_value(key));
00439         }
00440 
00441         template<typename T> inline
00442         void set_prop_value(PropKey key, const T &val)
00443         {
00444             return set_prop_value(key, Any(val));
00445         }
00446 
00447         std::size_t prop_adapter_count() const;
00448 
00449         PropIterator prop_adapters_begin();
00450 
00451         ConstPropIterator prop_adapters_begin() const;
00452 
00453         PropIterator prop_adapters_end();
00454 
00455         ConstPropIterator prop_adapters_end() const;
00456 
00457         std::size_t prop_count() const;
00458 
00459         PropIterator props_begin();
00460 
00461         ConstPropIterator props_begin() const;
00462 
00463         PropIterator props_end();
00464 
00465         ConstPropIterator props_end() const;
00466 
00467         void erase_prop(PropKey key);
00468 
00469         void erase_prop(PropIterator it);
00470 
00471         bool has_prop(PropKey key) const;
00472 
00473         PropKey get_prop_key(const std::string &name) const;
00474 
00475         PropKey add_prop(const std::string &name, Prop *prop);
00476 
00477         const std::string &get_prop_name(PropKey key) const;
00478 
00482 
00483         typedef boost::signals2::signal<void (
00484             PropKey, Prop *)> PropAddedSignal;
00485 
00486         typedef boost::signals2::signal<void (
00487             PropKey, Prop *)> PropErasedSignal;
00488 
00489         typedef boost::signals2::signal<void (
00490             SceneObject *child)> ChildAttachedSignal;
00491 
00492         typedef boost::signals2::signal<void ()> DetachedSignal;
00493 
00494         typedef boost::signals2::signal<void ()> TransformationSetSignal;
00495 
00496         typedef boost::signals2::signal<void ()> NameSetSignal;
00497 
00498         typedef boost::signals2::signal<void ()> LayerSetSignal;
00499 
00500         typedef boost::signals2::signal<void ()> OpacitySetSignal;
00501 
00502         typedef boost::signals2::signal<void ()> ColorSetSignal;
00503 
00504         typedef boost::signals2::signal<void ()> HiddenSetSignal;
00505 
00506         typedef boost::signals2::signal<void ()> AccumOpacityChangedSignal;
00507 
00508         typedef boost::signals2::signal<void ()> MtowChangedSignal;
00509 
00510         typedef boost::signals2::signal<void ()> VisibilityChangedSignal;
00511 
00512         typedef boost::signals2::signal<void ()> SelectedSetSignal;
00513 
00514         typedef boost::signals2::signal<void (
00515             )> HasSelectedAncestorChangedSignal;
00516 
00517         // ---
00518 
00519         inline PropAddedSignal &prop_added_signal() const
00520         {
00521             return m_prop_added_signal;
00522         }
00523 
00524         inline PropErasedSignal &prop_erased_signal() const
00525         {
00526             return m_prop_erased_signal;
00527         }
00528 
00529         inline ChildAttachedSignal &child_attached_signal() const
00530         {
00531             return m_child_attached_signal;
00532         }
00533 
00534         inline DetachedSignal &detached_signal() const
00535         {
00536             return m_detached_signal;
00537         }
00538 
00539         inline TransformationSetSignal &transformation_set_signal() const
00540         {
00541             return m_transformation_set_signal;
00542         }
00543 
00544         inline NameSetSignal &name_set_signal() const
00545         {
00546             return m_name_set_signal;
00547         }
00548 
00549         inline LayerSetSignal &layer_set_signal() const
00550         {
00551             return m_layer_set_signal;
00552         }
00553 
00554         inline OpacitySetSignal &opacity_set_signal() const
00555         {
00556             return m_opacity_set_signal;
00557         }
00558 
00559         inline ColorSetSignal &color_set_signal() const
00560         {
00561             return m_color_set_signal;
00562         }
00563 
00564         inline HiddenSetSignal &hidden_set_signal() const
00565         {
00566             return m_hidden_set_signal;
00567         }
00568 
00569         inline AccumOpacityChangedSignal &accum_opacity_changed_signal() const
00570         {
00571             return m_accum_opacity_changed_signal;
00572         }
00573 
00574         inline MtowChangedSignal &mtow_changed_signal() const
00575         {
00576             return m_mtow_changed_signal;
00577         }
00578 
00579         inline VisibilityChangedSignal &visibility_changed_signal() const
00580         {
00581             return m_visibility_changed_signal;
00582         }
00583 
00584         inline SelectedSetSignal &selected_set_signal() const
00585         {
00586             return m_selected_set_signal;
00587         }
00588 
00589         inline HasSelectedAncestorChangedSignal &
00590         has_selected_ancestor_changed_signal() const
00591         {
00592             return m_has_selected_ancestor_changed_signal;
00593         }
00594 
00596 
00597     private:
00602         void recalc_mtows();
00603 
00608         void recalc_visibility();
00609 
00619         void orthonormalize_transform() throw();
00620 
00621         SceneObject *_get_descendent(
00622             const std::vector<std::string>::const_iterator first,
00623             const std::vector<std::string>::const_iterator last) throw();
00624 
00625         const SceneObject *_get_descendent(
00626             const std::vector<std::string>::const_iterator first,
00627             const std::vector<std::string>::const_iterator last) const throw();
00628 
00629         static void create_prop_adapters(PropMap &adapters);
00630 
00631     protected:
00632         virtual PropMap &get_prop_adapters();
00633 
00634         const PropMap &get_prop_adapters() const;
00635 
00636         static void merge_prop_adapters(PropMap &to, const PropMap &from);
00637 
00640 
00643         void end_handler(
00644             const std::string &name,
00645             ScopedHandler *handler) throw(std::runtime_error);
00646 
00649         void name_start_handler(
00650             const std::string & name,
00651             XMLHandler::AttributeMap &attributes,
00652             ScopedHandler *handler) throw();
00653 
00656         void name_cdata_handler(
00657             const std::string &cdata,
00658             ScopedHandler *handler) throw(std::runtime_error);
00659 
00662         void opacity_start_handler(
00663             const std::string & name,
00664             XMLHandler::AttributeMap &attributes,
00665             ScopedHandler *handler) throw();
00666 
00669         void opacity_cdata_handler(
00670             bool absolute,
00671             const std::string &cdata,
00672             ScopedHandler *handler) throw();
00673 
00676         void hidden_start_handler(
00677             const std::string & name,
00678             XMLHandler::AttributeMap &attributes,
00679             ScopedHandler *handler) throw();
00680 
00683         void transform_start_handler(
00684             const std::string &name,
00685             XMLHandler::AttributeMap &attributes,
00686             ScopedHandler *handler) throw();
00687 
00690         void transform_end_handler(
00691             const std::string &name, ScopedHandler *handler) throw();
00692 
00695         void rotate_start_handler(
00696             const std::string &name,
00697             XMLHandler::AttributeMap &attributes,
00698             ScopedHandler *handler) throw();
00699 
00702         void rotate_cdata_handler(
00703             boost::uint32_t tag,
00704             CoordinateSystem system,
00705             const std::string &cdata,
00706             ScopedHandler *handler) throw();
00707 
00710         void rotate_end_handler(
00711             const std::string &name, ScopedHandler *handler) throw();
00712 
00715         void translate_start_handler(
00716             const std::string &name,
00717             XMLHandler::AttributeMap &attributes,
00718             ScopedHandler *handler) throw();
00719 
00722         void translate_cdata_handler(
00723             CoordinateSystem system,
00724             const std::string &cdata,
00725             ScopedHandler *handler) throw();
00726 
00729         void matrix_start_handler(
00730             const std::string &name,
00731             XMLHandler::AttributeMap &attributes,
00732             ScopedHandler *handler) throw();
00733 
00736         void matrix_cdata_handler(
00737             CoordinateSystem system,
00738             const std::string &cdata,
00739             ScopedHandler *handler) throw();
00740 
00743         void children_start_handler(
00744             const std::string &name,
00745             XMLHandler::AttributeMap &attributes,
00746             ScopedHandler *handler) throw();
00747 
00749 
00750         //
00751         // ----------------------- Members ---------------------------
00752         //
00753 
00754     private:
00755         PropMap m_props;
00756 
00757         typedef boost::bimap<PropKey, std::string> PropNames;
00758         static PropNames ms_prop_names;
00759 
00760         static PropKey ms_next_prop_key;
00761 
00762         static ObjectID ms_next_object_id;
00763 
00766         bool m_is_hidden;
00767 
00771         bool m_is_visible;
00772 
00775         SceneObject *m_parent;
00776 
00779         boost::uint8_t m_layer;
00780 
00783         float m_opacity;
00784 
00787         std::string m_name;
00788 
00791         ObjectID m_id;
00792 
00793         int m_transforms_since_normalization;
00794 
00798         RGBColor m_color;
00799 
00803         Children m_children;
00804 
00808         Eigen::Transform3f m_mtop;
00809 
00813         Eigen::Transform3f m_mtow;
00814 
00815         bool m_is_selected;
00816 
00817 
00818         mutable PropAddedSignal m_prop_added_signal;
00819         mutable PropErasedSignal m_prop_erased_signal;
00820         mutable ChildAttachedSignal m_child_attached_signal;
00821         mutable DetachedSignal m_detached_signal;
00822         mutable TransformationSetSignal m_transformation_set_signal;
00823         mutable NameSetSignal m_name_set_signal;
00824         mutable LayerSetSignal m_layer_set_signal;
00825         mutable OpacitySetSignal m_opacity_set_signal;
00826         mutable ColorSetSignal m_color_set_signal;
00827         mutable HiddenSetSignal m_hidden_set_signal;
00828         mutable AccumOpacityChangedSignal m_accum_opacity_changed_signal;
00829         mutable MtowChangedSignal m_mtow_changed_signal;
00830         mutable VisibilityChangedSignal m_visibility_changed_signal;
00831         mutable SelectedSetSignal m_selected_set_signal;
00832         mutable HasSelectedAncestorChangedSignal m_has_selected_ancestor_changed_signal;
00833     };
00834 }
00835 
00836 #endif // PEEKABOT_SCENE_OBJECT_HH_INCLUDED

Generated on Sun Jan 30 2011 for peekabot by  doxygen 1.7.1