Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for vtk python wrapping #269

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions modules/VTK/include/vtkKernels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of the statismo library.
*
* Author: Marcel Luethi (marcel.luethi@unibas.ch)
*
* Copyright (c) 2011 University of Basel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the project's author nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#ifndef __VTK_KERNELS_H
#define __VTK_KERNELS_H

#include "Kernels.h"
#include "vtkHelper.h"
#include "vtkStandardMeshRepresenter.h"

namespace statismo {

class GaussianKernel: public ScalarValuedKernel<vtkPoint> {
public:

GaussianKernel(double sigma) : m_sigma(sigma), m_sigma2(sigma * sigma) {
}

inline double operator()(const vtkPoint& x, const vtkPoint& y) const {
VectorType r(3);
r << x[0] - y[0], x[1] - y[1], x[2] - y[2];
return exp(-r.dot(r) / m_sigma2);
}

std::string GetKernelInfo() const {
std::ostringstream os;
os << "GaussianKernel(" << m_sigma << ")";
return os.str();
}

private:

double m_sigma;
double m_sigma2;
};

} // namespace statismo


#endif // __VTK_KERNELS_H
6 changes: 1 addition & 5 deletions modules/VTK/src/vtkStandardMeshRepresenter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ vtkStandardMeshRepresenter::Clone() const {

void vtkStandardMeshRepresenter::Load(const H5::Group& fg) {
vtkPolyData* ref = 0;

std::string repName = HDF5Utils::readStringAttribute(fg, "name");
if (repName == "vtkPolyDataRepresenter" || repName == "itkMeshRepresenter") {
ref = LoadRefLegacy(fg);
Expand All @@ -96,7 +95,6 @@ void vtkStandardMeshRepresenter::Load(const H5::Group& fg) {


vtkPolyData* vtkStandardMeshRepresenter::LoadRef(const H5::Group& fg) const {

statismo::MatrixType vertexMat;
HDF5Utils::readMatrix(fg, "./points", vertexMat);

Expand Down Expand Up @@ -173,7 +171,6 @@ vtkPolyData* vtkStandardMeshRepresenter::LoadRef(const H5::Group& fg) const {

vtkPolyData* vtkStandardMeshRepresenter::LoadRefLegacy(const H5::Group& fg) const {
std::string tmpfilename = statismo::Utils::CreateTmpName(".vtk");

HDF5Utils::getFileFromHDF5(fg, "./reference", tmpfilename.c_str());
vtkPolyData* pd = vtkPolyData::New();
vtkPolyDataReader* reader = vtkPolyDataReader::New();
Expand Down Expand Up @@ -272,8 +269,8 @@ statismo::VectorType vtkStandardMeshRepresenter::PointToVector(const PointType&

statismo::VectorType vtkStandardMeshRepresenter::SampleToSampleVector(
DatasetConstPointerType _sample) const {
assert(m_reference != 0);

assert(m_reference != 0);
vtkPolyData* sample = const_cast<vtkPolyData*>(_sample);

VectorType sampleVec = VectorType::Zero(
Expand All @@ -290,7 +287,6 @@ statismo::VectorType vtkStandardMeshRepresenter::SampleToSampleVector(

vtkStandardMeshRepresenter::DatasetPointerType vtkStandardMeshRepresenter::SampleVectorToSample(
const VectorType& sample) const {

assert(m_reference != 0);

vtkPolyData* reference = const_cast<vtkPolyData*>(m_reference);
Expand Down
20 changes: 20 additions & 0 deletions modules/VTK/wrapping/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ install( TARGETS _statismo_VTK
LIBRARY DESTINATION ${INSTALL_LIB_DIR}
ARCHIVE DESTINATION ${INSTALL_LIB_DIR}
)

# Extract the python install directory.
# Source: https://stackoverflow.com/a/40006251/3388962
execute_process(
COMMAND "${PYTHON_EXECUTABLE}" -c
"from distutils import sysconfig as sc;\
print(sc.get_python_lib(prefix='', plat_specific=True))"
OUTPUT_VARIABLE PYTHON_SITE
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Create a __init__ file for the python module.
file(WRITE "${swig_outdir}/__init__.py" "from statismo.statismo_VTK import *" )

install( FILES
"${swig_outdir}/_statismo_VTK.so"
"${swig_outdir}/statismo_VTK.py"
"${swig_outdir}/__init__.py"
DESTINATION "${CMAKE_INSTALL_PREFIX}/${PYTHON_SITE}/statismo"
COMPONENT Development
)