source: trunk/packages/vizservers/nanovis/ParticleSystemFactory.cpp @ 2798

Last change on this file since 2798 was 2798, checked in by ldelgass, 13 years ago

Add emacs mode magic line in preparation for indentation cleanup

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