summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/Renderers/NXBallAndStickOpenGLRenderer.cpp
blob: 565e5153fc7439b3a16b65468f1dc0579e9dc762 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#include "NXBallAndStickOpenGLRenderer.h"
#include <Nanorex/Interface/NXBondData.h>
#include <Nanorex/Interface/NXNanoVisionResultCodes.h>
#include "../GLT/guarded_gl_ops.h"
#include "../GLT/glt_error.h"

#include <iostream>



using namespace std;
using namespace Nanorex;

// utility function
inline void guarded_delete(NXSGOpenGLNode* &ptr)
{
	if(ptr != (NXSGOpenGLNode*) NULL) {
		delete ptr;
		ptr = (NXSGOpenGLNode*) NULL;
	}
}


// static data
double const NXBallAndStickOpenGLRenderer::BOND_WIDTH(0.5);

// .............................................................................

/* CONSTRUCTOR */
NXBallAndStickOpenGLRenderer::
NXBallAndStickOpenGLRenderer(NXRenderingEngine *parentEngine)
: NXOpenGLRendererPlugin(parentEngine),
canonicalSphereNode(NULL),
canonicalSphereNodeGuard(),
canonicalCylinderNode(NULL),
canonicalCylinderNodeGuard(),
atomicRadiusMap()
{
	for(int bondType=SINGLE_BOND; bondType<NUM_BOND_TYPES; ++bondType)
		canonicalBondNode[bondType] = (NXSGOpenGLNode*) NULL;
	
	initializeAtomicRadiusMap();
}

// .............................................................................

/* DESTRUCTOR */
NXBallAndStickOpenGLRenderer::~NXBallAndStickOpenGLRenderer()
{
	if(isInitialized())
		cleanup(); // will reset the 'initialize' flag
}

// .............................................................................

void NXBallAndStickOpenGLRenderer::initializeAtomicRadiusMap(void)
{
	atomicRadiusMap[0] = 1.1;
	atomicRadiusMap[1] = 1.2;
	atomicRadiusMap[2] = 1.4;
	atomicRadiusMap[3] = 4.0;
	atomicRadiusMap[4] = 3.0;
	atomicRadiusMap[5] = 2.0;
	atomicRadiusMap[6] = 1.84;
	atomicRadiusMap[7] = 1.55;
	atomicRadiusMap[8] = 1.74;
	atomicRadiusMap[9] = 1.65;
	atomicRadiusMap[10] = 1.82;
	atomicRadiusMap[11] = 4.0;
	atomicRadiusMap[12] = 3.0;
	atomicRadiusMap[13] = 2.5;
	atomicRadiusMap[14] = 2.25;
	atomicRadiusMap[15] = 2.11;
	atomicRadiusMap[16] = 2.11;
	atomicRadiusMap[17] = 2.03;
	atomicRadiusMap[18] = 1.88;
	atomicRadiusMap[19] = 5.0;
	atomicRadiusMap[20] = 4.0;
	atomicRadiusMap[21] = 3.7;
	atomicRadiusMap[22] = 3.5;
	atomicRadiusMap[23] = 3.3;
	atomicRadiusMap[24] = 3.1;
	atomicRadiusMap[25] = 3.0;
	atomicRadiusMap[26] = 3.0;
	atomicRadiusMap[27] = 3.0;
	atomicRadiusMap[28] = 3.0;
	atomicRadiusMap[29] = 3.0;
	atomicRadiusMap[30] = 2.9;
	atomicRadiusMap[21] = 2.7;
	atomicRadiusMap[32] = 2.5;
	atomicRadiusMap[33] = 2.2;
	atomicRadiusMap[34] = 2.1;
	atomicRadiusMap[35] = 2.0;
	atomicRadiusMap[36] = 1.9;
	atomicRadiusMap[51] = 2.2;
	atomicRadiusMap[52] = 2.1;
	atomicRadiusMap[53] = 2.0;
	atomicRadiusMap[54] = 1.9;
	
	atomicRadiusMap[200] = 5.0;
	atomicRadiusMap[201] = 4.0;
	atomicRadiusMap[202] = 3.2;
	atomicRadiusMap[203] = 4.0;
	atomicRadiusMap[204] = 3.5;
	atomicRadiusMap[205] = 3.0;
	atomicRadiusMap[206] = 2.5;
	atomicRadiusMap[207] = 4.0;
	atomicRadiusMap[208] = 5.0;
	atomicRadiusMap[209] = 5.0;
	atomicRadiusMap[210] = 2.5;
	atomicRadiusMap[211] = 2.5;
	atomicRadiusMap[212] = 2.5;
	
	atomicRadiusMap[300] = 4.5;
	atomicRadiusMap[301] = 4.5;
	atomicRadiusMap[302] = 3.0;
	atomicRadiusMap[303] = 4.5;
	atomicRadiusMap[304] = 4.5;
	atomicRadiusMap[305] = 4.5;
	atomicRadiusMap[306] = 3.0;
	atomicRadiusMap[307] = 4.5;
	atomicRadiusMap[310] = 2.3;
	atomicRadiusMap[311] = 2.3;
	atomicRadiusMap[312] = 2.3;
}

