[vtkusers] tubesurface cutting to ensure the cube periodicity
番茄骑士
522250912 at qq.com
Sun Dec 30 09:21:40 EST 2012
what i want to do is just like above picture.
for now i just generate may fibres with out above operation:
i dont know how to do with each one cut and move like above. and i don't know how to save so many tubemapper as a stl file.
so what vtk function should i use? or any one give me example code.
my codes show as bellow:
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkStripper.h>
#include <vtkCutter.h>
#include <vtkSplineFilter.h>
#include <vtkSpline.h>
#include <vtkPlane.h>
#include <vtkSphereSource.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkTubeFilter.h>
#include <vtkKochanekSpline.h>
#include <vtkMath.h>
#include"math.h"
#include <vtkParametricSpline.h>
#include <vtkParametricFunctionSource.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPointSource.h>
void normalize(float *a)
{
float length=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
if(length>0)
{
a[0]=a[0]/length;
a[1]=a[1]/length;
a[2]=a[2]/length;
}
else
{
a[0]=0;
a[1]=0;
a[2]=0;
}
}
float* cross(float* a,float *b,float *re)
{
re[0]=a[1]*b[2]-a[2]*b[1];
re[1]=a[2]*b[0]-a[0]*b[2];
re[2]=a[0]*b[1]-a[1]*b[0];
normalize(re);
return re;
}
int main(int argc, char *argv[])
{
vtkMath::RandomSeed(1);
float beta=1;
float PI=3.1415926;
int const N=5;
int fibernumber=130;
float fiberradius=0.1;
float boxsize=10;
float fiberlength=10;
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(0.8,0.83,0.9);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(style);
for(int k=0;k<fibernumber;k++)
{
double x1=vtkMath::Random(0,1);
double x2=vtkMath::Random(0,1);
double x=vtkMath::Random(-boxsize/2.0,boxsize/2.0);
double y=vtkMath::Random(-boxsize/2.0,boxsize/2.0);
double z=vtkMath::Random(-boxsize/2.0,boxsize/2.0);
double costheta=x1/sqrt(x1*x1-beta*beta*x1*x1+beta*beta);
double theta=acos(costheta);
double phi=2*PI*x2;
vtkSmartPointer<vtkPoints> points=
vtkSmartPointer<vtkPoints>::New();
points->SetNumberOfPoints(N);
float step=fiberlength/N;
float dx=sin(theta)*cos(phi);
float dy=sin(theta)*sin(phi);
float dz=cos(theta);
float up[3]={0,0,1};
float T[3]={dx,dy,dz};
float N1[3],B1[3];
cross(T,up,N1);
cross(N1,T,B1);
float points2[N][3];
for(int i=0;i<N;i++)
{
points2[i][0]=x+(i-N/2)*dx*step;
points2[i][1]=y+(i-N/2)*dy*step;
points2[i][2]=z+(i-N/2)*dz*step;
if(N%2!=0)
{
float offset=step/2.0;
points2[i][0]+=-offset*dx;
points2[i][1]+=-offset*dy;
points2[i][2]+=-offset*dz;
}
if(i!=0&&i!=(N-1))
{
float angle=vtkMath::Random(0,2*PI);
float altitude=vtkMath::Random(-fiberlength/8,fiberlength/8);
points2[i][0]+=altitude*(N1[0]*cos(angle)+B1[0]*sin(angle));
points2[i][1]+=altitude*(N1[1]*cos(angle)+B1[1]*sin(angle));
points2[i][2]+=altitude*(N1[2]*cos(angle)+B1[2]*sin(angle));
}
points->InsertPoint(i,points2[i][0],points2[i][1],points2[i][2]);
//points->InsertNextPoint(points2[i]);
}
vtkSmartPointer<vtkCellArray> lines =
vtkSmartPointer<vtkCellArray>::New();
lines->InsertNextCell(N);
for (int i = 0; i < N; i++)
{
lines->InsertCellPoint(i);
}
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(lines);
vtkSmartPointer<vtkKochanekSpline> xSpline =
vtkSmartPointer<vtkKochanekSpline>::New();
xSpline->SetDefaultTension(0);
vtkSmartPointer<vtkKochanekSpline> ySpline =
vtkSmartPointer<vtkKochanekSpline>::New();
ySpline->SetDefaultTension(0);
vtkSmartPointer<vtkKochanekSpline> zSpline =
vtkSmartPointer<vtkKochanekSpline>::New();
zSpline->SetDefaultTension(0);
vtkSmartPointer<vtkParametricSpline> spline =
vtkSmartPointer<vtkParametricSpline>::New();
spline->SetXSpline(xSpline);
spline->SetYSpline(ySpline);
spline->SetZSpline(zSpline);
spline->SetPoints(points);
vtkSmartPointer<vtkParametricFunctionSource> functionSource =
vtkSmartPointer<vtkParametricFunctionSource>::New();
functionSource->SetParametricFunction(spline);
functionSource->Update();
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> lineMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
lineMapper->SetInputConnection(functionSource->GetOutputPort());
vtkSmartPointer<vtkActor> lineActor =
vtkSmartPointer<vtkActor>::New();
lineActor->GetProperty()->SetColor(1,0,1); // Give some color to the line
lineActor->SetMapper(lineMapper);
// Create a tube (cylinder) around the line
vtkSmartPointer<vtkTubeFilter> tubeFilter =
vtkSmartPointer<vtkTubeFilter>::New();
tubeFilter->SetInputConnection(functionSource->GetOutputPort());
tubeFilter->SetRadius(fiberradius); //default is .5
tubeFilter->SetNumberOfSides(15);
tubeFilter->CappingOn();
tubeFilter->Update();
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> tubeMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
tubeMapper->SetInputConnection(tubeFilter->GetOutputPort());
vtkSmartPointer<vtkActor> tubeActor =
vtkSmartPointer<vtkActor>::New();
tubeActor->GetProperty()->SetOpacity(0.5); //Make the tube have some transparency.
tubeActor->GetProperty()->SetColor(0.8,0.7,0.2);
tubeActor->SetMapper(tubeMapper);
// Add the actor to the scene
renderer->AddActor(tubeActor);
renderer->AddActor(lineActor);
}
// Render and interact
renderWindow->SetSize(600, 600);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121230/a46e7144/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 129373 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121230/a46e7144/attachment.jpeg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 33770 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121230/a46e7144/attachment-0001.jpeg>
More information about the vtkusers
mailing list