home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / mesh_selection.h < prev    next >
C/C++ Source or Header  |  2009-01-23  |  6KB  |  198 lines

  1. #ifndef K3DSDK_MESH_SELECTION_H
  2. #define K3DSDK_MESH_SELECTION_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \author Timothy M. Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "mesh.h"
  28. #include "selection.h"
  29. #include "serialization_xml.h"
  30. #include "types.h"
  31. #include "xml.h"
  32.  
  33. #include <boost/static_assert.hpp>
  34. #include <boost/type_traits.hpp>
  35.  
  36. #include <map>
  37.  
  38. namespace k3d
  39. {
  40.  
  41. namespace legacy { class mesh; }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // mesh_selection
  45.  
  46. /// Stores a collection of selected mesh components
  47. class mesh_selection
  48. {
  49. public:
  50.     /** \name Deprecated for generic mesh primitives */
  51.     //@{
  52.     /// Stores selection data that will apply to a range of components
  53.     struct record
  54.     {
  55.         uint_t begin;
  56.         uint_t end;
  57.         double weight;
  58.  
  59.         record(const uint_t Begin, const double Weight) :
  60.             begin(Begin),
  61.             end(Begin + 1),
  62.             weight(Weight)
  63.         {
  64.         }
  65.         
  66.         record(const uint_t Begin, const uint_t End, const double Weight) :
  67.             begin(Begin),
  68.             end(End),
  69.             weight(Weight)
  70.         {
  71.         }
  72.  
  73.         bool operator<(const record& RHS) const
  74.         {
  75.             return begin < RHS.begin;
  76.         }
  77.  
  78.         bool operator==(const record& RHS) const
  79.         {
  80.             return begin == RHS.begin && end == RHS.end && weight == RHS.weight;
  81.         }
  82.  
  83.         bool operator!=(const record& RHS) const
  84.         {
  85.             return !(operator==(RHS));
  86.         }
  87.     };
  88.  
  89.     /// Stores a set of selection ranges, where the map key is the beginning of the range, and the map value contains the end
  90.     typedef std::vector<record> records_t;
  91.     static const records_t component_select_all();
  92.     static const records_t component_deselect_all();
  93.  
  94.     records_t points;
  95.     records_t edges;
  96.     records_t faces;
  97.     records_t nurbs_curves;
  98.     records_t nurbs_patches;
  99.     //@}
  100.  
  101.  
  102.     /// Constructs a mesh selection that explicitly selects everything
  103.     static const mesh_selection select_all();
  104.     /// Constructs a mesh selection that explicitly deselects everything
  105.     static const mesh_selection deselect_all();
  106.     /// Constructs a mesh selection that explicitly doesn't alter selection at all
  107.     static const mesh_selection select_null();
  108.  
  109.     /// Copies the selection state of a mesh into a mesh_selection.
  110.     static void store(const mesh& Mesh, mesh_selection& Selection);
  111.     /// Copies the selection state of a mesh into a mesh_selection
  112.     static void store(const legacy::mesh& Mesh, mesh_selection& MeshSelection);
  113.  
  114.     /// Merges a mesh_selection with the current selection state in the given mesh.
  115.     static void merge(const mesh_selection& MeshSelection, mesh& Mesh);
  116.     /// Merges a set of mesh_selection records with the current selection state in the given array.
  117.     static void merge(const mesh_selection::records_t& Records, mesh::selection_t& Selection);
  118.     /// Copies mesh_selection state over any previous selection state in the given mesh
  119.     static void merge(const mesh_selection& MeshSelection, legacy::mesh& Mesh);
  120.  
  121.     struct component
  122.     {
  123.         component();
  124.         component(const uint_t PrimitiveBegin, const uint_t PrimitiveEnd, const selection::type Type);
  125.         component(const uint_t PrimitiveBegin, const uint_t PrimitiveEnd, const selection::type Type, const uint_t IndexBegin, const uint_t IndexEnd, const double_t Weight);
  126.  
  127.         void add_range(const uint_t IndexBegin, const uint_t IndexEnd, const double_t Weight);
  128.         void clear();
  129.         const bool_t empty() const;
  130.  
  131.         uint_t primitive_begin;
  132.         uint_t primitive_end;
  133.         selection::type type;
  134.  
  135.         std::vector<uint_t> index_begin;
  136.         std::vector<uint_t> index_end;
  137.         std::vector<double_t> weight;
  138.  
  139.         bool operator==(const component& RHS) const;
  140.     };
  141.  
  142.     /// Adds a new component to the selection.
  143.     void add_component(const component& Component);
  144.     /// Clears the selection.
  145.     void clear();
  146.     /// Returns true if the selection is "empty", i.e. none of the components are selected
  147.     bool empty() const;
  148.     /// Equivalence
  149.     bool operator==(const mesh_selection& RHS) const;
  150.     /// Non-equivalence
  151.     bool operator!=(const mesh_selection& RHS) const;
  152.  
  153.     typedef std::vector<component> components_t;
  154.     components_t components;
  155. };
  156.  
  157. std::ostream& operator<<(std::ostream&, const mesh_selection::records_t&);
  158. std::ostream& operator<<(std::ostream&, const mesh_selection&);
  159. std::ostream& operator<<(std::ostream&, const mesh_selection::component&);
  160.  
  161. /////////////////////////////////////////////////////////////////////////////
  162. // mesh_selection_serialization
  163.  
  164. /// k3d::data serialization policy for use with mesh selection data
  165. template<typename value_t, class property_policy_t>
  166. class mesh_selection_serialization :
  167.     public property_policy_t,
  168.     public ipersistent
  169. {
  170.     // This policy only works for data stored by-value
  171.     BOOST_STATIC_ASSERT((!boost::is_pointer<value_t>::value));
  172.  
  173. public:
  174.     void save(xml::element& Element, const ipersistent::save_context& Context)
  175.     {
  176.         xml::element& xml_property = Element.append(xml::element("property", xml::attribute("name", property_policy_t::name())));
  177.         xml::save(property_policy_t::internal_value(), xml_property, Context);
  178.     }
  179.  
  180.     void load(xml::element& Element, const ipersistent::load_context& Context)
  181.     {
  182.         xml::load(property_policy_t::internal_value(), Element, Context);
  183.     }
  184.  
  185. protected:
  186.     template<typename init_t>
  187.     mesh_selection_serialization(const init_t& Init) :
  188.         property_policy_t(Init)
  189.     {
  190.         Init.owner().enable_serialization(Init.name(), *this);
  191.     }
  192. };
  193.  
  194. } // namespace k3d
  195.  
  196. #endif // !K3DSDK_MESH_SELECTION_H
  197.  
  198.