1 | #include "NvVectorField.h" |
---|
2 | #include "NvParticleRenderer.h" |
---|
3 | #include "nanovis.h" |
---|
4 | |
---|
5 | NvVectorField::NvVectorField() : |
---|
6 | _vectorFieldId(0), |
---|
7 | _activated(true), |
---|
8 | _scaleX(1), |
---|
9 | _scaleY(1), |
---|
10 | _scaleZ(1), |
---|
11 | _max(1) |
---|
12 | { |
---|
13 | _deviceVisible = false; |
---|
14 | _volPtr = 0; |
---|
15 | _physicalSize.set(1.0f, 1.0f, 1.0f); |
---|
16 | } |
---|
17 | |
---|
18 | NvVectorField::~NvVectorField() |
---|
19 | { |
---|
20 | std::map<std::string, NvParticleRenderer*>::iterator iter; |
---|
21 | for (iter = _particleRendererMap.begin(); |
---|
22 | iter != _particleRendererMap.end(); iter++) { |
---|
23 | delete (*iter).second; |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | void |
---|
28 | NvVectorField::setVectorField(Volume* volPtr, const Vector3& ori, |
---|
29 | float scaleX, float scaleY, float scaleZ, |
---|
30 | float max) |
---|
31 | { |
---|
32 | _volPtr = volPtr; |
---|
33 | _origin = ori; |
---|
34 | _scaleX = scaleX; |
---|
35 | _scaleY = scaleY; |
---|
36 | _scaleZ = scaleZ; |
---|
37 | _max = max; |
---|
38 | _vectorFieldId = volPtr->id; |
---|
39 | _physicalMin = volPtr->getPhysicalBBoxMin(); |
---|
40 | printf("_pysicalMin %f %f %f\n", _physicalMin.x, _physicalMin.y, _physicalMin.z); |
---|
41 | _physicalSize = volPtr->getPhysicalBBoxMax() - _physicalMin; |
---|
42 | printf("_pysicalSize %f %f %f\n", |
---|
43 | _physicalSize.x, _physicalSize.y, _physicalSize.z); |
---|
44 | } |
---|
45 | |
---|
46 | void |
---|
47 | NvVectorField::addDeviceShape(const std::string& name, |
---|
48 | const NvDeviceShape& shape) |
---|
49 | { |
---|
50 | _shapeMap[name] = shape; |
---|
51 | } |
---|
52 | |
---|
53 | void NvVectorField::removeDeviceShape(const std::string& name) |
---|
54 | { |
---|
55 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
56 | if (iter != _shapeMap.end()) _shapeMap.erase(iter); |
---|
57 | } |
---|
58 | |
---|
59 | void NvVectorField::initialize() |
---|
60 | { |
---|
61 | std::map<std::string, NvParticleRenderer*>::iterator iter; |
---|
62 | for (iter = _particleRendererMap.begin(); |
---|
63 | iter != _particleRendererMap.end(); iter++) { |
---|
64 | if ((*iter).second) (*iter).second->initialize(); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | void NvVectorField::reset() |
---|
69 | { |
---|
70 | std::map<std::string, NvParticleRenderer*>::iterator iter; |
---|
71 | for (iter = _particleRendererMap.begin(); |
---|
72 | iter != _particleRendererMap.end(); iter++) { |
---|
73 | if ((*iter).second) (*iter).second->reset(); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void NvVectorField::setPlaneAxis(const std::string& name, int axis) |
---|
78 | { |
---|
79 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
80 | if (iter != _particleRendererMap.end()) { |
---|
81 | (*iter).second->setAxis(axis); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | void NvVectorField::setPlanePos(const std::string& name, float pos) |
---|
87 | { |
---|
88 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
89 | if (iter != _particleRendererMap.end()) { |
---|
90 | (*iter).second->setPos(pos); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void NvVectorField::addPlane(const std::string& name) |
---|
95 | { |
---|
96 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
97 | NvParticleRenderer* renderer = 0; |
---|
98 | if (iter != _particleRendererMap.end()) { |
---|
99 | if ((*iter).second != 0) { |
---|
100 | renderer = (*iter).second; |
---|
101 | } else { |
---|
102 | renderer = (*iter).second = new NvParticleRenderer(NMESH, NMESH, g_context); |
---|
103 | } |
---|
104 | } else { |
---|
105 | renderer = new NvParticleRenderer(NMESH, NMESH, g_context); |
---|
106 | _particleRendererMap[name] = renderer; |
---|
107 | } |
---|
108 | |
---|
109 | renderer->setVectorField(_vectorFieldId, _origin, _scaleX, _scaleY, _scaleZ, _max); |
---|
110 | if (renderer) { |
---|
111 | renderer->initialize(); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void NvVectorField::removePlane(const std::string& name) |
---|
116 | { |
---|
117 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
118 | if (iter != _particleRendererMap.end()) { |
---|
119 | delete (*iter).second; |
---|
120 | _particleRendererMap.erase(iter); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void NvVectorField::activatePlane(const std::string& name) |
---|
125 | { |
---|
126 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
127 | if (iter != _particleRendererMap.end()) { |
---|
128 | (*iter).second->active(true); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | void NvVectorField::deactivatePlane(const std::string& name) |
---|
133 | { |
---|
134 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
135 | if (iter != _particleRendererMap.end()) { |
---|
136 | (*iter).second->active(false); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | void NvVectorField::setParticleColor(const std::string& name, const Vector4& color) |
---|
141 | { |
---|
142 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
143 | if (iter != _particleRendererMap.end()) { |
---|
144 | (*iter).second->setColor(color); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | void NvVectorField::setParticleColor(const std::string& name, float r, float g, float b, float a) |
---|
149 | { |
---|
150 | std::map<std::string, NvParticleRenderer*>::iterator iter = _particleRendererMap.find(name); |
---|
151 | if (iter != _particleRendererMap.end()) { |
---|
152 | if ((*iter).second) (*iter).second->setColor(Vector4(r,g,b,a)); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | void NvVectorField::advect() |
---|
157 | { |
---|
158 | std::map<std::string, NvParticleRenderer*>::iterator iter; |
---|
159 | for (iter = _particleRendererMap.begin(); |
---|
160 | iter != _particleRendererMap.end(); ++iter) { |
---|
161 | if ((*iter).second && (*iter).second->active()) |
---|
162 | (*iter).second->advect(); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | void NvVectorField::render() |
---|
167 | { |
---|
168 | std::map<std::string, NvParticleRenderer*>::iterator iter; |
---|
169 | for (iter = _particleRendererMap.begin(); |
---|
170 | iter != _particleRendererMap.end(); ++iter) { |
---|
171 | if ((*iter).second && (*iter).second->active()) { |
---|
172 | (*iter).second->render(); |
---|
173 | } |
---|
174 | } |
---|
175 | if (_deviceVisible) { |
---|
176 | drawDeviceShape(); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | void |
---|
181 | NvVectorField::drawDeviceShape() |
---|
182 | { |
---|
183 | glPushMatrix(); |
---|
184 | glEnable(GL_DEPTH_TEST); |
---|
185 | glDisable(GL_TEXTURE_2D); |
---|
186 | glEnable(GL_BLEND); |
---|
187 | |
---|
188 | float x0, y0, z0, x1, y1, z1; |
---|
189 | std::map<std::string, NvDeviceShape>::iterator iterShape; |
---|
190 | |
---|
191 | glPushMatrix(); |
---|
192 | glTranslatef(_origin.x, _origin.y, _origin.z); |
---|
193 | glScaled(_scaleX, _scaleY, _scaleZ); |
---|
194 | for (iterShape = _shapeMap.begin(); iterShape != _shapeMap.end(); |
---|
195 | ++iterShape) { |
---|
196 | NvDeviceShape& shape = (*iterShape).second; |
---|
197 | |
---|
198 | if (!shape.visible) continue; |
---|
199 | |
---|
200 | |
---|
201 | glColor4d(shape.color.x, shape.color.y, shape.color.z, shape.color.w); |
---|
202 | #if 0 |
---|
203 | x0 = _physicalMin.x + (shape.min.x - _physicalMin.x) / _physicalSize.x; |
---|
204 | y0 = _physicalMin.y + (shape.min.y - _physicalMin.y) / _physicalSize.y; |
---|
205 | z0 = _physicalMin.z + (shape.min.z - _physicalMin.z) / _physicalSize.z; |
---|
206 | x1 = _physicalMin.x + (shape.max.x - _physicalMin.x) / _physicalSize.x; |
---|
207 | y1 = _physicalMin.y + (shape.max.y - _physicalMin.y) / _physicalSize.y; |
---|
208 | z1 = _physicalMin.z + (shape.max.z - _physicalMin.z) / _physicalSize.z; |
---|
209 | #endif |
---|
210 | x0 = (shape.min.x - _physicalMin.x) / _physicalSize.x; |
---|
211 | y0 = (shape.min.y - _physicalMin.y) / _physicalSize.y; |
---|
212 | z0 = (shape.min.z - _physicalMin.z) / _physicalSize.z; |
---|
213 | x1 = (shape.max.x - _physicalMin.x) / _physicalSize.x; |
---|
214 | y1 = (shape.max.y - _physicalMin.y) / _physicalSize.y; |
---|
215 | z1 = (shape.max.z - _physicalMin.z) / _physicalSize.z; |
---|
216 | |
---|
217 | glLineWidth(1.2); |
---|
218 | glBegin(GL_LINE_LOOP); |
---|
219 | { |
---|
220 | glVertex3d(x0, y0, z0); |
---|
221 | glVertex3d(x1, y0, z0); |
---|
222 | glVertex3d(x1, y1, z0); |
---|
223 | glVertex3d(x0, y1, z0); |
---|
224 | } |
---|
225 | glEnd(); |
---|
226 | glBegin(GL_LINE_LOOP); |
---|
227 | { |
---|
228 | glVertex3d(x0, y0, z1); |
---|
229 | glVertex3d(x1, y0, z1); |
---|
230 | glVertex3d(x1, y1, z1); |
---|
231 | glVertex3d(x0, y1, z1); |
---|
232 | } |
---|
233 | glEnd(); |
---|
234 | |
---|
235 | glBegin(GL_LINE_LOOP); |
---|
236 | { |
---|
237 | glVertex3d(x0, y0, z0); |
---|
238 | glVertex3d(x0, y0, z1); |
---|
239 | glVertex3d(x0, y1, z1); |
---|
240 | glVertex3d(x0, y1, z0); |
---|
241 | } |
---|
242 | glEnd(); |
---|
243 | |
---|
244 | glBegin(GL_LINE_LOOP); |
---|
245 | { |
---|
246 | glVertex3d(x1, y0, z0); |
---|
247 | glVertex3d(x1, y0, z1); |
---|
248 | glVertex3d(x1, y1, z1); |
---|
249 | glVertex3d(x1, y1, z0); |
---|
250 | } |
---|
251 | glEnd(); |
---|
252 | } |
---|
253 | glPopMatrix(); |
---|
254 | |
---|
255 | glPopMatrix(); |
---|
256 | glDisable(GL_DEPTH_TEST); |
---|
257 | glDisable(GL_BLEND); |
---|
258 | glEnable(GL_TEXTURE_2D); |
---|
259 | } |
---|
260 | |
---|
261 | void |
---|
262 | NvVectorField::activateDeviceShape(const std::string& name) |
---|
263 | { |
---|
264 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
265 | if (iter != _shapeMap.end()) { |
---|
266 | (*iter).second.visible = true; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | void |
---|
271 | NvVectorField::deactivateDeviceShape(const std::string& name) |
---|
272 | { |
---|
273 | std::map<std::string, NvDeviceShape>::iterator iter = _shapeMap.find(name); |
---|
274 | if (iter != _shapeMap.end()) { |
---|
275 | (*iter).second.visible = false; |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | |
---|