1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* sm_stree.c |
9 |
*/ |
10 |
#include "standard.h" |
11 |
#include "object.h" |
12 |
|
13 |
#include "sm_geom.h" |
14 |
#include "sm_stree.h" |
15 |
|
16 |
|
17 |
/* Define 4 vertices on the sphere to create a tetrahedralization on |
18 |
the sphere: triangles are as follows: |
19 |
(0,1,2),(0,2,3), (0,3,1), (1,3,2) |
20 |
*/ |
21 |
|
22 |
FVECT stDefault_base[4] = { {SQRT3_INV, SQRT3_INV, SQRT3_INV}, |
23 |
{-SQRT3_INV, -SQRT3_INV, SQRT3_INV}, |
24 |
{-SQRT3_INV, SQRT3_INV, -SQRT3_INV}, |
25 |
{SQRT3_INV, -SQRT3_INV, -SQRT3_INV}}; |
26 |
int stTri_verts[4][3] = { {2,1,0}, |
27 |
{3,2,0}, |
28 |
{1,3,0}, |
29 |
{2,3,1}}; |
30 |
|
31 |
stNth_base_verts(st,i,v1,v2,v3) |
32 |
STREE *st; |
33 |
int i; |
34 |
FVECT v1,v2,v3; |
35 |
{ |
36 |
VCOPY(v1,ST_NTH_BASE(st,stTri_verts[i][0])); |
37 |
VCOPY(v2,ST_NTH_BASE(st,stTri_verts[i][1])); |
38 |
VCOPY(v3,ST_NTH_BASE(st,stTri_verts[i][2])); |
39 |
} |
40 |
|
41 |
/* Frees the 4 quadtrees rooted at st */ |
42 |
stClear(st) |
43 |
STREE *st; |
44 |
{ |
45 |
int i; |
46 |
|
47 |
/* stree always has 4 children corresponding to the base tris |
48 |
*/ |
49 |
for (i = 0; i < 4; i++) |
50 |
qtFree(ST_NTH_ROOT(st, i)); |
51 |
|
52 |
QT_CLEAR_CHILDREN(ST_ROOT(st)); |
53 |
|
54 |
} |
55 |
|
56 |
|
57 |
STREE |
58 |
*stInit(st,center,base) |
59 |
STREE *st; |
60 |
FVECT center,base[4]; |
61 |
{ |
62 |
|
63 |
if(base) |
64 |
ST_SET_BASE(st,base); |
65 |
else |
66 |
ST_SET_BASE(st,stDefault_base); |
67 |
|
68 |
ST_SET_CENTER(st,center); |
69 |
stClear(st); |
70 |
|
71 |
return(st); |
72 |
} |
73 |
|
74 |
|
75 |
/* "base" defines 4 vertices on the sphere to create a tetrahedralization on |
76 |
the sphere: triangles are as follows:(0,1,2),(0,2,3), (0,3,1), (1,3,2) |
77 |
if base is null: does default. |
78 |
|
79 |
*/ |
80 |
STREE |
81 |
*stAlloc(st) |
82 |
STREE *st; |
83 |
{ |
84 |
int i; |
85 |
|
86 |
if(!st) |
87 |
st = (STREE *)malloc(sizeof(STREE)); |
88 |
|
89 |
ST_ROOT(st) = qtAlloc(); |
90 |
|
91 |
QT_CLEAR_CHILDREN(ST_ROOT(st)); |
92 |
|
93 |
return(st); |
94 |
} |
95 |
|
96 |
|
97 |
/* Find location of sample point in the DAG and return lowest level |
98 |
containing triangle. "type" indicates whether the point was found |
99 |
to be in interior to the triangle: GT_FACE, on one of its |
100 |
edges GT_EDGE or coinciding with one of its vertices GT_VERTEX. |
101 |
"which" specifies which vertex (0,1,2) or edge (0=v0v1, 1 = v1v2, 2 = v21) |
102 |
*/ |
103 |
int |
104 |
stPoint_locate(st,npt,type,which) |
105 |
STREE *st; |
106 |
FVECT npt; |
107 |
char *type,*which; |
108 |
{ |
109 |
int i,d,j,id; |
110 |
QUADTREE *rootptr,qt; |
111 |
FVECT v1,v2,v3; |
112 |
OBJECT os[MAXSET+1],*optr; |
113 |
FVECT p0,p1,p2; |
114 |
char w; |
115 |
|
116 |
/* Test each of the root triangles against point id */ |
117 |
for(i=0; i < 4; i++) |
118 |
{ |
119 |
rootptr = ST_NTH_ROOT_PTR(st,i); |
120 |
stNth_base_verts(st,i,v1,v2,v3); |
121 |
/* Return tri that p falls in */ |
122 |
qt = qtPoint_locate(rootptr,v1,v2,v3,npt,type,which,p0,p1,p2); |
123 |
if(QT_IS_EMPTY(qt)) |
124 |
continue; |
125 |
/* Get the set */ |
126 |
qtgetset(os,qt); |
127 |
for (j = QT_SET_CNT(os),optr = QT_SET_PTR(os); j > 0; j--) |
128 |
{ |
129 |
/* Find the first triangle that pt falls */ |
130 |
id = QT_SET_NEXT_ELEM(optr); |
131 |
qtTri_verts_from_id(id,p0,p1,p2); |
132 |
d = test_single_point_against_spherical_tri(p0,p1,p2,npt,&w); |
133 |
if(d) |
134 |
{ |
135 |
if(type) |
136 |
*type = d; |
137 |
if(which) |
138 |
*which = w; |
139 |
return(id); |
140 |
} |
141 |
} |
142 |
} |
143 |
if(which) |
144 |
*which = 0; |
145 |
if(type) |
146 |
*type = 0; |
147 |
return(EMPTY); |
148 |
} |
149 |
|
150 |
int |
151 |
stPoint_locate_cell(st,p,p0,p1,p2,type,which) |
152 |
STREE *st; |
153 |
FVECT p,p0,p1,p2; |
154 |
char *type,*which; |
155 |
{ |
156 |
int i,d; |
157 |
QUADTREE *rootptr,qt; |
158 |
FVECT v0,v1,v2; |
159 |
|
160 |
|
161 |
/* Test each of the root triangles against point id */ |
162 |
for(i=0; i < 4; i++) |
163 |
{ |
164 |
rootptr = ST_NTH_ROOT_PTR(st,i); |
165 |
stNth_base_verts(st,i,v0,v1,v2); |
166 |
/* Return tri that p falls in */ |
167 |
qt = qtPoint_locate(rootptr,v0,v1,v2,p,type,which,p0,p1,p2); |
168 |
/* NOTE: For now return only one triangle */ |
169 |
if(!QT_IS_EMPTY(qt)) |
170 |
return(qt); |
171 |
} /* Point not found */ |
172 |
if(which) |
173 |
*which = 0; |
174 |
if(type) |
175 |
*type = 0; |
176 |
return(EMPTY); |
177 |
} |
178 |
|
179 |
int |
180 |
stAdd_tri(st,id,v1,v2,v3) |
181 |
STREE *st; |
182 |
int id; |
183 |
FVECT v1,v2,v3; |
184 |
{ |
185 |
|
186 |
int i,found; |
187 |
QUADTREE *rootptr; |
188 |
FVECT t1,t2,t3; |
189 |
|
190 |
found = 0; |
191 |
for(i=0; i < 4; i++) |
192 |
{ |
193 |
rootptr = ST_NTH_ROOT_PTR(st,i); |
194 |
stNth_base_verts(st,i,t1,t2,t3); |
195 |
found |= qtAdd_tri(rootptr,id,v1,v2,v3,t1,t2,t3,0); |
196 |
} |
197 |
return(found); |
198 |
} |
199 |
|
200 |
int |
201 |
stApply_to_tri_cells(st,v0,v1,v2,func,arg) |
202 |
STREE *st; |
203 |
FVECT v0,v1,v2; |
204 |
int (*func)(); |
205 |
char *arg; |
206 |
{ |
207 |
int i,found; |
208 |
QUADTREE *rootptr; |
209 |
FVECT t0,t1,t2; |
210 |
|
211 |
found = 0; |
212 |
for(i=0; i < 4; i++) |
213 |
{ |
214 |
rootptr = ST_NTH_ROOT_PTR(st,i); |
215 |
stNth_base_verts(st,i,t0,t1,t2); |
216 |
found |= qtApply_to_tri_cells(rootptr,v0,v1,v2,t0,t1,t2,func,arg); |
217 |
} |
218 |
return(found); |
219 |
} |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
int |
225 |
stRemove_tri(st,id,v0,v1,v2) |
226 |
STREE *st; |
227 |
int id; |
228 |
FVECT v0,v1,v2; |
229 |
{ |
230 |
|
231 |
int i,found; |
232 |
QUADTREE *rootptr; |
233 |
FVECT t0,t1,t2; |
234 |
|
235 |
found = 0; |
236 |
for(i=0; i < 4; i++) |
237 |
{ |
238 |
rootptr = ST_NTH_ROOT_PTR(st,i); |
239 |
stNth_base_verts(st,i,t0,t1,t2); |
240 |
found |= qtRemove_tri(rootptr,id,v0,v1,v2,t0,t1,t2); |
241 |
} |
242 |
return(found); |
243 |
} |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|