diff --git a/utils/src/TransformMesh.cpp b/utils/src/TransformMesh.cpp index 3783f1bba..b1514f85b 100644 --- a/utils/src/TransformMesh.cpp +++ b/utils/src/TransformMesh.cpp @@ -3,12 +3,12 @@ #include #include #include +#include #include #include #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; @@ -21,6 +21,7 @@ using CompositeTransform = itk::CompositeTransform; using MeshTransformer = itk::TransformMeshFilter; using TransformWriter = itk::TransformFileWriterTemplate; +using TransformReader = itk::TransformFileReaderTemplate; int main(int argc, char** argv) { @@ -34,6 +35,7 @@ int main(int argc, char** argv) "Input mesh file") ("output-mesh,o", po::value()->required(), "Output mesh file") + ("input-tfm", po::value(), "Input transformation file") ("output-tfm,t", po::value(), "Output transformation file"); po::options_description transformOpts("Transformations"); @@ -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(); - 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(); - displacement[1] = parsed["translate-y"].as(); - displacement[2] = parsed["translate-z"].as(); - affine->Translate(displacement); + // Load transform + if (parsed.count("input-tfm") > 0) { + fs::path tfmPath = parsed["input-tfm"].as(); + auto readTfm = TransformReader::New(); + readTfm->SetFileName(tfmPath.string()); + readTfm->Update(); + auto it = readTfm->GetTransformList()->begin(); + auto* tfm = static_cast((*it).GetPointer()); + compositeTrans->AddTransform(tfm); + } - // Add scale - auto precompose = parsed.count("scale-precompose") > 0; - affine->Scale(parsed["scale"].as(), precompose); + // Build affine transform + else { + auto affine = AffineTransform::New(); + + // Add translation + Displacement displacement; + displacement[0] = parsed["translate-x"].as(); + displacement[1] = parsed["translate-y"].as(); + displacement[2] = parsed["translate-z"].as(); + affine->Translate(displacement); + + // Add scale + auto precompose = parsed.count("scale-precompose") > 0; + affine->Scale(parsed["scale"].as(), 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(); - 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) {