ParComponentFactory.cpp 3.78 KB
Newer Older
1
#include "MantidGeometry/Instrument/CompAssembly.h"
2
3
#include "MantidGeometry/Instrument/Component.h"
#include "MantidGeometry/Instrument/Detector.h"
4
#include "MantidGeometry/Instrument.h"
5
#include "MantidGeometry/Instrument/ObjCompAssembly.h"
6
#include "MantidGeometry/Instrument/ObjComponent.h"
7
8
#include "MantidGeometry/Instrument/ParComponentFactory.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
9
#include "MantidGeometry/Instrument/RectangularDetectorPixel.h"
Peterson, Peter's avatar
Peterson, Peter committed
10
#include <boost/make_shared.hpp>
11

12
13
namespace Mantid {
namespace Geometry {
14

15
16
17
18
19
20
21
22
/**
 * Create a parameterized detector from the given base detector and
 * ParameterMap. This version
 * avoids a cast by directly returning the Detector pointer
 * @param base A pointer to the unparameterized version
 * @param map A pointer to the ParameterMap
 * @returns A pointer to a parameterized component
 */
23
boost::shared_ptr<IDetector>
24
25
ParComponentFactory::createDetector(const IDetector *base,
                                    const ParameterMap *map) {
26
27
28
  // Clone may be a Detector or RectangularDetectorPixel instance (or nullptr)
  auto clone = base->cloneParameterized(map);
  return boost::shared_ptr<IDetector>(clone);
29
}
30

31
32
33
34
35
36
37
38
39
40
41
/**
 * Create a parameterized instrument from the given base detector and
 * ParameterMap. This version
 * avoids a cast by directly returning the Instrument pointer
 * @param base A pointer to the unparameterized version
 * @param map A pointer to the ParameterMap
 * @returns A pointer to a parameterized component
 */
boost::shared_ptr<Instrument>
ParComponentFactory::createInstrument(boost::shared_ptr<const Instrument> base,
                                      boost::shared_ptr<ParameterMap> map) {
Peterson, Peter's avatar
Peterson, Peter committed
42
  return boost::make_shared<Instrument>(base, map);
43
}
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Create a parameterized component from the given base component and
 * ParameterMap
 * SLOW VERSION as it has to check each possible type
 * @param base A pointer to the unparameterized version
 * @param map A pointer to the ParameterMap
 * @returns A pointer to a parameterized component
 */
IComponent_sptr
ParComponentFactory::create(boost::shared_ptr<const IComponent> base,
                            const ParameterMap *map) {
  boost::shared_ptr<const IDetector> det_sptr =
      boost::dynamic_pointer_cast<const IDetector>(base);
  if (det_sptr) {
    return createDetector(det_sptr.get(), map);
  }
61

62
63
64
65
66
67
68
69
70
71
72
  boost::shared_ptr<const Instrument> inst_sptr =
      boost::dynamic_pointer_cast<const Instrument>(base);
  // @todo One of the review tasks is to take a look at the parameterized mess
  // and
  // short out this problem with different classes carrying different types of
  // pointers around
  if (inst_sptr) {
    return createInstrument(boost::const_pointer_cast<Instrument>(inst_sptr),
                            boost::shared_ptr<ParameterMap>(
                                const_cast<ParameterMap *>(map), NoDeleting()));
  }
73

74
75
76
77
78
79
  // Everything gets created on the fly. Note that the order matters here
  // @todo Really could do with a better system than this. Virtual function
  // maybe?
  const RectangularDetector *rd =
      dynamic_cast<const RectangularDetector *>(base.get());
  if (rd)
Peterson, Peter's avatar
Peterson, Peter committed
80
    return boost::make_shared<RectangularDetector>(rd, map);
81

82
83
  const CompAssembly *ac = dynamic_cast<const CompAssembly *>(base.get());
  if (ac)
Peterson, Peter's avatar
Peterson, Peter committed
84
    return boost::make_shared<CompAssembly>(ac, map);
85
86
87
  const ObjCompAssembly *oac =
      dynamic_cast<const ObjCompAssembly *>(base.get());
  if (oac)
Peterson, Peter's avatar
Peterson, Peter committed
88
    return boost::make_shared<ObjCompAssembly>(oac, map);
89

90
91
  const ObjComponent *oc = dynamic_cast<const ObjComponent *>(base.get());
  if (oc)
Peterson, Peter's avatar
Peterson, Peter committed
92
    return boost::make_shared<ObjComponent>(oc, map);
93
94
95
  // Must be a component
  const IComponent *cc = dynamic_cast<const IComponent *>(base.get());
  if (cc)
Peterson, Peter's avatar
Peterson, Peter committed
96
    return boost::make_shared<Component>(cc, map);
97

98
99
100
  return IComponent_sptr();
}
}
101
}