source: nanovis/trunk/ParticleSystemFactory.cpp @ 4827

Last change on this file since 4827 was 3618, checked in by ldelgass, 11 years ago

Some fixes for particle system compile (still doesn't work).

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