-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTetrahedron.cpp
186 lines (152 loc) · 5.41 KB
/
Tetrahedron.cpp
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
/***********************************************************************
Tetrahedron - Class for silicate structural units (used to build
glasses).
Copyright (c) 2003-2011 Oliver Kreylos
The Nanotech Construction Kit is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Nanotech Construction Kit is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Nanotech Construction Kit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include <Misc/ConfigurationFile.h>
#include <Misc/StandardValueCoders.h>
#include <Math/Math.h>
#include <GL/gl.h>
#include <GL/GLContextData.h>
#include <GL/GLGeometryWrappers.h>
#include <GL/GLTransformationWrappers.h>
#include "Tetrahedron.h"
namespace NCK {
/************************************
Methods of class TetrahedronRenderer:
************************************/
void TetrahedronRenderer::initContext(GLContextData& contextData) const
{
/* Create context data item: */
DataItem* dataItem=new DataItem;
contextData.addDataItem(this,dataItem);
/* Create model display list: */
glNewList(dataItem->displayListId,GL_COMPILE);
/* Render tetrahedron: */
glBegin(GL_TRIANGLES);
glNormal(Tetrahedron::renderNormals[0]);
glVertex(Tetrahedron::renderVertices[1]);
glVertex(Tetrahedron::renderVertices[2]);
glVertex(Tetrahedron::renderVertices[3]);
glNormal(Tetrahedron::renderNormals[1]);
glVertex(Tetrahedron::renderVertices[2]);
glVertex(Tetrahedron::renderVertices[0]);
glVertex(Tetrahedron::renderVertices[3]);
glNormal(Tetrahedron::renderNormals[2]);
glVertex(Tetrahedron::renderVertices[0]);
glVertex(Tetrahedron::renderVertices[1]);
glVertex(Tetrahedron::renderVertices[3]);
glNormal(Tetrahedron::renderNormals[3]);
glVertex(Tetrahedron::renderVertices[0]);
glVertex(Tetrahedron::renderVertices[2]);
glVertex(Tetrahedron::renderVertices[1]);
glEnd();
/* Finish model display list: */
glEndList();
}
/************************************
Static elements of class Tetrahedron:
************************************/
Scalar Tetrahedron::radius;
Scalar Tetrahedron::radius2;
Scalar Tetrahedron::mass;
Vector Tetrahedron::vertexOffsets[4];
Point Tetrahedron::renderVertices[4];
Vector Tetrahedron::renderNormals[4];
TetrahedronRenderer* Tetrahedron::unitRenderer=0;
/****************************
Methods of class Tetrahedron:
****************************/
void Tetrahedron::calculateShape(void)
{
/* Calculate vertex offset radius based on force parameters: */
Scalar vertexRadius=calcVertexRadius(radius);
/* Recalculate vertex offsets: */
Scalar c1=Math::cos(Math::rad(30.0));
Scalar s1=Math::sin(Math::rad(30.0));
Scalar tetAngle=Math::acos(Scalar(-1)/Scalar(3));
Scalar c2=Math::sin(tetAngle);
Scalar s2=-Math::cos(tetAngle);
Vector v[4];
v[0]=Vector(-c1*c2,-s1*c2,-s2);
v[1]=Vector(c1*c2,-s1*c2,-s2);
v[2]=Vector(Scalar(0),c2,-s2);
v[3]=Vector(Scalar(0),Scalar(0),Scalar(1));
for(int i=0;i<4;++i)
vertexOffsets[i]=v[i]*vertexRadius;
/* Recalculate render vertices and normals: */
for(int i=0;i<4;++i)
{
renderVertices[i]=Point::origin+v[i]*radius;
renderNormals[i]=-v[i];
}
}
void Tetrahedron::initClass(const Misc::ConfigurationFileSection& configFileSection)
{
/* Read class settings: */
radius=configFileSection.retrieveValue<Scalar>("./radius",1.550185);
radius2=Math::sqr(radius);
mass=configFileSection.retrieveValue<Scalar>("./mass",1.0);
/* Calculate tetrahedron shape: */
calculateShape();
/* Create tetrahedron renderer: */
unitRenderer=new TetrahedronRenderer;
}
void Tetrahedron::deinitClass(void)
{
/* Destroy tetrahedron renderer: */
delete unitRenderer;
unitRenderer=0;
}
void Tetrahedron::setRadius(Scalar newRadius)
{
/* Set tetrahedron size: */
radius=newRadius;
radius2=Math::sqr(radius);
/* Re-calculate tetrahedron shape: */
calculateShape();
}
void Tetrahedron::setMass(Scalar newMass)
{
/* Set tetrahedron mass: */
mass=newMass;
}
void Tetrahedron::applyVertexForce(int index,const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
/* Calculate torque: */
Vector torque=Geometry::cross(orientation.transform(vertexOffsets[index]),force);
/* Apply angular acceleration: */
angularVelocity+=torque*(timeStep/mass); // Should be moment of inertia instead of mass here
}
void Tetrahedron::applyCentralForce(const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
}
void Tetrahedron::glRenderAction(GLContextData& contextData) const
{
/* Retrieve data item from GL context: */
TetrahedronRenderer::DataItem* dataItem=contextData.retrieveDataItem<TetrahedronRenderer::DataItem>(unitRenderer);
/* Move model coordinate system to tetrahedron's position and orientation: */
glPushMatrix();
glTranslate(position.getComponents());
glRotate(orientation);
/* Render tetrahedron: */
glCallList(dataItem->displayListId);
/* Reset model coordinates: */
glPopMatrix();
}
}