/[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 3680 - (show annotations)
Fri Nov 18 04:48:53 2011 UTC (7 years, 7 months ago) by jfenwick
File size: 3743 byte(s)
It has memory errors during a getX.

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() 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 return temp;
134 }
135
136 void OctTree::assignIDs() const
137 {
138 unsigned id=0;
139 p.doLeafWalk_const(zorderlabel, &id);
140 p.doLeafWalk_const(clearIDs, 0);
141 unkid i=HANG_NODE+1;
142 p.doLeafWalkWithKids_const(setUnkids, 0, &i);
143 }
144
145
146 void OctTree::walkLeaves(const_cellfn c, void* v) const
147 {
148 p.doLeafWalk_const(c, v);
149 }
150
151 void OctTree::walkLeaves(cellfn c, void* v)
152 {
153 p.doLeafWalk(c, v);
154 }
155
156
157
158 void OctTree::walkWithKids(const_cellfn2 c, void* v)
159 {
160 p.doLeafWalkWithKids_const(c, 0, v); // we need to pick a childnum to start but a level 0 leaf is a trivial
161 } // tree anyway
162
163 unsigned OctTree::leafCountByWalk() const
164 {
165 unsigned c;
166 p.doLeafWalk_const(countleaves, &c);
167 return c;
168 }
169
170 unsigned OctTree::leafCount() const
171 {
172 return leafcount;
173 }
174
175 void OctTree::debug()
176 {
177 p.debug(false);
178 }
179

  ViewVC Help
Powered by ViewVC 1.1.26