-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFaceList.cpp
More file actions
362 lines (283 loc) · 8.33 KB
/
Copy pathFaceList.cpp
File metadata and controls
362 lines (283 loc) · 8.33 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "stdafx.h"
#include "NeuralNet.h"
int FaceList::DeleteOld(int Age, bool MarkNeurons){
// the function returns the number of deleted edges
// use deletion age to delete by edge age,
// if MarkNeurons is set then neurons with no edges
// are marked for further processing later
int OldSize=Size;
Face *TempFace=Head;
for (int i=0; i<OldSize ; i++){
if ( TempFace->CreationAge > Age ){
TempFace=DelFace(TempFace,MarkNeurons);
}
else TempFace=TempFace->Next;
}
return (OldSize - Size);
}
FaceList::FaceList(){
DispListNumber=0;
Size=0;
Head=NULL;
}
FaceList::~FaceList(){
// delete the list - notice that the lsit has to be deleted before the neuron list,
// otherwise the delete statement may conclude at an access violation
while (Head!=NULL){
DelFace(Head);
}
#ifdef MY_DEBUG
//debug check
if (Size!=0) MyFatalError("FaceList::~FaceList - inconsistency detected while destructing face list");
#endif
}
int FaceList::Age(Neuron *N = NULL){
// all the faces connected to the Neuron are made older !!
// if no input is spesified, all faces are aged
int FacesAged=0;
Face *TempFace=Head;
if (Head != NULL) do{
if (TempFace->IsFaceOfNeuron(N) || N==NULL){
TempFace->CreationAge++;
FacesAged++;
}
TempFace=TempFace->Next;
} while (TempFace!=Head);
return FacesAged;
}
Face *FaceList::IsFace(Neuron *N1,Neuron *N2,Neuron *N3){
Edge *E12;
Edge *E13;
Edge *E23;
// first convert to edges and if edges don't exist exit with NULL (no face can be defined with no edges )
Edge *TempEdge=N1->EdgeStuckHead;
while (TempEdge != NULL) {
if ( TempEdge->IsEdgeOfNeuron(N2) )
break;
TempEdge=N1->NextEdgeInEdgeStuck(TempEdge);
}
E12=TempEdge;
if (E12==NULL) return NULL;
TempEdge=N1->EdgeStuckHead;
while (TempEdge!=NULL) {
if ( TempEdge->IsEdgeOfNeuron(N3) )
break;
TempEdge=N1->NextEdgeInEdgeStuck(TempEdge);
}
E13=TempEdge;
if (E13==NULL) return NULL;
TempEdge=N2->EdgeStuckHead;
while (TempEdge!=NULL){
if ( TempEdge->IsEdgeOfNeuron(N3) ){
break;
}
TempEdge=N2->NextEdgeInEdgeStuck(TempEdge);
}
E23=TempEdge;
if (E23==NULL) return NULL;
// now look if such a face really exist
return IsFace(E12,E13,E23);
}
Face *FaceList::IsFace(Edge *E1,Edge *E2,Edge *E3){
Face *TempFace=E1->FacesStuckHead;
while (TempFace!=NULL){
if ( TempFace->IsFaceBetweenEdges(E1,E2,E3))
return TempFace;
TempFace=E1->NextFaceInFaceStuck (TempFace);
}
return NULL;
}
Face *FaceList::DelFace(Face *F, bool MarkNeurons){
// this function deltes a Face and frees it's memory
// it returns a pointer to the Next face after the deleted one
// it changes the head if the face is the first face it also changes
// the neuron counters.
// if MarkNeurons is set he neurons are marked for later deletion
Face *TempFace=F->Next;
if (TempFace==F){ // this happens when there is only one edge in the list
#ifdef MY_DEBUG
// this is a debug check
if (Size!=1 || F->Prev!=F) MyFatalError("FaceList::DelFace inconsistency detected in the face list");
#endif
// now reset the Head to NULL and also TempFace to be returned
Head=NULL;
TempFace=NULL;
}
else{
TempFace->Prev=F->Prev;
F->Prev->Next=TempFace;
// if this is the face in the list - modify the head
if (F==Head) Head=TempFace;
}
Neuron *Neuron1,*Neuron2,*Neuron3;
F->GetFaceNeurons(&Neuron1,&Neuron2,&Neuron3);
Neuron1->FaceCounter--;
Neuron2->FaceCounter--;
Neuron3->FaceCounter--;
Edge *E;
for (int i=0;i<3;i++){
bool DebugFaceDeleted = false;
switch (i){
case 0: E=F->Edge1;
break;
case 1: E=F->Edge2;
break;
case 2: E=F->Edge3;
break;
}
Neuron *N2=E->Neuron2;
if (E->FacesStuckHead==F){
E->FacesStuckHead = E->NextFaceInFaceStuck(E->FacesStuckHead);
#ifdef MY_DEBUG
DebugFaceDeleted=true;
#endif
}
else {
Face *TempStuckFace = E->FacesStuckHead;
Face *NextStuckFace = E->NextFaceInFaceStuck(TempStuckFace);
while (NextStuckFace !=NULL){
if (NextStuckFace == F){
if (E==TempStuckFace->Edge1)
TempStuckFace->NextFaceInE1 = E->NextFaceInFaceStuck(NextStuckFace);
else if (E==TempStuckFace->Edge2)
TempStuckFace->NextFaceInE2 = E->NextFaceInFaceStuck(NextStuckFace);
else
TempStuckFace->NextFaceInE3 = E->NextFaceInFaceStuck(NextStuckFace);
#ifdef MY_DEBUG
DebugFaceDeleted =true;
#endif
break;
}
TempStuckFace = NextStuckFace;
NextStuckFace = E->NextFaceInFaceStuck(NextStuckFace);
}
}
#ifdef MY_DEBUG
if (!DebugFaceDeleted)
MyFatalError("Face not deleted from stuck");
#endif
}
F->Edge1->FaceCounter--;
F->Edge2->FaceCounter--;
F->Edge3->FaceCounter--;
// if the MarkNeurons flag is set mark neurons
if (MarkNeurons && F->Edge3->FaceCounter==0) F->Edge3->Tag |= EDGE_FACES_REMOVED;
if (MarkNeurons && F->Edge2->FaceCounter==0) F->Edge2->Tag |= EDGE_FACES_REMOVED;
if (MarkNeurons && F->Edge1->FaceCounter==0) F->Edge1->Tag |= EDGE_FACES_REMOVED;
#ifdef MY_DEBUG
// now free the memory of the face
TRACE ("FaceList::DelFace: Deleteing face between neurons %d, %d, %d \n",F->Edge1->EdgeId,F->Edge2->EdgeId,F->Edge3->EdgeId);
#endif
delete(F);
Size--;
return TempFace;
}
Face *FaceList::DelFace(Edge *E1, Edge *E2 , Edge *E3){
// the function deletes a Face between three edges (if exists)
// it returns a pointer to the member after the deleted face
// it retuns NULL if no faces were deleted
Face *TempFace=Head;
if (Head!=NULL) while (1){
if ( TempFace->IsFaceBetweenEdges(E1,E2,E3)){
// free memory and remove from list
TempFace=DelFace(TempFace);
//we can get out of the loop now - we assume that no two faces are alike!
return TempFace;
}
else
TempFace=TempFace->Next;
// when covered all the faces you can get out of the loop
if (TempFace==Head) return NULL;
}
return NULL;
}
Face *FaceList::DelFace(Neuron *N1, Neuron *N2 , Neuron *N3){
// the function deletes a Face between three neurons (if exists)
// it returns a pointer to the member after the deleted face
// it retuns NULL if no faces were deleted
Face *TempFace=Head;
if (Head!=NULL) while (1){
if ( TempFace->IsFaceBetweenNeurons(N1,N2,N3)){
// free memory and remove from list
TempFace=DelFace(TempFace);
//we can get out of the loop now - we assume that no two faces are alike!
return TempFace;
}
else
TempFace=TempFace->Next;
// when covered all the faces you can get out of the loop
if (TempFace==Head) return NULL;
}
return NULL;
}
Face *FaceList::CreateOrRefreshFace(Edge *E1, Edge *E2, Edge *E3 , int FaceType){
// this function scans for existing faces beween the same Edgess, if found it refreshed the face
// if non are found a new face is created at the tail of the list (older faces will be closer to the head this way)
// the function returns a pointer to the created or refreshed face
Face *TempFace=Head;
if (Head != NULL) do{
if ( TempFace->IsFaceBetweenEdges(E1,E2,E3) ){
// face found refresh it and exit
TempFace->CreationAge = 0;
return TempFace;
}
TempFace=TempFace->Next;
} while (TempFace!=Head);
// if we got to this point it means that you need to create a new face - put it at the back of the list
Face *NewFace = new Face(E1,E2,E3);
NewFace->Tag=FaceType;
if (Head==NULL){ // empty list - fill it
NewFace->Prev=NewFace;
NewFace->Next=NewFace;
Head=NewFace;
}
else{
NewFace->Next=Head;
NewFace->Prev=Head->Prev;
Head->Prev->Next=NewFace;
Head->Prev=NewFace;
}
Size++;
return NewFace;
}
void FaceList::MaskConnectedNeurons(int Mask)
{
Face *TempFace = Head;
if (Head != NULL) do{
Neuron *Neuron1,*Neuron2,*Neuron3;
TempFace->GetFaceNeurons(&Neuron1,&Neuron2,&Neuron3);
Neuron1->Tags &= Mask;
Neuron2->Tags &= Mask;
Neuron3->Tags &= Mask;
TempFace=TempFace->Next;
} while (TempFace!=Head);
}
int FaceList::CountFacesForEdge(Edge *E){
int Counter=0;
Face *TempFace = Head;
if (Head != NULL) do{
if (TempFace->IsFaceOfEdge(E))
Counter++;
TempFace=TempFace->Next;
} while (TempFace!=Head);
return Counter;
}
/* old stuff not used any more
Face **FaceList::FindFacesByEdge( Face **ListOfFaces, Edge *E){
Face **ReturnList;
if (ListOfFaces==NULL)
ReturnList= new (Face (*[E->FaceCounter]));
else
ReturnList=ListOfFaces;
Face *F=Head;
for (int i=0; i<E->FaceCounter;){
if (F->IsFaceOfEdge (E))
ReturnList[i++]=F;
F=F->Next;
if (F==Head)
break;
}
ASSERT(i==E->FaceCounter);
return ReturnList;
}
*/