source: trunk/packages/vizservers/vtkvis/RpVtkRendererGraphicsObjs.h @ 3112

Last change on this file since 3112 was 2612, checked in by ldelgass, 13 years ago

Refactor vtkvis to support setting colormap fields by name/attribute type
rather than always using active scalars/vectors. Also convert common
graphics objects set methods in Renderer to template methods and separate
core and graphics object related methods to separate files.

  • Property svn:eol-style set to native
File size: 18.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2011, Purdue Research Foundation
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#ifndef __RAPPTURE_VTKVIS_RENDERER_GRAPHICS_OBJS_H__
9#define __RAPPTURE_VTKVIS_RENDERER_GRAPHICS_OBJS_H__
10
11#include <tr1/unordered_map>
12#include <typeinfo>
13
14#include "RpVtkRenderer.h"
15
16namespace Rappture {
17namespace VtkVis {
18
19template<class GraphicsObject>
20GraphicsObject *Renderer::getGraphicsObject(const DataSetId& id)
21{
22    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
23        getGraphicsObjectHashmap<GraphicsObject>();
24    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator
25        itr = hashmap.find(id);
26
27    if (itr == hashmap.end()) {
28#ifdef DEBUG
29        TRACE("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
30#endif
31        return NULL;
32    } else
33        return itr->second;
34}
35
36template<class GraphicsObject>
37void Renderer::deleteGraphicsObject(const DataSetId& id)
38{
39    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
40        getGraphicsObjectHashmap<GraphicsObject>();
41    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
42
43    bool doAll = false;
44
45    if (id.compare("all") == 0) {
46        itr = hashmap.begin();
47        doAll = true;
48    } else {
49        itr = hashmap.find(id);
50    }
51    if (itr == hashmap.end()) {
52        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
53        return;
54    }
55
56    TRACE("Deleting %s for %s", itr->second->getClassName(), id.c_str());
57
58    do {
59        GraphicsObject *gobj = itr->second;
60        if (gobj->getProp())
61            _renderer->RemoveViewProp(gobj->getProp());
62        delete gobj;
63
64        itr = hashmap.erase(itr);
65    } while (doAll && itr != hashmap.end());
66
67    initCamera();
68    _needsRedraw = true;
69}
70
71template<class GraphicsObject>
72bool Renderer::addGraphicsObject(const DataSetId& id)
73{
74    DataSetHashmap::iterator itr;
75
76    bool doAll = false;
77
78    if (id.compare("all") == 0) {
79        itr = _dataSets.begin();
80    } else {
81        itr = _dataSets.find(id);
82    }
83    if (itr == _dataSets.end()) {
84        ERROR("Unknown dataset %s", id.c_str());
85        return false;
86    }
87
88    do {
89        DataSet *ds = itr->second;
90        const DataSetId& dsID = ds->getName();
91
92        GraphicsObject *gobj;
93        if ((gobj = getGraphicsObject<GraphicsObject>(dsID)) != NULL) {
94            WARN("Replacing existing %s %s", gobj->getClassName(), dsID.c_str());
95            deleteGraphicsObject<GraphicsObject>(dsID);
96        }
97
98        gobj = new GraphicsObject();
99 
100        gobj->setDataSet(ds, this);
101
102        if (gobj->getProp() == NULL) {
103            delete gobj;
104            return false;
105        } else {
106            _renderer->AddViewProp(gobj->getProp());
107        }
108
109        getGraphicsObjectHashmap<GraphicsObject>()[dsID] = gobj;
110    } while (doAll && ++itr != _dataSets.end());
111
112    initCamera();
113    _needsRedraw = true;
114    return true;
115}
116
117/**
118 * \brief Set the prop orientation with a quaternion
119 */
120template<class GraphicsObject>
121void Renderer::setGraphicsObjectTransform(const DataSetId& id, vtkMatrix4x4 *trans)
122{
123    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
124        getGraphicsObjectHashmap<GraphicsObject>();
125    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
126
127    bool doAll = false;
128
129    if (id.compare("all") == 0) {
130        itr = hashmap.begin();
131        doAll = true;
132    } else {
133        itr = hashmap.find(id);
134    }
135    if (itr == hashmap.end()) {
136        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
137        return;
138    }
139
140    do {
141        itr->second->setTransform(trans);
142    } while (doAll && ++itr != hashmap.end());
143
144    resetAxes();
145    _needsRedraw = true;
146}
147
148/**
149 * \brief Set the prop orientation with a quaternion
150 */
151template<class GraphicsObject>
152void Renderer::setGraphicsObjectOrientation(const DataSetId& id, double quat[4])
153{
154    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
155        getGraphicsObjectHashmap<GraphicsObject>();
156    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
157
158    bool doAll = false;
159
160    if (id.compare("all") == 0) {
161        itr = hashmap.begin();
162        doAll = true;
163    } else {
164        itr = hashmap.find(id);
165    }
166    if (itr == hashmap.end()) {
167        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
168        return;
169    }
170
171    do {
172        itr->second->setOrientation(quat);
173    } while (doAll && ++itr != hashmap.end());
174
175    resetAxes();
176    _needsRedraw = true;
177}
178
179/**
180 * \brief Set the prop orientation with a rotation about an axis
181 */
182template<class GraphicsObject>
183void Renderer::setGraphicsObjectOrientation(const DataSetId& id, double angle, double axis[3])
184{
185    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
186        getGraphicsObjectHashmap<GraphicsObject>();
187    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
188
189    bool doAll = false;
190
191    if (id.compare("all") == 0) {
192        itr = hashmap.begin();
193        doAll = true;
194    } else {
195        itr = hashmap.find(id);
196    }
197    if (itr == hashmap.end()) {
198        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
199        return;
200    }
201
202    do {
203        itr->second->setOrientation(angle, axis);
204    } while (doAll && ++itr != hashmap.end());
205
206    resetAxes();
207    _needsRedraw = true;
208}
209
210/**
211 * \brief Set the prop position in world coords
212 */
213template<class GraphicsObject>
214void Renderer::setGraphicsObjectPosition(const DataSetId& id, double pos[3])
215{
216    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
217        getGraphicsObjectHashmap<GraphicsObject>();
218    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
219
220    bool doAll = false;
221
222    if (id.compare("all") == 0) {
223        itr = hashmap.begin();
224        doAll = true;
225    } else {
226        itr = hashmap.find(id);
227    }
228    if (itr == hashmap.end()) {
229        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
230        return;
231    }
232
233    do {
234        itr->second->setPosition(pos);
235    } while (doAll && ++itr != hashmap.end());
236
237    resetAxes();
238    _needsRedraw = true;
239}
240
241/**
242 * \brief Set the prop scaling
243 */
244template<class GraphicsObject>
245void Renderer::setGraphicsObjectScale(const DataSetId& id, double scale[3])
246{
247    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
248        getGraphicsObjectHashmap<GraphicsObject>();
249    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
250
251    bool doAll = false;
252
253    if (id.compare("all") == 0) {
254        itr = hashmap.begin();
255        doAll = true;
256    } else {
257        itr = hashmap.find(id);
258    }
259    if (itr == hashmap.end()) {
260        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
261        return;
262    }
263
264    do {
265        itr->second->setScale(scale);
266    } while (doAll && ++itr != hashmap.end());
267
268    resetAxes();
269    _needsRedraw = true;
270}
271
272/**
273 * \brief Set visibility of VtkGraphicsObject for the given DataSet
274 */
275template<class GraphicsObject>
276void Renderer::setGraphicsObjectVisibility(const DataSetId& id, bool state)
277{
278    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
279        getGraphicsObjectHashmap<GraphicsObject>();
280    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
281
282    bool doAll = false;
283
284    if (id.compare("all") == 0) {
285        itr = hashmap.begin();
286        doAll = true;
287    } else {
288        itr = hashmap.find(id);
289    }
290    if (itr == hashmap.end()) {
291        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
292        return;
293    }
294
295    do {
296        itr->second->setVisibility(state);
297    } while (doAll && ++itr != hashmap.end());
298
299    _needsRedraw = true;
300}
301
302/**
303 * \brief Set the volume slice
304 */
305template<class GraphicsObject>
306void Renderer::setGraphicsObjectVolumeSlice(const DataSetId& id, Axis axis, double ratio)
307{
308    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
309        getGraphicsObjectHashmap<GraphicsObject>();
310    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
311
312    bool doAll = false;
313
314    if (id.compare("all") == 0) {
315        itr = hashmap.begin();
316        doAll = true;
317    } else {
318        itr = hashmap.find(id);
319    }
320
321    if (itr == hashmap.end()) {
322        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
323        return;
324    }
325
326    do {
327        itr->second->selectVolumeSlice(axis, ratio);
328     } while (doAll && ++itr != hashmap.end());
329
330    _renderer->ResetCameraClippingRange();
331    _needsRedraw = true;
332}
333
334
335/**
336 * \brief Set the RGB actor color for the specified DataSet
337 */
338template<class GraphicsObject>
339void Renderer::setGraphicsObjectColor(const DataSetId& id, float color[3])
340{
341    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
342        getGraphicsObjectHashmap<GraphicsObject>();
343    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
344
345    bool doAll = false;
346
347    if (id.compare("all") == 0) {
348        itr = hashmap.begin();
349        doAll = true;
350    } else {
351        itr = hashmap.find(id);
352    }
353    if (itr == hashmap.end()) {
354        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
355        return;
356    }
357
358    do {
359        itr->second->setColor(color);
360    } while (doAll && ++itr != hashmap.end());
361    _needsRedraw = true;
362}
363
364/**
365 * \brief Turn on/off edges for the given DataSet
366 */
367template<class GraphicsObject>
368void Renderer::setGraphicsObjectEdgeVisibility(const DataSetId& id, bool state)
369{
370    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
371        getGraphicsObjectHashmap<GraphicsObject>();
372    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
373
374    bool doAll = false;
375
376    if (id.compare("all") == 0) {
377        itr = hashmap.begin();
378        doAll = true;
379    } else {
380        itr = hashmap.find(id);
381    }
382    if (itr == hashmap.end()) {
383        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
384        return;
385    }
386
387    do {
388        itr->second->setEdgeVisibility(state);
389    } while (doAll && ++itr != hashmap.end());
390
391    _needsRedraw = true;
392}
393
394/**
395 * \brief Set the RGB isosurface edge color for the specified DataSet
396 */
397template<class GraphicsObject>
398void Renderer::setGraphicsObjectEdgeColor(const DataSetId& id, float color[3])
399{
400    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
401        getGraphicsObjectHashmap<GraphicsObject>();
402    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
403
404    bool doAll = false;
405
406    if (id.compare("all") == 0) {
407        itr = hashmap.begin();
408        doAll = true;
409    } else {
410        itr = hashmap.find(id);
411    }
412    if (itr == hashmap.end()) {
413        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
414        return;
415    }
416
417    do {
418        itr->second->setEdgeColor(color);
419    } while (doAll && ++itr != hashmap.end());
420
421    _needsRedraw = true;
422}
423
424/**
425 * \brief Set the isosurface edge width for the specified DataSet (may be a no-op)
426 *
427 * If the OpenGL implementation/hardware does not support wide lines,
428 * this function may not have an effect.
429 */
430template<class GraphicsObject>
431void Renderer::setGraphicsObjectEdgeWidth(const DataSetId& id, float edgeWidth)
432{
433    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
434        getGraphicsObjectHashmap<GraphicsObject>();
435    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
436
437    bool doAll = false;
438
439    if (id.compare("all") == 0) {
440        itr = hashmap.begin();
441        doAll = true;
442    } else {
443        itr = hashmap.find(id);
444    }
445    if (itr == hashmap.end()) {
446        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
447        return;
448    }
449
450    do {
451        itr->second->setEdgeWidth(edgeWidth);
452    } while (doAll && ++itr != hashmap.end());
453
454    _needsRedraw = true;
455}
456
457/**
458 * \brief Set ambient lighting/shading coefficient for the specified DataSet
459 */
460template<class GraphicsObject>
461void Renderer::setGraphicsObjectAmbient(const DataSetId& id, double coeff)
462{
463    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
464        getGraphicsObjectHashmap<GraphicsObject>();
465    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
466
467    bool doAll = false;
468
469    if (id.compare("all") == 0) {
470        itr = hashmap.begin();
471        doAll = true;
472    } else {
473        itr = hashmap.find(id);
474    }
475    if (itr == hashmap.end()) {
476        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
477        return;
478    }
479
480    do {
481        itr->second->setAmbient(coeff);
482    } while (doAll && ++itr != hashmap.end());
483
484    _needsRedraw = true;
485}
486
487/**
488 * \brief Set diffuse lighting/shading coefficient for the specified DataSet
489 */
490template<class GraphicsObject>
491void Renderer::setGraphicsObjectDiffuse(const DataSetId& id, double coeff)
492{
493    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
494        getGraphicsObjectHashmap<GraphicsObject>();
495    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
496
497    bool doAll = false;
498
499    if (id.compare("all") == 0) {
500        itr = hashmap.begin();
501        doAll = true;
502    } else {
503        itr = hashmap.find(id);
504    }
505    if (itr == hashmap.end()) {
506        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
507        return;
508    }
509
510    do {
511        itr->second->setDiffuse(coeff);
512    } while (doAll && ++itr != hashmap.end());
513
514    _needsRedraw = true;
515}
516
517/**
518 * \brief Set specular lighting/shading coefficient and power for the specified DataSet
519 */
520template<class GraphicsObject>
521void Renderer::setGraphicsObjectSpecular(const DataSetId& id, double coeff, double power)
522{
523    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
524        getGraphicsObjectHashmap<GraphicsObject>();
525    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
526
527    bool doAll = false;
528
529    if (id.compare("all") == 0) {
530        itr = hashmap.begin();
531        doAll = true;
532    } else {
533        itr = hashmap.find(id);
534    }
535    if (itr == hashmap.end()) {
536        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
537        return;
538    }
539
540    do {
541        itr->second->setSpecular(coeff, power);
542    } while (doAll && ++itr != hashmap.end());
543
544    _needsRedraw = true;
545}
546
547/**
548 * \brief Turn actor lighting on/off for the specified DataSet
549 */
550template<class GraphicsObject>
551void Renderer::setGraphicsObjectLighting(const DataSetId& id, bool state)
552{
553    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
554        getGraphicsObjectHashmap<GraphicsObject>();
555    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
556
557    bool doAll = false;
558
559    if (id.compare("all") == 0) {
560        itr = hashmap.begin();
561        doAll = true;
562    } else {
563        itr = hashmap.find(id);
564    }
565    if (itr == hashmap.end()) {
566        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
567        return;
568    }
569
570    do {
571        itr->second->setLighting(state);
572    } while (doAll && ++itr != hashmap.end());
573    _needsRedraw = true;
574}
575
576/**
577 * \brief Set opacity of VtkGraphicsObject for the given DataSet
578 */
579template<class GraphicsObject>
580void Renderer::setGraphicsObjectOpacity(const DataSetId& id, double opacity)
581{
582    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
583        getGraphicsObjectHashmap<GraphicsObject>();
584    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
585
586    bool doAll = false;
587
588    if (id.compare("all") == 0) {
589        itr = hashmap.begin();
590        doAll = true;
591    } else {
592        itr = hashmap.find(id);
593    }
594    if (itr == hashmap.end()) {
595        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
596        return;
597    }
598
599    do {
600        itr->second->setOpacity(opacity);
601    } while (doAll && ++itr != hashmap.end());
602
603    _needsRedraw = true;
604}
605
606/**
607 * \brief Set the point size for the specified DataSet (may be a no-op)
608 *
609 * If the OpenGL implementation/hardware does not support wide points,
610 * this function may not have an effect.
611 */
612template<class GraphicsObject>
613void Renderer::setGraphicsObjectPointSize(const DataSetId& id, float size)
614{
615    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
616        getGraphicsObjectHashmap<GraphicsObject>();
617    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
618
619    bool doAll = false;
620
621    if (id.compare("all") == 0) {
622        itr = hashmap.begin();
623        doAll = true;
624    } else {
625        itr = hashmap.find(id);
626    }
627    if (itr == hashmap.end()) {
628        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
629        return;
630    }
631
632    do {
633        itr->second->setPointSize(size);
634    } while (doAll && ++itr != hashmap.end());
635
636    _needsRedraw = true;
637}
638
639/**
640 * \brief Set wireframe actor rendering for the specified DataSet
641 */
642template<class GraphicsObject>
643void Renderer::setGraphicsObjectWireframe(const DataSetId& id, bool state)
644{
645    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
646        getGraphicsObjectHashmap<GraphicsObject>();
647    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
648
649    bool doAll = false;
650
651    if (id.compare("all") == 0) {
652        itr = hashmap.begin();
653        doAll = true;
654    } else {
655        itr = hashmap.find(id);
656    }
657    if (itr == hashmap.end()) {
658        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
659        return;
660    }
661
662    do {
663        itr->second->setWireframe(state);
664    } while (doAll && ++itr != hashmap.end());
665
666    _needsRedraw = true;
667}
668
669/**
670 * \brief Associate an existing named color map with a graphics object for the given DataSet
671 */
672template<class GraphicsObject>
673void Renderer::setGraphicsObjectColorMap(const DataSetId& id, const ColorMapId& colorMapId)
674{
675    std::tr1::unordered_map<DataSetId, GraphicsObject *>& hashmap =
676        getGraphicsObjectHashmap<GraphicsObject>();
677    typename std::tr1::unordered_map<DataSetId, GraphicsObject *>::iterator itr;
678
679    bool doAll = false;
680
681    if (id.compare("all") == 0) {
682        itr = hashmap.begin();
683        doAll = true;
684    } else {
685        itr = hashmap.find(id);
686    }
687
688    if (itr == hashmap.end()) {
689        ERROR("%s not found: %s", typeid(GraphicsObject).name(), id.c_str());
690        return;
691    }
692
693    ColorMap *cmap = getColorMap(colorMapId);
694    if (cmap == NULL) {
695        ERROR("Unknown colormap: %s", colorMapId.c_str());
696        return;
697    }
698
699    do {
700        TRACE("Set %s color map: %s for dataset %s",
701              typeid(GraphicsObject).name(),
702              colorMapId.c_str(),
703              itr->second->getDataSet()->getName().c_str());
704
705        itr->second->setColorMap(cmap);
706    } while (doAll && ++itr != hashmap.end());
707
708    _needsRedraw = true;
709}
710
711}
712}
713
714#endif
Note: See TracBrowser for help on using the repository browser.