// .............................................................................

NXRendererPlugin*
NXBallAndStickOpenGLRenderer::newInstance(NXRenderingEngine *parentEngine) const
{
	NXBallAndStickOpenGLRenderer* newRenderer =
		new NXBallAndStickOpenGLRenderer(parentEngine);
	return static_cast<NXRendererPlugin*>(newRenderer);
}

// .............................................................................

/// Initialize the plugin. If not successful, as indicated by the return
/// command-result, then the instance is left partially initialized.
/// The user is then expected to destroy the instance to ensure proper cleanup
NXCommandResult const *const NXBallAndStickOpenGLRenderer::initialize(void)
{
	commandResult.setResult((int) NX_CMD_SUCCESS);
	
	// using NXRendererPlugin::initialized
	
	initialized = initializeCanonicalGeometryNodes();
	if(initialized) {
		initialized = initializeCanonicalBondNodes();
		if(initialized) {
			for(int bondType = (int)SINGLE_BOND;
			    bondType < (int) NUM_BOND_TYPES;
			    ++bondType)
			{
				bool const addedChild =
					canonicalBondNodeGuard[bondType].
						addChild(canonicalBondNode[bondType]);
				assert(canonicalBondNodeGuard[bondType].getRefCount() == 0);
				assert(canonicalBondNodeGuard[bondType].getNumChildren() == 1);
	            initialized = initialized && addedChild;
			}
		}
	}
	
	if(!initialized)
		cleanup(); // will again reset the 'initialized' flag
	
    // The failure location, if any, should have recorded the error
	return &commandResult;
}

// .............................................................................

/// Renders the canonical sphere and cylinder in the parent engine's OpenGL
/// context
bool NXBallAndStickOpenGLRenderer::initializeCanonicalGeometryNodes(void)
{
	bool success = true;
	
	success = initializeCanonicalSphereNode();
	if(!success)
		return false;
	
	success = initializeCanonicalCylinderNode();
	if(!success)
		return false;
	
	// assign a minimum ref count of 1. If all nodes that access these canonical
	// nodes as parents cleanup, then these nodes will be deleted when their
	// ref count becomes zero. This initial increment will ensure that the
	// ref count is at least one if all other nodes behave correctly
	bool const guardedSphereNode =
		canonicalSphereNodeGuard.addChild(canonicalSphereNode);
	assert(canonicalSphereNode->getRefCount() == 1);
	assert(canonicalSphereNodeGuard.getNumChildren() == 1);
	
	bool const guardedCylinderNode =
		canonicalCylinderNodeGuard.addChild(canonicalCylinderNode);
	assert(canonicalCylinderNode->getRefCount() == 1);
	assert(canonicalCylinderNodeGuard.getNumChildren() == 1);
	
	if(!guardedSphereNode || !guardedCylinderNode) {
		SetError(commandResult,
		         "Could not set up canonical sphere/cylinder node guards");
	}
	
	success = guardedSphereNode && guardedCylinderNode;
	return success;
}

// .............................................................................

