/[escript]/branches/refine/buckley/src/OctTree.cc
ViewVC logotype

Contents of /branches/refine/buckley/src/OctTree.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3682 - (show annotations)
Mon Nov 21 01:56:45 2011 UTC (9 years, 8 months ago) by jfenwick
File size: 3773 byte(s)
Fixed some cases which allowed domain objects to be created without wrappers

1
2 #include "OctTree.h"
3 #include <cmath>
4 #include <cstring> // for memset
5 #include "FaceConsts.h"
6
7 using namespace buckley;
8
9
10 // Note: This code assumes x,y,z are positive
11 OctTree::OctTree(double x, double y, double z)
12 :p(fabs(x)/2, fabs(y)/2, fabs(z)/2, fabs(x), fabs(y), fabs(z),0), modified(true)
13 {
14 side[0]=fabs(x);
15 side[1]=fabs(y);
16 side[2]=fabs(z);
17 leafcount=1;
18 }
19
20 OctTree::~OctTree(){}
21
22 // guarantees that all leaves are at depth >=d
23 void OctTree::allSplit(unsigned int d)
24 {
25 modified=true;
26 p.allSplit(d, &leafcount);
27 }
28
29 void OctTree::allCollapse(unsigned d)
30 {
31 modified=true;
32 p.collapseAll(d, &leafcount);
33 }
34
35 void OctTree::collapsePoint(double x, double y, double z, unsigned int d)
36 {
37 modified=true;
38 p.collapsePoint(x,y,z,d, &leafcount);
39
40 }
41
42 void OctTree::splitPoint(double x, double y, double z, unsigned int desdepth)
43 {
44 if ((x<0) || (y<0) || (z<0) ||
45 (x>side[0]) || (y>side[1]) || (z>side[2]))
46 {
47 return;
48 }
49 modified=true;
50 p.splitPoint(x, y, z, desdepth, &leafcount);
51 }
52
53 namespace
54 {
55
56 void zorderlabel(const OctCell& c, void* v)
57 {
58 c.id=(*reinterpret_cast<unsigned*>(v))++;
59 }
60
61 void countleaves(const OctCell& c, void* v)
62 {
63 (*reinterpret_cast<int*>(v))++;
64 }
65
66 void clearIDs(const OctCell& c, void* v)
67 {
68 if (c.leaf)
69 {
70 memset(c.leafinfo->pmap,0,sizeof(unkid)*8);
71 }
72 }
73
74 // set the unknown ids in pmap
75 // the void argument is a pair <childnum, nextid>
76 // where childnum is the position of this cell in its parents array and nextid is the next available id to give to a corner
77 void setUnkids(const OctCell& c, int kidnum, void* v)
78 {
79 bool bigger[6]; // record if the neighbour cell on that face is bigger/shallower than us
80 for (unsigned i=0;i<6;++i)
81 {
82 bigger[i]=(c.leafinfo->next[i])?
83 (c.leafinfo->next[i]->owner->depth < c.depth) :
84 false;
85 }
86 // we need to assign an id to each corner or mark it as hanging
87 for (unsigned i=0;i<8;++i)
88 {
89 if (!c.leafinfo->pmap[i]) // no id yet
90 {
91 // now we need to workout whether the node hangs
92 if ((canhang[kidnum][i]) && (bigger[facestouch[i][0]] || bigger[facestouch[i][1]] || bigger[facestouch[i][2]]))
93 {
94 // the node hangs
95 c.leafinfo->pmap[i]=HANG_NODE;
96 }
97 else
98 {
99 // the node does not hang
100 c.leafinfo->pmap[i]=(*reinterpret_cast<unkid*>(v))++;
101 }
102 }
103 }
104 }
105
106
107 typedef struct
108 {
109 const OctCell** ca;
110 unkid id;
111 } copier;
112
113 void copytoarr(const OctCell& c, void* v)
114 {
115 copier* cs=reinterpret_cast<copier*>(v);
116 cs->ca[cs->id++]=&c;
117 }
118
119
120 }
121
122
123 const OctCell** OctTree::process(unkid& numunk) const
124 {
125 assignIDs();
126 // nasty hack process for now
127 // ultimately, this should be done by starting each thread midway through the traversal
128 const OctCell** temp=new const OctCell*[leafcount];
129 copier c;
130 c.ca=temp;
131 c.id=0;
132 p.doLeafWalk_const(copytoarr, &c);
133 numunk=c.id;
134 return temp;
135 }
136
137 void OctTree::assignIDs() const
138 {
139 unsigned id=0;
140 p.doLeafWalk_const(zorderlabel, &id);
141 p.doLeafWalk_const(clearIDs, 0);
142 unkid i=HANG_NODE+1;
143 p.doLeafWalkWithKids_const(setUnkids, 0, &i);
144 }
145
146
147 void OctTree::walkLeaves(const_cellfn c, void* v) const
148 {
149 p.doLeafWalk_const(c, v);
150 }
151
152 void OctTree::walkLeaves(cellfn c, void* v)
153 {
154 p.doLeafWalk(c, v);
155 }
156
157
158
159 void OctTree::walkWithKids(const_cellfn2 c, void* v)
160 {
161 p.doLeafWalkWithKids_const(c, 0, v); // we need to pick a childnum to start but a level 0 leaf is a trivial
162 } // tree anyway
163
164 unsigned OctTree::leafCountByWalk() const
165 {
166 unsigned c;
167 p.doLeafWalk_const(countleaves, &c);
168 return c;
169 }
170
171 unsigned OctTree::leafCount() const
172 {
173 return leafcount;
174 }
175
176 void OctTree::debug()
177 {
178 p.debug(false);
179 }
180

  ViewVC Help
Powered by ViewVC 1.1.26