File: odd_even.cc

package info (click to toggle)
voro%2B%2B 0.4.6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,372 kB
  • sloc: cpp: 6,384; perl: 232; makefile: 164
file content (56 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download | duplicates (3)
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
// Odd/even face coloring code
//
// Author   : Chris H. Rycroft (LBL / UC Berkeley)
// Email    : chr@alum.mit.edu
// Date     : August 30th 2011

#include <vector>
using namespace std;

#include "voro++.hh"
using namespace voro;

// This function returns a random floating point number between 0 and 1
double rnd() {return double(rand())/RAND_MAX;}

int main() {
	unsigned int i;
	double x,y,z,rsq,r;
	voronoicell v;

	// Initialize the Voronoi cell to be a cube of side length 2, centered
	// on the origin
	v.init(-1,1,-1,1,-1,1);

	// Cut the cell by 250 random planes which are all a distance 1 away
	// from the origin, to make an approximation to a sphere
	for(i=0;i<250;i++) {
		x=2*rnd()-1;
		y=2*rnd()-1;
		z=2*rnd()-1;
		rsq=x*x+y*y+z*z;
		if(rsq>0.01&&rsq<1) {
			r=1/sqrt(rsq);x*=r;y*=r;z*=r;
			v.plane(x,y,z,1);
		}
	}

	// Calculate the orders of the faces and the normal vectors
	vector<int> f_vert;
	vector<double> nor;
	v.face_orders(f_vert);
	v.normals(nor);

	// Output POV-Ray planes with textures based on whether a face is
	// composed of an odd or even number of edges
	const char* parity[2]={"even","odd"};
	FILE *fp=safe_fopen("odd_even_pl.pov","w");
	for(i=0;i<f_vert.size();i++)
		fprintf(fp,"plane{<%g,%g,%g>,0.5 texture{t_%s}}\n"
			,nor[3*i],nor[3*i+1],nor[3*i+2]
			,parity[f_vert[i]&1]);
	fclose(fp);

	// Save the Voronoi cell as a spheres and cylinders
	v.draw_pov(0,0,0,"odd_even_v.pov");
}