summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Interface/NXRenderingEngine.cpp
blob: 301b57174e4873decdd8f349997565b826ddb829 (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
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#include <Nanorex/Interface/NXRenderingEngine.h>
#include <Nanorex/Interface/NXRendererPlugin.h>
#include <Nanorex/Interface/NXGraphicsManager.h>
#include <Nanorex/Interface/NXNanoVisionResultCodes.h>
#include <string>

using namespace std;

namespace Nanorex {


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

/*static*/
void NXRenderingEngine::SetResult(NXCommandResult& commandResult,
                                  int errCode,
                                  string const& errMsg)
{
	commandResult.setResult(errCode);
	vector<QString> message;
	message.push_back(QObject::tr(errMsg.c_str()));
	commandResult.setParamVector(message);
}

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

/* static */
void NXRenderingEngine::
	ClearResult(NXCommandResult& commandResult)
{
	commandResult.setResult((int) NX_CMD_SUCCESS);
	vector<QString> message;
	commandResult.setParamVector(message);
}

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

/* CONSTRUCTOR */
NXRenderingEngine::NXRenderingEngine()
	: // rendererSet(),
	renderStyleMap(),
	graphicsManager(NULL),
	moleculeSets(),
	frames(),
	currentFrameIndex(-1),
	pluginsInitialized(false),
	commandResult()
{
	ClearResult(commandResult);
}

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

/* DESTRUCTOR */
NXRenderingEngine::~NXRenderingEngine()
{
	// release plugin instances initialized in this context
	map<string, NXRendererPlugin*>::iterator pluginIter;
	for(pluginIter = renderStyleMap.begin();
	    pluginIter != renderStyleMap.end();
	    ++pluginIter)
	{
		string const renderStyleCode = pluginIter->first;
		if(renderStyleCode != "def") {
			NXRendererPlugin *plugin = pluginIter->second;
			delete plugin;
		}
	}
	
	// release frame-memory allocated in this context
	std::vector<NXSGNode*>::iterator frameIter;
	for(frameIter = frames.begin(); frameIter != frames.end(); ++frameIter) {
		NXSGNode *frame = *frameIter;
		delete frame;
	}
}

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

/// Returns NULL if index is out of bounds
NXMoleculeSet *const NXRenderingEngine::getFrame(int idx)
{
	if(idx < 0 || idx >= int(moleculeSets.size()))
		return (NXMoleculeSet*) NULL;
	else
		return moleculeSets[idx];
}

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

NXCommandResult const *const
	NXRenderingEngine::addFrame(NXMoleculeSet *const molSetPtr)
{
	ClearResult(commandResult);
	NXSGNode *newFrame = createSceneGraph(molSetPtr);
	if(newFrame != (NXSGNode*) NULL) {
		moleculeSets.push_back(molSetPtr);
		frames.push_back(newFrame);
	}
	return &commandResult;
}

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

void NXRenderingEngine::setCurrentFrame(int frameIdx)
{
	if(frameIdx < int(frames.size()))
		currentFrameIndex = frameIdx;
}

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

#if 0
bool NXRenderingEngine::importRendererPluginsFromGraphicsManager(void)
{
	ClearResult(commandResult);
	
	if(graphicsManager == NULL) {
		string const msg = "Graphics-manager not set";
		SetResult(commandResult, NX_INITIALIZATION_ERROR, msg);
		NXLOG_SEVERE("NXRenderingEngine (import plugins from graphics manager)",
		             msg);
		return false;
	}
	
	bool success = true;
	
	// rendererSet.clear();
	vector<string> renderStyleCodes = graphicsManager->getRenderStyles();
	vector<string>::const_iterator renderStyleCodeIter;
	for(renderStyleCodeIter = renderStyleCodes.begin();
	    renderStyleCodeIter != renderStyleCodes.end();
	    ++renderStyleCodeIter)
	{
		NXRendererPlugin *const plugin =
			graphicsManager->getRenderer(*renderStyleCodeIter);
		NXRendererPlugin *const renderer = plugin->newInstance(this);
		if(renderer == NULL) {
			string const& renderStyleName =
				graphicsManager->getRenderStyleName(*renderStyleCodeIter);
			string const msg = "Failed to create new instance of renderer-plugin"
				" for style '" + renderStyleName + "' from graphics-manager";
			NXLOG_SEVERE("NXRenderingEngine", msg);
			SetResult(commandResult, NX_PLUGIN_CAUSED_ERROR, msg);
			success = false;
		}
		else {
			renderStyleMap[*renderStyleCodeIter] = renderer;
			// rendererSet.push_back(renderer);
		}
	}
	
	return success;
}
#endif

} // Nanorex