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

Last change on this file since 2817 was 2817, checked in by ldelgass, 9 years ago

Remove a couple of unneeded headers

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