2012-10-24 10:39:11 +00:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* 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) 2005 by the Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s): Alexander Pinzon
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file blender/modifiers/intern/MOD_laplaciansmooth.c
|
|
|
|
* \ingroup modifiers
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "BKE_cdderivedmesh.h"
|
|
|
|
#include "BKE_deform.h"
|
|
|
|
#include "BKE_modifier.h"
|
|
|
|
|
|
|
|
#include "MOD_util.h"
|
|
|
|
|
2015-11-24 20:42:10 +01:00
|
|
|
#include "eigen_capi.h"
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2013-06-08 05:24:55 +00:00
|
|
|
#if 0
|
2012-11-03 18:23:30 +00:00
|
|
|
#define MOD_LAPLACIANSMOOTH_MAX_EDGE_PERCENTAGE 1.8f
|
|
|
|
#define MOD_LAPLACIANSMOOTH_MIN_EDGE_PERCENTAGE 0.02f
|
2013-06-08 05:24:55 +00:00
|
|
|
#endif
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
struct BLaplacianSystem {
|
2012-10-24 11:31:57 +00:00
|
|
|
float *eweights; /* Length weights per Edge */
|
2012-10-24 10:39:11 +00:00
|
|
|
float (*fweights)[3]; /* Cotangent weights per face */
|
2012-10-24 11:31:57 +00:00
|
|
|
float *ring_areas; /* Total area per ring*/
|
|
|
|
float *vlengths; /* Total sum of lengths(edges) per vertice*/
|
|
|
|
float *vweights; /* Total sum of weights per vertice*/
|
|
|
|
int numEdges; /* Number of edges*/
|
2015-08-04 21:30:10 +10:00
|
|
|
int numLoops; /* Number of edges*/
|
|
|
|
int numPolys; /* Number of faces*/
|
2012-10-24 11:31:57 +00:00
|
|
|
int numVerts; /* Number of verts*/
|
|
|
|
short *numNeFa; /* Number of neighboors faces around vertice*/
|
|
|
|
short *numNeEd; /* Number of neighboors Edges around vertice*/
|
|
|
|
short *zerola; /* Is zero area or length*/
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
/* Pointers to data*/
|
|
|
|
float (*vertexCos)[3];
|
2015-08-04 21:30:10 +10:00
|
|
|
const MPoly *mpoly;
|
|
|
|
const MLoop *mloop;
|
|
|
|
const MEdge *medges;
|
2015-11-24 20:42:10 +01:00
|
|
|
LinearSolver *context;
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
/*Data*/
|
|
|
|
float min_area;
|
|
|
|
float vert_centroid[3];
|
|
|
|
};
|
|
|
|
typedef struct BLaplacianSystem LaplacianSystem;
|
|
|
|
|
2013-06-02 03:59:19 +00:00
|
|
|
static CustomDataMask required_data_mask(Object *ob, ModifierData *md);
|
|
|
|
static bool is_disabled(ModifierData *md, int useRenderParams);
|
2015-08-04 21:30:10 +10:00
|
|
|
static float compute_volume(const float center[3], float (*vertexCos)[3], const MPoly *mpoly, int numPolys, const MLoop *mloop);
|
|
|
|
static LaplacianSystem *init_laplacian_system(int a_numEdges, int a_numPolys, int a_numLoops, int a_numVerts);
|
2012-10-24 10:39:11 +00:00
|
|
|
static void copy_data(ModifierData *md, ModifierData *target);
|
2012-10-24 11:31:57 +00:00
|
|
|
static void delete_laplacian_system(LaplacianSystem *sys);
|
|
|
|
static void fill_laplacian_matrix(LaplacianSystem *sys);
|
2012-10-24 10:39:11 +00:00
|
|
|
static void init_data(ModifierData *md);
|
2012-10-24 11:31:57 +00:00
|
|
|
static void init_laplacian_matrix(LaplacianSystem *sys);
|
2012-10-24 10:39:11 +00:00
|
|
|
static void memset_laplacian_system(LaplacianSystem *sys, int val);
|
|
|
|
static void volume_preservation(LaplacianSystem *sys, float vini, float vend, short flag);
|
2013-01-16 19:38:50 +00:00
|
|
|
static void validate_solution(LaplacianSystem *sys, short flag, float lambda, float lambda_border);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2012-10-24 11:31:57 +00:00
|
|
|
static void delete_laplacian_system(LaplacianSystem *sys)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2015-08-04 22:31:32 +10:00
|
|
|
MEM_SAFE_FREE(sys->eweights);
|
|
|
|
MEM_SAFE_FREE(sys->fweights);
|
|
|
|
MEM_SAFE_FREE(sys->numNeEd);
|
|
|
|
MEM_SAFE_FREE(sys->numNeFa);
|
|
|
|
MEM_SAFE_FREE(sys->ring_areas);
|
|
|
|
MEM_SAFE_FREE(sys->vlengths);
|
|
|
|
MEM_SAFE_FREE(sys->vweights);
|
|
|
|
MEM_SAFE_FREE(sys->zerola);
|
|
|
|
|
2012-10-24 10:39:11 +00:00
|
|
|
if (sys->context) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_delete(sys->context);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
sys->vertexCos = NULL;
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->mpoly = NULL;
|
|
|
|
sys->mloop = NULL;
|
2012-10-24 10:39:11 +00:00
|
|
|
sys->medges = NULL;
|
|
|
|
MEM_freeN(sys);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void memset_laplacian_system(LaplacianSystem *sys, int val)
|
|
|
|
{
|
2012-10-24 11:31:57 +00:00
|
|
|
memset(sys->eweights, val, sizeof(float) * sys->numEdges);
|
2015-08-04 21:30:10 +10:00
|
|
|
memset(sys->fweights, val, sizeof(float[3]) * sys->numLoops);
|
2012-10-24 11:31:57 +00:00
|
|
|
memset(sys->numNeEd, val, sizeof(short) * sys->numVerts);
|
|
|
|
memset(sys->numNeFa, val, sizeof(short) * sys->numVerts);
|
|
|
|
memset(sys->ring_areas, val, sizeof(float) * sys->numVerts);
|
|
|
|
memset(sys->vlengths, val, sizeof(float) * sys->numVerts);
|
|
|
|
memset(sys->vweights, val, sizeof(float) * sys->numVerts);
|
|
|
|
memset(sys->zerola, val, sizeof(short) * sys->numVerts);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
static LaplacianSystem *init_laplacian_system(int a_numEdges, int a_numPolys, int a_numLoops, int a_numVerts)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2012-10-24 11:31:57 +00:00
|
|
|
LaplacianSystem *sys;
|
2012-10-24 10:39:11 +00:00
|
|
|
sys = MEM_callocN(sizeof(LaplacianSystem), "ModLaplSmoothSystem");
|
|
|
|
sys->numEdges = a_numEdges;
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->numPolys = a_numPolys;
|
|
|
|
sys->numLoops = a_numLoops;
|
2012-10-24 10:39:11 +00:00
|
|
|
sys->numVerts = a_numVerts;
|
|
|
|
|
2015-08-04 22:31:32 +10:00
|
|
|
sys->eweights = MEM_callocN(sizeof(float) * sys->numEdges, __func__);
|
|
|
|
sys->fweights = MEM_callocN(sizeof(float[3]) * sys->numLoops, __func__);
|
|
|
|
sys->numNeEd = MEM_callocN(sizeof(short) * sys->numVerts, __func__);
|
|
|
|
sys->numNeFa = MEM_callocN(sizeof(short) * sys->numVerts, __func__);
|
|
|
|
sys->ring_areas = MEM_callocN(sizeof(float) * sys->numVerts, __func__);
|
|
|
|
sys->vlengths = MEM_callocN(sizeof(float) * sys->numVerts, __func__);
|
|
|
|
sys->vweights = MEM_callocN(sizeof(float) * sys->numVerts, __func__);
|
|
|
|
sys->zerola = MEM_callocN(sizeof(short) * sys->numVerts, __func__);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
return sys;
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
static float compute_volume(
|
|
|
|
const float center[3], float (*vertexCos)[3],
|
|
|
|
const MPoly *mpoly, int numPolys, const MLoop *mloop)
|
2013-01-16 19:38:50 +00:00
|
|
|
{
|
2012-10-24 10:39:11 +00:00
|
|
|
int i;
|
2015-08-04 21:30:10 +10:00
|
|
|
float vol = 0.0f;
|
|
|
|
|
|
|
|
for (i = 0; i < numPolys; i++) {
|
|
|
|
const MPoly *mp = &mpoly[i];
|
|
|
|
const MLoop *l_first = &mloop[mp->loopstart];
|
|
|
|
const MLoop *l_prev = l_first + 1;
|
|
|
|
const MLoop *l_curr = l_first + 2;
|
|
|
|
const MLoop *l_term = l_first + mp->totloop;
|
|
|
|
|
|
|
|
|
|
|
|
for (;
|
|
|
|
l_curr != l_term;
|
|
|
|
l_prev = l_curr, l_curr++)
|
|
|
|
{
|
|
|
|
vol += volume_tetrahedron_signed_v3(
|
|
|
|
center,
|
|
|
|
vertexCos[l_first->v],
|
|
|
|
vertexCos[l_prev->v],
|
|
|
|
vertexCos[l_curr->v]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-04 21:30:10 +10:00
|
|
|
|
2012-10-27 11:10:12 +00:00
|
|
|
return fabsf(vol);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void volume_preservation(LaplacianSystem *sys, float vini, float vend, short flag)
|
|
|
|
{
|
|
|
|
float beta;
|
|
|
|
int i;
|
|
|
|
|
2012-10-27 10:42:28 +00:00
|
|
|
if (vend != 0.0f) {
|
|
|
|
beta = pow(vini / vend, 1.0f / 3.0f);
|
2012-10-24 10:39:11 +00:00
|
|
|
for (i = 0; i < sys->numVerts; i++) {
|
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_X) {
|
2012-10-24 11:31:57 +00:00
|
|
|
sys->vertexCos[i][0] = (sys->vertexCos[i][0] - sys->vert_centroid[0]) * beta + sys->vert_centroid[0];
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_Y) {
|
2012-10-24 11:31:57 +00:00
|
|
|
sys->vertexCos[i][1] = (sys->vertexCos[i][1] - sys->vert_centroid[1]) * beta + sys->vert_centroid[1];
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_Z) {
|
2012-10-24 11:31:57 +00:00
|
|
|
sys->vertexCos[i][2] = (sys->vertexCos[i][2] - sys->vert_centroid[2]) * beta + sys->vert_centroid[2];
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
2012-10-27 10:42:28 +00:00
|
|
|
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 11:31:57 +00:00
|
|
|
static void init_laplacian_matrix(LaplacianSystem *sys)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2015-08-04 21:30:10 +10:00
|
|
|
float *v1, *v2;
|
|
|
|
float w1, w2, w3;
|
2012-10-24 10:39:11 +00:00
|
|
|
float areaf;
|
2015-08-04 21:30:10 +10:00
|
|
|
int i;
|
|
|
|
unsigned int idv1, idv2;
|
|
|
|
|
2012-10-27 10:42:28 +00:00
|
|
|
for (i = 0; i < sys->numEdges; i++) {
|
2012-10-24 10:39:11 +00:00
|
|
|
idv1 = sys->medges[i].v1;
|
|
|
|
idv2 = sys->medges[i].v2;
|
|
|
|
|
|
|
|
v1 = sys->vertexCos[idv1];
|
|
|
|
v2 = sys->vertexCos[idv2];
|
|
|
|
|
|
|
|
sys->numNeEd[idv1] = sys->numNeEd[idv1] + 1;
|
|
|
|
sys->numNeEd[idv2] = sys->numNeEd[idv2] + 1;
|
|
|
|
w1 = len_v3v3(v1, v2);
|
|
|
|
if (w1 < sys->min_area) {
|
|
|
|
sys->zerola[idv1] = 1;
|
|
|
|
sys->zerola[idv2] = 1;
|
2012-10-24 11:31:57 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-10-24 10:39:11 +00:00
|
|
|
w1 = 1.0f / w1;
|
|
|
|
}
|
2012-10-27 10:42:28 +00:00
|
|
|
|
2012-10-24 10:39:11 +00:00
|
|
|
sys->eweights[i] = w1;
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
for (i = 0; i < sys->numPolys; i++) {
|
|
|
|
const MPoly *mp = &sys->mpoly[i];
|
|
|
|
const MLoop *l_next = &sys->mloop[mp->loopstart];
|
|
|
|
const MLoop *l_term = l_next + mp->totloop;
|
|
|
|
const MLoop *l_prev = l_term - 2;
|
|
|
|
const MLoop *l_curr = l_term - 1;
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
for (;
|
|
|
|
l_next != l_term;
|
|
|
|
l_prev = l_curr, l_curr = l_next, l_next++)
|
|
|
|
{
|
|
|
|
const float *v_prev = sys->vertexCos[l_prev->v];
|
|
|
|
const float *v_curr = sys->vertexCos[l_curr->v];
|
|
|
|
const float *v_next = sys->vertexCos[l_next->v];
|
|
|
|
const unsigned int l_curr_index = l_curr - sys->mloop;
|
2012-10-27 10:42:28 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->numNeFa[l_curr->v] += 1;
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
areaf = area_tri_v3(v_prev, v_curr, v_next);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
if (areaf < sys->min_area) {
|
|
|
|
sys->zerola[l_curr->v] = 1;
|
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->ring_areas[l_prev->v] += areaf;
|
|
|
|
sys->ring_areas[l_curr->v] += areaf;
|
|
|
|
sys->ring_areas[l_next->v] += areaf;
|
2012-10-27 10:42:28 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
w1 = cotangent_tri_weight_v3(v_curr, v_next, v_prev) / 2.0f;
|
|
|
|
w2 = cotangent_tri_weight_v3(v_next, v_prev, v_curr) / 2.0f;
|
|
|
|
w3 = cotangent_tri_weight_v3(v_prev, v_curr, v_next) / 2.0f;
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->fweights[l_curr_index][0] += w1;
|
|
|
|
sys->fweights[l_curr_index][1] += w2;
|
|
|
|
sys->fweights[l_curr_index][2] += w3;
|
2012-10-27 10:42:28 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->vweights[l_curr->v] += w2 + w3;
|
|
|
|
sys->vweights[l_next->v] += w1 + w3;
|
|
|
|
sys->vweights[l_prev->v] += w1 + w2;
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-27 10:42:28 +00:00
|
|
|
for (i = 0; i < sys->numEdges; i++) {
|
2012-10-24 10:39:11 +00:00
|
|
|
idv1 = sys->medges[i].v1;
|
|
|
|
idv2 = sys->medges[i].v2;
|
|
|
|
/* if is boundary, apply scale-dependent umbrella operator only with neighboors in boundary */
|
2012-10-27 10:42:28 +00:00
|
|
|
if (sys->numNeEd[idv1] != sys->numNeFa[idv1] && sys->numNeEd[idv2] != sys->numNeFa[idv2]) {
|
2012-10-24 10:39:11 +00:00
|
|
|
sys->vlengths[idv1] += sys->eweights[i];
|
|
|
|
sys->vlengths[idv2] += sys->eweights[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-24 11:31:57 +00:00
|
|
|
static void fill_laplacian_matrix(LaplacianSystem *sys)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2015-08-04 21:30:10 +10:00
|
|
|
int i;
|
|
|
|
unsigned int idv1, idv2;
|
|
|
|
|
|
|
|
for (i = 0; i < sys->numPolys; i++) {
|
|
|
|
const MPoly *mp = &sys->mpoly[i];
|
|
|
|
const MLoop *l_next = &sys->mloop[mp->loopstart];
|
|
|
|
const MLoop *l_term = l_next + mp->totloop;
|
|
|
|
const MLoop *l_prev = l_term - 2;
|
|
|
|
const MLoop *l_curr = l_term - 1;
|
|
|
|
|
|
|
|
for (;
|
|
|
|
l_next != l_term;
|
|
|
|
l_prev = l_curr, l_curr = l_next, l_next++)
|
|
|
|
{
|
|
|
|
const unsigned int l_curr_index = l_curr - sys->mloop;
|
|
|
|
|
2012-10-24 10:39:11 +00:00
|
|
|
/* Is ring if number of faces == number of edges around vertice*/
|
2015-08-04 21:30:10 +10:00
|
|
|
if (sys->numNeEd[l_curr->v] == sys->numNeFa[l_curr->v] && sys->zerola[l_curr->v] == 0) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_curr->v, l_next->v, sys->fweights[l_curr_index][2] * sys->vweights[l_curr->v]);
|
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_curr->v, l_prev->v, sys->fweights[l_curr_index][1] * sys->vweights[l_curr->v]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
2015-08-04 21:30:10 +10:00
|
|
|
if (sys->numNeEd[l_next->v] == sys->numNeFa[l_next->v] && sys->zerola[l_next->v] == 0) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_next->v, l_curr->v, sys->fweights[l_curr_index][2] * sys->vweights[l_next->v]);
|
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_next->v, l_prev->v, sys->fweights[l_curr_index][0] * sys->vweights[l_next->v]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
2015-08-04 21:30:10 +10:00
|
|
|
if (sys->numNeEd[l_prev->v] == sys->numNeFa[l_prev->v] && sys->zerola[l_prev->v] == 0) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_prev->v, l_curr->v, sys->fweights[l_curr_index][1] * sys->vweights[l_prev->v]);
|
|
|
|
EIG_linear_solver_matrix_add(sys->context, l_prev->v, l_next->v, sys->fweights[l_curr_index][0] * sys->vweights[l_prev->v]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 10:42:28 +00:00
|
|
|
for (i = 0; i < sys->numEdges; i++) {
|
2012-10-24 10:39:11 +00:00
|
|
|
idv1 = sys->medges[i].v1;
|
|
|
|
idv2 = sys->medges[i].v2;
|
|
|
|
/* Is boundary */
|
2012-10-27 10:42:28 +00:00
|
|
|
if (sys->numNeEd[idv1] != sys->numNeFa[idv1] &&
|
|
|
|
sys->numNeEd[idv2] != sys->numNeFa[idv2] &&
|
|
|
|
sys->zerola[idv1] == 0 &&
|
|
|
|
sys->zerola[idv2] == 0)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, idv1, idv2, sys->eweights[i] * sys->vlengths[idv1]);
|
|
|
|
EIG_linear_solver_matrix_add(sys->context, idv2, idv1, sys->eweights[i] * sys->vlengths[idv2]);
|
2012-10-27 10:42:28 +00:00
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-08 04:00:06 +00:00
|
|
|
static void validate_solution(LaplacianSystem *sys, short flag, float lambda, float lambda_border)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
2013-01-18 06:26:06 +00:00
|
|
|
int i;
|
2013-01-16 19:38:50 +00:00
|
|
|
float lam;
|
2015-01-12 11:42:20 +11:00
|
|
|
float vini, vend;
|
2013-01-18 06:26:06 +00:00
|
|
|
|
2012-11-20 05:50:19 +00:00
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_PRESERVE_VOLUME) {
|
2015-08-04 21:30:10 +10:00
|
|
|
vini = compute_volume(sys->vert_centroid, sys->vertexCos, sys->mpoly, sys->numPolys, sys->mloop);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < sys->numVerts; i++) {
|
|
|
|
if (sys->zerola[i] == 0) {
|
2013-01-16 23:07:24 +00:00
|
|
|
lam = sys->numNeEd[i] == sys->numNeFa[i] ? (lambda >= 0.0f ? 1.0f : -1.0f) : (lambda_border >= 0.0f ? 1.0f : -1.0f);
|
2012-10-24 10:39:11 +00:00
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_X) {
|
2015-11-24 20:42:10 +01:00
|
|
|
sys->vertexCos[i][0] += lam * ((float)EIG_linear_solver_variable_get(sys->context, 0, i) - sys->vertexCos[i][0]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_Y) {
|
2015-11-24 20:42:10 +01:00
|
|
|
sys->vertexCos[i][1] += lam * ((float)EIG_linear_solver_variable_get(sys->context, 1, i) - sys->vertexCos[i][1]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_Z) {
|
2015-11-24 20:42:10 +01:00
|
|
|
sys->vertexCos[i][2] += lam * ((float)EIG_linear_solver_variable_get(sys->context, 2, i) - sys->vertexCos[i][2]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-20 05:50:19 +00:00
|
|
|
if (flag & MOD_LAPLACIANSMOOTH_PRESERVE_VOLUME) {
|
2015-08-04 21:30:10 +10:00
|
|
|
vend = compute_volume(sys->vert_centroid, sys->vertexCos, sys->mpoly, sys->numPolys, sys->mloop);
|
2012-10-24 10:39:11 +00:00
|
|
|
volume_preservation(sys, vini, vend, flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void laplaciansmoothModifier_do(
|
2012-11-03 15:35:03 +00:00
|
|
|
LaplacianSmoothModifierData *smd, Object *ob, DerivedMesh *dm,
|
|
|
|
float (*vertexCos)[3], int numVerts)
|
2012-10-24 10:39:11 +00:00
|
|
|
{
|
|
|
|
LaplacianSystem *sys;
|
|
|
|
MDeformVert *dvert = NULL;
|
|
|
|
MDeformVert *dv = NULL;
|
|
|
|
float w, wpaint;
|
|
|
|
int i, iter;
|
|
|
|
int defgrp_index;
|
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys = init_laplacian_system(dm->getNumEdges(dm), dm->getNumPolys(dm), dm->getNumLoops(dm), numVerts);
|
2012-10-24 11:31:57 +00:00
|
|
|
if (!sys) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-08-04 21:30:10 +10:00
|
|
|
sys->mpoly = dm->getPolyArray(dm);
|
|
|
|
sys->mloop = dm->getLoopArray(dm);
|
2012-10-24 10:39:11 +00:00
|
|
|
sys->medges = dm->getEdgeArray(dm);
|
|
|
|
sys->vertexCos = vertexCos;
|
|
|
|
sys->min_area = 0.00001f;
|
|
|
|
modifier_get_vgroup(ob, dm, smd->defgrp_name, &dvert, &defgrp_index);
|
|
|
|
|
|
|
|
sys->vert_centroid[0] = 0.0f;
|
|
|
|
sys->vert_centroid[1] = 0.0f;
|
|
|
|
sys->vert_centroid[2] = 0.0f;
|
2013-01-21 19:33:58 +00:00
|
|
|
memset_laplacian_system(sys, 0);
|
2014-01-30 13:06:48 +01:00
|
|
|
|
2015-11-24 20:42:10 +01:00
|
|
|
sys->context = EIG_linear_least_squares_solver_new(numVerts, numVerts, 3);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2013-01-21 19:33:58 +00:00
|
|
|
init_laplacian_matrix(sys);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2013-01-22 11:23:05 +00:00
|
|
|
for (iter = 0; iter < smd->repeat; iter++) {
|
2012-10-24 10:39:11 +00:00
|
|
|
for (i = 0; i < numVerts; i++) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_variable_set(sys->context, 0, i, vertexCos[i][0]);
|
|
|
|
EIG_linear_solver_variable_set(sys->context, 1, i, vertexCos[i][1]);
|
|
|
|
EIG_linear_solver_variable_set(sys->context, 2, i, vertexCos[i][2]);
|
2012-10-24 10:39:11 +00:00
|
|
|
if (iter == 0) {
|
2012-10-24 11:31:57 +00:00
|
|
|
add_v3_v3(sys->vert_centroid, vertexCos[i]);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-24 11:31:57 +00:00
|
|
|
if (iter == 0 && numVerts > 0) {
|
|
|
|
mul_v3_fl(sys->vert_centroid, 1.0f / (float)numVerts);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dv = dvert;
|
|
|
|
for (i = 0; i < numVerts; i++) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_right_hand_side_add(sys->context, 0, i, vertexCos[i][0]);
|
|
|
|
EIG_linear_solver_right_hand_side_add(sys->context, 1, i, vertexCos[i][1]);
|
|
|
|
EIG_linear_solver_right_hand_side_add(sys->context, 2, i, vertexCos[i][2]);
|
2013-01-21 19:33:58 +00:00
|
|
|
if (iter == 0) {
|
|
|
|
if (dv) {
|
|
|
|
wpaint = defvert_find_weight(dv, defgrp_index);
|
|
|
|
dv++;
|
2012-10-24 11:31:57 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-01-21 19:33:58 +00:00
|
|
|
wpaint = 1.0f;
|
|
|
|
}
|
2013-01-16 19:38:50 +00:00
|
|
|
|
2013-01-21 19:33:58 +00:00
|
|
|
if (sys->zerola[i] == 0) {
|
|
|
|
if (smd->flag & MOD_LAPLACIANSMOOTH_NORMALIZED) {
|
|
|
|
w = sys->vweights[i];
|
|
|
|
sys->vweights[i] = (w == 0.0f) ? 0.0f : -fabsf(smd->lambda) * wpaint / w;
|
|
|
|
w = sys->vlengths[i];
|
|
|
|
sys->vlengths[i] = (w == 0.0f) ? 0.0f : -fabsf(smd->lambda_border) * wpaint * 2.0f / w;
|
|
|
|
if (sys->numNeEd[i] == sys->numNeFa[i]) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, i, i, 1.0f + fabsf(smd->lambda) * wpaint);
|
2013-01-21 19:33:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, i, i, 1.0f + fabsf(smd->lambda_border) * wpaint * 2.0f);
|
2013-01-21 19:33:58 +00:00
|
|
|
}
|
2013-01-16 19:38:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-01-21 19:33:58 +00:00
|
|
|
w = sys->vweights[i] * sys->ring_areas[i];
|
|
|
|
sys->vweights[i] = (w == 0.0f) ? 0.0f : -fabsf(smd->lambda) * wpaint / (4.0f * w);
|
|
|
|
w = sys->vlengths[i];
|
|
|
|
sys->vlengths[i] = (w == 0.0f) ? 0.0f : -fabsf(smd->lambda_border) * wpaint * 2.0f / w;
|
|
|
|
|
|
|
|
if (sys->numNeEd[i] == sys->numNeFa[i]) {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, i, i, 1.0f + fabsf(smd->lambda) * wpaint / (4.0f * sys->ring_areas[i]));
|
2013-01-21 19:33:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, i, i, 1.0f + fabsf(smd->lambda_border) * wpaint * 2.0f);
|
2013-01-21 19:33:58 +00:00
|
|
|
}
|
2013-01-16 19:38:50 +00:00
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
2013-01-21 19:33:58 +00:00
|
|
|
else {
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_matrix_add(sys->context, i, i, 1.0f);
|
2013-01-21 19:33:58 +00:00
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:23:05 +00:00
|
|
|
if (iter == 0) {
|
2013-01-21 19:33:58 +00:00
|
|
|
fill_laplacian_matrix(sys);
|
|
|
|
}
|
2012-10-24 10:39:11 +00:00
|
|
|
|
2015-11-24 20:42:10 +01:00
|
|
|
if (EIG_linear_solver_solve(sys->context)) {
|
2013-01-16 19:38:50 +00:00
|
|
|
validate_solution(sys, smd->flag, smd->lambda, smd->lambda_border);
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 20:42:10 +01:00
|
|
|
EIG_linear_solver_delete(sys->context);
|
2013-01-21 19:33:58 +00:00
|
|
|
sys->context = NULL;
|
2014-01-30 13:06:48 +01:00
|
|
|
|
2012-10-24 10:39:11 +00:00
|
|
|
delete_laplacian_system(sys);
|
2013-11-25 15:33:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_data(ModifierData *md)
|
|
|
|
{
|
|
|
|
LaplacianSmoothModifierData *smd = (LaplacianSmoothModifierData *) md;
|
|
|
|
smd->lambda = 0.01f;
|
|
|
|
smd->lambda_border = 0.01f;
|
|
|
|
smd->repeat = 1;
|
|
|
|
smd->flag = MOD_LAPLACIANSMOOTH_X | MOD_LAPLACIANSMOOTH_Y | MOD_LAPLACIANSMOOTH_Z | MOD_LAPLACIANSMOOTH_PRESERVE_VOLUME | MOD_LAPLACIANSMOOTH_NORMALIZED;
|
|
|
|
smd->defgrp_name[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_data(ModifierData *md, ModifierData *target)
|
|
|
|
{
|
2013-12-22 04:35:52 +11:00
|
|
|
#if 0
|
2013-11-25 15:33:41 +11:00
|
|
|
LaplacianSmoothModifierData *smd = (LaplacianSmoothModifierData *) md;
|
|
|
|
LaplacianSmoothModifierData *tsmd = (LaplacianSmoothModifierData *) target;
|
2013-12-22 04:35:52 +11:00
|
|
|
#endif
|
2013-11-25 15:33:41 +11:00
|
|
|
|
2013-12-22 04:35:52 +11:00
|
|
|
modifier_copyData_generic(md, target);
|
2013-11-25 15:33:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_disabled(ModifierData *md, int UNUSED(useRenderParams))
|
|
|
|
{
|
|
|
|
LaplacianSmoothModifierData *smd = (LaplacianSmoothModifierData *) md;
|
|
|
|
short flag;
|
|
|
|
|
|
|
|
flag = smd->flag & (MOD_LAPLACIANSMOOTH_X | MOD_LAPLACIANSMOOTH_Y | MOD_LAPLACIANSMOOTH_Z);
|
|
|
|
|
|
|
|
/* disable if modifier is off for X, Y and Z or if factor is 0 */
|
|
|
|
if (flag == 0) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CustomDataMask required_data_mask(Object *UNUSED(ob), ModifierData *md)
|
|
|
|
{
|
|
|
|
LaplacianSmoothModifierData *smd = (LaplacianSmoothModifierData *)md;
|
|
|
|
CustomDataMask dataMask = 0;
|
|
|
|
|
|
|
|
/* ask for vertexgroups if we need them */
|
|
|
|
if (smd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
|
|
|
|
|
|
|
|
return dataMask;
|
2012-10-24 10:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData,
|
|
|
|
float (*vertexCos)[3], int numVerts, ModifierApplyFlag UNUSED(flag))
|
|
|
|
{
|
2014-01-08 03:44:00 +11:00
|
|
|
DerivedMesh *dm;
|
|
|
|
|
|
|
|
if (numVerts == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dm = get_dm(ob, NULL, derivedData, NULL, false, false);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
laplaciansmoothModifier_do((LaplacianSmoothModifierData *)md, ob, dm,
|
2012-10-27 10:42:28 +00:00
|
|
|
vertexCos, numVerts);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
if (dm != derivedData)
|
|
|
|
dm->release(dm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void deformVertsEM(
|
|
|
|
ModifierData *md, Object *ob, struct BMEditMesh *editData,
|
|
|
|
DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
|
|
|
|
{
|
2014-01-08 03:44:00 +11:00
|
|
|
DerivedMesh *dm;
|
|
|
|
|
|
|
|
if (numVerts == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dm = get_dm(ob, editData, derivedData, NULL, false, false);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
laplaciansmoothModifier_do((LaplacianSmoothModifierData *)md, ob, dm,
|
2012-10-27 10:42:28 +00:00
|
|
|
vertexCos, numVerts);
|
2012-10-24 10:39:11 +00:00
|
|
|
|
|
|
|
if (dm != derivedData)
|
|
|
|
dm->release(dm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ModifierTypeInfo modifierType_LaplacianSmooth = {
|
|
|
|
/* name */ "Laplacian Smooth",
|
|
|
|
/* structName */ "LaplacianSmoothModifierData",
|
|
|
|
/* structSize */ sizeof(LaplacianSmoothModifierData),
|
|
|
|
/* type */ eModifierTypeType_OnlyDeform,
|
|
|
|
/* flags */ eModifierTypeFlag_AcceptsMesh |
|
|
|
|
eModifierTypeFlag_SupportsEditmode,
|
|
|
|
|
|
|
|
/* copy_data */ copy_data,
|
|
|
|
/* deformVerts */ deformVerts,
|
|
|
|
/* deformMatrices */ NULL,
|
|
|
|
/* deformVertsEM */ deformVertsEM,
|
|
|
|
/* deformMatricesEM */ NULL,
|
|
|
|
/* applyModifier */ NULL,
|
|
|
|
/* applyModifierEM */ NULL,
|
2012-10-24 11:31:57 +00:00
|
|
|
/* initData */ init_data,
|
|
|
|
/* requiredDataMask */ required_data_mask,
|
2012-10-24 10:39:11 +00:00
|
|
|
/* freeData */ NULL,
|
2012-10-24 11:31:57 +00:00
|
|
|
/* isDisabled */ is_disabled,
|
2012-10-24 10:39:11 +00:00
|
|
|
/* updateDepgraph */ NULL,
|
Depsgraph: New dependency graph integration commit
This commit integrates the work done so far on the new dependency graph system,
where goal was to replace legacy depsgraph with the new one, supporting loads of
neat features like:
- More granular dependency relation nature, which solves issues with fake cycles
in the dependencies.
- Move towards all-animatable, by better integration of drivers into the system.
- Lay down some basis for upcoming copy-on-write, overrides and so on.
The new system is living side-by-side with the previous one and disabled by
default, so nothing will become suddenly broken. The way to enable new depsgraph
is to pass `--new-depsgraph` command line argument.
It's a bit early to consider the system production-ready, there are some TODOs
and issues were discovered during the merge period, they'll be addressed ASAP.
But it's important to merge, because it's the only way to attract artists to
really start testing this system.
There are number of assorted documents related on the design of the new system:
* http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents
* http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph
There are also some user-related information online:
* http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/
* http://code.blender.org/2015/03/more-dependency-graph-tricks/
Kudos to everyone who was involved into the project:
- Joshua "Aligorith" Leung -- design specification, initial code
- Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes
- Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the
project and so
- Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the
issues and recording/writing documentation.
- Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
|
|
|
/* updateDepsgraph */ NULL,
|
2012-10-24 10:39:11 +00:00
|
|
|
/* dependsOnTime */ NULL,
|
|
|
|
/* dependsOnNormals */ NULL,
|
|
|
|
/* foreachObjectLink */ NULL,
|
|
|
|
/* foreachIDLink */ NULL,
|
|
|
|
/* foreachTexLink */ NULL,
|
|
|
|
};
|