forked from alvesoaj/eFLL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuzzyComposition.cpp
executable file
·247 lines (218 loc) · 5.92 KB
/
FuzzyComposition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Robotic Research Group (RRG)
* State University of Piaui (UESPI), Brazil - Piauí - Teresina
*
* FuzzyComposition.cpp
*
* Author: Msc. Marvin Lemos <[email protected]>
* AJ Alves <[email protected]>
* Co authors: Douglas S. Kridi <[email protected]>
* Kannya Leal <[email protected]>
*/
#include "FuzzyComposition.h"
#include <math.h>
// CONSTRUTORES
FuzzyComposition::FuzzyComposition(){
this->pointsCursor = NULL;
this->points = NULL;
}
// DESTRUTOR
FuzzyComposition::~FuzzyComposition(){
this->cleanPoints(this->points);
}
bool FuzzyComposition::addPoint(float point, float pertinence){
pointsArray* aux;
// Alocando espaço na memória
if((aux = (pointsArray* ) malloc(sizeof(pointsArray))) == NULL){
return false;
}
aux->previous = NULL;
aux->point = point;
aux->pertinence = pertinence;
aux->next = NULL;
if(this->points == NULL){
this->points = aux;
this->pointsCursor = aux;
}else{
aux->previous = this->pointsCursor;
this->pointsCursor = aux;
aux->previous->next = this->pointsCursor;
}
return true;
}
bool FuzzyComposition::checkPoint(float point, float pertinence){
pointsArray* aux;
aux = this->pointsCursor;
while(aux != NULL){
if(aux->point == point && aux->pertinence == pertinence){
return true;
}
aux = aux->previous;
}
return false;
}
bool FuzzyComposition::build(){
pointsArray* aux;
aux = this->points;
while(aux != NULL){
pointsArray* temp = aux;
while(temp->previous != NULL){
if(temp->point < temp->previous->point){
break;
}
temp = temp->previous;
}
pointsArray* zPoint;
if(temp != NULL){
zPoint = temp;
while(temp->previous != NULL){
bool result = false;
if(temp->previous->previous != NULL){
result = rebuild(zPoint, zPoint->next, temp->previous, temp->previous->previous);
}
if(result == true){
aux = this->points;
break;
}
temp = temp->previous;
}
}
aux = aux->next;
}
return true;
}
float FuzzyComposition::avaliate(){
pointsArray* aux;
float numerator = 0.0;
float denominator = 0.0;
aux = this->points;
while(aux != NULL){
if(aux->next != NULL){
float area = 0.0;
float middle = 0.0;
if(aux->point == aux->next->point){
// Se Singleton
area = aux->pertinence;
middle = aux->point;
}else if(aux->pertinence == 0.0 || aux->next->pertinence == 0.0){
// Se triangulo
float pertinence;
if(aux->pertinence > 0.0){
pertinence = aux->pertinence;
}else{
pertinence = aux->next->pertinence;
}
area = ((aux->next->point - aux->point) * pertinence) / 2.0;
if(aux->pertinence < aux->next->pertinence){
middle = ((aux->next->point - aux->point) / 1.5) + aux->point;
}else{
middle = ((aux->next->point - aux->point) / 3.0) + aux->point;
}
}else if((aux->pertinence > 0.0 && aux->next->pertinence > 0.0) && (aux->pertinence == aux->next->pertinence)){
// Se quadrado
area = (aux->next->point - aux->point) * aux->pertinence;
middle = ((aux->next->point - aux->point) / 2.0) + aux->point;
}else if((aux->pertinence > 0.0 && aux->next->pertinence > 0.0) && (aux->pertinence != aux->next->pertinence)){
// Se trapezio
area = ((aux->pertinence + aux->next->pertinence) / 2.0) * (aux->next->point - aux->point);
middle = ((aux->next->point - aux->point) / 2.0) + aux->point;
}
numerator += middle * area;
denominator += area;
}
aux = aux->next;
}
if(denominator == 0.0){
return 0.0;
}else{
return numerator / denominator;
}
}
bool FuzzyComposition::empty(){
// limpando a memória
this->cleanPoints(this->points);
// resetando os ponteiros
this->points = NULL;
this->pointsCursor = NULL;
return true;
}
// MÉTODOS PRIVADOS
void FuzzyComposition::cleanPoints(pointsArray* aux){
if(aux != NULL){
// Esvaziando a memória alocada
this->cleanPoints(aux->next);
free(aux);
}
}
bool FuzzyComposition::rebuild(pointsArray* aSegmentBegin, pointsArray* aSegmentEnd, pointsArray* bSegmentBegin, pointsArray* bSegmentEnd){
float x1 = aSegmentBegin->point;
float y1 = aSegmentBegin->pertinence;
float x2 = aSegmentEnd->point;
float y2 = aSegmentEnd->pertinence;
float x3 = bSegmentBegin->point;
float y3 = bSegmentBegin->pertinence;
float x4 = bSegmentEnd->point;
float y4 = bSegmentEnd->pertinence;
float point, pertinence;
float denom, numera, numerb;
float mua, mub;
denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
numera = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
numerb = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
if(denom < 0.0){
denom *= -1.0;
}
if(numera < 0.0){
numera *= -1.0;
}
if(numerb < 0.0){
numerb *= -1.0;
}
// Se os seguimentos forem paralelos, retornar falso
if(denom < EPS){
return false;
}
// Verificar se há interseção ao longo do seguimento
mua = numera / denom;
mub = numerb / denom;
if(mua < 0.0 || mua > 1.0 || mub < 0.0 || mub > 1.0){
return false;
}else{
// Calculando o ponto e a pertinencia do novo elemento
point = x1 + mua * (x2 - x1);
pertinence = y1 + mua * (y2 - y1);
// Adicionando um novo ponto
pointsArray* aux;
// Alocando espaço na memória
if((aux = (pointsArray *) malloc(sizeof(pointsArray))) == NULL){
return false;
}
aux->previous = bSegmentEnd;
aux->point = point;
aux->pertinence = pertinence;
aux->next = aSegmentEnd;
bSegmentEnd->next = aux;
aSegmentEnd->previous = aux;
float stopPoint = bSegmentBegin->point;
float stopPertinence = bSegmentBegin->pertinence;
pointsArray* temp = aSegmentBegin;
pointsArray* excl;
do{
float pointToCompare = temp->point;
float pertinenceToCompare = temp->pertinence;
excl = temp->previous;
this->rmvPoint(temp);
temp = excl;
if(stopPoint == pointToCompare && stopPertinence == pertinenceToCompare){
break;
}
}while(temp != NULL);
return true;
}
}
bool FuzzyComposition::rmvPoint(pointsArray* point){
if(point != NULL){
free(point);
}
return true;
}