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

(vc_transform_mesh) Add the ability to load composite transforms #45

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions utils/src/TransformMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <boost/program_options.hpp>
#include <itkAffineTransform.h>
#include <itkCompositeTransform.h>
#include <itkTransformFileReader.h>
#include <itkTransformFileWriter.h>
#include <itkTransformMeshFilter.h>

#include "vc/core/filesystem.hpp"
#include "vc/core/io/OBJReader.hpp"
#include "vc/core/io/OBJWriter.hpp"
#include "vc/core/io/MeshIO.hpp"

namespace fs = volcart::filesystem;
namespace po = boost::program_options;
Expand All @@ -21,6 +21,7 @@ using CompositeTransform = itk::CompositeTransform<double, 3>;
using MeshTransformer =
itk::TransformMeshFilter<vc::ITKMesh, vc::ITKMesh, CompositeTransform>;
using TransformWriter = itk::TransformFileWriterTemplate<double>;
using TransformReader = itk::TransformFileReaderTemplate<double>;

int main(int argc, char** argv)
{
Expand All @@ -34,6 +35,7 @@ int main(int argc, char** argv)
"Input mesh file")
("output-mesh,o", po::value<std::string>()->required(),
"Output mesh file")
("input-tfm", po::value<std::string>(), "Input transformation file")
("output-tfm,t", po::value<std::string>(), "Output transformation file");

po::options_description transformOpts("Transformations");
Expand Down Expand Up @@ -69,52 +71,57 @@ int main(int argc, char** argv)
// Load mesh
std::cout << "Loading mesh..." << std::endl;
fs::path inputPath = parsed["input-mesh"].as<std::string>();
vc::io::OBJReader reader;
reader.setPath(inputPath);
auto mesh = reader.read();
auto meshGroup = vc::ReadMesh(inputPath);
auto mesh = meshGroup.mesh;

// Setup composite transform
auto compositeTrans = CompositeTransform::New();

// Build affine transform
auto affine = AffineTransform::New();

// Add translation
Displacement displacement;
displacement[0] = parsed["translate-x"].as<double>();
displacement[1] = parsed["translate-y"].as<double>();
displacement[2] = parsed["translate-z"].as<double>();
affine->Translate(displacement);
// Load transform
if (parsed.count("input-tfm") > 0) {
fs::path tfmPath = parsed["input-tfm"].as<std::string>();
auto readTfm = TransformReader::New();
readTfm->SetFileName(tfmPath.string());
readTfm->Update();
auto it = readTfm->GetTransformList()->begin();
auto* tfm = static_cast<CompositeTransform*>((*it).GetPointer());
compositeTrans->AddTransform(tfm);
}

// Add scale
auto precompose = parsed.count("scale-precompose") > 0;
affine->Scale(parsed["scale"].as<double>(), precompose);
// Build affine transform
else {
auto affine = AffineTransform::New();

// Add translation
Displacement displacement;
displacement[0] = parsed["translate-x"].as<double>();
displacement[1] = parsed["translate-y"].as<double>();
displacement[2] = parsed["translate-z"].as<double>();
affine->Translate(displacement);

// Add scale
auto precompose = parsed.count("scale-precompose") > 0;
affine->Scale(parsed["scale"].as<double>(), precompose);

// Build composite transform
compositeTrans->AddTransform(affine);
}

// Build composite transform
compositeTrans->AddTransform(affine);
// Simplify the transform
compositeTrans->FlattenTransformQueue();

// Apply the composite transform to the mesh
std::cout << "Applying transform..." << std::endl;
auto output = vc::ITKMesh::New();
auto transformer = MeshTransformer::New();
transformer->SetInput(mesh);
transformer->SetOutput(output);
transformer->SetTransform(compositeTrans);
transformer->Update();
auto output = transformer->GetOutput();

// Write the new mesh
std::cout << "Writing mesh..." << std::endl;
fs::path outputPath = parsed["output-mesh"].as<std::string>();
vc::io::OBJWriter writer;
writer.setPath(outputPath);
writer.setMesh(output);
try {
writer.setUVMap(reader.getUVMap());
writer.setTexture(reader.getTextureMat());
} catch (...) {
// Do nothing if there's no UV map or Texture image
}
writer.write();
vc::WriteMesh(outputPath, output, meshGroup.uv, meshGroup.texture);

///// Write the final transformations /////
if (parsed.count("output-tfm") > 0) {
Expand Down