Browse Source

jsais pu trop désolé

master
DemiSel 3 years ago
parent
commit
9ac7423034

+ 17
- 2
lib/Agents/Agent.hpp View File

@@ -9,11 +9,13 @@ public:
9 9
 	Perception fCurrentPerception;
10 10
 	PictureEnvironment* fEnvironment;
11 11
 	sf::Color fColor;
12
+	int fDotSize;
13
+	int fMoveSize;
12 14
 	int fXPos;
13 15
 	int fYPos;
14 16
 
15 17
 	Agent();
16
-	Agent(PictureEnvironment* iEnvironment);
18
+	Agent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize);
17 19
 	virtual void loop();
18 20
 
19 21
 	virtual void move();
@@ -23,10 +25,23 @@ public:
23 25
 class RepaintAgent : public Agent
24 26
 {
25 27
 public:
26
-	RepaintAgent(PictureEnvironment* iEnvironment);
28
+	//list of possible movement vectors, to be used to perceive the environment
29
+	std::vector<std::vector<int>> fMovements;
30
+	std::vector<int> fValidMovementIndexes;
31
+	std::vector<int> fCurrentMovement;
32
+
33
+	RepaintAgent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize);
27 34
 	virtual void loop();
28 35
 	virtual void move();
29 36
 	virtual void paint();
30 37
 };
31 38
 
39
+class PullPaintAgent : public RepaintAgent
40
+{
41
+public:
42
+	PullPaintAgent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize);
43
+	virtual void loop();
44
+	virtual void move();
45
+};
46
+
32 47
 #endif // AGENT_H

+ 6
- 3
lib/Environment/PictureEnvironment.hpp View File

@@ -18,17 +18,20 @@ public: // Access specifier
18 18
 
19 19
 	sf::Texture fTextureImage;
20 20
 	sf::Image fImage;
21
+	sf::Image fOriginalImage;
21 22
 
22 23
 	std::vector<std::unique_ptr<Agent>> fAgents;
23 24
 	std::list<Agent>::iterator fAgentsIterator;
24 25
 
25 26
 	PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow, util::Platform* iPlatform);
26 27
 	void getPerception(Perception* ioPercept, int iX, int iY);
27
-	void makeAgents(int iNumber);
28
-	void makeRepaintAgents(int iNumber);
28
+	void makeAgents(int iNumber, int iDotSize, int iMoveSize);
29
+	void makeRepaintAgents(int iNumber, int iDotSize, int iMoveSize);
30
+	void makePullPaintAgents(int iNumber, int iDotSize, int iMoveSize);
29 31
 	bool paint(int iX, int iY, const sf::Color Color);
30 32
 
31
-	uint32_t getCell(int iX, int iY);
33
+	sf::Uint32 getCell(int iX, int iY);
34
+	sf::Uint32 getCellOriginal(int iX, int iY);
32 35
 
33 36
 	int getWidth();
34 37
 	int getHeight();

+ 7
- 7
src/Agents/Agent.cpp View File

@@ -8,10 +8,12 @@ Agent::Agent()
8 8
 
9 9
 int PERCEPTION_SIZE = 2;
10 10
 
11
-Agent::Agent(PictureEnvironment* iEnvironment)
11
+Agent::Agent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize)
12 12
 {
13 13
 	//uint32_t WHITE = 0xFFFFFFFF;
14 14
 	fEnvironment = iEnvironment;
15
+	fDotSize = iDotSize;
16
+	fMoveSize = iMoveSize;
15 17
 	fCurrentPerception = Perception(PERCEPTION_SIZE, PERCEPTION_SIZE);
16 18
 	//fColor = sf::Color(rand() % 0xFFFFFFFFFFFF);
17 19
 	uint32_t vRand = (((double)rand() / RAND_MAX) * 0xFFFFFFFFU);
@@ -28,14 +30,12 @@ void Agent::loop()
28 30
 	this->paint();
29 31
 }
