|
| 1 | +/** |
| 2 | + This file is part of mesh-deform. |
| 3 | + |
| 4 | + Copyright(C) 2016 Christoph Heindl |
| 5 | + All rights reserved. |
| 6 | + |
| 7 | + This software may be modified and distributed under the terms |
| 8 | + of the BSD license.See the LICENSE file for details. |
| 9 | +
|
| 10 | + This examples shows the deformation of a sphere like object. In this particular |
| 11 | + case a single vertex is pinned to its initial location and a harmonic motion |
| 12 | + is applied to the vertex on the opposite side of the sphere. All other vertices |
| 13 | + are unconstrained. |
| 14 | + */ |
| 15 | + |
| 16 | +#define _USE_MATH_DEFINES |
| 17 | + |
| 18 | +#include <deform/arap.h> |
| 19 | +#include <deform/openmesh_adapter.h> |
| 20 | +#include <deform/trajectory.h> |
| 21 | + |
| 22 | +#include "osg_viewer.h" |
| 23 | +#include "example_config.h" |
| 24 | + |
| 25 | +#include <Eigen/Geometry> |
| 26 | + |
| 27 | +#include <string> |
| 28 | +#include <iostream> |
| 29 | + |
| 30 | + |
| 31 | +const std::vector<int> handles = { 8,9,10,11,15,18,19,20,21,35,38,39,40,41,51,54,55,56,57,74,75,76,77,78,79,103,104,107,108,109,110,111,112,113,114,116,123,126,127,128,129,135,136,141,142,144,145,146,147,148,149,150,151,162,163,179,182,183,184,185,195,198,199,200,201,203,205,218,219,220,221,222,223,261,262,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,315,316,324,325,342,343,351,352,357,358,360,361,362,363,364,365,366,367,378,379 }; |
| 32 | + |
| 33 | + |
| 34 | +const std::vector<int> anchors = {0,1,2,3,4,5,6,7,12,26,27,28,29,30,33,34,46,47,48,92,93,94,95,96,97,98,99,100,101,102,105,106,115,117,118,119,120,137,152,155,158,164,165,166,167,168,169,170,171,172,173,174,177,178,190,191,192,193,194,196,197,202,204,211,226,229,232,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,269,271,275,277,278,280,284,286,287,289,293,295,332,334,338,340,380,381,382,383,384,385 }; |
| 35 | + |
| 36 | + |
| 37 | +typedef OpenMesh::TriMesh_ArrayKernelT<> Mesh; |
| 38 | +typedef deform::OpenMeshAdapter<> Adapter; |
| 39 | +typedef deform::AsRigidAsPossibleDeformation<Adapter, double> ARAP; |
| 40 | +typedef deform::TrajectorySE3<float> Trajectory; |
| 41 | + |
| 42 | + |
| 43 | +void applyTransformationToHandles(const Eigen::Affine3f &delta, Mesh &mesh, ARAP &arap) { |
| 44 | + for (auto h : handles) { |
| 45 | + Mesh::VertexHandle vh = mesh.vertex_handle(h); |
| 46 | + Eigen::Vector3f v = delta * deform::convert::toEigen(mesh.point(vh)); |
| 47 | + arap.setConstraint(vh.idx(), v); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +int main(int argc, char **argv) { |
| 53 | + |
| 54 | + std::string pathToSource = std::string(DEFORM_ETC_DIR) + std::string("/bar.obj"); |
| 55 | + |
| 56 | + Mesh mesh; |
| 57 | + if (!OpenMesh::IO::read_mesh(mesh, pathToSource)) { |
| 58 | + std::cerr << "Failed to read mesh" << std::endl; |
| 59 | + return -1; |
| 60 | + } |
| 61 | + |
| 62 | + Adapter ma(mesh); |
| 63 | + ARAP arap(ma); |
| 64 | + |
| 65 | + // Set anchors. |
| 66 | + for (auto a : anchors) { |
| 67 | + Mesh::VertexHandle va = mesh.vertex_handle(a); |
| 68 | + arap.setConstraint(va.idx(), deform::convert::toEigen(mesh.point(va))); |
| 69 | + } |
| 70 | + |
| 71 | + // Setup trajectory |
| 72 | + Trajectory trajectory; |
| 73 | + trajectory.addKeyPose(Trajectory::Transform::Identity()); |
| 74 | + trajectory.addKeyPose(Trajectory::Transform::Identity() * Eigen::Translation3f(1.f,0.f,0.f)); |
| 75 | + trajectory.addKeyPose(Trajectory::Transform::Identity() * Eigen::Translation3f(2.f, 0.f, 0.f)); |
| 76 | + trajectory.addKeyPose(Trajectory::Transform::Identity() * Eigen::AngleAxisf((float)M_PI / 2.f, Eigen::Vector3f::UnitZ()) * Eigen::AngleAxisf((float)M_PI / 4.f, Eigen::Vector3f::UnitX()) * Eigen::Translation3f(2.f, 0.f, 0.f)); |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + Eigen::Affine3f prev = trajectory(0.f); |
| 82 | + |
| 83 | + // Create an OSG viewer to visualize incremental deformation. |
| 84 | + double duration = 10.f; |
| 85 | + |
| 86 | + deform::example::OSGViewer viewer(mesh, anchors, handles); |
| 87 | + viewer.onFrame([&](Mesh &mesh, double time) { |
| 88 | + |
| 89 | + |
| 90 | + double t = time / duration; |
| 91 | + if (t > 1.f) |
| 92 | + return false; |
| 93 | + |
| 94 | + Trajectory::Transform transform = trajectory((float)t); |
| 95 | + applyTransformationToHandles(transform * prev.inverse(Eigen::Isometry), mesh, arap); |
| 96 | + prev = transform; |
| 97 | + |
| 98 | + arap.deform(10); |
| 99 | + |
| 100 | + return true; |
| 101 | + }); |
| 102 | + viewer.run(); |
| 103 | + |
| 104 | + return 0; |
| 105 | + |
| 106 | +} |
0 commit comments