commit f1d96e7cc753ed1e51c547049595c5c3c0d5597a Author: David Baranyai Date: Mon Jul 2 01:20:09 2018 +0200 Added example project and data file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee88ab4 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# Parameters - Class for sharing data between classes +## Using +* Copy the source files into the corresponding directory in your project +* Create a pointer and initialize the class. For example: + +``` +Parameters *parameters = Parameters::GetInstance(); +``` + +It's the only way to initialize it, because the constructor is private. + +## How it works? +On the first initialization, it reads the file in the given format: + +Energy Shape +Length_X Lenght_Y Length_Z +Nx Ny Nz +index material (*Nx*Ny*Nz) + +Throws error if the index is incorrect in the file. + +## Public functions + +Returns with the material form the list by it's index (returns -1 if the index is out of range): + +``` +double GetMaterial(int index); +``` + +Returns with the material from the list by it's coordinates (not implemented yet): + +``` +double GetMaterial(int x, int y, int z); +``` + +Returns with the list size: + +``` +double GetListSize(); +``` + +Returns with the vector: + +``` +std::vector GetMaterialList(); +``` diff --git a/data.txt b/data.txt new file mode 100644 index 0000000..bca2484 --- /dev/null +++ b/data.txt @@ -0,0 +1,30 @@ +100 0 +5 5 5 +3 3 3 +0 26 +1 25 +2 24 +3 23 +4 22 +5 21 +6 20 +7 19 +8 18 +9 17 +10 16 +11 15 +12 14 +13 13 +14 12 +15 11 +16 10 +17 9 +18 8 +19 7 +20 6 +21 5 +22 4 +23 3 +24 2 +25 1 +26 0 diff --git a/include/Parameters.hpp b/include/Parameters.hpp new file mode 100644 index 0000000..b831d71 --- /dev/null +++ b/include/Parameters.hpp @@ -0,0 +1,52 @@ +// +// Parameters.hpp +// Parameters +// +// Created by Baranyai David on 2018. 06. 30.. +// Copyright © 2018. Baranyai David. All rights reserved. +// + +#ifndef Parameters_hpp +#define Parameters_hpp + +#include +#include +#include +#include +#include +#include + +class Parameters +{ +public: + static Parameters *GetInstance(); + + double GetMaterial(int, int, int); //Get by coordinates + double GetMaterial(int); //Get by index + std::vector GetMaterialList(); //Get the whole vector + double GetListSize(); + + ~Parameters(); +private: + Parameters(); + static Parameters *instance; + + std::ifstream file; + + double energy; + double shape; + + //Shape dimension + double length_x; + double length_y; + double length_z; + + //Voxel + double n_x; + double n_y; + double n_z; + + std::vector material; +}; + +#endif /* Parameters_hpp */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..04ea6b1 --- /dev/null +++ b/main.cpp @@ -0,0 +1,22 @@ +// +// main.cpp +// Parameters +// +// Created by Baranyai David on 2018. 06. 30.. +// Copyright © 2018. Baranyai David. All rights reserved. +// + +#include +#include "Parameters.hpp" + +int main(int argc, const char * argv[]) +{ + Parameters *param = Parameters::GetInstance(); + + for(int i = 0; i < param -> GetListSize(); i++) + { + std::cout << param -> GetMaterial(i) << std::endl; + } + + return 0; +} diff --git a/src/Parameters.cpp b/src/Parameters.cpp new file mode 100644 index 0000000..e7871a6 --- /dev/null +++ b/src/Parameters.cpp @@ -0,0 +1,87 @@ +// +// Parameters.cpp +// Parameters +// +// Created by Baranyai David on 2018. 06. 30.. +// Copyright © 2018. Baranyai David. All rights reserved. +// + +#include "Parameters.hpp" + +Parameters* Parameters::GetInstance() +{ + if (instance == 0) + { + instance = new Parameters(); + } + + return instance; +} + +Parameters* Parameters::instance = 0; + +Parameters::Parameters() +{ + try + { + file.open("data.txt"); + if(!file.is_open()) + { + throw "File opening error"; + } + + int index_helper = 0; + double material_helper = 0; + + //Read the data + file >> energy >> shape; + file >> length_x >> length_y >> length_z; + file >> n_x >> n_y >> n_z; + for(int i = 0; i < (n_x * n_y * n_z); i++) + { + file >> index_helper; + if(index_helper == i) + { + file >> material_helper; + material.push_back(material_helper); + } + else + { + throw "Index mismatch in file"; + } + } + } + //Check if any errors happen when opening the file + catch(char param) + { + std::cout << param << std::endl; + } +} + +double Parameters::GetMaterial(int x, int y, int z) +{ + //To-Do + return 0; +} + +double Parameters::GetMaterial(int index) +{ + if(index > material.size() || index < 0) + { + return -1; + } + else + { + return material[index]; + } +} + +std::vector Parameters::GetMaterialList() +{ + return material; +} + +double Parameters::GetListSize() +{ + return material.size(); +}