30 32
 
31
-int MOVESIZE = 10;
32
-
33 33
 void Agent::move()
34 34
 {
35
-	double vRand = rand() % (MOVESIZE + 1);
36
-	fXPos += vRand - MOVESIZE / 2;
37
-	vRand = rand() % (MOVESIZE + 1);
38
-	fYPos += vRand - MOVESIZE / 2;
35
+	double vRand = rand() % (fMoveSize + 1);
36
+	fXPos += vRand - fMoveSize / 2;
37
+	vRand = rand() % (fMoveSize + 1);
38
+	fYPos += vRand - fMoveSize / 2;
39 39
 }
40 40
 
41 41
 void Agent::paint()

+ 28
- 0
src/Agents/PullPaintAgent.cpp View File

@@ -0,0 +1,28 @@
1
+#include <Agents/Agent.hpp>
2
+#include <Environment/PictureEnvironment.hpp>
3
+
4
+PullPaintAgent::PullPaintAgent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize) :
5
+	RepaintAgent(iEnvironment, iDotSize, iMoveSize)
6
+{
7
+	int vWidth = fEnvironment->getWidth();
8
+	int vHeight = fEnvironment->getHeight();
9
+
10
+	fXPos = (((double)rand() / RAND_MAX) * vWidth);
11
+	fYPos = (((double)rand() / RAND_MAX) * vHeight);
12
+
13
+	fColor = sf::Color(fEnvironment->getCell(fXPos, fYPos));
14
+}
15
+
16
+void PullPaintAgent::move()
17
+{
18
+	double vRand = rand() % (fMoveSize + 1);
19
+	fXPos += vRand - fMoveSize / 2;
20
+	vRand = rand() % (fMoveSize + 1);
21
+	fYPos += vRand - fMoveSize / 2;
22
+}
23
+
24
+void PullPaintAgent::loop()
25
+{
26
+	move();
27
+	this->paint();
28
+}

+ 45
- 12
src/Agents/RepaintAgent.cpp View File

@@ -1,8 +1,8 @@
1 1
 #include <Agents/Agent.hpp>
2 2
 #include <Environment/PictureEnvironment.hpp>
3 3
 
4
-RepaintAgent::RepaintAgent(PictureEnvironment* iEnvironment) :
5
-	Agent(iEnvironment)
4
+RepaintAgent::RepaintAgent(PictureEnvironment* iEnvironment, int iDotSize, int iMoveSize) :
5
+	Agent(iEnvironment, iDotSize, iMoveSize)
6 6
 {
7 7
 	int vWidth = fEnvironment->getWidth();
8 8
 	int vHeight = fEnvironment->getHeight();
@@ -11,23 +11,57 @@ RepaintAgent::RepaintAgent(PictureEnvironment* iEnvironment) :
11 11
 	fYPos = (((double)rand() / RAND_MAX) * vHeight);
12 12
 
13 13
 	fColor = sf::Color(fEnvironment->getCell(fXPos, fYPos));
14
-}
15 14
 
16
-int MOVESIZE_PAINT = 10;
15
+	//Initalize list of possible movement vectors
16
+	fMovements = std::vector<std::vector<int>>();
17
+	fValidMovementIndexes = std::vector<int>();
18
+	fCurrentMovement = std::vector<int>();
19
+	for (int i = -fMoveSize; i <= fMoveSize; i++)
20
+	{
21
+		for (int j = -fMoveSize; j <= fMoveSize; j++)
22
+		{
23
+			fMovements.push_back({ i, j });
24
+		}
25
+	}
26
+}
17 27
 
18 28
 void RepaintAgent::move()
