File: cylinder.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 7,337
  • sloc: cpp: 72,977; sh: 11,381; xml: 6,304; makefile: 1,663; ansic: 1,061
file content (267 lines) | stat: -rw-r--r-- 7,163 bytes parent folder | download | duplicates (4)
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
/**********************************************************************
  Cylinder - OpenGL Cylinder drawing class.

  Copyright (C) 2007-2009 Jean Brefort <jean.brefort@normalesup.org>
  Copyright (C) 2006,2007 Benoit Jacob <jacob@math.jussieu.fr>

  This file is part of the Avogadro molecular editor project.
  For more information, see <http://avogadro.sourceforge.net/>

  Avogadro 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 3 of the License, or
  (at your option) any later version.

  Avogadro 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 this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
 **********************************************************************/

#include "config.h"
#include "cylinder.h"
#include "matrix.h"
#include"vector.h"
#include <GL/gl.h>
#include <cmath>

namespace gcu {

class CylinderPrivate {
public:
	CylinderPrivate () : vertexBuffer (0), normalBuffer (0), displayList (0), isValid (false) {}

	/** Pointer to the buffer storing the vertex array */
	Vector3f *vertexBuffer;
	/** Pointer to the buffer storing the normal array */
	Vector3f *normalBuffer;
	/** The id of the OpenGL display list */
	GLuint displayList;
	/** Equals true if the vertex array has been correctly initialized */
	bool isValid;

