[vtkusers] Normals and curvatures of cloud points
Ismail Elkhrachy
I.Elkhrachy at tu-bs.de
Wed Nov 22 12:26:05 EST 2006
Dear All,
I have a problem to calculate the normal vector and display it , and the
curvature (Gaussian and mean) of my point cloud (xyz). My code is listed
below, any one can help?
Many thanks,
The code is:
// This example demonstrate the use of VTK data arrays as attribute
// data as well as field data. It creates geometry (vtkPolyData) as
// well as attribute data explicitly.
// first include the required header files for the vtk classes we are using
#define M_PI 3.141592653589793238
#include "vtkPointData.h"
#include "vtkVectorNorm.h"
#include "vtkCellData.h"
#include "vtkContourFilter.h"
#include "vtkTriangleFilter.h"
#include "vtkPolyDataToPolyDataFilter.h"
#include "vtkPolyDataNormals.h"
#include "vtkMatrix4x4.h"
#include "vtkMath.h"
#include "vtkActor.h"
#include "vtkDoubleArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkProperty.h"
#include "vtkLandmarkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkMaskPoints.h"
//#include "vtkNormals.h"
#include <iostream.h>
#include <iomanip>
#include <iostream>
#include <time.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<fstream.h>
using namespace std;
int main()
{
FILE *fp;
FILE *fp2;
std:: cout << "Program Starting" << endl;
vtkPolyData* polyData1 = vtkPolyData::New();
vtkPoints* cloud1 = vtkPoints::New();
static float mypoint_1[3];
//?????????????????????????????????????????????????????????????????ß
fp=fopen("data\\scan4.nor","rb");
if(fp==NULL)
{
puts("Cannot open firdt file");
exit(0);
}
int PointNumberInScan1 = 0;
while(fscanf (fp,"%f %f
%f\n",&mypoint_1[0],&mypoint_1[1],&mypoint_1[2]) != EOF)
{
// std::cout << mypoint_1[0] << ", " << mypoint_1[1] << ", "
<< mypoint_1[2] << std::endl;
cloud1->InsertNextPoint(mypoint_1);
PointNumberInScan1 +=1;
}
fclose(fp);
polyData1->SetPoints(cloud1);
int num_of_points1 = polyData1->GetPoints()->GetNumberOfPoints();
std:: cout << "in all, num of points in cloud 1 is " <<
num_of_points1 << endl;
vtkPolyData* polyData2 = vtkPolyData::New();
vtkPoints* cloud2 = vtkPoints::New();
static float mypoint_2[3];
fp2=fopen("data\\scan3.nor","rb");
if(fp2==NULL)
{
puts("Cannot open scan 2 file");
exit(0);
}
int PointNumberInScan2 = 0;
while(fscanf (fp2,"%f %f
%f\n",&mypoint_2[0],&mypoint_2[1],&mypoint_2[2]) != EOF)
{
// std::cout << mypoint_2[0] << ", " << mypoint_2[1] << ", " <<
mypoint_2[2] << std::endl;
cloud2->InsertNextPoint(mypoint_2);
PointNumberInScan2 +=1;
}
fclose(fp2);
int numPoints =0;
if (PointNumberInScan2 > PointNumberInScan2){
numPoints = PointNumberInScan2;
}
else {
numPoints = PointNumberInScan1;
}
polyData2->SetPoints(cloud2);
int num_of_points2 = polyData2->GetPoints()->GetNumberOfPoints();
std:: cout << "in all, num of points in cloud 2 is " <<
num_of_points2 << endl;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
vtkTriangleFilter *triangle1 = vtkTriangleFilter::New();
triangle1->SetInput(polyData1);
vtkPolyDataNormals *Normals1 = vtkPolyDataNormals::New ();
Normals1->SetInput(triangle1->GetOutput());
Normals1->SetFeatureAngle(60);
Normals1->FlipNormalsOff();
Normals1->Update();
for (int pointID=0; pointID <
Normals1->GetOutput()->GetNumberOfPoints();
pointID++) {
float *point =
(float*)triangle1->GetOutput()->GetPointData()->GetNormals()->GetTuple(pointID);
printf("x-> %f",(float)point[0]);
printf("y-> %f",(float)point[1]);
printf("z-> %f",(float)point[2]);
}
vtkPolyDataMapper *Normals1Mapper = vtkPolyDataMapper::New();
Normals1Mapper->SetInput(Normals1->GetOutput());;
Normals1Mapper->ScalarVisibilityOn();
Normals1Mapper->SetScalarRange (1, 1150); //((gradient->
// Normals1Mapper->GetOutput())->GetScalarRange());
// Create actor.
vtkActor *Normals1Actor = vtkActor::New();
Normals1Actor->SetMapper(Normals1Mapper);
Normals1Actor->GetProperty()->SetColor(1,0.49,0.25);
// Create the rendering objects and render window.
vtkRenderer* ren = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->SetPosition(512,0);
renWin->AddRenderer(ren);
// an interactor
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
// add the actor to the scene
ren->SetBackground(1,1,0); // Background color white
ren->AddActor(Normals1Actor);
iren->Initialize();
iren->Start();
Normals1Actor->Delete();
ren->Delete();
renWin->Delete();
iren->Delete();
return 0;
}
--
M.Sc. Ismail Elkhrachy
Institut für Geodäsie und Photogrammetrie
Technische Universität Braunschweig
Gaußstr. 22
38106 Braunschweig
Germany
Mob. : 0049 0163 3623486
Tel. : 0049 0531 3917497
Fax : 0049 0531 3917499
E-mail : I.Elkhrachy at tu-bs.de, I.Elkhrachy at yahoo.com
More information about the vtkusers
mailing list