19 29
 {
20
-	double vRand = rand() % (MOVESIZE_PAINT + 1);
21
-	fXPos += vRand - MOVESIZE_PAINT / 2;
22
-	vRand = rand() % (MOVESIZE_PAINT + 1);
23
-	fYPos += vRand - MOVESIZE_PAINT / 2;
24
-}
30
+	fValidMovementIndexes.clear();
31
+	int tolerance = 5;
32
+	for (int i = 0; i < static_cast<int>(fMovements.size()); i++)
33
+	{
34
+		sf::Uint32 va = fEnvironment->getCellOriginal(fXPos + fMovements.at(i).at(0), fYPos + fMovements.at(i).at(1));
35
+		sf::Uint32 vb = (int)fColor.toInteger();
36
+		//std::cout << std::hex << va << " : " << std::hex << vb << "\n";
37
+		//std::cout << std::hex << (va << 8 >> 24) << " : " << (vb << 8 >> 24) << "\n";
38
+		//std::cout << abs((int)(va << 8 >> 24) - (int)(vb << 8 >> 24)) << "\n\n";
39
+		bool vcomp =
40
+			abs((int)(va >> 24) - (int)(vb >> 24)) <= tolerance &&			 //R
41
+			abs((int)(va << 8 >> 24) - (int)(vb << 8 >> 24)) <= tolerance && //G
42
+			abs((int)(va << 16 >> 24) - (int)(vb << 16 >> 24)) <= tolerance; //B
25 43
 
26
-int BRUSH_SIZE_PAINT = 20;
44
+		//if ((double)std::min(va, vb) / std::max(va, vb) > 0.7)
45
+		if (vcomp)
46
+		{
47
+			//std::cout << vcomp << "\n";
48
+			//std::string j;
49
+			//std::cin >> j;
50
+			fValidMovementIndexes.push_back(i);
51
+		}
52
+	}
53
+	if (fValidMovementIndexes.size() > 0)
54
+	{
55
+		int vRandIndex = rand() % static_cast<int>(fValidMovementIndexes.size());
56
+		fCurrentMovement = fMovements.at(fValidMovementIndexes.at(vRandIndex));
57
+		fXPos += fCurrentMovement.at(0);
58
+		fYPos += fCurrentMovement.at(1);
59
+	}
60
+}
27 61
 
28 62
 void RepaintAgent::paint()
