add Normals to meshes
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
set(MATH_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkStructuredGrid.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkQuadMesh.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.cpp
|
||||
PARENT_SCOPE)
|
||||
|
||||
@@ -13,6 +14,7 @@ set(MATH_HEADERS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkHLineRepresentation.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkStructuredGrid.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkQuadMesh.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.h
|
||||
PARENT_SCOPE)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
set(TESTS
|
||||
vtkStructuredGridTest
|
||||
vtkTriangleMeshTest
|
||||
vtkQuadMeshTest
|
||||
vtkVoxImageTest
|
||||
)
|
||||
|
||||
|
||||
56
src/Vtk/Math/testing/vtkQuadMeshTest.cpp
Normal file
56
src/Vtk/Math/testing/vtkQuadMeshTest.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
------------------------------------------------------------------
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#include "Vtk/Math/vtkQuadMesh.h"
|
||||
#include "Math/QuadMesh.h"
|
||||
#include "Vtk/uLibVtkViewer.h"
|
||||
|
||||
#define BOOST_TEST_MODULE VtkQuadMeshTest
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace uLib;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(vtkQuadMeshConstruction) {
|
||||
QuadMesh mesh;
|
||||
|
||||
mesh.AddPoint(Vector3f(0, 0, 0));
|
||||
mesh.AddPoint(Vector3f(1, 0, 0));
|
||||
mesh.AddPoint(Vector3f(1, 1, 0));
|
||||
mesh.AddPoint(Vector3f(0, 1, 0));
|
||||
|
||||
mesh.AddQuad(Vector4i(0, 1, 2, 3));
|
||||
|
||||
Vtk::vtkQuadMesh v_mesh(mesh);
|
||||
v_mesh.Update();
|
||||
|
||||
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
|
||||
Vtk::Viewer viewer;
|
||||
viewer.AddPuppet(v_mesh);
|
||||
viewer.Start();
|
||||
}
|
||||
|
||||
BOOST_CHECK_EQUAL(mesh.Points().size(), 4u);
|
||||
BOOST_CHECK_EQUAL(mesh.Quads().size(), 1u);
|
||||
}
|
||||
171
src/Vtk/Math/vtkQuadMesh.cpp
Normal file
171
src/Vtk/Math/vtkQuadMesh.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
------------------------------------------------------------------
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <vtkSmartPointer.h>
|
||||
|
||||
#include <vtkOBJReader.h>
|
||||
#include <vtkPolyDataReader.h>
|
||||
#include <vtkSTLReader.h>
|
||||
#include <vtkXMLPolyDataReader.h>
|
||||
|
||||
#include <vtkActor.h>
|
||||
#include <vtkCellArray.h>
|
||||
#include <vtkPoints.h>
|
||||
#include <vtkPolyData.h>
|
||||
#include <vtkPolyDataMapper.h>
|
||||
|
||||
#include "Vtk/Math/vtkQuadMesh.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
void vtkQuadMesh::vtk2uLib_update() {
|
||||
vtkIdType number_of_points = m_Poly->GetNumberOfPoints();
|
||||
vtkIdType number_of_quads = m_Poly->GetNumberOfPolys();
|
||||
|
||||
std::cout << "//////\n"
|
||||
<< "number of points = " << number_of_points << "\n"
|
||||
<< "number of quads = " << number_of_quads << "\n"
|
||||
<< "//////\n";
|
||||
|
||||
m_content.Points().resize(number_of_points);
|
||||
for (int i = 0; i < number_of_points; ++i) {
|
||||
double *point = m_Poly->GetPoint(i);
|
||||
m_content.Points()[i](0) = point[0];
|
||||
m_content.Points()[i](1) = point[1];
|
||||
m_content.Points()[i](2) = point[2];
|
||||
}
|
||||
|
||||
m_content.Quads().resize(number_of_quads);
|
||||
m_Poly->GetPolys()->InitTraversal();
|
||||
vtkSmartPointer<vtkIdList> idList = vtkSmartPointer<vtkIdList>::New();
|
||||
for (int i = 0; i < number_of_quads; ++i) {
|
||||
m_Poly->GetPolys()->GetNextCell(idList);
|
||||
if (idList->GetNumberOfIds() == 4) {
|
||||
m_content.Quads()[i](0) = idList->GetId(0);
|
||||
m_content.Quads()[i](1) = idList->GetId(1);
|
||||
m_content.Quads()[i](2) = idList->GetId(2);
|
||||
m_content.Quads()[i](3) = idList->GetId(3);
|
||||
}
|
||||
}
|
||||
m_Poly->Modified();
|
||||
m_Actor->GetMapper()->Update();
|
||||
}
|
||||
|
||||
void vtkQuadMesh::uLib2vtk_update() {
|
||||
vtkIdType number_of_points = m_content.Points().size();
|
||||
vtkIdType number_of_quads = m_content.Quads().size();
|
||||
|
||||
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
|
||||
points->SetNumberOfPoints(number_of_points);
|
||||
for (vtkIdType i = 0; i < number_of_points; i++) {
|
||||
double x, y, z;
|
||||
x = m_content.Points().at(i)(0);
|
||||
y = m_content.Points().at(i)(1);
|
||||
z = m_content.Points().at(i)(2);
|
||||
points->SetPoint(i, x, y, z);
|
||||
}
|
||||
|
||||
vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New();
|
||||
for (vtkIdType i = 0; i < number_of_quads; i++) {
|
||||
vtkIdType a, b, c, d;
|
||||
a = m_content.Quads().at(i)(0);
|
||||
b = m_content.Quads().at(i)(1);
|
||||
c = m_content.Quads().at(i)(2);
|
||||
d = m_content.Quads().at(i)(3);
|
||||
polys->InsertNextCell(4);
|
||||
polys->InsertCellPoint(a);
|
||||
polys->InsertCellPoint(b);
|
||||
polys->InsertCellPoint(c);
|
||||
polys->InsertCellPoint(d);
|
||||
}
|
||||
|
||||
m_Poly->SetPoints(points);
|
||||
m_Poly->SetPolys(polys);
|
||||
m_Poly->Modified();
|
||||
m_Actor->GetMapper()->Update();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
|
||||
vtkQuadMesh::vtkQuadMesh(vtkQuadMesh::Content &content)
|
||||
: m_content(content), m_Poly(vtkPolyData::New()), m_Actor(vtkActor::New()) {
|
||||
vtkSmartPointer<vtkPolyDataMapper> mapper =
|
||||
vtkSmartPointer<vtkPolyDataMapper>::New();
|
||||
mapper->SetInputData(m_Poly);
|
||||
m_Actor->SetMapper(mapper);
|
||||
this->SetProp(m_Actor);
|
||||
}
|
||||
|
||||
vtkQuadMesh::~vtkQuadMesh() {
|
||||
m_Poly->Delete();
|
||||
m_Actor->Delete();
|
||||
}
|
||||
|
||||
void vtkQuadMesh::ReadFromFile(const char *filename) {
|
||||
vtkSmartPointer<vtkPolyDataReader> reader =
|
||||
vtkSmartPointer<vtkPolyDataReader>::New();
|
||||
reader->SetFileName(filename);
|
||||
reader->Update();
|
||||
m_Poly->DeepCopy(reader->GetOutput());
|
||||
vtk2uLib_update();
|
||||
}
|
||||
|
||||
void vtkQuadMesh::ReadFromXMLFile(const char *filename) {
|
||||
vtkSmartPointer<vtkXMLPolyDataReader> reader =
|
||||
vtkSmartPointer<vtkXMLPolyDataReader>::New();
|
||||
reader->SetFileName(filename);
|
||||
reader->Update();
|
||||
m_Poly->DeepCopy(reader->GetOutput());
|
||||
vtk2uLib_update();
|
||||
}
|
||||
|
||||
void vtkQuadMesh::ReadFromObjFile(const char *filename) {
|
||||
vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
|
||||
reader->SetFileName(filename);
|
||||
reader->Update();
|
||||
m_Poly->DeepCopy(reader->GetOutput());
|
||||
vtk2uLib_update();
|
||||
}
|
||||
|
||||
void vtkQuadMesh::ReadFromStlFile(const char *filename) {
|
||||
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
|
||||
reader->SetFileName(filename);
|
||||
reader->Update();
|
||||
m_Poly->DeepCopy(reader->GetOutput());
|
||||
vtk2uLib_update();
|
||||
}
|
||||
|
||||
vtkPolyData *vtkQuadMesh::GetPolyData() const { return m_Poly; }
|
||||
|
||||
void vtkQuadMesh::Update() { uLib2vtk_update(); }
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
69
src/Vtk/Math/vtkQuadMesh.h
Normal file
69
src/Vtk/Math/vtkQuadMesh.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
------------------------------------------------------------------
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library.
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#ifndef VTKQUADMESH_H
|
||||
#define VTKQUADMESH_H
|
||||
|
||||
#include "Math/QuadMesh.h"
|
||||
#include "Vtk/uLibVtkInterface.h"
|
||||
|
||||
class vtkPolyData;
|
||||
class vtkActor;
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
class vtkQuadMesh : public Puppet, public Polydata {
|
||||
typedef QuadMesh Content;
|
||||
|
||||
public:
|
||||
vtkQuadMesh(Content &content);
|
||||
~vtkQuadMesh();
|
||||
|
||||
void ReadFromFile(const char *filename);
|
||||
|
||||
void ReadFromXMLFile(const char *filename);
|
||||
|
||||
void ReadFromObjFile(const char *filename);
|
||||
|
||||
void ReadFromStlFile(const char *filename);
|
||||
|
||||
virtual class vtkPolyData *GetPolyData() const;
|
||||
|
||||
void Update();
|
||||
|
||||
private:
|
||||
void vtk2uLib_update();
|
||||
void uLib2vtk_update();
|
||||
|
||||
QuadMesh &m_content;
|
||||
vtkPolyData *m_Poly;
|
||||
vtkActor *m_Actor;
|
||||
};
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
|
||||
#endif // VTKQUADMESH_H
|
||||
Reference in New Issue
Block a user