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