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

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

Const correctness fixes, pass vector/matrix objects by reference, various
formatting and style cleanups, don't spam syslog and uncomment openlog() call.
Still needs to be compiled with -DWANT_TRACE to get tracing, but now trace
output will be output to file in /tmp.

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