29 63
 {
30
-	int vHalfBrush = BRUSH_SIZE_PAINT / 2;
64
+	int vHalfBrush = fDotSize / 2;
31 65
 	int vXLoopIndex = fXPos - vHalfBrush;
32 66
 	int vYLoopIndex = fYPos - vHalfBrush;
33 67
 	while (vXLoopIndex < (fXPos + vHalfBrush))
@@ -44,7 +78,6 @@ void RepaintAgent::paint()
44 78
 
45 79
 void RepaintAgent::loop()
46 80
 {
47
-	fEnvironment->getPerception(&fCurrentPerception, fXPos, fYPos);
48 81
 	move();
49 82
 	this->paint();
50 83
 }

+ 31
- 9
src/Environment/PictureEnvironment.cpp View File

@@ -20,6 +20,7 @@ PictureEnvironment::PictureEnvironment(std::string& iFilePath, sf::RenderWindow*
20 20
 		std::cout << "\n COULDNT LOAD PICTURE\n";
21 21
 	}
22 22
 	fImage = fTextureImage.copyToImage();
23
+	fOriginalImage = fTextureImage.copyToImage();
23 24
 
24 25
 	//memccpy(&fFilePath, &iFilePath, *iFilePath., 10);
25 26
 	fPlatform = iPlatform;
@@ -31,7 +32,7 @@ PictureEnvironment::PictureEnvironment(std::string& iFilePath, sf::RenderWindow*
31 32
 	fInitialSize.y = 600.0f * screenScalingFactor;
32 33
 	//fShape = sf::RectangleShape(fSize);
33 34
 	fShape = sf::RectangleShape(DEFAUMT_SIMZE);
34
-
35
+	fAgents = std::vector<std::unique_ptr<Agent>>();
35 36
 	fShape.setFillColor(sf::Color::White);
36 37
 
37 38
 	fShape.setTexture(&fTextureImage);
@@ -102,28 +103,49 @@ int PictureEnvironment::getWidth()
102 103
 	return fImage.getSize().x;
103 104
 }
104 105
 
105
-uint32_t PictureEnvironment::getCell(int iX, int iy)
106
+sf::Uint32 PictureEnvironment::getCell(int iX, int iY)
106 107
 {
107
-	return fImage.getPixel(iX, iy).toInteger();
108
+	bool doable = iX >= 0 && iX < getWidth() && iY >= 0 && iY < getHeight();
109
+	uint32_t v_Ret = 0;
110
+	if (doable)
111
+	{
112
+		v_Ret = fImage.getPixel(iX, iY).toInteger();
113
+	}
114
+	return v_Ret;
108 115
 }
109 116
 
110
-void PictureEnvironment::makeAgents(int iNumber)
117
+sf::Uint32 PictureEnvironment::getCellOriginal(int iX, int iY)
111 118
 {
119
+	bool doable = iX >= 0 && iX < getWidth() && iY >= 0 && iY < getHeight();
120
+	uint32_t v_Ret = 0;
121
+	if (doable)
122
+	{
123
+		v_Ret = fOriginalImage.getPixel(iX, iY).toInteger();
124
+	}
125
+	return v_Ret;
126
+}
112 127
 
113
-	fAgents = std::vector<std::unique_ptr<Agent>>();
128
+void PictureEnvironment::makeAgents(int iNumber, int iDotSize, int iMoveSize)
129
+{
130
+	for (int i = 0; i < iNumber; i++)
131
+	{
132
+		fAgents.push_back(std::make_unique<Agent>(this, iDotSize, iMoveSize));
133
+	}
134
+}
114 135
 
136
+void PictureEnvironment::makeRepaintAgents(int iNumber, int iDotSize, int iMoveSize)
137
+{
115 138
 	for (int i = 0; i < iNumber; i++)
116 139
 	{
117
-		fAgents.push_back(std::make_unique<Agent>(this));
140
+		fAgents.push_back(std::make_unique<RepaintAgent>(this, iDotSize, iMoveSize));
118 141
 	}
119 142
 }
120 143
 
121
-void PictureEnvironment::makeRepaintAgents(int iNumber)
144
+void PictureEnvironment::makePullPaintAgents(int iNumber, int iDotSize, int iMoveSize)
122 145
 {
123
-	fAgents = std::vector<std::unique_ptr<Agent>>();
124 146
 	for (int i = 0; i < iNumber; i++)
125 147
 	{
126
-		fAgents.push_back(std::make_unique<RepaintAgent>(this));
148
+		fAgents.push_back(std::make_unique<PullPaintAgent>(this, iDotSize, iMoveSize));
127 149
 	}
128 150
 }
129 151
 

+ 5
- 4
src/Main.cpp View File

@@ -10,8 +10,8 @@ int main()
10 10
 	std::cout << "Hello World!" << std::endl;
11 11
 #endif
12 12
 	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\dickbutt.jpg";
13
-	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\caisseenre.png";
14
-	std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\RepAgents\\PA315024.jpg";
13
+	std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\caisseenre.png";
14
+	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\RepAgents\\PA315024.jpg";
15 15
 	//std::string filepath = "content/sfml.png";
16 16
 	sf::RenderWindow window;
17 17
 
@@ -23,8 +23,9 @@ int main()
23 23
 
24 24
 	PictureEnvironment vEnvironment(filepath, &window, &platform);
25 25
 
26
-	//vEnvironment.makeAgents(10000);
27
-	vEnvironment.makeRepaintAgents(500);
26
+	//vEnvironment.makeAgents(10000, 1, 10);
27
+	vEnvironment.makeRepaintAgents(300, 20, 10);
28
+	vEnvironment.makePullPaintAgents(200, 20, 10);
28 29
 	//int i = 0;
29 30
 	while (vEnvironment.loop())
30 31
 	{

Loading…
Cancel
Save