Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DT_Polyhedron.cpp « convex « src « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f48ac6e4b6dfd44e91b18d37727cb17e983a5820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#include "DT_Polyhedron.h"

#ifdef QHULL

extern "C" {
#include <qhull/qhull_a.h>
}

#include <vector>
#include <new>  

typedef std::vector<MT_Point3> T_VertexBuf;
typedef std::vector<DT_Index> T_IndexBuf;
typedef std::vector<T_IndexBuf> T_MultiIndexBuf;

static char options[] = "qhull Qts i Tv";

#define DK_HIERARCHY

T_IndexBuf *adjacency_graph(DT_Count count, const MT_Point3 *verts, const char *flags)
{
	int curlong, totlong, exitcode;
	
    facetT *facet;
    vertexT *vertex;
    vertexT **vertexp;
    
    std::vector<MT::Tuple3<coordT> > array;
	T_IndexBuf index;
    DT_Index i;
    for (i = 0; i != count; ++i) 
	{
		if (flags == 0 || flags[i])
		{
            array.push_back(MT::Tuple3<coordT>(verts[i]));
			index.push_back(i);
		}
    }

    qh_init_A(stdin, stdout, stderr, 0, NULL);
    if ((exitcode = setjmp(qh errexit))) 
	{
		exit(exitcode);
	}
    qh_initflags(options);
    qh_init_B(array[0], array.size(), 3, False);
    qh_qhull();
    qh_check_output();
    
    T_IndexBuf *indexBuf = new T_IndexBuf[count];
    FORALLfacets 
	{
		setT *vertices = qh_facet3vertex(facet);
		
		T_IndexBuf  facetIndices;

		FOREACHvertex_(vertices) 
		{
			facetIndices.push_back(index[qh_pointid(vertex->point)]);
		}
		int i, j;
		for (i = 0, j = facetIndices.size()-1; i < (int)facetIndices.size(); j = i++)
		{
			indexBuf[facetIndices[j]].push_back(facetIndices[i]);
		}
    }

    
    qh NOerrexit = True;
    qh_freeqhull(!qh_ALL);
    qh_memfreeshort(&curlong, &totlong);

	return indexBuf;
}

T_IndexBuf *simplex_adjacency_graph(DT_Count count, const char *flags)
{
	T_IndexBuf *indexBuf = new T_IndexBuf[count];

	DT_Index index[4];
	
	DT_Index k = 0;
	DT_Index i;
	for (i = 0; i != count; ++i) 
	{
		if (flags == 0 || flags[i])
		{
			index[k++] = i;
		}
	}

	assert(k <= 4);

	for (i = 0; i != k; ++i)
	{
		DT_Index j;
		for (j = 0; j != k; ++j)
		{
			if (i != j)
			{
				indexBuf[index[i]].push_back(index[j]);
			}
		}
	}

	return indexBuf;
}

#ifdef DK_HIERARCHY

void prune(DT_Count count, T_MultiIndexBuf *cobound)
{
	DT_Index i;
	for (i = 0; i != count; ++i)
	{
		assert(cobound[i].size());

		DT_Index j;
		for (j = 0; j != cobound[i].size() - 1; ++j)
		{
			T_IndexBuf::iterator it = cobound[i][j].begin();
			while (it != cobound[i][j].end())
			{
				T_IndexBuf::iterator jt = 
					std::find(cobound[i][j+1].begin(), cobound[i][j+1].end(), *it);

				if (jt != cobound[i][j+1].end())
				{
					std::swap(*it, cobound[i][j].back());
					cobound[i][j].pop_back();
				}
				else
				{
					++it;
				}
			}
		}
	}	
}

#endif

DT_Polyhedron::DT_Polyhedron(const DT_VertexBase *base, DT_Count count, const DT_Index *indices)
{
	assert(count);

	std::vector<MT_Point3> vertexBuf;
	DT_Index i;
	for (i = 0; i != count; ++i) 
	{
		vertexBuf.push_back((*base)[indices[i]]);
	}

	T_IndexBuf *indexBuf = count > 4 ? adjacency_graph(count, &vertexBuf[0], 0) : simplex_adjacency_graph(count, 0);
	
	std::vector<MT_Point3> pointBuf;
	
	for (i = 0; i != count; ++i) 
	{
		if (!indexBuf[i].empty()) 
		{
			pointBuf.push_back(vertexBuf[i]);
		}
	}
			
	delete [] indexBuf;

	m_count = pointBuf.size();
	m_verts = new MT_Point3[m_count];	
	std::copy(pointBuf.begin(), pointBuf.end(), &m_verts[0]);

	T_MultiIndexBuf *cobound = new T_MultiIndexBuf[m_count];
    char *flags = new char[m_count];
	std::fill(&flags[0], &flags[m_count], 1);

	DT_Count num_layers = 0;
	DT_Count layer_count = m_count;
	while (layer_count > 4)
	{
		T_IndexBuf *indexBuf = adjacency_graph(m_count, m_verts, flags);
		
		DT_Index i;
		for (i = 0; i != m_count; ++i) 
		{
			if (flags[i])
			{
				assert(!indexBuf[i].empty());
				cobound[i].push_back(indexBuf[i]);
			}
		}
			
		++num_layers;

		delete [] indexBuf;

		std::fill(&flags[0], &flags[m_count], 0);

		for (i = 0; i != m_count; ++i)
		{
			if (cobound[i].size() == num_layers) 
			{
				T_IndexBuf& curr_cobound = cobound[i].back();	
				if (!flags[i] && curr_cobound.size() <= 8)
				{	
					DT_Index j;
					for (j  = 0; j != curr_cobound.size(); ++j)
					{
						flags[curr_cobound[j]] = 1;
					}
				}
			}
		}
		
		layer_count = 0;
		
		for (i = 0; i != m_count; ++i)
		{
			if (flags[i])
			{
				++layer_count;
			}
		}	
	}
	
	indexBuf = simplex_adjacency_graph(m_count, flags);
		
	for (i = 0; i != m_count; ++i) 
	{
		if (flags[i])
		{
			assert(!indexBuf[i].empty());
			cobound[i].push_back(indexBuf[i]);
		}
	}
	
	++num_layers;

	delete [] indexBuf;
	delete [] flags;
		


#ifdef DK_HIERARCHY
	prune(m_count, cobound);
#endif

	m_cobound = new T_MultiIndexArray[m_count];

	for (i = 0; i != m_count; ++i)
	{
		new (&m_cobound[i]) T_MultiIndexArray(cobound[i].size());
		
		DT_Index j;
		for (j = 0; j != cobound[i].size(); ++j)
		{
			new (&m_cobound[i][j]) DT_IndexArray(cobound[i][j].size(), &cobound[i][j][0]);
		}
	}
		
	delete [] cobound;

	m_start_vertex = 0;
	while (m_cobound[m_start_vertex].size() != num_layers) 
	{
		++m_start_vertex;
		assert(m_start_vertex < m_count);
	}

	m_curr_vertex = m_start_vertex;
} 


DT_Polyhedron::~DT_Polyhedron() 
{
	delete [] m_verts;
    delete [] m_cobound;
}

#ifdef DK_HIERARCHY

MT_Scalar DT_Polyhedron::supportH(const MT_Vector3& v) const 
{
    m_curr_vertex = m_start_vertex;
    MT_Scalar d = (*this)[m_curr_vertex].dot(v);
    MT_Scalar h = d;
	int curr_layer;
	for (curr_layer = m_cobound[m_start_vertex].size(); curr_layer != 0; --curr_layer)
	{
		const DT_IndexArray& curr_cobound = m_cobound[m_curr_vertex][curr_layer-1];
        DT_Index i;
		for (i = 0; i != curr_cobound.size(); ++i) 
		{
			d = (*this)[curr_cobound[i]].dot(v);
			if (d > h)
			{
				m_curr_vertex = curr_cobound[i];
				h = d;
			}
		}
	}
	
    return h;
}

MT_Point3 DT_Polyhedron::support(const MT_Vector3& v) const 
{
	m_curr_vertex = m_start_vertex;
    MT_Scalar d = (*this)[m_curr_vertex].dot(v);
    MT_Scalar h = d;
	int curr_layer;
	for (curr_layer = m_cobound[m_start_vertex].size(); curr_layer != 0; --curr_layer)
	{
		const DT_IndexArray& curr_cobound = m_cobound[m_curr_vertex][curr_layer-1];
        DT_Index i;
		for (i = 0; i != curr_cobound.size(); ++i) 
		{
			d = (*this)[curr_cobound[i]].dot(v);
			if (d > h)
			{
				m_curr_vertex = curr_cobound[i];
				h = d;
			}
		}
	}
	
    return (*this)[m_curr_vertex];
}

#else

MT_Scalar DT_Polyhedron::supportH(const MT_Vector3& v) const 
{
    int last_vertex = -1;
    MT_Scalar d = (*this)[m_curr_vertex].dot(v);
    MT_Scalar h = d;
	
	for (;;) 
	{
        DT_IndexArray& curr_cobound = m_cobound[m_curr_vertex][0];
        int i = 0, n = curr_cobound.size(); 
        while (i != n && 
               (curr_cobound[i] == last_vertex || 
				(d = (*this)[curr_cobound[i]].dot(v)) - h <= MT_abs(h) * MT_EPSILON)) 
		{
            ++i;
		}
		
        if (i == n) 
		{
			break;
		}
		
        last_vertex = m_curr_vertex;
        m_curr_vertex = curr_cobound[i];
        h = d;
    }
    return h;
}

MT_Point3 DT_Polyhedron::support(const MT_Vector3& v) const 
{
	int last_vertex = -1;
    MT_Scalar d = (*this)[m_curr_vertex].dot(v);
    MT_Scalar h = d;
	
    for (;;)
	{
        DT_IndexArray& curr_cobound = m_cobound[m_curr_vertex][0];
        int i = 0, n = curr_cobound.size();
        while (i != n && 
               (curr_cobound[i] == last_vertex || 
				(d = (*this)[curr_cobound[i]].dot(v)) - h <= MT_abs(h) * MT_EPSILON)) 
		{
            ++i;
		}
		
        if (i == n)
		{
			break;
		}
		
		last_vertex = m_curr_vertex;
        m_curr_vertex = curr_cobound[i];
        h = d;
    }
    return (*this)[m_curr_vertex];
}

#endif

#endif