/// Encapsulates an OpenGL display list for rendering a unit sphere at the
/// origin in an OpenGL-scenegraph and sets canonicalSphereNode to point to it.
/// Returns true if successful. Upon failure, it releases any resources claimed,
/// sets canonicalSphereNode to NULL and returns false.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalSphereNode(void)
{
    // quick return if node is already created
	if(canonicalSphereNode != NULL)
		return true;
	
	try {
		canonicalSphereNode = new NXSGOpenGLRenderable;
#ifdef NX_DEBUG
		canonicalSphereNode->setName("CanonicalSphere");
#endif
	}
	catch (...) {
        // fail silently if unable to create for any reason
		canonicalSphereNode = NULL;
		SetError(commandResult, "Could not allocated sphere-scenegraph node");
		return false;
	}
	
	if(canonicalSphereNode == NULL) {
		SetError(commandResult, "Could not allocated sphere-scenegraph node");
		return false;
	}
	
	bool beginRenderOK = canonicalSphereNode->beginRender();
	if(!beginRenderOK) {
		NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
		commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
		commandResult.setParamVector(scenegraphCtxtError->getParamVector());
		delete canonicalSphereNode;
		canonicalSphereNode = NULL;
		return false;
	}
	
	drawOpenGLCanonicalSphere();
	
	bool const ok = (commandResult.getResult() == (int) NX_CMD_SUCCESS);
	if(!ok) {
		delete canonicalSphereNode;
		canonicalSphereNode = NULL;
		return false;
	}
	
	bool endRenderOK = canonicalSphereNode->endRender();
	if(!endRenderOK) {
		NXCommandResult *scenegraphCtxtError =
			NXSGOpenGLNode::GetCommandResult();
		commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
		commandResult.setParamVector(scenegraphCtxtError->getParamVector());
		delete canonicalSphereNode;
		canonicalSphereNode = NULL;
		return false;
	}
	
	return true;
}

// .............................................................................

/// Issues OpenGL commands to draw a sphere
void NXBallAndStickOpenGLRenderer::drawOpenGLCanonicalSphere(void)
{
	const double r = 1.0; /* radius */
	double theta, rSinTheta, phi, sinPhi, cosPhi;
	double theta2, rSinTheta2;
	GLint iTheta, iPhi;
	GLdouble x,y,z,z1,z2;
	
	const int ALPHA = 5;
	
    /* Automatic normalization of normals */
	GUARDED_GL_OP(glEnable(GL_NORMALIZE));
	
    /* Top cap - draw triangles instead of quads */
	GUARDED_GL_OP(glBegin(GL_TRIANGLE_FAN));
    /* Top pole */
	GUARDED_GL_OP(glNormal3d(0,0,1));
	GUARDED_GL_OP(glVertex3d(0,0,r));
	theta = ALPHA * M_PI/180.0;
	rSinTheta = r*sin(theta);
	z = r*cos(theta);
	
	for(iPhi = 0; iPhi <= 360; iPhi += ALPHA) {
		phi = M_PI/180.0 * (GLdouble) iPhi;
		x = rSinTheta*cos(phi);
		y = rSinTheta*sin(phi);
        /* normal to point on sphere is ray from center to point */
		GUARDED_GL_OP(glNormal3d(x, y, z));
		GUARDED_GL_OP(glVertex3d(x, y, z));
	}
	GUARDED_GL_OP(glEnd());
	
    /* Sphere body - draw quad strips */
	for(iTheta = ALPHA; iTheta <= 180-(2*ALPHA); iTheta += ALPHA) {
		theta = M_PI/180.0 * (double) iTheta;
		theta2 = M_PI/180.0 * (double) (iTheta+10);
		z1 = (GLdouble) (r*cos(theta));
		z2 = (GLdouble) (r*cos(theta2));
		rSinTheta = r*sin(theta);
		rSinTheta2 = r*sin(theta2);
		GUARDED_GL_OP(glBegin(GL_QUAD_STRIP));
		for(iPhi = 0; iPhi <= 360; iPhi += 10) {
			phi = M_PI/180.00 * (double)(iPhi);
			cosPhi = cos(phi);
			sinPhi = sin(phi);
			x = (GLdouble) (rSinTheta*cosPhi);
			y = (GLdouble) (rSinTheta*sinPhi);
			GUARDED_GL_OP(glNormal3d(x, y, z1));
			GUARDED_GL_OP(glVertex3d(x, y, z1));
			x = (GLdouble) (rSinTheta2*cosPhi);
			y = (GLdouble) (rSinTheta2*sinPhi);
			GUARDED_GL_OP(glNormal3d(x, y, z2));
			GUARDED_GL_OP(glVertex3d(x, y, z2));
		}
		GUARDED_GL_OP(glEnd());
	}
	
    /* Bottom cap - draw triangle fan */
	iTheta = 180-ALPHA;
	theta = M_PI/180.0 * (GLdouble) iTheta;
	z = r*cos(theta);
	rSinTheta = r*sin(theta);
	GUARDED_GL_OP(glBegin(GL_TRIANGLE_FAN));
    /* Bottom pole */
	GUARDED_GL_OP(glNormal3d(0,0,-1));
	GUARDED_GL_OP(glVertex3d(0,0,-r));
	for(iPhi = 0; iPhi <= 360; iPhi += ALPHA) {
		phi = M_PI/180.0 * (GLdouble) iPhi;
		x = rSinTheta*cos(phi);
		y = rSinTheta*sin(phi);
		GUARDED_GL_OP(glNormal3d(x, y, z));
		GUARDED_GL_OP(glVertex3d(x, y, z));
	}
	GUARDED_GL_OP(glEnd());
	
	
	ostringstream errMsgStream;
	GLenum const err = GLERROR(errMsgStream);
	if(err == GL_NO_ERROR) {
		commandResult.setResult(NX_CMD_SUCCESS);
	}
	else {
		SetError(commandResult,
		         ("Error drawing openGL unit sphere"+errMsgStream.str()).c_str());
	}
}