	/** the number of faces of the cylinder. This only
	* includes the lateral faces, as the base and top faces (the
	* two discs) are not rendered. */
	int faces;
};

Cylinder::Cylinder (int faces) : d (new CylinderPrivate)
{
	setup (faces);
}

Cylinder::~Cylinder ()
{
	freeBuffers ();
	if (d->displayList) {
		glDeleteLists( d->displayList, 1 );
	}
	delete d;
}

void Cylinder::freeBuffers ()
{
	if(d->normalBuffer) {
		delete[] d->normalBuffer;
		d->normalBuffer = 0;
	}
	if (d->vertexBuffer) {
		delete[] d->vertexBuffer;
		d->vertexBuffer = 0;
	}
}

void Cylinder::setup (int faces)
{
	if (d->isValid && faces == d->faces)
		return;
	d->faces = faces;
	initialize ();
}

void Cylinder::initialize()
{
	d->isValid = false;
	if (d->faces < 0)
		return;

	// compile display list and free buffers
	if (!d->displayList)
		d->displayList = glGenLists (1);
	if (!d->displayList)
		return;

	if (d->faces < 3)  {
		glNewList (d->displayList, GL_COMPILE);
		glLineWidth (1.0);
		glBegin (GL_LINES);
		glVertex3f (0, 0, 0);
		glVertex3f (0, 0, 1);
		glEnd ();
		glEndList ();
	} else {
		// compute number of vertices
		int vertexCount = 2 * d->faces + 2;

		// deallocate any previously allocated buffer
		freeBuffers ();

		// allocate memory for buffers
		d->vertexBuffer = new Vector3f[vertexCount];
		if (!d->vertexBuffer)
			return;
		d->normalBuffer = new Vector3f[vertexCount];
		if (!d->normalBuffer)
			return;

		float baseAngle = 2 * M_PI / d->faces;
		// build vertex and normal buffers
		for (int i = 0; i <= d->faces; i++) {
			float angle = baseAngle * i;
			Vector3f v (cosf (angle), sinf (angle), 0.0f);
			d->normalBuffer[2 * i] = v;
			d->normalBuffer[2 * i + 1] = v;
			d->vertexBuffer[2 * i] = v;
			d->vertexBuffer[2 * i + 1] = v;
			d->vertexBuffer[2 * i].GetRefz () = 1.0f;
		}
		glEnableClientState (GL_VERTEX_ARRAY);
		glEnableClientState (GL_NORMAL_ARRAY);
		glNewList (d->displayList, GL_COMPILE);
		glVertexPointer (3, GL_FLOAT, 0, d->vertexBuffer);
		glNormalPointer (GL_FLOAT, 0, d->normalBuffer);
		glDrawArrays (GL_QUAD_STRIP, 0, vertexCount);
		glEndList ();
		glDisableClientState (GL_VERTEX_ARRAY);
		glDisableClientState (GL_NORMAL_ARRAY);
	}
	freeBuffers ();
	d->isValid = true;
}

void Cylinder::draw (const Vector &end1, const Vector &end2, double radius) const
{
	// the "axis vector" of the cylinder
	Vector axis = end2 - end1;

	// construct an orthogonal basis whose first vector is axis, and whose other vectors
	// have norm equal to 'radius'.
	Vector axisNormalized = axis / axis.GetLength ();
	Vector ortho1, ortho2;
	ortho1 = axisNormalized.CreateOrthogonal ();
	ortho1 *= radius;
	ortho2 = axisNormalized.Cross (ortho1);

	// construct the 4D transformation matrix
	GLMatrix matrix;

	matrix (0, 0) = ortho1.GetX ();
	matrix (0, 1) = ortho1.GetY ();
	matrix (0, 2) = ortho1.GetZ ();
	matrix (0, 3) = 0.0;

	matrix (1, 0) = ortho2.GetX ();
	matrix (1, 1) = ortho2.GetY ();
	matrix (1, 2) = ortho2.GetZ ();
	matrix (1, 3) = 0.0;

	matrix (2, 0) = axis.GetX ();
	matrix (2, 1) = axis.GetY ();
	matrix (2, 2) = axis.GetZ ();
	matrix (2, 3) = 0.0;

	matrix (3, 0) = end1.GetX ();
	matrix (3, 1) = end1.GetY ();
	matrix (3, 2) = end1.GetZ ();
	matrix (3, 3) = 1.0;

	//now we can do the actual drawing !
	glPushMatrix ();
	glMultMatrixd (matrix.array ());
	glCallList (d->displayList);
	glPopMatrix ();
}

void Cylinder::drawMulti( const Vector &end1, const Vector &end2,
double radius, int order, double shift,
const Vector &planeNormalVector ) const
{
	// the "axis vector" of the cylinder
	Vector axis = end2 - end1;

	// now we want to construct an orthonormal basis whose first
	// vector is axis.normalized(). We don't use Eigen's loadOrthoBasis()
	// for that, because we want one more thing. The second vector in this
	// basis, which we call ortho1, should be approximately lying in the
	// z=0 plane if possible. This is to ensure double bonds don't look
	// like single bonds from the default point of view.
	double axisNorm = axis.GetLength ();
	if (axisNorm == 0.0)
		return;
	Vector axisNormalized = axis / axisNorm;

	Vector ortho1 = axisNormalized.Cross (planeNormalVector);
	double ortho1Norm = ortho1.GetLength ();
	if (ortho1Norm > 0.001)
		ortho1 /= ortho1Norm;
	else
		axisNormalized = ortho1.CreateOrthogonal ();
	ortho1 *= radius;

	Vector ortho2 = axisNormalized.Cross (ortho1);

	// construct the 4D transformation matrix
	GLMatrix matrix;

	matrix (0, 0) = ortho1.GetX ();
	matrix (0, 1) = ortho1.GetY ();
	matrix (0, 2) = ortho1.GetZ ();
	matrix (0, 3) = 0.0;

	matrix (1, 0) = ortho2.GetX ();
	matrix (1, 1) = ortho2.GetY ();
	matrix (1, 2) = ortho2.GetZ ();
	matrix (1, 3) = 0.0;

	matrix (2, 0) = axis.GetX ();
	matrix (2, 1) = axis.GetY ();
	matrix (2, 2) = axis.GetZ ();
	matrix (2, 3) = 0.0;

	matrix (3, 0) = end1.GetX ();
	matrix (3, 1) = end1.GetY ();
	matrix (3, 2) = end1.GetZ ();
	matrix (3, 3) = 1.0;

	//now we can do the actual drawing !
	glPushMatrix ();
	glMultMatrixd (matrix.array ());
	if (order == 1)
		glCallList (d->displayList);
	else {
		double angleOffset = 0.0;
		if (order >= 3) {
			if (order == 3)
				angleOffset = 90.0;
			else angleOffset = 22.5;
		}

		double displacementFactor = shift / radius;
		for (int i = 0; i < order; i++) {
			glPushMatrix ();
			glRotated (angleOffset + 360.0 * i / order, 0.0, 0.0, 1.0);
			glTranslated (displacementFactor, 0.0, 0.0);
			glCallList (d->displayList);
			glPopMatrix ();
		}
	}
	glPopMatrix ();
}

}