├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── cmake └── FindLIBIGL.cmake └── src ├── XPBD.h ├── XPBDEnergyType.h ├── main.cpp ├── meshgrid.cpp ├── meshgrid.h └── xpbd.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | 8 | # Precompiled Headers 9 | *.gch 10 | *.pch 11 | 12 | 13 | # Compiled Dynamic libraries 14 | *.so 15 | *.dylib 16 | *.dll 17 | 18 | 19 | # Fortran module files 20 | *.mod 21 | 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | 30 | # Executables 31 | *.exe 32 | *.out 33 | *.app 34 | 35 | *.un~ 36 | *.swo 37 | *.swp 38 | 39 | # Output 40 | *.png 41 | *.csv 42 | 43 | build/* 44 | experiment/* 45 | screenshot/* 46 | obj/* 47 | csv/* 48 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jenny19880324/XPBD/08b70f577fee80dcccf2ebdd95562d9ad00aaecf/.gitmodules -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | project(XPBD) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) 5 | 6 | # libigl 7 | option(LIBIGL_USE_STATIC_LIBRARY "Use libigl as static library" ON) 8 | option(LIBIGL_WITH_OPENGL "Use OpenGL" ON) 9 | option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON) 10 | option(LIBIGL_WITH_VIEWER "Use OpenGL viewer" ON) 11 | option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON) 12 | option(LIBIGL_WITH_PNG "Use PNG" ON) 13 | 14 | find_package(LIBIGL REQUIRED QUIET) 15 | 16 | # Add your project files 17 | file(GLOB SRCFILES 18 | src/*.cpp 19 | src/*.h) 20 | 21 | add_executable(${PROJECT_NAME} ${SRCFILES}) 22 | target_link_libraries(${PROJECT_NAME} 23 | igl::core 24 | igl::opengl 25 | igl::opengl_glfw 26 | igl::opengl_glfw_imgui 27 | igl::png) 28 | target_include_directories(${PROJECT_NAME} PUBLIC ${LIBIGL_EXTERNAL}/stb) 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. "Contributor" 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. "Contributor Version" 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. "Covered Software" 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. "Incompatible With Secondary Licenses" 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of 33 | version 1.1 or earlier of the License, but not also under the terms of 34 | a Secondary License. 35 | 36 | 1.6. "Executable Form" 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. "Larger Work" 41 | 42 | means a work that combines Covered Software with other material, in a 43 | separate file or files, that is not Covered Software. 44 | 45 | 1.8. "License" 46 | 47 | means this document. 48 | 49 | 1.9. "Licensable" 50 | 51 | means having the right to grant, to the maximum extent possible, whether 52 | at the time of the initial grant or subsequently, any and all of the 53 | rights conveyed by this License. 54 | 55 | 1.10. "Modifications" 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, 60 | deletion from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. "Patent Claims" of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, 67 | process, and apparatus claims, in any patent Licensable by such 68 | Contributor that would be infringed, but for the grant of the License, 69 | by the making, using, selling, offering for sale, having made, import, 70 | or transfer of either its Contributions or its Contributor Version. 71 | 72 | 1.12. "Secondary License" 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. "Source Code Form" 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. "You" (or "Your") 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, "You" includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, "control" means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or 104 | as part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its 108 | Contributions or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution 113 | become effective for each Contribution on the date the Contributor first 114 | distributes such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under 119 | this License. No additional rights or licenses will be implied from the 120 | distribution or licensing of Covered Software under this License. 121 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 122 | Contributor: 123 | 124 | a. for any code that a Contributor has removed from Covered Software; or 125 | 126 | b. for infringements caused by: (i) Your and any other third party's 127 | modifications of Covered Software, or (ii) the combination of its 128 | Contributions with other software (except as part of its Contributor 129 | Version); or 130 | 131 | c. under Patent Claims infringed by Covered Software in the absence of 132 | its Contributions. 133 | 134 | This License does not grant any rights in the trademarks, service marks, 135 | or logos of any Contributor (except as may be necessary to comply with 136 | the notice requirements in Section 3.4). 137 | 138 | 2.4. Subsequent Licenses 139 | 140 | No Contributor makes additional grants as a result of Your choice to 141 | distribute the Covered Software under a subsequent version of this 142 | License (see Section 10.2) or under the terms of a Secondary License (if 143 | permitted under the terms of Section 3.3). 144 | 145 | 2.5. Representation 146 | 147 | Each Contributor represents that the Contributor believes its 148 | Contributions are its original creation(s) or it has sufficient rights to 149 | grant the rights to its Contributions conveyed by this License. 150 | 151 | 2.6. Fair Use 152 | 153 | This License is not intended to limit any rights You have under 154 | applicable copyright doctrines of fair use, fair dealing, or other 155 | equivalents. 156 | 157 | 2.7. Conditions 158 | 159 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 160 | Section 2.1. 161 | 162 | 163 | 3. Responsibilities 164 | 165 | 3.1. Distribution of Source Form 166 | 167 | All distribution of Covered Software in Source Code Form, including any 168 | Modifications that You create or to which You contribute, must be under 169 | the terms of this License. You must inform recipients that the Source 170 | Code Form of the Covered Software is governed by the terms of this 171 | License, and how they can obtain a copy of this License. You may not 172 | attempt to alter or restrict the recipients' rights in the Source Code 173 | Form. 174 | 175 | 3.2. Distribution of Executable Form 176 | 177 | If You distribute Covered Software in Executable Form then: 178 | 179 | a. such Covered Software must also be made available in Source Code Form, 180 | as described in Section 3.1, and You must inform recipients of the 181 | Executable Form how they can obtain a copy of such Source Code Form by 182 | reasonable means in a timely manner, at a charge no more than the cost 183 | of distribution to the recipient; and 184 | 185 | b. You may distribute such Executable Form under the terms of this 186 | License, or sublicense it under different terms, provided that the 187 | license for the Executable Form does not attempt to limit or alter the 188 | recipients' rights in the Source Code Form under this License. 189 | 190 | 3.3. Distribution of a Larger Work 191 | 192 | You may create and distribute a Larger Work under terms of Your choice, 193 | provided that You also comply with the requirements of this License for 194 | the Covered Software. If the Larger Work is a combination of Covered 195 | Software with a work governed by one or more Secondary Licenses, and the 196 | Covered Software is not Incompatible With Secondary Licenses, this 197 | License permits You to additionally distribute such Covered Software 198 | under the terms of such Secondary License(s), so that the recipient of 199 | the Larger Work may, at their option, further distribute the Covered 200 | Software under the terms of either this License or such Secondary 201 | License(s). 202 | 203 | 3.4. Notices 204 | 205 | You may not remove or alter the substance of any license notices 206 | (including copyright notices, patent notices, disclaimers of warranty, or 207 | limitations of liability) contained within the Source Code Form of the 208 | Covered Software, except that You may alter any license notices to the 209 | extent required to remedy known factual inaccuracies. 210 | 211 | 3.5. Application of Additional Terms 212 | 213 | You may choose to offer, and to charge a fee for, warranty, support, 214 | indemnity or liability obligations to one or more recipients of Covered 215 | Software. However, You may do so only on Your own behalf, and not on 216 | behalf of any Contributor. You must make it absolutely clear that any 217 | such warranty, support, indemnity, or liability obligation is offered by 218 | You alone, and You hereby agree to indemnify every Contributor for any 219 | liability incurred by such Contributor as a result of warranty, support, 220 | indemnity or liability terms You offer. You may include additional 221 | disclaimers of warranty and limitations of liability specific to any 222 | jurisdiction. 223 | 224 | 4. Inability to Comply Due to Statute or Regulation 225 | 226 | If it is impossible for You to comply with any of the terms of this License 227 | with respect to some or all of the Covered Software due to statute, 228 | judicial order, or regulation then You must: (a) comply with the terms of 229 | this License to the maximum extent possible; and (b) describe the 230 | limitations and the code they affect. Such description must be placed in a 231 | text file included with all distributions of the Covered Software under 232 | this License. Except to the extent prohibited by statute or regulation, 233 | such description must be sufficiently detailed for a recipient of ordinary 234 | skill to be able to understand it. 235 | 236 | 5. Termination 237 | 238 | 5.1. The rights granted under this License will terminate automatically if You 239 | fail to comply with any of its terms. However, if You become compliant, 240 | then the rights granted under this License from a particular Contributor 241 | are reinstated (a) provisionally, unless and until such Contributor 242 | explicitly and finally terminates Your grants, and (b) on an ongoing 243 | basis, if such Contributor fails to notify You of the non-compliance by 244 | some reasonable means prior to 60 days after You have come back into 245 | compliance. Moreover, Your grants from a particular Contributor are 246 | reinstated on an ongoing basis if such Contributor notifies You of the 247 | non-compliance by some reasonable means, this is the first time You have 248 | received notice of non-compliance with this License from such 249 | Contributor, and You become compliant prior to 30 days after Your receipt 250 | of the notice. 251 | 252 | 5.2. If You initiate litigation against any entity by asserting a patent 253 | infringement claim (excluding declaratory judgment actions, 254 | counter-claims, and cross-claims) alleging that a Contributor Version 255 | directly or indirectly infringes any patent, then the rights granted to 256 | You by any and all Contributors for the Covered Software under Section 257 | 2.1 of this License shall terminate. 258 | 259 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 260 | license agreements (excluding distributors and resellers) which have been 261 | validly granted by You or Your distributors under this License prior to 262 | termination shall survive termination. 263 | 264 | 6. Disclaimer of Warranty 265 | 266 | Covered Software is provided under this License on an "as is" basis, 267 | without warranty of any kind, either expressed, implied, or statutory, 268 | including, without limitation, warranties that the Covered Software is free 269 | of defects, merchantable, fit for a particular purpose or non-infringing. 270 | The entire risk as to the quality and performance of the Covered Software 271 | is with You. Should any Covered Software prove defective in any respect, 272 | You (not any Contributor) assume the cost of any necessary servicing, 273 | repair, or correction. This disclaimer of warranty constitutes an essential 274 | part of this License. No use of any Covered Software is authorized under 275 | this License except under this disclaimer. 276 | 277 | 7. Limitation of Liability 278 | 279 | Under no circumstances and under no legal theory, whether tort (including 280 | negligence), contract, or otherwise, shall any Contributor, or anyone who 281 | distributes Covered Software as permitted above, be liable to You for any 282 | direct, indirect, special, incidental, or consequential damages of any 283 | character including, without limitation, damages for lost profits, loss of 284 | goodwill, work stoppage, computer failure or malfunction, or any and all 285 | other commercial damages or losses, even if such party shall have been 286 | informed of the possibility of such damages. This limitation of liability 287 | shall not apply to liability for death or personal injury resulting from 288 | such party's negligence to the extent applicable law prohibits such 289 | limitation. Some jurisdictions do not allow the exclusion or limitation of 290 | incidental or consequential damages, so this exclusion and limitation may 291 | not apply to You. 292 | 293 | 8. Litigation 294 | 295 | Any litigation relating to this License may be brought only in the courts 296 | of a jurisdiction where the defendant maintains its principal place of 297 | business and such litigation shall be governed by laws of that 298 | jurisdiction, without reference to its conflict-of-law provisions. Nothing 299 | in this Section shall prevent a party's ability to bring cross-claims or 300 | counter-claims. 301 | 302 | 9. Miscellaneous 303 | 304 | This License represents the complete agreement concerning the subject 305 | matter hereof. If any provision of this License is held to be 306 | unenforceable, such provision shall be reformed only to the extent 307 | necessary to make it enforceable. Any law or regulation which provides that 308 | the language of a contract shall be construed against the drafter shall not 309 | be used to construe this License against a Contributor. 310 | 311 | 312 | 10. Versions of the License 313 | 314 | 10.1. New Versions 315 | 316 | Mozilla Foundation is the license steward. Except as provided in Section 317 | 10.3, no one other than the license steward has the right to modify or 318 | publish new versions of this License. Each version will be given a 319 | distinguishing version number. 320 | 321 | 10.2. Effect of New Versions 322 | 323 | You may distribute the Covered Software under the terms of the version 324 | of the License under which You originally received the Covered Software, 325 | or under the terms of any subsequent version published by the license 326 | steward. 327 | 328 | 10.3. Modified Versions 329 | 330 | If you create software not governed by this License, and you want to 331 | create a new license for such software, you may create and use a 332 | modified version of this License if you rename the license and remove 333 | any references to the name of the license steward (except to note that 334 | such modified license differs from this License). 335 | 336 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 337 | Licenses If You choose to distribute Source Code Form that is 338 | Incompatible With Secondary Licenses under the terms of this version of 339 | the License, the notice described in Exhibit B of this License must be 340 | attached. 341 | 342 | Exhibit A - Source Code Form License Notice 343 | 344 | This Source Code Form is subject to the 345 | terms of the Mozilla Public License, v. 346 | 2.0. If a copy of the MPL was not 347 | distributed with this file, You can 348 | obtain one at 349 | http://mozilla.org/MPL/2.0/. 350 | 351 | If it is not possible or desirable to put the notice in a particular file, 352 | then You may include the notice in a location (such as a LICENSE file in a 353 | relevant directory) where a recipient would be likely to look for such a 354 | notice. 355 | 356 | You may add additional accurate notices of copyright ownership. 357 | 358 | Exhibit B - "Incompatible With Secondary Licenses" Notice 359 | 360 | This Source Code Form is "Incompatible 361 | With Secondary Licenses", as defined by 362 | the Mozilla Public License, v. 2.0. 363 | 364 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XPBD 2 | C++ implementation of XPBD: Position-Based Simulation of Compliant Constrained Dynamics 3 | http://mmacklin.com/xpbd.pdf 4 | 5 | ## See the tutorial first 6 | 7 | Then build, run and understand the [libigl 8 | tutorial](http://libigl.github.io/libigl/tutorial/). 9 | 10 | ## Compile 11 | 12 | Compile this project using the standard cmake routine: 13 | 14 | mkdir build 15 | cd build 16 | cmake .. 17 | make 18 | 19 | This should find and build the dependencies and create a `XPBD` binary. 20 | 21 | ## Run 22 | 23 | From within the `build` directory just issue: 24 | 25 | ./XPBD 26 | 27 | 28 | ## Dependencies 29 | 30 | The only dependencies are stl, eigen, [libigl](http://libigl.github.io/libigl/) and 31 | the dependencies of the `igl::opengl::glfw::Viewer`. 32 | 33 | We recommend you to install libigl using git via: 34 | 35 | git clone https://github.com/libigl/libigl.git 36 | cd libigl/ 37 | git submodule update --init --recursive 38 | cd .. 39 | 40 | If you have installed libigl at `/path/to/libigl/` then a good place to clone 41 | this library is `/path/to/XPBD/`. 42 | -------------------------------------------------------------------------------- /cmake/FindLIBIGL.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find the LIBIGL library 2 | # Once done this will define 3 | # 4 | # LIBIGL_FOUND - system has LIBIGL 5 | # LIBIGL_INCLUDE_DIR - **the** LIBIGL include directory 6 | if(LIBIGL_FOUND) 7 | return() 8 | endif() 9 | 10 | find_path(LIBIGL_INCLUDE_DIR igl/readOBJ.h 11 | HINTS 12 | ENV LIBIGL 13 | ENV LIBIGLROOT 14 | ENV LIBIGL_ROOT 15 | ENV LIBIGL_DIR 16 | PATHS 17 | ${CMAKE_SOURCE_DIR}/../.. 18 | ${CMAKE_SOURCE_DIR}/.. 19 | ${CMAKE_SOURCE_DIR} 20 | ${CMAKE_SOURCE_DIR}/libigl 21 | ${CMAKE_SOURCE_DIR}/../libigl 22 | ${CMAKE_SOURCE_DIR}/../../libigl 23 | /usr 24 | /usr/local 25 | /usr/local/igl/libigl 26 | PATH_SUFFIXES include 27 | ) 28 | 29 | include(FindPackageHandleStandardArgs) 30 | find_package_handle_standard_args(LIBIGL 31 | "\nlibigl not found --- You can download it using:\n\tgit clone --recursive https://github.com/libigl/libigl.git ${CMAKE_SOURCE_DIR}/../libigl" 32 | LIBIGL_INCLUDE_DIR) 33 | mark_as_advanced(LIBIGL_INCLUDE_DIR) 34 | 35 | list(APPEND CMAKE_MODULE_PATH "${LIBIGL_INCLUDE_DIR}/../cmake") 36 | include(libigl) 37 | -------------------------------------------------------------------------------- /src/XPBD.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "XPBDEnergyType.h" 4 | #include 5 | #include 6 | #include 7 | 8 | namespace xpbd 9 | { 10 | struct XPBDData 11 | { 12 | // n #V 13 | // energy type of energy to use 14 | // with_dynamics whether using dynamics 15 | // f_ext #V by dim list of external forces 16 | // vel #V by dim list of velocities 17 | // h dynamics time step 18 | // ym Young's modulus smaller is softer, larger is more rigid/stiff 19 | // max_iter maximum inner iterations 20 | // M mass matrix 21 | // b list of boundary indices into V 22 | // dim dimension being used for solving 23 | 24 | int n; 25 | int dim; 26 | int max_iter; 27 | XPBDEnergyType energy; 28 | double h; 29 | double ym; 30 | 31 | Eigen::MatrixXd f_ext, vel, V; 32 | Eigen::VectorXd lambda_iter; 33 | Eigen::SparseMatrix M, M_inv; 34 | std::unordered_set b; 35 | 36 | XPBDData() : 37 | n(0), 38 | max_iter(20), 39 | energy(XPBD_ENERGY_TYPE_DEFAULT), 40 | f_ext(), 41 | h(0.01), 42 | ym(1), 43 | b(), 44 | dim(2) 45 | { 46 | }; 47 | }; 48 | 49 | // Compute necessary information to start using xpbd 50 | // 51 | // Inputs: 52 | // V #V by dim list of mesh positions 53 | // F #F by simplex-size list of triangle|tet indices into V 54 | // dim dimension being used at solve time. 55 | // For deformation usually dim = V.cols(), 56 | // for surface parameterization V.cols() = 3 and dim = 2 57 | // b #b list of "boundary" fixed vertex indices into V 58 | // Outputs: 59 | // data struct containing necessary precomputation 60 | template< 61 | typename DerivedV, 62 | typename DerivedF, 63 | typename DerivedE> 64 | IGL_INLINE bool xpbd_precomputation( 65 | const Eigen::PlainObjectBase & V, 66 | const Eigen::PlainObjectBase & F, 67 | const Eigen::PlainObjectBase & E, 68 | const int dim, 69 | const std::unordered_set & b, 70 | XPBDData & data); 71 | 72 | 73 | // update vertex positions with a solver in Gauss-Seidel fashion 74 | // Algorithm 1 XPBD simulation loop described in 75 | // XPBD: Position-Based Simulation of Compliant Constrained Dynamics 76 | // http://mmacklin.com/xpbd.pdf 77 | // 78 | // Inputs: 79 | // V #V by dim initial guess 80 | // F #F by simplex-size list of triangle|tet indices into V 81 | // dim dimension being used at solve time. 82 | // For deformation usually dim = V.cols(), 83 | // for surface parameterization V.cols() = 3 and dim 2 84 | // b #b list of "boundary" fixed vertex indices into V 85 | // bc #b by dim list of boundary conditions 86 | template < 87 | typename DerivedV, 88 | typename DerivedE> 89 | IGL_INLINE bool xpbd_solve( 90 | Eigen::PlainObjectBase & U, 91 | const Eigen::PlainObjectBase & E, 92 | XPBDData & data 93 | ); 94 | 95 | } // namespace XPBD 96 | 97 | #ifndef IGL_STATIC_LIBRARY 98 | #include "xpbd.cpp" 99 | #endif -------------------------------------------------------------------------------- /src/XPBDEnergyType.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace xpbd 4 | { 5 | enum XPBDEnergyType 6 | { 7 | XPBD_ENERGY_TYPE_SPRING = 0, 8 | XPBD_ENERGY_TYPE_DEFAULT = 1, 9 | NUM_ARAP_ENERGY_TYPES = 2 10 | }; 11 | } // namespace XPBD 12 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "meshgrid.h" 3 | #include "xpbd.h" 4 | #include 5 | 6 | Eigen::MatrixXd V, U; 7 | Eigen::MatrixXi F, E; 8 | xpbd::XPBDData xpbd_data; 9 | 10 | // This function is called every time a keyboard button is pressed 11 | bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier) 12 | { 13 | std::cout << "Key: " << key << " " << (unsigned int)key << std::endl; 14 | if ((key == 'e') || (key == 'E')) { 15 | static bool show_edges = false; 16 | 17 | show_edges = !show_edges; 18 | 19 | if (show_edges) { 20 | viewer.data().set_edges(V, E, Eigen::RowVector3d(1., 0., 0.)); 21 | } 22 | else { 23 | viewer.data().set_edges(Eigen::MatrixXd(), Eigen::MatrixXi(), Eigen::RowVector3d(1., 0., 0.)); 24 | } 25 | } 26 | 27 | if (key == ' ') { 28 | viewer.core.is_animating = !viewer.core.is_animating; 29 | } 30 | 31 | return false; 32 | } 33 | 34 | int main(int argc, char *argv[]) 35 | { 36 | const int rows = 8; 37 | const int cols = 12; 38 | Eigen::VectorXi x = Eigen::VectorXi::LinSpaced(cols, 0, cols - 1); 39 | Eigen::VectorXi y = Eigen::VectorXi::LinSpaced(rows, 0, rows - 1); 40 | 41 | 42 | igl::meshgrid(x, y, V, F, E); 43 | 44 | U = V; 45 | 46 | std::unordered_set b = { (rows - 1) * cols, rows * cols - 1 }; 47 | if (!xpbd::xpbd_precomputation(V, F, E, 3, b, xpbd_data)) { 48 | std::cerr << "xpbd_precomputation failed." << std::endl; 49 | return EXIT_FAILURE; 50 | } 51 | 52 | const int n = V.rows(); 53 | xpbd_data.f_ext = xpbd_data.M * Eigen::RowVector3d(0, -9.8, 0).replicate(n, 1); 54 | for (int i = 0; i < n; i++) { 55 | xpbd_data.vel.row(i) = V(i, 1) * Eigen::RowVector3d(0., 0., 2.); 56 | } 57 | 58 | // Plot the mesh 59 | igl::opengl::glfw::Viewer viewer; 60 | viewer.callback_key_down = &key_down; 61 | viewer.data().set_mesh(V, F); 62 | viewer.data().set_face_based(true); 63 | 64 | viewer.callback_pre_draw = [&](igl::opengl::glfw::Viewer & viewer) ->bool 65 | { 66 | if (viewer.core.is_animating) 67 | { 68 | xpbd::xpbd_solve(U, E, xpbd_data); 69 | viewer.data().set_vertices(U); 70 | viewer.data().compute_normals(); 71 | } 72 | return false; 73 | }; 74 | 75 | viewer.launch(); 76 | } 77 | -------------------------------------------------------------------------------- /src/meshgrid.cpp: -------------------------------------------------------------------------------- 1 | #include "meshgrid.h" 2 | #include 3 | 4 | template < 5 | typename Derivedx, 6 | typename DerivedV, 7 | typename DerivedF, 8 | typename DerivedE> 9 | IGL_INLINE void igl::meshgrid( 10 | const Eigen::PlainObjectBase & x, 11 | const Eigen::PlainObjectBase & y, 12 | Eigen::PlainObjectBase & V, 13 | Eigen::PlainObjectBase & F, 14 | Eigen::PlainObjectBase & E 15 | ) 16 | { 17 | const int m = y.rows(); 18 | const int n = x.rows(); 19 | 20 | V.resize(m * n, 3); 21 | F.resize((m - 1)*(n-1) * 2, 3); 22 | //E.resize((m - 1)*n + (n - 1)*m, 2); 23 | E.resize((m - 1)*n + (n - 1)*m + 2 * (m - 1)*(n - 1), 2); 24 | 25 | // generate the vertices 26 | for (int x_i = 0; x_i < n; x_i++) { 27 | for (int y_i = 0; y_i < m; y_i++) { 28 | V(n * y_i + x_i, 0) = x(x_i); 29 | V(n * y_i + x_i, 1) = y(y_i); 30 | V(n * y_i + x_i, 2) = 0.; 31 | } 32 | } 33 | 34 | // generate the triangles for visualization 35 | for (int x_i = 0; x_i < n - 1; x_i++) { 36 | for (int y_i = 0; y_i < m - 1; y_i++) { 37 | F(2 * ((n - 1) * y_i + x_i) + 0, 0) = n * y_i + x_i; 38 | F(2 * ((n - 1) * y_i + x_i) + 0, 1) = n * y_i + x_i + 1; 39 | F(2 * ((n - 1) * y_i + x_i) + 0, 2) = n * (y_i + 1) + x_i + 1; 40 | F(2 * ((n - 1) * y_i + x_i) + 1, 0) = n * y_i + x_i; 41 | F(2 * ((n - 1) * y_i + x_i) + 1, 1) = n * (y_i + 1) + x_i + 1; 42 | F(2 * ((n - 1) * y_i + x_i) + 1, 2) = n * (y_i + 1) + x_i; 43 | } 44 | } 45 | 46 | // generate the edges for simulation 47 | // generate all the horizontal edges 48 | int edge_idx = 0; 49 | for (int x_i = 0; x_i < n - 1; x_i++) { 50 | for (int y_i = 0; y_i < m; y_i++) { 51 | E(edge_idx, 0) = n * y_i + x_i; 52 | E(edge_idx, 1) = n * y_i + x_i + 1; 53 | edge_idx++; 54 | } 55 | } 56 | 57 | // generate all the vertical edges 58 | for (int x_i = 0; x_i < n; x_i++) { 59 | for (int y_i = 0; y_i < m - 1; y_i++) { 60 | E(edge_idx, 0) = n * y_i + x_i; 61 | E(edge_idx, 1) = n * (y_i + 1) + x_i; 62 | edge_idx++; 63 | } 64 | } 65 | 66 | // generate all the crossing edges 67 | for (int x_i = 0; x_i < n - 1; x_i++) { 68 | for (int y_i = 0; y_i < m - 1; y_i++) { 69 | E(edge_idx, 0) = n * y_i + x_i; 70 | E(edge_idx, 1) = n * (y_i + 1) + x_i + 1; 71 | edge_idx++; 72 | E(edge_idx, 0) = n * y_i + x_i + 1; 73 | E(edge_idx, 1) = n * (y_i + 1) + x_i; 74 | edge_idx++; 75 | } 76 | } 77 | } 78 | 79 | #ifdef IGL_STATIC_LIBRARY 80 | template void igl::meshgrid, class Eigen::Matrix, class Eigen::Matrix, class Eigen::Matrix >(class Eigen::PlainObjectBase > const &, class Eigen::PlainObjectBase > const &, class Eigen::PlainObjectBase > &, class Eigen::PlainObjectBase > &, class Eigen::PlainObjectBase > &); 81 | #endif -------------------------------------------------------------------------------- /src/meshgrid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace igl 7 | { 8 | // like matlab's meshgrid function 9 | // meshgrid(x, y, V, F) V is the #x by dim grid coordinates 10 | // usage: 11 | // x = Eigen::VectorXi::LinSpaced(n, 0, n-1); 12 | // y = Eigen::vectorXi::LinSpaced(n, 0, n-1); 13 | // meshgrid(x, y, V, F, E); 14 | // 15 | // ---------- 16 | // |\/|\/|\/| 17 | // |/\|/\|/\| 18 | // ---------- 19 | // |\/|\/|\/| 20 | // |/\|/\|/\| 21 | // ---------- 22 | // E contains 2 crossing springs within one grid 23 | // 24 | // Inputs: 25 | // x x-coordinates of points, specified as a 1D vector 26 | // y y-coordinates of points, specified as a 1D vector 27 | // Outputs: 28 | // V #V by dim mesh positions 29 | // F #F by simplex size indices into V, for visualization 30 | // E #E by simplex size indices into V, for simulation 31 | template < 32 | typename Derivedx, 33 | typename DerivedV, 34 | typename DerivedF, 35 | typename DerivedE> 36 | IGL_INLINE void meshgrid( 37 | const Eigen::PlainObjectBase & x, 38 | const Eigen::PlainObjectBase & y, 39 | Eigen::PlainObjectBase & V, 40 | Eigen::PlainObjectBase & F, 41 | Eigen::PlainObjectBase & E); 42 | } // namespace igl 43 | 44 | #ifndef IGL_STATIC_LIBRARY 45 | # include "meshgrid.cpp" 46 | #endif 47 | -------------------------------------------------------------------------------- /src/xpbd.cpp: -------------------------------------------------------------------------------- 1 | #include "xpbd.h" 2 | #include 3 | #include 4 | 5 | 6 | template < 7 | typename DerivedV, 8 | typename DerivedF, 9 | typename DerivedE> 10 | IGL_INLINE bool xpbd::xpbd_precomputation( 11 | const Eigen::PlainObjectBase & V, 12 | const Eigen::PlainObjectBase & F, 13 | const Eigen::PlainObjectBase & E, 14 | const int dim, 15 | const std::unordered_set & b, 16 | XPBDData & data) 17 | { 18 | using namespace std; 19 | using namespace Eigen; 20 | 21 | data.V = V; 22 | // number of vertices 23 | const int n = V.rows(); 24 | data.n = n; 25 | // remember b 26 | data.b = b; 27 | assert((dim == 3 || dim == 2) && "dim should be 2 or 3"); 28 | data.dim = dim; 29 | data.f_ext = MatrixXd::Zero(n, data.dim); 30 | data.vel = MatrixXd::Zero(n, data.dim); 31 | data.lambda_iter = VectorXd::Zero(E.rows()); 32 | 33 | SparseMatrix M; 34 | igl::massmatrix(V, F, igl::MASSMATRIX_TYPE_DEFAULT, data.M); 35 | data.M = 1e-5 * data.M; 36 | data.M_inv = data.M.cwiseInverse(); 37 | 38 | return true; 39 | } 40 | 41 | 42 | template < 43 | typename DerivedV, 44 | typename DerivedF> 45 | IGL_INLINE bool xpbd::xpbd_solve( 46 | Eigen::PlainObjectBase & U, 47 | const Eigen::PlainObjectBase & E, 48 | XPBDData & data) 49 | { 50 | using namespace Eigen; 51 | 52 | // predict position 53 | const double h = data.h; 54 | const int n = U.rows(); 55 | Eigen::MatrixXd U_prev = U; 56 | Eigen::MatrixXd U0 = U + h * data.vel + h * h * data.M_inv * data.f_ext; 57 | 58 | // The Position of the vertex is simply set to the static target position 59 | // or updated at every time step to coincide with the position of the kinematic object. 60 | for (auto it = data.b.begin(); it != data.b.end(); it++) { 61 | U0.row(*it) = U.row(*it); 62 | } 63 | 64 | 65 | static std::vector d(E.rows()); // rest_length 66 | const Eigen::MatrixXd V = data.V; 67 | for (int j = 0; j < E.rows(); j++) { 68 | d[j] = (V.row(E(j, 0)) - V.row(E(j, 1))).norm(); 69 | } 70 | 71 | 72 | auto C = [&](int j) { 73 | return (U0.row(E(j, 0)) - U0.row(E(j, 1))).norm() - d[j]; 74 | }; 75 | 76 | auto dC = [&](int j) -> Eigen::RowVector3d { 77 | Eigen::RowVector3d l = U0.row(E(j, 0)) - U0.row(E(j, 1)); 78 | return l.normalized(); 79 | }; 80 | 81 | auto dC_squaredNorm = [&](int j) { 82 | Eigen::RowVector3d l = U0.row(E(j, 0)) - U0.row(E(j, 1)); 83 | double norm = l.norm() - d[j]; 84 | //return norm * norm; 85 | return 1.; 86 | }; 87 | 88 | int iter = 0; 89 | double tilde_alpha = (1./ data.ym) / (h * h); 90 | while (iter < data.max_iter) { 91 | 92 | for (int j = 0; j < E.rows(); j++) { 93 | 94 | // compute dlambda using Eq(18) 95 | double Cj = C(j); 96 | double dCj_squaredNorm = dC_squaredNorm(j); 97 | Eigen::RowVector3d dCj = dC(j); 98 | double m_inv_0 = data.M_inv.coeff(E(j, 0), E(j, 0)); 99 | double m_inv_1 = data.M_inv.coeff(E(j, 1), E(j, 1)); 100 | double m_inv_sum = m_inv_0 + m_inv_1; 101 | double dlambda_j = (-Cj - tilde_alpha * data.lambda_iter(j)) / (m_inv_sum + tilde_alpha); 102 | 103 | // compute dx using Eq(17) 104 | Eigen::RowVector3d dxj = dCj * dlambda_j; 105 | 106 | // update lambda_{i+1} <= lambda_{i} + \nabla lambda 107 | data.lambda_iter(j) += dlambda_j; 108 | 109 | // update x_{i+1} <= x_{i} + \nabla x 110 | if (data.b.find(E(j, 0)) == data.b.end()) { 111 | U0.row(E(j, 0)) += m_inv_0 * dxj; 112 | } 113 | if (data.b.find(E(j, 1)) == data.b.end()) { 114 | U0.row(E(j, 1)) -= m_inv_1 * dxj; 115 | } 116 | } 117 | 118 | iter++; 119 | } 120 | 121 | U = U0; 122 | data.vel = (U - U_prev) / h; 123 | 124 | return true; 125 | } 126 | 127 | 128 | #ifdef IGL_STATIC_LIBRARY 129 | template bool xpbd::xpbd_precomputation, Eigen::Matrix, Eigen::Matrix >(Eigen::PlainObjectBase > const &, Eigen::PlainObjectBase > const &, Eigen::PlainObjectBase > const &, int, std::unordered_set, struct std::equal_to, std::allocator > const &, struct xpbd::XPBDData &); 130 | template bool xpbd::xpbd_solve, Eigen::Matrix >(Eigen::PlainObjectBase > &, Eigen::PlainObjectBase > const &, struct xpbd::XPBDData &); 131 | #endif --------------------------------------------------------------------------------