From dd1e9fd41b1f051e59eb3343b64a619dd97c5eab Mon Sep 17 00:00:00 2001 From: Michael Turner Date: Wed, 23 Aug 2017 09:22:29 +0100 Subject: [PATCH] Re #37 Now using clang-format and const references --- .../MantidDataHandling/LoadNexusGeometry.h | 75 +++---- .../DataHandling/src/LoadNexusGeometry.cpp | 95 ++++----- .../DataHandling/test/LoadNexusGeometryTest.h | 186 +++++++++--------- 3 files changed, 180 insertions(+), 176 deletions(-) diff --git a/Framework/DataHandling/inc/MantidDataHandling/LoadNexusGeometry.h b/Framework/DataHandling/inc/MantidDataHandling/LoadNexusGeometry.h index a633beeab6c7..e0c1c7ac12bb 100644 --- a/Framework/DataHandling/inc/MantidDataHandling/LoadNexusGeometry.h +++ b/Framework/DataHandling/inc/MantidDataHandling/LoadNexusGeometry.h @@ -8,49 +8,52 @@ #include "Eigen/Core" -namespace Mantid{ -namespace DataHandling{ +namespace Mantid { +namespace DataHandling { class DLLExport LoadNexusGeometry - : public API::IFileLoader { + : public API::IFileLoader { public: - /// Default constructor - LoadNexusGeometry() = default; - /// Algorithm's name for identification overriding a virtual method - const std::string name() const override { return "LoadNexusGeometry"; } - /// Algorithm's version for identification overriding a virtual method - int version() const override { return 1; } - /// Algorithm's category for identification overriding a virtual method - const std::string category() const override { return "DataHandling\\Nexus"; } - /// Summary of algorithms purpose - const std::string summary() const override { - return "Loads Instrument Geometry from a NeXus file."; - } - - /// Returns a confidence value that this algorithm can load a file - int confidence(Kernel::NexusDescriptor &descriptor) const override; - - /// Add component to instrument - Geometry::IComponent* addComponent(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument); - /// Add source to instrument - void addSource(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument); - /// Add sample to instrument - void addSample(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument); - /// Add detector to instrument - void addDetector(std::string &name, Eigen::Vector3d &position, int detId, Geometry::Instrument_sptr instrument); -private: - /// Overwrites Algorithm method. - void init() override; - /// Overwrites Algorithm method - void exec() override; - //Instrument pointer - std::string defaultName = "defaultInstrumentName"; + /// Default constructor + LoadNexusGeometry() = default; + /// Algorithm's name for identification overriding a virtual method + const std::string name() const override { return "LoadNexusGeometry"; } + /// Algorithm's version for identification overriding a virtual method + int version() const override { return 1; } + /// Algorithm's category for identification overriding a virtual method + const std::string category() const override { return "DataHandling\\Nexus"; } + /// Summary of algorithms purpose + const std::string summary() const override { + return "Loads Instrument Geometry from a NeXus file."; + } + + /// Returns a confidence value that this algorithm can load a file + int confidence(Kernel::NexusDescriptor &descriptor) const override; + + /// Add component to instrument + Geometry::IComponent *addComponent(const std::string &name, + const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument); + /// Add source to instrument + void addSource(const std::string &name, const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument); + /// Add sample to instrument + void addSample(const std::string &name, const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument); + /// Add detector to instrument + void addDetector(const std::string &name, const Eigen::Vector3d &position, const int detId, + Geometry::Instrument_sptr instrument); +private: + /// Overwrites Algorithm method. + void init() override; + /// Overwrites Algorithm method + void exec() override; + // Instrument pointer + std::string defaultName = "defaultInstrumentName"; }; - } } #endif // LOAD_NEXUS_GEOMETRY_H_ - diff --git a/Framework/DataHandling/src/LoadNexusGeometry.cpp b/Framework/DataHandling/src/LoadNexusGeometry.cpp index 9c0d8a36d36a..06ed09f1f513 100644 --- a/Framework/DataHandling/src/LoadNexusGeometry.cpp +++ b/Framework/DataHandling/src/LoadNexusGeometry.cpp @@ -10,65 +10,70 @@ #include "MantidGeometry/Instrument/InstrumentVisitor.h" namespace Mantid { -namespace DataHandling -{ -//Register the algorithm into the algorithm factory +namespace DataHandling { +// Register the algorithm into the algorithm factory DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadNexusGeometry) -///Initialises algorithm -void LoadNexusGeometry::init() -{ - declareProperty("InstrumentName", " ", "Name of Instrument"); +/// Initialises algorithm +void LoadNexusGeometry::init() { + declareProperty("InstrumentName", " ", "Name of Instrument"); } -//Executes algorithm -void LoadNexusGeometry::exec() -{ - //Add instrument to data service - std::string instName = getPropertyValue("InstrumentName"); - Geometry::Instrument_sptr instrument(new Geometry::Instrument(instName)); +// Executes algorithm +void LoadNexusGeometry::exec() { + // Add instrument to data service + std::string instName = getPropertyValue("InstrumentName"); + Geometry::Instrument_sptr instrument(new Geometry::Instrument(instName)); - API::InstrumentDataService::Instance().add(instName, instrument); + API::InstrumentDataService::Instance().add(instName, instrument); } -//Set confidence level for successful loading -int LoadNexusGeometry::confidence(Kernel::NexusDescriptor &descriptor) const -{ return 0; } - -//Add component to instrument -Geometry::IComponent* LoadNexusGeometry::addComponent(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument) -{ - Geometry::IComponent *component(new Geometry::ObjCompAssembly(name)); - component->setPos (position(0), position(1), position(2)); - instrument->add(component); - //Return the component - return component; +// Set confidence level for successful loading +int LoadNexusGeometry::confidence(Kernel::NexusDescriptor &descriptor) const { + return 0; } -//Add source to instrument -void LoadNexusGeometry::addSource(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument) -{ - auto *source(this->addComponent(name, position, instrument)); - instrument->markAsSource(source); +// Add component to instrument +Geometry::IComponent * +LoadNexusGeometry::addComponent(const std::string &name, + const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument) { + Geometry::IComponent *component(new Geometry::ObjCompAssembly(name)); + component->setPos(position(0), position(1), position(2)); + instrument->add(component); + // Return the component + return component; } -//Add sample to instrument -void LoadNexusGeometry::addSample(std::string &name, Eigen::Vector3d &position, Geometry::Instrument_sptr instrument) -{ - auto *sample(this->addComponent(name, position, instrument)); - instrument->markAsSamplePos(sample); +// Add source to instrument +void LoadNexusGeometry::addSource(const std::string &name, + const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument) { + auto *source(this->addComponent(name, position, instrument)); + instrument->markAsSource(source); } -//Add detector to instrument -void LoadNexusGeometry::addDetector(std::string &name, Eigen::Vector3d &position, int detId, Geometry::Instrument_sptr instrument) -{ - auto *detector(new Geometry::Detector(name, detId, const_cast(instrument->getBaseComponent()))); - detector->setPos(position(0), position(1), position(2)); - instrument->add(detector); - instrument->markAsDetectorIncomplete(detector); - //Will be moved to exec, when more than one detector created - instrument->markAsDetectorFinalize(); +// Add sample to instrument +void LoadNexusGeometry::addSample(const std::string &name, + const Eigen::Vector3d &position, + Geometry::Instrument_sptr instrument) { + auto *sample(this->addComponent(name, position, instrument)); + instrument->markAsSamplePos(sample); } +// Add detector to instrument +void LoadNexusGeometry::addDetector(const std::string &name, + const Eigen::Vector3d &position, + const int detId, + Geometry::Instrument_sptr instrument) { + auto *detector(new Geometry::Detector( + name, detId, + const_cast(instrument->getBaseComponent()))); + detector->setPos(position(0), position(1), position(2)); + instrument->add(detector); + instrument->markAsDetectorIncomplete(detector); + // Will be moved to exec, when more than one detector created + instrument->markAsDetectorFinalize(); +} } } diff --git a/Framework/DataHandling/test/LoadNexusGeometryTest.h b/Framework/DataHandling/test/LoadNexusGeometryTest.h index a79d69d60fe8..5e6fb3d67f49 100644 --- a/Framework/DataHandling/test/LoadNexusGeometryTest.h +++ b/Framework/DataHandling/test/LoadNexusGeometryTest.h @@ -16,104 +16,100 @@ using namespace DataHandling; typedef boost::shared_ptr ComponentInfo_sptr; -class LoadNexusGeometryTest : public CxxTest::TestSuite -{ +class LoadNexusGeometryTest : public CxxTest::TestSuite { public: - void testInit() - { - LoadNexusGeometry loader; - loader.setChild(true); - TS_ASSERT(!loader.isInitialized ()); - loader.initialize (); - TS_ASSERT(loader.isInitialized ()); - - TS_ASSERT_THROWS_NOTHING(loader.setProperty("InstrumentName", "testInstName")); - TS_ASSERT(loader.getPropertyValue("InstrumentName") == "testInstName"); - } - - void testExec() - { - LoadNexusGeometry loader; - loader.initialize(); - loader.setProperty("InstrumentName", "testInstrument"); - - TS_ASSERT(!loader.isExecuted()); - TS_ASSERT(loader.execute()); - TS_ASSERT(loader.isExecuted()); - - //Get instrument from data service - Geometry::Instrument_sptr testInst1; - TS_ASSERT_THROWS_NOTHING(testInst1 = API::InstrumentDataService::Instance().retrieve("testInstrument")); - } - - void testMemberFunctions() - { - LoadNexusGeometry loader; - std::string instName = "testInst1"; - Geometry::Instrument_sptr instrument(new Geometry::Instrument(instName)); - - setUpAddDetectorWithParent(loader, instrument); - setUpAddSource(loader, instrument); - setUpAddSample(loader, instrument); - - //Convert to instrument_2 interface for testing - Geometry::InstrumentVisitor inst2 = Geometry::InstrumentVisitor(instrument); - TS_ASSERT(!inst2.isEmpty()); - inst2.walkInstrument(); - - ComponentInfo_sptr compInfo = inst2.componentInfo(); - - checkSource(compInfo); - checkSample(compInfo); - - checkDetectorWithParent(inst2, compInfo); - - - } + void testInit() { + LoadNexusGeometry loader; + loader.setChild(true); + TS_ASSERT(!loader.isInitialized()); + loader.initialize(); + TS_ASSERT(loader.isInitialized()); + + TS_ASSERT_THROWS_NOTHING( + loader.setProperty("InstrumentName", "testInstName")); + TS_ASSERT(loader.getPropertyValue("InstrumentName") == "testInstName"); + } + + void testExec() { + LoadNexusGeometry loader; + loader.initialize(); + loader.setProperty("InstrumentName", "testInstrument"); + + TS_ASSERT(!loader.isExecuted()); + TS_ASSERT(loader.execute()); + TS_ASSERT(loader.isExecuted()); + + // Get instrument from data service + Geometry::Instrument_sptr testInst1; + TS_ASSERT_THROWS_NOTHING( + testInst1 = + API::InstrumentDataService::Instance().retrieve("testInstrument")); + } + + void testMemberFunctions() { + LoadNexusGeometry loader; + std::string instName = "testInst1"; + Geometry::Instrument_sptr instrument(new Geometry::Instrument(instName)); + + setUpAddDetectorWithParent(loader, instrument); + setUpAddSource(loader, instrument); + setUpAddSample(loader, instrument); + + // Convert to instrument_2 interface for testing + Geometry::InstrumentVisitor inst2 = Geometry::InstrumentVisitor(instrument); + TS_ASSERT(!inst2.isEmpty()); + inst2.walkInstrument(); + + ComponentInfo_sptr compInfo = inst2.componentInfo(); + + checkSource(compInfo); + checkSample(compInfo); + + checkDetectorWithParent(inst2, compInfo); + } private: - - void setUpAddDetectorWithParent(LoadNexusGeometry &loader, Geometry::Instrument_sptr instrument) - { - std::string detName("testDetector"); - Eigen::Vector3d detPos(1.0,1.0,0.5); - int id(5); - TS_ASSERT_THROWS_NOTHING(loader.addDetector(detName, detPos, id, instrument)); - } - void setUpAddSource(LoadNexusGeometry &loader, Geometry::Instrument_sptr instrument) - { - std::string name = "testSource"; - Eigen::Vector3d pos(0.0,0.0,-1.0); - TS_ASSERT_THROWS_NOTHING(loader.addSource(name, pos, instrument)); - } - void setUpAddSample(LoadNexusGeometry &loader, Geometry::Instrument_sptr instrument) - { - std::string name = "testSample"; - Eigen::Vector3d pos(1.0, 0.0, 2.0); - TS_ASSERT_THROWS_NOTHING(loader.addSample(name, pos, instrument)); - } - - void checkDetectorWithParent(Geometry::InstrumentVisitor &inst, ComponentInfo_sptr compInfo) - { - //Get index of sole detector - auto detIndexMap = inst.detectorIdToIndexMap(); - TS_ASSERT(!detIndexMap->empty()); - TS_ASSERT(detIndexMap->begin()->first == 5); - auto detIndex(detIndexMap->begin()->second); - - TS_ASSERT(compInfo->isDetector(detIndex)); - TS_ASSERT(inst.detectorInfo()->position(detIndex) == Eigen::Vector3d(1.0,1.0,0.5)); - } - void checkSource(ComponentInfo_sptr compInfo) - { - TS_ASSERT(compInfo->hasSource()); - TS_ASSERT(compInfo->sourcePosition() == Eigen::Vector3d(0.0,0.0,-1.0)); - } - void checkSample(ComponentInfo_sptr compInfo) - { - TS_ASSERT(compInfo->hasSample()); - TS_ASSERT(compInfo->samplePosition() == Eigen::Vector3d(1.0,0.0,2.0)); - } + void setUpAddDetectorWithParent(LoadNexusGeometry &loader, + Geometry::Instrument_sptr instrument) { + std::string detName("testDetector"); + Eigen::Vector3d detPos(1.0, 1.0, 0.5); + int id(5); + TS_ASSERT_THROWS_NOTHING( + loader.addDetector(detName, detPos, id, instrument)); + } + void setUpAddSource(LoadNexusGeometry &loader, + Geometry::Instrument_sptr instrument) { + std::string name = "testSource"; + Eigen::Vector3d pos(0.0, 0.0, -1.0); + TS_ASSERT_THROWS_NOTHING(loader.addSource(name, pos, instrument)); + } + void setUpAddSample(LoadNexusGeometry &loader, + Geometry::Instrument_sptr instrument) { + std::string name = "testSample"; + Eigen::Vector3d pos(1.0, 0.0, 2.0); + TS_ASSERT_THROWS_NOTHING(loader.addSample(name, pos, instrument)); + } + + void checkDetectorWithParent(Geometry::InstrumentVisitor &inst, + ComponentInfo_sptr compInfo) { + // Get index of sole detector + auto detIndexMap = inst.detectorIdToIndexMap(); + TS_ASSERT(!detIndexMap->empty()); + TS_ASSERT(detIndexMap->begin()->first == 5); + auto detIndex(detIndexMap->begin()->second); + + TS_ASSERT(compInfo->isDetector(detIndex)); + TS_ASSERT(inst.detectorInfo()->position(detIndex) == + Eigen::Vector3d(1.0, 1.0, 0.5)); + } + void checkSource(ComponentInfo_sptr compInfo) { + TS_ASSERT(compInfo->hasSource()); + TS_ASSERT(compInfo->sourcePosition() == Eigen::Vector3d(0.0, 0.0, -1.0)); + } + void checkSample(ComponentInfo_sptr compInfo) { + TS_ASSERT(compInfo->hasSample()); + TS_ASSERT(compInfo->samplePosition() == Eigen::Vector3d(1.0, 0.0, 2.0)); + } }; #endif // LOAD_NEXUS_GEOMETRY_TEST_H_