2011-10-10 09:38:02 +00:00
|
|
|
/*
|
2008-01-07 19:13:47 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-01-07 19:13:47 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* 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,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2009-01-04 14:14:06 +00:00
|
|
|
* Contributor(s): Blender Foundation
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
2008-01-07 19:13:47 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-27 20:40:57 +00:00
|
|
|
/** \file blender/blenkernel/intern/mesh.c
|
|
|
|
* \ingroup bke
|
|
|
|
*/
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
#include "DNA_key_types.h"
|
2012-02-19 22:17:30 +00:00
|
|
|
#include "DNA_mesh_types.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-05-09 04:06:48 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_math.h"
|
2017-02-22 09:40:46 +01:00
|
|
|
#include "BLI_linklist.h"
|
|
|
|
#include "BLI_memarena.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_edgehash.h"
|
2013-09-20 11:14:08 +00:00
|
|
|
#include "BLI_string.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2009-12-28 03:45:24 +00:00
|
|
|
#include "BKE_animsys.h"
|
2018-05-03 15:42:55 +02:00
|
|
|
#include "BKE_idcode.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_main.h"
|
2005-03-29 16:43:39 +00:00
|
|
|
#include "BKE_DerivedMesh.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_global.h"
|
|
|
|
#include "BKE_mesh.h"
|
|
|
|
#include "BKE_library.h"
|
|
|
|
#include "BKE_material.h"
|
2010-09-09 00:14:51 +00:00
|
|
|
#include "BKE_modifier.h"
|
|
|
|
#include "BKE_multires.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_object.h"
|
2013-04-13 20:31:52 +00:00
|
|
|
#include "BKE_editmesh.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
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
|
|
|
#include "DEG_depsgraph.h"
|
2009-05-26 04:17:47 +00:00
|
|
|
|
2010-10-27 02:22:55 +00:00
|
|
|
enum {
|
|
|
|
MESHCMP_DVERT_WEIGHTMISMATCH = 1,
|
|
|
|
MESHCMP_DVERT_GROUPMISMATCH,
|
|
|
|
MESHCMP_DVERT_TOTGROUPMISMATCH,
|
|
|
|
MESHCMP_LOOPCOLMISMATCH,
|
|
|
|
MESHCMP_LOOPUVMISMATCH,
|
|
|
|
MESHCMP_LOOPMISMATCH,
|
|
|
|
MESHCMP_POLYVERTMISMATCH,
|
|
|
|
MESHCMP_POLYMISMATCH,
|
|
|
|
MESHCMP_EDGEUNKNOWN,
|
|
|
|
MESHCMP_VERTCOMISMATCH,
|
2012-05-21 06:33:45 +00:00
|
|
|
MESHCMP_CDLAYERS_MISMATCH
|
2010-10-27 02:22:55 +00:00
|
|
|
};
|
|
|
|
|
2011-05-09 05:09:07 +00:00
|
|
|
static const char *cmpcode_to_str(int code)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
case MESHCMP_DVERT_WEIGHTMISMATCH:
|
|
|
|
return "Vertex Weight Mismatch";
|
|
|
|
case MESHCMP_DVERT_GROUPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Group Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_DVERT_TOTGROUPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Doesn't Belong To Same Number Of Groups";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPCOLMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Color Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPUVMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "UV Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_LOOPMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_POLYVERTMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Vert Mismatch In Poly Test";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_POLYMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Loop Vert Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_EDGEUNKNOWN:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Edge Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_VERTCOMISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Vertex Coordinate Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
case MESHCMP_CDLAYERS_MISMATCH:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "CustomData Layer Count Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
default:
|
2012-05-06 15:15:33 +00:00
|
|
|
return "Mesh Comparison Code Unknown";
|
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 20:19:11 +00:00
|
|
|
/* thresh is threshold for comparing vertices, uvs, vertex colors,
|
|
|
|
* weights, etc.*/
|
2013-05-08 12:55:36 +00:00
|
|
|
static int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2, const float thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
2013-05-08 12:55:36 +00:00
|
|
|
const float thresh_sq = thresh * thresh;
|
2010-10-27 02:22:55 +00:00
|
|
|
CustomDataLayer *l1, *l2;
|
2012-05-06 15:15:33 +00:00
|
|
|
int i, i1 = 0, i2 = 0, tot, j;
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2012-04-28 06:31:57 +00:00
|
|
|
for (i = 0; i < c1->totlayer; i++) {
|
2014-07-20 01:30:29 +10:00
|
|
|
if (ELEM(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
2017-05-25 15:11:00 +10:00
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MDEFORMVERT))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2010-10-27 02:22:55 +00:00
|
|
|
i1++;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2012-04-28 06:31:57 +00:00
|
|
|
|
|
|
|
for (i = 0; i < c2->totlayer; i++) {
|
2014-07-20 01:30:29 +10:00
|
|
|
if (ELEM(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
2017-05-25 15:11:00 +10:00
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MDEFORMVERT))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2010-10-27 02:22:55 +00:00
|
|
|
i2++;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
2012-04-28 06:31:57 +00:00
|
|
|
|
2010-10-27 02:22:55 +00:00
|
|
|
if (i1 != i2)
|
|
|
|
return MESHCMP_CDLAYERS_MISMATCH;
|
|
|
|
|
|
|
|
l1 = c1->layers; l2 = c2->layers;
|
|
|
|
tot = i1;
|
2014-07-20 01:30:29 +10:00
|
|
|
i1 = 0; i2 = 0;
|
2012-05-06 15:15:33 +00:00
|
|
|
for (i = 0; i < tot; i++) {
|
2014-07-20 01:30:29 +10:00
|
|
|
while (i1 < c1->totlayer && !ELEM(l1->type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
2017-05-25 15:11:00 +10:00
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MDEFORMVERT))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2016-03-05 09:09:05 +11:00
|
|
|
i1++;
|
|
|
|
l1++;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2014-07-20 01:30:29 +10:00
|
|
|
while (i2 < c2->totlayer && !ELEM(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
2017-05-25 15:11:00 +10:00
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MDEFORMVERT))
|
2012-04-28 06:31:57 +00:00
|
|
|
{
|
2016-03-05 09:09:05 +11:00
|
|
|
i2++;
|
|
|
|
l2++;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2010-10-27 02:22:55 +00:00
|
|
|
|
|
|
|
if (l1->type == CD_MVERT) {
|
|
|
|
MVert *v1 = l1->data;
|
|
|
|
MVert *v2 = l2->data;
|
|
|
|
int vtot = m1->totvert;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < vtot; j++, v1++, v2++) {
|
2014-02-02 01:36:40 +11:00
|
|
|
if (len_squared_v3v3(v1->co, v2->co) > thresh_sq)
|
2010-10-27 02:22:55 +00:00
|
|
|
return MESHCMP_VERTCOMISMATCH;
|
2012-08-17 14:43:20 +00:00
|
|
|
/* I don't care about normals, let's just do coodinates */
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*we're order-agnostic for edges here*/
|
|
|
|
if (l1->type == CD_MEDGE) {
|
|
|
|
MEdge *e1 = l1->data;
|
|
|
|
MEdge *e2 = l2->data;
|
|
|
|
int etot = m1->totedge;
|
2013-08-24 13:47:57 +00:00
|
|
|
EdgeHash *eh = BLI_edgehash_new_ex(__func__, etot);
|
2010-10-27 02:22:55 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < etot; j++, e1++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
BLI_edgehash_insert(eh, e1->v1, e1->v2, e1);
|
|
|
|
}
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < etot; j++, e2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
if (!BLI_edgehash_lookup(eh, e2->v1, e2->v2))
|
|
|
|
return MESHCMP_EDGEUNKNOWN;
|
|
|
|
}
|
|
|
|
BLI_edgehash_free(eh, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l1->type == CD_MPOLY) {
|
|
|
|
MPoly *p1 = l1->data;
|
|
|
|
MPoly *p2 = l2->data;
|
|
|
|
int ptot = m1->totpoly;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < ptot; j++, p1++, p2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
MLoop *lp1, *lp2;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (p1->totloop != p2->totloop)
|
|
|
|
return MESHCMP_POLYMISMATCH;
|
|
|
|
|
|
|
|
lp1 = m1->mloop + p1->loopstart;
|
|
|
|
lp2 = m2->mloop + p2->loopstart;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (k = 0; k < p1->totloop; k++, lp1++, lp2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
if (lp1->v != lp2->v)
|
|
|
|
return MESHCMP_POLYVERTMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l1->type == CD_MLOOP) {
|
|
|
|
MLoop *lp1 = l1->data;
|
|
|
|
MLoop *lp2 = l2->data;
|
|
|
|
int ltot = m1->totloop;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < ltot; j++, lp1++, lp2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
if (lp1->v != lp2->v)
|
|
|
|
return MESHCMP_LOOPMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l1->type == CD_MLOOPUV) {
|
|
|
|
MLoopUV *lp1 = l1->data;
|
|
|
|
MLoopUV *lp2 = l2->data;
|
|
|
|
int ltot = m1->totloop;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < ltot; j++, lp1++, lp2++) {
|
2013-05-08 12:55:36 +00:00
|
|
|
if (len_squared_v2v2(lp1->uv, lp2->uv) > thresh_sq)
|
2010-10-27 02:22:55 +00:00
|
|
|
return MESHCMP_LOOPUVMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l1->type == CD_MLOOPCOL) {
|
|
|
|
MLoopCol *lp1 = l1->data;
|
|
|
|
MLoopCol *lp2 = l2->data;
|
|
|
|
int ltot = m1->totloop;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < ltot; j++, lp1++, lp2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
if (ABS(lp1->r - lp2->r) > thresh ||
|
|
|
|
ABS(lp1->g - lp2->g) > thresh ||
|
|
|
|
ABS(lp1->b - lp2->b) > thresh ||
|
|
|
|
ABS(lp1->a - lp2->a) > thresh)
|
|
|
|
{
|
|
|
|
return MESHCMP_LOOPCOLMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l1->type == CD_MDEFORMVERT) {
|
|
|
|
MDeformVert *dv1 = l1->data;
|
|
|
|
MDeformVert *dv2 = l2->data;
|
|
|
|
int dvtot = m1->totvert;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (j = 0; j < dvtot; j++, dv1++, dv2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
int k;
|
2012-05-06 15:15:33 +00:00
|
|
|
MDeformWeight *dw1 = dv1->dw, *dw2 = dv2->dw;
|
2010-10-27 02:22:55 +00:00
|
|
|
|
|
|
|
if (dv1->totweight != dv2->totweight)
|
|
|
|
return MESHCMP_DVERT_TOTGROUPMISMATCH;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (k = 0; k < dv1->totweight; k++, dw1++, dw2++) {
|
2010-10-27 02:22:55 +00:00
|
|
|
if (dw1->def_nr != dw2->def_nr)
|
|
|
|
return MESHCMP_DVERT_GROUPMISMATCH;
|
2013-12-01 13:11:12 +11:00
|
|
|
if (fabsf(dw1->weight - dw2->weight) > thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
return MESHCMP_DVERT_WEIGHTMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 23:36:56 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/**
|
|
|
|
* Used for unit testing; compares two meshes, checking only
|
|
|
|
* differences we care about. should be usable with leaf's
|
|
|
|
* testing framework I get RNA work done, will use hackish
|
|
|
|
* testing code for now.
|
|
|
|
*/
|
2012-05-05 21:28:12 +00:00
|
|
|
const char *BKE_mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (!me1 || !me2)
|
|
|
|
return "Requires two input meshes";
|
|
|
|
|
|
|
|
if (me1->totvert != me2->totvert)
|
|
|
|
return "Number of verts don't match";
|
|
|
|
|
|
|
|
if (me1->totedge != me2->totedge)
|
|
|
|
return "Number of edges don't match";
|
|
|
|
|
|
|
|
if (me1->totpoly != me2->totpoly)
|
|
|
|
return "Number of faces don't match";
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (me1->totloop != me2->totloop)
|
2010-10-27 02:22:55 +00:00
|
|
|
return "Number of loops don't match";
|
|
|
|
|
|
|
|
if ((c = customdata_compare(&me1->vdata, &me2->vdata, me1, me2, thresh)))
|
|
|
|
return cmpcode_to_str(c);
|
|
|
|
|
|
|
|
if ((c = customdata_compare(&me1->edata, &me2->edata, me1, me2, thresh)))
|
|
|
|
return cmpcode_to_str(c);
|
|
|
|
|
|
|
|
if ((c = customdata_compare(&me1->ldata, &me2->ldata, me1, me2, thresh)))
|
|
|
|
return cmpcode_to_str(c);
|
|
|
|
|
|
|
|
if ((c = customdata_compare(&me1->pdata, &me2->pdata, me1, me2, thresh)))
|
|
|
|
return cmpcode_to_str(c);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-02 16:05:54 +00:00
|
|
|
static void mesh_ensure_tessellation_customdata(Mesh *me)
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
2012-03-20 05:04:51 +00:00
|
|
|
if (UNLIKELY((me->totface != 0) && (me->totpoly == 0))) {
|
|
|
|
/* Pass, otherwise this function clears 'mface' before
|
|
|
|
* versioning 'mface -> mpoly' code kicks in [#30583]
|
|
|
|
*
|
|
|
|
* Callers could also check but safer to do here - campbell */
|
|
|
|
}
|
|
|
|
else {
|
2017-05-25 15:11:00 +10:00
|
|
|
const int tottex_original = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
|
2012-03-20 05:04:51 +00:00
|
|
|
const int totcol_original = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2012-03-20 05:04:51 +00:00
|
|
|
const int tottex_tessface = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
|
|
|
|
const int totcol_tessface = CustomData_number_of_layers(&me->fdata, CD_MCOL);
|
2011-12-06 09:28:25 +00:00
|
|
|
|
2012-03-20 05:04:51 +00:00
|
|
|
if (tottex_tessface != tottex_original ||
|
2012-05-06 15:15:33 +00:00
|
|
|
totcol_tessface != totcol_original)
|
2012-03-20 05:04:51 +00:00
|
|
|
{
|
|
|
|
BKE_mesh_tessface_clear(me);
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2017-05-25 15:11:00 +10:00
|
|
|
CustomData_from_bmeshpoly(&me->fdata, &me->ldata, me->totface);
|
2011-12-06 09:28:25 +00:00
|
|
|
|
2012-04-12 14:36:57 +00:00
|
|
|
/* TODO - add some --debug-mesh option */
|
|
|
|
if (G.debug & G_DEBUG) {
|
2012-04-25 06:06:40 +00:00
|
|
|
/* note: this warning may be un-called for if we are initializing the mesh for the
|
2012-04-12 14:36:57 +00:00
|
|
|
* first time from bmesh, rather then giving a warning about this we could be smarter
|
|
|
|
* and check if there was any data to begin with, for now just print the warning with
|
|
|
|
* some info to help troubleshoot whats going on - campbell */
|
|
|
|
printf("%s: warning! Tessellation uvs or vcol data got out of sync, "
|
2017-05-25 15:11:00 +10:00
|
|
|
"had to reset!\n CD_MTFACE: %d != CD_MLOOPUV: %d || CD_MCOL: %d != CD_MLOOPCOL: %d\n",
|
2012-05-06 15:15:33 +00:00
|
|
|
__func__, tottex_tessface, tottex_original, totcol_tessface, totcol_original);
|
2012-04-12 14:36:57 +00:00
|
|
|
}
|
2012-03-20 05:04:51 +00:00
|
|
|
}
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 21:55:30 +11:00
|
|
|
void BKE_mesh_ensure_skin_customdata(Mesh *me)
|
|
|
|
{
|
|
|
|
BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL;
|
|
|
|
MVertSkin *vs;
|
|
|
|
|
|
|
|
if (bm) {
|
|
|
|
if (!CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
|
|
|
|
BMVert *v;
|
|
|
|
BMIter iter;
|
|
|
|
|
|
|
|
BM_data_layer_add(bm, &bm->vdata, CD_MVERT_SKIN);
|
|
|
|
|
|
|
|
/* Mark an arbitrary vertex as root */
|
|
|
|
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
|
|
|
|
vs = CustomData_bmesh_get(&bm->vdata, v->head.data,
|
|
|
|
CD_MVERT_SKIN);
|
|
|
|
vs->flag |= MVERT_SKIN_ROOT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!CustomData_has_layer(&me->vdata, CD_MVERT_SKIN)) {
|
|
|
|
vs = CustomData_add_layer(&me->vdata,
|
|
|
|
CD_MVERT_SKIN,
|
|
|
|
CD_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
me->totvert);
|
|
|
|
|
|
|
|
/* Mark an arbitrary vertex as root */
|
|
|
|
if (vs) {
|
|
|
|
vs->flag |= MVERT_SKIN_ROOT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 18:37:30 +10:00
|
|
|
bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me)
|
|
|
|
{
|
|
|
|
BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL;
|
|
|
|
bool changed = false;
|
|
|
|
if (bm) {
|
|
|
|
if (!CustomData_has_layer(&bm->pdata, CD_FACEMAP)) {
|
|
|
|
BM_data_layer_add(bm, &bm->pdata, CD_FACEMAP);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!CustomData_has_layer(&me->pdata, CD_FACEMAP)) {
|
|
|
|
CustomData_add_layer(&me->pdata,
|
|
|
|
CD_FACEMAP,
|
|
|
|
CD_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
me->totpoly);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BKE_mesh_clear_facemap_customdata(struct Mesh *me)
|
|
|
|
{
|
|
|
|
BMesh *bm = me->edit_btmesh ? me->edit_btmesh->bm : NULL;
|
|
|
|
bool changed = false;
|
|
|
|
if (bm) {
|
|
|
|
if (CustomData_has_layer(&bm->pdata, CD_FACEMAP)) {
|
|
|
|
BM_data_layer_free(bm, &bm->pdata, CD_FACEMAP);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (CustomData_has_layer(&me->pdata, CD_FACEMAP)) {
|
|
|
|
CustomData_free_layers(&me->pdata, CD_FACEMAP, me->totpoly);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2011-12-06 09:28:25 +00:00
|
|
|
/* this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or
|
|
|
|
* mloopcol and mcol) have the same relative active/render/clone/mask indices.
|
|
|
|
*
|
|
|
|
* note that for undo mesh data we want to skip 'ensure_tess_cd' call since
|
2012-03-18 07:38:51 +00:00
|
|
|
* we don't want to store memory for tessface when its only used for older
|
2011-12-06 09:28:25 +00:00
|
|
|
* versions of the mesh. - campbell*/
|
2013-03-17 19:55:10 +00:00
|
|
|
static void mesh_update_linked_customdata(Mesh *me, const bool do_ensure_tess_cd)
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
2011-12-06 09:28:25 +00:00
|
|
|
if (do_ensure_tess_cd) {
|
2012-03-02 16:05:54 +00:00
|
|
|
mesh_ensure_tessellation_customdata(me);
|
2011-12-06 09:28:25 +00:00
|
|
|
}
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2017-05-25 15:11:00 +10:00
|
|
|
CustomData_bmesh_update_active_layers(&me->fdata, &me->ldata);
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
2009-05-23 03:24:15 +00:00
|
|
|
|
2013-03-17 19:55:10 +00:00
|
|
|
void BKE_mesh_update_customdata_pointers(Mesh *me, const bool do_ensure_tess_cd)
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
{
|
2011-12-06 09:28:25 +00:00
|
|
|
mesh_update_linked_customdata(me, do_ensure_tess_cd);
|
2009-08-30 21:30:07 +00:00
|
|
|
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
me->mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
|
|
|
|
me->dvert = CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
|
|
|
|
|
|
|
|
me->medge = CustomData_get_layer(&me->edata, CD_MEDGE);
|
|
|
|
|
|
|
|
me->mface = CustomData_get_layer(&me->fdata, CD_MFACE);
|
|
|
|
me->mcol = CustomData_get_layer(&me->fdata, CD_MCOL);
|
|
|
|
me->mtface = CustomData_get_layer(&me->fdata, CD_MTFACE);
|
2009-07-16 06:27:37 +00:00
|
|
|
|
2009-05-26 04:17:47 +00:00
|
|
|
me->mpoly = CustomData_get_layer(&me->pdata, CD_MPOLY);
|
|
|
|
me->mloop = CustomData_get_layer(&me->ldata, CD_MLOOP);
|
|
|
|
|
|
|
|
me->mloopcol = CustomData_get_layer(&me->ldata, CD_MLOOPCOL);
|
|
|
|
me->mloopuv = CustomData_get_layer(&me->ldata, CD_MLOOPUV);
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
}
|
|
|
|
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
bool BKE_mesh_has_custom_loop_normals(Mesh *me)
|
|
|
|
{
|
|
|
|
if (me->edit_btmesh) {
|
|
|
|
return CustomData_has_layer(&me->edit_btmesh->bm->ldata, CD_CUSTOMLOOPNORMAL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CustomData_has_layer(&me->ldata, CD_CUSTOMLOOPNORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
ID-Remap - Step one: core work (cleanup and rework of generic ID datablock handling).
This commit changes a lot of how IDs are handled internally, especially the unlinking/freeing
processes. So far, this was very fuzy, to summarize cleanly deleting or replacing a datablock
was pretty much impossible, except for a few special cases.
Also, unlinking was handled by each datatype, in a rather messy and prone-to-errors way (quite
a few ID usages were missed or wrongly handled that way).
One of the main goal of id-remap branch was to cleanup this, and fatorize ID links handling
by using library_query utils to allow generic handling of those, which is now the case
(now, generic ID links handling is only "knwon" from readfile.c and library_query.c).
This commit also adds backends to allow live replacement and deletion of datablocks in Blender
(so-called 'remapping' process, where we replace all usages of a given ID pointer by a new one,
or NULL one in case of unlinking).
This will allow nice new features, like ability to easily reload or relocate libraries, real immediate
deletion of datablocks in blender, replacement of one datablock by another, etc.
Some of those are for next commits.
A word of warning: this commit is highly risky, because it affects potentially a lot in Blender core.
Though it was tested rather deeply, being totally impossible to check all possible ID usage cases,
it's likely there are some remaining issues and bugs in new code... Please report them! ;)
Review task: D2027 (https://developer.blender.org/D2027).
Reviewed by campbellbarton, thanks a bunch.
2016-06-22 17:29:38 +02:00
|
|
|
/** Free (or release) any data used by this mesh (does not free the mesh itself). */
|
|
|
|
void BKE_mesh_free(Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
ID-Remap - Step one: core work (cleanup and rework of generic ID datablock handling).
This commit changes a lot of how IDs are handled internally, especially the unlinking/freeing
processes. So far, this was very fuzy, to summarize cleanly deleting or replacing a datablock
was pretty much impossible, except for a few special cases.
Also, unlinking was handled by each datatype, in a rather messy and prone-to-errors way (quite
a few ID usages were missed or wrongly handled that way).
One of the main goal of id-remap branch was to cleanup this, and fatorize ID links handling
by using library_query utils to allow generic handling of those, which is now the case
(now, generic ID links handling is only "knwon" from readfile.c and library_query.c).
This commit also adds backends to allow live replacement and deletion of datablocks in Blender
(so-called 'remapping' process, where we replace all usages of a given ID pointer by a new one,
or NULL one in case of unlinking).
This will allow nice new features, like ability to easily reload or relocate libraries, real immediate
deletion of datablocks in blender, replacement of one datablock by another, etc.
Some of those are for next commits.
A word of warning: this commit is highly risky, because it affects potentially a lot in Blender core.
Though it was tested rather deeply, being totally impossible to check all possible ID usage cases,
it's likely there are some remaining issues and bugs in new code... Please report them! ;)
Review task: D2027 (https://developer.blender.org/D2027).
Reviewed by campbellbarton, thanks a bunch.
2016-06-22 17:29:38 +02:00
|
|
|
BKE_animdata_free(&me->id, false);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2018-05-11 15:48:14 -03:00
|
|
|
BKE_mesh_runtime_clear_cache(me);
|
2017-01-25 09:16:29 +01:00
|
|
|
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
CustomData_free(&me->vdata, me->totvert);
|
|
|
|
CustomData_free(&me->edata, me->totedge);
|
|
|
|
CustomData_free(&me->fdata, me->totface);
|
2009-05-26 04:17:47 +00:00
|
|
|
CustomData_free(&me->ldata, me->totloop);
|
|
|
|
CustomData_free(&me->pdata, me->totpoly);
|
2004-07-08 20:38:27 +00:00
|
|
|
|
ID-Remap - Step one: core work (cleanup and rework of generic ID datablock handling).
This commit changes a lot of how IDs are handled internally, especially the unlinking/freeing
processes. So far, this was very fuzy, to summarize cleanly deleting or replacing a datablock
was pretty much impossible, except for a few special cases.
Also, unlinking was handled by each datatype, in a rather messy and prone-to-errors way (quite
a few ID usages were missed or wrongly handled that way).
One of the main goal of id-remap branch was to cleanup this, and fatorize ID links handling
by using library_query utils to allow generic handling of those, which is now the case
(now, generic ID links handling is only "knwon" from readfile.c and library_query.c).
This commit also adds backends to allow live replacement and deletion of datablocks in Blender
(so-called 'remapping' process, where we replace all usages of a given ID pointer by a new one,
or NULL one in case of unlinking).
This will allow nice new features, like ability to easily reload or relocate libraries, real immediate
deletion of datablocks in blender, replacement of one datablock by another, etc.
Some of those are for next commits.
A word of warning: this commit is highly risky, because it affects potentially a lot in Blender core.
Though it was tested rather deeply, being totally impossible to check all possible ID usage cases,
it's likely there are some remaining issues and bugs in new code... Please report them! ;)
Review task: D2027 (https://developer.blender.org/D2027).
Reviewed by campbellbarton, thanks a bunch.
2016-06-22 17:29:38 +02:00
|
|
|
MEM_SAFE_FREE(me->mat);
|
|
|
|
MEM_SAFE_FREE(me->bb);
|
|
|
|
MEM_SAFE_FREE(me->mselect);
|
|
|
|
MEM_SAFE_FREE(me->edit_btmesh);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 16:22:41 +00:00
|
|
|
static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
|
|
|
|
{
|
2012-10-31 09:50:24 +00:00
|
|
|
if (free_customdata) {
|
2012-03-06 16:22:41 +00:00
|
|
|
CustomData_free(&mesh->fdata, mesh->totface);
|
2012-10-31 09:50:24 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
CustomData_reset(&mesh->fdata);
|
|
|
|
}
|
2012-03-06 16:22:41 +00:00
|
|
|
|
|
|
|
mesh->mface = NULL;
|
|
|
|
mesh->mtface = NULL;
|
|
|
|
mesh->mcol = NULL;
|
|
|
|
mesh->totface = 0;
|
|
|
|
}
|
|
|
|
|
First step to handle missing libs/datablocks when reading a file.
Idea is, instead of ignoring completely missing linked datablocks, to
create void placeholders for them.
That way, you can work on your file, save it, and find again your missing data once
lib becomes available again. Or you can edit missing lib's path (in Outliner),
save and reload the file, and you are done.
Also, Outliner now shows broken libraries (and placeholders) with a 'broken lib' icon.
Future plans are also to be able to relocate missing libs and reload them at runtime.
Code notes:
- Placeholder ID is just a regular datablock of same type as expected linked one,
with 'default' data, and a LIB_MISSING bitflag set.
- To allow creation of such datablocks, creation of datablocks in BKE was split in two step:
+ Allocation of memory itself.
+ Setting of all internal data to default values.
See also the design task (T43351).
Reviewed by @campbellbarton, thanks a bunch!
Differential Revision: https://developer.blender.org/D1394
2015-10-20 14:44:57 +02:00
|
|
|
void BKE_mesh_init(Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
First step to handle missing libs/datablocks when reading a file.
Idea is, instead of ignoring completely missing linked datablocks, to
create void placeholders for them.
That way, you can work on your file, save it, and find again your missing data once
lib becomes available again. Or you can edit missing lib's path (in Outliner),
save and reload the file, and you are done.
Also, Outliner now shows broken libraries (and placeholders) with a 'broken lib' icon.
Future plans are also to be able to relocate missing libs and reload them at runtime.
Code notes:
- Placeholder ID is just a regular datablock of same type as expected linked one,
with 'default' data, and a LIB_MISSING bitflag set.
- To allow creation of such datablocks, creation of datablocks in BKE was split in two step:
+ Allocation of memory itself.
+ Setting of all internal data to default values.
See also the design task (T43351).
Reviewed by @campbellbarton, thanks a bunch!
Differential Revision: https://developer.blender.org/D1394
2015-10-20 14:44:57 +02:00
|
|
|
BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(me, id));
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
me->size[0] = me->size[1] = me->size[2] = 1.0;
|
2016-03-01 12:58:33 +01:00
|
|
|
me->smoothresh = DEG2RADF(30);
|
2012-05-06 15:15:33 +00:00
|
|
|
me->texflag = ME_AUTOSPACE;
|
2014-02-12 02:45:52 +11:00
|
|
|
|
|
|
|
/* disable because its slow on many GPU's, see [#37518] */
|
|
|
|
#if 0
|
2012-05-06 15:15:33 +00:00
|
|
|
me->flag = ME_TWOSIDED;
|
2014-02-12 02:45:52 +11:00
|
|
|
#endif
|
2012-05-06 15:15:33 +00:00
|
|
|
me->drawflag = ME_DRAWEDGES | ME_DRAWFACES | ME_DRAWCREASES;
|
2012-10-31 15:14:14 +00:00
|
|
|
|
|
|
|
CustomData_reset(&me->vdata);
|
|
|
|
CustomData_reset(&me->edata);
|
|
|
|
CustomData_reset(&me->fdata);
|
|
|
|
CustomData_reset(&me->pdata);
|
|
|
|
CustomData_reset(&me->ldata);
|
First step to handle missing libs/datablocks when reading a file.
Idea is, instead of ignoring completely missing linked datablocks, to
create void placeholders for them.
That way, you can work on your file, save it, and find again your missing data once
lib becomes available again. Or you can edit missing lib's path (in Outliner),
save and reload the file, and you are done.
Also, Outliner now shows broken libraries (and placeholders) with a 'broken lib' icon.
Future plans are also to be able to relocate missing libs and reload them at runtime.
Code notes:
- Placeholder ID is just a regular datablock of same type as expected linked one,
with 'default' data, and a LIB_MISSING bitflag set.
- To allow creation of such datablocks, creation of datablocks in BKE was split in two step:
+ Allocation of memory itself.
+ Setting of all internal data to default values.
See also the design task (T43351).
Reviewed by @campbellbarton, thanks a bunch!
Differential Revision: https://developer.blender.org/D1394
2015-10-20 14:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Mesh *BKE_mesh_add(Main *bmain, const char *name)
|
|
|
|
{
|
|
|
|
Mesh *me;
|
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
me = BKE_libblock_alloc(bmain, ID_ME, name, 0);
|
First step to handle missing libs/datablocks when reading a file.
Idea is, instead of ignoring completely missing linked datablocks, to
create void placeholders for them.
That way, you can work on your file, save it, and find again your missing data once
lib becomes available again. Or you can edit missing lib's path (in Outliner),
save and reload the file, and you are done.
Also, Outliner now shows broken libraries (and placeholders) with a 'broken lib' icon.
Future plans are also to be able to relocate missing libs and reload them at runtime.
Code notes:
- Placeholder ID is just a regular datablock of same type as expected linked one,
with 'default' data, and a LIB_MISSING bitflag set.
- To allow creation of such datablocks, creation of datablocks in BKE was split in two step:
+ Allocation of memory itself.
+ Setting of all internal data to default values.
See also the design task (T43351).
Reviewed by @campbellbarton, thanks a bunch!
Differential Revision: https://developer.blender.org/D1394
2015-10-20 14:44:57 +02:00
|
|
|
|
|
|
|
BKE_mesh_init(me);
|
2012-10-31 15:14:14 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
/**
|
|
|
|
* Only copy internal data of Mesh ID from source to already allocated/initialized destination.
|
|
|
|
* You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
|
|
|
|
*
|
|
|
|
* WARNING! This function will not handle ID user count!
|
|
|
|
*
|
|
|
|
* \param flag Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
|
|
|
|
*/
|
|
|
|
void BKE_mesh_copy_data(Main *bmain, Mesh *me_dst, const Mesh *me_src, const int flag)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
const bool do_tessface = ((me_src->totface != 0) && (me_src->totpoly == 0)); /* only do tessface if we have no polys */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
me_dst->mat = MEM_dupallocN(me_src->mat);
|
|
|
|
|
|
|
|
CustomData_copy(&me_src->vdata, &me_dst->vdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totvert);
|
|
|
|
CustomData_copy(&me_src->edata, &me_dst->edata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totedge);
|
|
|
|
CustomData_copy(&me_src->ldata, &me_dst->ldata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totloop);
|
|
|
|
CustomData_copy(&me_src->pdata, &me_dst->pdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totpoly);
|
2012-03-05 18:05:06 +00:00
|
|
|
if (do_tessface) {
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
CustomData_copy(&me_src->fdata, &me_dst->fdata, CD_MASK_MESH, CD_DUPLICATE, me_dst->totface);
|
2012-03-05 18:05:06 +00:00
|
|
|
}
|
|
|
|
else {
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
mesh_tessface_clear_intern(me_dst, false);
|
2012-03-05 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
BKE_mesh_update_customdata_pointers(me_dst, do_tessface);
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
me_dst->edit_btmesh = NULL;
|
2018-05-02 15:47:45 +02:00
|
|
|
me_dst->runtime.batch_cache = NULL;
|
2018-05-08 20:00:51 -03:00
|
|
|
me_dst->runtime.bvh_cache = NULL;
|
2006-05-10 17:22:49 +00:00
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
me_dst->mselect = MEM_dupallocN(me_dst->mselect);
|
|
|
|
me_dst->bb = MEM_dupallocN(me_dst->bb);
|
2016-07-11 19:13:42 +02:00
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
/* TODO Do we want to add flag to prevent this? */
|
|
|
|
if (me_src->key) {
|
|
|
|
BKE_id_copy_ex(bmain, &me_src->key->id, (ID **)&me_dst->key, flag, false);
|
2016-07-09 14:44:48 +02:00
|
|
|
}
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
}
|
2006-08-20 15:22:56 +00:00
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
Mesh *BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
|
2018-05-04 11:42:55 +02:00
|
|
|
{
|
2018-05-08 16:57:07 +02:00
|
|
|
Mesh *mesh = BKE_libblock_alloc(
|
|
|
|
NULL, ID_ME,
|
|
|
|
BKE_idcode_to_name(ID_ME),
|
|
|
|
LIB_ID_CREATE_NO_MAIN |
|
|
|
|
LIB_ID_CREATE_NO_USER_REFCOUNT |
|
|
|
|
LIB_ID_CREATE_NO_DEG_TAG);
|
2018-05-04 11:42:55 +02:00
|
|
|
BKE_libblock_init_empty(&mesh->id);
|
|
|
|
|
|
|
|
/* don't use CustomData_reset(...); because we dont want to touch customdata */
|
|
|
|
copy_vn_i(mesh->vdata.typemap, CD_NUMTYPES, -1);
|
|
|
|
copy_vn_i(mesh->edata.typemap, CD_NUMTYPES, -1);
|
|
|
|
copy_vn_i(mesh->fdata.typemap, CD_NUMTYPES, -1);
|
|
|
|
copy_vn_i(mesh->ldata.typemap, CD_NUMTYPES, -1);
|
|
|
|
copy_vn_i(mesh->pdata.typemap, CD_NUMTYPES, -1);
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer(&mesh->vdata, CD_ORIGINDEX, CD_CALLOC, NULL, verts_len);
|
|
|
|
CustomData_add_layer(&mesh->edata, CD_ORIGINDEX, CD_CALLOC, NULL, edges_len);
|
|
|
|
CustomData_add_layer(&mesh->fdata, CD_ORIGINDEX, CD_CALLOC, NULL, tessface_len);
|
|
|
|
CustomData_add_layer(&mesh->pdata, CD_ORIGINDEX, CD_CALLOC, NULL, polys_len);
|
2018-05-04 11:42:55 +02:00
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer(&mesh->vdata, CD_MVERT, CD_CALLOC, NULL, verts_len);
|
|
|
|
CustomData_add_layer(&mesh->edata, CD_MEDGE, CD_CALLOC, NULL, edges_len);
|
|
|
|
CustomData_add_layer(&mesh->fdata, CD_MFACE, CD_CALLOC, NULL, tessface_len);
|
|
|
|
CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_CALLOC, NULL, loops_len);
|
|
|
|
CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_CALLOC, NULL, polys_len);
|
2018-05-04 11:42:55 +02:00
|
|
|
|
|
|
|
mesh->mvert = CustomData_get_layer(&mesh->vdata, CD_MVERT);
|
|
|
|
mesh->medge = CustomData_get_layer(&mesh->edata, CD_MEDGE);
|
|
|
|
mesh->mface = CustomData_get_layer(&mesh->fdata, CD_MFACE);
|
|
|
|
mesh->mloop = CustomData_get_layer(&mesh->ldata, CD_MLOOP);
|
|
|
|
mesh->mpoly = CustomData_get_layer(&mesh->pdata, CD_MPOLY);
|
|
|
|
|
2018-05-11 10:37:16 +02:00
|
|
|
mesh->totvert = verts_len;
|
|
|
|
mesh->totedge = edges_len;
|
|
|
|
mesh->totloop = loops_len;
|
|
|
|
mesh->totpoly = polys_len;
|
|
|
|
|
2018-05-04 11:42:55 +02:00
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2018-05-08 17:06:30 +02:00
|
|
|
static Mesh *mesh_new_nomain_from_template_ex(
|
2018-04-25 12:21:07 +02:00
|
|
|
const Mesh *me_src,
|
2018-05-08 17:15:23 +02:00
|
|
|
int verts_len, int edges_len, int tessface_len,
|
|
|
|
int loops_len, int polys_len,
|
2018-04-19 11:03:58 +02:00
|
|
|
CustomDataMask mask)
|
|
|
|
{
|
|
|
|
const bool do_tessface = ((me_src->totface != 0) && (me_src->totpoly == 0)); /* only do tessface if we have no polys */
|
|
|
|
|
2018-05-03 15:42:55 +02:00
|
|
|
Mesh *me_dst = BKE_id_new_nomain(ID_ME, NULL);
|
2018-04-19 11:03:58 +02:00
|
|
|
|
|
|
|
me_dst->mat = MEM_dupallocN(me_src->mat);
|
|
|
|
me_dst->mselect = MEM_dupallocN(me_dst->mselect);
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
me_dst->totvert = verts_len;
|
|
|
|
me_dst->totedge = edges_len;
|
|
|
|
me_dst->totloop = loops_len;
|
|
|
|
me_dst->totpoly = polys_len;
|
2018-04-19 11:03:58 +02:00
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_copy(&me_src->vdata, &me_dst->vdata, mask, CD_CALLOC, verts_len);
|
|
|
|
CustomData_copy(&me_src->edata, &me_dst->edata, mask, CD_CALLOC, edges_len);
|
|
|
|
CustomData_copy(&me_src->ldata, &me_dst->ldata, mask, CD_CALLOC, loops_len);
|
|
|
|
CustomData_copy(&me_src->pdata, &me_dst->pdata, mask, CD_CALLOC, polys_len);
|
2018-04-19 11:03:58 +02:00
|
|
|
if (do_tessface) {
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_copy(&me_src->fdata, &me_dst->fdata, mask, CD_CALLOC, tessface_len);
|
2018-04-19 11:03:58 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mesh_tessface_clear_intern(me_dst, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
BKE_mesh_update_customdata_pointers(me_dst, false);
|
|
|
|
|
2018-05-02 15:08:54 +02:00
|
|
|
if (!CustomData_get_layer(&me_dst->vdata, CD_ORIGINDEX))
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer(&me_dst->vdata, CD_ORIGINDEX, CD_CALLOC, NULL, verts_len);
|
2018-05-02 15:08:54 +02:00
|
|
|
if (!CustomData_get_layer(&me_dst->edata, CD_ORIGINDEX))
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer(&me_dst->edata, CD_ORIGINDEX, CD_CALLOC, NULL, edges_len);
|
2018-05-02 15:08:54 +02:00
|
|
|
if (!CustomData_get_layer(&me_dst->pdata, CD_ORIGINDEX))
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer(&me_dst->pdata, CD_ORIGINDEX, CD_CALLOC, NULL, polys_len);
|
2018-05-02 15:08:54 +02:00
|
|
|
|
2018-04-19 11:03:58 +02:00
|
|
|
return me_dst;
|
|
|
|
}
|
|
|
|
|
2018-05-08 17:06:30 +02:00
|
|
|
Mesh * BKE_mesh_new_nomain_from_template(
|
|
|
|
const Mesh *me_src,
|
2018-05-08 17:15:23 +02:00
|
|
|
int verts_len, int edges_len, int tessface_len,
|
|
|
|
int loops_len, int polys_len)
|
2018-04-19 11:03:58 +02:00
|
|
|
{
|
2018-05-08 17:06:30 +02:00
|
|
|
return mesh_new_nomain_from_template_ex(
|
|
|
|
me_src,
|
2018-05-08 17:15:23 +02:00
|
|
|
verts_len, edges_len, tessface_len,
|
|
|
|
loops_len, polys_len,
|
2018-05-08 17:06:30 +02:00
|
|
|
CD_MASK_EVERYTHING);
|
2018-04-19 11:03:58 +02:00
|
|
|
}
|
|
|
|
|
Refactor ID copying (and to some extent, ID freeing).
This will allow much finer controll over how we copy data-blocks, from
full copy in Main database, to "lighter" ones (out of Main, inside an
already allocated datablock, etc.).
This commit also transfers a llot of what was previously handled by
per-ID-type custom code to generic ID handling code in BKE_library.
Hopefully will avoid in future inconsistencies and missing bits we had
all over the codebase in the past.
It also adds missing copying handling for a few types, most notably
Scene (which where using a fully customized handling previously).
Note that the type of allocation used during copying (regular in Main,
allocated but outside of Main, or not allocated by ID handling code at
all) is stored in ID's, which allows to handle them correctly when
freeing. This needs to be taken care of with caution when doing 'weird'
unusual things with ID copying and/or allocation!
As a final note, while rather noisy, this commit will hopefully not
break too much existing branches, old 'API' has been kept for the main
part, as a wrapper around new code. Cleaning it up will happen later.
Design task : T51804
Phab Diff: D2714
2017-08-07 16:39:55 +02:00
|
|
|
Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me)
|
|
|
|
{
|
|
|
|
Mesh *me_copy;
|
|
|
|
BKE_id_copy_ex(bmain, &me->id, (ID **)&me_copy, 0, false);
|
|
|
|
return me_copy;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
BMesh *BKE_mesh_to_bmesh_ex(
|
|
|
|
Mesh *me,
|
|
|
|
const struct BMeshCreateParams *create_params,
|
|
|
|
const struct BMeshFromMeshParams *convert_params)
|
2009-05-26 04:17:47 +00:00
|
|
|
{
|
|
|
|
BMesh *bm;
|
2013-09-24 03:31:00 +00:00
|
|
|
const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_ME(me);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
bm = BM_mesh_create(&allocsize, create_params);
|
|
|
|
BM_mesh_bm_from_me(bm, me, convert_params);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2018-05-02 11:39:23 +02:00
|
|
|
BMesh *BKE_mesh_to_bmesh(
|
|
|
|
Mesh *me, Object *ob,
|
|
|
|
const bool add_key_index, const struct BMeshCreateParams *params)
|
|
|
|
{
|
2018-05-04 09:04:33 +02:00
|
|
|
return BKE_mesh_to_bmesh_ex(
|
|
|
|
me, params,
|
|
|
|
&(struct BMeshFromMeshParams){
|
|
|
|
.calc_face_normal = false,
|
|
|
|
.add_key_index = add_key_index,
|
|
|
|
.use_shapekey = true,
|
|
|
|
.active_shapekey = ob->shapenr,
|
|
|
|
});
|
2018-05-02 11:39:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 11:03:16 +02:00
|
|
|
Mesh *BKE_bmesh_to_mesh_nomain(BMesh *bm, const struct BMeshToMeshParams *params)
|
2018-05-02 11:39:23 +02:00
|
|
|
{
|
2018-05-03 15:42:55 +02:00
|
|
|
Mesh *mesh = BKE_id_new_nomain(ID_ME, NULL);
|
2018-05-02 11:39:23 +02:00
|
|
|
BM_mesh_bm_to_me(bm, mesh, params);
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:49:45 +02:00
|
|
|
void BKE_mesh_make_local(Main *bmain, Mesh *me, const bool lib_local)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2016-07-20 19:49:45 +02:00
|
|
|
BKE_id_make_local_generic(bmain, &me->id, true, lib_local);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 21:03:16 +02:00
|
|
|
bool BKE_mesh_uv_cdlayer_rename_index(
|
|
|
|
Mesh *me, const int loop_index, const int face_index,
|
|
|
|
const char *new_name, const bool do_tessface)
|
2013-09-20 11:14:08 +00:00
|
|
|
{
|
2018-04-30 21:03:16 +02:00
|
|
|
CustomData *ldata, *fdata;
|
|
|
|
CustomDataLayer *cdlu, *cdlf;
|
2013-09-20 11:14:08 +00:00
|
|
|
|
2013-09-29 06:47:07 +00:00
|
|
|
if (me->edit_btmesh) {
|
|
|
|
ldata = &me->edit_btmesh->bm->ldata;
|
|
|
|
fdata = NULL; /* No tessellated data in BMesh! */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ldata = &me->ldata;
|
|
|
|
fdata = &me->fdata;
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:23:27 +10:00
|
|
|
cdlu = &ldata->layers[loop_index];
|
|
|
|
cdlf = (face_index != -1) && fdata && do_tessface ? &fdata->layers[face_index] : NULL;
|
2017-05-25 15:11:00 +10:00
|
|
|
|
|
|
|
if (cdlu->name != new_name) {
|
2014-01-13 16:26:10 +06:00
|
|
|
/* Mesh validate passes a name from the CD layer as the new name,
|
|
|
|
* Avoid memcpy from self to self in this case.
|
|
|
|
*/
|
2017-05-25 15:11:00 +10:00
|
|
|
BLI_strncpy(cdlu->name, new_name, sizeof(cdlu->name));
|
2017-06-28 10:23:27 +10:00
|
|
|
CustomData_set_layer_unique_name(ldata, loop_index);
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:03:16 +02:00
|
|
|
if (cdlf == NULL) {
|
2017-06-28 10:23:27 +10:00
|
|
|
return false;
|
2014-01-13 16:26:10 +06:00
|
|
|
}
|
2013-09-20 11:14:08 +00:00
|
|
|
|
2018-04-30 21:03:16 +02:00
|
|
|
BLI_strncpy(cdlf->name, cdlu->name, sizeof(cdlf->name));
|
|
|
|
CustomData_set_layer_unique_name(fdata, face_index);
|
2013-09-20 11:14:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BKE_mesh_uv_cdlayer_rename(Mesh *me, const char *old_name, const char *new_name, bool do_tessface)
|
|
|
|
{
|
2017-05-25 15:11:00 +10:00
|
|
|
CustomData *ldata, *fdata;
|
2013-09-29 15:09:48 +00:00
|
|
|
if (me->edit_btmesh) {
|
|
|
|
ldata = &me->edit_btmesh->bm->ldata;
|
|
|
|
/* No tessellated data in BMesh! */
|
|
|
|
fdata = NULL;
|
|
|
|
do_tessface = false;
|
2013-09-20 11:14:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-09-29 15:09:48 +00:00
|
|
|
ldata = &me->ldata;
|
|
|
|
fdata = &me->fdata;
|
|
|
|
do_tessface = (do_tessface && fdata->totlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const int lidx_start = CustomData_get_layer_index(ldata, CD_MLOOPUV);
|
|
|
|
const int fidx_start = do_tessface ? CustomData_get_layer_index(fdata, CD_MTFACE) : -1;
|
|
|
|
int lidx = CustomData_get_named_layer(ldata, CD_MLOOPUV, old_name);
|
|
|
|
int fidx = do_tessface ? CustomData_get_named_layer(fdata, CD_MTFACE, old_name) : -1;
|
|
|
|
|
|
|
|
/* None of those cases should happen, in theory!
|
|
|
|
* Note this assume we have the same number of mtexpoly, mloopuv and mtface layers!
|
|
|
|
*/
|
2017-05-25 15:11:00 +10:00
|
|
|
if (lidx == -1) {
|
|
|
|
if (fidx == -1) {
|
|
|
|
/* No layer found with this name! */
|
|
|
|
return false;
|
2013-09-29 15:09:48 +00:00
|
|
|
}
|
2017-05-25 15:11:00 +10:00
|
|
|
else {
|
|
|
|
lidx = fidx;
|
2013-09-29 15:09:48 +00:00
|
|
|
}
|
2013-09-20 11:14:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 20:32:27 +00:00
|
|
|
/* Go back to absolute indices! */
|
|
|
|
lidx += lidx_start;
|
|
|
|
if (fidx != -1)
|
|
|
|
fidx += fidx_start;
|
|
|
|
|
2018-04-30 21:03:16 +02:00
|
|
|
return BKE_mesh_uv_cdlayer_rename_index(me, lidx, fidx, new_name, do_tessface);
|
2013-09-29 15:09:48 +00:00
|
|
|
}
|
2013-09-20 11:14:08 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
void BKE_mesh_boundbox_calc(Mesh *me, float r_loc[3], float r_size[3])
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
BoundBox *bb;
|
|
|
|
float min[3], max[3];
|
|
|
|
float mloc[3], msize[3];
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (me->bb == NULL) me->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
|
|
|
|
bb = me->bb;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (!r_loc) r_loc = mloc;
|
|
|
|
if (!r_size) r_size = msize;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-08-03 00:56:43 +00:00
|
|
|
INIT_MINMAX(min, max);
|
2012-05-05 21:28:12 +00:00
|
|
|
if (!BKE_mesh_minmax(me, min, max)) {
|
2005-07-18 17:33:51 +00:00
|
|
|
min[0] = min[1] = min[2] = -1.0f;
|
|
|
|
max[0] = max[1] = max[2] = 1.0f;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
mid_v3_v3v3(r_loc, min, max);
|
2005-07-18 17:33:51 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
r_size[0] = (max[0] - min[0]) / 2.0f;
|
|
|
|
r_size[1] = (max[1] - min[1]) / 2.0f;
|
|
|
|
r_size[2] = (max[2] - min[2]) / 2.0f;
|
2005-07-18 17:33:51 +00:00
|
|
|
|
2012-05-05 14:03:12 +00:00
|
|
|
BKE_boundbox_init_from_minmax(bb, min, max);
|
2013-08-19 09:58:28 +00:00
|
|
|
|
|
|
|
bb->flag &= ~BOUNDBOX_DIRTY;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
void BKE_mesh_texspace_calc(Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-11-01 00:06:53 +00:00
|
|
|
float loc[3], size[3];
|
2002-10-12 11:37:38 +00:00
|
|
|
int a;
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
BKE_mesh_boundbox_calc(me, loc, size);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-02-19 22:17:30 +00:00
|
|
|
if (me->texflag & ME_AUTOSPACE) {
|
2012-05-06 15:15:33 +00:00
|
|
|
for (a = 0; a < 3; a++) {
|
|
|
|
if (size[a] == 0.0f) size[a] = 1.0f;
|
|
|
|
else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
|
|
|
|
else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 11:00:36 +00:00
|
|
|
copy_v3_v3(me->loc, loc);
|
|
|
|
copy_v3_v3(me->size, size);
|
|
|
|
zero_v3(me->rot);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
BoundBox *BKE_mesh_boundbox_get(Object *ob)
|
2005-07-14 18:04:27 +00:00
|
|
|
{
|
2012-05-06 15:15:33 +00:00
|
|
|
Mesh *me = ob->data;
|
2007-12-05 21:50:23 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (ob->bb)
|
2007-12-05 21:50:23 +00:00
|
|
|
return ob->bb;
|
|
|
|
|
2013-08-19 09:58:28 +00:00
|
|
|
if (me->bb == NULL || (me->bb->flag & BOUNDBOX_DIRTY)) {
|
2012-05-05 21:28:12 +00:00
|
|
|
BKE_mesh_texspace_calc(me);
|
2013-08-19 09:58:28 +00:00
|
|
|
}
|
2005-07-14 18:04:27 +00:00
|
|
|
|
|
|
|
return me->bb;
|
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
void BKE_mesh_texspace_get(Mesh *me, float r_loc[3], float r_rot[3], float r_size[3])
|
2005-07-14 21:57:18 +00:00
|
|
|
{
|
2013-08-19 09:58:28 +00:00
|
|
|
if (me->bb == NULL || (me->bb->flag & BOUNDBOX_DIRTY)) {
|
2012-05-05 21:28:12 +00:00
|
|
|
BKE_mesh_texspace_calc(me);
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (r_loc) copy_v3_v3(r_loc, me->loc);
|
|
|
|
if (r_rot) copy_v3_v3(r_rot, me->rot);
|
2012-02-28 14:05:00 +00:00
|
|
|
if (r_size) copy_v3_v3(r_size, me->size);
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 15:19:02 +02:00
|
|
|
void BKE_mesh_texspace_get_reference(Mesh *me, short **r_texflag, float **r_loc, float **r_rot, float **r_size)
|
|
|
|
{
|
|
|
|
if (me->bb == NULL || (me->bb->flag & BOUNDBOX_DIRTY)) {
|
|
|
|
BKE_mesh_texspace_calc(me);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_texflag != NULL) *r_texflag = &me->texflag;
|
|
|
|
if (r_loc != NULL) *r_loc = me->loc;
|
|
|
|
if (r_rot != NULL) *r_rot = me->rot;
|
|
|
|
if (r_size != NULL) *r_size = me->size;
|
|
|
|
}
|
|
|
|
|
2013-07-15 11:11:36 +00:00
|
|
|
void BKE_mesh_texspace_copy_from_object(Mesh *me, Object *ob)
|
|
|
|
{
|
|
|
|
float *texloc, *texrot, *texsize;
|
|
|
|
short *texflag;
|
|
|
|
|
|
|
|
if (BKE_object_obdata_texspace_get(ob, &texflag, &texloc, &texsize, &texrot)) {
|
|
|
|
me->texflag = *texflag;
|
|
|
|
copy_v3_v3(me->loc, texloc);
|
|
|
|
copy_v3_v3(me->size, texsize);
|
|
|
|
copy_v3_v3(me->rot, texrot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 07:28:14 +00:00
|
|
|
float (*BKE_mesh_orco_verts_get(Object *ob))[3]
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2005-07-20 04:44:02 +00:00
|
|
|
Mesh *me = ob->data;
|
2009-11-01 00:06:53 +00:00
|
|
|
MVert *mvert = NULL;
|
2012-05-06 15:15:33 +00:00
|
|
|
Mesh *tme = me->texcomesh ? me->texcomesh : me;
|
2005-07-18 19:58:23 +00:00
|
|
|
int a, totvert;
|
2007-01-21 23:46:00 +00:00
|
|
|
float (*vcos)[3] = NULL;
|
2005-07-18 18:28:16 +00:00
|
|
|
|
2007-12-05 12:40:54 +00:00
|
|
|
/* Get appropriate vertex coordinates */
|
2018-01-14 22:14:20 +01:00
|
|
|
vcos = MEM_calloc_arrayN(me->totvert, sizeof(*vcos), "orco mesh");
|
2009-11-01 00:06:53 +00:00
|
|
|
mvert = tme->mvert;
|
2012-10-23 13:28:22 +00:00
|
|
|
totvert = min_ii(tme->totvert, me->totvert);
|
2009-11-01 00:06:53 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (a = 0; a < totvert; a++, mvert++) {
|
2010-08-01 11:00:36 +00:00
|
|
|
copy_v3_v3(vcos[a], mvert->co);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2005-07-18 18:28:16 +00:00
|
|
|
|
2012-12-21 07:28:14 +00:00
|
|
|
return vcos;
|
2007-12-05 12:40:54 +00:00
|
|
|
}
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
void BKE_mesh_orco_verts_transform(Mesh *me, float (*orco)[3], int totvert, int invert)
|
2007-12-05 12:40:54 +00:00
|
|
|
{
|
|
|
|
float loc[3], size[3];
|
|
|
|
int a;
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
BKE_mesh_texspace_get(me->texcomesh ? me->texcomesh : me, loc, NULL, size);
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (invert) {
|
2012-05-06 15:15:33 +00:00
|
|
|
for (a = 0; a < totvert; a++) {
|
2007-12-11 20:02:21 +00:00
|
|
|
float *co = orco[a];
|
2010-08-01 11:00:36 +00:00
|
|
|
madd_v3_v3v3v3(co, loc, co, size);
|
2007-12-11 20:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-05-06 15:15:33 +00:00
|
|
|
for (a = 0; a < totvert; a++) {
|
2007-12-11 20:02:21 +00:00
|
|
|
float *co = orco[a];
|
2012-05-06 15:15:33 +00:00
|
|
|
co[0] = (co[0] - loc[0]) / size[0];
|
|
|
|
co[1] = (co[1] - loc[1]) / size[1];
|
|
|
|
co[2] = (co[2] - loc[2]) / size[2];
|
2007-12-11 20:02:21 +00:00
|
|
|
}
|
2005-07-18 19:58:23 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2006-11-08 20:14:04 +00:00
|
|
|
/* rotates the vertices of a face in case v[2] or v[3] (vertex index) is = 0.
|
2012-03-03 20:19:11 +00:00
|
|
|
* this is necessary to make the if (mface->v4) check for quads work */
|
2008-01-29 19:49:03 +00:00
|
|
|
int test_index_face(MFace *mface, CustomData *fdata, int mfindex, int nr)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
/* first test if the face is legal */
|
2012-05-06 15:15:33 +00:00
|
|
|
if ((mface->v3 || nr == 4) && mface->v3 == mface->v4) {
|
|
|
|
mface->v4 = 0;
|
2002-10-12 11:37:38 +00:00
|
|
|
nr--;
|
|
|
|
}
|
2012-05-06 15:15:33 +00:00
|
|
|
if ((mface->v2 || mface->v4) && mface->v2 == mface->v3) {
|
|
|
|
mface->v3 = mface->v4;
|
|
|
|
mface->v4 = 0;
|
2002-10-12 11:37:38 +00:00
|
|
|
nr--;
|
|
|
|
}
|
2012-05-06 15:15:33 +00:00
|
|
|
if (mface->v1 == mface->v2) {
|
|
|
|
mface->v2 = mface->v3;
|
|
|
|
mface->v3 = mface->v4;
|
|
|
|
mface->v4 = 0;
|
2002-10-12 11:37:38 +00:00
|
|
|
nr--;
|
|
|
|
}
|
|
|
|
|
2012-09-26 20:05:38 +00:00
|
|
|
/* check corrupt cases, bow-tie geometry, cant handle these because edge data wont exist so just return 0 */
|
2012-05-06 15:15:33 +00:00
|
|
|
if (nr == 3) {
|
2012-02-23 02:17:50 +00:00
|
|
|
if (
|
2012-05-06 15:15:33 +00:00
|
|
|
/* real edges */
|
|
|
|
mface->v1 == mface->v2 ||
|
|
|
|
mface->v2 == mface->v3 ||
|
|
|
|
mface->v3 == mface->v1)
|
|
|
|
{
|
2011-02-09 04:45:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-05-06 15:15:33 +00:00
|
|
|
else if (nr == 4) {
|
2012-02-23 02:17:50 +00:00
|
|
|
if (
|
2012-05-06 15:15:33 +00:00
|
|
|
/* real edges */
|
|
|
|
mface->v1 == mface->v2 ||
|
|
|
|
mface->v2 == mface->v3 ||
|
|
|
|
mface->v3 == mface->v4 ||
|
|
|
|
mface->v4 == mface->v1 ||
|
|
|
|
/* across the face */
|
|
|
|
mface->v1 == mface->v3 ||
|
2012-05-20 19:49:27 +00:00
|
|
|
mface->v2 == mface->v4)
|
|
|
|
{
|
2011-02-09 04:45:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* prevent a zero at wrong index location */
|
2012-05-06 15:15:33 +00:00
|
|
|
if (nr == 3) {
|
|
|
|
if (mface->v3 == 0) {
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
static int corner_indices[4] = {1, 2, 0, 3};
|
|
|
|
|
2011-12-27 03:54:23 +00:00
|
|
|
SWAP(unsigned int, mface->v1, mface->v2);
|
|
|
|
SWAP(unsigned int, mface->v2, mface->v3);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (fdata)
|
2016-02-28 15:16:42 +01:00
|
|
|
CustomData_swap_corners(fdata, mfindex, corner_indices);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-06 15:15:33 +00:00
|
|
|
else if (nr == 4) {
|
|
|
|
if (mface->v3 == 0 || mface->v4 == 0) {
|
Added custom vertex/edge/face data for meshes:
All data layers, including MVert/MEdge/MFace, are now managed as custom
data layers. The pointers like Mesh.mvert, Mesh.dvert or Mesh.mcol are
still used of course, but allocating, copying or freeing these arrays
should be done through the CustomData API.
Work in progress documentation on this is here:
http://mediawiki.blender.org/index.php/BlenderDev/BlenderArchitecture/CustomData
Replaced TFace by MTFace:
This is the same struct, except that it does not contain color, that now
always stays separated in MCol. This was not a good design decision to
begin with, and it is needed for adding multiple color layers later. Note
that this does mean older Blender versions will not be able to read UV
coordinates from the next release, due to an SDNA limitation.
Removed DispListMesh:
This now fully replaced by DerivedMesh. To provide access to arrays of
vertices, edges and faces, like DispListMesh does. The semantics of the
DerivedMesh.getVertArray() and similar functions were changed to return
a pointer to an array if one exists, or otherwise allocate a temporary
one. On releasing the DerivedMesh, this temporary array will be removed
automatically.
Removed ssDM and meshDM DerivedMesh backends:
The ssDM backend was for DispListMesh, so that became obsolete automatically.
The meshDM backend was replaced by the custom data backend, that now figures
out which layers need to be modified, and only duplicates those.
This changes code in many places, and overall removes 2514 lines of code.
So, there's a good chance this might break some stuff, although I've been
testing it for a few days now. The good news is, adding multiple color and
uv layers should now become easy.
2006-11-20 04:28:02 +00:00
|
|
|
static int corner_indices[4] = {2, 3, 0, 1};
|
|
|
|
|
2011-12-27 03:54:23 +00:00
|
|
|
SWAP(unsigned int, mface->v1, mface->v3);
|
|
|
|
SWAP(unsigned int, mface->v2, mface->v4);
|
- convert all DerivedMesh map functions to use index based
mapping (instead of Edit{Vert,Edge,Face} pointers)
- dropped convertToDispListMeshMapped (whew, glad of it too)
- added DerivedMesh drawMappedFaces function
- dropped EM suffix for DerivedMesh functions, it was neither
particularly correct nor descriptive
- converted test_index_mface to test_index_face that also corrects
MCol and TFace. Good thing we had three versions of this routine,
you never know when one might burn down.
- removed flipnorm_mesh, not used anymore (and was incorrect to
boot)
- Getting face select to work with modifiers turned out to be much
more complicated than expected. Reworked mapping architecture for
modifiers - basically elements in a DispListMesh are now required
to be stored in an order that corresponds exactly to original
ordering. MVert/MEdge/MFace all have a new flag ME_XXX_STEPINDEX
that is set on each element that is set on the first derived element
of each original element. I can't say the code to follow these
requirements for subsurf is particularly transparent, but on the
upside it is a reasonably consistent and simple system that is memory
efficient and allows keeping the DispListMesh structure.
- rewrote mirror modifier to be simpler/conform to new requirements
for mapped DispListMesh structure. This also means that mirror interacts
much better with incremental subsurf calculation (it used to recalc
one entire side on any topology change, now it generally avoids that).
- added EM_{init,free}_index_arrays and EM_get_{vert,edge,face}_for_index
functions to handle mapping indices back into appropriate EditMesh
structures.
- bug fix, make edges didn't recalc object data
- bug fix, initial image assignment to TFace's didn't recalc object data
- new feature, added circle select support for FACESELECT
- bug fix, creating new faces in editmode duplicated the TFACE active
flag - but there should only be one active tface
- bug fix, possible crash when deleting all faces in faceselect mode
on mesh with tfaces...
Still todo: TFace edge drawing is still not always correct in face
mode, in particular with a mirror modifier when mesh has edges (and
no preceeding subsurf). Have not yet decided how to deal with this.
Best solution is probably to do switch to meshes all having MEdge's,
in which case I can get rid of TFace edge flags (and need to recalc
modifiers on tface selection change).
2005-08-20 03:08:23 +00:00
|
|
|
|
2012-02-23 02:17:50 +00:00
|
|
|
if (fdata)
|
2016-02-28 15:16:42 +01:00
|
|
|
CustomData_swap_corners(fdata, mfindex, corner_indices);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-29 19:49:03 +00:00
|
|
|
|
|
|
|
return nr;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 16:03:57 +00:00
|
|
|
Mesh *BKE_mesh_from_object(Object *ob)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (ob == NULL) return NULL;
|
|
|
|
if (ob->type == OB_MESH) return ob->data;
|
2011-02-13 10:52:18 +00:00
|
|
|
else return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-17 19:55:10 +00:00
|
|
|
void BKE_mesh_assign_object(Object *ob, Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2012-05-06 15:15:33 +00:00
|
|
|
Mesh *old = NULL;
|
2010-09-09 00:14:51 +00:00
|
|
|
|
|
|
|
multires_force_update(ob);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (ob == NULL) return;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
if (ob->type == OB_MESH) {
|
|
|
|
old = ob->data;
|
2009-10-22 23:22:05 +00:00
|
|
|
if (old)
|
2015-11-09 19:47:10 +01:00
|
|
|
id_us_min(&old->id);
|
2012-05-06 15:15:33 +00:00
|
|
|
ob->data = me;
|
2002-10-12 11:37:38 +00:00
|
|
|
id_us_plus((ID *)me);
|
|
|
|
}
|
|
|
|
|
2016-07-08 14:36:25 +02:00
|
|
|
test_object_materials(ob, (ID *)me);
|
2010-09-09 00:14:51 +00:00
|
|
|
|
|
|
|
test_object_modifiers(ob);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 11:29:58 +00:00
|
|
|
void BKE_mesh_material_index_remove(Mesh *me, short index)
|
2009-01-04 14:14:06 +00:00
|
|
|
{
|
2013-08-14 11:29:58 +00:00
|
|
|
MPoly *mp;
|
|
|
|
MFace *mf;
|
2004-03-20 23:59:57 +00:00
|
|
|
int i;
|
|
|
|
|
2013-08-14 11:29:58 +00:00
|
|
|
for (mp = me->mpoly, i = 0; i < me->totpoly; i++, mp++) {
|
|
|
|
if (mp->mat_nr && mp->mat_nr >= index) {
|
2011-04-24 08:11:44 +00:00
|
|
|
mp->mat_nr--;
|
2013-08-14 11:29:58 +00:00
|
|
|
}
|
2011-04-24 08:06:26 +00:00
|
|
|
}
|
2013-08-14 11:29:58 +00:00
|
|
|
|
|
|
|
for (mf = me->mface, i = 0; i < me->totface; i++, mf++) {
|
|
|
|
if (mf->mat_nr && mf->mat_nr >= index) {
|
2004-03-20 23:59:57 +00:00
|
|
|
mf->mat_nr--;
|
2013-08-14 11:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_material_index_clear(Mesh *me)
|
|
|
|
{
|
|
|
|
MPoly *mp;
|
|
|
|
MFace *mf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (mp = me->mpoly, i = 0; i < me->totpoly; i++, mp++) {
|
|
|
|
mp->mat_nr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (mf = me->mface, i = 0; i < me->totface; i++, mf++) {
|
|
|
|
mf->mat_nr = 0;
|
2004-03-20 23:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 07:24:56 +10:00
|
|
|
void BKE_mesh_material_remap(Mesh *me, const unsigned int *remap, unsigned int remap_len)
|
|
|
|
{
|
|
|
|
const short remap_len_short = (short)remap_len;
|
|
|
|
|
|
|
|
#define MAT_NR_REMAP(n) \
|
|
|
|
if (n < remap_len_short) { \
|
|
|
|
BLI_assert(n >= 0 && remap[n] < remap_len_short); \
|
|
|
|
n = remap[n]; \
|
|
|
|
} ((void)0)
|
|
|
|
|
|
|
|
if (me->edit_btmesh) {
|
|
|
|
BMEditMesh *em = me->edit_btmesh;
|
|
|
|
BMIter iter;
|
|
|
|
BMFace *efa;
|
|
|
|
|
|
|
|
BM_ITER_MESH(efa, &iter, em->bm, BM_FACES_OF_MESH) {
|
|
|
|
MAT_NR_REMAP(efa->mat_nr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < me->totpoly; i++) {
|
|
|
|
MAT_NR_REMAP(me->mpoly[i].mat_nr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAT_NR_REMAP
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-05-05 21:28:12 +00:00
|
|
|
void BKE_mesh_smooth_flag_set(Object *meshOb, int enableSmooth)
|
2009-01-04 14:14:06 +00:00
|
|
|
{
|
2005-07-14 15:48:01 +00:00
|
|
|
Mesh *me = meshOb->data;
|
2004-03-20 23:59:57 +00:00
|
|
|
int i;
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (i = 0; i < me->totpoly; i++) {
|
2015-07-12 02:53:37 +10:00
|
|
|
MPoly *mp = &me->mpoly[i];
|
2011-04-24 08:06:26 +00:00
|
|
|
|
|
|
|
if (enableSmooth) {
|
|
|
|
mp->flag |= ME_SMOOTH;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-04-24 08:06:26 +00:00
|
|
|
mp->flag &= ~ME_SMOOTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 15:15:33 +00:00
|
|
|
for (i = 0; i < me->totface; i++) {
|
2015-07-12 02:53:37 +10:00
|
|
|
MFace *mf = &me->mface[i];
|
2004-03-20 23:59:57 +00:00
|
|
|
|
|
|
|
if (enableSmooth) {
|
|
|
|
mf->flag |= ME_SMOOTH;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2004-03-20 23:59:57 +00:00
|
|
|
mf->flag &= ~ME_SMOOTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
|
2013-08-27 02:25:15 +00:00
|
|
|
/**
|
2013-09-09 02:11:44 +00:00
|
|
|
* Return a newly MEM_malloc'd array of all the mesh vertex locations
|
2018-05-08 17:15:23 +02:00
|
|
|
* \note \a r_verts_len may be NULL
|
2013-08-27 02:25:15 +00:00
|
|
|
*/
|
2018-05-08 17:15:23 +02:00
|
|
|
float (*BKE_mesh_vertexCos_get(const Mesh *me, int *r_verts_len))[3]
|
2013-08-27 02:25:15 +00:00
|
|
|
{
|
2018-05-08 17:15:23 +02:00
|
|
|
int i, verts_len = me->totvert;
|
|
|
|
float (*cos)[3] = MEM_malloc_arrayN(verts_len, sizeof(*cos), "vertexcos1");
|
2013-08-27 02:25:15 +00:00
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
if (r_verts_len) *r_verts_len = verts_len;
|
|
|
|
for (i = 0; i < verts_len; i++)
|
2013-09-09 02:11:44 +00:00
|
|
|
copy_v3_v3(cos[i], me->mvert[i].co);
|
2013-08-27 02:25:15 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
return cos;
|
2011-12-06 22:55:41 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/**
|
|
|
|
* Find the index of the loop in 'poly' which references vertex,
|
|
|
|
* returns -1 if not found
|
|
|
|
*/
|
2015-04-19 14:21:23 +10:00
|
|
|
int poly_find_loop_from_vert(
|
|
|
|
const MPoly *poly, const MLoop *loopstart,
|
|
|
|
unsigned vert)
|
2011-04-15 01:19:13 +00:00
|
|
|
{
|
2013-09-09 02:11:44 +00:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < poly->totloop; j++, loopstart++) {
|
|
|
|
if (loopstart->v == vert)
|
|
|
|
return j;
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
return -1;
|
2004-03-20 23:59:57 +00:00
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/**
|
2014-03-16 03:24:05 +11:00
|
|
|
* Fill \a r_adj with the loop indices in \a poly adjacent to the
|
2013-09-09 02:11:44 +00:00
|
|
|
* vertex. Returns the index of the loop matching vertex, or -1 if the
|
|
|
|
* vertex is not in \a poly
|
|
|
|
*/
|
2015-04-19 14:21:23 +10:00
|
|
|
int poly_get_adj_loops_from_vert(
|
2015-07-11 04:39:27 +10:00
|
|
|
const MPoly *poly,
|
|
|
|
const MLoop *mloop, unsigned int vert,
|
|
|
|
unsigned int r_adj[2])
|
2012-01-06 00:08:37 +00:00
|
|
|
{
|
2013-09-09 02:11:44 +00:00
|
|
|
int corner = poly_find_loop_from_vert(poly,
|
|
|
|
&mloop[poly->loopstart],
|
|
|
|
vert);
|
|
|
|
|
|
|
|
if (corner != -1) {
|
2015-04-19 14:21:23 +10:00
|
|
|
#if 0 /* unused - this loop */
|
2013-09-09 02:11:44 +00:00
|
|
|
const MLoop *ml = &mloop[poly->loopstart + corner];
|
2015-04-19 14:21:23 +10:00
|
|
|
#endif
|
2013-05-10 10:07:01 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* vertex was found */
|
2014-03-16 03:24:05 +11:00
|
|
|
r_adj[0] = ME_POLY_LOOP_PREV(mloop, poly, corner)->v;
|
2015-04-19 14:21:23 +10:00
|
|
|
r_adj[1] = ME_POLY_LOOP_NEXT(mloop, poly, corner)->v;
|
2013-05-10 10:07:01 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
return corner;
|
2013-05-10 10:07:01 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/**
|
|
|
|
* Return the index of the edge vert that is not equal to \a v. If
|
|
|
|
* neither edge vertex is equal to \a v, returns -1.
|
|
|
|
*/
|
|
|
|
int BKE_mesh_edge_other_vert(const MEdge *e, int v)
|
2013-05-10 10:07:01 +00:00
|
|
|
{
|
2013-09-09 02:11:44 +00:00
|
|
|
if (e->v1 == v)
|
|
|
|
return e->v2;
|
|
|
|
else if (e->v2 == v)
|
|
|
|
return e->v1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-12 09:35:02 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* basic vertex data functions */
|
2015-07-11 04:39:27 +10:00
|
|
|
bool BKE_mesh_minmax(const Mesh *me, float r_min[3], float r_max[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
|
|
|
int i = me->totvert;
|
|
|
|
MVert *mvert;
|
|
|
|
for (mvert = me->mvert; i--; mvert++) {
|
|
|
|
minmax_v3v3_v3(r_min, r_max, mvert->co);
|
2013-06-12 09:35:02 +00:00
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
|
|
|
|
return (me->totvert != 0);
|
|
|
|
}
|
2013-06-12 09:35:02 +00:00
|
|
|
|
2014-09-01 20:09:31 +10:00
|
|
|
void BKE_mesh_transform(Mesh *me, float mat[4][4], bool do_keys)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
MVert *mvert = me->mvert;
|
2015-05-03 11:55:58 +02:00
|
|
|
float (*lnors)[3] = CustomData_get_layer(&me->ldata, CD_NORMAL);
|
2014-09-01 20:09:31 +10:00
|
|
|
|
|
|
|
for (i = 0; i < me->totvert; i++, mvert++)
|
|
|
|
mul_m4_v3(mat, mvert->co);
|
|
|
|
|
|
|
|
if (do_keys && me->key) {
|
|
|
|
KeyBlock *kb;
|
|
|
|
for (kb = me->key->block.first; kb; kb = kb->next) {
|
|
|
|
float *fp = kb->data;
|
|
|
|
for (i = kb->totelem; i--; fp += 3) {
|
|
|
|
mul_m4_v3(mat, fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 11:55:58 +02:00
|
|
|
/* don't update normals, caller can do this explicitly.
|
|
|
|
* We do update loop normals though, those may not be auto-generated (see e.g. STL import script)! */
|
|
|
|
if (lnors) {
|
|
|
|
float m3[3][3];
|
|
|
|
|
|
|
|
copy_m3_m4(m3, mat);
|
|
|
|
normalize_m3(m3);
|
|
|
|
for (i = 0; i < me->totloop; i++, lnors++) {
|
|
|
|
mul_m3_v3(m3, *lnors);
|
|
|
|
}
|
|
|
|
}
|
2014-09-01 20:09:31 +10:00
|
|
|
}
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_translate(Mesh *me, const float offset[3], const bool do_keys)
|
|
|
|
{
|
|
|
|
int i = me->totvert;
|
|
|
|
MVert *mvert;
|
|
|
|
for (mvert = me->mvert; i--; mvert++) {
|
|
|
|
add_v3_v3(mvert->co, offset);
|
2013-05-30 18:09:19 +00:00
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
|
|
|
|
if (do_keys && me->key) {
|
|
|
|
KeyBlock *kb;
|
|
|
|
for (kb = me->key->block.first; kb; kb = kb->next) {
|
|
|
|
float *fp = kb->data;
|
|
|
|
for (i = kb->totelem; i--; fp += 3) {
|
|
|
|
add_v3_v3(fp, offset);
|
|
|
|
}
|
2013-05-30 18:09:19 +00:00
|
|
|
}
|
2013-05-10 10:07:01 +00:00
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_ensure_navmesh(Mesh *me)
|
|
|
|
{
|
|
|
|
if (!CustomData_has_layer(&me->pdata, CD_RECAST)) {
|
|
|
|
int i;
|
2018-05-08 17:15:23 +02:00
|
|
|
int polys_len = me->totpoly;
|
2013-09-09 02:11:44 +00:00
|
|
|
int *recastData;
|
2018-05-08 17:15:23 +02:00
|
|
|
recastData = (int *)MEM_malloc_arrayN(polys_len, sizeof(int), __func__);
|
|
|
|
for (i = 0; i < polys_len; i++) {
|
2013-09-09 02:11:44 +00:00
|
|
|
recastData[i] = i + 1;
|
2012-09-17 22:22:06 +00:00
|
|
|
}
|
2018-05-08 17:15:23 +02:00
|
|
|
CustomData_add_layer_named(&me->pdata, CD_RECAST, CD_ASSIGN, recastData, polys_len, "recastData");
|
2012-01-06 00:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_tessface_calc(Mesh *mesh)
|
2013-05-28 14:23:07 +00:00
|
|
|
{
|
2013-09-09 02:11:44 +00:00
|
|
|
mesh->totface = BKE_mesh_recalc_tessellation(&mesh->fdata, &mesh->ldata, &mesh->pdata,
|
|
|
|
mesh->mvert,
|
|
|
|
mesh->totface, mesh->totloop, mesh->totpoly,
|
|
|
|
/* calc normals right after, don't copy from polys here */
|
|
|
|
false);
|
|
|
|
|
|
|
|
BKE_mesh_update_customdata_pointers(mesh, true);
|
2013-05-28 14:23:07 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_tessface_ensure(Mesh *mesh)
|
2005-07-17 21:24:43 +00:00
|
|
|
{
|
2013-09-09 02:11:44 +00:00
|
|
|
if (mesh->totpoly && mesh->totface == 0) {
|
|
|
|
BKE_mesh_tessface_calc(mesh);
|
2005-07-17 21:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-22 17:03:50 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_tessface_clear(Mesh *mesh)
|
2012-02-08 11:52:44 +00:00
|
|
|
{
|
2014-04-01 11:34:00 +11:00
|
|
|
mesh_tessface_clear_intern(mesh, true);
|
2012-02-08 11:52:44 +00:00
|
|
|
}
|
2012-05-08 14:58:38 +00:00
|
|
|
|
2013-01-10 04:43:31 +00:00
|
|
|
void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh)
|
|
|
|
{
|
|
|
|
if (UNLIKELY(mesh->cd_flag)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MVert *mv;
|
|
|
|
MEdge *med;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (mv = mesh->mvert, i = 0; i < mesh->totvert; mv++, i++) {
|
|
|
|
if (mv->bweight != 0) {
|
|
|
|
mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (med = mesh->medge, i = 0; i < mesh->totedge; med++, i++) {
|
|
|
|
if (med->bweight != 0) {
|
|
|
|
mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
|
|
|
|
if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (med->crease != 0) {
|
|
|
|
mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE;
|
|
|
|
if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 09:42:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* MSelect functions (currently used in weight paint mode) */
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_clear(Mesh *me)
|
|
|
|
{
|
2013-06-15 11:02:36 +00:00
|
|
|
if (me->mselect) {
|
|
|
|
MEM_freeN(me->mselect);
|
|
|
|
me->mselect = NULL;
|
|
|
|
}
|
2013-06-15 09:42:58 +00:00
|
|
|
me->totselect = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_validate(Mesh *me)
|
|
|
|
{
|
|
|
|
MSelect *mselect_src, *mselect_dst;
|
|
|
|
int i_src, i_dst;
|
|
|
|
|
|
|
|
if (me->totselect == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mselect_src = me->mselect;
|
2018-01-14 22:14:20 +01:00
|
|
|
mselect_dst = MEM_malloc_arrayN((me->totselect), sizeof(MSelect), "Mesh selection history");
|
2013-06-15 09:42:58 +00:00
|
|
|
|
|
|
|
for (i_src = 0, i_dst = 0; i_src < me->totselect; i_src++) {
|
|
|
|
int index = mselect_src[i_src].index;
|
|
|
|
switch (mselect_src[i_src].type) {
|
|
|
|
case ME_VSEL:
|
|
|
|
{
|
|
|
|
if (me->mvert[index].flag & SELECT) {
|
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
i_dst++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ME_ESEL:
|
|
|
|
{
|
|
|
|
if (me->medge[index].flag & SELECT) {
|
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
i_dst++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ME_FSEL:
|
|
|
|
{
|
|
|
|
if (me->mpoly[index].flag & SELECT) {
|
|
|
|
mselect_dst[i_dst] = mselect_src[i_src];
|
|
|
|
i_dst++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
BLI_assert(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(mselect_src);
|
|
|
|
|
|
|
|
if (i_dst == 0) {
|
|
|
|
MEM_freeN(mselect_dst);
|
|
|
|
mselect_dst = NULL;
|
|
|
|
}
|
|
|
|
else if (i_dst != me->totselect) {
|
|
|
|
mselect_dst = MEM_reallocN(mselect_dst, sizeof(MSelect) * i_dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
me->totselect = i_dst;
|
|
|
|
me->mselect = mselect_dst;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the index within me->mselect, or -1
|
|
|
|
*/
|
|
|
|
int BKE_mesh_mselect_find(Mesh *me, int index, int type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-07-20 01:30:29 +10:00
|
|
|
BLI_assert(ELEM(type, ME_VSEL, ME_ESEL, ME_FSEL));
|
2013-06-15 09:42:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < me->totselect; i++) {
|
|
|
|
if ((me->mselect[i].index == index) &&
|
2014-01-12 22:05:24 +11:00
|
|
|
(me->mselect[i].type == type))
|
2013-06-15 09:42:58 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return The index of the active element.
|
|
|
|
*/
|
|
|
|
int BKE_mesh_mselect_active_get(Mesh *me, int type)
|
|
|
|
{
|
2014-07-20 01:30:29 +10:00
|
|
|
BLI_assert(ELEM(type, ME_VSEL, ME_ESEL, ME_FSEL));
|
2013-06-15 09:42:58 +00:00
|
|
|
|
2013-06-30 22:04:03 +00:00
|
|
|
if (me->totselect) {
|
2013-06-15 09:42:58 +00:00
|
|
|
if (me->mselect[me->totselect - 1].type == type) {
|
|
|
|
return me->mselect[me->totselect - 1].index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_mselect_active_set(Mesh *me, int index, int type)
|
|
|
|
{
|
|
|
|
const int msel_index = BKE_mesh_mselect_find(me, index, type);
|
|
|
|
|
|
|
|
if (msel_index == -1) {
|
|
|
|
/* add to the end */
|
|
|
|
me->mselect = MEM_reallocN(me->mselect, sizeof(MSelect) * (me->totselect + 1));
|
|
|
|
me->mselect[me->totselect].index = index;
|
|
|
|
me->mselect[me->totselect].type = type;
|
|
|
|
me->totselect++;
|
|
|
|
}
|
|
|
|
else if (msel_index != me->totselect - 1) {
|
|
|
|
/* move to the end */
|
|
|
|
SWAP(MSelect, me->mselect[msel_index], me->mselect[me->totselect - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_assert((me->mselect[me->totselect - 1].index == index) &&
|
|
|
|
(me->mselect[me->totselect - 1].type == type));
|
|
|
|
}
|
Bake API - bpy.ops.object.bake()
New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api.
The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result.
The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit
Python Operator:
----------------
The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake
bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False)
Note: external save mode is currently disabled.
Supported Features:
------------------
* Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture.
* Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object.
* Cage Extrusion - distance to use for the inward ray cast when using selected to active
* Custom Cage - object to use as cage (instead of the lowpoly object).
* Normal swizzle - change the axis that gets mapped to RGB
* Normal space - save as tangent or object normal spaces
Supported Passes:
-----------------
Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled"
Development Notes for External Engines:
---------------------------------------
(read them in bake_api.c)
* For a complete implementation example look at the Cycles Bake commit (next).
Review: D421
Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge
Normal map pipeline "consulting" by Andy Davies (metalliandy)
Original design by Brecht van Lommel.
The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
|
|
|
|
2018-05-08 11:33:31 +02:00
|
|
|
|
|
|
|
void BKE_mesh_apply_vert_coords(Mesh *mesh, float (*vertCoords)[3])
|
|
|
|
{
|
|
|
|
MVert *vert;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* this will just return the pointer if it wasn't a referenced layer */
|
|
|
|
vert = CustomData_duplicate_referenced_layer(&mesh->vdata, CD_MVERT, mesh->totvert);
|
|
|
|
mesh->mvert = vert;
|
|
|
|
|
|
|
|
for (i = 0; i < mesh->totvert; ++i, ++vert)
|
|
|
|
copy_v3_v3(vert->co, vertCoords[i]);
|
|
|
|
|
|
|
|
mesh->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
|
|
|
|
}
|
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
/**
|
|
|
|
* Compute 'split' (aka loop, or per face corner's) normals.
|
|
|
|
*
|
|
|
|
* \param r_lnors_spacearr Allows to get computed loop normal space array. That data, among other things,
|
|
|
|
* contains 'smooth fan' info, useful e.g. to split geometry along sharp edges...
|
|
|
|
*/
|
2017-02-22 09:40:46 +01:00
|
|
|
void BKE_mesh_calc_normals_split_ex(Mesh *mesh, MLoopNorSpaceArray *r_lnors_spacearr)
|
2015-03-20 12:41:31 +05:00
|
|
|
{
|
|
|
|
float (*r_loopnors)[3];
|
|
|
|
float (*polynors)[3];
|
|
|
|
short (*clnors)[2] = NULL;
|
|
|
|
bool free_polynors = false;
|
|
|
|
|
2017-09-04 21:02:46 +02:00
|
|
|
/* Note that we enforce computing clnors when the clnor space array is requested by caller here.
|
|
|
|
* However, we obviously only use the autosmooth angle threshold only in case autosmooth is enabled. */
|
|
|
|
const bool use_split_normals = (r_lnors_spacearr != NULL) || ((mesh->flag & ME_AUTOSMOOTH) != 0);
|
|
|
|
const float split_angle = (mesh->flag & ME_AUTOSMOOTH) != 0 ? mesh->smoothresh : (float)M_PI;
|
|
|
|
|
2015-03-20 12:41:31 +05:00
|
|
|
if (CustomData_has_layer(&mesh->ldata, CD_NORMAL)) {
|
|
|
|
r_loopnors = CustomData_get_layer(&mesh->ldata, CD_NORMAL);
|
|
|
|
memset(r_loopnors, 0, sizeof(float[3]) * mesh->totloop);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r_loopnors = CustomData_add_layer(&mesh->ldata, CD_NORMAL, CD_CALLOC, NULL, mesh->totloop);
|
|
|
|
CustomData_set_layer_flag(&mesh->ldata, CD_NORMAL, CD_FLAG_TEMPORARY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* may be NULL */
|
|
|
|
clnors = CustomData_get_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL);
|
|
|
|
|
|
|
|
if (CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
|
|
|
|
/* This assume that layer is always up to date, not sure this is the case (esp. in Edit mode?)... */
|
|
|
|
polynors = CustomData_get_layer(&mesh->pdata, CD_NORMAL);
|
|
|
|
free_polynors = false;
|
|
|
|
}
|
|
|
|
else {
|
2018-01-14 22:14:20 +01:00
|
|
|
polynors = MEM_malloc_arrayN(mesh->totpoly, sizeof(float[3]), __func__);
|
2015-10-12 20:12:55 +02:00
|
|
|
BKE_mesh_calc_normals_poly(
|
|
|
|
mesh->mvert, NULL, mesh->totvert,
|
|
|
|
mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly, polynors, false);
|
2015-03-20 12:41:31 +05:00
|
|
|
free_polynors = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BKE_mesh_normals_loop_split(
|
|
|
|
mesh->mvert, mesh->totvert, mesh->medge, mesh->totedge,
|
|
|
|
mesh->mloop, r_loopnors, mesh->totloop, mesh->mpoly, (const float (*)[3])polynors, mesh->totpoly,
|
2017-09-04 21:02:46 +02:00
|
|
|
use_split_normals, split_angle, r_lnors_spacearr, clnors, NULL);
|
2015-03-20 12:41:31 +05:00
|
|
|
|
|
|
|
if (free_polynors) {
|
|
|
|
MEM_freeN(polynors);
|
|
|
|
}
|
2018-05-08 11:56:31 +02:00
|
|
|
|
|
|
|
mesh->runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
|
2015-03-20 12:41:31 +05:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
void BKE_mesh_calc_normals_split(Mesh *mesh)
|
2015-03-20 12:41:31 +05:00
|
|
|
{
|
2017-02-22 09:40:46 +01:00
|
|
|
BKE_mesh_calc_normals_split_ex(mesh, NULL);
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Split faces helper functions. */
|
|
|
|
|
|
|
|
typedef struct SplitFaceNewVert {
|
|
|
|
struct SplitFaceNewVert *next;
|
|
|
|
int new_index;
|
|
|
|
int orig_index;
|
|
|
|
float *vnor;
|
|
|
|
} SplitFaceNewVert;
|
|
|
|
|
|
|
|
typedef struct SplitFaceNewEdge {
|
|
|
|
struct SplitFaceNewEdge *next;
|
|
|
|
int new_index;
|
|
|
|
int orig_index;
|
|
|
|
int v1;
|
|
|
|
int v2;
|
|
|
|
} SplitFaceNewEdge;
|
|
|
|
|
|
|
|
/* Detect needed new vertices, and update accordingly loops' vertex indices.
|
|
|
|
* WARNING! Leaves mesh in invalid state. */
|
|
|
|
static int split_faces_prepare_new_verts(
|
2017-08-30 16:42:53 +02:00
|
|
|
const Mesh *mesh, MLoopNorSpaceArray *lnors_spacearr, SplitFaceNewVert **new_verts, MemArena *memarena)
|
2017-02-22 09:40:46 +01:00
|
|
|
{
|
2017-08-30 16:42:53 +02:00
|
|
|
/* This is now mandatory, trying to do the job in simple way without that data is doomed to fail, even when only
|
|
|
|
* dealing with smooth/flat faces one can find cases that no simple algorithm can handle properly. */
|
|
|
|
BLI_assert(lnors_spacearr != NULL);
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
const int loops_len = mesh->totloop;
|
|
|
|
int verts_len = mesh->totvert;
|
2017-02-22 09:40:46 +01:00
|
|
|
MVert *mvert = mesh->mvert;
|
|
|
|
MLoop *mloop = mesh->mloop;
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
BLI_bitmap *verts_used = BLI_BITMAP_NEW(verts_len, __func__);
|
|
|
|
BLI_bitmap *done_loops = BLI_BITMAP_NEW(loops_len, __func__);
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
MLoop *ml = mloop;
|
|
|
|
MLoopNorSpace **lnor_space = lnors_spacearr->lspacearr;
|
|
|
|
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
BLI_assert(lnors_spacearr->data_type == MLNOR_SPACEARR_LOOP_INDEX);
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
for (int loop_idx = 0; loop_idx < loops_len; loop_idx++, ml++, lnor_space++) {
|
2017-08-30 16:42:53 +02:00
|
|
|
if (!BLI_BITMAP_TEST(done_loops, loop_idx)) {
|
|
|
|
const int vert_idx = ml->v;
|
|
|
|
const bool vert_used = BLI_BITMAP_TEST_BOOL(verts_used, vert_idx);
|
|
|
|
/* If vert is already used by another smooth fan, we need a new vert for this one. */
|
2018-05-08 17:15:23 +02:00
|
|
|
const int new_vert_idx = vert_used ? verts_len++ : vert_idx;
|
2017-08-30 16:42:53 +02:00
|
|
|
|
|
|
|
BLI_assert(*lnor_space);
|
|
|
|
|
Refactor: Merge non-functional-change part of 'edit normals' 2017 GSoC.
This merges changes in internals, runtime-only of existing custom
normals code, which make sense as of themselves, and will make diff of
soc branch easier/lighter to review.
In the details, it mostly changes two things:
* Now, smooth fans (aka MLoopNorSpaceArray) can store either loop
indices, or pointers to BMLoop themselves. This makes sense since in
BMesh, it's relatively easy to get index from a BMElement, but nearly
impracticable to go the other way around.
* First change enforces another, now we cannot rely anymore on `loops`
being NULL in MLoopNorSpace to detect single-loop fans, so we instead
store that info in a new flag.
Again, these are expected to be totally non-functional changes.
2018-03-01 16:54:21 +01:00
|
|
|
if ((*lnor_space)->flags & MLNOR_SPACE_IS_SINGLE) {
|
|
|
|
/* Single loop in this fan... */
|
|
|
|
BLI_assert(GET_INT_FROM_POINTER((*lnor_space)->loops) == loop_idx);
|
|
|
|
BLI_BITMAP_ENABLE(done_loops, loop_idx);
|
|
|
|
if (vert_used) {
|
|
|
|
ml->v = new_vert_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-08-30 16:42:53 +02:00
|
|
|
for (LinkNode *lnode = (*lnor_space)->loops; lnode; lnode = lnode->next) {
|
|
|
|
const int ml_fan_idx = GET_INT_FROM_POINTER(lnode->link);
|
|
|
|
BLI_BITMAP_ENABLE(done_loops, ml_fan_idx);
|
2017-02-22 09:40:46 +01:00
|
|
|
if (vert_used) {
|
2017-08-30 16:42:53 +02:00
|
|
|
mloop[ml_fan_idx].v = new_vert_idx;
|
2017-02-22 09:40:46 +01:00
|
|
|
}
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
2017-02-15 13:56:50 +01:00
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
if (!vert_used) {
|
|
|
|
BLI_BITMAP_ENABLE(verts_used, vert_idx);
|
|
|
|
/* We need to update that vertex's normal here, we won't go over it again. */
|
|
|
|
/* This is important! *DO NOT* set vnor to final computed lnor, vnor should always be defined to
|
|
|
|
* 'automatic normal' value computed from its polys, not some custom normal.
|
|
|
|
* Fortunately, that's the loop normal space's 'lnor' reference vector. ;) */
|
|
|
|
normal_float_to_short_v3(mvert[vert_idx].no, (*lnor_space)->vec_lnor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Add new vert to list. */
|
|
|
|
SplitFaceNewVert *new_vert = BLI_memarena_alloc(memarena, sizeof(*new_vert));
|
|
|
|
new_vert->orig_index = vert_idx;
|
|
|
|
new_vert->new_index = new_vert_idx;
|
|
|
|
new_vert->vnor = (*lnor_space)->vec_lnor; /* See note above. */
|
|
|
|
new_vert->next = *new_verts;
|
|
|
|
*new_verts = new_vert;
|
2017-02-15 13:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
MEM_freeN(done_loops);
|
2017-02-22 09:40:46 +01:00
|
|
|
MEM_freeN(verts_used);
|
|
|
|
|
2018-05-08 17:15:23 +02:00
|
|
|
return verts_len - mesh->totvert;
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Detect needed new edges, and update accordingly loops' edge indices.
|
|
|
|
* WARNING! Leaves mesh in invalid state. */
|
|
|
|
static int split_faces_prepare_new_edges(
|
|
|
|
const Mesh *mesh, SplitFaceNewEdge **new_edges, MemArena *memarena)
|
2015-03-20 12:41:31 +05:00
|
|
|
{
|
2017-02-15 23:09:31 +01:00
|
|
|
const int num_polys = mesh->totpoly;
|
2017-02-22 09:40:46 +01:00
|
|
|
int num_edges = mesh->totedge;
|
|
|
|
MEdge *medge = mesh->medge;
|
|
|
|
MLoop *mloop = mesh->mloop;
|
2017-02-15 23:09:31 +01:00
|
|
|
const MPoly *mpoly = mesh->mpoly;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
BLI_bitmap *edges_used = BLI_BITMAP_NEW(num_edges, __func__);
|
|
|
|
EdgeHash *edges_hash = BLI_edgehash_new_ex(__func__, num_edges);
|
|
|
|
|
|
|
|
const MPoly *mp = mpoly;
|
|
|
|
for (int poly_idx = 0; poly_idx < num_polys; poly_idx++, mp++) {
|
|
|
|
MLoop *ml_prev = &mloop[mp->loopstart + mp->totloop - 1];
|
|
|
|
MLoop *ml = &mloop[mp->loopstart];
|
|
|
|
for (int loop_idx = 0; loop_idx < mp->totloop; loop_idx++, ml++) {
|
|
|
|
void **eval;
|
|
|
|
if (!BLI_edgehash_ensure_p(edges_hash, ml_prev->v, ml->v, &eval)) {
|
2017-03-02 17:22:03 +01:00
|
|
|
const int edge_idx = ml_prev->e;
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* That edge has not been encountered yet, define it. */
|
2017-03-02 17:22:03 +01:00
|
|
|
if (BLI_BITMAP_TEST(edges_used, edge_idx)) {
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Original edge has already been used, we need to define a new one. */
|
2017-03-02 17:22:03 +01:00
|
|
|
const int new_edge_idx = num_edges++;
|
|
|
|
*eval = SET_INT_IN_POINTER(new_edge_idx);
|
|
|
|
ml_prev->e = new_edge_idx;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
SplitFaceNewEdge *new_edge = BLI_memarena_alloc(memarena, sizeof(*new_edge));
|
2017-03-02 17:22:03 +01:00
|
|
|
new_edge->orig_index = edge_idx;
|
|
|
|
new_edge->new_index = new_edge_idx;
|
2017-02-22 09:40:46 +01:00
|
|
|
new_edge->v1 = ml_prev->v;
|
|
|
|
new_edge->v2 = ml->v;
|
|
|
|
new_edge->next = *new_edges;
|
|
|
|
*new_edges = new_edge;
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-22 09:40:46 +01:00
|
|
|
/* We can re-use original edge. */
|
|
|
|
medge[edge_idx].v1 = ml_prev->v;
|
|
|
|
medge[edge_idx].v2 = ml->v;
|
|
|
|
*eval = SET_INT_IN_POINTER(edge_idx);
|
|
|
|
BLI_BITMAP_ENABLE(edges_used, edge_idx);
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
else {
|
|
|
|
/* Edge already known, just update loop's edge index. */
|
|
|
|
ml_prev->e = GET_INT_FROM_POINTER(*eval);
|
|
|
|
}
|
|
|
|
|
|
|
|
ml_prev = ml;
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
MEM_freeN(edges_used);
|
|
|
|
BLI_edgehash_free(edges_hash, NULL);
|
|
|
|
|
|
|
|
return num_edges - mesh->totedge;
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Perform actual split of vertices. */
|
|
|
|
static void split_faces_split_new_verts(
|
|
|
|
Mesh *mesh, SplitFaceNewVert *new_verts, const int num_new_verts)
|
2017-02-15 23:09:31 +01:00
|
|
|
{
|
2018-05-08 17:15:23 +02:00
|
|
|
const int verts_len = mesh->totvert - num_new_verts;
|
2015-03-20 12:41:31 +05:00
|
|
|
MVert *mvert = mesh->mvert;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
/* Remember new_verts is a single linklist, so its items are in reversed order... */
|
|
|
|
MVert *new_mv = &mvert[mesh->totvert - 1];
|
2018-05-08 17:15:23 +02:00
|
|
|
for (int i = mesh->totvert - 1; i >= verts_len ; i--, new_mv--, new_verts = new_verts->next) {
|
2017-02-22 09:40:46 +01:00
|
|
|
BLI_assert(new_verts->new_index == i);
|
2017-03-02 17:22:03 +01:00
|
|
|
BLI_assert(new_verts->new_index != new_verts->orig_index);
|
2017-02-22 09:40:46 +01:00
|
|
|
CustomData_copy_data(&mesh->vdata, &mesh->vdata, new_verts->orig_index, i, 1);
|
|
|
|
if (new_verts->vnor) {
|
|
|
|
normal_float_to_short_v3(new_mv->no, new_verts->vnor);
|
2017-02-15 13:56:50 +01:00
|
|
|
}
|
2017-02-15 20:59:55 +01:00
|
|
|
}
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Perform actual split of edges. */
|
|
|
|
static void split_faces_split_new_edges(
|
|
|
|
Mesh *mesh, SplitFaceNewEdge *new_edges, const int num_new_edges)
|
2017-02-15 23:09:31 +01:00
|
|
|
{
|
|
|
|
const int num_edges = mesh->totedge - num_new_edges;
|
2015-03-20 12:41:31 +05:00
|
|
|
MEdge *medge = mesh->medge;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
/* Remember new_edges is a single linklist, so its items are in reversed order... */
|
|
|
|
MEdge *new_med = &medge[mesh->totedge - 1];
|
|
|
|
for (int i = mesh->totedge - 1; i >= num_edges ; i--, new_med--, new_edges = new_edges->next) {
|
|
|
|
BLI_assert(new_edges->new_index == i);
|
2017-03-02 17:22:03 +01:00
|
|
|
BLI_assert(new_edges->new_index != new_edges->orig_index);
|
2017-02-22 09:40:46 +01:00
|
|
|
CustomData_copy_data(&mesh->edata, &mesh->edata, new_edges->orig_index, i, 1);
|
|
|
|
new_med->v1 = new_edges->v1;
|
|
|
|
new_med->v2 = new_edges->v2;
|
2015-03-20 12:41:31 +05:00
|
|
|
}
|
2017-02-15 23:09:31 +01:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Split faces based on the edge angle and loop normals.
|
2017-02-15 23:09:31 +01:00
|
|
|
* Matches behavior of face splitting in render engines.
|
2017-02-20 11:56:02 +01:00
|
|
|
*
|
|
|
|
* NOTE: Will leave CD_NORMAL loop data layer which is
|
|
|
|
* used by render engines to set shading up.
|
2017-02-15 23:09:31 +01:00
|
|
|
*/
|
2017-02-22 10:53:28 +01:00
|
|
|
void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals)
|
2017-02-15 23:09:31 +01:00
|
|
|
{
|
|
|
|
const int num_polys = mesh->totpoly;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-02-15 23:09:31 +01:00
|
|
|
if (num_polys == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-20 12:41:31 +05:00
|
|
|
BKE_mesh_tessface_clear(mesh);
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
MLoopNorSpaceArray lnors_spacearr = {NULL};
|
|
|
|
/* Compute loop normals and loop normal spaces (a.k.a. smooth fans of faces around vertices). */
|
|
|
|
BKE_mesh_calc_normals_split_ex(mesh, &lnors_spacearr);
|
|
|
|
/* Stealing memarena from loop normals space array. */
|
|
|
|
MemArena *memarena = lnors_spacearr.mem;
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
SplitFaceNewVert *new_verts = NULL;
|
|
|
|
SplitFaceNewEdge *new_edges = NULL;
|
|
|
|
|
|
|
|
/* Detect loop normal spaces (a.k.a. smooth fans) that will need a new vert. */
|
2017-08-30 16:42:53 +02:00
|
|
|
const int num_new_verts = split_faces_prepare_new_verts(mesh, &lnors_spacearr, &new_verts, memarena);
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
if (num_new_verts > 0) {
|
|
|
|
/* Reminder: beyond this point, there is no way out, mesh is in invalid state (due to early-reassignment of
|
|
|
|
* loops' vertex and edge indices to new, to-be-created split ones). */
|
|
|
|
|
|
|
|
const int num_new_edges = split_faces_prepare_new_edges(mesh, &new_edges, memarena);
|
2017-03-13 16:11:48 +01:00
|
|
|
/* We can have to split a vertex without having to add a single new edge... */
|
|
|
|
const bool do_edges = (num_new_edges > 0);
|
2017-02-22 09:40:46 +01:00
|
|
|
|
|
|
|
/* Reallocate all vert and edge related data. */
|
|
|
|
mesh->totvert += num_new_verts;
|
|
|
|
CustomData_realloc(&mesh->vdata, mesh->totvert);
|
2017-03-13 16:11:48 +01:00
|
|
|
if (do_edges) {
|
2017-08-30 16:42:53 +02:00
|
|
|
mesh->totedge += num_new_edges;
|
2017-03-13 16:11:48 +01:00
|
|
|
CustomData_realloc(&mesh->edata, mesh->totedge);
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
/* Update pointers to a newly allocated memory. */
|
|
|
|
BKE_mesh_update_customdata_pointers(mesh, false);
|
|
|
|
|
|
|
|
/* Perform actual split of vertices and edges. */
|
|
|
|
split_faces_split_new_verts(mesh, new_verts, num_new_verts);
|
2017-03-13 16:11:48 +01:00
|
|
|
if (do_edges) {
|
|
|
|
split_faces_split_new_edges(mesh, new_edges, num_new_edges);
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: after this point mesh is expected to be valid again. */
|
|
|
|
|
|
|
|
/* CD_NORMAL is expected to be temporary only. */
|
2017-02-22 10:53:28 +01:00
|
|
|
if (free_loop_normals) {
|
|
|
|
CustomData_free_layers(&mesh->ldata, CD_NORMAL, mesh->totloop);
|
|
|
|
}
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-08-30 16:42:53 +02:00
|
|
|
/* Also frees new_verts/edges temp data, since we used its memarena to allocate them. */
|
|
|
|
BKE_lnor_spacearr_free(&lnors_spacearr);
|
2017-02-22 09:40:46 +01:00
|
|
|
|
2017-02-15 13:56:50 +01:00
|
|
|
#ifdef VALIDATE_MESH
|
|
|
|
BKE_mesh_validate(mesh, true, true);
|
|
|
|
#endif
|
2015-03-20 12:41:31 +05:00
|
|
|
}
|
|
|
|
|
2015-05-12 13:57:11 +05:00
|
|
|
/* **** Depsgraph evaluation **** */
|
|
|
|
|
2018-05-02 11:46:56 +02:00
|
|
|
void BKE_mesh_eval_geometry(Depsgraph *depsgraph,
|
2015-05-12 13:57:11 +05:00
|
|
|
Mesh *mesh)
|
|
|
|
{
|
2018-05-02 11:46:56 +02:00
|
|
|
DEG_debug_print_eval(depsgraph, __func__, mesh->id.name, mesh);
|
2015-05-12 13:57:11 +05:00
|
|
|
if (mesh->bb == NULL || (mesh->bb->flag & BOUNDBOX_DIRTY)) {
|
|
|
|
BKE_mesh_texspace_calc(mesh);
|
|
|
|
}
|
|
|
|
}
|