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

Last change on this file since 3162 was 3131, checked in by ldelgass, 12 years ago

First pass at atom label support in VTK molecules

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