// .............................................................................

/// Encapsulates an OpenGL display list for rendering a unit cylinder with axis
/// along the z-axis, one cap on the z=0 plane and the other on the z=1 plane,
/// and sets canonicalCylinderNode to point to it.
/// Returns true if successful. Upon failure, it deallocates any resources 
/// claimed, sets canonicalCylinderNode to NULL and returns false.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalCylinderNode(void)
{
	// quick return if already initialized
	if(canonicalCylinderNode != NULL)
		return true;
	
	try {
		canonicalCylinderNode = new NXSGOpenGLRenderable;
#ifdef NX_DEBUG
		canonicalCylinderNode->setName("CanonicalCylinder");
#endif
	}
	catch (...) {
        // fail if unable to create for any reason
		canonicalCylinderNode = NULL;
		SetError(commandResult, "Could not allocate canonical cylinder OpenGL "
		         "scenegraph node");
		return false;
	}
	
    // extra check for NULL before performing ops on it
	if(canonicalCylinderNode == NULL) {
        // fail if unable to create for any reason
		canonicalCylinderNode = NULL;
		SetError(commandResult, "Could not allocate canonical cylinder OpenGL "
		         "scenegraph node");
		return false;
	}
	
	bool beginRenderOK = canonicalCylinderNode->beginRender();
	if(!beginRenderOK) {
		NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
		commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
		commandResult.setParamVector(scenegraphCtxtError->getParamVector());
		delete canonicalCylinderNode;
		canonicalCylinderNode = NULL;
		return false;
	}
	
	drawOpenGLCanonicalCylinder();
	
	bool const ok = (commandResult.getResult() == (int) NX_CMD_SUCCESS);
	if(!ok) {
		delete canonicalCylinderNode;
		canonicalCylinderNode = NULL;
		return false;
	}
	
	
	bool endRenderOK = canonicalCylinderNode->endRender();
	if(!endRenderOK) {
		NXCommandResult *scenegraphCtxtError = NXSGOpenGLNode::GetCommandResult();
		commandResult.setResult(NX_PLUGIN_REPORTS_ERROR);
		commandResult.setParamVector(scenegraphCtxtError->getParamVector());
		delete canonicalCylinderNode;
		canonicalCylinderNode = NULL;
		return false;
	}
	
#ifdef NX_DEBUG
	// cout << "canonicalCylinderNode scenegraph:" << endl;
	// canonicalCylinderNode->writeDotGraph(cout);
#endif
	return true;
}

// .............................................................................

