Can't write polydata to file...

Hi there. Can’t make my VTK module write polydata. Any help welcome. Thanks.

/usr/bin/ld: CMakeFiles/createmesh.dir/MeshWriter.cxx.o: undefined reference to symbol “_ZN9vtkWriter12SetInputDataEP13vtkDataObject”
/usr/bin/ld: /home/3484681/Area de Trabalho/vtk-master/build/lib/libvtkIOCore-9.3.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/createmesh.dir/build.make:347: createmesh] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/createmesh.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

#include “MeshWriter.h” // Replace ‘path/to/’ with the actual path
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkPLYWriter.h>
#include <vtkSTLWriter.h>
#include <vtkOBJWriter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyDataWriter.h>

#include
#include

#include <vtkPolyDataReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkOBJReader.h>
#include
#include
#include <vtkAlgorithmOutput.h>
#include <vtkDataReader.h>
#include <vtkPolyDataReader.h>

std::string GetFileFormat(const std::string& filename) {
// Find the position of the last dot in the filename.
size_t dotPos = filename.find_last_of(‘.’);

  // Check if a dot was found and extract the format if found.
  if (dotPos != std::string::npos) {
      // Use substr to get the characters following the last dot.
      return filename.substr(dotPos + 1);
  }
  
  // If no dot is found, return an empty string to indicate no format.
  return "";

}

MeshWriter::MeshWriter() {
// Constructor implementation, if needed
}

MeshWriter::~MeshWriter() {
// Destructor implementation, if needed
}

bool MeshWriter::WriteVTK(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer = vtkSmartPointer::New();
writer->SetFileName(fileName.c_str());
writer->SetInputData(polydata);
return writer->Write();
}

bool MeshWriter::WriteVTP(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer = vtkSmartPointer::New();
writer->SetFileName(fileName.c_str());
writer->SetInputData(polydata);
return writer->Write();
}

bool MeshWriter::WriteSTL(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer = vtkSmartPointer::New();
writer->SetFileName(fileName.c_str());
writer->SetInputData(polydata);
return writer->Write();
}

bool MeshWriter::WriteOBJ(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer = vtkSmartPointer::New();
writer->SetFileName(fileName.c_str());
writer->SetInputData(polydata);
return writer->Write();
}

bool MeshWriter::WritePLY(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer = vtkSmartPointer::New();
writer->SetFileName(fileName.c_str());
writer->SetInputData(polydata);
return writer->Write();
}

bool MeshWriter::WriteMesh(const vtkSmartPointer& polydata, const std::string& fileName) {
vtkSmartPointer writer;

  std::string format ;
  format= GetFileFormat(fileName);
  
  if (format == "vtk") {
      return WriteVTK(polydata, fileName);
  } else if (format == "vtp") {
      return WriteVTP(polydata, fileName);
  } else if (format == "stl") {
      return WriteSTL(polydata, fileName);
  } else if (format == "obj") {
      return WriteOBJ(polydata, fileName);
  } else {
      return WritePLY(polydata, fileName);
  }

}

MeshWriter.cxx (5.9 KB)
MeshWriter.h (1.5 KB)
CMakeLists.txt (2.6 KB)

It compiled with these changes to the cmaklist file:

#PRISMAMESH CMAKELISTS FILE

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(createmesh)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_ENTENSIONS OFF)
set(CMAKE_BUILD_TYPE Release)

set(CMAKE_CXX_FLAGS “”)
set(CMAKE_CXX_FLAGS_DEBUG “-g”)
set(CMAKE_CXX_FLAGS_RELEASE “-O3”)

Find ITK.

find_package(ITK REQUIRED)
include({ITK_USE_FILE}) if (ITKVtkGlue_LOADED) find_package(VTK REQUIRED) if(VTK_VERSION VERSION_LESS "8.90") include({VTK_USE_FILE})
endif()
endif()

Try to find VTK, but don’t use the default paths.

find_package(VTK QUIET NO_DEFAULT_PATH)

If VTK is not found, set the directories manually.

if(NOT VTK_FOUND)
set(VTK_DIR “/home/3484681/Área de Trabalho/vtk-master/vtk-9.3”)
set(VTK_INCLUDE_DIRS “/home/3484681/Área de Trabalho/vtk-master/vtk-9.3/include/vtk-9.3”)
endif()

FIND VTK PACKAGE

find_package(VTK REQUIRED
COMPONENTS
${VTK_LIBRARIES}
IOCore
CommonColor
CommonCore
CommonDataModel
IOImage
IOXML
IOXMLParser
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
IOGeometry
CommonExecutionModel
vtksys
CommonSystem
CommonMisc
IOPLY

#FILTERING
FiltersCore
FiltersGeneral
FiltersGeometry
ImagingStatistics
)

set(MPI_FOUND FALSE)

if (NOT VTK_FOUND)
message(FATAL_ERROR “PRISMAMESH: Unable to find the VTK build folder.”)
endif()

#FIND MPI
find_package(MPI REQUIRED)
if(MPI_FOUND)
message(“MPI found.”)
else()
message(“MPI not found.”)
endif()

MANUALLY ENABLE/DISENABLE MPI SUPPORT

option(ENABLE_MPI “Enable MPI support” OFF)
set(ENABLE_MPI OFF)

if(ENABLE_MPI)
message(“MPI support is enabled”)
else()
message(“MPI support is not enabled”)
endif()

if(VTK_VERSION VERSION_LESS “8.90”)
include(${VTK_USE_FILE})
endif()

add_executable(createmesh createmesh.cxx segmentation_io.cxx visualize_polidata.cxx MeshWriter.cxx)

Use the find_package command to search for Boost

find_package(Boost REQUIRED)

Specify the Boost library path

set(BOOST_LIBRARY_DIRS “/home/3484681/miniconda3/envs/vtkenv/include/boost”)

Check if Boost is found and include the library path

if(Boost_FOUND)
target_include_directories(createmesh PRIVATE ${Boost_INCLUDE_DIRS})

# Specify the Boost library path
target_link_directories(createmesh PRIVATE ${BOOST_LIBRARY_DIRS})

# Link against Boost libraries
target_link_libraries(createmesh PRIVATE ${Boost_LIBRARIES})

endif()

target_include_directories(createmesh PRIVATE {VTK_INCLUDE_DIRS} {CMAKE_SOURCE_DIR}/include)
target_link_libraries(createmesh PRIVATE {VTK_LIBRARIES} {ITK_LIBRARIES})

if(ENABLE_MPI)
target_include_directories(createmesh PRIVATE {MPI_CXX_INCLUDE_PATH} SYSTEM {MPI_INCLUDE_PATH})
target_link_libraries(createmesh PRIVATE ${MPI_LIBRARIES})
endif()

if(ENABLE_MPI AND MPI_FOUND)
#Define the WriteParallelMesh function
target_sources(createmesh PRIVATE write_parallel.cxx)
#Add the MPI_ENABLED definition to your compile flags
add_compile_definitions(MPI_ENABLED)
endif()

vtk_module_autoinit is needed

vtk_module_autoinit(
TARGETS createmesh
MODULES ${VTK_LIBRARIES}
)
CMakeLists.txt (3.3 KB)