forked from alvesoaj/eFLL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuzzyComposition.cpp
executable file
·333 lines (321 loc) · 10.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Robotic Research Group (RRG)
* State University of Piauí (UESPI), Brazil - Piauí - Teresina
*
* FuzzyComposition.cpp
*
* Author: AJ Alves <[email protected]>
* Co authors: Dr. Ricardo Lira <[email protected]>
* Msc. Marvin Lemos <[email protected]>
* Douglas S. Kridi <[email protected]>
* Kannya Leal <[email protected]>
*/
#include "FuzzyComposition.h"
#include <math.h>
// CONTRUCTORS
FuzzyComposition::FuzzyComposition()
{
this->points = NULL;
}
// DESTRUCTOR
FuzzyComposition::~FuzzyComposition()
{
this->cleanPoints(this->points);
}
// Method to include a new pointsArray struct into FuzzyComposition
bool FuzzyComposition::addPoint(float point, float pertinence)
{
// auxiliary variable to handle the operation
pointsArray *newOne;
// allocating in memory
if ((newOne = (pointsArray *)malloc(sizeof(pointsArray))) == NULL)
{
// return false if in out of memory
return false;
}
// populate the struct
newOne->previous = NULL;
newOne->point = point;
newOne->pertinence = pertinence;
newOne->next = NULL;
// if it is the first pointsArray, set it as the head
if (this->points == NULL)
{
this->points = newOne;
}
else
{
// auxiliary variable to handle the operation
pointsArray *aux = this->points;
// find the last element of the array
while (aux != NULL)
{
if (aux->next == NULL)
{
// make the relations between them
newOne->previous = aux;
aux->next = newOne;
return true;
}
aux = aux->next;
}
}
return true;
}
// Method to check if FuzzyComposition contains an specific point and pertinence
bool FuzzyComposition::checkPoint(float point, float pertinence)
{
// auxiliary variable to handle the operation
pointsArray *aux = this->points;
// while not in the end of the array, iterate
while (aux != NULL)
{
// if params match with this point
if (aux->point == point && aux->pertinence == pertinence)
{
return true;
}
aux = aux->next;
}
return false;
}
// Method to iterate over the pointsArray, detect possible intersections and sent these points for "correction"
bool FuzzyComposition::build()
{
// auxiliary variable to handle the operation, instantiate with the first element from array
pointsArray *aux = this->points;
// while not in the end of the array, iterate
while (aux != NULL)
{
// another auxiliary variable to handle the operation
pointsArray *temp = aux;
// while not in the beginning of the array, iterate
while (temp->previous != NULL)
{
// if the previous point is greater then the current
if (aux->point < temp->point)
{
// if yes, break an use this point
break;
}
temp = temp->previous;
}
// iterate over the previous pointsArray
while (temp->previous != NULL)
{
// if previous of previous point is not NULL, and some intersection was fixed by rebuild
if (this->rebuild(aux, aux->next, temp, temp->previous) == true)
{
// move the first auxiliary to beginning of the array for a new validation, and breaks
aux = this->points;
break;
}
temp = temp->previous;
}
aux = aux->next;
}
return true;
}
// Method to calculate the center of the area of this FuzzyComposition
float FuzzyComposition::calculate()
{
// auxiliary variable to handle the operation, instantiate with the first element from array
pointsArray *aux = this->points;
float numerator = 0.0;
float denominator = 0.0;
// while not in the end of the array, iterate
while (aux != NULL && aux->next != NULL)
{
float area = 0.0;
float middle = 0.0;
// if it is a singleton
if (aux->pertinence != aux->next->pertinence && aux->point == aux->next->point)
{
// enter in all points of singleton, but calculate only once
if (aux->pertinence > 0.0)
{
area = aux->pertinence;
middle = aux->point;
}
}
// if a triangle (Not properly a membership function)
else if (aux->pertinence == 0.0 || aux->next->pertinence == 0.0)
{
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 a square (Not properly a membership function)
else if ((aux->pertinence > 0.0 && aux->next->pertinence > 0.0) && aux->pertinence == aux->next->pertinence)
{
area = (aux->next->point - aux->point) * aux->pertinence;
middle = ((aux->next->point - aux->point) / 2.0) + aux->point;
}
// else if a trapeze (Not properly a membership function)
else if ((aux->pertinence > 0.0 && aux->next->pertinence > 0.0) && aux->pertinence != aux->next->pertinence)
{
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;
}
// avoiding zero division
if (denominator == 0.0)
{
return 0.0;
}
else
{
return numerator / denominator;
}
}
// Method to reset the Object
bool FuzzyComposition::empty()
{
// clean all pointsArray from memory
this->cleanPoints(this->points);
// reset the pointer
this->points = NULL;
return true;
}
// Method to count the amount of points used in this FuzzyComposition
int FuzzyComposition::countPoints()
{
// variable to hold the count
int count = 0;
// auxiliary variable to handle the operation
pointsArray *aux = this->points;
// while not in the end of the array, iterate
while (aux != NULL)
{
count = count + 1;
aux = aux->next;
}
return count;
}
// PRIVATE METHODS
// Method to recursively clean all pointsArray structs from memory
void FuzzyComposition::cleanPoints(pointsArray *aux)
{
if (aux != NULL)
{
this->cleanPoints(aux->next);
// emptying allocated memory
free(aux);
}
}
// Method to search intersection between two segments, if found, create a new pointsArray (in found intersection) and remove not necessary ones
bool FuzzyComposition::rebuild(pointsArray *aSegmentBegin, pointsArray *aSegmentEnd, pointsArray *bSegmentBegin, pointsArray *bSegmentEnd)
{
// create a reference for each point
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;
// calculate the denominator and numerator
float denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
float numera = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
float numerb = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
// if negative, convert to positive
if (denom < 0.0)
{
denom *= -1.0;
}
// If the denominator is zero or close to it, it means that the lines are parallels, so return false for intersection
if (denom < EPSILON_VALUE)
{
// return false for intersection
return false;
}
// if negative, convert to positive
if (numera < 0.0)
{
numera *= -1.0;
}
// if negative, convert to positive
if (numerb < 0.0)
{
numerb *= -1.0;
}
// verify if has intersection between the segments
float mua = numera / denom;
float mub = numerb / denom;
if (mua <= 0.0 || mua >= 1.0 || mub <= 0.0 || mub >= 1.0)
{
// return false for intersection
return false;
}
else
{
// we found an intersection
// auxiliary variable to handle the operation
pointsArray *aux;
// allocating in memory
if ((aux = (pointsArray *)malloc(sizeof(pointsArray))) == NULL)
{
// return false if in out of memory
return false;
}
// calculate the point (y) and its pertinence (y) for the new element (pointsArray)
aux->previous = bSegmentEnd;
aux->point = x1 + mua * (x2 - x1);
aux->pertinence = y1 + mua * (y2 - y1);
aux->next = aSegmentEnd;
// changing pointsArray to accomplish with new state
aSegmentBegin->next = aux;
aSegmentEnd->previous = aux;
bSegmentBegin->previous = aux;
bSegmentEnd->next = aux;
// initiate a proccess of remotion of not needed pointsArray
// some variables to help in this proccess, the start pointsArray
pointsArray *temp = bSegmentBegin;
// do, while
do
{
// hold next
pointsArray *excl = temp->next;
// remove it from array
this->rmvPoint(temp);
// set new current
temp = excl;
// check if it is the stop pointsArray
if (temp != NULL && temp->point == aux->point && temp->pertinence == aux->pertinence)
{
// if true, stop the deletions
break;
}
} while (temp != NULL);
return true;
}
}
// Function to remove (deallocate) some pointsArray
bool FuzzyComposition::rmvPoint(pointsArray *point)
{
if (point != NULL)
{
// emptying allocated memory
free(point);
}
return true;
}