File: OctTree.cpp

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (489 lines) | stat: -rw-r--r-- 11,040 bytes parent folder | download
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include "OctTree.h"

using namespace tlp;


/**
 * Creates an OctTree containing exactly one graph node.
 *
 * @param node     graph node
 * @param position position of the graph node
 * @param minPos   minimum coordinates of the cuboid
 * @param maxPos   maximum coordinates of the cuboid
 */
OctTree::OctTree (tlp::node _node,
                  Coord _position,
                  Coord _minPos,
                  Coord _maxPos,
                  tlp::DoubleProperty* _linLogWeight,
                  bool _firstNode)
  : position (_position[0],_position[1],_position[2]),
    minPos (_minPos[0],_minPos[1],_minPos[2]),
    maxPos (_maxPos[0],_maxPos[1],_maxPos[2]) {

  firstNode = _firstNode;
  linLogWeight = _linLogWeight;
  MAX_DEPTH = 8;
  MAX_CHILDREN = 8;
  //node = NULL;
  //if(_node != NULL)
  node = _node;
  isLeaf = true;

  if (firstNode) {
    isLeaf = false;
  }

  //childrenSize = 0;
  childCount = 0;
  _children = NULL; //new OctTree*[8]; //init à NULL?
  this->weight = 0.0;

  /*
    for(int i=0;i<8;++i)
    {
    _children[i] = NULL;
    }
  */
  // if (_node != NULL)
  // {
  if(!firstNode)
    this->weight = linLogWeight->getNodeValue(_node);

  // }else{
  //   std::cerr<<"assert: creating a an octtree with a null node\n";
  // }
}

/**
 * Deletes the an OctTree.
 */
OctTree::~OctTree () {
  //std::cerr<<"deletion of an octtree\n";
  if (_children!=NULL) {

    for (unsigned int i=0; i<MAX_CHILDREN; ++i) {
      if(_children[i]!=NULL)
        delete _children[i];

      _children[i] = NULL;
    }

    delete[] _children;
    _children = NULL;
  }
}

/**
 *  Sets the maximum number of children in the OctTree.
 *
 *  @param max maximum nimber of children
 */

void
OctTree::setMaxChildren (unsigned int max) {
  MAX_CHILDREN = max;
}

/**
 * Adds a graph node to the octtree.
 *
 * @param newNode  graph node
 * @param newPos   position of the graph node
 * @param depth    depth of this tree node in the octtree
 */
void
OctTree::addNode (tlp::node newNode,
                  Coord newPos,
                  unsigned int depth) {
  if (depth > MAX_DEPTH-1) {
    std::cerr<<"assert: adding a node at a depth deeper than the max depth (add1)\n";
    return;
  }

  /*
  if(newNode == NULL)
  {
  std::cerr<<"assert: adding a null node\n";
  return;
  }
  */

  double nnWeight = linLogWeight->getNodeValue(newNode);

  if (nnWeight == 0.0) {
    //std::cerr<<"assert: new node has a null weight, cannot add\n";
    return;
  }

  //on passe de feuille a branche
  //if (node != NULL)
  //{
  if (isLeaf) {
    addNode2 (node, position, depth); //!depth
    isLeaf = false;
  }

  //  node = NULL;
  //}

  for (int d = 0; d < 3; ++d) {
    position[d] = (weight*position[d] + nnWeight * newPos[d])
                  / (weight+nnWeight);
  }

  weight += nnWeight;

  addNode2 (newNode, newPos, depth); //!depth
}

/**
 * Returns the current node of this OctTree
 */

tlp::node
OctTree::getNode() {
  return node;
}

/**
 * Adds a graph node to the OctTree,
 * without changing the position and weight of the root.
 *
 * @param newNode  graph node
 * @param newPos   position of the graph node
 * @param depth    depth of this tree node in the octtree
 */
