Skip to content

Commit 0e11952

Browse files
adding decorator for bird view analysis
1 parent d2cb768 commit 0e11952

File tree

9 files changed

+95
-14
lines changed

9 files changed

+95
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ADD_LIBRARY(LibsModule
2121
src/Wind.cpp
2222
src/Math.cpp
2323
src/Metric.cpp
24+
src/ProfilingDecorator.cpp
2425
)
2526

2627
target_link_libraries(LibsModule PRIVATE PostgreSQL::PostgreSQL)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MAINFILE := src/main.cpp
55
TESTMAINFILE := tests/test_main.cpp
66
TARGET := bin/main
77

8-
SRCS := src/Cell.cpp src/CellStorage.cpp src/Coordinates.cpp src/Wind.cpp src/Fire.cpp src/Connection.cpp src/Math.cpp src/Metric.cpp
8+
SRCS := src/Cell.cpp src/CellStorage.cpp src/Coordinates.cpp src/Wind.cpp src/Fire.cpp src/Connection.cpp src/Math.cpp src/Metric.cpp src/ProfilingDecorator.cpp
99

1010
compile_run: clean compile run
1111

include/Math.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class Math
99
Math();
1010
//virtual double calculateKoef(float windSpeed, double slopeAngleRad) const = 0;
1111
//virtual double calculateWindKoef(const cell* c, directions InvestigatedDirection) const = 0;
12-
virtual bool willSpread(const cell* c, directions InvestigatedDirection) const = 0;
13-
virtual bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection) const = 0;
12+
virtual bool willSpread(const cell* c, directions InvestigatedDirection, int altitudeDifference) const = 0;
13+
virtual bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection, int altitudeDifference) const = 0;
1414
~Math();
1515
};
1616

