source: trunk/packages/vizservers/vtkvis/RpPolyData.cpp @ 3492

Last change on this file since 3492 was 3427, checked in by ldelgass, 11 years ago

Fix warnings with WANT_TRACE disabled

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2012  HUBzero Foundation, LLC
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#include <cassert>
9
10#include <vtkDataSet.h>
11#include <vtkPolyData.h>
12#include <vtkUnstructuredGrid.h>
13#include <vtkPolyDataMapper.h>
14#include <vtkActor.h>
15#include <vtkProperty.h>
16#include <vtkTransform.h>
17#include <vtkDelaunay2D.h>
18#include <vtkDelaunay3D.h>
19#include <vtkDataSetSurfaceFilter.h>
20
21#include "RpPolyData.h"
22#include "Trace.h"
23
24using namespace Rappture::VtkVis;
25
26PolyData::PolyData() :
27    VtkGraphicsObject()
28{
29}
30
31PolyData::~PolyData()
32{
33#ifdef WANT_TRACE
34    if (_dataSet != NULL)
35        TRACE("Deleting PolyData for %s", _dataSet->getName().c_str());
36    else
37        TRACE("Deleting PolyData with NULL DataSet");
38#endif
39}
40
41/**
42 * \brief Create and initialize a VTK Prop to render a mesh
43 */
44void PolyData::initProp()
45{
46    VtkGraphicsObject::initProp();
47}
48
49/**
50 * \brief Internal method to set up pipeline after a state change
51 */
52void PolyData::update()
53{
54    if (_dataSet == NULL) {
55        return;
56    }
57
58    if (_pdMapper == NULL) {
59        _pdMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
60        _pdMapper->SetResolveCoincidentTopologyToPolygonOffset();
61        _pdMapper->ScalarVisibilityOff();
62    }
63
64    vtkDataSet *ds = _dataSet->getVtkDataSet();
65    vtkPolyData *pd = vtkPolyData::SafeDownCast(ds);
66    if (pd) {
67        TRACE("Points: %d Verts: %d Lines: %d Polys: %d Strips: %d",
68              pd->GetNumberOfPoints(),
69              pd->GetNumberOfVerts(),
70              pd->GetNumberOfLines(),
71              pd->GetNumberOfPolys(),
72              pd->GetNumberOfStrips());
73        // DataSet is a vtkPolyData
74        if (pd->GetNumberOfLines() == 0 &&
75            pd->GetNumberOfPolys() == 0 &&
76            pd->GetNumberOfStrips() == 0) {
77            // DataSet is a point cloud
78            PrincipalPlane plane;
79            double offset;
80            if (_dataSet->numDimensions() < 2 || pd->GetNumberOfPoints() < 3) {
81                // 0D or 1D or not enough points to mesh
82#ifdef USE_VTK6
83                _pdMapper->SetInputData(pd);
84#else
85                _pdMapper->SetInput(pd);
86#endif
87            } else if (_dataSet->is2D(&plane, &offset)) {
88                vtkSmartPointer<vtkDelaunay2D> mesher = vtkSmartPointer<vtkDelaunay2D>::New();
89                if (plane == PLANE_ZY) {
90                    vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
91                    trans->RotateWXYZ(90, 0, 1, 0);
92                    if (offset != 0.0) {
93                        trans->Translate(-offset, 0, 0);
94                    }
95                    mesher->SetTransform(trans);
96                } else if (plane == PLANE_XZ) {
97                     vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
98                    trans->RotateWXYZ(-90, 1, 0, 0);
99                    if (offset != 0.0) {
100                        trans->Translate(0, -offset, 0);
101                    }
102                    mesher->SetTransform(trans);
103                } else if (offset != 0.0) {
104                    // XY with Z offset
105                    vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
106                    trans->Translate(0, 0, -offset);
107                    mesher->SetTransform(trans);
108                }
109#ifdef USE_VTK6
110                mesher->SetInputData(pd);
111#else
112                mesher->SetInput(pd);
113#endif
114                mesher->ReleaseDataFlagOn();
115                mesher->Update();
116                vtkPolyData *outpd = mesher->GetOutput();
117                TRACE("Delaunay2D Verts: %d Lines: %d Polys: %d Strips: %d",
118                      outpd->GetNumberOfVerts(),
119                      outpd->GetNumberOfLines(),
120                      outpd->GetNumberOfPolys(),
121                      outpd->GetNumberOfStrips());
122                if (outpd->GetNumberOfPolys() == 0) {
123                    WARN("Delaunay2D mesher failed");
124#ifdef USE_VTK6
125                    _pdMapper->SetInputData(pd);
126#else
127                    _pdMapper->SetInput(pd);
128#endif
129                } else {
130                    _pdMapper->SetInputConnection(mesher->GetOutputPort());
131                }
132            } else {
133                vtkSmartPointer<vtkDelaunay3D> mesher = vtkSmartPointer<vtkDelaunay3D>::New();
134#ifdef USE_VTK6
135                mesher->SetInputData(pd);
136#else
137                mesher->SetInput(pd);
138#endif
139                mesher->ReleaseDataFlagOn();
140                mesher->Update();
141                vtkUnstructuredGrid *grid = mesher->GetOutput();
142                TRACE("Delaunay3D Cells: %d",
143                      grid->GetNumberOfCells());
144                if (grid->GetNumberOfCells() == 0) {
145                    WARN("Delaunay3D mesher failed");
146#ifdef USE_VTK6
147                    _pdMapper->SetInputData(pd);
148#else
149                    _pdMapper->SetInput(pd);
150#endif
151                } else {
152                    // Delaunay3D returns an UnstructuredGrid, so feed it
153                    // through a surface filter to get the grid boundary
154                    // as a PolyData
155                    vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
156                    gf->UseStripsOn();
157                    gf->SetInputConnection(mesher->GetOutputPort());
158                    gf->ReleaseDataFlagOn();
159                    _pdMapper->SetInputConnection(gf->GetOutputPort());
160                }
161             }
162        } else {
163            // DataSet is a vtkPolyData with lines and/or polygons
164#ifdef USE_VTK6
165            _pdMapper->SetInputData(pd);
166#else
167            _pdMapper->SetInput(pd);
168#endif
169        }
170    } else {
171        // DataSet is NOT a vtkPolyData
172        TRACE("DataSet is not a PolyData");
173        vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
174        gf->UseStripsOn();
175#ifdef USE_VTK6
176        gf->SetInputData(ds);
177#else
178        gf->SetInput(ds);
179#endif
180        gf->ReleaseDataFlagOn();
181        _pdMapper->SetInputConnection(gf->GetOutputPort());
182    }
183
184    initProp();
185    setVisibility(_dataSet->getVisibility());
186    getActor()->SetMapper(_pdMapper);
187    _pdMapper->Update();
188#ifdef WANT_TRACE
189    double *b = getBounds();
190    TRACE("bounds: %g %g %g %g %g %g", b[0], b[1], b[2], b[3], b[4], b[5]);
191#endif
192}
193
194/**
195 * \brief Set a group of world coordinate planes to clip rendering
196 *
197 * Passing NULL for planes will remove all cliping planes
198 */
199void PolyData::setClippingPlanes(vtkPlaneCollection *planes)
200{
201    if (_pdMapper != NULL) {
202        _pdMapper->SetClippingPlanes(planes);
203    }
204}
205
Note: See TracBrowser for help on using the repository browser.