void
OctTree::addNode2 (tlp::node newNode,
                   Coord newPos,
                   unsigned int depth) {
  if (depth > MAX_DEPTH-1) {
    std::cerr<<"assert: adding a node at a depth deeper than the max depth! (add2)\n";
    return;
  }

  if (depth == MAX_DEPTH-1) {
    if (childCount == MAX_CHILDREN) {
      OctTree** _oldChildren = _children;
      _children = new OctTree*[2*MAX_CHILDREN];

      for (unsigned int i=0; i<MAX_CHILDREN; ++i) {
        _children[i] = _oldChildren[i];
      }

      for (unsigned int i=MAX_CHILDREN; i<MAX_CHILDREN*2; ++i) {
        _children[i] = NULL;
      }

      MAX_CHILDREN *= 2;
    }

    if (childCount == 0 || _children == NULL) {
      _children = new OctTree*[MAX_CHILDREN];

      for (unsigned int i = 0; i<MAX_CHILDREN; ++i)
        _children[i] = NULL;
    }

    _children[childCount++] = new OctTree(newNode, newPos, newPos, newPos,linLogWeight, false);

    return;
  }

  //on localise le noeud
  int childIndex = 0;

  for (int d = 0; d < 3; ++d) {
    if (newPos[d] > (minPos[d]+maxPos[d])/2) {
      childIndex += 1 << d;
    }
  }

  if (childCount == 0 || _children == NULL) {
    _children = new OctTree*[MAX_CHILDREN];

    for (unsigned int i = 0; i<MAX_CHILDREN; ++i)
      _children[i] = NULL;
  }

  //si la place est vide
  if (_children[childIndex] == NULL) {

    Coord newMinPos;
    Coord newMaxPos;

    for (int d = 0; d < 3; ++d) {
      if ((childIndex & 1<<d) == 0) {
        newMinPos[d] = minPos[d];
        newMaxPos[d] = (minPos[d] + maxPos[d]) / 2;
      }
      else {
        newMinPos[d] = (minPos[d] + maxPos[d]) / 2;
        newMaxPos[d] = maxPos[d];
      }
    }

    childCount++;
    _children[childIndex] = new OctTree(newNode, newPos, newMinPos, newMaxPos, linLogWeight, false);
  }
  else {
    _children[childIndex]->addNode(newNode, newPos, depth+1);
  }
}

/**
 * Prints the OctTree on a console output at the desired depth
 *
 * @param depth the desired depth
 */

void OctTree::printTree (unsigned int depth) {
  std::cerr<<"\n";

  for (unsigned int i=0; i<depth; ++i) {
    std::cerr<<"\t";;
  }

  std::cerr<<"[d("<<depth<<"),w("<<weight<<"),n("<<node.id<<"),l("<<isLeaf<<"),p("<<position[0]<<","<<position[1]<<","<<position[2]<<"),";

  if(_children!=NULL) {
    for (unsigned int i=0; i<MAX_CHILDREN; ++i) {
      if(_children[i] ==NULL) {
        std::cerr<<"X,";
      }
      else {
        std::cerr<<"O,";
      }
    }

    for (unsigned int i=0; i<MAX_CHILDREN; ++i)
      if(_children[i] !=NULL)
        if (depth < MAX_DEPTH)
          _children[i]->printTree(depth+1);

  }

  std::cerr<<"]\n";

}

/**
 * Removes a graph node from the octtree.
 *
 * @param oldNode  graph node
 * @param oldPos   position of the graph node
 * @param depth    current depth
 */