@@ -26,14 +26,15 @@ constexpr int throughPercentage(){
2626
/// "Aildland fire spread modelling using cellular
2727
/// automata: evolution in large-scale spatially heterogeneous environments
2828
/// under fire suppression tactics"
29-
class Math1 final: public Math
29+
class Math1 final: public Math
3030
{
3131
private:
3232
double calculateKoef(float windSpeed, double slopeAngleRad) const;
3333
double calculateWindKoef(const cell* c, directions InvestigatedDirection) const;
34+
double calculateGroundSlopeKoef(directions InvestigatedDirection, int altitudeDifference) const;
3435
public:
35-
bool willSpread(const cell* c, directions InvestigatedDirection) const override;
36-
bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection) const override;
36+
bool willSpread(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
37+
bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
3738
Math1() = default;
3839
~Math1() = default;
3940
};

include/ProfilingDecorator.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include <map>
3+
#include "Math.h"
4+
5+
class ProfilingDecorator final: public Math
6+
{
7+
private:
8+
Math* primary_class;
9+
mutable int positiveCounter;
10+
mutable int counter;
11+
public:
12+
ProfilingDecorator(Math* formula);
13+
bool willSpread(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
14+
bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
15+
~ProfilingDecorator();
16+
};

src/CellStorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void CellStorage::iterateCell(int i, int j)
9797
double fireKoeff = 0;
9898
if (nearestCell->getState() == cellState::Tree)
9999
{
100-
if (formula->willSpread(iteratedCell, analyzedDirection))
100+
if (formula->willSpread(iteratedCell, analyzedDirection, iteratedCell->getAltitude() - nearestCell->getAltitude()))
101101
{
102102
setNewState(cellState::Fire, i + x.first, j + x.second);
103103
};
@@ -109,7 +109,7 @@ void CellStorage::iterateCell(int i, int j)
109109
};
110110
if ((throughCell->getState() == cellState::Tree) && (nearestCell->getState() != cellState::Tree) && ((nearestCell->getState() != cellState::Fire)))
111111
{
112-
if (formula->willSpreadThroughOne(iteratedCell, analyzedDirection))
112+
if (formula->willSpreadThroughOne(iteratedCell, analyzedDirection, iteratedCell->getAltitude() - throughCell->getAltitude()))
113113
{
114114
setNewState(cellState::Fire, i + 2 * x.first, j + 2 * x.second);
115115
};

src/Math.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../include/Math.h"
22
#include "../include/Properties.h"
3+
#include <cmath>
34

45
Math::Math(/* args */)
56
{
@@ -19,18 +20,38 @@ double Math1::calculateKoef(float windSpeed, double slopeAngleRad) const
1920
double Math1::calculateWindKoef(const cell *c, directions InvestigatedDirection) const
2021
{
2122
auto wind = c->getWind().get();
23+
if (wind == nullptr){
24+
return double(1);
25+
}
2226
float angleRadians = wind->angleBetweenDirections(InvestigatedDirection) * pi() / 180;
2327
return std::exp(wind->getWindSpeed() * (cos(cos(angleRadians)) - 1));
2428
}
2529

26-
bool Math1::willSpread(const cell *c, directions InvestigatedDirection) const
30+
double Math1::calculateGroundSlopeKoef(directions InvestigatedDirection, int altitudeDifference) const
31+
{
32+
double coef = 0;
33+
if (InvestigatedDirection == directions::North || InvestigatedDirection == directions::West ||
34+
InvestigatedDirection == directions::East || InvestigatedDirection == directions::South)
35+
{
36+
coef = atan2(static_cast<double>(altitudeDifference), static_cast<double>(cellSizeInMeters()));
37+
}
38+
else
39+
{
40+
coef = atan2(static_cast<double>(altitudeDifference), static_cast<double>(cellSizeInMeters() * sqrt(2.0)));
41+
}
42+
return std::exp(0.5 * coef);
43+
}
44+
45+
bool Math1::willSpread(const cell *c, directions InvestigatedDirection, int altitudeDifference) const
2746
{
2847
double fireKoeff = calculateWindKoef(c, InvestigatedDirection);
29-
return int(fireKoeff * 100) + (rand() % 100) > ignitionPercentage();
48+
double slopeKoeff = calculateGroundSlopeKoef(InvestigatedDirection, altitudeDifference);
49+
return int(fireKoeff * slopeKoeff * 100) + (rand() % 100) > ignitionPercentage();
3050
}
3151

32-
bool Math1::willSpreadThroughOne(const cell *c, directions InvestigatedDirection) const
52+
bool Math1::willSpreadThroughOne(const cell *c, directions InvestigatedDirection, int altitudeDifference) const
3353
{
3454
double fireKoeff = calculateWindKoef(c, InvestigatedDirection);
35-
return int(fireKoeff * 100) + (rand() % 30) > throughPercentage();
55+
double slopeKoeff = calculateGroundSlopeKoef(InvestigatedDirection, altitudeDifference);
56+
return int(fireKoeff * slopeKoeff * 100) + (rand() % 30) > throughPercentage();
3657
}

src/ProfilingDecorator.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "../include/ProfilingDecorator.h"
2+
#include "ProfilingDecorator.h"
3+
#include <iostream>
4+
5+
ProfilingDecorator::ProfilingDecorator(Math *formula)
6+
: primary_class(formula), counter(0), positiveCounter(0)
7+
{
8+
}
9+
10+
bool ProfilingDecorator::willSpread(const cell *c, directions InvestigatedDirection, int altitudeDifference) const
11+
{
12+
auto result = this->primary_class->willSpread(c, InvestigatedDirection, altitudeDifference);
13+
this->counter++;
14+
if (result)
15+
{
16+
this->positiveCounter++;
17+
};
18+
return result;
19+
}
20+
21+
bool ProfilingDecorator::willSpreadThroughOne(const cell *c, directions InvestigatedDirection, int altitudeDifference) const
22+
{
23+
return this->primary_class->willSpread(c, InvestigatedDirection, altitudeDifference);
24+
}
25+
26+
ProfilingDecorator::~ProfilingDecorator()
27+
{
28+
std::cout << "Overall: " << this->counter << " Positive: " << this->positiveCounter << std::endl;
29+
primary_class->~Math();
30+
}

src/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../include/Connection.h"
1010
#include "../include/Math.h"
1111
#include "../include/Metric.h"
12+
#include "../include/ProfilingDecorator.h"
1213

1314
//#include <windows.h>
1415
//#include <Psapi.h>
@@ -22,7 +23,8 @@ int main()
2223

2324
Connection c = Connection();
2425
Math1 formula = Math1();
25-
CellStorage s = CellStorage(&formula);
26+
auto formulaDecorator = ProfilingDecorator(&formula);
27+
CellStorage s = CellStorage(&formulaDecorator);
2628
s.latitudeMin = 10.0;
2729
s.longtitudeMin = 30.0;
2830
auto xRange = std::make_pair<int, int>(0, getXArea());
@@ -67,6 +69,5 @@ int main()
6769
m->calculateVariables(s, fired, burnt);
6870
std::cout <<"Simpson metric is equal to: " << m->compute() << std::endl;
6971
printf("Time taken: %.2fs\n", (double)(clock() - tStartMetrics) / CLOCKS_PER_SEC);
70-
// s.printCurrentStates();
7172
return 0;
7273
}

tests/test_main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ void test_Wind(){
100100
assert(fabs(w.CalculateWindKoef((directions::North)) - 0.63147451510646979) < tolerance());
101101
}
102102

103+
void test_Math(){
104+
Math1 formula = Math1();
105+
106+
cell c = cell();
107+
c.setState(cellState::Fire);
108+
c.setAltitude(100);
109+
assert(formula.willSpread(&c, directions::East, 1) == false);
110+
}
111+
103112
void testFire(){
104113
Fire fire = Fire();
105114
assert(fire.getState() == FireState::Cursory);
@@ -167,6 +176,8 @@ int main()
167176
std::cout << "fire in cell tested successfully" << std::endl;
168177
testMetrics();
169178
std::cout << "metrics tested successfully" << std::endl;
179+
test_Math();
180+
std::cout << "math tested successfully" << std::endl;
170181
std::cout << "all tests passed" << std::endl;
171182
return 0;
172183
}

0 commit comments

Comments
 (0)