123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- class LRUItem{
- constructor(node){
- this.previous = null;
- this.next = null;
- this.node = node;
- }
- }
- /**
- *
- * @class A doubly-linked-list of the least recently used elements.
- */
- class LRU{
- constructor(){ //类似链表存储
- // the least recently used item
- this.first = null;
- // the most recently used item
- this.last = null;
- // a list of all items in the lru list
- this.items = {}; //按node的id存储。(id为0的就是root,name='r')
- this.elements = 0;
- this.numPoints = 0;
- }
- size(){
- return this.elements;
- }
- contains(node){
- return this.items[node.id] == null;
- }
- touch(node){//链接node,并且永远放在最后. (每次updatePointClouds都要刷新一次链表)
- if (!node.loaded) {
- return;
- }
- let item;
- if (this.items[node.id] == null) {
- // add to list
- item = new LRUItem(node);
- item.previous = this.last;
- this.last = item;
- if (item.previous !== null) {
- item.previous.next = item;
- }
- this.items[node.id] = item;
- this.elements++;
- if (this.first === null) {
- this.first = item;
- }
- this.numPoints += node.numPoints;
- } else {
- // update in list
- item = this.items[node.id];
- if (item.previous === null) {
- // handle touch on first element
- if (item.next !== null) {
- this.first = item.next;
- this.first.previous = null;
- item.previous = this.last;
- item.next = null;
- this.last = item;
- item.previous.next = item;
- }
- } else if (item.next === null) {
- // handle touch on last element
- } else {//从原来的位置挑出放最后
- // handle touch on any other element
- item.previous.next = item.next;
- item.next.previous = item.previous;
- item.previous = this.last;
- item.next = null;
- this.last = item;
- item.previous.next = item;
- }
- }
- }
- //因为需要显示的都放末尾,所以不显示的部分都在前面,删除时从头删除。(但并不代表开头的一定是不显示的,所以如果第一个仍是显示的,它很可能是root,也就是name='r'的nodeGeo,删除它也就会删除全部)
-
- remove(node){
- let lruItem = this.items[node.id];
- if (lruItem) {
- if (this.elements === 1) {
- this.first = null;
- this.last = null;
- } else {
- if (!lruItem.previous) {
- this.first = lruItem.next;
- this.first.previous = null;
- }
- if (!lruItem.next) {
- this.last = lruItem.previous;
- this.last.next = null;
- }
- if (lruItem.previous && lruItem.next) {
- lruItem.previous.next = lruItem.next;
- lruItem.next.previous = lruItem.previous;
- }
- }
- delete this.items[node.id];
- this.elements--;
- this.numPoints -= node.numPoints;
- }
- }
- getLRUItem(){
- if (this.first === null) {
- return null;
- }
- let lru = this.first;
- return lru.node;
- }
- toString(){
- let string = '{ ';
- let curr = this.first;
- while (curr !== null) {
- string += curr.node.id;
- if (curr.next !== null) {
- string += ', ';
- }
- curr = curr.next;
- }
- string += '}';
- string += '(' + this.size() + ')';
- return string;
- }
- freeMemory(){
- if (this.elements <= 1) {
- return;
- }
- while (this.numPoints > Potree.pointLoadLimit) {
- let element = this.first;
- let node = element.node;
- this.disposeDescendants(node);
- }
- }
- disposeDescendants(node){
- let stack = [];
- stack.push(node);
- while (stack.length > 0) {
- let current = stack.pop();
- // console.log(current);
- current.dispose(); //真正删除geometry等
- this.remove(current);
- for (let key in current.children) {
- if (current.children.hasOwnProperty(key)) {
- let child = current.children[key];
- if (child.loaded) {
- stack.push(current.children[key]);
- }
- }
- }
- }
- }
- }
- export {LRU, LRUItem};
|