Changeset 3131 for trunk


Ignore:
Timestamp:
Aug 2, 2012, 1:58:53 PM (12 years ago)
Author:
ldelgass
Message:

First pass at atom label support in VTK molecules

Location:
trunk/packages/vizservers/vtkvis
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/vtkvis/RpMolecule.cpp

    r3112 r3131  
    66 */
    77
     8#include <cstdio>
    89#include <cassert>
    910
     
    1112#include <vtkPointData.h>
    1213#include <vtkFloatArray.h>
     14#include <vtkStringArray.h>
    1315#include <vtkPolyData.h>
    1416#include <vtkPolyDataMapper.h>
     
    1820#include <vtkSphereSource.h>
    1921#include <vtkGlyph3D.h>
     22#include <vtkPointSetToLabelHierarchy.h>
     23#include <vtkLabelPlacementMapper.h>
     24#include <vtkTextProperty.h>
    2025
    2126#include "RpMolecule.h"
     
    2934    _radiusScale(0.3),
    3035    _atomScaling(VAN_DER_WAALS_RADIUS),
    31     _colorMap(NULL)
     36    _colorMap(NULL),
     37    _labelsOn(false)
    3238{
    3339    _bondColor[0] = _bondColor[1] = _bondColor[2] = 1.0f;
     
    7581            _bondProp->GetProperty()->LightingOff();
    7682    }
     83    if (_labelProp == NULL) {
     84        _labelProp = vtkSmartPointer<vtkActor2D>::New();
     85    }
    7786    if (_prop == NULL) {
    7887        _prop = vtkSmartPointer<vtkAssembly>::New();
     
    99108        _bondMapper->SetResolveCoincidentTopologyToPolygonOffset();
    100109        _bondMapper->ScalarVisibilityOn();
     110    }
     111    if (_labelMapper == NULL) {
     112        _labelMapper = vtkSmartPointer<vtkLabelPlacementMapper>::New();
     113        _labelMapper->SetShapeToRoundedRect();
     114        _labelMapper->SetBackgroundColor(1.0, 1.0, 0.7);
     115        _labelMapper->SetBackgroundOpacity(0.8);
     116        _labelMapper->SetMargin(3);
    101117    }
    102118
     
    117133    initProp();
    118134
     135    addLabelArray(ds);
    119136    addRadiusArray(ds, _atomScaling, _radiusScale);
    120137
     
    141158        }
    142159        if (pd->GetNumberOfVerts() > 0) {
     160            vtkSmartPointer<vtkPointSetToLabelHierarchy> hier = vtkSmartPointer<vtkPointSetToLabelHierarchy>::New();
     161            hier->SetInput(pd);
     162            hier->SetLabelArrayName("labels");
     163            hier->GetTextProperty()->SetColor(0, 0, 0);
     164            _labelMapper->SetInputConnection(hier->GetOutputPort());
     165            _labelProp->SetMapper(_labelMapper);
     166
    143167            // Atoms
    144168            vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
     
    171195    }
    172196
     197    setAtomLabelVisibility(_labelsOn);
     198
    173199    _atomMapper->Update();
    174200    _bondMapper->Update();
     201    _labelMapper->Update();
    175202}
    176203
     
    237264
    238265/**
     266 * \brief Turn on/off rendering of atom labels
     267 */
     268void Molecule::setAtomLabelVisibility(bool state)
     269{
     270    _labelsOn = state;
     271    if (_labelProp != NULL) {
     272        _labelProp->SetVisibility((state ? 1 : 0));
     273    }
     274}
     275
     276/**
    239277 * \brief Turn on/off rendering of the atoms
    240278 */
     
    253291    if (_bondProp != NULL) {
    254292        _bondProp->SetVisibility((state ? 1 : 0));
     293    }
     294}
     295
     296/**
     297 * \brief Toggle visibility of the prop
     298 */
     299void Molecule::setVisibility(bool state)
     300{
     301    VtkGraphicsObject::setVisibility(state);
     302    if (_labelProp != NULL) {
     303        if (!state)
     304            _labelProp->SetVisibility(0);
     305        else
     306            setAtomLabelVisibility(_labelsOn);
     307    }
     308}
     309
     310/**
     311 * \brief Set opacity of molecule
     312 */
     313void Molecule::setOpacity(double opacity)
     314{
     315    VtkGraphicsObject::setOpacity(opacity);
     316    if (_labelMapper != NULL) {
     317        _labelMapper->SetBackgroundOpacity(opacity);
    255318    }
    256319}
     
    341404    if (_tuber != NULL)
    342405        _tuber->SetRadius(scale);
     406}
     407
     408void Molecule::addLabelArray(vtkDataSet *dataSet)
     409{
     410    vtkDataArray *elements = NULL;
     411    if (dataSet->GetPointData() != NULL &&
     412        dataSet->GetPointData()->GetScalars() != NULL &&
     413        strcmp(dataSet->GetPointData()->GetScalars()->GetName(), "element") == 0) {
     414        elements = dataSet->GetPointData()->GetScalars();
     415    } else {
     416        WARN("Can't label atoms without an element array");
     417    }
     418
     419    vtkSmartPointer<vtkStringArray> labelArray = vtkSmartPointer<vtkStringArray>::New();
     420    labelArray->SetName("labels");
     421    vtkPolyData *pd = vtkPolyData::SafeDownCast(dataSet);
     422    if (pd == NULL) {
     423        ERROR("DataSet not a PolyData");
     424        return;
     425    }
     426    for (int i = 0; i < pd->GetNumberOfVerts(); i++) {
     427        char buf[32];
     428        if (elements != NULL) {
     429            int elt = (int)elements->GetComponent(i, 0);
     430            sprintf(buf, "%s%d", g_elementNames[elt], i);
     431        } else {
     432            sprintf(buf, "%d", i);
     433            labelArray->InsertNextValue(buf);
     434        }
     435        labelArray->InsertNextValue(buf);
     436    }
     437    dataSet->GetPointData()->AddArray(labelArray);
    343438}
    344439
  • trunk/packages/vizservers/vtkvis/RpMolecule.h

    r3112 r3131  
    1313#include <vtkPolyDataMapper.h>
    1414#include <vtkActor.h>
     15#include <vtkActor2D.h>
    1516#include <vtkAssembly.h>
    1617#include <vtkTubeFilter.h>
    1718#include <vtkGlyph3D.h>
     19#include <vtkLabelPlacementMapper.h>
    1820
    1921#include "ColorMap.h"
     
    5557    }
    5658
     59    virtual vtkProp *getOverlayProp()
     60    {
     61        return _labelProp;
     62    }
     63
    5764    virtual void setClippingPlanes(vtkPlaneCollection *planes);
    5865
     
    7784    void setBondRadiusScale(double scale);
    7885
     86    virtual void setVisibility(bool state);
     87
     88    virtual void setOpacity(double opacity);
     89
    7990    void setAtomVisibility(bool state);
     91
     92    void setAtomLabelVisibility(bool state);
    8093
    8194    void setBondVisibility(bool state);
     
    91104    virtual void update();
    92105
     106    static void addLabelArray(vtkDataSet *dataSet);
     107
    93108    static void addRadiusArray(vtkDataSet *dataSet, AtomScaling scaling, double scaleFactor);
    94109
     
    97112    AtomScaling _atomScaling;
    98113    ColorMap *_colorMap;
     114    bool _labelsOn;
    99115
    100116    vtkSmartPointer<vtkLookupTable> _lut;
    101117    vtkSmartPointer<vtkActor> _atomProp;
    102118    vtkSmartPointer<vtkActor> _bondProp;
     119    vtkSmartPointer<vtkActor2D> _labelProp;
    103120    vtkSmartPointer<vtkGlyph3D> _glypher;
    104121    vtkSmartPointer<vtkTubeFilter> _tuber;
    105122    vtkSmartPointer<vtkPolyDataMapper> _atomMapper;
    106123    vtkSmartPointer<vtkPolyDataMapper> _bondMapper;
     124    vtkSmartPointer<vtkLabelPlacementMapper> _labelMapper;
    107125};
    108126
  • trunk/packages/vizservers/vtkvis/RpVtkGraphicsObject.h

    r3092 r3131  
    165165    }
    166166
     167    virtual vtkProp *getOverlayProp()
     168    {
     169        return NULL;
     170    }
     171
    167172    /**
    168173     * \brief Set an additional transform on the prop
     
    242247
    243248    /**
     249     * \brief Set 2D aspect ratio scaling
     250     *
     251     * \param aspect 0=no scaling, otherwise aspect
     252     * is horiz/vertical ratio
     253     */
     254    virtual void setAspect(double aspect)
     255    {
     256        ;
     257    }
     258
     259    /**
    244260     * \brief Set the prop scaling
    245261     *
     
    251267            getProp3D()->SetScale(scale);
    252268        }
     269    }
     270
     271    /**
     272     * \brief Get the physical bounds of the prop
     273     *
     274     * If the prop is scaled, these bounds will be
     275     * scaled as well
     276     */
     277    virtual double *getBounds()
     278    {
     279        if (getProp() != NULL)
     280            return getProp()->GetBounds();
     281        else
     282            return NULL;
     283    }
     284
     285    /**
     286     * \brief Get the data bounds of the prop
     287     *
     288     * If the prop is scaled, these bounds will NOT
     289     * be scaled, they will reflect the unscaled data
     290     * bounds.
     291     */
     292    virtual double *getUnscaledBounds()
     293    {
     294        if (getProp3D() != NULL) {
     295            double scale[3];
     296            getProp3D()->GetScale(scale);
     297            if (scale[0] == scale[1] && scale[1] == scale[2] &&
     298                scale[0] == 1.0) {
     299                return getBounds();
     300            } else if (getBounds() != NULL) {
     301                _unscaledBounds[0] = getBounds()[0] / scale[0];
     302                _unscaledBounds[1] = getBounds()[1] / scale[0];
     303                _unscaledBounds[2] = getBounds()[2] / scale[1];
     304                _unscaledBounds[3] = getBounds()[3] / scale[1];
     305                _unscaledBounds[4] = getBounds()[4] / scale[2];
     306                _unscaledBounds[5] = getBounds()[5] / scale[2];
     307                return _unscaledBounds;
     308            }
     309        }
     310        return getBounds();
    253311    }
    254312
     
    743801    DataSet *_dataSet;
    744802    double _dataRange[2];
     803    double _unscaledBounds[6];
    745804    double _opacity;
    746805    float _color[3];
  • trunk/packages/vizservers/vtkvis/RpVtkRendererGraphicsObjs.h

    r2612 r3131  
    6060        if (gobj->getProp())
    6161            _renderer->RemoveViewProp(gobj->getProp());
     62        if (gobj->getOverlayProp())
     63            _renderer->RemoveViewProp(gobj->getOverlayProp());
    6264        delete gobj;
    6365
     
    100102        gobj->setDataSet(ds, this);
    101103
    102         if (gobj->getProp() == NULL) {
     104        if (gobj->getProp() == NULL &&
     105            gobj->getOverlayProp() == NULL) {
    103106            delete gobj;
    104107            return false;
    105108        } else {
    106             _renderer->AddViewProp(gobj->getProp());
     109            if (gobj->getProp())
     110                _renderer->AddViewProp(gobj->getProp());
     111            if (gobj->getOverlayProp())
     112                _renderer->AddViewProp(gobj->getOverlayProp());
    107113        }
    108114
     
    233239    do {
    234240        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);
    235272    } while (doAll && ++itr != hashmap.end());
    236273
Note: See TracChangeset for help on using the changeset viewer.