Welcome to mirror list, hosted at ThFree Co, Russian Federation.

admmpd_api.cpp « softbody « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 261c935b41363156331149de20b20781329b12bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation
 * All rights reserved.
 */

/** \file
 * \ingroup admmpd
 */

#include "admmpd_api.h"
#include "admmpd_types.h"
#include "admmpd_solver.h"
#include "admmpd_tetmesh.h"
#include "admmpd_embeddedmesh.h"
#include "admmpd_collision.h"

#include "tetgen_api.h"
#include "DNA_mesh_types.h" // Mesh
#include "DNA_meshdata_types.h" // MVert
#include "BKE_mesh_remesh_voxel.h" // TetGen
#include "BKE_mesh.h" // BKE_mesh_free
#include "BKE_softbody.h" // BodyPoint
#include "MEM_guardedalloc.h" // 

#include <iostream>

struct ADMMPDInternalData {
  admmpd::Options *options;
  admmpd::SolverData *data;
  admmpd::TetMeshData *tetmesh; // init_mode=0
  admmpd::EmbeddedMeshData *embmesh; // init_mode=1
  admmpd::Collision *collision;
  int in_totverts; // number of input verts
};

void admmpd_dealloc(ADMMPDInterfaceData *iface)
{
  if (iface==NULL)
    return;

  iface->totverts = 0; // output vertices

  if (iface->idata)
  {
    if (iface->idata->options)
      delete iface->idata->options;
    if (iface->idata->data)
      delete iface->idata->data;
    if (iface->idata->tetmesh)
      delete iface->idata->tetmesh;
    if (iface->idata->embmesh)
      delete iface->idata->embmesh;
    if (iface->idata->collision)
      delete iface->idata->collision;
    delete iface->idata;
  }

  iface->idata = NULL;
}

static int admmpd_init_with_tetgen(
  ADMMPDInterfaceData *iface, float *in_verts, unsigned int *in_faces,
  Eigen::MatrixXd *V, Eigen::MatrixXi *T, Eigen::VectorXd *m)
{
  TetGenRemeshData tg;
  init_tetgenremeshdata(&tg);
  tg.in_verts = in_verts;
  tg.in_totverts = iface->mesh_totverts;
  tg.in_faces = in_faces;
  tg.in_totfaces = iface->mesh_totfaces;
  bool success = tetgen_resmesh(&tg);
  if (!success || tg.out_tottets==0)
    return 0;
  
  // Create initializer for ADMMPD
  iface->totverts = tg.out_totverts;
  int nv = tg.out_totverts;
  int nt = tg.out_tottets;
  V->resize(nv,3);
  T->resize(nt,4);
  V->setZero();
  iface->idata->tetmesh->faces.resize(iface->mesh_totfaces,3);
  iface->idata->tetmesh->x_rest.resize(nv,3);
  iface->idata->tetmesh->tets.resize(nt,3);
  for (int i=0; i<iface->mesh_totfaces; ++i)
  {
    for (int j=0; j<3; ++j)
    {
      iface->idata->tetmesh->faces(i,j) = in_faces[i*3+j];
    } 
  }

  for (int i=0; i<nv; ++i)
  {
    for (int j=0; j<3; ++j)
    {
      V->operator()(i,j) = tg.out_verts[i*3+j];
      iface->idata->tetmesh->x_rest(i,j) = tg.out_verts[i*3+j];
    }
  }
  T->setZero();
  for (int i=0; i<nt; ++i)
  {
    T->operator()(i,0) = tg.out_tets[i*4+0];
    T->operator()(i,1) = tg.out_tets[i*4+1];
    T->operator()(i,2) = tg.out_tets[i*4+2];
    T->operator()(i,3) = tg.out_tets[i*4+3];
    iface->idata->tetmesh->tets.row(i) = T->row(i);
  }

  admmpd::TetMesh().compute_masses(
       iface->idata->tetmesh, V, m);

  // Clean up tetgen data
  MEM_freeN(tg.out_tets);
  MEM_freeN(tg.out_facets);
  MEM_freeN(tg.out_verts);
  return 1;
}

static int admmpd_init_with_lattice(
  ADMMPDInterfaceData *iface, float *in_verts, unsigned int *in_faces,
  Eigen::MatrixXd *V, Eigen::MatrixXi *T, Eigen::VectorXd *m)
{

  int nv = iface->mesh_totverts;
  Eigen::MatrixXd in_V(nv,3);
  for (int i=0; i<nv; ++i)
  {
    for (int j=0; j<3; ++j)
    {
      in_V(i,j) = in_verts[i*3+j];
    }
  }

  int nf = iface->mesh_totfaces;
  Eigen::MatrixXi in_F(nf,3);
  for (int i=0; i<nf; ++i)
  {
    for (int j=0; j<3; ++j)
    {
      in_F(i,j) = in_faces[i*3+j];
    }
  }

  iface->totverts = 0;
  bool trim_lattice = true;
  bool success = admmpd::EmbeddedMesh().generate(in_V,in_F,iface->idata->embmesh,trim_lattice);
  if (success)
  {
    admmpd::EmbeddedMesh().compute_masses(iface->idata->embmesh, m);
    *T = iface->idata->embmesh->tets;
    *V = iface->idata->embmesh->rest_x;
    iface->totverts = V->rows();
    return 1;
  }

  return 0;
}

