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

Last change on this file since 3464 was 3463, checked in by ldelgass, 11 years ago

Begin process of renaming R2 library

  • 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 <R2/R2FilePath.h>
8
9#include "ParticleSystemFactory.h"
10#include "ParticleSystem.h"
11#include "Trace.h"
12
13using namespace nv::util;
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", 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 glyphEnabled = 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], "glyph-enabled")) {
117            if (!strcmp(attrs[i + 1], "true"))
118                glyphEnabled = 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 = FilePath::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");
156                }
157            }
158
159            dir = path.substr(0, index + 1);
160            path = dir + fileName;
161               
162            _newParticleSystem =
163                new ParticleSystem(width, height, path.c_str(),
164                                   fieldWidth, fieldHeight, fieldDepth,
165                                   timeVaryingData, startIndex, endIndex);
166        } else {
167            _newParticleSystem =
168                new ParticleSystem(width, height, fileName.c_str(),
169                                   fieldWidth, fieldHeight, fieldDepth,
170                                   timeVaryingData, startIndex, endIndex);
171        }
172    } else {
173        std::string path = FilePath::getInstance()->getPath(fileName.c_str());
174        _newParticleSystem =
175            new ParticleSystem(width, height, path.c_str(),
176                               fieldWidth, fieldHeight, fieldDepth,
177                               timeVaryingData, startIndex, endIndex);
178    }
179
180    if (pointSize != -1.0f) _newParticleSystem->setDefaultPointSize(pointSize);
181    if (sortEnabled) _newParticleSystem->enable(ParticleSystem::PS_SORT);
182    if (glyphEnabled) _newParticleSystem->enable(ParticleSystem::PS_GLYPH);
183    if (bboxVisible) _newParticleSystem->enable(ParticleSystem::PS_DRAW_BBOX);
184    if (advectionEnabled) _newParticleSystem->enable(ParticleSystem::PS_ADVECTION);
185    if (streamlineEnabled) _newParticleSystem->enable(ParticleSystem::PS_STREAMLINE);
186    if (numOfUsedParticles != -1) _newParticleSystem->setUserDefinedNumOfParticles(numOfUsedParticles);
187}
188
189void ParticleSystemFactory::parseEmitterInfo(const char** attrs)
190{
191    ParticleEmitter* emitter = new ParticleEmitter;
192    for (int i = 0; attrs[i]; i += 2) {
193        if (!strcmp(attrs[i], "max-position-offset")) {
194            float x = 0, y = 0, z = 0;
195            sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
196            emitter->setMaxPositionOffset(x, y, z);
197        } else if (!strcmp(attrs[i], "position")) {
198            float x = 0, y = 0, z = 0;
199            sscanf(attrs[i+1], "%f%f%f",&x, &y, &z);
200            emitter->setPosition(x, y, z);
201        } else if (!strcmp(attrs[i], "min-max-life-time")) {
202            float min = 0, max = 0;
203            sscanf(attrs[i+1], "%f%f",&min, &max);
204            emitter->setMinMaxLifeTime(min, max);
205        } else if (!strcmp(attrs[i], "min-max-new-particles")) {
206            int min = 0, max = 0;
207            sscanf(attrs[i+1], "%d%d",&min, &max);
208            emitter->setMinMaxNumOfNewParticles(min, max);
209        } else if (!strcmp(attrs[i], "enabled")) {
210            if (!strcmp(attrs[i + 1], "true"))
211                emitter->setEnabled(true);
212            else
213                emitter->setEnabled(false);
214        }
215    }
216    _newParticleSystem->addEmitter(emitter);
217}
Note: See TracBrowser for help on using the repository browser.