/// Issues OpenGL commands to render a canonical cylinder
void NXBallAndStickOpenGLRenderer::drawOpenGLCanonicalCylinder(void)
{
	int const NUM_FACETS = 72;
	const double DELTA_PHI = 360.0 / (double) NUM_FACETS;
	GLdouble vertex[NUM_FACETS][2]; // store (x,y) values
	double x=0.0, y=0.0;
    // GLdouble const z = 0.5;
	
    // generate vertices
	int iFacet = 0; // counter
	double phi = 0.0;
	for(iFacet=0; iFacet<NUM_FACETS; ++iFacet, phi+=DELTA_PHI) {
		double const phi_rad = M_PI/180.0 * phi;
#ifdef _GNU_SOURCE
		sincos(phi_rad, &y, &x);
#else
		x = cos(phi_rad);
		y = sin(phi_rad);
#endif
		vertex[iFacet][0] = (GLdouble)(x);
		vertex[iFacet][1] = (GLdouble)(y);
	}
	
	
    /* Automatic normalization of normals */
	GUARDED_GL_OP(glEnable(GL_NORMALIZE));
	
    /* Fill polygons */
	GUARDED_GL_OP(glShadeModel(GL_SMOOTH));
	GUARDED_GL_OP(glPolygonMode(GL_FRONT, GL_FILL));
	
    /* Top cap - draw triangles instead of quads */
	GUARDED_GL_OP(glBegin(GL_TRIANGLE_FAN));
	GUARDED_GL_OP(glNormal3d(0,0,1));
	GUARDED_GL_OP(glVertex3d(0.0, 0.0, 1.0));
	for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
		GUARDED_GL_OP(glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 1.0));
	}
    // close top-cap
	GUARDED_GL_OP(glVertex3d(vertex[0][0], vertex[0][1], 1.0));
	GUARDED_GL_OP(glEnd());
	
	
    /* Cylinder body - draw triangle strips */
	GUARDED_GL_OP(glBegin(GL_TRIANGLE_STRIP));
	for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
		GUARDED_GL_OP(glNormal3d(vertex[iFacet][0], vertex[iFacet][1], 0.0));
		GUARDED_GL_OP(glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 1.0));
		GUARDED_GL_OP(glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 0.0));
	}
    // close the side surface
	GUARDED_GL_OP(glNormal3d(vertex[0][0], vertex[0][1], 0.0));
	GUARDED_GL_OP(glVertex3d(vertex[0][0], vertex[0][1], 1.0));
	GUARDED_GL_OP(glVertex3d(vertex[0][0], vertex[0][1], 0.0));
	GUARDED_GL_OP(glEnd());
	
	
	
    /* Bottom cap - draw triangle fan */
	GUARDED_GL_OP(glBegin(GL_TRIANGLE_FAN));
	/* Bottom pole */
	GUARDED_GL_OP(glNormal3d(0,0,-1));
	GUARDED_GL_OP(glVertex3d(0.0, 0.0, 0.0));
	for(iFacet=0; iFacet<NUM_FACETS; ++iFacet) {
		GUARDED_GL_OP(glVertex3d(vertex[iFacet][0], vertex[iFacet][1], 0.0));
	}
    // close bottom-cap
	GUARDED_GL_OP(glVertex3d(vertex[0][0], vertex[0][1], 0.0));
	GUARDED_GL_OP(glEnd());
	
	
	ostringstream errMsgStream;
	GLenum const err = GLERROR(errMsgStream);
	if(err == GL_NO_ERROR) {
		commandResult.setResult(NX_CMD_SUCCESS);
	}
	else {
		SetError(commandResult,
		         ("Error drawing openGL unit cylinder"+errMsgStream.str()).c_str());
	}
}

// .............................................................................

