File: _polyhedra_utils.cpp

package info (click to toggle)
yade 2025.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,308 kB
  • sloc: cpp: 93,298; python: 50,409; sh: 577; makefile: 162
file content (637 lines) | stat: -rw-r--r-- 22,709 bytes parent folder | download | duplicates (2)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// © 2013 Jan Elias, http://www.fce.vutbr.cz/STM/elias.j/, elias.j@fce.vutbr.cz
// https://www.vutbr.cz/www_base/gigadisk.php?i=95194aa9a

#ifdef YADE_CGAL

#include <lib/base/AliasNamespaces.hpp>
#include <lib/base/Logging.hpp>
#include <lib/high-precision/Constants.hpp>
#include <lib/pyutil/doc_opts.hpp>
#include <core/Omega.hpp>
#include <core/Scene.hpp>
#include <pkg/common/ElastMat.hpp>
#include <pkg/common/Sphere.hpp>
#include <pkg/polyhedra/Polyhedra.hpp>
#include <numpy/ndarraytypes.h>

CREATE_CPP_LOCAL_LOGGER("_polyhedra_utils.cpp");

namespace yade { // Cannot have #include directive inside.

using math::max;
using math::min;

//**********************************************************************************
//print polyhedron in basic position
void PrintPolyhedra(const shared_ptr<Shape>& shape)
{
	Polyhedra* A  = static_cast<Polyhedra*>(shape.get());
	Polyhedron PA = A->GetPolyhedron();
	A->Initialize();
	PrintPolyhedron(PA);
}

//**********************************************************************************
//print polyhedron in actual position
void PrintPolyhedraActualPos(const shared_ptr<Shape>& cm1, const State& state1)
{
	const Se3r& se3 = state1.se3;
	Polyhedra*  A   = static_cast<Polyhedra*>(cm1.get());
	A->Initialize();

	//move and rotate CGAL structure Polyhedron
	Matrix3r       rot_mat   = (se3.orientation).toRotationMatrix();
	Vector3r       trans_vec = se3.position;
	Transformation t_rot_trans(
	        rot_mat(0, 0),
	        rot_mat(0, 1),
	        rot_mat(0, 2),
	        trans_vec[0],
	        rot_mat(1, 0),
	        rot_mat(1, 1),
	        rot_mat(1, 2),
	        trans_vec[1],
	        rot_mat(2, 0),
	        rot_mat(2, 1),
	        rot_mat(2, 2),
	        trans_vec[2],
	        1);
	Polyhedron PA = A->GetPolyhedron();
	std::transform(PA.points_begin(), PA.points_end(), PA.points_begin(), t_rot_trans);

	PrintPolyhedron(PA);
}


//**********************************************************************************
//test of polyhedron intersection callable from python shell
bool do_Polyhedras_Intersect(const shared_ptr<Shape>& cm1, const shared_ptr<Shape>& cm2, const State& state1, const State& state2)
{
	const Se3r& se31 = state1.se3;
	const Se3r& se32 = state2.se3;
	Polyhedra*  A    = static_cast<Polyhedra*>(cm1.get());
	Polyhedra*  B    = static_cast<Polyhedra*>(cm2.get());

	//move and rotate 1st the CGAL structure Polyhedron
	Matrix3r       rot_mat   = (se31.orientation).toRotationMatrix();
	Vector3r       trans_vec = se31.position;
	Transformation t_rot_trans(
	        rot_mat(0, 0),
	        rot_mat(0, 1),
	        rot_mat(0, 2),
	        trans_vec[0],
	        rot_mat(1, 0),
	        rot_mat(1, 1),
	        rot_mat(1, 2),
	        trans_vec[1],
	        rot_mat(2, 0),
	        rot_mat(2, 1),
	        rot_mat(2, 2),
	        trans_vec[2],
	        1);
	Polyhedron PA = A->GetPolyhedron();
	std::transform(PA.points_begin(), PA.points_end(), PA.points_begin(), t_rot_trans);

	//move and rotate 2st the CGAL structure Polyhedron
	rot_mat     = (se32.orientation).toRotationMatrix();
	trans_vec   = se32.position;
	t_rot_trans = Transformation(
	        rot_mat(0, 0),
	        rot_mat(0, 1),
	        rot_mat(0, 2),
	        trans_vec[0],
	        rot_mat(1, 0),
	        rot_mat(1, 1),
	        rot_mat(1, 2),
	        trans_vec[1],
	        rot_mat(2, 0),
	        rot_mat(2, 1),
	        rot_mat(2, 2),
	        trans_vec[2],
	        1);
	Polyhedron PB = B->GetPolyhedron();
	std::transform(PB.points_begin(), PB.points_end(), PB.points_begin(), t_rot_trans);

	//calculate plane equations
	std::transform(PA.facets_begin(), PA.facets_end(), PA.planes_begin(), Plane_equation());
	std::transform(PB.facets_begin(), PB.facets_end(), PB.planes_begin(), Plane_equation());


	//call test
	return do_intersect(PA, PB);
}

//**********************************************************************************
//returns approximate sieve size of polyhedron
Real SieveSize(const shared_ptr<Shape>& cm1)
{
	Polyhedra* A = static_cast<Polyhedra*>(cm1.get());

	Real phi = M_PI / 4.;
	Real x, y;
	Real minx = 0, maxx = 0, miny = 0, maxy = 0;

	for (vector<Vector3r>::iterator i = A->v.begin(); i != A->v.end(); ++i) {
		x    = cos(phi) * (*i)[1] + sin(phi) * (*i)[2];
		y    = -sin(phi) * (*i)[1] + cos(phi) * (*i)[2];
		minx = min(minx, x);
		maxx = max(maxx, x);
		miny = min(miny, y);
		maxy = max(maxy, y);
	}

	return max(maxx - minx, maxy - miny);
}


//**********************************************************************************
//returns approximate size of polyhedron
Vector3r SizeOfPolyhedra(const shared_ptr<Shape>& cm1)
{
	Polyhedra* A = static_cast<Polyhedra*>(cm1.get());

	Real minx = 0, maxx = 0, miny = 0, maxy = 0, minz = 0, maxz = 0;

	for (vector<Vector3r>::iterator i = A->v.begin(); i != A->v.end(); ++i) {
		minx = min(minx, (*i)[0]);
		maxx = max(maxx, (*i)[0]);
		miny = min(miny, (*i)[1]);
		maxy = max(maxy, (*i)[1]);
		minz = min(minz, (*i)[2]);
		maxz = max(maxz, (*i)[2]);
	}

	return Vector3r(maxx - minx, maxy - miny, maxz - minz);
}

//**********************************************************************************
//save sieve curve points into a file
void SieveCurve()
{
	const shared_ptr<Scene>            _rb = shared_ptr<Scene>();
	shared_ptr<Scene>                  rb  = (_rb ? _rb : Omega::instance().getScene());
	std::vector<std::pair<Real, Real>> sieve_volume;
	Real                               total_volume = 0;
	for (const auto& b : *rb->bodies) {
		if (!b || !b->shape) continue;
		shared_ptr<Polyhedra> p = YADE_PTR_DYN_CAST<Polyhedra>(b->shape);
		if (p) {
			sieve_volume.push_back(std::pair<Real, Real>(SieveSize(p), p->GetVolume()));
			total_volume += p->GetVolume();
		}
	}

	std::sort(sieve_volume.begin(), sieve_volume.end());
	Real cumul_vol = 0;

	std::ofstream myfile;
	myfile.open("sieve_curve.dat");
	for (std::vector<std::pair<Real, Real>>::iterator i = sieve_volume.begin(); i != sieve_volume.end(); ++i) {
		cumul_vol += i->second / total_volume;
		myfile << i->first << "\t" << cumul_vol << endl;
	}
	myfile.close();
}

//**********************************************************************************
//save size of polyhedrons into a file
void SizeRatio()
{
	const shared_ptr<Scene> _rb = shared_ptr<Scene>();
	shared_ptr<Scene>       rb  = (_rb ? _rb : Omega::instance().getScene());
	std::ofstream           myfile;
	myfile.open("sizes.dat");
	for (const auto& b : *rb->bodies) {
		if (!b || !b->shape) continue;
		shared_ptr<Polyhedra> p = YADE_PTR_DYN_CAST<Polyhedra>(b->shape);
		if (p) { myfile << SizeOfPolyhedra(p) << endl; }
	}
	myfile.close();
}

//**********************************************************************************
//returns max coordinates
Vector3r MaxCoord(const shared_ptr<Shape>& cm1, const State& state1)
{
	const Se3r& se3 = state1.se3;
	Polyhedra*  A   = static_cast<Polyhedra*>(cm1.get());

	//move and rotate CGAL structure Polyhedron
	Matrix3r       rot_mat   = (se3.orientation).toRotationMatrix();
	Vector3r       trans_vec = se3.position;
	Transformation t_rot_trans(
	        rot_mat(0, 0),
	        rot_mat(0, 1),
	        rot_mat(0, 2),
	        trans_vec[0],
	        rot_mat(1, 0),
	        rot_mat(1, 1),
	        rot_mat(1, 2),
	        trans_vec[1],
	        rot_mat(2, 0),
	        rot_mat(2, 1),
	        rot_mat(2, 2),
	        trans_vec[2],
	        1);
	Polyhedron PA = A->GetPolyhedron();
	std::transform(PA.points_begin(), PA.points_end(), PA.points_begin(), t_rot_trans);

	Vector3r maxccord = trans_vec;
	for (Polyhedron::Vertex_iterator vi = PA.vertices_begin(); vi != PA.vertices_end(); ++vi) {
		if (vi->point()[0] > maxccord[0]) maxccord[0] = vi->point()[0];
		if (vi->point()[1] > maxccord[1]) maxccord[1] = vi->point()[1];
		if (vi->point()[2] > maxccord[2]) maxccord[2] = vi->point()[2];
	}

	return maxccord;
}

//**********************************************************************************
//returns min coordinates
Vector3r MinCoord(const shared_ptr<Shape>& cm1, const State& state1)
{
	const Se3r& se3 = state1.se3;
	Polyhedra*  A   = static_cast<Polyhedra*>(cm1.get());

	//move and rotate CGAL structure Polyhedron
	Matrix3r       rot_mat   = (se3.orientation).toRotationMatrix();
	Vector3r       trans_vec = se3.position;
	Transformation t_rot_trans(
	        rot_mat(0, 0),
	        rot_mat(0, 1),
	        rot_mat(0, 2),
	        trans_vec[0],
	        rot_mat(1, 0),
	        rot_mat(1, 1),
	        rot_mat(1, 2),
	        trans_vec[1],
	        rot_mat(2, 0),
	        rot_mat(2, 1),
	        rot_mat(2, 2),
	        trans_vec[2],
	        1);
	Polyhedron PA = A->GetPolyhedron();
	std::transform(PA.points_begin(), PA.points_end(), PA.points_begin(), t_rot_trans);

	Vector3r minccord = trans_vec;
	for (Polyhedron::Vertex_iterator vi = PA.vertices_begin(); vi != PA.vertices_end(); ++vi) {
		if (vi->point()[0] < minccord[0]) minccord[0] = vi->point()[0];
		if (vi->point()[1] < minccord[1]) minccord[1] = vi->point()[1];
		if (vi->point()[2] < minccord[2]) minccord[2] = vi->point()[2];
	}

	return minccord;
}


//**********************************************************************************
//generate "packing" of non-overlapping polyhedrons
vector<Vector3r> fillBox_cpp(Vector3r minCoord, Vector3r maxCoord, Vector3r sizemin, Vector3r sizemax, Vector3r ratio, int seed, shared_ptr<Material> mat)
{
	vector<Vector3r> v;
	Polyhedra        trialP;
	Polyhedron       trial, trial_moved;
	srand(seed);
	int                      it = 0;
	vector<Polyhedron>       polyhedrons;
	vector<vector<Vector3r>> vv;
	Vector3r                 position;
	bool                     intersection;
	int                      count = 0;

	bool fixed_ratio = 0;
	if (ratio[0] > 0 && ratio[1] > 0 && ratio[2] > 0) {
		fixed_ratio = 1;
		sizemax[0]  = min(min(sizemax[0] / ratio[0], sizemax[1] / ratio[1]), sizemax[2] / ratio[2]);
		sizemin[0]  = max(max(sizemin[0] / ratio[0], sizemin[1] / ratio[1]), sizemin[2] / ratio[2]);
	}

	//it - number of trials to make packing possibly more/less dense
	Vector3r random_size;
	while (it < 1000) {
		it = it + 1;
		if (it == 1) {
			trialP.Clear();
			trialP.seed = rand();
			if (fixed_ratio) trialP.size = (rand() * (sizemax[0] - sizemin[0]) / RAND_MAX + sizemin[0]) * ratio;
			else
				trialP.size
				        = Vector3r(rand() * (sizemax[0] - sizemin[0]), rand() * (sizemax[1] - sizemin[1]), rand() * (sizemax[2] - sizemin[2]))
				                / RAND_MAX
				        + sizemin;
			trialP.Initialize();
			trial                  = trialP.GetPolyhedron();
			Matrix3r       rot_mat = (trialP.GetOri()).toRotationMatrix();
			Transformation t_rot(
			        rot_mat(0, 0),
			        rot_mat(0, 1),
			        rot_mat(0, 2),
			        rot_mat(1, 0),
			        rot_mat(1, 1),
			        rot_mat(1, 2),
			        rot_mat(2, 0),
			        rot_mat(2, 1),
			        rot_mat(2, 2),
			        1);
			std::transform(trial.points_begin(), trial.points_end(), trial.points_begin(), t_rot);
		}
		position = Vector3r(rand() * (maxCoord[0] - minCoord[0]), rand() * (maxCoord[1] - minCoord[1]), rand() * (maxCoord[2] - minCoord[2])) / RAND_MAX
		        + minCoord;

		//move CGAL structure Polyhedron
		Transformation transl(CGAL::TRANSLATION, ToCGALVector(position));
		trial_moved = trial;
		std::transform(trial_moved.points_begin(), trial_moved.points_end(), trial_moved.points_begin(), transl);
		//calculate plane equations
		std::transform(trial_moved.facets_begin(), trial_moved.facets_end(), trial_moved.planes_begin(), Plane_equation());

		intersection = false;
		//call test with boundary
		for (Polyhedron::Vertex_iterator vi = trial_moved.vertices_begin(); (vi != trial_moved.vertices_end()) && (!intersection); vi++) {
			intersection = (vi->point().x() < minCoord[0]) || (vi->point().x() > maxCoord[0]) || (vi->point().y() < minCoord[1])
			        || (vi->point().y() > maxCoord[1]) || (vi->point().z() < minCoord[2]) || (vi->point().z() > maxCoord[2]);
		}
		//call test with other polyhedrons
		for (vector<Polyhedron>::iterator a = polyhedrons.begin(); (a != polyhedrons.end()) && (!intersection); a++) {
			intersection = do_intersect(*a, trial_moved);
			if (intersection) break;
		}
		if (!intersection) {
			polyhedrons.push_back(trial_moved);
			v.clear();
			for (Polyhedron::Vertex_iterator vi = trial_moved.vertices_begin(); vi != trial_moved.vertices_end(); vi++) {
				v.push_back(FromCGALPoint(vi->point()));
			}
			vv.push_back(v);
			it = 0;
			count++;
		}
	}
	cout << "generated " << count << " polyhedrons" << endl;

	//can't be used - no information about material
	Scene* scene = Omega::instance().getScene().get();
	for (vector<vector<Vector3r>>::iterator p = vv.begin(); p != vv.end(); ++p) {
		shared_ptr<Body> BP = NewPolyhedra(*p, mat);
		BP->shape->color    = Vector3r(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, double(rand()) / RAND_MAX);
		scene->bodies->insert(BP);
	}
	return v;
}

//**************************************************************************
/* Generate truncated icosahedron*/
vector<Vector3r> TruncIcosaHedPoints(Vector3r radii)
{
	vector<Vector3r> v;

	Real     p = (1. + sqrt(5.)) / 2.;
	Vector3r f, c, b;
	f = radii / sqrt(9. * p + 1.);
	vector<Vector3r> A, B;
	A.push_back(Vector3r(0., 1., 3. * p));
	A.push_back(Vector3r(2., 1. + 2. * p, p));
	A.push_back(Vector3r(1., 2. + p, 2. * p));
	for (int i = 0; i < (int)A.size(); i++) {
		B.clear();
		c = Vector3r(A[i][0] * f[0], A[i][1] * f[1], A[i][2] * f[2]);
		B.push_back(c);
		B.push_back(Vector3r(c[1], c[2], c[0]));
		B.push_back(Vector3r(c[2], c[0], c[1]));
		for (int j = 0; j < (int)B.size(); j++) {
			b = B[j];
			v.push_back(b);
			if (b[0] != 0.) {
				v.push_back(Vector3r(-b[0], b[1], b[2]));
				if (b[1] != 0.) {
					v.push_back(Vector3r(-b[0], -b[1], b[2]));
					if (b[2] != 0.) v.push_back(Vector3r(-b[0], -b[1], -b[2]));
				}
				if (b[2] != 0.) v.push_back(Vector3r(-b[0], b[1], -b[2]));
			}
			if (b[1] != 0.) {
				v.push_back(Vector3r(b[0], -b[1], b[2]));
				if (b[2] != 0.) v.push_back(Vector3r(b[0], -b[1], -b[2]));
			}
			if (b[2] != 0.) v.push_back(Vector3r(b[0], b[1], -b[2]));
		}
	}
	return v;
}

//**************************************************************************
/* Generate SnubCube*/
vector<Vector3r> SnubCubePoints(Vector3r radii)
{
	vector<Vector3r> v;

	double   c1 = 0.337754;
	double   c2 = 1.14261;
	double   c3 = 0.621226;
	Vector3r f, b;
	f = radii / 1.3437133737446;
	vector<Vector3r> A;
	A.push_back(Vector3r(c2, c1, c3));
	A.push_back(Vector3r(c1, c3, c2));
	A.push_back(Vector3r(c3, c2, c1));
	A.push_back(Vector3r(-c1, -c2, -c3));
	A.push_back(Vector3r(-c2, -c3, -c1));
	A.push_back(Vector3r(-c3, -c1, -c2));
	for (int i = 0; i < (int)A.size(); i++) {
		b = Vector3r(A[i][0] * f[0], A[i][1] * f[1], A[i][2] * f[2]);
		v.push_back(b);
		v.push_back(Vector3r(-b[0], -b[1], b[2]));
		v.push_back(Vector3r(-b[0], b[1], -b[2]));
		v.push_back(Vector3r(b[0], -b[1], -b[2]));
	}
	return v;
}

//**************************************************************************
/* Generate ball*/
vector<Vector3r> BallPoints(Vector3r radii, int NumFacets, int seed)
{
	vector<Vector3r> v;
	if (NumFacets == 60) v = TruncIcosaHedPoints(radii);
	if (NumFacets == 24) v = SnubCubePoints(radii);
	else {
		Real inc = Mathr::PI * (3. - pow(5., 0.5));
		Real off = 2. / double(NumFacets);
		Real y, r, phi;
		for (int k = 0; k < NumFacets; k++) {
			y   = Real(k) * off - 1. + (off / 2.);
			r   = pow(1. - y * y, 0.5);
			phi = Real(k) * inc;
			v.push_back(Vector3r(cos(phi) * r * radii[0], y * radii[1], sin(phi) * r * radii[2]));
		}
	}

	// randomly rotate
	srand(seed);
	Quaternionr Rot(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, double(rand()) / RAND_MAX);
	Rot.normalize();
	for (int i = 0; i < (int)v.size(); i++) {
		v[i] = Rot * (Vector3r(v[i][0], v[i][1], v[i][2]));
	}
	return v;
}

//**********************************************************************************
//generate "packing" of non-overlapping balls
vector<Vector3r>
fillBoxByBalls_cpp(Vector3r minCoord, Vector3r maxCoord, Vector3r sizemin, Vector3r sizemax, Vector3r ratio, int seed, shared_ptr<Material> mat, int NumPoints)
{
	vector<Vector3r> v;
	Polyhedra        trialP;
	Polyhedron       trial, trial_moved;
	srand(seed);
	int                      it = 0;
	vector<Polyhedron>       polyhedrons;
	vector<vector<Vector3r>> vv;
	Vector3r                 position;
	bool                     intersection;
	int                      count = 0;
	Vector3r                 radii;


	bool fixed_ratio = 0;
	if (ratio[0] > 0 && ratio[1] > 0 && ratio[2] > 0) {
		fixed_ratio = 1;
		sizemax[0]  = min(min(sizemax[0] / ratio[0], sizemax[1] / ratio[1]), sizemax[2] / ratio[2]);
		sizemin[0]  = max(max(sizemin[0] / ratio[0], sizemin[1] / ratio[1]), sizemin[2] / ratio[2]);
	}

	fixed_ratio = 1; //force spherical

	//it - number of trials to make packing possibly more/less dense
	Vector3r random_size;
	while (it < 1000) {
		it = it + 1;
		if (it == 1) {
			if (fixed_ratio) {
				Real rrr = (rand() * (sizemax[0] - sizemin[0]) / RAND_MAX + sizemin[0]) / 2.;
				radii    = Vector3r(rrr, rrr, rrr);
			} else {
				radii = Vector3r(
				                rand() * (sizemax[0] - sizemin[0]) / 2.,
				                rand() * (sizemax[1] - sizemin[1]) / 2.,
				                rand() * (sizemax[2] - sizemin[2]) / 2.)
				                / RAND_MAX
				        + sizemin / 2.;
			}
			trialP.v = BallPoints(radii, NumPoints, rand());
			trialP.Initialize();
			trial                  = trialP.GetPolyhedron();
			Matrix3r       rot_mat = (trialP.GetOri()).toRotationMatrix();
			Transformation t_rot(
			        rot_mat(0, 0),
			        rot_mat(0, 1),
			        rot_mat(0, 2),
			        rot_mat(1, 0),
			        rot_mat(1, 1),
			        rot_mat(1, 2),
			        rot_mat(2, 0),
			        rot_mat(2, 1),
			        rot_mat(2, 2),
			        1);
			std::transform(trial.points_begin(), trial.points_end(), trial.points_begin(), t_rot);
		}
		position = Vector3r(rand() * (maxCoord[0] - minCoord[0]), rand() * (maxCoord[1] - minCoord[1]), rand() * (maxCoord[2] - minCoord[2])) / RAND_MAX
		        + minCoord;

		//move CGAL structure Polyhedron
		Transformation transl(CGAL::TRANSLATION, ToCGALVector(position));
		trial_moved = trial;
		std::transform(trial_moved.points_begin(), trial_moved.points_end(), trial_moved.points_begin(), transl);
		//calculate plane equations
		std::transform(trial_moved.facets_begin(), trial_moved.facets_end(), trial_moved.planes_begin(), Plane_equation());

		intersection = false;
		//call test with boundary
		for (Polyhedron::Vertex_iterator vi = trial_moved.vertices_begin(); (vi != trial_moved.vertices_end()) && (!intersection); vi++) {
			intersection = (vi->point().x() < minCoord[0]) || (vi->point().x() > maxCoord[0]) || (vi->point().y() < minCoord[1])
			        || (vi->point().y() > maxCoord[1]) || (vi->point().z() < minCoord[2]) || (vi->point().z() > maxCoord[2]);
		}
		//call test with other polyhedrons
		for (vector<Polyhedron>::iterator a = polyhedrons.begin(); (a != polyhedrons.end()) && (!intersection); a++) {
			intersection = do_intersect(*a, trial_moved);
			if (intersection) break;
		}
		if (!intersection) {
			polyhedrons.push_back(trial_moved);
			v.clear();
			for (Polyhedron::Vertex_iterator vi = trial_moved.vertices_begin(); vi != trial_moved.vertices_end(); vi++) {
				v.push_back(FromCGALPoint(vi->point()));
			}
			vv.push_back(v);
			it = 0;
			count++;
		}
	}
	cout << "generated " << count << " polyhedrons" << endl;

	//can't be used - no information about material
	Scene* scene = Omega::instance().getScene().get();
	for (vector<vector<Vector3r>>::iterator p = vv.begin(); p != vv.end(); ++p) {
		shared_ptr<Body> BP = NewPolyhedra(*p, mat);
		BP->shape->color    = Vector3r(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, double(rand()) / RAND_MAX);
		scene->bodies->insert(BP);
	}
	return v;
}

//**********************************************************************************
//split polyhedra
void Split(const shared_ptr<Body> body, Vector3r direction, Vector3r point) { SplitPolyhedra(body, direction, point); }

//**********************************************************************************
//distace of point from a plane (squared) with sign
Real Oriented_squared_distance2(Plane P, CGALpoint x)
{
	Real h = P.a() * x.x() + P.b() * x.y() + P.c() * x.z() + P.d();
	return ((h > 0.) - (h < 0.)) * pow(h, 2) / (CGALvector(P.a(), P.b(), P.c())).squared_length();
}

//**********************************************************************************
bool convexHull(vector<Vector3r> points)
{
	vector<CGALpoint> pointsCGAL;
	for (int i = 0; i < (int)points.size(); i++) {
		pointsCGAL.push_back(ToCGALPoint(points[i]));
	}
	Polyhedron P;
	CGAL::convex_hull_3(pointsCGAL.begin(), pointsCGAL.end(), P);
	return true;
}

} // namespace yade

