⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 三角化.cpp

📁 VTK入门样例(种类齐全)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataReader.h"
#include "vtkRenderer.h"
#include "vtkWin32OpenGLRenderWindow.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkTexture.h"
#include "vtkTextureMapToCylinder.h"
#include "vtkParametricSuperEllipsoid.h"
#include "vtkParametricSuperToroid.h"
#include "vtkParametricMobius.h"
#include "vtkPolyData.h"
#include "vtkDataSetMapper.h"
#include "vtkParametricFigure8Klein.h"
#include "vtkActor2D.h"
#include "vtkParametricKlein.h"
#include "vtkTextMapper.h"
#include "vtkParametricFunctionSource.h"
#include "vtkParametricTorus.h"
#include "vtkJPEGReader.h"

#include "vtkPoints.h"
#include "vtkParametricSpline.h"
#include "vtkParametricConicSpiral.h"
#include "vtkParametricBoy.h"
#include "vtkParametricRoman.h"
#include "vtkParametricRandomHills.h"
#include "vtkParametricEllipsoid.h"
#include "vtkParametricDini.h"
#include "vtkParametricEnneper.h"
#include "vtkParametricCrossCap.h"
#include "vtkParametricEnneper.h"
#include "vtkTextProperty.h"
#include "vtkMath.h"