/// Creates scenegraph nodes for different kinds of bonds
bool NXBallAndStickOpenGLRenderer::initializeCanonicalBondNodes(void)
{
	bool success = true;
    success = success && initializeCanonicalSingleBondNode();
	success = success && initializeCanonicalDoubleBondNode();
	success = success && initializeCanonicalTripleBondNode();
	success = success && initializeCanonicalAromaticBondNode();
	success = success && initializeCanonicalCarbomericBondNode();
	success = success && initializeCanonicalGraphiticBondNode();
	return success;	
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a single bond. On success, sets
/// canonicalBondNode[SINGLE_BOND] to point to it and returns true. On failure,
/// releases any resources acquired, sets canonicalBondNode[SINGLE_BOND] to NULL,
/// and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalSingleBondNode(void)
{
    if(canonicalBondNode[SINGLE_BOND] == NULL) {
        try {
            canonicalBondNode[SINGLE_BOND] =
                new NXSGOpenGLScale(BOND_WIDTH, BOND_WIDTH, 1.0);
#ifdef NX_DEBUG
	        canonicalBondNode[SINGLE_BOND]->setName("SingleBond_Scale");
#endif
        }
        catch(...) {
            canonicalBondNode[SINGLE_BOND] = NULL;
	        SetError(commandResult, "Could not allocated single-bond OpenGL "
	                 "scenegraph node");
            return false;
        }
#ifdef NX_DEBUG
	    // canonicalCylinderNode->writeDotGraph(cerr);
#endif
	    assert(canonicalCylinderNode->getNumChildren()==0);
	    assert(canonicalBondNode[SINGLE_BOND]->getNumChildren()==0);
        bool ok =
		    canonicalBondNode[SINGLE_BOND]->addChild(canonicalCylinderNode);
        if(!ok) {
            delete canonicalBondNode[SINGLE_BOND];
            canonicalBondNode[SINGLE_BOND] = NULL;
	        SetError(commandResult, "Allocated single-bond OpenGL scenegraph "
	                 "node but could not parent it");
	        return false;
        }
    }
	return true;
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a double bond. On success, sets
/// canonicalBondNode[DOUBLE_BOND] to point to it and returns true. On failure,
/// releases any resources acquired, sets canonicalBondNode[DOUBLE_BOND] to NULL,
/// and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalDoubleBondNode(void)
{
	bool doubleBondOK = true;
	if(canonicalBondNode[DOUBLE_BOND] == NULL &&
       canonicalBondNode[SINGLE_BOND] != NULL)
    {
        NXSGOpenGLNode *translateNode1 = NULL;
        NXSGOpenGLNode *translateNode2 = NULL;
        try {
            translateNode1 = new NXSGOpenGLTranslate(BOND_WIDTH, 0.0, 0.0);
            translateNode2 = new NXSGOpenGLTranslate(-BOND_WIDTH, 0.0, 0.0);
            canonicalBondNode[DOUBLE_BOND] = new NXSGOpenGLNode;
#ifdef NX_DEBUG
	        canonicalBondNode[DOUBLE_BOND]->setName("DoubleBond");
#endif
        }
        catch(...) {
            doubleBondOK = false;
        }
        if(doubleBondOK) {
            bool ok1 = translateNode1->addChild(canonicalBondNode[SINGLE_BOND]);
            bool ok2 = translateNode2->addChild(canonicalBondNode[SINGLE_BOND]);
            if(ok1 && ok2) {
                bool ok3 = canonicalBondNode[DOUBLE_BOND]->addChild(translateNode1);
                bool ok4 = canonicalBondNode[DOUBLE_BOND]->addChild(translateNode2);
                doubleBondOK = ok3 && ok4;
            }
            else {
                doubleBondOK = false;
            }
        }
        
#ifdef NX_DEBUG
	    translateNode1->setName("DoubleBond_TranslateSingle1");
	    translateNode2->setName("DoubleBond_TranslateSingle2");
#endif
	    
	    // if any of the above steps failed, release partially allocated mem
        if(!doubleBondOK) {
            if(translateNode1 != NULL)
                delete translateNode1;
            if(translateNode2 != NULL)
                delete translateNode2;
            if(canonicalBondNode[DOUBLE_BOND] != NULL) {
                delete canonicalBondNode[DOUBLE_BOND];
                canonicalBondNode[DOUBLE_BOND] = NULL;
            }
        }
    }
	
	return doubleBondOK;
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a triple bond. On success, sets
/// canonicalBondNode[TRIPLE_BOND] to point to it and returns true. On failure,
/// releases any resources acquired, sets canonicalBondNode[TRIPLE_BOND] to NULL,
/// and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalTripleBondNode(void)
{
	bool tripleBondOK = true;
	
	if(canonicalBondNode[TRIPLE_BOND] == NULL &&
       canonicalBondNode[DOUBLE_BOND] != NULL &&
       canonicalBondNode[SINGLE_BOND] != NULL)
    {
        NXSGOpenGLNode *translateNode1 = NULL;
        NXSGOpenGLNode *translateNode2 = NULL;
        try {
            translateNode1 = new NXSGOpenGLTranslate(1.5*BOND_WIDTH, 0.0, 0.0);
            translateNode2 =
		        new NXSGOpenGLTranslate(1.5*(-BOND_WIDTH), 0.0, 0.0);
            canonicalBondNode[TRIPLE_BOND] = new NXSGOpenGLNode;
#ifdef NX_DEBUG
	        canonicalBondNode[TRIPLE_BOND]->setName("TripleBond_Group");
	        translateNode1->setName("TripleBond_TranslateSingle1");
	        translateNode2->setName("TripleBond_TranslateSingle2");
#endif
        }
        catch(...) {
            tripleBondOK = false;
        }
        if(tripleBondOK) {
            bool ok1 = translateNode1->addChild(canonicalBondNode[SINGLE_BOND]);
            bool ok2 = translateNode2->addChild(canonicalBondNode[SINGLE_BOND]);
            if(ok1 && ok2) {
                bool ok3 =
		            canonicalBondNode[TRIPLE_BOND]->addChild(translateNode1);
                bool ok4 =
		            canonicalBondNode[TRIPLE_BOND]->addChild(translateNode2);
                bool ok5 =
                    canonicalBondNode[TRIPLE_BOND]->
		            addChild(canonicalBondNode[SINGLE_BOND]);
                tripleBondOK = ok3 && ok4 && ok5;
            }
            else {
                tripleBondOK = false;
            }
        }
        
	    // if any of the above steps failed, release partially allocated mem
        if(!tripleBondOK) {
            if(translateNode1 != NULL)
                delete translateNode1;
            if(translateNode2 != NULL)
                delete translateNode2;
            if(canonicalBondNode[TRIPLE_BOND] != NULL) {
                delete canonicalBondNode[TRIPLE_BOND];
                canonicalBondNode[TRIPLE_BOND] = NULL;
            }
        }
    }
	
	return tripleBondOK;
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a aromatic bond. On success, sets
/// canonicalBondNode[AROMATIC_BOND] to point to it and returns true. On failure,
/// releases any resources acquired, sets canonicalBondNode[AROMATIC_BOND] to
/// NULL, and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalAromaticBondNode(void)
{
    /// @todo check NE-1 for aromatic bond representation and implement
    /// ref. bond_drawer.py
        
	bool success = true;
    try {
        canonicalBondNode[AROMATIC_BOND] = new NXSGOpenGLNode;
#ifdef NX_DEBUG
	    canonicalBondNode[AROMATIC_BOND]->setName("AromaticBond");
#endif
	    if(!canonicalBondNode[AROMATIC_BOND]->
           addChild(canonicalBondNode[SINGLE_BOND]))
        {
            delete canonicalBondNode[AROMATIC_BOND];
            canonicalBondNode[AROMATIC_BOND] = NULL;
        }
    }
    catch(...) {
	    success = false;
	    SetError(commandResult, "Failed to allocate or parent aromatic bond "
	             "OpenGL scenegraph node");
	    guarded_delete(canonicalBondNode[AROMATIC_BOND]);
    }
	return success;
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a carbomeric bond. On success, sets
/// canonicalBondNode[CARBOMERIC_BOND] to point to it and returns true. On
/// failure, releases any resources acquired, sets
/// canonicalBondNode[CARBOMERIC_BOND] to NULL, and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalCarbomericBondNode(void)
{
    /// @todo check NE-1 for carbomeric bond representation and implement
    /// ref. bond_drawer.py
        
	bool success = true;
    try {
        canonicalBondNode[CARBOMERIC_BOND] = new NXSGOpenGLNode;
#ifdef NX_DEBUG
	    canonicalBondNode[CARBOMERIC_BOND]->setName("CarbomericBond");
#endif
	    if(!canonicalBondNode[CARBOMERIC_BOND]->
           addChild(canonicalBondNode[SINGLE_BOND]))
        {
            delete canonicalBondNode[CARBOMERIC_BOND];
            canonicalBondNode[CARBOMERIC_BOND] = NULL;
        }
    }
    catch(...) {
	    success = false;
	    SetError(commandResult, "Failed to allocate or parent carbomeric bond "
	             "OpenGL scenegraph node");
	    guarded_delete(canonicalBondNode[CARBOMERIC_BOND]);
    }
	return success;
}

// .............................................................................

/// Creates an OpenGL scenegraph node for a graphitic bond. On success, sets
/// canonicalBondNode[GRAPHITIC_BOND] to point to it and returns true. On
/// failure, releases any resources acquired, sets
/// canonicalBondNode[GRAPHITIC_BOND] to NULL, and returns true.
bool NXBallAndStickOpenGLRenderer::initializeCanonicalGraphiticBondNode(void)
{
    /// @todo check NE-1 for graphitic bond representation and implement
    /// ref. bond_drawer.py
        
	bool success = true;
    try {
        canonicalBondNode[GRAPHITIC_BOND] = new NXSGOpenGLNode;
#ifdef NX_DEBUG
	    canonicalBondNode[GRAPHITIC_BOND]->setName("GraphiticBond");
#endif
        if(!canonicalBondNode[GRAPHITIC_BOND]->
           addChild(canonicalBondNode[SINGLE_BOND]))
        {
            delete canonicalBondNode[GRAPHITIC_BOND];
            canonicalBondNode[GRAPHITIC_BOND] = NULL;
        }
    }
    catch(...) {
	    success = false;
	    SetError(commandResult, "Failed to allocate or parent graphitic bond "
	             "OpenGL scenegraph node");
	    guarded_delete(canonicalBondNode[GRAPHITIC_BOND]);
    }
	return success;
}

// .............................................................................

/// Release (partially-) allocated resources if initialized. Afterwards,
/// set the initialized flag to false.
NXCommandResult const *const NXBallAndStickOpenGLRenderer::cleanup(void)
{
	NXRendererPlugin::ClearResult(commandResult);
	if(initialized) {
		// guarded_delete(canonicalSphereNode);
		// guarded_delete(canonicalCylinderNode);
		
		/// @note no need to delete bond pointers because bond-guards will
		/// delete them when they get destructed. The single-bond node is 
		/// heavily referenced by all other bond-nodes and therefore it alone
		/// will have a ref-count > 1. All other should have a ref-count == 1
		for(int bondType = (int)GRAPHITIC_BOND;
		    bondType > (int)SINGLE_BOND;
		    --bondType)
		{
			// guarded_delete(canonicalBondNode[bondType]);
			assert(canonicalBondNode[bondType]->getRefCount() == 1);
		}
		initialized = false;
	}
	return &commandResult;
}

// .............................................................................

/// It is assumed that the plugin was initialized successfully and that the
/// developer has trapped any errors
NXSGOpenGLNode*
    NXBallAndStickOpenGLRenderer::renderAtom(NXAtomData const& info)
{
    std::vector<void const*> const& paramVec = info.getSupplementalData();
    NXOpenGLMaterial const& defaultMaterial =
        *static_cast<NXOpenGLMaterial const*>(paramVec[0]);
    NXSGOpenGLMaterial *atomNode = NULL;
    try {
        atomNode = new NXSGOpenGLMaterial(defaultMaterial);
    }
    catch (...) {
        SetError(commandResult, "Could not create node for rendering atom");
        return NULL;
    } // fail silently
    
	NXSGOpenGLScale *atomScaleNode;
	try {
		int atomicNum = info.getAtomicNum();
		// cerr << "NXBallAndStickOpenGLRenderer: rendering atomicNum = " << atomicNum << endl;
		double radius = atomicRadiusMap[atomicNum] * 0.25;
		atomScaleNode = new NXSGOpenGLScale(radius, radius, radius);
	}
	catch (...) {
		SetError(commandResult, "Could not create node for rendering atom");
		return NULL;
	} // fail silently
	
	if(!atomNode->addChild(atomScaleNode)) {
		SetError(commandResult,
		         "Created scenegraph node for atom but could not scale it");
		delete atomNode;
		return NULL;
	}
	
    if(!atomScaleNode->addChild(canonicalSphereNode)) {
        SetError(commandResult,
                 "Created scenegraph node for atom but could not include it");
        delete atomNode;
        return NULL;
    }
    
    return atomNode;
}

// .............................................................................

NXSGOpenGLNode*
	NXBallAndStickOpenGLRenderer::renderBond(NXBondData const& info)
{
    std::vector<void const*> const& paramVec = info.getSupplementalData();
    NXOpenGLMaterial const& defaultMaterial =
        *static_cast<NXOpenGLMaterial const*>(paramVec[0]);
    NXSGOpenGLMaterial *bondNode = NULL;
    try {
        bondNode = new NXSGOpenGLMaterial(defaultMaterial);
    }
    catch (...) {
        SetError(commandResult,
                 "Could not create bond scenegraph node");
        return NULL;
    } // fail silently
    
    NXSGOpenGLScale *bondScale = NULL;
    try {
	    double const bondLength = info.getLength();
	    // cerr << "bond-length = " << bondLength << endl;
        bondScale = new NXSGOpenGLScale(0.25, 0.25, bondLength);
    }
    catch(...) {
        SetError(commandResult,
                 "Could not create scaling scenegraph node for bond-length");
        delete bondNode;
        return NULL;
    }
    
    if(!bondNode->addChild(bondScale)) {
        SetError(commandResult,
                 "Created scenegraph nodes for bond but could not include them");
        delete bondNode;
        return NULL;
    }
    
    // note x-y displacements are not affected by z-scaling for length
    
    // single bond
    if(!bondScale->addChild(canonicalBondNode[info.getOrder()-1])) {
        SetError(commandResult,
                 "Error including canonical bond node in bond scenegraph");
        delete bondNode;
        return NULL;
    }
    return bondNode;
}

// .............................................................................
#if 0
/// @fixme r1.0.0 hacks
	// -- begin hacks --
NXSGOpenGLNode* NXBallAndStickOpenGLRenderer::renderDnaSegment(/*TODO*/)
{
	/// @todo implement
}
#endif
// .............................................................................

#if 0
NXSGOpenGLNode* NXBallAndStickOpenGLRenderer::renderDnaStrand(/*TODO*/)
{
	/// @todo implement
}
#endif

// -- end hacks --


Q_EXPORT_PLUGIN2(NXBallAndStickOpenGLRenderer, NXBallAndStickOpenGLRenderer)