/
Triangle.cpp
207 lines (171 loc) · 5.89 KB
/
Triangle.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***********************************************************************
Triangle - Class for carbon structural units (used to build nanotubes
and Buckyballs).
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 "Triangle.h"
namespace NCK {
/*********************************
Methods of class TriangleRenderer:
*********************************/
void TriangleRenderer::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 triangle sides: */
glBegin(GL_QUADS);
glNormal(Triangle::renderNormals[0]);
glVertex(Triangle::renderVertices[0]);
glVertex(Triangle::renderVertices[1]);
glVertex(Triangle::renderVertices[4]);
glVertex(Triangle::renderVertices[3]);
glNormal(Triangle::renderNormals[1]);
glVertex(Triangle::renderVertices[1]);
glVertex(Triangle::renderVertices[2]);
glVertex(Triangle::renderVertices[5]);
glVertex(Triangle::renderVertices[4]);
glNormal(Triangle::renderNormals[2]);
glVertex(Triangle::renderVertices[2]);
glVertex(Triangle::renderVertices[0]);
glVertex(Triangle::renderVertices[3]);
glVertex(Triangle::renderVertices[5]);
glEnd();
/* Render triangle caps: */
glBegin(GL_TRIANGLES);
glNormal(Triangle::renderNormals[3]);
glVertex(Triangle::renderVertices[0]);
glVertex(Triangle::renderVertices[2]);
glVertex(Triangle::renderVertices[1]);
glNormal(Triangle::renderNormals[4]);
glVertex(Triangle::renderVertices[3]);
glVertex(Triangle::renderVertices[4]);
glVertex(Triangle::renderVertices[5]);
glEnd();
/* Finish model display list: */
glEndList();
}
/*********************************
Static elements of class Triangle:
*********************************/
Scalar Triangle::radius;
Scalar Triangle::radius2;
Scalar Triangle::width;
Scalar Triangle::mass;
Vector Triangle::vertexOffsets[3];
Point Triangle::renderVertices[6];
Vector Triangle::renderNormals[5];
TriangleRenderer* Triangle::unitRenderer=0;
/*************************
Methods of class Triangle:
*************************/
void Triangle::calculateShape(void)
{
/* Calculate vertex offset radius based on force parameters: */
Scalar vertexRadius=calcVertexRadius(radius);
/* Recalculate vertex offsets: */
Scalar c=Math::cos(Math::rad(Scalar(30)));
Scalar s=Math::sin(Math::rad(Scalar(30)));
Vector v[3];
v[0]=Vector(-c,-s,Scalar(0));
v[1]=Vector(c,-s,Scalar(0));
v[2]=Vector(Scalar(0),Scalar(1),Scalar(0));
for(int i=0;i<3;++i)
vertexOffsets[i]=v[i]*vertexRadius;
/* Recalculate render vertices and normals: */
Point bottom=Point(Scalar(0),Scalar(0),-width);
Point top=Point(Scalar(0),Scalar(0),width);
for(int i=0;i<3;++i)
{
renderVertices[i]=bottom+v[i]*radius;
renderVertices[3+i]=top+v[i]*radius;
}
for(int i=0;i<3;++i)
renderNormals[i]=-v[(i+2)%3];
renderNormals[3]=Vector(Scalar(0),Scalar(0),Scalar(-1));
renderNormals[4]=Vector(Scalar(0),Scalar(0),Scalar(1));
}
void Triangle::initClass(const Misc::ConfigurationFileSection& configFileSection)
{
/* Read class settings: */
radius=configFileSection.retrieveValue<Scalar>("./radius",1.550185);
radius2=Math::sqr(radius);
width=configFileSection.retrieveValue<Scalar>("./width",0.25);
mass=configFileSection.retrieveValue<Scalar>("./mass",1.0);
/* Calculate triangle shape: */
calculateShape();
/* Create triangle renderer: */
unitRenderer=new TriangleRenderer;
}
void Triangle::deinitClass(void)
{
/* Destroy triangle renderer: */
delete unitRenderer;
unitRenderer=0;
}
void Triangle::setRadius(Scalar newRadius)
{
/* Set radius: */
radius=newRadius;
radius2=Math::sqr(radius);
/* Re-calculate triangle shape: */
calculateShape();
}
void Triangle::setWidth(Scalar newWidth)
{
width=newWidth;
/* Re-calculate triangle shape: */
calculateShape();
}
void Triangle::setMass(Scalar newMass)
{
mass=newMass;
}
void Triangle::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 Triangle::applyCentralForce(const Vector& force,Scalar timeStep)
{
/* Apply linear acceleration: */
linearVelocity+=force*(timeStep/mass);
}
void Triangle::glRenderAction(GLContextData& contextData) const
{
/* Retrieve data item from GL context: */
TriangleRenderer::DataItem* dataItem=contextData.retrieveDataItem<TriangleRenderer::DataItem>(unitRenderer);
/* Move model coordinate system to triangle's position and orientation: */
glPushMatrix();
glTranslate(position.getComponents());
glRotate(orientation);
/* Render triangle: */
glCallList(dataItem->displayListId);
/* Reset model coordinates: */
glPopMatrix();
}
}