GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
Tree.hh
Go to the documentation of this file.
1//
2// Copyright (C) 2009 Tero Pulkkinen
3// Released under LGPL License.
4//
5// This file is part of Polygon.
6//
7// Polygon is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Polygon is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU Library General Public License
18// along with Polygon. If not, see <http://www.gnu.org/licenses/>.
19//
20
21
22#ifndef TREE_HH
23#define TREE_HH
24
25#include "Effect.hh"
26#include "Event.hh"
27#include <assert.h>
28#include "VolumeObjects.hh"
29#include <typeinfo>
30#include <vector>
31
32#if 0
33
34template<class T>
35class Tree
36{
37public:
38 virtual int NumberOfLevels() const=0;
39 virtual const Array<int, T> *TreeChilds(const Array<int, int> &path) const=0;
40};
41
42class TimeChange
43{
44public:
45 virtual float Time(float time) const = 0;
46};
47class EmptyTimeChange : public TimeChange
48{
49public:
50 virtual float Time(float time) const { return time; }
51};
52
53struct TreeNode
54{
55 mutable Matrix matrix;
56 const BoxableFaceCollection *faces;
57 bool quads; // false = triangles, true = quads
58 mutable ShaderParameters *params;
59 TimeChange *timechange;
60 friend std::ostream &operator<<(std::ostream &o, const TreeNode &tn);
61};
62TreeNode matrix_treenode(Matrix m);
63TreeNode faces_treenode(Matrix m, const BoxableFaceCollection *faces);
64
65struct TreeNodeInternal
66{
67 mutable Matrix matrix;
68 BoxableFaceCollection *faces;
69 bool quads;
70 ShaderParameters *params;
71 Matrix current_level_matrix;
72 friend std::ostream &operator<<(std::ostream &o, const TreeNodeInternal &tn);
73 friend bool operator==(const TreeNodeInternal &n1, const TreeNodeInternal &n2);
74};
75class ModifyTree
76{
77public:
78 virtual void Modify(float time)=0;
79};
80
81class TreeNodeCollection : public Array<int, TreeNode>, public ModifyTree
82{
83public:
84};
85
86
87class EventNodes : public TreeNodeCollection
88{
89public:
90};
91class MatrixFacesNodes : public TreeNodeCollection
92{
93public:
94 MatrixFacesNodes(BoxCollection &coll, BoxableFaceCollection &faces) : coll(coll), faces(faces) { }
95 int Size() const { return coll.Size(); }
96 TreeNode Index(int i) const
97 {
98 TreeNode n = faces_treenode(coll.Index(i), &faces);
99 return n;
100 }
101 void Modify(float time)
102 {
103 }
104private:
105 BoxCollection &coll;
106 BoxableFaceCollection &faces;
107};
108
109class MatrixNodes : public TreeNodeCollection
110{
111public:
112 MatrixNodes(BoxCollection &coll) : coll(coll) { }
113 int Size() const { return coll.Size(); }
114 TreeNode Index(int i) const
115 {
116 TreeNode n = matrix_treenode(coll.Index(i));
117 return n;
118 }
119 void Modify(float time)
120 {
121 }
122private:
123 BoxCollection &coll;
124};
125class TimedMatrixNodes : public TreeNodeCollection
126{
127public:
128 TimedMatrixNodes(TimedMatrixPaths &paths) : paths(paths) { }
129 int Size() const { return paths.Size(); }
130 TreeNode Index(int i) const
131 {
132 TreeNode n = matrix_treenode(paths.Index(t, i));
133 return n;
134 }
135 void Modify(float time)
136 {
137 t = time;
138 }
139
140private:
141 mutable float t;
142 TimedMatrixPaths &paths;
143};
144
145class EmptyTreeNodeCollection : public TreeNodeCollection
146{
147public:
148 int Size() const { return 0; }
149 TreeNode Index(int a) const { TreeNode n; n.faces = 0; return n; }
150 void Modify(float time) { }
151};
152
153class MatrixTreeNodeCollection : public TreeNodeCollection
154{
155public:
156 MatrixTreeNodeCollection(TreeNodeCollection &coll) : coll(coll) { }
157 int Size() const { return coll.Size(); }
158 TreeNode Index(int i) const
159 {
160 TreeNode n = matrix_treenode(coll.Index(i).matrix);
161 return n;
162 }
163 void Modify(float time)
164 {
165 coll.Modify(time);
166 }
167private:
168 TreeNodeCollection &coll;
169};
170
171class FacesTreeNodeCollection : public TreeNodeCollection
172{
173public:
174 FacesTreeNodeCollection(TreeNodeCollection &coll) : coll(coll) { }
175 int Size() const { return coll.Size(); }
176 TreeNode Index(int i) const
177 {
178 TreeNode n = faces_treenode(Matrix::Identity(), coll.Index(i).faces);
179 return n;
180 }
181 void Modify(float time)
182 {
183 coll.Modify(time);
184 }
185private:
186 TreeNodeCollection &coll;
187};
188
189class SingletonTreeNodeCollection : public TreeNodeCollection
190{
191public: // not very useful class, only for dynamic treenode generation
192 SingletonTreeNodeCollection(TreeNode n) : n(n) { }
193 void Modify(float time) { }
194 int Size() const { return 1; }
195 TreeNode Index(int a) const
196 {
197 return n;
198 }
199
200private:
201 TreeNode n;
202};
203
204class RingTreeNodes : public TreeNodeCollection
205{
206public:
207 RingTreeNodes()
208 : pvarray(array, array+4),
209 pointvector(pvarray),
210 ring(pointvector, 100.0, 50),
211 ring1(pointvector, 150.0,50),
212 ring2(pointvector, 200.0,50),
213 ring3(pointvector, 250.0,50)
214 {
215 }
216 int Size() const { return 4; }
217 TreeNode Index(int i) const
218 {
219 TreeNode n;
220 switch(i)
221 {
222 case 0: n = faces_treenode(m,&ring); break;
223 case 1: n = faces_treenode(m1,&ring1); break;
224 case 2: n = faces_treenode(m2,&ring2); break;
225 case 3: n = faces_treenode(m3,&ring3); break;
226 default: std::cout << "RingTreeNode: ERROR! " << i << std::endl; break;
227 }
228 return n;
229 }
230 void Modify(float time)
231 {
232 m = Matrix::XRotation(time/100.0);
233 m1 = Matrix::ZRotation(time/75.0);
234 m2 = Matrix::XRotation(time/50.0);
235 m3 = Matrix::ZRotation(time/25.0);
236 }
237private:
238 Matrix m;
239 Matrix m1;
240 Matrix m2;
241 Matrix m3;
242 VectorArray<std::pair<Point,Vector> > pvarray;
243 PointVectorCollectionConvert pointvector;
244 RingElem ring;
245 RingElem ring1;
246 RingElem ring2;
247 RingElem ring3;
248 static std::pair<Point,Vector> array[];
249};
250
251class ObjectTree
252{
253public:
254 virtual int NumberOfLevels() const=0;
255 virtual const TreeNodeCollection *TreeChilds(const Array<int, int> &path) const=0;
256};
257
258class ArrayObjectTreeStatic : public ObjectTree
259{
260public:
261 ArrayObjectTreeStatic(TreeNodeCollection &coll) : coll(coll) { }
262 int NumberOfLevels() const { return 1; }
263 const TreeNodeCollection *TreeChilds(const Array<int, int> &path) const
264 {
265 if (path.Size()==0)
266 {
267 return &coll;
268 }
269 return &empty;
270 }
271private:
272 TreeNodeCollection &coll;
273 EmptyTreeNodeCollection empty;
274};
275
276class ArrayObjectTreeDynamic : public ObjectTree
277{
278public:
279 ArrayObjectTreeDynamic(TreeNodeCollection &coll) : /*coll(coll),*/ matrix(coll), faces(coll), singleton(0) { }
280 int NumberOfLevels() const { return 2; }
281 const TreeNodeCollection *TreeChilds(const Array<int, int> &path) const
282 {
283 int size = path.Size();
284 if (size==0)
285 {
286 return &matrix;
287 }
288 if (size==1)
289 {
290 delete singleton;
291 int index = path.Index(0);
292 TreeNode n = faces.Index(index);
293 singleton = new SingletonTreeNodeCollection(n);
294 return singleton;
295 }
296
297 return &empty;
298 }
299 ~ArrayObjectTreeDynamic() { delete singleton; }
300private:
301 //TreeNodeCollection &coll;
302 EmptyTreeNodeCollection empty;
303 MatrixTreeNodeCollection matrix;
304 FacesTreeNodeCollection faces;
305 mutable SingletonTreeNodeCollection *singleton;
306};
307
308
309class ObjectTreeForest : public Array<int, ObjectTree*>
310{
311public:
312};
313typedef ArrayConvert<ObjectTreeForest, int, ObjectTree*> ObjectTreeForestConvert;
314
315
316class ForestToTree : public ObjectTree
317{
318public:
319 // Forest and nodes array sizes must match
320 ForestToTree(ObjectTreeForest &forest, TreeNodeCollection &nodes) : forest(forest), nodes(nodes) { }
321 virtual int NumberOfLevels() const
322 {
323 int size = forest.Size();
324 int count =0;
325 for(int i=0;i<size;i++)
326 {
327 count = std::max(count, forest.Index(i)->NumberOfLevels());
328 }
329 return count;
330 }
331 virtual const TreeNodeCollection *TreeChilds(const Array<int, int> &path) const
332 {
333 int size = path.Size();
334 if (size==0)
335 {
336 return &nodes;
337 }
338 int index = path.Index(0);
339 ObjectTree *tree = forest.Index(index);
340 SubArray<int,int> path2(path, 1, path.Size()-1);
341 return tree->TreeChilds(path2);
342 }
343private:
344 ObjectTreeForest &forest;
345 TreeNodeCollection &nodes;
346};
347
348class AppendArray : public Array<int,int>
349{
350public:
351 AppendArray(const Array<int,int> &arr, int i) : arr(arr), i(i) { }
352 int Size() const { return arr.Size()+1; }
353 int Index(int ii) const
354 {
355 if (ii==0) return i;
356 return arr.Index(ii-1);
357 }
358private:
359 const Array<int,int> &arr;
360 int i;
361};
362
363class ObjectSubTree : public ObjectTree
364{
365public:
366 ObjectSubTree(ObjectTree &tree, int index) : tree(tree), index(index) { }
367 int NumberOfLevels() const { return tree.NumberOfLevels()-1; }
368 const TreeNodeCollection *TreeChilds(const Array<int, int> &path) const
369 {
370 AppendArray arr(path, index);
371 return tree.TreeChilds(arr);
372 }
373private:
374 ObjectTree &tree;
375 int index;
376};
377
378class TreeSize
379{
380public:
381 virtual int SubTreeSize(VectorArray<int> &path) const=0;
382};
383
384class TreeSizeImpl : public TreeSize
385{
386public:
387 TreeSizeImpl(ObjectTree &tree): tree(tree) { }
388 int SubTreeSize(VectorArray<int> &path) const
389 {
390 const TreeNodeCollection *coll = tree.TreeChilds(path);
391 int count = 0;
392 int num = coll->Size();
393 count += num;
394
395 for(int i=0;i<num;i++)
396 {
397 path.push_back(i);
398 count += SubTreeSize(path);
399 path.pop_back();
400 }
401 return count;
402 }
403private:
404 ObjectTree &tree;
405};
406
407class ObjectRanges
408{
409public:
410 virtual int NumObjects() const=0;
411 virtual int StartRange(int obj) const=0;
412 virtual int EndRange(int obj) const=0;
413 virtual Matrix Transform(int obj) const =0;
414};
415template<class T>
416class RangeFaceCollectionArray : public Array<int, T>
417{
418public:
419 RangeFaceCollectionArray(ObjectRanges &ranges, Array<int,T> &arr, int obj) : ranges(ranges), arr(arr), obj(obj) { }
420 int Size() const { return ranges.EndRange(obj)-ranges.StartRange(obj); }
421 T Index(int i) const
422 {
423 int startindex = ranges.StartRange(obj);
424 return arr.Index(startindex+i);
425 }
426private:
427 ObjectRanges &ranges;
428 Array<int, T> &arr;
429 int obj;
430};
431class SelectFaceCollection : public Array<int,std::pair<const FaceCollection*, bool> >
432{
433public:
434 SelectFaceCollection(Array<int, TreeNodeInternal> &arr) : arr(arr) { }
435 int Size() const { return arr.Size(); }
436 std::pair<const FaceCollection *, bool> Index(int i) const
437 {
438 TreeNodeInternal n = arr.Index(i);
439 return std::make_pair(n.faces, n.quads);
440 }
441private:
442 Array<int, TreeNodeInternal> &arr;
443};
444
445class ApplyMatrix : public Array<int, TreeNodeInternal>
446{
447public:
448 ApplyMatrix(Array<int,TreeNodeInternal> &arr) : arr(arr), elem(0) { }
449 int Size() const { return arr.Size(); }
450 TreeNodeInternal Index(int i) const
451 {
452
453 TreeNodeInternal n = arr.Index(i);
454
455 if (!elem)
456 {
457 elem = new MatrixElem(*n.faces, n.current_level_matrix);
458 }
459 //delete elem;
460 elem->SetMatrix(n.current_level_matrix);
461 elem->SetNext(n.faces);
462
463 //elem = new MatrixElem(*n.faces, n.current_level_matrix);
464 n.faces = elem;
465 return n;
466 }
467 ~ApplyMatrix() { delete elem; }
468private:
469 Array<int,TreeNodeInternal> &arr;
470 mutable MatrixElem *elem;
471};
472
473class QuadTriChooser : public Array<int, FaceCollectionTri*>
474{
475public:
476 QuadTriChooser(Array<int, std::pair<const FaceCollection*,bool> > &arr) : arr(arr), coll(0) { }
477 int Size() const { return arr.Size(); }
478 FaceCollectionTri *Index(int i) const
479 {
480 delete coll;
481 std::pair<const FaceCollection*, bool> p = arr.Index(i);
482 if (p.second)
483 { // quads
484 coll = new QuadsToTris(*p.first);
485 }
486 else
487 {
488 coll = new TrisToTris(*p.first);
489 }
490 return coll;
491 }
492 ~QuadTriChooser() { delete coll; }
493private:
494 Array<int, std::pair<const FaceCollection*, bool> > &arr;
495 mutable FaceCollectionTri *coll;
496};
497
498class CompressFaceCollectionArrays : public Array<int, FaceCollectionTri*>
499{
500public:
501 CompressFaceCollectionArrays(ObjectRanges &ranges, Array<int, TreeNodeInternal> &arr) : ranges(ranges), arr(arr), range_arr(0), apply_matrix(0), select(0), chooser(0), /*iter(0),*/ oa(0)
502 { }
503 int Size() const
504 {
505 return ranges.NumObjects();
506 }
507 FaceCollectionTri *Index(int i) const
508 {
509 delete oa;
510 //delete iter;
511 delete chooser;
512 delete select;
513 delete apply_matrix;
514 delete range_arr;
515
516 range_arr = new RangeFaceCollectionArray<TreeNodeInternal>(ranges, arr, i);
517 apply_matrix = new ApplyMatrix(*range_arr);
518 select = new SelectFaceCollection(*apply_matrix);
519 chooser = new QuadTriChooser(*select);
520 //comp = new CompressObjectTri(*chooser);
521 //iter = new IteratorArray<int, FaceCollectionTri*>(*chooser);
522 oa = new ObjectArray2(*chooser);
523 return oa;
524 }
525 ~CompressFaceCollectionArrays()
526 {
527 delete oa;
528 //delete iter;
529 delete chooser;
530 delete select;
531 delete apply_matrix;
532 delete range_arr;
533 }
534private:
535 ObjectRanges &ranges;
536 Array<int, TreeNodeInternal> &arr;
537 mutable RangeFaceCollectionArray<TreeNodeInternal> *range_arr;
538 mutable ApplyMatrix *apply_matrix;
539 mutable SelectFaceCollection *select;
540 mutable QuadTriChooser *chooser;
541 //CompressObjectTri *comp;
542 //mutable IteratorArray<int, FaceCollectionTri*> *iter;
543 mutable ObjectArray2 *oa;
544};
545class VBOArrays : public Element<const FaceCollectionVBO*>
546{
547public:
548 VBOArrays(Array<int, FaceCollectionTri*> &arr) : oa(arr), vbo(oa) { }
549 const FaceCollectionVBO *Elem() const
550 {
551 return &vbo;
552 }
553private:
554 //IteratorArray<int, FaceCollectionTri*> iter;
555 ObjectArray2 oa;
556 VBO vbo;
557};
558
559class MemoizeRanges : public ObjectRanges
560{
561public:
562 MemoizeRanges(ObjectRanges &ranges_) : count(ranges_.NumObjects()), ranges(ranges_), start_range(count), start_range_flag(count), end_range(count), end_range_flag(count), transform(count), transform_flag(count)
563 {
564 for(int i=0;i<count;i++)
565 {
566 start_range_flag[i] = 0;
567 end_range_flag[i] = 0;
568 transform_flag[i] = 0;
569 }
570 }
571 virtual int NumObjects() const
572 {
573 return count;
574 }
575 virtual int StartRange(int obj) const
576 {
577 if (start_range_flag[obj]==0)
578 {
579 start_range_flag[obj]=1;
580 start_range[obj]=ranges.StartRange(obj);
581 }
582 return start_range[obj];
583 }
584 virtual int EndRange(int obj) const
585 {
586 if (end_range_flag[obj]==0)
587 {
588 end_range_flag[obj]=1;
589 end_range[obj]=ranges.EndRange(obj);
590 }
591 return end_range[obj];
592
593 }
594 virtual Matrix Transform(int obj) const
595 {
596 if (transform_flag[obj]==0)
597 {
598 transform_flag[obj]=1;
599 transform[obj]=ranges.Transform(obj);
600 }
601 return transform[obj];
602
603 }
604
605private:
606 int count;
607 ObjectRanges &ranges;
608 mutable std::vector<int> start_range;
609 mutable std::vector<char> start_range_flag;
610 mutable std::vector<int> end_range;
611 mutable std::vector<char> end_range_flag;
612 mutable std::vector<Matrix> transform;
613 mutable std::vector<char> transform_flag;
614
615};
616
617class SharedArray : public Array<int, TreeNodeInternal>, public ObjectRanges
618{
619public:
620 SharedArray(Array<int, TreeNodeInternal> &arr, ObjectRanges &ranges) : arr(arr), ranges(ranges), enabled_func(this, &SharedArray::Enabled), mapping_range_i(this, &SharedArray::MappingRangeI) { }
621
622public:
623 int Size() const
624 {
625 int c = arr.Size();
626 int count = 0;
627 for(int i=0;i<c;i++)
628 {
629 bool b = InsideEnabledRange(i);
630 if (b) count++;
631 }
632 return count;
633 }
634 TreeNodeInternal Index(int i) const
635 {
636 return arr.Index(MappingArrayI(i));
637 }
638
639 int NumObjects() const
640 {
641 int c = ranges.NumObjects();
642 int count = 0;
643 for(int i=0;i<c;i++)
644 {
645 bool b = Enabled(i)==-1;
646 if (b) count++;
647 }
648 return count;
649 }
650
651 int StartRange(int obj) const
652 {
653 int next = Enabled(obj);
654 if (next == -1)
655 {
656 return MappingArray(ranges.StartRange(MappingRange(obj)));
657 }
658 else
659 {
660 return MappingArray(ranges.StartRange(MappingRange(next)));
661 }
662 }
663 int EndRange(int obj) const
664 {
665 int next = Enabled(obj);
666 if (next == -1)
667 {
668 return MappingArray(ranges.EndRange(MappingRange(obj)));
669 }
670 else
671 {
672 return MappingArray(ranges.EndRange(MappingRange(next)));
673 }
674 }
675 Matrix Transform(int obj) const
676 {
677 return Matrix::Identity();
678 }
679public:
680 // bigger -> smaller
681 int MappingArray(int obj) const
682 {
683 int count = 0;
684 int num = arr.Size();
685 for(int i=0;i<num;i++)
686 {
687 if (i==obj) return count;
688 bool b = InsideEnabledRange(i);
689 if (b)
690 {
691 count++;
692 }
693 }
694 return 0;
695 }
696
697 // smaller -> bigger
698 int MappingArrayI(int obj) const
699 {
700 int count = 0;
701 int num = arr.Size();
702 for(int i=0;i<num;i++)
703 {
704 bool b = InsideEnabledRange(i);
705 if (b)
706 {
707 if (count==obj) return i;
708 count++;
709 }
710 }
711 return 0;
712 }
713
714
715 bool InsideEnabledRange(int num) const
716 {
717 int size = ranges.NumObjects();
718 for(int i=0;i<size;i++)
719 {
720 if (InsideRange(num,i))
721 {
722 if (Enabled(i)==-1)
723 {
724 return true;
725 }
726 else
727 {
728 return false;
729 }
730 }
731 }
732 return false;
733 }
734
735 bool InsideRange(int num, int obj) const
736 {
737 int start = ranges.StartRange(obj);
738 int end = ranges.EndRange(obj);
739 return num >=start && num < end;
740 }
741
742 // smaller -> bigger
743 int MappingRange(int obj) const
744 {
745 int count = 0;
746 int num = ranges.NumObjects();
747 for(int i=0;i<num;i++)
748 {
749 bool b = Enabled(i)==-1;
750 if (b)
751 {
752 if (count==obj) return i;
753 count++;
754 }
755 }
756 return 0;
757 }
758
759 // bigger -> smaller
760 int MappingRangeI(int obj) const
761 {
762 int count = 0;
763 int num = ranges.NumObjects();
764 for(int i=0;i<num;i++)
765 {
766 if (i==obj) return count;
767 bool b = Enabled(i)==-1;
768 if (b)
769 {
770 count++;
771 }
772 }
773 return 0;
774 }
775
776
777
778
779
780 // returns -1 if enabled
781 // returns 0..obj-1 if disabled
782 int Enabled(int obj) const
783 {
784 for(int i=0;i<obj;i++)
785 {
786 if (Equal(obj,i))
787 return i;
788 }
789 return -1;
790 }
791
792 bool Equal(int obj1, int obj2) const
793 {
794 //if (obj1==obj2) return false;
795 int start1 = ranges.StartRange(obj1);
796 int end1 = ranges.EndRange(obj1);
797 int start2 = ranges.StartRange(obj2);
798 int end2 = ranges.EndRange(obj2);
799 if (end1-start1 != end2-start2) return false;
800 for(int i=0;i<end1-start1;i++)
801 {
802 TreeNodeInternal n1 = arr.Index(start1+i);
803 TreeNodeInternal n2 = arr.Index(start2+i);
804 if (n1 == n2) continue;
805 return false;
806 }
807 return true;
808 }
809private:
810 Array<int, TreeNodeInternal> &arr;
811 ObjectRanges &ranges;
812public:
813 ForwardFunction<SharedArray, int, int> enabled_func;
814 ForwardFunction<SharedArray, int, int> mapping_range_i;
815};
816
817class FlattenTree : public Array<int, TreeNodeInternal>, public ObjectRanges
818{
819public:
820 FlattenTree(ObjectTree &tree) : tree(tree), size(sizeimpl), sizeimpl(tree) { }
821 int Size() const
822 {
823 VectorArray<int> vec;
824 return size.SubTreeSize(vec);
825 }
826 int NumObjects() const
827 {
828 int count = 0;
829 VectorArray<int> vec;
830 const TreeNodeCollection *coll = tree.TreeChilds(vec);
831 if (coll->Size() == 0) return 0;
832 count += 1;
833 int num = coll->Size();
834 for(int i=0;i<num;i++)
835 {
836 ObjectSubTree subtree(tree, i);
837 FlattenTree flat(subtree);
838 count += flat.NumObjects();
839 }
840 return count;
841 }
842 Matrix Transform(int obj) const
843 {
844 if (obj==0) return Matrix::Identity();
845 obj-=1;
846 VectorArray<int> vec;
847 const TreeNodeCollection *coll = tree.TreeChilds(vec);
848 int num = coll->Size();
849 int count = 0;
850 for(int i=0;i<num;i++)
851 {
852 int oldcount = count;
853 ObjectSubTree subtree(tree, i);
854 FlattenTree flat(subtree);
855 count += flat.NumObjects();
856 if (count > obj)
857 {
858 return flat.Transform(obj-oldcount) * coll->Index(i).matrix;
859 }
860 }
861 std::cout << "Flatten::Transform ERROR! " << std::endl;
862 return Matrix::Identity();
863 }
864 int StartRange(int obj) const
865 {
866 if (obj==0) return 0;
867 obj -= 1;
868
869 VectorArray<int> vec;
870 const TreeNodeCollection *coll = tree.TreeChilds(vec);
871 int num = coll->Size();
872 int count = 0;
873 int count2 = 0;
874 for(int i=0;i<num;i++)
875 {
876 int oldcount = count;
877 int oldcount2 = count2;
878 ObjectSubTree subtree(tree, i);
879 FlattenTree flat(subtree);
880 count += flat.NumObjects();
881
882 vec.push_back(i);
883 count2 += size.SubTreeSize(vec);
884 vec.pop_back();
885
886 if (count > obj)
887 {
888 int val = num + oldcount2 + flat.StartRange(obj-oldcount);
889 return val;
890 }
891 }
892 std::cout << "Flatten::Startrange ERROR! " << std::endl;
893 return 0;
894 }
895 int EndRange(int obj) const
896 {
897 VectorArray<int> vec;
898 const TreeNodeCollection *coll = tree.TreeChilds(vec);
899 if (obj==0) return coll->Size();
900 obj -= 1;
901
902 int num = coll->Size();
903 int count = 0;
904 int count2 = 0;
905 for(int i=0;i<num;i++)
906 {
907 int oldcount = count;
908 int oldcount2 = count2;
909 ObjectSubTree subtree(tree, i);
910 FlattenTree flat(subtree);
911 count += flat.NumObjects();
912
913 vec.push_back(i);
914 count2 += size.SubTreeSize(vec);
915 vec.pop_back();
916
917 if (count > obj)
918 {
919 int val = num + oldcount2 + flat.EndRange(obj-oldcount);
920 return val;
921 }
922 }
923 std::cout << "Flatten::Endrange ERROR! " << std::endl;
924 return 0;
925 }
926 TreeNodeInternal Index(int i) const
927 {
928 VectorArray<int> vec;
929 const TreeNodeCollection *coll = tree.TreeChilds(vec);
930 if (i < coll->Size())
931 {
932 TreeNode n = coll->Index(i);
933 TreeNodeInternal nn;
934 nn.matrix = n.matrix;
935 nn.faces = n.faces;
936 nn.quads = n.quads;
937 nn.params = n.params;
938 nn.current_level_matrix = n.matrix;
939 return nn;
940 }
941 int num = coll->Size();
942 i -= num;
943 int count = 0;
944 for(int ii=0;ii<num;ii++)
945 {
946 int oldcount = count;
947 vec.push_back(ii);
948 count += size.SubTreeSize(vec);
949 vec.pop_back();
950 if (count > i)
951 { // this one
952 ObjectSubTree subtree(tree, ii);
953 FlattenTree subtreeflat(subtree);
954 TreeNodeInternal n = subtreeflat.Index(i-oldcount);
955 n.matrix = coll->Index(ii).matrix * n.matrix;
956 return n;
957 }
958 }
959 std::cout << "ERROR: FlattenTree: i=" << i+num << std::endl;
960 TreeNodeInternal n = { };
961 return n;
962 }
963private:
964 ObjectTree &tree;
965 TreeSize &size;
966 TreeSizeImpl sizeimpl;
967};
968
969class TreeObjectData : public ObjectDataArray
970{
971public:
972 TreeObjectData(ObjectRanges &ranges, Function<int,int> &enabled, Function<int,int> &mapping_range_i) : ranges(ranges), enabled(enabled), mapping_range_i(mapping_range_i) { }
973 int Size() const { return ranges.NumObjects(); }
974 ObjectData Index(int i) const
975 {
976 ObjectData d;
977 d.matrix = ranges.Transform(i);
978 int en = enabled.Index(i);
979 if (en == -1) en = i;
980 d.object = mapping_range_i.Index(en);
981 d.params = 0; // TODO, ShaderParameters (flattentree needs to provide via ObjectRanges)
982 return d;
983 }
984private:
985 ObjectRanges &ranges;
986 Function<int,int> &enabled;
987 Function<int,int> &mapping_range_i;
988};
989class TreeStateInternal;
990class TreeState
991{
992public:
993 TreeState();
994 ~TreeState();
995 friend void PrepareTree(ObjectTree &tree, TreeState &treestate, VBOUpdate u);
996 friend void RenderTree(ObjectTree &tree, float time, TreeState &treestate, VBOUpdate u);
997private:
998 TreeStateInternal *priv;
999};
1000
1001void PrepareTree(ObjectTree &tree, TreeState &treestate, VBOUpdate u=UpdateVertexNormalIndex);
1002void RenderTree(ObjectTree &tree, float time, TreeState &treestate, VBOUpdate u=UpdateVertexNormalIndex);
1003
1004class RenderTreeCmds : public OpenGlCommands
1005{
1006public:
1007 RenderTreeCmds(ObjectTree &tree, float time) : tree(tree), time(time) { }
1008 void PreCalc();
1009 void Execute();
1010private:
1011 ObjectTree &tree;
1012 float time;
1013 TreeState treestate;
1014};
1015
1016class PositionTree : public ObjectTree
1017{
1018public:
1019 PositionTree(ObjectTreeForest &forest, TimedMatrixPaths &positions) : nodes(positions), tree(forest, nodes) { }
1020 int NumberOfLevels() const { return tree.NumberOfLevels(); }
1021 const TreeNodeCollection *TreeChilds(const Array<int,int> &path) const
1022 {
1023 return tree.TreeChilds(path);
1024 }
1025private:
1026 TimedMatrixNodes nodes;
1027 ForestToTree tree;
1028};
1029
1030class CircleTree : public ObjectTree
1031{
1032public:
1033 CircleTree(ObjectTree &object_tree) : circle(400.0), sample(circle, 5), treenode(sample), forest_array(&object_tree, treenode.Size()), forest(forest_array), tree(forest, treenode) { }
1034 int NumberOfLevels() const { return tree.NumberOfLevels(); }
1035 const TreeNodeCollection *TreeChilds(const Array<int,int> &path) const
1036 {
1037 return tree.TreeChilds(path);
1038 }
1039private:
1040 CircleMatrixCurve circle;
1041 SampleMatrixCurve sample;
1042 MatrixNodes treenode;
1043 DuplicateArray<int, ObjectTree*> forest_array;
1044 ObjectTreeForestConvert forest;
1045 ForestToTree tree;
1046};
1047
1048class RingTreeEffect : public FrameAnim
1049{
1050public:
1051 RingTreeEffect(Render *r);
1052 void Init();
1053 bool Frame(float time);
1054private:
1055 RingTreeNodes nodes;
1056 ArrayObjectTreeDynamic tree;
1057 //CircleTree circle;
1058 TreeState vbostate;
1059
1060 SingletonArray<ObjectTree*> forestarray;
1061 ObjectTreeForestConvert forest;
1062 CircleMatrixPath cpath;
1063 PositionTree pos;
1064};
1065class ManyCubesEffect : public FrameAnim
1066{
1067public:
1068 ManyCubesEffect(Render *r);
1069 void Init();
1070 bool Frame(float time);
1071private:
1072 CubeElem cube;
1073 MatrixElem me;
1074 RandomPointsInBox box;
1075 PointsToBoxCollection matrix;
1076 MatrixFacesNodes nodes;
1077 ArrayObjectTreeStatic tree;
1078 TreeState vbostate;
1079
1080 CircleTree circle;
1081};
1082
1083//
1084// New kind of trees
1085//
1086template<class Node, class Link>
1087class TTree
1088{
1089public:
1090 TTree(Node n) : n(n) { }
1091 TTree(typename std::vector<TTree<Node,Link> *> &children,
1092 Node n,
1093 std::vector<Link> links)
1094 : children(children), n(n), links(links) { }
1095
1096 int NumNodes() const { return children.size(); }
1097 Link &GetLink(int i) const { return links[i]; }
1098 Node &GetNode() const { return n; }
1099 TTree<Node,Link> *GetChildren(int i) const { }
1100 ~TTree()
1101 {
1102 int s = children.size();
1103 for(int i=0;i<s;i++) delete children[i];
1104 }
1105private:
1106 std::vector<TTree*> children;
1107 Node n;
1108 std::vector<Link> links;
1109};
1110
1111
1112
1113class TValue
1114{
1115public:
1116 virtual void *Value() const=0;
1117 virtual std::string Type() const=0;
1118};
1119template<class T>
1120class TTValue : public TValue
1121{
1122public:
1123 TTValue(T t) : t(t) { }
1124 void *Value() const { return &t; }
1125 std::string Type() const { return typeid(T).name(); }
1126private:
1127 T t;
1128};
1129
1130class TFunction
1131{
1132public:
1133 virtual void *ret_val() const=0;
1134 virtual std::string ret_type() const=0;
1135 virtual int num_params() const=0;
1136 virtual void set_param(int i, void *val) const=0;
1137 virtual std::string param_type(int i) const=0;
1138 virtual void Execute()=0;
1139 virtual TValue *create_return_value()=0;
1140};
1141
1142struct TFunctionArray
1143{
1144public:
1145 std::vector<TFunction*> vec;
1146};
1147
1148template<class T, class R>
1149class TTFunction : public TFunction
1150{
1151public:
1152 TTFunction(Function<T,R> &f) : f(f) { }
1153 virtual void *ret_val() const { return ret_value; }
1154 virtual std::string ret_type() const { return typeid(R).name(); }
1155 virtual int num_params() const { return 1; }
1156 virtual void set_param(int i, void *val) const
1157 {
1158 if (i==0)
1159 {
1160 param1 = val;
1161 }
1162 }
1163 virtual std::string param_type(int i) const { return typeid(T).name(); }
1164 virtual void Execute()
1165 {
1166 T t = *(T*)param1;
1167 r = f.Index(t);
1168 ret_value = (void*)&r;
1169 }
1170 virtual TValue *create_return_value() {
1171 return new TTValue<R>(r);
1172 }
1173private:
1174 Function<T,R> &f;
1175 void *ret_value;
1176 R r;
1177 void *param1;
1178};
1179
1180class DynamicTree : public TTree<TFunctionArray, TValue*>
1181{
1182};
1183
1184#endif
1185
1186#endif
P tree(EveryApi &ev)
Definition test.cpp:301
Type
Definition Database.hh:9
VBOUpdate
Definition Effect.hh:10676
@ UpdateVertexNormalIndex
Definition Effect.hh:10684
void Execute(FrameAnim &f, Low_SDL_Surface *screen)
Definition Main.cc:1603
void ring(Matrix m, const PointVectorCollection &points, float x, int steps, FaceStore &s)
Definition Functions.cc:73
void matrix(BoxableFaceCollection &coll, Matrix m, FaceStore &s)
Definition Functions.cc:56
void cube(Matrix m, FaceStore &s)
Definition Functions.cc:10
long double select(long double n, int k)
Definition Math2.cc:413
FlexibleCube box(Point p1, float sx, float sy, float sz)
Definition VectorTools.cc:1529
std::ostream & operator<<(std::ostream &, Catch_global_namespace_dummy)
Definition Effect.hh:919
Definition EffectI.hh:22
virtual I Size() const =0
Definition Effect.hh:67
Definition Effect.hh:571
virtual T Elem() const =0
Definition Effect.hh:108
virtual bool Frame(float time)=0
virtual void Init()
Definition Effect.hh:113
virtual B Index(A a) const =0
Definition VectorTools.hh:279
static Matrix XRotation(float rot)
Definition VectorTools.cc:498
static Matrix ZRotation(float rot)
Definition VectorTools.cc:537
static Matrix Identity()
Definition VectorTools.cc:487
Definition Effect.hh:5623
Definition Effect.hh:10773
virtual void Execute()=0
virtual void PreCalc()=0
virtual int Size() const =0
void push_back(T t)
Definition Effect.hh:1131
T pop_back()
Definition Effect.hh:1132
d
Definition meshpage.php:3895
iVBORw0KGgoAAAANSUhEUgAAANcAAAB5CAMAAACdtUQZAAAA51BMVEX</div ></div >< div style="padding: 0px 0px; width: 1324px;">< div style="height: 400px; width: 1324px;"></div >< div itemscope itemtype="https: </div><div itemscope itemtype="https:</div >< div itemscope itemtype="https: </div></div><p><div style="padding:0px 0px;width:1324px;"><div></div><div itemscope itemtype="https:< ul >< li >< b > Application name
8AkrcAgqgAs9wqQ0ey2tkAo8gAuuQfeI4hPUH09fXHy8z4+fng4uNKXF/Lz9BzfoAcgpkQMTUAKjAbOT4Qr9AXhJ8Rl7ktMCwTosA...
Definition meshpage.php:1610
n< br > font size
Definition meshpage.php:3536
not_this_one end(...)
size_t count(const T &t_target, const typename T::key_type &t_key)
Definition bootstrap_stl.hpp:104
void array(const std::string &type, Module &m)
Definition bootstrap.hpp:23
double f(const std::string &, double, bool) noexcept
Definition profile_cpp_calls_2.cpp:4
bool operator==(const short_alloc< T, N, A1 > &x, const short_alloc< U, M, A2 > &y) noexcept
Definition short_alloc.hpp:145
Matrix matrix
Definition Effect.hh:5616
W empty()
Definition ui.cc:634
num
Definition view.php:2821