vtkwrap_test01.cpp

00001 /***********************************************************************************
00002  * VTKwrap - Simple VTK wrappers                                                   *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>     *
00004  *                                                                                 *
00005  * This file is part of VTKwrap.                                                   *
00006  *                                                                                 *
00007  * VTKwrap is free software; you can redistribute it and/or modify it under        *
00008  * the  terms of the GNU General Public License as published by the Free Software  *
00009  * Foundation; either version 2 of the License, or (at your option) any later      *
00010  * version.                                                                        *
00011  *                                                                                 *
00012  * VTKwrap is distributed in the hope that it will be useful, but WITHOUT ANY      *
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
00014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.        *
00015  *                                                                                 *
00016  * You should have received a copy of the GNU General Public License along with    *
00017  * VTKwrap; if not, write to the Free Software Foundation, Inc., 51 Franklin       *
00018  * Street, Fifth Floor, Boston, MA 02110-1301, USA                                 *
00019  ***********************************************************************************/
00020 
00021 #include <iostream>
00022 
00023 // VTK
00024 #include "vtkPoints.h"
00025 #include "vtkPolyData.h"
00026 #include "vtkDelaunay3D.h"
00027 #include "vtkShrinkFilter.h"
00028 #include "vtkDataSetMapper.h"
00029 #include "vtkOutlineFilter.h"
00030 #include "vtkProperty.h"
00031 #include "vtkPolyDataMapper.h"
00032 
00033 // MechCys
00034 #include "util/exception.h"
00035 #include "numerical/meshgrid.h"
00036 #include "vtkwrap/vtkwin.h"
00037 
00038 using std::cout;
00039 using std::endl;
00040 
00041 int main(int argc, char **argv) try
00042 {
00043     if (argc!=2)
00044     {
00045         cout << "Usage:\n";
00046         cout << "      " << argv[0] << " NPTS\n";
00047         cout << endl;
00048         cout << "ex.:  " << argv[0] << " 6\n";
00049         cout << endl;
00050         return 1;
00051     }
00052 
00053     // Create a meshgrid
00054     int  N = atoi(argv[1]); if (N<2) throw new Fatal(_("NPTS must be greater than 1"));
00055     REAL L = 10.0;
00056     MeshGrid mg(0.0,L,atoi(argv[1]),  // X
00057                 0.0,L,atoi(argv[1]),  // Y
00058                 0.0,L,atoi(argv[1])); // Z
00059 
00060     // Create VTK points
00061     vtkPoints * points = vtkPoints::New();
00062     points->Allocate(mg.Length());
00063     for (int i=0; i<mg.Length(); ++i)
00064     {
00065         double P[3] = {mg.X(i), mg.Y(i), mg.Z(i)};
00066         points->InsertPoint(i,P);
00067     }
00068 
00069     // Create a 3D triangulation
00070     //   - The Tolerance is the distance that nearly coincident points are merged together.
00071     //   - Delaunay does better if points are well spaced.
00072     //   - The alpha value is the radius of circumcircles, circumspheres.
00073     //     Any mesh entity whose circumcircle is smaller than this value is output.
00074     vtkPolyData   * vertices = vtkPolyData::New();
00075     vtkDelaunay3D * delaunay = vtkDelaunay3D::New();
00076     vertices -> SetPoints(points);
00077     delaunay -> SetInput(vertices);
00078     delaunay -> SetTolerance(0.01);
00079     //delaunay -> SetAlpha(L);
00080     delaunay -> SetAlpha(0); 
00081     delaunay -> BoundingTriangulationOff();
00082 
00083     // Shrink the result to help see it better.
00084     vtkShrinkFilter * shrink = vtkShrinkFilter::New();
00085     shrink -> SetInputConnection(delaunay->GetOutputPort());
00086     shrink -> SetShrinkFactor(0.9);
00087 
00088     // Mapper and actor
00089     vtkDataSetMapper * mapper = vtkDataSetMapper::New();
00090     vtkActor         * actor  = vtkActor::New();
00091     mapper -> SetInputConnection (shrink->GetOutputPort());
00092     actor  -> SetMapper          (mapper);
00093     actor  -> GetProperty        () -> SetColor(0,1,0);
00094 
00095     // Create a box around the points (Outline)
00096     vtkOutlineFilter  * outline        = vtkOutlineFilter::New();
00097     vtkPolyDataMapper * outline_mapper = vtkPolyDataMapper::New();
00098     vtkActor          * outline_actor  = vtkActor::New();
00099     outline        -> SetInputConnection(shrink->GetOutputPort());
00100     outline_mapper -> SetInput    (outline->GetOutput());
00101     outline_actor  -> SetMapper   (outline_mapper);
00102     outline_actor  -> GetProperty () -> SetColor(0,0,0);
00103 
00104     // Window
00105     VTKWin win;
00106     win.AddActor(actor);
00107     win.AddActor(outline_actor);
00108     win.Show();
00109 
00110     // Clean up
00111     points         -> Delete();
00112     vertices       -> Delete();
00113     delaunay       -> Delete();
00114     shrink         -> Delete();
00115     mapper         -> Delete();
00116     actor          -> Delete();
00117     outline        -> Delete();
00118     outline_mapper -> Delete();
00119     outline_actor  -> Delete();
00120 
00121     return 0;
00122 }
00123 catch (Exception * e) //{{{ 
00124 {
00125     e->Cout();
00126     if (e->IsFatal()) {delete e; exit(1);}
00127     delete e;
00128 }
00129 catch (char const * m)
00130 {
00131     std::cout << "Fatal: " << m << std::endl;
00132     exit (1);
00133 }
00134 catch (...)
00135 {
00136     std::cout << "Some exception (...) ocurred\n";
00137 } //}}} 
00138 
00139 // vim:fdm=marker

Generated on Wed Jan 24 15:56:28 2007 for MechSys by  doxygen 1.4.7