Added example project and data file

This commit is contained in:
David Baranyai 2018-07-02 01:20:09 +02:00
commit f1d96e7cc7
5 changed files with 237 additions and 0 deletions

46
README.md Normal file
View File

@ -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<double> GetMaterialList();
```

30
data.txt Normal file
View File

@ -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

52
include/Parameters.hpp Normal file
View File

@ -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 <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
class Parameters
{
public:
static Parameters *GetInstance();
double GetMaterial(int, int, int); //Get by coordinates
double GetMaterial(int); //Get by index
std::vector<double> 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<double> material;
};
#endif /* Parameters_hpp */

22
main.cpp Normal file
View File

@ -0,0 +1,22 @@
//
// main.cpp
// Parameters
//
// Created by Baranyai David on 2018. 06. 30..
// Copyright © 2018. Baranyai David. All rights reserved.
//
#include <iostream>
#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;
}

87
src/Parameters.cpp Normal file
View File

@ -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<double> Parameters::GetMaterialList()
{
return material;
}
double Parameters::GetListSize()
{
return material.size();
}