1 /**
2  *  \file RMF/decorator/alternatives.h
3  *  \brief Helper functions for manipulating RMF files.
4  *
5  *  Copyright 2007-2021 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef RMF_DECORATORS_ALTERNATIVES_H
10 #define RMF_DECORATORS_ALTERNATIVES_H
11 
12 #include <RMF/config.h>
13 #include <RMF/infrastructure_macros.h>
14 #include <RMF/NodeHandle.h>
15 #include <RMF/FileHandle.h>
16 #include <RMF/Decorator.h>
17 #include <RMF/constants.h>
18 #include <RMF/Enum.h>
19 #include <RMF/Vector.h>
20 
21 RMF_ENABLE_WARNINGS
22 namespace RMF {
23 namespace decorator {
24 
25 /** See also Alternatives and AlternativesFactory.
26   */
27 class RMFEXPORT AlternativesConst : public Decorator {
28   friend class AlternativesFactory;
29   friend class Alternatives;
30   IntsKey types_key_;
31   IntsKey roots_key_;
AlternativesConst(NodeConstHandle nh,IntsKey types_key,IntsKey roots_key)32   AlternativesConst(NodeConstHandle nh, IntsKey types_key, IntsKey roots_key)
33       : Decorator(nh), types_key_(types_key), roots_key_(roots_key) {}
34   NodeID get_alternative_impl(RepresentationType type, float resolution) const;
35   NodeIDs get_alternatives_impl(RepresentationType type) const;
36 
37  public:
38   /** Get the alternative root that best matches the criteria. */
39   NodeConstHandle get_alternative(RepresentationType type,
40                                   double resolution) const;
41 
42   /** Get the type of the representation with the given node id. */
43   RepresentationType get_representation_type(NodeID id) const;
44 
get_representation_type(NodeConstHandle id)45   RepresentationType get_representation_type(NodeConstHandle id) const {
46     return get_representation_type(id.get_id());
47   }
48 
49   /** Get all the alternatives (including this node).
50 
51      You can use get_resolution() and get_representation_type() to get info
52      about them. */
53   NodeConstHandles get_alternatives(RepresentationType type) const;
54 
get_decorator_type_name()55   static std::string get_decorator_type_name() { return "AlternativesConst"; }
56   RMF_SHOWABLE(AlternativesConst, "Alternatives: " << get_node());
57 };
58 
59 /** See also AlternativesConst and AlternativesFactory.
60   */
61 class RMFEXPORT Alternatives : public AlternativesConst {
62   friend class AlternativesFactory;
63   Alternatives(NodeHandle nh, IntsKey types_key, IntsKey roots_key);
64 
65  public:
66   void add_alternative(NodeHandle root, RepresentationType type);
67 
get_decorator_type_name()68   static std::string get_decorator_type_name() { return "Alternatives"; }
69 };
70 
71 /** Create decorators of type Alternatives.
72 
73      See also Alternatives and AlternativesFactory.
74   */
75 class RMFEXPORT AlternativesFactory : public Factory {
76   Category cat_;
77   IntsKey types_key_;
78   IntsKey roots_key_;
79 
80  public:
81   AlternativesFactory(FileConstHandle fh);
82   AlternativesFactory(FileHandle fh);
83 
get(NodeHandle nh)84   Alternatives get(NodeHandle nh) const {
85     return Alternatives(nh, types_key_, roots_key_);
86   }
get(NodeConstHandle nh)87   AlternativesConst get(NodeConstHandle nh) const {
88     return AlternativesConst(nh, types_key_, roots_key_);
89   }
get_is(NodeConstHandle nh)90   bool get_is(NodeConstHandle nh) const { return nh.get_has_value(types_key_); }
get_is_static(NodeConstHandle nh)91   bool get_is_static(NodeConstHandle nh) const {
92     return nh.get_has_value(types_key_);
93   }
94   RMF_SHOWABLE(AlternativesFactory, "AlternativesFactory");
95 };
96 
97 #ifndef RMF_DOXYGEN
98 struct AlternativesConstFactory : public AlternativesFactory {
AlternativesConstFactoryAlternativesConstFactory99   AlternativesConstFactory(FileConstHandle fh) : AlternativesFactory(fh) {}
AlternativesConstFactoryAlternativesConstFactory100   AlternativesConstFactory(FileHandle fh) : AlternativesFactory(fh) {}
101 };
102 #endif
103 
104 /** Return the canonical resolution of the subtree. */
105 RMFEXPORT double get_resolution(NodeConstHandle root);
106 
107 /** Return a list of (clustered) resolution levels available in the subtree.
108 
109     Use this, for example, when making a slider for display.
110 */
111 RMFEXPORT Floats get_resolutions(NodeConstHandle root,
112                                  RepresentationType type = PARTICLE,
113                                  double accuracy = 0);
114 
115 } /* namespace decorator */
116 } /* namespace RMF */
117 RMF_DISABLE_WARNINGS
118 
119 #endif /* RMF_DECORATORS_ALTERNATIVES_H */
120