void OctTree::removeNode (tlp::node oldNode,
                          Coord oldPos,
                          unsigned int depth) {
  //std::cerr<<"depth: "<<depth<<"\n";

  /*
    if(_children!=NULL)
    {
    std::cerr<<"[";

    for (int i=0; i<MAX_CHILDREN; ++i)
    {
    if(_children[i] ==NULL)
    {
    std::cerr<<" NULL |";
    }
    else
    std::cerr<<" "<<_children[i]<<" |";
    }
    std::cerr<<"\n";
    }*/


  if (depth > MAX_DEPTH-1) {
    std::cerr<<"assert: remove a node at a depth deeper than the max depth: "<<depth<<" / "<<MAX_DEPTH-1<<"\n";
    return;
  }

  double onWeight = linLogWeight->getNodeValue(oldNode);

  if (onWeight == 0.0) {
    //std::cerr<<"assert: removing a node with a weight of 0\n";
    return;
  }

  if (weight <= onWeight) {
    weight = 0.0;
    //    node = NULL;

    for (unsigned int i = 0; i < childCount; ++i) {
      delete _children[i];
      _children[i] = NULL;
    }

    delete[] _children;
    _children = NULL;

    childCount = 0;
    return;
  }

  for (int d = 0; d < 3; ++d) {
    position[d] = (weight * position[d] - onWeight * oldPos[d])
                  / (weight - onWeight);
  }

  weight -= onWeight;


  if (depth == MAX_DEPTH-1) {
    if(childCount>0) {
      //std::cerr<<"childCount: "<<childCount<<"\n";
      unsigned int childIndex = 0;

      /*
      if((_children[childIndex])->node == oldNode)
      {
      std::cerr<<"Identical nodes :) before while\n";
      }
      */

      bool endwhile = false;

      while (endwhile == false) {
        if(childIndex < MAX_CHILDREN) {
          if (_children[childIndex] == NULL) {
            std::cerr<<"this part of the tree is null\n";
            childIndex++;
          }
          /*          else if ((_children[childIndex])->node == NULL || oldNode == NULL)
                {
                //std::cerr<<"this tree node or the searched one is NULL: "<<oldNode<<" and "<<(_children[childIndex])->node<<"\n";
                childIndex++;
                }
          */
          else if((_children[childIndex])->node.id != oldNode.id) {
            //std::cerr<<"this tree node is different from the searched one: "<<oldNode.id<<" and "<<(_children[childIndex])->node.id<<"\n";
            childIndex++;
          }
          else {
            //std::cerr<<"node found :)\n";
            endwhile = true;
          }

        }
        else {
          std::cerr<<"we're stopping at the end of the table: "<<childIndex<<"\n";
          endwhile = true;
        }
      }

      if (childIndex == MAX_CHILDREN) {
        std::cerr<<"assert: removing a non existant node in the tree\n";
        return;
      }

      /*
      if((_children[childIndex])->node == oldNode)
      {
      //  std::cerr<<"Identical nodes :)\n";
      }
      */

      delete _children[childIndex];
      _children[childIndex] = NULL;

      for (unsigned int i = childIndex; i < childCount-1; ++i) {
        _children[i] = _children[i+1];
      }

      //delete _children[childCount];
      _children[childCount-1] = NULL; //delete

      --childCount;

    }
    else {
      std::cerr<<"assert ChildCount <= 0: "<< childCount<< "\n";
    }

  }
  else {
    int childIndex = 0;

    //on localise le noeud
    for (int d = 0; d < 3; ++d) {
      if (oldPos[d] > (minPos[d]+maxPos[d])/2) {
        childIndex += 1 << d;
      }
    }

    if(_children[childIndex]!=NULL) {

      _children[childIndex]->removeNode(oldNode, oldPos, depth+1);

      if (_children[childIndex]->weight == 0.0) {
        delete _children[childIndex];
        _children[childIndex] = NULL;
        --childCount;
      }
    }
    else {
      std::cerr<<"assert: the selected child it is not supposed to be NULL!\n";
      return;
    }
  }
}

/**
 * Returns the maximum extension of the octtree.
 *
 * @return maximum over all dimensions of the extension of the octtree
 */
double OctTree::width () {
  double width = 0.0;

  for (int d = 0; d < 3; ++d) {
    if (maxPos[d] - minPos[d] > width) {
      width = maxPos[d] - minPos[d];
    }
  }

  return width;
}

/**
 * Returns the height of the octtree.
 *
 * @return height of the octtree
 */
int
OctTree::getHeight () {
  int height = -1;

  for (unsigned int i=0; i<childCount; ++i) {
    OctTree* aChild = _children[i];

    if (aChild != NULL) {
      height = std::max(height, aChild->getHeight());
    }
  }

  return height+1;
}