polyhedron.h 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*Uses code from Stackoverflow which uses the MIT license and the CC BY-SA 3.0*/
/*These licenses are compatible with Apache 2.0*/

/*
# Copyright 2016-2018 The NOMAD Developers Group
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
 #     http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
*/

20 21 22
#ifndef __POLYHEDRON_H
#define __POLYHEDRON_H

lrz's avatar
lrz committed
23 24
#include <math.h>

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class Icosahedron {
public:
    static const int nFaces;
	static const int Faces[];
    static const int nVerts;
	static const float Verts[];
};

class Octahedron {
public:
    static const int nFaces;
    static const int Faces[];
    static const int nVerts;
    static const float Verts[];
};

class Tetrahedron {
public:
    static const int nFaces;
    static const int Faces[];
    static const int nVerts;
    static const float Verts[];
};
48

49

50 51 52 53 54
class Solid {
public:
	enum Type {
		Icosahedron,
		Octahedron,
55 56
		Tetrahedron,
		Sphere
57
	};
58
	Solid(Type t, int SphereFacets=15) {
59 60 61 62 63 64 65 66 67 68 69 70
		switch (t) {
		case Icosahedron:
			nFaces=Icosahedron::nFaces;
			Faces=Icosahedron::Faces;
			nVerts=Icosahedron::nVerts;
			Verts=Icosahedron::Verts;
			break;
		case Octahedron:
			nFaces=Octahedron::nFaces;
			Faces=Octahedron::Faces;
			nVerts=Octahedron::nVerts;
			Verts=Octahedron::Verts;
71
			break;
72 73 74 75 76
		case Tetrahedron:
			nFaces=Tetrahedron::nFaces;
			Faces=Tetrahedron::Faces;
			nVerts=Tetrahedron::nVerts;
			Verts=Tetrahedron::Verts;
77 78 79 80 81 82 83 84 85
			break;
		case Sphere:
//https://stackoverflow.com/questions/23143921/python-program-to-create-sphere-coordinates-not-working
			nVerts=SphereFacets*SphereFacets;
			
			float *tVerts=new float[nVerts*3];
			for (int i=0;i<SphereFacets;i++)
				for (int j=0;j<SphereFacets;j++) {
					float t, f;
86 87
					t=i*2.0f*(float)M_PI/(float)(SphereFacets-1);
					f=j*(float)M_PI/(float)(SphereFacets-1);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
					
					tVerts[i*SphereFacets*3 + j*3]=sin(f)*sin(t);
					tVerts[i*SphereFacets*3 + j*3+1]=sin(f)*cos(t);
					tVerts[i*SphereFacets*3 + j*3+2]=cos(f);
				}
			nFaces=2*(SphereFacets-1)*(SphereFacets-1);
			int *tFaces=new int[3*nFaces];
			for (int i=0;i<SphereFacets-1;i++)
				for (int j=0;j<SphereFacets-1;j++) {
					tFaces[i*(SphereFacets-1)*6 + j*6]=i*SphereFacets+j;
					tFaces[i*(SphereFacets-1)*6 + j*6+1]=((i+1)%SphereFacets)*SphereFacets+j;
					tFaces[i*(SphereFacets-1)*6 + j*6+2]=(i)*SphereFacets+(j+1)%SphereFacets;
					tFaces[i*(SphereFacets-1)*6 + j*6+3]=((i+1)%SphereFacets)*SphereFacets+j;
					tFaces[i*(SphereFacets-1)*6 + j*6+4]=(i)*SphereFacets+(j+1)%SphereFacets;
					tFaces[i*(SphereFacets-1)*6 + j*6+5]=((i+1)%SphereFacets)*SphereFacets+(j+1)%SphereFacets;
				}
			Faces=tFaces; Verts=tVerts;
		} // switch
	} // Solid (constructor)
	
108 109 110 111
int nFaces;
const int *Faces;
int nVerts;
const float *Verts;
112
}; // class Solid
113

114
#endif // __POLYHEDRON_H