// BOOST_PYTHON_MODULE cannot be inside yade namespace, it has 'extern "C"' keyword, which strips it out of any namespaces.
BOOST_PYTHON_MODULE(_polyhedra_utils)
try {
	using namespace yade; // 'using namespace' inside function keeps namespace pollution under control. Alernatively I could add y:: in front of function names below and put 'namespace y  = ::yade;' here.
	namespace py = ::boost::python;
	YADE_SET_DOCSTRING_OPTS;
	py::def("PrintPolyhedra", PrintPolyhedra, "Print list of vertices sorted according to polyhedrons facets.");
	py::def("PrintPolyhedraActualPos", PrintPolyhedraActualPos, "Print list of vertices sorted according to polyhedrons facets.");
	py::def("do_Polyhedras_Intersect", do_Polyhedras_Intersect, "check polyhedras intersection");
	py::def("fillBox_cpp", fillBox_cpp, "Generate non-overlaping polyhedrons in box");
	py::def("fillBoxByBalls_cpp", fillBoxByBalls_cpp, "Generate non-overlaping 'spherical' polyhedrons in box");
	py::def("MinCoord", MinCoord, "returns min coordinates");
	py::def("MaxCoord", MaxCoord, "returns max coordinates");
	py::def("SieveSize", SieveSize, "returns approximate sieve size of polyhedron");
	py::def("SieveCurve", SieveCurve, "save sieve curve coordinates into file");
	py::def("SizeOfPolyhedra", SizeOfPolyhedra, "returns max, middle an min size in perpendicular directions");
	py::def("SizeRatio", SizeRatio, "save sizes of polyhedra into file");
	py::def("convexHull", convexHull, "TODO");
	py::def("Split", Split, "split polyhedron perpendicularly to given direction through given point");

} catch (...) {
	LOG_FATAL("Importing this module caused an exception and this module is in an inconsistent state now.");
	PyErr_Print();
	PyErr_SetString(PyExc_SystemError, __FILE__);
	boost::python::handle_exception();
	throw;
}

#endif // YADE_CGAL