int admmpd_init(ADMMPDInterfaceData *iface, float *in_verts, unsigned int *in_faces)
{
  if (iface==NULL)
    return 0;
  if (in_verts==NULL || in_faces==NULL)
    return 0;
  if (iface->mesh_totverts<=0 || iface->mesh_totfaces<=0)
    return 0;

  // Delete any existing data
  admmpd_dealloc(iface);

  // Generate solver data
  iface->idata = new ADMMPDInternalData();
  iface->idata->options = new admmpd::Options();
  admmpd::Options *options = iface->idata->options;
  iface->idata->data = new admmpd::SolverData();
  admmpd::SolverData *data = iface->idata->data;
  iface->idata->tetmesh = new admmpd::TetMeshData();
  iface->idata->embmesh = new admmpd::EmbeddedMeshData();
  iface->idata->collision = NULL;

  // Generate tets and vertices
  Eigen::MatrixXd V; // defo verts
  Eigen::MatrixXi T; // defo tets
  Eigen::VectorXd m; // masses
  int gen_success = 0;
  switch (iface->init_mode)
  {
    default:
    case 0: {
      gen_success = admmpd_init_with_tetgen(iface,in_verts,in_faces,&V,&T,&m);
      //iface->idata->collision = new admmpd::TetMeshCollision();
      } break;
    case 1: {
      gen_success = admmpd_init_with_lattice(iface,in_verts,in_faces,&V,&T,&m);
      iface->idata->collision = new admmpd::EmbeddedMeshCollision(iface->idata->embmesh);
    } break;
  }
  if (!gen_success || iface->totverts==0)
  {
    printf("**ADMMPD Failed to generate tets\n");
    return 0;
  } 

  // Initialize
  bool init_success = false;
  try
  {
    init_success = admmpd::Solver().init(V, T, m, options, data);
  }
  catch(const std::exception &e)
  {
    printf("**ADMMPD Error on init: %s\n", e.what());
  }

  if (!init_success)
  {
    printf("**ADMMPD Failed to initialize\n");
    return 0;
  }

  return 1;
}

void admmpd_copy_from_bodypoint(ADMMPDInterfaceData *iface, const BodyPoint *pts)
{
  if (iface == NULL || pts == NULL)
    return;

  for (int i=0; i<iface->totverts; ++i)
  {
    const BodyPoint *pt = &pts[i];
    for(int j=0; j<3; ++j)
    {
      iface->idata->data->x(i,j)=pt->pos[j];
      iface->idata->data->v(i,j)=pt->vec[j];
    }
  }
}

void admmpd_update_obstacles(
    ADMMPDInterfaceData *iface,
    float *in_verts_0,
    float *in_verts_1,
    int nv,
    unsigned int *in_faces,
    int nf)
{
    if (iface==NULL || in_verts_0==NULL || in_verts_1==NULL || in_faces==NULL)
      return;
    if (iface->idata==NULL)
      return;
    if (iface->idata->collision==NULL)
      return;

    iface->idata->collision->set_obstacles(
      in_verts_0, in_verts_1, nv, in_faces, nf);
}

void admmpd_copy_to_bodypoint_and_object(ADMMPDInterfaceData *iface, BodyPoint *pts, float (*vertexCos)[3])
{

  if (iface == NULL)
    return;

  for (int i=0; i<iface->totverts; ++i)
  {
    if (pts != NULL)
    {
      BodyPoint *pt = &pts[i];
      for(int j=0; j<3; ++j)
      {
        pt->pos[j] = iface->idata->data->x(i,j);
        pt->vec[j] = iface->idata->data->v(i,j);
      }
    }

    // If we're using TetGen, then we know the first
    // n vertices of the tet mesh are the input surface mesh.
    if (vertexCos != NULL && iface->init_mode==0 && i<iface->mesh_totverts)
    {
      vertexCos[i][0] = iface->idata->data->x(i,0);
      vertexCos[i][1] = iface->idata->data->x(i,1);
      vertexCos[i][2] = iface->idata->data->x(i,2);
    }
  } // end loop all verts

  // If using lattice, get the embedded vertex position
  // from the deformed lattice.
  if (vertexCos != NULL && iface->init_mode==1)
  {
      for (int i=0; i<iface->mesh_totverts; ++i)
      {
        
        Eigen::Vector3d xi = admmpd::EmbeddedMesh().get_mapped_vertex(
          iface->idata->embmesh, &iface->idata->data->x, i);
        vertexCos[i][0] = xi[0];
        vertexCos[i][1] = xi[1];
        vertexCos[i][2] = xi[2];
      }
  }

} // end map ADMMPD to bodypoint and object

void admmpd_solve(ADMMPDInterfaceData *iface)
{
  
  if (iface == NULL)
    return;

  if (iface->idata == NULL || iface->idata->options == NULL || iface->idata->data == NULL)
    return;

  try
  {
    admmpd::Solver().solve(iface->idata->options,iface->idata->data,iface->idata->collision);
  }
  catch(const std::exception &e)
  {
    printf("**ADMMPD Error on solve: %s\n", e.what());
  }
}