source: branches/blt4/packages/vizservers/nanovis/ParticleSystemFactory.cpp @ 2409

Last change on this file since 2409 was 2409, checked in by gah, 13 years ago

update from branch

File size: 6.5 KB
Line 
1#include "ParticleSystemFactory.h"
2#include "ParticleSystem.h"
3#include <vrutil/vrFilePath.h>
4#include <stdio.h>
5#include "Trace.h"
6
7#ifdef _WIN32
8#include <expat/expat.h>
9#else
10#include <expat.h>
11#endif
12
13#include <string.h>
14
15#ifdef WIN32
16#pragma warning (disable : 4996)
17#endif
18
19#define BUFSIZE 4096
20
21void ParticleSystemFactory::text(void *data, const XML_Char *txt, int len)
22{
23
24
25void ParticleSystemFactory::startElement(void *userData, const char *elementName, const char **attrs)
26{
27        ParticleSystemFactory* This = reinterpret_cast<ParticleSystemFactory*>(userData);
28
29        if (!strcmp(elementName, "particle-sys-info"))
30        {
31                This->parseParticleSysInfo(attrs);
32        }
33        else if (!strcmp(elementName, "emitter"))
34        {
35                This->parseEmitterInfo(attrs);
36        }
37}
38
39void ParticleSystemFactory::endElement(void *userData, const char *name)
40{
41}
42
43ParticleSystemFactory::ParticleSystemFactory()
44: _newParticleSystem(0)
45{
46}
47
48ParticleSystemFactory::~ParticleSystemFactory()
49{
50}
51
52ParticleSystem* ParticleSystemFactory::create(const std::string& fileName)
53{
54        FILE* fp = fopen(fileName.c_str(), "rb");
55
56        if (fp == 0) {
57                return 0;
58        }
59        XML_Parser parser = XML_ParserCreate(NULL);
60
61        XML_SetUserData(parser, this);
62        XML_SetElementHandler(parser, startElement, endElement);
63
64        size_t stat;
65        size_t cnt;
66
67        while(! feof(fp))
68        {
69                void *buff = XML_GetBuffer(parser, BUFSIZE);
70                if (! buff)
71                {
72                        break;
73                }
74                cnt = fread(buff, 1, BUFSIZE, fp);
75                stat = XML_ParseBuffer(parser, (int) cnt, 0);
76                if (!stat)
77                {
78                        //TRACE("Parse error at line %d\n", XML_GetCurrentLineNumber(parser));
79                        break;
80                }
81        }
82
83        fclose(fp);
84
85        XML_ParserFree(parser);
86
87        return _newParticleSystem;
88}
89
90void ParticleSystemFactory::parseParticleSysInfo(const char** attrs)
91{
92        int width = 2, height = 2; // default
93        std::string fileName = "J-wire-vec.dx";
94        float pointSize = -1.0f;
95        int numOfUsedParticles = -1;
96        bool sortEnabled = false;
97        bool glypEnabled = false;
98        bool bboxVisible = false;
99        bool advectionEnabled = false;
100        bool streamlineEnabled = false;
101        bool timeVaryingData = false;
102        int fieldWidth = 1;
103        int fieldHeight = 1;
104        int fieldDepth = 1;
105        // TBD..
106        //float timeSeries_vel_mag_min;
107        //float timeSeries_vel_mag_max;
108        //float timeSeriesVelMagMax;
109        int startIndex = -1, endIndex = -1;
110        for (int i = 0; attrs[i]; i += 2) {
111                if (!strcmp(attrs[i], "rendertarget-width"))
112                {
113                        width = atoi(attrs[i + 1]);
114                }
115                else if (!strcmp(attrs[i], "rendertarget-height"))
116                {
117                        height = atoi(attrs[i + 1]);
118                }
119                else if (!strcmp(attrs[i], "particle-point-size"))
120                {
121                        pointSize = (float) atof(attrs[i + 1]);
122                }
123                else if (!strcmp(attrs[i], "vector-field-x"))
124                {
125                        fieldWidth = (float) atof(attrs[i + 1]);
126                }
127                else if (!strcmp(attrs[i], "vector-field-y"))
128                {
129                        fieldHeight = (float) atof(attrs[i + 1]);
130                }
131                else if (!strcmp(attrs[i], "vector-field-z"))
132                {
133                        fieldDepth = (float) atof(attrs[i + 1]);
134                }
135                else if (!strcmp(attrs[i], "sort-enabled"))
136                {
137                        if (!strcmp(attrs[i + 1], "true"))
138                                sortEnabled = true;
139                }
140                else if (!strcmp(attrs[i], "glyp-enabled"))
141                {
142                        if (!strcmp(attrs[i + 1], "true"))
143                                glypEnabled = true;
144                }
145                else if (!strcmp(attrs[i], "bbox-draw-enabled"))
146                {
147                        if (!strcmp(attrs[i + 1], "true"))
148                                bboxVisible = true;
149                }
150                else if (!strcmp(attrs[i], "advection-enabled"))
151                {
152                        if (!strcmp(attrs[i + 1], "true"))
153                                advectionEnabled = true;
154                }
155                else if (!strcmp(attrs[i], "stream-line-enabled"))
156                {
157                        if (!strcmp(attrs[i + 1], "true"))
158                                streamlineEnabled = true;
159                }
160                else if (!strcmp(attrs[i], "vector-field"))
161                {
162                        fileName = attrs[i + 1];
163                }
164                else if (!strcmp(attrs[i], "vector-field"))
165                {
166                        if (!strcmp(attrs[i + 1], "true"))
167                                timeVaryingData = true;
168                }
169                else if (!strcmp(attrs[i], "particle-user-num"))
170                {
171                        numOfUsedParticles = atoi(attrs[i + 1]);
172                }
173                else if (!strcmp(attrs[i], "time-series-start-index"))
174                {
175                        startIndex = atoi(attrs[i + 1]);
176                }
177                else if (!strcmp(attrs[i], "time-series-end-index"))
178                {
179                        endIndex = atoi(attrs[i + 1]);
180                }
181                else if (!strcmp(attrs[i], "time-varying"))
182                {
183                        if (!strcmp(attrs[i + 1], "true"))
184                                timeVaryingData = true;
185                }
186               
187        }
188
189        if (timeVaryingData)
190        {
191                char buff[256];
192                sprintf(buff, fileName.c_str(), startIndex);
193                std::string path = vrFilePath::getInstance()->getPath(buff);
194                if (path.size())
195                {
196                        std::string dir;
197                        int index = path.rfind('/');
198                        if (index == -1) {
199                                index = path.rfind('\\');
200                                if (index == -1)
201                                        TRACE("file not found\n");
202                        }
203
204                        dir = path.substr(0, index + 1);
205                        path = dir + fileName;
206               
207                        _newParticleSystem = new ParticleSystem(width, height, path.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
208                }
209                else
210                {
211                        _newParticleSystem = new ParticleSystem(width, height, fileName.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
212                }
213        }
214        else
215        {
216                std::string path = vrFilePath::getInstance()->getPath(fileName.c_str());
217                _newParticleSystem = new ParticleSystem(width, height, path.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
218        }
219
220        if (pointSize != -1.0f) _newParticleSystem->setDefaultPointSize(pointSize);
221        if (sortEnabled) _newParticleSystem->enable(ParticleSystem::PS_SORT);
222        if (glypEnabled) _newParticleSystem->enable(ParticleSystem::PS_GLYPE);
223        if (bboxVisible) _newParticleSystem->enable(ParticleSystem::PS_DRAW_BBOX);
224        if (advectionEnabled) _newParticleSystem->enable(ParticleSystem::PS_ADVECTION);
225        if (streamlineEnabled) _newParticleSystem->enable(ParticleSystem::PS_STREAMLINE);
226        if (numOfUsedParticles != -1) _newParticleSystem->setUserDefinedNumOfParticles(numOfUsedParticles);
227}
228
229void ParticleSystemFactory::parseEmitterInfo(const char** attrs)
230{
231        ParticleEmitter* emitter = new ParticleEmitter;
232        for (int i = 0; attrs[i]; i += 2) {
233                if (!strcmp(attrs[i], "max-position-offset"))
234                {
235                        float x = 0, y = 0, z = 0;
236                        sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
237                        emitter->setMaxPositionOffset(x, y, z);
238                }
239                else if (!strcmp(attrs[i], "position"))
240                {
241                        float x = 0, y = 0, z = 0;
242                        sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
243                        emitter->setPosition(x, y, z);
244                }
245                else if (!strcmp(attrs[i], "min-max-life-time"))
246                {
247                        float min = 0, max = 0;
248                        sscanf(attrs[i+1], "%f%f",&min, &max);
249                        emitter->setMinMaxLifeTime(min, max);
250                }
251                else if (!strcmp(attrs[i], "min-max-new-particles"))
252                {
253                        int min = 0, max = 0;
254                        sscanf(attrs[i+1], "%d%d",&min, &max);
255                        emitter->setMinMaxNumOfNewParticles(min, max);
256                }
257                else if (!strcmp(attrs[i], "enabled"))
258                {
259                        if (!strcmp(attrs[i + 1], "true"))
260                                emitter->setEnabled(true);
261                        else
262                                emitter->setEnabled(false);
263                }
264        }
265        _newParticleSystem->addEmitter(emitter);
266}
Note: See TracBrowser for help on using the repository browser.