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

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

Glew header wants to be included before GL headers

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3#include <vrutil/vrFilePath.h>
4
5#include "ParticleSystemFactory.h"
6#include "ParticleSystem.h"
7
8#include <stdio.h>
9#include "Trace.h"
10
11#ifdef _WIN32
12#include <expat/expat.h>
13#else
14#include <expat.h>
15#endif
16
17#include <string.h>
18
19#ifdef WIN32
20#pragma warning (disable : 4996)
21#endif
22
23#define BUFSIZE 4096
24
25void ParticleSystemFactory::text(void *data, const XML_Char *txt, int len)
26{
27
28
29void ParticleSystemFactory::startElement(void *userData, const char *elementName, const char **attrs)
30{
31    ParticleSystemFactory* This = reinterpret_cast<ParticleSystemFactory*>(userData);
32
33    if (!strcmp(elementName, "particle-sys-info")) {
34        This->parseParticleSysInfo(attrs);
35    } else if (!strcmp(elementName, "emitter")) {
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        void *buff = XML_GetBuffer(parser, BUFSIZE);
70        if (! buff) {
71            break;
72        }
73        cnt = fread(buff, 1, BUFSIZE, fp);
74        stat = XML_ParseBuffer(parser, (int) cnt, 0);
75        if (!stat) {
76            //TRACE("Parse error at line %d\n", XML_GetCurrentLineNumber(parser));
77            break;
78        }
79    }
80
81    fclose(fp);
82
83    XML_ParserFree(parser);
84
85    return _newParticleSystem;
86}
87
88void ParticleSystemFactory::parseParticleSysInfo(const char** attrs)
89{
90    int width = 2, height = 2; // default
91    std::string fileName = "J-wire-vec.dx";
92    float pointSize = -1.0f;
93    int numOfUsedParticles = -1;
94    bool sortEnabled = false;
95    bool glypEnabled = false;
96    bool bboxVisible = false;
97    bool advectionEnabled = false;
98    bool streamlineEnabled = false;
99    bool timeVaryingData = false;
100    int fieldWidth = 1;
101    int fieldHeight = 1;
102    int fieldDepth = 1;
103    // TBD..
104    //float timeSeries_vel_mag_min;
105    //float timeSeries_vel_mag_max;
106    //float timeSeriesVelMagMax;
107    int startIndex = -1, endIndex = -1;
108    for (int i = 0; attrs[i]; i += 2) {
109        if (!strcmp(attrs[i], "rendertarget-width")) {
110            width = atoi(attrs[i + 1]);
111        } else if (!strcmp(attrs[i], "rendertarget-height")) {
112            height = atoi(attrs[i + 1]);
113        } else if (!strcmp(attrs[i], "particle-point-size")) {
114            pointSize = (float) atof(attrs[i + 1]);
115        } else if (!strcmp(attrs[i], "vector-field-x")) {
116            fieldWidth = (float) atof(attrs[i + 1]);
117        } else if (!strcmp(attrs[i], "vector-field-y")) {
118            fieldHeight = (float) atof(attrs[i + 1]);
119        } else if (!strcmp(attrs[i], "vector-field-z")) {
120            fieldDepth = (float) atof(attrs[i + 1]);
121        } else if (!strcmp(attrs[i], "sort-enabled")) {
122            if (!strcmp(attrs[i + 1], "true"))
123                sortEnabled = true;
124        } else if (!strcmp(attrs[i], "glyp-enabled")) {
125            if (!strcmp(attrs[i + 1], "true"))
126                glypEnabled = true;
127        } else if (!strcmp(attrs[i], "bbox-draw-enabled")) {
128            if (!strcmp(attrs[i + 1], "true"))
129                bboxVisible = true;
130        } else if (!strcmp(attrs[i], "advection-enabled")) {
131            if (!strcmp(attrs[i + 1], "true"))
132                advectionEnabled = true;
133        } else if (!strcmp(attrs[i], "stream-line-enabled")) {
134            if (!strcmp(attrs[i + 1], "true"))
135                streamlineEnabled = true;
136        } else if (!strcmp(attrs[i], "vector-field")) {
137            fileName = attrs[i + 1];
138        } else if (!strcmp(attrs[i], "vector-field")) {
139            if (!strcmp(attrs[i + 1], "true"))
140                timeVaryingData = true;
141        } else if (!strcmp(attrs[i], "particle-user-num")) {
142            numOfUsedParticles = atoi(attrs[i + 1]);
143        } else if (!strcmp(attrs[i], "time-series-start-index")) {
144            startIndex = atoi(attrs[i + 1]);
145        } else if (!strcmp(attrs[i], "time-series-end-index")) {
146            endIndex = atoi(attrs[i + 1]);
147        } else if (!strcmp(attrs[i], "time-varying")) {
148            if (!strcmp(attrs[i + 1], "true"))
149                timeVaryingData = true;
150        }
151    }
152
153    if (timeVaryingData) {
154        char buff[256];
155        sprintf(buff, fileName.c_str(), startIndex);
156        std::string path = vrFilePath::getInstance()->getPath(buff);
157        if (path.size()) {
158            std::string dir;
159            int index = path.rfind('/');
160            if (index == -1) {
161                index = path.rfind('\\');
162                if (index == -1)
163                    TRACE("file not found\n");
164            }
165
166            dir = path.substr(0, index + 1);
167            path = dir + fileName;
168
169            _newParticleSystem = new ParticleSystem(width, height, path.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
170        } else {
171            _newParticleSystem = new ParticleSystem(width, height, fileName.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
172        }
173    } else {
174        std::string path = vrFilePath::getInstance()->getPath(fileName.c_str());
175        _newParticleSystem = new ParticleSystem(width, height, path.c_str(), fieldWidth, fieldHeight, fieldDepth, timeVaryingData, startIndex, endIndex);
176    }
177
178    if (pointSize != -1.0f) _newParticleSystem->setDefaultPointSize(pointSize);
179    if (sortEnabled) _newParticleSystem->enable(ParticleSystem::PS_SORT);
180    if (glypEnabled) _newParticleSystem->enable(ParticleSystem::PS_GLYPE);
181    if (bboxVisible) _newParticleSystem->enable(ParticleSystem::PS_DRAW_BBOX);
182    if (advectionEnabled) _newParticleSystem->enable(ParticleSystem::PS_ADVECTION);
183    if (streamlineEnabled) _newParticleSystem->enable(ParticleSystem::PS_STREAMLINE);
184    if (numOfUsedParticles != -1) _newParticleSystem->setUserDefinedNumOfParticles(numOfUsedParticles);
185}
186
187void ParticleSystemFactory::parseEmitterInfo(const char** attrs)
188{
189    ParticleEmitter* emitter = new ParticleEmitter;
190    for (int i = 0; attrs[i]; i += 2) {
191        if (!strcmp(attrs[i], "max-position-offset")) {
192            float x = 0, y = 0, z = 0;
193            sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
194            emitter->setMaxPositionOffset(x, y, z);
195        } else if (!strcmp(attrs[i], "position")) {
196            float x = 0, y = 0, z = 0;
197            sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
198            emitter->setPosition(x, y, z);
199        } else if (!strcmp(attrs[i], "min-max-life-time")) {
200            float min = 0, max = 0;
201            sscanf(attrs[i+1], "%f%f",&min, &max);
202            emitter->setMinMaxLifeTime(min, max);
203        } else if (!strcmp(attrs[i], "min-max-new-particles")) {
204            int min = 0, max = 0;
205            sscanf(attrs[i+1], "%d%d",&min, &max);
206            emitter->setMinMaxNumOfNewParticles(min, max);
207        } else if (!strcmp(attrs[i], "enabled")) {
208            if (!strcmp(attrs[i + 1], "true"))
209                emitter->setEnabled(true);
210            else
211                emitter->setEnabled(false);
212        }
213    }
214    _newParticleSystem->addEmitter(emitter);
215}
Note: See TracBrowser for help on using the repository browser.