void main()
{
//Compute normals, in case they are not provided with the model.
	vtkRenderer *ren1=vtkRenderer::New();
	vtkWin32OpenGLRenderWindow *renWin=vtkWin32OpenGLRenderWindow::New();
  	  renWin->AddRenderer(ren1);
	vtkWin32RenderWindowInteractor *iren=vtkWin32RenderWindowInteractor::New();
	  iren->SetRenderWindow(renWin);	

vtkJPEGReader *textureReader=vtkJPEGReader::New();
  textureReader-> SetFileName ("beach.jpg");
vtkTexture *texture=vtkTexture::New();
  texture ->SetInputConnection (textureReader ->GetOutputPort());
int i;
/*
# ------------------------------------------------------------
# For each parametric surface:
# 1) Create it
# 2) Assign mappers and actors
# 3) Position ths object
# 5) Add a label
# ------------------------------------------------------------

# ------------------------------------------------------------
# Create a torus
# ------------------------------------------------------------
*/
vtkParametricTorus *torus=vtkParametricTorus::New();
vtkParametricFunctionSource *torusSource=vtkParametricFunctionSource::New();
  torusSource-> SetParametricFunction (torus);
  torusSource-> SetScalarModeToPhase();

vtkPolyDataMapper *torusMapper=vtkPolyDataMapper::New();
  torusMapper-> SetInputConnection (torusSource-> GetOutputPort());
  torusMapper-> SetScalarRange( 0 ,360);

vtkActor *torusActor=vtkActor::New();
  torusActor-> SetMapper (torusMapper);
  torusActor-> SetPosition( 0 ,12, 0);

vtkTextMapper *torusTextMapper=vtkTextMapper::New();
    torusTextMapper-> SetInput ("Torus");
    torusTextMapper-> GetTextProperty()-> SetJustificationToCentered();
    torusTextMapper-> GetTextProperty()-> SetVerticalJustificationToCentered();
    torusTextMapper-> GetTextProperty()-> SetColor( 1 ,0 ,0);
    torusTextMapper-> GetTextProperty()-> SetFontSize (14);

vtkActor2D *torusTextActor =vtkActor2D::New();
    torusTextActor-> SetMapper (torusTextMapper );   
    torusTextActor-> GetPositionCoordinate()-> SetCoordinateSystemToWorld();
    torusTextActor-> GetPositionCoordinate()-> SetValue (0, 9.5 ,0);

//# ------------------------------------------------------------
//# Create a klein bottle
//# ------------------------------------------------------------
vtkParametricKlein *klein=vtkParametricKlein::New();
vtkParametricFunctionSource *kleinSource=vtkParametricFunctionSource::New();
  kleinSource-> SetParametricFunction (klein);
  kleinSource-> SetScalarModeToU0V0();

vtkPolyDataMapper *kleinMapper=vtkPolyDataMapper::New();
  kleinMapper-> SetInputConnection (kleinSource->GetOutputPort());
  kleinMapper-> SetScalarRange (0, 3);

vtkActor *kleinActor=vtkActor::New();
  kleinActor-> SetMapper(kleinMapper);
  kleinActor-> SetPosition (8 ,10.5 ,0);

vtkTextMapper *kleinTextMapper=vtkTextMapper::New();
    kleinTextMapper-> SetInput ("Klein");
    kleinTextMapper-> GetTextProperty() ->SetJustificationToCentered();
    kleinTextMapper-> GetTextProperty() ->SetVerticalJustificationToCentered();
    kleinTextMapper-> GetTextProperty()-> SetColor( 1, 0 ,0);
    kleinTextMapper-> GetTextProperty()-> SetFontSize (14);
vtkActor2D *kleinTextActor=vtkActor2D ::New();
    kleinTextActor-> SetMapper (kleinTextMapper );   
    kleinTextActor-> GetPositionCoordinate() ->SetCoordinateSystemToWorld();
    kleinTextActor-> GetPositionCoordinate() ->SetValue (8 ,9.5 ,0);

//------------------------------------------------------------
// Create a Figure-8 Klein
// ------------------------------------------------------------
vtkParametricFigure8Klein *klein2=vtkParametricFigure8Klein::New();
vtkParametricFunctionSource *klein2Source=vtkParametricFunctionSource::New();
  klein2Source-> SetParametricFunction (klein2);
  klein2Source ->GenerateTextureCoordinatesOn();

vtkPolyDataMapper *klein2Mapper=vtkPolyDataMapper::New();
  klein2Mapper-> SetInputConnection (klein2Source-> GetOutputPort());
  klein2Mapper-> SetScalarRange (0 ,3);

vtkActor *klein2Actor=vtkActor::New();
  klein2Actor-> SetMapper (klein2Mapper);
  klein2Actor-> SetPosition (16 ,12 ,0);
  klein2Actor-> SetTexture (texture);


vtkTextMapper *fig8KleinTextMapper=vtkTextMapper::New();
    fig8KleinTextMapper ->SetInput ("Fig-8 Klein");
    fig8KleinTextMapper-> GetTextProperty()-> SetJustificationToCentered();
    fig8KleinTextMapper-> GetTextProperty()-> SetVerticalJustificationToCentered();
    fig8KleinTextMapper-> GetTextProperty()-> SetColor (1 ,0 ,0);
    fig8KleinTextMapper-> GetTextProperty()-> SetFontSize (14);
vtkActor2D *fig8KleinTextActor=vtkActor2D::New();
    fig8KleinTextActor-> SetMapper (fig8KleinTextMapper);
    fig8KleinTextActor-> GetPositionCoordinate() ->SetCoordinateSystemToWorld();
    fig8KleinTextActor-> GetPositionCoordinate() ->SetValue( 16 ,9.5 ,0);

// ------------------------------------------------------------
// Create a mobius strip
// ------------------------------------------------------------
vtkParametricMobius *mobius=vtkParametricMobius::New();
vtkParametricFunctionSource *mobiusSource=vtkParametricFunctionSource::New();
  mobiusSource-> SetParametricFunction (mobius);
  mobiusSource-> GenerateTextureCoordinatesOn();

vtkPolyDataMapper *mobiusMapper=vtkPolyDataMapper::New();
  mobiusMapper ->SetInputConnection(mobiusSource ->GetOutputPort());

vtkActor *mobiusActor=vtkActor::New();
  mobiusActor-> SetMapper (mobiusMapper);
  mobiusActor-> RotateX (45);
  mobiusActor-> SetPosition (24 ,12 ,0);
  mobiusActor-> SetTexture (texture);

vtkTextMapper *mobiusTextMapper=vtkTextMapper::New();
    mobiusTextMapper-> SetInput ("Mobius");
    mobiusTextMapper-> GetTextProperty() ->SetJustificationToCentered();
    mobiusTextMapper-> GetTextProperty()-> SetVerticalJustificationToCentered();
    mobiusTextMapper-> GetTextProperty()-> SetColor (1, 0, 0);
    mobiusTextMapper-> GetTextProperty()-> SetFontSize (14);
vtkActor2D *mobiusTextActor=vtkActor2D::New();
    mobiusTextActor-> SetMapper (mobiusTextMapper);
    mobiusTextActor-> GetPositionCoordinate() ->SetCoordinateSystemToWorld();
    mobiusTextActor-> GetPositionCoordinate() ->SetValue (24 ,9.5 ,0);

//# ------------------------------------------------------------
//# Create a super toroid
//# ------------------------------------------------------------
vtkParametricSuperToroid *toroid=vtkParametricSuperToroid::New();
  toroid-> SetN1( 2);
  toroid-> SetN2( 3);
vtkParametricFunctionSource *toroidSource=vtkParametricFunctionSource::New();
  toroidSource-> SetParametricFunction (toroid);
  toroidSource-> SetScalarModeToU();

vtkPolyDataMapper *toroidMapper=vtkPolyDataMapper::New();
  toroidMapper-> SetInputConnection (toroidSource-> GetOutputPort());
  toroidMapper ->SetScalarRange (0 ,6.28);

vtkActor *toroidActor=vtkActor::New();
  toroidActor ->SetMapper (toroidMapper);
  toroidActor ->SetPosition( 0, 4, 0);

vtkTextMapper *superToroidTextMapper=vtkTextMapper::New();
    superToroidTextMapper-> SetInput ("Super Toroid");
    superToroidTextMapper-> GetTextProperty()-> SetJustificationToCentered();
    superToroidTextMapper-> GetTextProperty()-> SetVerticalJustificationToCentered();
    superToroidTextMapper-> GetTextProperty()-> SetColor (1,0 ,0);
    superToroidTextMapper-> GetTextProperty()-> SetFontSize (14);
vtkActor2D *superToroidTextActor=vtkActor2D::New();
    superToroidTextActor-> SetMapper(superToroidTextMapper);
    superToroidTextActor-> GetPositionCoordinate() ->SetCoordinateSystemToWorld();
    superToroidTextActor-> GetPositionCoordinate() ->SetValue (0 ,1.5 ,0);

//# ------------------------------------------------------------
//# Create a super ellipsoid
//# ------------------------------------------------------------
vtkParametricSuperEllipsoid *superEllipsoid=vtkParametricSuperEllipsoid::New();
superEllipsoid-> SetXRadius (1.25);
superEllipsoid-> SetYRadius (1.5);
superEllipsoid-> SetZRadius (1.0);
superEllipsoid ->SetN1 (1.1);
superEllipsoid-> SetN2 (1.75);
vtkParametricFunctionSource *superEllipsoidSource=vtkParametricFunctionSource::New();
  superEllipsoidSource ->SetParametricFunction (superEllipsoid);
  superEllipsoidSource ->SetScalarModeToV();

vtkPolyDataMapper *superEllipsoidMapper=vtkPolyDataMapper::New();
  superEllipsoidMapper ->SetInputConnection( superEllipsoidSource ->GetOutputPort());
  superEllipsoidMapper ->SetScalarRange (0 ,3.14);

vtkActor *superEllipsoidActor=vtkActor::New();
  superEllipsoidActor ->SetMapper (superEllipsoidMapper);
  superEllipsoidActor ->SetPosition (8,4 ,0);

vtkTextMapper *superEllipsoidTextMapper=vtkTextMapper::New();
    superEllipsoidTextMapper ->SetInput ("Super Ellipsoid");
    superEllipsoidTextMapper ->GetTextProperty() ->SetJustificationToCentered();
    superEllipsoidTextMapper-> GetTextProperty() ->SetVerticalJustificationToCentered();
    superEllipsoidTextMapper-> GetTextProperty() ->SetColor (1, 0, 0);
    superEllipsoidTextMapper-> GetTextProperty() ->SetFontSize (14);
vtkActor2D *superEllipsoidTextActor=vtkActor2D::New();
    superEllipsoidTextActor ->SetMapper (superEllipsoidTextMapper);
    superEllipsoidTextActor ->GetPositionCoordinate() ->SetCoordinateSystemToWorld();
    superEllipsoidTextActor-> GetPositionCoordinate() ->SetValue (8, 1.5, 0);

// ------------------------------------------------------------
// Create an open 1D spline
// ------------------------------------------------------------
vtkMath *math=vtkMath::New();
vtkPoints *inputPoints=vtkPoints::New();
for (i=0;i<10;i++)
{
    double x = math-> Random (-1 ,1);
    double y = math-> Random (-1 ,1);
    double z = math-> Random (-1 ,1);
    inputPoints ->InsertPoint (i ,x ,y ,z);
}
vtkParametricSpline *spline=vtkParametricSpline::New();
  spline ->SetPoints  (inputPoints);
  spline ->ClosedOff ();
vtkParametricFunctionSource *splineSource=vtkParametricFunctionSource::New();
  splineSource ->SetParametricFunction  (spline);

vtkPolyDataMapper *splineMapper=vtkPolyDataMapper::New();
  splineMapper ->SetInputConnection (splineSource ->GetOutputPort());

vtkActor *splineActor=vtkActor::New();
  splineActor ->SetMapper (splineMapper);
  splineActor ->SetPosition (16 ,4 ,0);
  splineActor ->GetProperty() ->SetColor (1 ,1 ,0);

vtkTextMapper *splineTextMapper=vtkTextMapper::New();
    splineTextMapper ->SetInput ("Open Spline");
    splineTextMapper ->GetTextProperty()-> SetJustificationToCentered();
    splineTextMapper-> GetTextProperty() ->SetVerticalJustificationToCentered();
    splineTextMapper ->GetTextProperty()-> SetColor (1 ,0 ,0);
    splineTextMapper ->GetTextProperty()-> SetFontSize (14);
vtkActor2D *splineTextActor=vtkActor2D::New();
    splineTextActor-> SetMapper (splineTextMapper);
    splineTextActor-> GetPositionCoordinate()-> SetCoordinateSystemToWorld();
    splineTextActor-> GetPositionCoordinate()-> SetValue (16 ,1.5 ,0);

//# ------------------------------------------------------------
//# Create a closed 1D spline
//# ------------------------------------------------------------
vtkParametricSpline *spline2=vtkParametricSpline::New();
  spline2 ->SetPoints( inputPoints);
  spline2 ->ClosedOn();
vtkParametricFunctionSource *spline2Source=vtkParametricFunctionSource::New();
  spline2Source ->SetParametricFunction( spline2);

vtkPolyDataMapper *spline2Mapper=vtkPolyDataMapper::New();
  spline2Mapper ->SetInputConnection (spline2Source ->GetOutputPort());

vtkActor *spline2Actor=vtkActor::New();
  spline2Actor ->SetMapper (spline2Mapper);
  spline2Actor ->SetPosition (24, 4 ,0);
  spline2Actor ->GetProperty() ->SetColor (1, 1 ,0);

vtkTextMapper *spline2TextMapper=vtkTextMapper::New();
    spline2TextMapper ->SetInput ("Closed Spline");
    spline2TextMapper ->GetTextProperty() ->SetJustificationToCentered();
    spline2TextMapper-> GetTextProperty() ->SetVerticalJustificationToCentered();
    spline2TextMapper-> GetTextProperty() ->SetColor (1, 0 ,0);
    spline2TextMapper-> GetTextProperty() ->SetFontSize (14);
vtkActor2D *spline2TextActor=vtkActor2D::New();
    spline2TextActor ->SetMapper (spline2TextMapper);
    spline2TextActor ->GetPositionCoordinate() ->SetCoordinateSystemToWorld();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -