/*////////////////////////////////////////////////////////////////////////////// // 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 #include #include #include #include #include #include #include #include #include #include #include #include "Math/vtkDense.h" #include "Vtk/Math/vtkQuadMesh.h" #include 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().clear(); for (int i = 0; i < number_of_points; ++i) { double *point = m_Poly->GetPoint(i); m_content.Points().push_back(Vector3f(point[0], point[1], point[2])); } m_content.Quads().resize(number_of_quads); m_Poly->GetPolys()->InitTraversal(); vtkSmartPointer idList = vtkSmartPointer::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 points = vtkSmartPointer::New(); points->SetNumberOfPoints(number_of_points); for (vtkIdType i = 0; i < number_of_points; i++) { Vector3f p = m_content.Points().at(i); points->SetPoint(i, p(0), p(1), p(2)); } vtkSmartPointer polys = vtkSmartPointer::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(); } void vtkQuadMesh::contentUpdate() { vtkMatrix4x4 *vmat = m_Actor->GetUserMatrix(); if (!vmat) { vtkNew mat; m_Actor->SetUserMatrix(mat); vmat = mat; } Matrix4f transform = m_content.GetWorldMatrix(); Matrix4fToVtk(transform, vmat); uLib2vtk_update(); m_Poly->Modified(); m_Actor->GetMapper()->Update(); Puppet::Update(); } void vtkQuadMesh::Update() { vtkMatrix4x4 *vmat = m_Actor->GetUserMatrix(); if (!vmat) return; Matrix4f transform = VtkToMatrix4f(vmat); m_content.SetMatrix(transform); m_content.Updated(); } // -------------------------------------------------------------------------- // vtkQuadMesh::vtkQuadMesh(vtkQuadMesh::Content &content) : m_content(content), m_Poly(vtkPolyData::New()), m_Actor(vtkActor::New()) { vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputData(m_Poly); m_Actor->SetMapper(mapper); vtkNew vmat; Matrix4fToVtk(m_content.GetWorldMatrix(), vmat); m_Actor->SetUserMatrix(vmat); this->SetProp(m_Actor); Object::connect(&m_content, &Content::Updated, this, &vtkQuadMesh::contentUpdate); this->contentUpdate(); } vtkQuadMesh::~vtkQuadMesh() { Object::disconnect(&m_content, &Content::Updated, this, &vtkQuadMesh::contentUpdate); m_Poly->Delete(); m_Actor->Delete(); } void vtkQuadMesh::ReadFromFile(const char *filename) { vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(filename); reader->Update(); m_Poly->DeepCopy(reader->GetOutput()); vtk2uLib_update(); } void vtkQuadMesh::ReadFromXMLFile(const char *filename) { vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(filename); reader->Update(); m_Poly->DeepCopy(reader->GetOutput()); vtk2uLib_update(); } void vtkQuadMesh::ReadFromObjFile(const char *filename) { vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(filename); reader->Update(); m_Poly->DeepCopy(reader->GetOutput()); vtk2uLib_update(); } void vtkQuadMesh::ReadFromStlFile(const char *filename) { vtkSmartPointer reader = vtkSmartPointer::New(); reader->SetFileName(filename); reader->Update(); m_Poly->DeepCopy(reader->GetOutput()); vtk2uLib_update(); } vtkPolyData *vtkQuadMesh::GetPolyData() const { return m_Poly; } } // namespace Vtk } // namespace uLib