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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2009-10-22 23:22:05 +00:00
|
|
|
#include "DNA_scene_types.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "DNA_material_types.h"
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
#include "DNA_key_types.h"
|
2004-03-20 22:55:42 +00:00
|
|
|
#include "DNA_meshdata_types.h"
|
2006-09-12 14:05:26 +00:00
|
|
|
#include "DNA_ipo_types.h"
|
2010-10-27 02:22:55 +00:00
|
|
|
#include "DNA_customdata_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_blenlib.h"
|
2011-10-27 05:34:39 +00:00
|
|
|
#include "BLI_bpath.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_editVert.h"
|
|
|
|
#include "BLI_math.h"
|
|
|
|
#include "BLI_edgehash.h"
|
2011-02-27 06:19:40 +00:00
|
|
|
#include "BLI_scanfill.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2009-12-28 03:45:24 +00:00
|
|
|
#include "BKE_animsys.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BKE_main.h"
|
2010-10-27 02:22:55 +00:00
|
|
|
#include "BKE_customdata.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_displist.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_key.h"
|
|
|
|
/* these 2 are only used by conversion functions */
|
|
|
|
#include "BKE_curve.h"
|
|
|
|
/* -- */
|
|
|
|
#include "BKE_object.h"
|
2009-05-23 03:24:15 +00:00
|
|
|
#include "BKE_tessmesh.h"
|
2010-10-27 02:22:55 +00:00
|
|
|
#include "BLI_edgehash.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
#include "BLI_blenlib.h"
|
|
|
|
#include "BLI_editVert.h"
|
2009-11-10 20:43:45 +00:00
|
|
|
#include "BLI_math.h"
|
2009-09-12 06:47:59 +00:00
|
|
|
#include "BLI_cellalloc.h"
|
2009-09-17 23:05:33 +00:00
|
|
|
#include "BLI_array.h"
|
2009-10-22 23:22:05 +00:00
|
|
|
#include "BLI_edgehash.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-05-26 04:17:47 +00:00
|
|
|
#include "bmesh.h"
|
|
|
|
|
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,
|
|
|
|
MESHCMP_CDLAYERS_MISMATCH,
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
return "Vertex Group Mismatch";
|
|
|
|
case MESHCMP_DVERT_TOTGROUPMISMATCH:
|
|
|
|
return "Vertex Doesn't Belong To Same Number Of Groups";
|
|
|
|
case MESHCMP_LOOPCOLMISMATCH:
|
|
|
|
return "Vertex Color Mismatch";
|
|
|
|
case MESHCMP_LOOPUVMISMATCH:
|
|
|
|
return "UV Mismatch";
|
|
|
|
case MESHCMP_LOOPMISMATCH:
|
|
|
|
return "Loop Mismatch";
|
|
|
|
case MESHCMP_POLYVERTMISMATCH:
|
|
|
|
return "Loop Vert Mismatch In Poly Test";
|
|
|
|
case MESHCMP_POLYMISMATCH:
|
|
|
|
return "Loop Vert Mismatch";
|
|
|
|
case MESHCMP_EDGEUNKNOWN:
|
|
|
|
return "Edge Mismatch";
|
|
|
|
case MESHCMP_VERTCOMISMATCH:
|
|
|
|
return "Vertex Coordinate Mismatch";
|
|
|
|
case MESHCMP_CDLAYERS_MISMATCH:
|
2011-11-16 17:09:41 +00:00
|
|
|
return "CustomData Layer Count Mismatch";
|
2010-10-27 02:22:55 +00:00
|
|
|
default:
|
|
|
|
return "Mesh Comparison Code Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*thresh is threshold for comparing vertices, uvs, vertex colors,
|
|
|
|
weights, etc.*/
|
2011-05-11 02:14:43 +00:00
|
|
|
static int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2, float thresh)
|
2010-10-27 02:22:55 +00:00
|
|
|
{
|
|
|
|
CustomDataLayer *l1, *l2;
|
|
|
|
int i, i1=0, i2=0, tot, j;
|
|
|
|
|
|
|
|
for (i=0; i<c1->totlayer; i++) {
|
|
|
|
if (ELEM7(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
|
|
|
|
i1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<c2->totlayer; i++) {
|
|
|
|
if (ELEM7(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
|
|
|
|
i2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i1 != i2)
|
|
|
|
return MESHCMP_CDLAYERS_MISMATCH;
|
|
|
|
|
|
|
|
l1 = c1->layers; l2 = c2->layers;
|
|
|
|
tot = i1;
|
|
|
|
i1 = 0; i2 = 0;
|
|
|
|
for (i=0; i < tot; i++) {
|
|
|
|
while (i1 < c1->totlayer && !ELEM7(l1->type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
|
|
|
|
i1++, l1++;
|
|
|
|
|
|
|
|
while (i2 < c2->totlayer && !ELEM7(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY,
|
|
|
|
CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
|
|
|
|
i2++, l2++;
|
|
|
|
|
|
|
|
if (l1->type == CD_MVERT) {
|
|
|
|
MVert *v1 = l1->data;
|
|
|
|
MVert *v2 = l2->data;
|
|
|
|
int vtot = m1->totvert;
|
|
|
|
|
|
|
|
for (j=0; j<vtot; j++, v1++, v2++) {
|
|
|
|
if (len_v3v3(v1->co, v2->co) > thresh)
|
|
|
|
return MESHCMP_VERTCOMISMATCH;
|
|
|
|
/*I don't care about normals, let's just do coodinates*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*we're order-agnostic for edges here*/
|
|
|
|
if (l1->type == CD_MEDGE) {
|
|
|
|
MEdge *e1 = l1->data;
|
|
|
|
MEdge *e2 = l2->data;
|
|
|
|
EdgeHash *eh = BLI_edgehash_new();
|
|
|
|
int etot = m1->totedge;
|
|
|
|
|
|
|
|
for (j=0; j<etot; j++, e1++) {
|
|
|
|
BLI_edgehash_insert(eh, e1->v1, e1->v2, e1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j=0; j<etot; j++, e2++) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (j=0; j<ptot; j++, p1++, p2++) {
|
|
|
|
MLoop *lp1, *lp2;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (p1->totloop != p2->totloop)
|
|
|
|
return MESHCMP_POLYMISMATCH;
|
|
|
|
|
|
|
|
lp1 = m1->mloop + p1->loopstart;
|
|
|
|
lp2 = m2->mloop + p2->loopstart;
|
|
|
|
|
|
|
|
for (k=0; k<p1->totloop; k++, lp1++, lp2++) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (j=0; j<ltot; j++, lp1++, lp2++) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (j=0; j<ltot; j++, lp1++, lp2++) {
|
|
|
|
if (len_v2v2(lp1->uv, lp2->uv) > thresh)
|
|
|
|
return MESHCMP_LOOPUVMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l1->type == CD_MLOOPCOL) {
|
|
|
|
MLoopCol *lp1 = l1->data;
|
|
|
|
MLoopCol *lp2 = l2->data;
|
|
|
|
int ltot = m1->totloop;
|
|
|
|
|
|
|
|
for (j=0; j<ltot; j++, lp1++, lp2++) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (j=0; j<dvtot; j++, dv1++, dv2++) {
|
|
|
|
int k;
|
|
|
|
MDeformWeight *dw1 = dv1->dw, *dw2=dv2->dw;
|
|
|
|
|
|
|
|
if (dv1->totweight != dv2->totweight)
|
|
|
|
return MESHCMP_DVERT_TOTGROUPMISMATCH;
|
|
|
|
|
|
|
|
for (k=0; k<dv1->totweight; k++, dw1++, dw2++) {
|
|
|
|
if (dw1->def_nr != dw2->def_nr)
|
|
|
|
return MESHCMP_DVERT_GROUPMISMATCH;
|
|
|
|
if (ABS(dw1->weight - dw2->weight) > thresh)
|
|
|
|
return MESHCMP_DVERT_WEIGHTMISMATCH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 23:36:56 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-10-27 02:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*used for testing. returns an error string the two meshes don't match*/
|
2011-05-09 05:09:07 +00:00
|
|
|
const char *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";
|
|
|
|
|
|
|
|
if (me1->totloop !=me2->totloop)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
static void mesh_ensure_tesselation_customdata(Mesh *me)
|
|
|
|
{
|
2011-12-06 09:28:25 +00:00
|
|
|
const int tottex_original = CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY);
|
|
|
|
const int totcol_original = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2011-12-06 09:28:25 +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);
|
|
|
|
|
|
|
|
if (tottex_tessface != tottex_original ||
|
|
|
|
totcol_tessface != totcol_original )
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
|
|
|
CustomData_free(&me->fdata, me->totface);
|
2009-09-02 20:26:12 +00:00
|
|
|
|
2009-09-02 20:36:48 +00:00
|
|
|
me->mface = NULL;
|
2009-09-02 20:40:17 +00:00
|
|
|
me->mtface = NULL;
|
2009-09-02 20:36:48 +00:00
|
|
|
me->mcol = NULL;
|
2009-09-02 20:26:12 +00:00
|
|
|
me->totface = 0;
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
memset(&me->fdata, 0, sizeof(&me->fdata));
|
|
|
|
|
|
|
|
CustomData_from_bmeshpoly(&me->fdata, &me->pdata, &me->ldata, me->totface);
|
2011-12-06 09:28:25 +00:00
|
|
|
|
|
|
|
/* note: this warning may be un-called for if we are inirializing the mesh for the
|
|
|
|
* 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! Tesselation uvs or vcol data got out of sync, "
|
|
|
|
"had to reset!\n CD_MTFACE: %d != CD_MTEXPOLY: %d || CD_MCOL: %d != CD_MLOOPCOL: %d\n",
|
|
|
|
__func__, tottex_tessface, tottex_original, totcol_tessface, totcol_original);
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* we dont want to store memory for tessface when its only used for older
|
|
|
|
* versions of the mesh. - campbell*/
|
|
|
|
static void mesh_update_linked_customdata(Mesh *me, const short do_ensure_tess_cd)
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
|
|
|
if (me->edit_btmesh)
|
|
|
|
BMEdit_UpdateLinkedCustomData(me->edit_btmesh);
|
|
|
|
|
2011-12-06 09:28:25 +00:00
|
|
|
if (do_ensure_tess_cd) {
|
|
|
|
mesh_ensure_tesselation_customdata(me);
|
|
|
|
}
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2011-12-01 09:49:27 +00:00
|
|
|
CustomData_bmesh_update_active_layers(&me->fdata, &me->pdata, &me->ldata);
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
2009-05-23 03:24:15 +00:00
|
|
|
|
2011-12-06 09:28:25 +00:00
|
|
|
void mesh_update_customdata_pointers(Mesh *me, const short 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->msticky = CustomData_get_layer(&me->vdata, CD_MSTICKY);
|
|
|
|
|
|
|
|
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->mtpoly = CustomData_get_layer(&me->pdata, CD_MTEXPOLY);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-09-12 14:05:26 +00:00
|
|
|
/* Note: unlinking is called when me->id.us is 0, question remains how
|
|
|
|
* much unlinking of Library data in Mesh should be done... probably
|
|
|
|
* we need a more generic method, like the expand() functions in
|
|
|
|
* readfile.c */
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void unlink_mesh(Mesh *me)
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(me==NULL) return;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
for(a=0; a<me->totcol; a++) {
|
|
|
|
if(me->mat[a]) me->mat[a]->id.us--;
|
2011-02-13 10:52:18 +00:00
|
|
|
me->mat[a]= NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2006-09-12 14:05:26 +00:00
|
|
|
|
|
|
|
if(me->key) {
|
2011-12-04 06:39:35 +00:00
|
|
|
me->key->id.us--;
|
2006-09-12 14:05:26 +00:00
|
|
|
}
|
2011-02-13 10:52:18 +00:00
|
|
|
me->key= NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(me->texcomesh) me->texcomesh= NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* do not free mesh itself */
|
2009-09-10 06:08:52 +00:00
|
|
|
void free_mesh(Mesh *me, int unlink)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-09-10 06:08:52 +00:00
|
|
|
if (unlink)
|
|
|
|
unlink_mesh(me);
|
2002-10-12 11:37:38 +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
|
|
|
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
|
|
|
|
2009-12-28 00:52:31 +00:00
|
|
|
if(me->adt) {
|
|
|
|
BKE_free_animdata(&me->id);
|
|
|
|
me->adt= NULL;
|
|
|
|
}
|
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
if(me->mat) MEM_freeN(me->mat);
|
2005-07-18 18:28:16 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if(me->bb) MEM_freeN(me->bb);
|
2006-05-10 21:08:38 +00:00
|
|
|
if(me->mselect) MEM_freeN(me->mselect);
|
2009-05-16 16:18:08 +00:00
|
|
|
if(me->edit_btmesh) MEM_freeN(me->edit_btmesh);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void copy_dverts(MDeformVert *dst, MDeformVert *src, int copycount)
|
|
|
|
{
|
|
|
|
/* Assumes dst is already set up */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!src || !dst)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy (dst, src, copycount * sizeof(MDeformVert));
|
|
|
|
|
|
|
|
for (i=0; i<copycount; i++){
|
|
|
|
if (src[i].dw){
|
2009-09-10 01:46:52 +00:00
|
|
|
dst[i].dw = BLI_cellalloc_calloc (sizeof(MDeformWeight)*src[i].totweight, "copy_deformWeight");
|
2002-10-12 11:37:38 +00:00
|
|
|
memcpy (dst[i].dw, src[i].dw, sizeof (MDeformWeight)*src[i].totweight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
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
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void free_dverts(MDeformVert *dvert, int totvert)
|
|
|
|
{
|
|
|
|
/* Instead of freeing the verts directly,
|
|
|
|
call this function to delete any special
|
|
|
|
vert data */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!dvert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Free any special data from the verts */
|
|
|
|
for (i=0; i<totvert; i++){
|
2009-09-15 15:32:09 +00:00
|
|
|
if (dvert[i].dw) BLI_cellalloc_free (dvert[i].dw);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
MEM_freeN (dvert);
|
|
|
|
}
|
|
|
|
|
2010-11-17 09:45:45 +00:00
|
|
|
Mesh *add_mesh(const char *name)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
Mesh *me;
|
|
|
|
|
2007-03-11 16:25:17 +00:00
|
|
|
me= alloc_libblock(&G.main->mesh, ID_ME, name);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
me->size[0]= me->size[1]= me->size[2]= 1.0;
|
|
|
|
me->smoothresh= 30;
|
2004-03-22 22:02:18 +00:00
|
|
|
me->texflag= AUTOSPACE;
|
2002-10-12 11:37:38 +00:00
|
|
|
me->flag= ME_TWOSIDED;
|
|
|
|
me->bb= unit_boundbox();
|
2009-01-31 13:30:56 +00:00
|
|
|
me->drawflag= ME_DRAWEDGES|ME_DRAWFACES|ME_DRAWCREASES;
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh *copy_mesh(Mesh *me)
|
|
|
|
{
|
|
|
|
Mesh *men;
|
2006-12-12 21:29:09 +00:00
|
|
|
MTFace *tface;
|
2009-05-26 04:17:47 +00:00
|
|
|
MTexPoly *txface;
|
2006-12-12 21:29:09 +00:00
|
|
|
int a, i;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-11-07 04:36:37 +00:00
|
|
|
men= copy_libblock(&me->id);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
men->mat= MEM_dupallocN(me->mat);
|
|
|
|
for(a=0; a<men->totcol; a++) {
|
|
|
|
id_us_plus((ID *)men->mat[a]);
|
|
|
|
}
|
|
|
|
id_us_plus((ID *)men->texcomesh);
|
|
|
|
|
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_copy(&me->vdata, &men->vdata, CD_MASK_MESH, CD_DUPLICATE, men->totvert);
|
|
|
|
CustomData_copy(&me->edata, &men->edata, CD_MASK_MESH, CD_DUPLICATE, men->totedge);
|
|
|
|
CustomData_copy(&me->fdata, &men->fdata, CD_MASK_MESH, CD_DUPLICATE, men->totface);
|
2009-05-26 04:17:47 +00:00
|
|
|
CustomData_copy(&me->ldata, &men->ldata, CD_MASK_MESH, CD_DUPLICATE, men->totloop);
|
|
|
|
CustomData_copy(&me->pdata, &men->pdata, CD_MASK_MESH, CD_DUPLICATE, men->totpoly);
|
2011-12-06 09:28:25 +00:00
|
|
|
mesh_update_customdata_pointers(men, TRUE);
|
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
|
|
|
|
2006-12-12 21:29:09 +00:00
|
|
|
/* ensure indirect linked data becomes lib-extern */
|
|
|
|
for(i=0; i<me->fdata.totlayer; i++) {
|
|
|
|
if(me->fdata.layers[i].type == CD_MTFACE) {
|
|
|
|
tface= (MTFace*)me->fdata.layers[i].data;
|
|
|
|
|
|
|
|
for(a=0; a<me->totface; a++, tface++)
|
|
|
|
if(tface->tpage)
|
|
|
|
id_lib_extern((ID*)tface->tpage);
|
|
|
|
}
|
2006-10-16 11:31:09 +00:00
|
|
|
}
|
2006-12-01 18:41:25 +00:00
|
|
|
|
2009-05-26 04:17:47 +00:00
|
|
|
for(i=0; i<me->pdata.totlayer; i++) {
|
|
|
|
if(me->pdata.layers[i].type == CD_MTEXPOLY) {
|
|
|
|
txface= (MTexPoly*)me->pdata.layers[i].data;
|
|
|
|
|
|
|
|
for(a=0; a<me->totpoly; a++, txface++)
|
|
|
|
if(txface->tpage)
|
|
|
|
id_lib_extern((ID*)txface->tpage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
men->mselect= NULL;
|
2011-05-08 23:43:18 +00:00
|
|
|
men->edit_btmesh= NULL;
|
2006-05-10 17:22:49 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
men->bb= MEM_dupallocN(men->bb);
|
|
|
|
|
|
|
|
men->key= copy_key(me->key);
|
|
|
|
if(men->key) men->key->from= (ID *)men;
|
2006-08-20 15:22:56 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return men;
|
|
|
|
}
|
|
|
|
|
2011-09-12 02:23:30 +00:00
|
|
|
BMesh *BKE_mesh_to_bmesh(Mesh *me, Object *ob)
|
2009-05-26 04:17:47 +00:00
|
|
|
{
|
|
|
|
BMesh *bm;
|
|
|
|
int allocsize[4] = {512,512,2048,512};
|
|
|
|
|
2011-03-29 05:48:18 +00:00
|
|
|
bm = BM_Make_Mesh(ob, allocsize);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
2011-09-12 02:23:30 +00:00
|
|
|
BMO_CallOpf(bm, "mesh_to_bmesh mesh=%p object=%p set_shapekey=%i", me, ob, 1);
|
2009-05-26 04:17:47 +00:00
|
|
|
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2011-10-23 17:52:20 +00:00
|
|
|
static void expand_local_mesh(Mesh *me)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2011-10-23 17:52:20 +00:00
|
|
|
id_lib_extern((ID *)me->texcomesh);
|
|
|
|
|
|
|
|
if(me->mtface) {
|
|
|
|
int a, i;
|
|
|
|
|
2011-10-27 09:42:03 +00:00
|
|
|
for(i=0; i<me->pdata.totlayer; i++) {
|
|
|
|
if(me->pdata.layers[i].type == CD_MTEXPOLY) {
|
|
|
|
MTexPoly *txface= (MTexPoly*)me->fdata.layers[i].data;
|
|
|
|
|
|
|
|
for(a=0; a<me->totpoly; a++, txface++) {
|
|
|
|
/* special case: ima always local immediately */
|
|
|
|
if(txface->tpage) {
|
|
|
|
if(txface->tpage) {
|
|
|
|
id_lib_extern((ID *)txface->tpage);
|
|
|
|
}
|
2009-05-26 04:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 17:52:20 +00:00
|
|
|
for(i=0; i<me->fdata.totlayer; i++) {
|
|
|
|
if(me->fdata.layers[i].type == CD_MTFACE) {
|
2011-10-27 09:42:03 +00:00
|
|
|
MTFace *tface= (MTFace*)me->fdata.layers[i].data;
|
2011-10-23 17:52:20 +00:00
|
|
|
|
|
|
|
for(a=0; a<me->totface; a++, tface++) {
|
2011-10-27 09:42:03 +00:00
|
|
|
/* special case: ima always local immediately */
|
2011-10-23 17:52:20 +00:00
|
|
|
if(tface->tpage) {
|
2011-10-27 09:42:03 +00:00
|
|
|
if(tface->tpage) {
|
|
|
|
id_lib_extern((ID *)tface->tpage);
|
|
|
|
}
|
2006-12-12 21:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 07:17:21 +00:00
|
|
|
|
|
|
|
if(me->mat) {
|
|
|
|
extern_local_matarar(me->mat, me->totcol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void make_local_mesh(Mesh *me)
|
|
|
|
{
|
2010-08-13 14:23:44 +00:00
|
|
|
Main *bmain= G.main;
|
2002-10-12 11:37:38 +00:00
|
|
|
Object *ob;
|
2011-12-28 22:47:55 +00:00
|
|
|
int is_local= FALSE, is_lib= FALSE;
|
2003-04-26 13:07:59 +00:00
|
|
|
|
|
|
|
/* - only lib users: do nothing
|
2011-04-26 07:17:21 +00:00
|
|
|
* - only local users: set flag
|
|
|
|
* - mixed: make copy
|
|
|
|
*/
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(me->id.lib==NULL) return;
|
2002-10-12 11:37:38 +00:00
|
|
|
if(me->id.us==1) {
|
2011-12-28 22:47:55 +00:00
|
|
|
id_clear_lib_data(bmain, &me->id);
|
2011-10-23 17:52:20 +00:00
|
|
|
expand_local_mesh(me);
|
2002-10-12 11:37:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-04-26 07:17:21 +00:00
|
|
|
|
2011-12-28 22:47:55 +00:00
|
|
|
for(ob= bmain->object.first; ob && ELEM(0, is_lib, is_local); ob= ob->id.next) {
|
2011-04-26 07:17:21 +00:00
|
|
|
if(me == ob->data) {
|
2011-12-28 22:47:55 +00:00
|
|
|
if(ob->id.lib) is_lib= TRUE;
|
|
|
|
else is_local= TRUE;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-26 07:17:21 +00:00
|
|
|
|
2011-12-28 22:47:55 +00:00
|
|
|
if(is_local && is_lib == FALSE) {
|
|
|
|
id_clear_lib_data(bmain, &me->id);
|
2011-10-23 17:52:20 +00:00
|
|
|
expand_local_mesh(me);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2011-12-28 22:47:55 +00:00
|
|
|
else if(is_local && is_lib) {
|
2011-11-30 00:32:13 +00:00
|
|
|
Mesh *me_new= copy_mesh(me);
|
|
|
|
me_new->id.us= 0;
|
2011-04-26 07:17:21 +00:00
|
|
|
|
2011-12-28 22:47:55 +00:00
|
|
|
|
|
|
|
/* Remap paths of new ID using old library as base. */
|
2011-11-30 00:32:13 +00:00
|
|
|
BKE_id_lib_local_paths(bmain, me->id.lib, &me_new->id);
|
2011-10-27 05:34:39 +00:00
|
|
|
|
2011-04-26 07:17:21 +00:00
|
|
|
for(ob= bmain->object.first; ob; ob= ob->id.next) {
|
|
|
|
if(me == ob->data) {
|
2011-02-13 10:52:18 +00:00
|
|
|
if(ob->id.lib==NULL) {
|
2011-11-30 00:32:13 +00:00
|
|
|
set_mesh(ob, me_new);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void boundbox_mesh(Mesh *me, float *loc, float *size)
|
|
|
|
{
|
|
|
|
BoundBox *bb;
|
|
|
|
float min[3], max[3];
|
|
|
|
float mloc[3], msize[3];
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(me->bb==NULL) me->bb= MEM_callocN(sizeof(BoundBox), "boundbox");
|
2002-10-12 11:37:38 +00:00
|
|
|
bb= me->bb;
|
|
|
|
|
|
|
|
if (!loc) loc= mloc;
|
|
|
|
if (!size) size= msize;
|
|
|
|
|
2010-08-03 00:56:43 +00:00
|
|
|
INIT_MINMAX(min, max);
|
|
|
|
if(!minmax_mesh(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
|
|
|
}
|
|
|
|
|
2010-08-01 11:00:36 +00:00
|
|
|
mid_v3_v3v3(loc, min, max);
|
2005-07-18 17:33:51 +00:00
|
|
|
|
|
|
|
size[0]= (max[0]-min[0])/2.0f;
|
|
|
|
size[1]= (max[1]-min[1])/2.0f;
|
|
|
|
size[2]= (max[2]-min[2])/2.0f;
|
|
|
|
|
|
|
|
boundbox_set_from_min_max(bb, min, max);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tex_space_mesh(Mesh *me)
|
|
|
|
{
|
2009-11-01 00:06:53 +00:00
|
|
|
float loc[3], size[3];
|
2002-10-12 11:37:38 +00:00
|
|
|
int a;
|
|
|
|
|
|
|
|
boundbox_mesh(me, loc, size);
|
|
|
|
|
2004-03-22 22:02:18 +00:00
|
|
|
if(me->texflag & AUTOSPACE) {
|
2005-07-14 21:57:18 +00:00
|
|
|
for (a=0; a<3; a++) {
|
2011-03-28 04:22:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:50:23 +00:00
|
|
|
BoundBox *mesh_get_bb(Object *ob)
|
2005-07-14 18:04:27 +00:00
|
|
|
{
|
2007-12-05 21:50:23 +00:00
|
|
|
Mesh *me= ob->data;
|
|
|
|
|
|
|
|
if(ob->bb)
|
|
|
|
return ob->bb;
|
|
|
|
|
|
|
|
if (!me->bb)
|
2005-07-14 18:04:27 +00:00
|
|
|
tex_space_mesh(me);
|
|
|
|
|
|
|
|
return me->bb;
|
|
|
|
}
|
|
|
|
|
2005-07-14 21:57:18 +00:00
|
|
|
void mesh_get_texspace(Mesh *me, float *loc_r, float *rot_r, float *size_r)
|
|
|
|
{
|
|
|
|
if (!me->bb) {
|
|
|
|
tex_space_mesh(me);
|
|
|
|
}
|
|
|
|
|
2011-11-20 16:21:13 +00:00
|
|
|
if (loc_r) copy_v3_v3(loc_r, me->loc);
|
|
|
|
if (rot_r) copy_v3_v3(rot_r, me->rot);
|
|
|
|
if (size_r) copy_v3_v3(size_r, me->size);
|
2005-07-14 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 12:40:54 +00:00
|
|
|
float *get_mesh_orco_verts(Object *ob)
|
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;
|
|
|
|
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 */
|
2009-11-01 00:06:53 +00:00
|
|
|
vcos = MEM_callocN(sizeof(*vcos)*me->totvert, "orco mesh");
|
|
|
|
mvert = tme->mvert;
|
|
|
|
totvert = MIN2(tme->totvert, me->totvert);
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-12-05 12:40:54 +00:00
|
|
|
return (float*)vcos;
|
|
|
|
}
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2007-12-11 20:02:21 +00:00
|
|
|
void transform_mesh_orco_verts(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
|
|
|
|
2005-08-13 16:39:22 +00:00
|
|
|
mesh_get_texspace(me->texcomesh?me->texcomesh:me, loc, NULL, size);
|
2005-07-18 19:58:23 +00:00
|
|
|
|
2007-12-11 20:02:21 +00:00
|
|
|
if(invert) {
|
|
|
|
for(a=0; a<totvert; a++) {
|
|
|
|
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 {
|
|
|
|
for(a=0; a<totvert; a++) {
|
|
|
|
float *co = orco[a];
|
|
|
|
co[0] = (co[0]-loc[0])/size[0];
|
|
|
|
co[1] = (co[1]-loc[1])/size[1];
|
|
|
|
co[2] = (co[2]-loc[2])/size[2];
|
|
|
|
}
|
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.
|
|
|
|
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 */
|
2010-09-23 14:29:51 +00:00
|
|
|
if((mface->v3 || nr==4) && mface->v3==mface->v4) {
|
2002-10-12 11:37:38 +00:00
|
|
|
mface->v4= 0;
|
|
|
|
nr--;
|
|
|
|
}
|
2011-02-09 03:49:59 +00:00
|
|
|
if((mface->v2 || mface->v4) && mface->v2==mface->v3) {
|
2002-10-12 11:37:38 +00:00
|
|
|
mface->v3= mface->v4;
|
|
|
|
mface->v4= 0;
|
|
|
|
nr--;
|
|
|
|
}
|
|
|
|
if(mface->v1==mface->v2) {
|
|
|
|
mface->v2= mface->v3;
|
|
|
|
mface->v3= mface->v4;
|
|
|
|
mface->v4= 0;
|
|
|
|
nr--;
|
|
|
|
}
|
|
|
|
|
2011-02-09 04:45:53 +00:00
|
|
|
/* check corrupt cases, bowtie geometry, cant handle these because edge data wont exist so just return 0 */
|
|
|
|
if(nr==3) {
|
|
|
|
if(
|
|
|
|
/* real edges */
|
|
|
|
mface->v1==mface->v2 ||
|
|
|
|
mface->v2==mface->v3 ||
|
|
|
|
mface->v3==mface->v1
|
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(nr==4) {
|
|
|
|
if(
|
|
|
|
/* 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 ||
|
|
|
|
mface->v2==mface->v4
|
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* prevent a zero at wrong index location */
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
if(nr==3) {
|
2002-10-12 11:37:38 +00:00
|
|
|
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
|
|
|
|
2006-11-20 19:03:37 +00:00
|
|
|
if(fdata)
|
|
|
|
CustomData_swap(fdata, mfindex, corner_indices);
|
2002-10-12 11:37:38 +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
|
|
|
|
2006-11-20 19:03:37 +00:00
|
|
|
if(fdata)
|
|
|
|
CustomData_swap(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
|
|
|
}
|
|
|
|
|
|
|
|
Mesh *get_mesh(Object *ob)
|
|
|
|
{
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(ob==NULL) return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void set_mesh(Object *ob, Mesh *me)
|
|
|
|
{
|
2011-02-13 10:52:18 +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
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
if(ob==NULL) return;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if(ob->type==OB_MESH) {
|
|
|
|
old= ob->data;
|
2009-10-22 23:22:05 +00:00
|
|
|
if (old)
|
|
|
|
old->id.us--;
|
2002-10-12 11:37:38 +00:00
|
|
|
ob->data= me;
|
|
|
|
id_us_plus((ID *)me);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_object_materials((ID *)me);
|
2010-09-09 00:14:51 +00:00
|
|
|
|
|
|
|
test_object_modifiers(ob);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
/* ************** make edges in a Mesh, for outside of editmode */
|
|
|
|
|
|
|
|
struct edgesort {
|
2011-12-27 03:54:23 +00:00
|
|
|
unsigned int v1, v2;
|
2005-09-22 17:00:58 +00:00
|
|
|
short is_loose, is_draw;
|
2004-07-08 20:38:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* edges have to be added with lowest index first for sorting */
|
2011-12-27 03:54:23 +00:00
|
|
|
static void to_edgesort(struct edgesort *ed,
|
|
|
|
unsigned int v1, unsigned int v2,
|
|
|
|
short is_loose, short is_draw)
|
2004-07-08 20:38:27 +00:00
|
|
|
{
|
|
|
|
if(v1<v2) {
|
|
|
|
ed->v1= v1; ed->v2= v2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ed->v1= v2; ed->v2= v1;
|
|
|
|
}
|
2005-08-20 09:16:09 +00:00
|
|
|
ed->is_loose= is_loose;
|
2005-09-22 17:00:58 +00:00
|
|
|
ed->is_draw= is_draw;
|
2004-07-08 20:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vergedgesort(const void *v1, const void *v2)
|
|
|
|
{
|
|
|
|
const struct edgesort *x1=v1, *x2=v2;
|
|
|
|
|
|
|
|
if( x1->v1 > x2->v1) return 1;
|
|
|
|
else if( x1->v1 < x2->v1) return -1;
|
|
|
|
else if( x1->v2 > x2->v2) return 1;
|
|
|
|
else if( x1->v2 < x2->v2) return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-05 16:47:52 +00:00
|
|
|
static void mfaces_strip_loose(MFace *mface, int *totface)
|
|
|
|
{
|
|
|
|
int a,b;
|
|
|
|
|
|
|
|
for (a=b=0; a<*totface; a++) {
|
|
|
|
if (mface[a].v3) {
|
|
|
|
if (a!=b) {
|
|
|
|
memcpy(&mface[b],&mface[a],sizeof(mface[b]));
|
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*totface= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create edges based on known verts and faces */
|
2011-04-15 01:19:13 +00:00
|
|
|
static void make_edges_mdata(MVert *UNUSED(allvert), MFace *allface, MLoop *allloop,
|
2011-05-09 14:32:55 +00:00
|
|
|
MPoly *allpoly, int UNUSED(totvert), int totface, int UNUSED(totloop), int totpoly,
|
2010-03-05 16:47:52 +00:00
|
|
|
int old, MEdge **alledge, int *_totedge)
|
2004-07-08 20:38:27 +00:00
|
|
|
{
|
2011-04-15 01:19:13 +00:00
|
|
|
MPoly *mpoly;
|
|
|
|
MLoop *mloop;
|
2004-07-08 20:38:27 +00:00
|
|
|
MFace *mface;
|
|
|
|
MEdge *medge;
|
2011-04-15 01:19:13 +00:00
|
|
|
EdgeHash *hash = BLI_edgehash_new();
|
2004-07-08 20:38:27 +00:00
|
|
|
struct edgesort *edsort, *ed;
|
2011-04-15 01:19:13 +00:00
|
|
|
int a, b, totedge=0, final=0;
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
/* we put all edges in array, sort them, and detect doubles that way */
|
2010-03-05 16:47:52 +00:00
|
|
|
|
|
|
|
for(a= totface, mface= allface; a>0; a--, mface++) {
|
2004-07-08 20:38:27 +00:00
|
|
|
if(mface->v4) totedge+=4;
|
|
|
|
else if(mface->v3) totedge+=3;
|
|
|
|
else totedge+=1;
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
- added data arguments to deformer modifiers, in case someone wants
to write one that is based on geometry (and not just vertex position)
- added editmode versions of modifier deform/apply calls and flag
to tag modifiers that support editmode
- added isFinalCalc param to applyModifier, basically a switch to let
subsurf know if it is calc'ng orco or not (so it can deal with cache
appropriately). This is kinda hacky and perhaps I can come up with
a better solution (its also a waste to do a complete subdivide just
to get vertex locations).
- changed ccgsubsurf to not preallocate hash's to be approximately correct
size... this was probably not a big performance savings but means that
the order of faces returned by the iterator can vary after the first
call, this messes up orco calculation so dropped for time being.
- minor bug fix, meshes with only key didn't get vertex normals correctly
calc'd
- updated editmesh derivedmesh to support auxiliary locations
- changed mesh_calc_modifiers to alloc deformVerts on demand
- added editmesh_calc_modifiers for calculating editmesh cage and final
derivedmesh's
- bug fix, update shadedisplist to always calc colors (even if totvert==0)
- changed load_editMesh and make_edge to build me->medge even if totedge==0
(incremental subsurf checks this)
todo: add drawFacesTex for ccgderivedmesh
So, modifiers in editmode are back (which means auto-mirror
in edit mode works now) although still not finished. Currently
no cage is computed, the cage is always the base mesh (in
other words, Optimal edge style editing is off), and the final
mesh currently includes all modifiers that work in edit mode
(including lattice and curve). At some point there will be toggles
for which modifiers affect the final/cage editmode derivedmesh's.
Also, very nice new feature is that incremental subsurf in object
mode returns a ccgderivedmesh object instead of copying to a new
displistmesh. This can make a *huge* speed difference, and is very
nice for working with deformed armatures (esp. with only small
per frame changes).
2005-07-22 07:37:15 +00:00
|
|
|
if(totedge==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
|
|
|
/* flag that mesh has edges */
|
2010-03-05 16:47:52 +00:00
|
|
|
(*alledge)= MEM_callocN(0, "make mesh edges");
|
|
|
|
(*_totedge) = 0;
|
- added data arguments to deformer modifiers, in case someone wants
to write one that is based on geometry (and not just vertex position)
- added editmode versions of modifier deform/apply calls and flag
to tag modifiers that support editmode
- added isFinalCalc param to applyModifier, basically a switch to let
subsurf know if it is calc'ng orco or not (so it can deal with cache
appropriately). This is kinda hacky and perhaps I can come up with
a better solution (its also a waste to do a complete subdivide just
to get vertex locations).
- changed ccgsubsurf to not preallocate hash's to be approximately correct
size... this was probably not a big performance savings but means that
the order of faces returned by the iterator can vary after the first
call, this messes up orco calculation so dropped for time being.
- minor bug fix, meshes with only key didn't get vertex normals correctly
calc'd
- updated editmesh derivedmesh to support auxiliary locations
- changed mesh_calc_modifiers to alloc deformVerts on demand
- added editmesh_calc_modifiers for calculating editmesh cage and final
derivedmesh's
- bug fix, update shadedisplist to always calc colors (even if totvert==0)
- changed load_editMesh and make_edge to build me->medge even if totedge==0
(incremental subsurf checks this)
todo: add drawFacesTex for ccgderivedmesh
So, modifiers in editmode are back (which means auto-mirror
in edit mode works now) although still not finished. Currently
no cage is computed, the cage is always the base mesh (in
other words, Optimal edge style editing is off), and the final
mesh currently includes all modifiers that work in edit mode
(including lattice and curve). At some point there will be toggles
for which modifiers affect the final/cage editmode derivedmesh's.
Also, very nice new feature is that incremental subsurf in object
mode returns a ccgderivedmesh object instead of copying to a new
displistmesh. This can make a *huge* speed difference, and is very
nice for working with deformed armatures (esp. with only small
per frame changes).
2005-07-22 07:37:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
ed= edsort= MEM_mallocN(totedge*sizeof(struct edgesort), "edgesort");
|
2010-03-05 16:47:52 +00:00
|
|
|
|
|
|
|
for(a= totface, mface= allface; a>0; a--, mface++) {
|
2005-09-22 17:00:58 +00:00
|
|
|
to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2);
|
2004-07-08 20:38:27 +00:00
|
|
|
if(mface->v4) {
|
2005-09-22 17:00:58 +00:00
|
|
|
to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
|
|
|
|
to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4);
|
|
|
|
to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1);
|
2004-07-08 20:38:27 +00:00
|
|
|
}
|
|
|
|
else if(mface->v3) {
|
2005-09-22 17:00:58 +00:00
|
|
|
to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
|
|
|
|
to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1);
|
2004-07-08 20:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
qsort(edsort, totedge, sizeof(struct edgesort), vergedgesort);
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
/* count final amount */
|
|
|
|
for(a=totedge, ed=edsort; a>1; a--, ed++) {
|
|
|
|
/* edge is unique when it differs from next edge, or is last */
|
|
|
|
if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) final++;
|
|
|
|
}
|
|
|
|
final++;
|
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
|
|
|
|
2010-03-05 16:47:52 +00:00
|
|
|
(*alledge)= medge= MEM_callocN(sizeof (MEdge) * final, "make_edges mdge");
|
|
|
|
(*_totedge)= final;
|
|
|
|
|
2004-07-08 20:38:27 +00:00
|
|
|
for(a=totedge, ed=edsort; a>1; a--, ed++) {
|
|
|
|
/* edge is unique when it differs from next edge, or is last */
|
|
|
|
if(ed->v1 != (ed+1)->v1 || ed->v2 != (ed+1)->v2) {
|
|
|
|
medge->v1= ed->v1;
|
|
|
|
medge->v2= ed->v2;
|
2005-09-22 17:52:41 +00:00
|
|
|
if(old==0 || ed->is_draw) medge->flag= ME_EDGEDRAW|ME_EDGERENDER;
|
2005-08-20 09:16:09 +00:00
|
|
|
if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
|
2010-03-30 22:43:43 +00:00
|
|
|
|
|
|
|
/* order is swapped so extruding this edge as a surface wont flip face normals
|
|
|
|
* with cyclic curves */
|
|
|
|
if(ed->v1+1 != ed->v2) {
|
2011-12-27 03:54:23 +00:00
|
|
|
SWAP(unsigned int, medge->v1, medge->v2);
|
2010-03-30 22:43:43 +00:00
|
|
|
}
|
2004-07-08 20:38:27 +00:00
|
|
|
medge++;
|
|
|
|
}
|
2005-09-22 17:00:58 +00:00
|
|
|
else {
|
|
|
|
/* equal edge, we merge the drawflag */
|
|
|
|
(ed+1)->is_draw |= ed->is_draw;
|
|
|
|
}
|
2004-07-08 20:38:27 +00:00
|
|
|
}
|
|
|
|
/* last edge */
|
|
|
|
medge->v1= ed->v1;
|
|
|
|
medge->v2= ed->v2;
|
2005-08-21 19:01:30 +00:00
|
|
|
medge->flag= ME_EDGEDRAW;
|
2005-08-20 09:16:09 +00:00
|
|
|
if(ed->is_loose) medge->flag|= ME_LOOSEEDGE;
|
2005-08-12 21:55:50 +00:00
|
|
|
medge->flag |= ME_EDGERENDER;
|
2004-07-08 20:38:27 +00:00
|
|
|
|
|
|
|
MEM_freeN(edsort);
|
2011-04-15 01:19:13 +00:00
|
|
|
|
|
|
|
/*set edge members of mloops*/
|
|
|
|
medge= *alledge;
|
|
|
|
for (a=0; a<*_totedge; a++, medge++) {
|
|
|
|
BLI_edgehash_insert(hash, medge->v1, medge->v2, SET_INT_IN_POINTER(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
mpoly = allpoly;
|
|
|
|
for (a=0; a<totpoly; a++, mpoly++) {
|
|
|
|
mloop = allloop + mpoly->loopstart;
|
|
|
|
for (b=0; b<mpoly->totloop; b++) {
|
|
|
|
int v1, v2;
|
|
|
|
|
|
|
|
v1 = mloop[b].v;
|
2011-12-28 07:10:27 +00:00
|
|
|
v2 = ME_POLY_LOOP_NEXT(mloop, mpoly, b)->v;
|
2011-04-15 01:19:13 +00:00
|
|
|
mloop[b].e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(hash, v1, v2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_edgehash_free(hash, NULL);
|
2010-03-05 16:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_edges(Mesh *me, int old)
|
|
|
|
{
|
|
|
|
MEdge *medge;
|
|
|
|
int totedge=0;
|
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
make_edges_mdata(me->mvert, me->mface, me->mloop, me->mpoly, me->totvert, me->totface, me->totloop, me->totpoly, old, &medge, &totedge);
|
2010-03-05 16:47:52 +00:00
|
|
|
if(totedge==0) {
|
|
|
|
/* flag that mesh has edges */
|
|
|
|
me->medge = medge;
|
|
|
|
me->totedge = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge);
|
|
|
|
me->medge= medge;
|
|
|
|
me->totedge= totedge;
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
|
|
|
|
mesh_strip_loose_faces(me);
|
2004-07-08 20:38:27 +00:00
|
|
|
}
|
|
|
|
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
void mesh_strip_loose_faces(Mesh *me)
|
|
|
|
{
|
|
|
|
int a,b;
|
|
|
|
|
|
|
|
for (a=b=0; a<me->totface; a++) {
|
2005-08-31 04:04:28 +00:00
|
|
|
if (me->mface[a].v3) {
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
if (a!=b) {
|
|
|
|
memcpy(&me->mface[b],&me->mface[a],sizeof(me->mface[b]));
|
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_copy_data(&me->fdata, &me->fdata, a, b, 1);
|
|
|
|
CustomData_free_elem(&me->fdata, a, 1);
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
me->totface = b;
|
|
|
|
}
|
2004-07-08 20:38:27 +00:00
|
|
|
|
2011-02-09 15:13:20 +00:00
|
|
|
void mesh_strip_loose_edges(Mesh *me)
|
|
|
|
{
|
|
|
|
int a,b;
|
|
|
|
|
|
|
|
for (a=b=0; a<me->totedge; a++) {
|
2011-02-10 09:29:31 +00:00
|
|
|
if (me->medge[a].v1!=me->medge[a].v2) {
|
2011-02-09 15:13:20 +00:00
|
|
|
if (a!=b) {
|
|
|
|
memcpy(&me->medge[b],&me->medge[a],sizeof(me->medge[b]));
|
|
|
|
CustomData_copy_data(&me->edata, &me->edata, a, b, 1);
|
|
|
|
CustomData_free_elem(&me->edata, a, 1);
|
|
|
|
}
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
me->totedge = b;
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
void mball_to_mesh(ListBase *lb, Mesh *me)
|
|
|
|
{
|
|
|
|
DispList *dl;
|
|
|
|
MVert *mvert;
|
|
|
|
MFace *mface;
|
|
|
|
float *nors, *verts;
|
|
|
|
int a, *index;
|
|
|
|
|
|
|
|
dl= lb->first;
|
2011-02-13 10:52:18 +00:00
|
|
|
if(dl==NULL) return;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if(dl->type==DL_INDEX4) {
|
|
|
|
me->totvert= dl->nr;
|
|
|
|
me->totface= dl->parts;
|
|
|
|
|
2006-12-12 21:29:09 +00:00
|
|
|
mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, dl->nr);
|
|
|
|
mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, dl->parts);
|
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= mvert;
|
|
|
|
me->mface= mface;
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
a= dl->nr;
|
|
|
|
nors= dl->nors;
|
|
|
|
verts= dl->verts;
|
|
|
|
while(a--) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(mvert->co, verts);
|
2011-03-28 02:34:55 +00:00
|
|
|
normal_float_to_short_v3(mvert->no, nors);
|
2002-10-12 11:37:38 +00:00
|
|
|
mvert++;
|
|
|
|
nors+= 3;
|
|
|
|
verts+= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
a= dl->parts;
|
|
|
|
index= dl->index;
|
|
|
|
while(a--) {
|
|
|
|
mface->v1= index[0];
|
|
|
|
mface->v2= index[1];
|
|
|
|
mface->v3= index[2];
|
|
|
|
mface->v4= index[3];
|
2006-11-23 00:26:39 +00:00
|
|
|
mface->flag= ME_SMOOTH;
|
|
|
|
|
2008-02-18 20:00:42 +00:00
|
|
|
test_index_face(mface, NULL, 0, (mface->v3==mface->v4)? 3: 4);
|
2006-11-23 00:26:39 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
mface++;
|
|
|
|
index+= 4;
|
|
|
|
}
|
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
|
|
|
|
2012-01-19 17:51:52 +00:00
|
|
|
/* BMESH_TODO - why is this converting from MFaces to MPoly's then tesselating?
|
|
|
|
* should just make mpolys */
|
|
|
|
|
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
|
|
|
make_edges(me, 0); // all edges
|
2011-04-15 01:19:13 +00:00
|
|
|
convert_mfaces_to_mpolys(me);
|
2011-11-13 15:13:59 +00:00
|
|
|
|
2012-01-19 17:51:52 +00:00
|
|
|
me->totface = mesh_recalcTesselation(&me->fdata, &me->ldata, &me->pdata,
|
|
|
|
me->mvert,
|
|
|
|
me->totface, me->totloop, me->totpoly,
|
|
|
|
TRUE);
|
2011-11-13 15:13:59 +00:00
|
|
|
|
2011-12-06 09:28:25 +00:00
|
|
|
mesh_update_customdata_pointers(me, TRUE);
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 16:47:52 +00:00
|
|
|
/* Initialize mverts, medges and, faces for converting nurbs to mesh and derived mesh */
|
|
|
|
/* return non-zero on error */
|
2010-03-08 13:49:13 +00:00
|
|
|
int nurbs_to_mdata(Object *ob, MVert **allvert, int *totvert,
|
2011-04-15 01:19:13 +00:00
|
|
|
MEdge **alledge, int *totedge, MFace **allface, MLoop **allloop, MPoly **allpoly,
|
|
|
|
int *totface, int *totloop, int *totpoly)
|
2010-03-08 13:49:13 +00:00
|
|
|
{
|
2011-01-05 10:40:38 +00:00
|
|
|
return nurbs_to_mdata_customdb(ob, &ob->disp,
|
2011-04-15 01:19:13 +00:00
|
|
|
allvert, totvert, alledge, totedge, allface, allloop, allpoly, totface, totloop, totpoly);
|
2010-03-08 13:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize mverts, medges and, faces for converting nurbs to mesh and derived mesh */
|
|
|
|
/* use specified dispbase */
|
|
|
|
int nurbs_to_mdata_customdb(Object *ob, ListBase *dispbase, MVert **allvert, int *_totvert,
|
2011-04-15 01:19:13 +00:00
|
|
|
MEdge **alledge, int *_totedge, MFace **allface, MLoop **allloop, MPoly **allpoly,
|
|
|
|
int *_totface, int *_totloop, int *_totpoly)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
DispList *dl;
|
|
|
|
Curve *cu;
|
|
|
|
MVert *mvert;
|
|
|
|
MFace *mface;
|
2011-04-15 01:19:13 +00:00
|
|
|
MPoly *mpoly;
|
|
|
|
MLoop *mloop;
|
2002-10-12 11:37:38 +00:00
|
|
|
float *data;
|
|
|
|
int a, b, ofs, vertcount, startvert, totvert=0, totvlak=0;
|
|
|
|
int p1, p2, p3, p4, *index;
|
2010-11-06 06:22:25 +00:00
|
|
|
int conv_polys= 0;
|
2011-04-15 01:19:13 +00:00
|
|
|
int i, j;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
cu= ob->data;
|
|
|
|
|
2010-11-06 06:22:25 +00:00
|
|
|
conv_polys|= cu->flag & CU_3D; /* 2d polys are filled with DL_INDEX3 displists */
|
|
|
|
conv_polys|= ob->type == OB_SURF; /* surf polys are never filled */
|
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* count */
|
2010-03-08 13:49:13 +00:00
|
|
|
dl= dispbase->first;
|
2002-10-12 11:37:38 +00:00
|
|
|
while(dl) {
|
|
|
|
if(dl->type==DL_SEGM) {
|
|
|
|
totvert+= dl->parts*dl->nr;
|
|
|
|
totvlak+= dl->parts*(dl->nr-1);
|
|
|
|
}
|
|
|
|
else if(dl->type==DL_POLY) {
|
2010-11-06 06:22:25 +00:00
|
|
|
if(conv_polys) {
|
|
|
|
totvert+= dl->parts*dl->nr;
|
|
|
|
totvlak+= dl->parts*dl->nr;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
else if(dl->type==DL_SURF) {
|
|
|
|
totvert+= dl->parts*dl->nr;
|
2005-08-11 22:27:53 +00:00
|
|
|
totvlak+= (dl->parts-1+((dl->flag & DL_CYCL_V)==2))*(dl->nr-1+(dl->flag & DL_CYCL_U));
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
else if(dl->type==DL_INDEX3) {
|
|
|
|
totvert+= dl->nr;
|
|
|
|
totvlak+= dl->parts;
|
|
|
|
}
|
|
|
|
dl= dl->next;
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if(totvert==0) {
|
2007-05-28 16:49:48 +00:00
|
|
|
/* error("can't convert"); */
|
|
|
|
/* Make Sure you check ob->data is a curve */
|
2010-03-05 16:47:52 +00:00
|
|
|
return -1;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 16:47:52 +00:00
|
|
|
*allvert= mvert= MEM_callocN(sizeof (MVert) * totvert, "nurbs_init mvert");
|
2011-04-15 01:19:13 +00:00
|
|
|
*allface= mface= MEM_callocN(sizeof (MFace) * totvlak, "nurbs_init mface");
|
|
|
|
*allloop = mloop = MEM_callocN(sizeof(MLoop) * totvlak * 4, "nurbs_init mloop");
|
|
|
|
*allpoly = mpoly = MEM_callocN(sizeof(MPoly) * totvlak * 4, "nurbs_init mloop");
|
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* verts and faces */
|
2002-10-12 11:37:38 +00:00
|
|
|
vertcount= 0;
|
|
|
|
|
2010-03-08 13:49:13 +00:00
|
|
|
dl= dispbase->first;
|
2002-10-12 11:37:38 +00:00
|
|
|
while(dl) {
|
2009-07-01 13:07:28 +00:00
|
|
|
int smooth= dl->rt & CU_SMOOTH ? 1 : 0;
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if(dl->type==DL_SEGM) {
|
|
|
|
startvert= vertcount;
|
|
|
|
a= dl->parts*dl->nr;
|
|
|
|
data= dl->verts;
|
|
|
|
while(a--) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(mvert->co, data);
|
2002-10-12 11:37:38 +00:00
|
|
|
data+=3;
|
|
|
|
vertcount++;
|
|
|
|
mvert++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(a=0; a<dl->parts; a++) {
|
|
|
|
ofs= a*dl->nr;
|
|
|
|
for(b=1; b<dl->nr; b++) {
|
|
|
|
mface->v1= startvert+ofs+b-1;
|
|
|
|
mface->v2= startvert+ofs+b;
|
2009-07-01 13:07:28 +00:00
|
|
|
if(smooth) mface->flag |= ME_SMOOTH;
|
2002-10-12 11:37:38 +00:00
|
|
|
mface++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(dl->type==DL_POLY) {
|
2010-11-06 06:22:25 +00:00
|
|
|
if(conv_polys) {
|
|
|
|
startvert= vertcount;
|
|
|
|
a= dl->parts*dl->nr;
|
|
|
|
data= dl->verts;
|
|
|
|
while(a--) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(mvert->co, data);
|
2010-11-06 06:22:25 +00:00
|
|
|
data+=3;
|
|
|
|
vertcount++;
|
|
|
|
mvert++;
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2010-11-06 06:22:25 +00:00
|
|
|
for(a=0; a<dl->parts; a++) {
|
|
|
|
ofs= a*dl->nr;
|
|
|
|
for(b=0; b<dl->nr; b++) {
|
|
|
|
mface->v1= startvert+ofs+b;
|
|
|
|
if(b==dl->nr-1) mface->v2= startvert+ofs;
|
|
|
|
else mface->v2= startvert+ofs+b+1;
|
|
|
|
if(smooth) mface->flag |= ME_SMOOTH;
|
|
|
|
mface++;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(dl->type==DL_INDEX3) {
|
|
|
|
startvert= vertcount;
|
|
|
|
a= dl->nr;
|
|
|
|
data= dl->verts;
|
|
|
|
while(a--) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(mvert->co, data);
|
2002-10-12 11:37:38 +00:00
|
|
|
data+=3;
|
|
|
|
vertcount++;
|
|
|
|
mvert++;
|
|
|
|
}
|
|
|
|
|
|
|
|
a= dl->parts;
|
|
|
|
index= dl->index;
|
|
|
|
while(a--) {
|
|
|
|
mface->v1= startvert+index[0];
|
2007-10-23 00:02:29 +00:00
|
|
|
mface->v2= startvert+index[2];
|
|
|
|
mface->v3= startvert+index[1];
|
2002-10-12 11:37:38 +00:00
|
|
|
mface->v4= 0;
|
2011-09-20 06:25:15 +00:00
|
|
|
mface->mat_nr= dl->col;
|
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
|
|
|
test_index_face(mface, NULL, 0, 3);
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2009-07-01 13:07:28 +00:00
|
|
|
if(smooth) mface->flag |= ME_SMOOTH;
|
2002-10-12 11:37:38 +00:00
|
|
|
mface++;
|
|
|
|
index+= 3;
|
|
|
|
}
|
2010-03-05 16:47:52 +00:00
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
else if(dl->type==DL_SURF) {
|
|
|
|
startvert= vertcount;
|
|
|
|
a= dl->parts*dl->nr;
|
|
|
|
data= dl->verts;
|
|
|
|
while(a--) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(mvert->co, data);
|
2002-10-12 11:37:38 +00:00
|
|
|
data+=3;
|
|
|
|
vertcount++;
|
|
|
|
mvert++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(a=0; a<dl->parts; a++) {
|
|
|
|
|
2005-08-11 22:27:53 +00:00
|
|
|
if( (dl->flag & DL_CYCL_V)==0 && a==dl->parts-1) break;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2005-08-11 22:27:53 +00:00
|
|
|
if(dl->flag & DL_CYCL_U) { /* p2 -> p1 -> */
|
2002-10-12 11:37:38 +00:00
|
|
|
p1= startvert+ dl->nr*a; /* p4 -> p3 -> */
|
2003-04-26 13:07:59 +00:00
|
|
|
p2= p1+ dl->nr-1; /* -----> next row */
|
2002-10-12 11:37:38 +00:00
|
|
|
p3= p1+ dl->nr;
|
|
|
|
p4= p2+ dl->nr;
|
|
|
|
b= 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p2= startvert+ dl->nr*a;
|
|
|
|
p1= p2+1;
|
|
|
|
p4= p2+ dl->nr;
|
|
|
|
p3= p1+ dl->nr;
|
|
|
|
b= 1;
|
|
|
|
}
|
2005-08-11 22:27:53 +00:00
|
|
|
if( (dl->flag & DL_CYCL_V) && a==dl->parts-1) {
|
2002-10-12 11:37:38 +00:00
|
|
|
p3-= dl->parts*dl->nr;
|
|
|
|
p4-= dl->parts*dl->nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; b<dl->nr; b++) {
|
|
|
|
mface->v1= p1;
|
|
|
|
mface->v2= p3;
|
|
|
|
mface->v3= p4;
|
|
|
|
mface->v4= p2;
|
2011-09-20 06:25:15 +00:00
|
|
|
mface->mat_nr= dl->col;
|
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
|
|
|
test_index_face(mface, NULL, 0, 4);
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2009-07-01 13:07:28 +00:00
|
|
|
if(smooth) mface->flag |= ME_SMOOTH;
|
2002-10-12 11:37:38 +00:00
|
|
|
mface++;
|
|
|
|
|
2010-03-05 16:47:52 +00:00
|
|
|
p4= p3;
|
2002-10-12 11:37:38 +00:00
|
|
|
p3++;
|
2010-03-05 16:47:52 +00:00
|
|
|
p2= p1;
|
2002-10-12 11:37:38 +00:00
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dl= dl->next;
|
|
|
|
}
|
2011-04-15 01:19:13 +00:00
|
|
|
|
|
|
|
mface= *allface;
|
|
|
|
j = 0;
|
|
|
|
for (i=0; i<totvert; i++, mpoly++, mface++) {
|
|
|
|
int k;
|
|
|
|
|
|
|
|
if (!mface->v3) {
|
|
|
|
mpoly--;
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mface >= *allface + totvlak)
|
|
|
|
break;
|
2011-08-17 05:42:23 +00:00
|
|
|
|
|
|
|
mpoly->flag |= mface->flag & ME_SMOOTH;
|
2011-04-15 01:19:13 +00:00
|
|
|
mpoly->loopstart= j;
|
|
|
|
mpoly->totloop= mface->v4 ? 4 : 3;
|
2011-08-17 05:42:23 +00:00
|
|
|
for (k=0; k<mpoly->totloop; k++, mloop++, j++) {
|
2011-04-15 01:19:13 +00:00
|
|
|
mloop->v = (&mface->v1)[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*_totpoly= i;
|
|
|
|
*_totloop= j;
|
2010-03-05 16:47:52 +00:00
|
|
|
*_totvert= totvert;
|
|
|
|
*_totface= totvlak;
|
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
make_edges_mdata(*allvert, *allface, *allloop, *allpoly, totvert, totvlak, *_totloop, *_totpoly, 0, alledge, _totedge);
|
2010-03-05 16:47:52 +00:00
|
|
|
mfaces_strip_loose(*allface, _totface);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this may fail replacing ob->data, be sure to check ob->type */
|
|
|
|
void nurbs_to_mesh(Object *ob)
|
|
|
|
{
|
2010-08-13 14:23:44 +00:00
|
|
|
Main *bmain= G.main;
|
2010-03-05 16:47:52 +00:00
|
|
|
Object *ob1;
|
|
|
|
DerivedMesh *dm= ob->derivedFinal;
|
|
|
|
Mesh *me;
|
|
|
|
Curve *cu;
|
|
|
|
MVert *allvert= NULL;
|
|
|
|
MEdge *alledge= NULL;
|
|
|
|
MFace *allface= NULL;
|
2011-04-15 01:19:13 +00:00
|
|
|
MLoop *allloop = NULL;
|
|
|
|
MPoly *allpoly = NULL;
|
|
|
|
int totvert, totedge, totface, totloop, totpoly;
|
2010-03-05 16:47:52 +00:00
|
|
|
|
|
|
|
cu= ob->data;
|
|
|
|
|
|
|
|
if (dm == NULL) {
|
2011-04-15 01:19:13 +00:00
|
|
|
if (nurbs_to_mdata (ob, &allvert, &totvert, &alledge, &totedge, &allface, &allloop, &allpoly, &totface, &totloop, &totpoly) != 0) {
|
2010-03-05 16:47:52 +00:00
|
|
|
/* Error initializing */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make mesh */
|
|
|
|
me= add_mesh("Mesh");
|
|
|
|
me->totvert= totvert;
|
|
|
|
me->totface= totface;
|
|
|
|
me->totedge= totedge;
|
2011-04-15 01:19:13 +00:00
|
|
|
me->totloop = totloop;
|
|
|
|
me->totpoly = totpoly;
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2010-03-06 10:22:27 +00:00
|
|
|
me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, allvert, me->totvert);
|
|
|
|
me->medge= CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, alledge, me->totedge);
|
2011-04-15 01:19:13 +00:00
|
|
|
me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, allface, me->totface);
|
|
|
|
me->mloop= CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, allloop, me->totloop);
|
|
|
|
me->mpoly= CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, allpoly, me->totpoly);
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2012-01-06 00:08:37 +00:00
|
|
|
mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
|
2010-03-05 16:47:52 +00:00
|
|
|
} else {
|
|
|
|
me= add_mesh("Mesh");
|
2011-04-15 05:20:18 +00:00
|
|
|
DM_to_mesh(dm, me, ob);
|
2010-03-05 16:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
me->totcol= cu->totcol;
|
|
|
|
me->mat= cu->mat;
|
2010-03-14 12:12:21 +00:00
|
|
|
|
|
|
|
tex_space_mesh(me);
|
|
|
|
|
2011-02-13 10:52:18 +00:00
|
|
|
cu->mat= NULL;
|
2010-03-05 16:47:52 +00:00
|
|
|
cu->totcol= 0;
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if(ob->data) {
|
2010-08-13 14:23:44 +00:00
|
|
|
free_libblock(&bmain->curve, ob->data);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
ob->data= me;
|
|
|
|
ob->type= OB_MESH;
|
2010-03-05 16:47:52 +00:00
|
|
|
|
2003-04-26 13:07:59 +00:00
|
|
|
/* other users */
|
2010-08-13 14:23:44 +00:00
|
|
|
ob1= bmain->object.first;
|
2002-10-12 11:37:38 +00:00
|
|
|
while(ob1) {
|
|
|
|
if(ob1->data==cu) {
|
|
|
|
ob1->type= OB_MESH;
|
|
|
|
|
|
|
|
ob1->data= ob->data;
|
|
|
|
id_us_plus((ID *)ob->data);
|
|
|
|
}
|
|
|
|
ob1= ob1->id.next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-22 23:22:05 +00:00
|
|
|
typedef struct EdgeLink {
|
|
|
|
Link *next, *prev;
|
|
|
|
void *edge;
|
|
|
|
} EdgeLink;
|
|
|
|
|
|
|
|
typedef struct VertLink {
|
|
|
|
Link *next, *prev;
|
2011-12-27 03:54:23 +00:00
|
|
|
unsigned int index;
|
2009-10-22 23:22:05 +00:00
|
|
|
} VertLink;
|
|
|
|
|
2011-12-27 03:54:23 +00:00
|
|
|
static void prependPolyLineVert(ListBase *lb, unsigned int index)
|
2009-10-22 23:22:05 +00:00
|
|
|
{
|
|
|
|
VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
|
|
|
|
vl->index = index;
|
|
|
|
BLI_addhead(lb, vl);
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:54:23 +00:00
|
|
|
static void appendPolyLineVert(ListBase *lb, unsigned int index)
|
2009-10-22 23:22:05 +00:00
|
|
|
{
|
|
|
|
VertLink *vl= MEM_callocN(sizeof(VertLink), "VertLink");
|
|
|
|
vl->index = index;
|
|
|
|
BLI_addtail(lb, vl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_to_curve(Scene *scene, Object *ob)
|
|
|
|
{
|
|
|
|
/* make new mesh data from the original copy */
|
|
|
|
DerivedMesh *dm= mesh_get_derived_final(scene, ob, CD_MASK_MESH);
|
|
|
|
|
|
|
|
MVert *mverts= dm->getVertArray(dm);
|
|
|
|
MEdge *med, *medge= dm->getEdgeArray(dm);
|
|
|
|
MFace *mf, *mface= dm->getTessFaceArray(dm);
|
|
|
|
|
|
|
|
int totedge = dm->getNumEdges(dm);
|
|
|
|
int totface = dm->getNumTessFaces(dm);
|
|
|
|
int totedges = 0;
|
2010-03-07 05:04:22 +00:00
|
|
|
int i, needsFree = 0;
|
2009-10-22 23:22:05 +00:00
|
|
|
|
|
|
|
/* only to detect edge polylines */
|
|
|
|
EdgeHash *eh = BLI_edgehash_new();
|
|
|
|
EdgeHash *eh_edge = BLI_edgehash_new();
|
|
|
|
|
|
|
|
|
|
|
|
ListBase edges = {NULL, NULL};
|
|
|
|
|
|
|
|
/* create edges from all faces (so as to find edges not in any faces) */
|
|
|
|
mf= mface;
|
|
|
|
for (i = 0; i < totface; i++, mf++) {
|
|
|
|
if (!BLI_edgehash_haskey(eh, mf->v1, mf->v2))
|
|
|
|
BLI_edgehash_insert(eh, mf->v1, mf->v2, NULL);
|
|
|
|
if (!BLI_edgehash_haskey(eh, mf->v2, mf->v3))
|
|
|
|
BLI_edgehash_insert(eh, mf->v2, mf->v3, NULL);
|
|
|
|
|
|
|
|
if (mf->v4) {
|
|
|
|
if (!BLI_edgehash_haskey(eh, mf->v3, mf->v4))
|
|
|
|
BLI_edgehash_insert(eh, mf->v3, mf->v4, NULL);
|
|
|
|
if (!BLI_edgehash_haskey(eh, mf->v4, mf->v1))
|
|
|
|
BLI_edgehash_insert(eh, mf->v4, mf->v1, NULL);
|
|
|
|
} else {
|
|
|
|
if (!BLI_edgehash_haskey(eh, mf->v3, mf->v1))
|
|
|
|
BLI_edgehash_insert(eh, mf->v3, mf->v1, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
med= medge;
|
|
|
|
for(i=0; i<totedge; i++, med++) {
|
|
|
|
if (!BLI_edgehash_haskey(eh, med->v1, med->v2)) {
|
|
|
|
EdgeLink *edl= MEM_callocN(sizeof(EdgeLink), "EdgeLink");
|
|
|
|
|
|
|
|
BLI_edgehash_insert(eh_edge, med->v1, med->v2, NULL);
|
|
|
|
edl->edge= med;
|
|
|
|
|
|
|
|
BLI_addtail(&edges, edl); totedges++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BLI_edgehash_free(eh_edge, NULL);
|
|
|
|
BLI_edgehash_free(eh, NULL);
|
|
|
|
|
|
|
|
if(edges.first) {
|
|
|
|
Curve *cu = add_curve(ob->id.name+2, OB_CURVE);
|
|
|
|
cu->flag |= CU_3D;
|
|
|
|
|
|
|
|
while(edges.first) {
|
|
|
|
/* each iteration find a polyline and add this as a nurbs poly spline */
|
|
|
|
|
|
|
|
ListBase polyline = {NULL, NULL}; /* store a list of VertLink's */
|
|
|
|
int closed = FALSE;
|
|
|
|
int totpoly= 0;
|
|
|
|
MEdge *med_current= ((EdgeLink *)edges.last)->edge;
|
2011-12-27 03:54:23 +00:00
|
|
|
unsigned int startVert= med_current->v1;
|
|
|
|
unsigned int endVert= med_current->v2;
|
2009-10-22 23:22:05 +00:00
|
|
|
int ok= TRUE;
|
|
|
|
|
|
|
|
appendPolyLineVert(&polyline, startVert); totpoly++;
|
|
|
|
appendPolyLineVert(&polyline, endVert); totpoly++;
|
|
|
|
BLI_freelinkN(&edges, edges.last); totedges--;
|
|
|
|
|
|
|
|
while(ok) { /* while connected edges are found... */
|
|
|
|
ok = FALSE;
|
|
|
|
i= totedges;
|
|
|
|
while(i) {
|
|
|
|
EdgeLink *edl;
|
|
|
|
|
|
|
|
i-=1;
|
|
|
|
edl= BLI_findlink(&edges, i);
|
|
|
|
med= edl->edge;
|
|
|
|
|
|
|
|
if(med->v1==endVert) {
|
|
|
|
endVert = med->v2;
|
|
|
|
appendPolyLineVert(&polyline, med->v2); totpoly++;
|
|
|
|
BLI_freelinkN(&edges, edl); totedges--;
|
|
|
|
ok= TRUE;
|
|
|
|
}
|
|
|
|
else if(med->v2==endVert) {
|
|
|
|
endVert = med->v1;
|
|
|
|
appendPolyLineVert(&polyline, endVert); totpoly++;
|
|
|
|
BLI_freelinkN(&edges, edl); totedges--;
|
|
|
|
ok= TRUE;
|
|
|
|
}
|
|
|
|
else if(med->v1==startVert) {
|
|
|
|
startVert = med->v2;
|
|
|
|
prependPolyLineVert(&polyline, startVert); totpoly++;
|
|
|
|
BLI_freelinkN(&edges, edl); totedges--;
|
|
|
|
ok= TRUE;
|
|
|
|
}
|
|
|
|
else if(med->v2==startVert) {
|
|
|
|
startVert = med->v1;
|
|
|
|
prependPolyLineVert(&polyline, startVert); totpoly++;
|
|
|
|
BLI_freelinkN(&edges, edl); totedges--;
|
|
|
|
ok= TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we have a polyline, make into a curve */
|
|
|
|
if(startVert==endVert) {
|
|
|
|
BLI_freelinkN(&polyline, polyline.last);
|
|
|
|
totpoly--;
|
|
|
|
closed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --- nurbs --- */
|
|
|
|
{
|
|
|
|
Nurb *nu;
|
|
|
|
BPoint *bp;
|
|
|
|
VertLink *vl;
|
|
|
|
|
|
|
|
/* create new 'nurb' within the curve */
|
|
|
|
nu = (Nurb *)MEM_callocN(sizeof(Nurb), "MeshNurb");
|
|
|
|
|
|
|
|
nu->pntsu= totpoly;
|
|
|
|
nu->pntsv= 1;
|
|
|
|
nu->orderu= 4;
|
merge own commits into render branch into trunk since 27560
27562, 27570, 27571, 27574, 27576, 27577, 27579, 27590, 27591, 27594, 27595, 27596, 27599, 27605, 27611, 27612, 27613, 27614, 27623
2010-03-20 16:41:01 +00:00
|
|
|
nu->flagu= CU_NURB_ENDPOINT | (closed ? CU_NURB_CYCLIC:0); /* endpoint */
|
2009-10-22 23:22:05 +00:00
|
|
|
nu->resolu= 12;
|
|
|
|
|
|
|
|
nu->bp= (BPoint *)MEM_callocN(sizeof(BPoint)*totpoly, "bpoints");
|
|
|
|
|
|
|
|
/* add points */
|
|
|
|
vl= polyline.first;
|
|
|
|
for (i=0, bp=nu->bp; i < totpoly; i++, bp++, vl=(VertLink *)vl->next) {
|
2009-11-10 20:43:45 +00:00
|
|
|
copy_v3_v3(bp->vec, mverts[vl->index].co);
|
2009-10-22 23:22:05 +00:00
|
|
|
bp->f1= SELECT;
|
|
|
|
bp->radius = bp->weight = 1.0;
|
|
|
|
}
|
|
|
|
BLI_freelistN(&polyline);
|
|
|
|
|
|
|
|
/* add nurb to curve */
|
|
|
|
BLI_addtail(&cu->nurb, nu);
|
|
|
|
}
|
|
|
|
/* --- done with nurbs --- */
|
|
|
|
}
|
|
|
|
|
|
|
|
((Mesh *)ob->data)->id.us--;
|
|
|
|
ob->data= cu;
|
|
|
|
ob->type= OB_CURVE;
|
2010-03-07 05:04:22 +00:00
|
|
|
|
|
|
|
/* curve objects can't contain DM in usual cases, we could free memory */
|
|
|
|
needsFree= 1;
|
2009-10-22 23:22:05 +00:00
|
|
|
}
|
|
|
|
|
2010-03-07 05:04:22 +00:00
|
|
|
dm->needsFree = needsFree;
|
2009-10-22 23:22:05 +00:00
|
|
|
dm->release(dm);
|
2010-03-07 05:04:22 +00:00
|
|
|
|
|
|
|
if (needsFree) {
|
|
|
|
ob->derivedFinal = NULL;
|
2010-06-23 09:58:02 +00:00
|
|
|
|
|
|
|
/* curve object could have got bounding box only in special cases */
|
|
|
|
if(ob->bb) {
|
|
|
|
MEM_freeN(ob->bb);
|
|
|
|
ob->bb= NULL;
|
|
|
|
}
|
2010-03-07 05:04:22 +00:00
|
|
|
}
|
2009-10-22 23:22:05 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 06:25:15 +00:00
|
|
|
void mesh_delete_material_index(Mesh *me, short index)
|
2009-01-04 14:14:06 +00:00
|
|
|
{
|
2004-03-20 23:59:57 +00:00
|
|
|
int i;
|
|
|
|
|
2011-04-24 08:06:26 +00:00
|
|
|
for (i=0; i<me->totpoly; i++) {
|
2011-04-24 08:11:44 +00:00
|
|
|
MPoly *mp = &((MPoly*) me->mpoly)[i];
|
|
|
|
if (mp->mat_nr && mp->mat_nr>=index)
|
|
|
|
mp->mat_nr--;
|
2011-04-24 08:06:26 +00:00
|
|
|
}
|
|
|
|
|
2004-03-20 23:59:57 +00:00
|
|
|
for (i=0; i<me->totface; i++) {
|
|
|
|
MFace *mf = &((MFace*) me->mface)[i];
|
|
|
|
if (mf->mat_nr && mf->mat_nr>=index)
|
|
|
|
mf->mat_nr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 14:14:06 +00:00
|
|
|
void mesh_set_smooth_flag(Object *meshOb, int enableSmooth)
|
|
|
|
{
|
2005-07-14 15:48:01 +00:00
|
|
|
Mesh *me = meshOb->data;
|
2004-03-20 23:59:57 +00:00
|
|
|
int i;
|
|
|
|
|
2011-04-24 08:06:26 +00:00
|
|
|
for (i=0; i<me->totpoly; i++) {
|
|
|
|
MPoly *mp = &((MPoly*) me->mpoly)[i];
|
|
|
|
|
|
|
|
if (enableSmooth) {
|
|
|
|
mp->flag |= ME_SMOOTH;
|
|
|
|
} else {
|
|
|
|
mp->flag &= ~ME_SMOOTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-20 23:59:57 +00:00
|
|
|
for (i=0; i<me->totface; i++) {
|
|
|
|
MFace *mf = &((MFace*) me->mface)[i];
|
|
|
|
|
|
|
|
if (enableSmooth) {
|
|
|
|
mf->flag |= ME_SMOOTH;
|
|
|
|
} else {
|
|
|
|
mf->flag &= ~ME_SMOOTH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
|
2012-01-06 00:08:37 +00:00
|
|
|
void mesh_calc_normals_mapping(MVert *mverts, int numVerts,
|
|
|
|
MLoop *mloop, MPoly *mpolys, int numLoops, int numPolys, float (*polyNors_r)[3],
|
|
|
|
MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3])
|
2011-12-06 22:55:41 +00:00
|
|
|
{
|
2012-01-06 00:08:37 +00:00
|
|
|
mesh_calc_normals_mapping_ex(mverts, numVerts, mloop, mpolys,
|
|
|
|
numLoops, numPolys, polyNors_r, mfaces, numFaces,
|
|
|
|
origIndexFace, faceNors_r, TRUE);
|
2011-12-06 22:55:41 +00:00
|
|
|
}
|
2012-01-06 00:08:37 +00:00
|
|
|
|
|
|
|
void mesh_calc_normals_mapping_ex(MVert *mverts, int numVerts,
|
|
|
|
MLoop *mloop, MPoly *mpolys,
|
|
|
|
int numLoops, int numPolys, float (*polyNors_r)[3],
|
|
|
|
MFace *mfaces, int numFaces, int *origIndexFace, float (*faceNors_r)[3],
|
|
|
|
const short only_face_normals)
|
2011-04-15 01:19:13 +00:00
|
|
|
{
|
|
|
|
float (*pnors)[3] = polyNors_r, (*fnors)[3] = faceNors_r;
|
2012-01-06 00:08:37 +00:00
|
|
|
int i;
|
2011-04-15 01:19:13 +00:00
|
|
|
MFace *mf;
|
|
|
|
MPoly *mp;
|
2011-09-30 05:27:44 +00:00
|
|
|
|
|
|
|
if (numPolys == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-06 22:55:41 +00:00
|
|
|
/* if we are not calculating verts and no verts were passes thene we have nothign to do */
|
|
|
|
if ((only_face_normals == TRUE) && (polyNors_r == NULL) && (faceNors_r == NULL)) {
|
|
|
|
printf("%s: called with nothing to do\n", __func__);
|
2011-09-30 05:27:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-06 22:55:41 +00:00
|
|
|
if (!pnors) pnors = MEM_callocN(sizeof(float) * 3 * numPolys, "poly_nors mesh.c");
|
|
|
|
/* if (!fnors) fnors = MEM_callocN(sizeof(float) * 3 * numFaces, "face nors mesh.c"); */ /* NO NEED TO ALLOC YET */
|
|
|
|
|
|
|
|
|
|
|
|
if (only_face_normals == FALSE) {
|
|
|
|
/* vertex normals are optional, they require some extra calculations,
|
|
|
|
* so make them optional */
|
2012-01-07 20:53:11 +00:00
|
|
|
mesh_calc_normals(mverts, numVerts, mloop, mpolys, numLoops, numPolys, pnors);
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
2011-12-06 22:55:41 +00:00
|
|
|
else {
|
|
|
|
/* only calc poly normals */
|
|
|
|
mp = mpolys;
|
|
|
|
for (i=0; i<numPolys; i++, mp++) {
|
|
|
|
mesh_calc_poly_normal(mp, mloop+mp->loopstart, mverts, pnors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( origIndexFace &&
|
|
|
|
/* fnors==faceNors_r */ /* NO NEED TO ALLOC YET */
|
|
|
|
fnors != NULL &&
|
|
|
|
numFaces)
|
|
|
|
{
|
2011-04-15 01:19:13 +00:00
|
|
|
mf = mfaces;
|
|
|
|
for (i=0; i<numFaces; i++, mf++, origIndexFace++) {
|
2011-04-15 02:18:24 +00:00
|
|
|
if (*origIndexFace < numPolys) {
|
2011-11-20 16:21:13 +00:00
|
|
|
copy_v3_v3(fnors[i], pnors[*origIndexFace]);
|
2011-04-15 01:19:13 +00:00
|
|
|
} else {
|
|
|
|
/*eek, we're not corrusponding to polys*/
|
|
|
|
printf("error in mesh_calc_normals; tesselation face indices are incorrect. normals may look bad.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-30 05:27:44 +00:00
|
|
|
|
2011-12-06 22:55:41 +00:00
|
|
|
if (pnors != polyNors_r) MEM_freeN(pnors);
|
|
|
|
/* if (fnors != faceNors_r) MEM_freeN(fnors); */ /* NO NEED TO ALLOC YET */
|
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
fnors = pnors = NULL;
|
|
|
|
|
2004-03-20 23:59:57 +00:00
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
|
2012-01-06 00:08:37 +00:00
|
|
|
void mesh_calc_normals(MVert *mverts, int numVerts, MLoop *mloop, MPoly *mpolys,
|
|
|
|
int UNUSED(numLoops), int numPolys, float (*polyNors_r)[3])
|
|
|
|
{
|
|
|
|
float (*pnors)[3] = polyNors_r;
|
|
|
|
|
|
|
|
float (*tnorms)[3], (*edgevecbuf)[3]= NULL;
|
|
|
|
float **vertcos = NULL, **vertnos = NULL;
|
|
|
|
BLI_array_declare(vertcos);
|
|
|
|
BLI_array_declare(vertnos);
|
|
|
|
BLI_array_declare(edgevecbuf);
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
MPoly *mp;
|
|
|
|
MLoop *ml;
|
|
|
|
|
|
|
|
if (!pnors) pnors = MEM_callocN(sizeof(float) * 3 * numPolys, "poly_nors mesh.c");
|
|
|
|
|
|
|
|
/*first go through and calculate normals for all the polys*/
|
|
|
|
tnorms = MEM_callocN(sizeof(float)*3*numVerts, "tnorms mesh.c");
|
|
|
|
|
|
|
|
mp = mpolys;
|
|
|
|
for (i=0; i<numPolys; i++, mp++) {
|
|
|
|
mesh_calc_poly_normal(mp, mloop+mp->loopstart, mverts, pnors[i]);
|
|
|
|
ml = mloop + mp->loopstart;
|
|
|
|
|
|
|
|
BLI_array_empty(vertcos);
|
|
|
|
BLI_array_empty(vertnos);
|
|
|
|
for (j=0; j<mp->totloop; j++) {
|
|
|
|
int vindex = ml[j].v;
|
|
|
|
BLI_array_append(vertcos, mverts[vindex].co);
|
|
|
|
BLI_array_append(vertnos, tnorms[vindex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_array_empty(edgevecbuf);
|
|
|
|
BLI_array_growitems(edgevecbuf, mp->totloop);
|
|
|
|
|
|
|
|
accumulate_vertex_normals_poly(vertnos, pnors[i], vertcos, edgevecbuf, mp->totloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_array_free(vertcos);
|
|
|
|
BLI_array_free(vertnos);
|
|
|
|
BLI_array_free(edgevecbuf);
|
|
|
|
|
|
|
|
/* following Mesh convention; we use vertex coordinate itself for normal in this case */
|
|
|
|
for(i=0; i<numVerts; i++) {
|
|
|
|
MVert *mv= &mverts[i];
|
|
|
|
float *no= tnorms[i];
|
|
|
|
|
|
|
|
if(normalize_v3(no) == 0.0f)
|
|
|
|
normalize_v3_v3(no, mv->co);
|
|
|
|
|
|
|
|
normal_float_to_short_v3(mv->no, no);
|
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(tnorms);
|
|
|
|
|
|
|
|
if (pnors != polyNors_r) MEM_freeN(pnors);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_calc_normals_tessface(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float (*faceNors_r)[3])
|
2005-07-17 21:24:43 +00:00
|
|
|
{
|
|
|
|
float (*tnorms)[3]= MEM_callocN(numVerts*sizeof(*tnorms), "tnorms");
|
2011-03-26 08:28:24 +00:00
|
|
|
float (*fnors)[3]= (faceNors_r)? faceNors_r: MEM_callocN(sizeof(*fnors)*numFaces, "meshnormals");
|
2005-07-17 21:24:43 +00:00
|
|
|
int i;
|
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-03-20 13:35:35 +00:00
|
|
|
for(i=0; i<numFaces; i++) {
|
2005-07-17 21:24:43 +00:00
|
|
|
MFace *mf= &mfaces[i];
|
2011-03-26 08:28:24 +00:00
|
|
|
float *f_no= fnors[i];
|
2011-04-04 13:02:12 +00:00
|
|
|
float *n4 = (mf->v4)? tnorms[mf->v4]: NULL;
|
|
|
|
float *c4 = (mf->v4)? mverts[mf->v4].co: NULL;
|
2005-07-17 21:24:43 +00:00
|
|
|
|
2011-03-20 13:35:35 +00:00
|
|
|
if(mf->v4)
|
|
|
|
normal_quad_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co);
|
- added mesh_strip_loose_faces, works in conjunction with make_edges
to get rid of faces with MFace.v3==0
- change all Mesh's to have ->medge now. This is forced by make_edges
on readfile, and in the various exotic important routines, and on
conversion back in python.
- make python NMesh structure always have medges now (needs testing)
- with above two changes it is guarenteed that mf->v3 is never ==0
in main blender code (i.e., all MFace's are actually triangles
or quads) and so I went through and removed all the historic tests
to deal with MFace.v3==0. Equals lots of deleting, I am in heaven!
- removed MEdge edcode flag, no longer needed
- added experimental replacement for edge flag system
Still are some inconsistencies in FACESELECT mode edge drawing to
be ironed out.
NOTE: This commit adds an experimental edge flag calc system, based
on 10-seconds-of-thought algorithm by yours truly. Would appreciate
feedback on how this system works, esp compared to old one and esp
on complex or interesting models.
To Use: New system is enabled by setting G.rt to a value between
1 and 1000 (Value of 0 uses old system). Value 1000 is reserved for
"auto" edge, which is more or less identical to old system but also
makes sure that at least 10% of edges are drawn (solves errors for
super subdivided meshes). Values between 1 and 999 act as percent
(out of 1000) of edges that should be drawn, starting with "most
interesting" edges first. Please try it and comment!
2005-08-21 07:19:20 +00:00
|
|
|
else
|
2011-03-20 13:35:35 +00:00
|
|
|
normal_tri_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co);
|
|
|
|
|
2011-04-04 13:02:12 +00:00
|
|
|
accumulate_vertex_normals(tnorms[mf->v1], tnorms[mf->v2], tnorms[mf->v3], n4,
|
|
|
|
f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4);
|
2005-07-17 21:24:43 +00:00
|
|
|
}
|
2011-03-20 13:35:35 +00:00
|
|
|
|
|
|
|
/* following Mesh convention; we use vertex coordinate itself for normal in this case */
|
|
|
|
for(i=0; i<numVerts; i++) {
|
2005-07-17 21:24:43 +00:00
|
|
|
MVert *mv= &mverts[i];
|
|
|
|
float *no= tnorms[i];
|
|
|
|
|
2011-03-20 13:35:35 +00:00
|
|
|
if(normalize_v3(no) == 0.0f)
|
2010-08-15 15:14:08 +00:00
|
|
|
normalize_v3_v3(no, mv->co);
|
2005-07-23 19:03:43 +00:00
|
|
|
|
2010-08-15 15:14:08 +00:00
|
|
|
normal_float_to_short_v3(mv->no, no);
|
2005-07-17 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(tnorms);
|
|
|
|
|
2011-03-26 08:28:24 +00:00
|
|
|
if(fnors != faceNors_r)
|
2005-07-17 21:24:43 +00:00
|
|
|
MEM_freeN(fnors);
|
|
|
|
}
|
2005-07-22 17:03:50 +00:00
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
|
2011-05-11 02:14:43 +00:00
|
|
|
static void bmesh_corners_to_loops(Mesh *me, int findex, int loopstart, int numTex, int numCol)
|
2011-04-15 01:19:13 +00:00
|
|
|
{
|
|
|
|
MTFace *texface;
|
|
|
|
MTexPoly *texpoly;
|
|
|
|
MCol *mcol;
|
|
|
|
MLoopCol *mloopcol;
|
|
|
|
MLoopUV *mloopuv;
|
|
|
|
MFace *mf;
|
|
|
|
int i;
|
|
|
|
|
2011-05-11 02:14:43 +00:00
|
|
|
mf = me->mface + findex;
|
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
for(i=0; i < numTex; i++){
|
|
|
|
texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
|
|
|
|
texpoly = CustomData_get_n(&me->pdata, CD_MTEXPOLY, findex, i);
|
|
|
|
|
|
|
|
texpoly->tpage = texface->tpage;
|
|
|
|
texpoly->flag = texface->flag;
|
|
|
|
texpoly->transp = texface->transp;
|
|
|
|
texpoly->mode = texface->mode;
|
|
|
|
texpoly->tile = texface->tile;
|
|
|
|
texpoly->unwrap = texface->unwrap;
|
|
|
|
|
|
|
|
mloopuv = CustomData_get_n(&me->ldata, CD_MLOOPUV, loopstart, i);
|
|
|
|
mloopuv->uv[0] = texface->uv[0][0]; mloopuv->uv[1] = texface->uv[0][1]; mloopuv++;
|
|
|
|
mloopuv->uv[0] = texface->uv[1][0]; mloopuv->uv[1] = texface->uv[1][1]; mloopuv++;
|
|
|
|
mloopuv->uv[0] = texface->uv[2][0]; mloopuv->uv[1] = texface->uv[2][1]; mloopuv++;
|
|
|
|
|
|
|
|
if (mf->v4) {
|
|
|
|
mloopuv->uv[0] = texface->uv[3][0]; mloopuv->uv[1] = texface->uv[3][1]; mloopuv++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < numCol; i++){
|
|
|
|
mloopcol = CustomData_get_n(&me->ldata, CD_MLOOPCOL, loopstart, i);
|
|
|
|
mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
|
|
|
|
|
|
|
|
mloopcol->r = mcol[0].r; mloopcol->g = mcol[0].g; mloopcol->b = mcol[0].b; mloopcol->a = mcol[0].a; mloopcol++;
|
|
|
|
mloopcol->r = mcol[1].r; mloopcol->g = mcol[1].g; mloopcol->b = mcol[1].b; mloopcol->a = mcol[1].a; mloopcol++;
|
|
|
|
mloopcol->r = mcol[2].r; mloopcol->g = mcol[2].g; mloopcol->b = mcol[2].b; mloopcol->a = mcol[2].a; mloopcol++;
|
|
|
|
if (mf->v4) {
|
|
|
|
mloopcol->r = mcol[3].r; mloopcol->g = mcol[3].g; mloopcol->b = mcol[3].b; mloopcol->a = mcol[3].a; mloopcol++;
|
|
|
|
}
|
2005-07-17 21:24:43 +00:00
|
|
|
}
|
2011-04-15 01:19:13 +00:00
|
|
|
|
|
|
|
if (CustomData_has_layer(&me->fdata, CD_MDISPS)) {
|
|
|
|
MDisps *ld = CustomData_get(&me->ldata, loopstart, CD_MDISPS);
|
|
|
|
MDisps *fd = CustomData_get(&me->fdata, findex, CD_MDISPS);
|
|
|
|
float (*disps)[3] = fd->disps;
|
|
|
|
int i, tot = mf->v4 ? 4 : 3;
|
|
|
|
int side, corners;
|
|
|
|
|
|
|
|
corners = multires_mdisp_corners(fd);
|
|
|
|
|
2011-10-09 21:59:29 +00:00
|
|
|
if (corners == 0) {
|
|
|
|
/* Empty MDisp layers appear in at least one of the sintel.blend files.
|
|
|
|
Not sure why this happens, but it seems fine to just ignore them here.
|
|
|
|
If corners==0 for a non-empty layer though, something went wrong. */
|
|
|
|
BLI_assert(fd->totdisp == 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
side = sqrt(fd->totdisp / corners);
|
|
|
|
|
|
|
|
for (i=0; i<tot; i++, disps += side*side, ld++) {
|
|
|
|
ld->totdisp = side*side;
|
2011-04-15 01:19:13 +00:00
|
|
|
|
2011-10-09 21:59:29 +00:00
|
|
|
if (ld->disps)
|
|
|
|
BLI_cellalloc_free(ld->disps);
|
2011-04-15 01:19:13 +00:00
|
|
|
|
2011-10-09 21:59:29 +00:00
|
|
|
ld->disps = BLI_cellalloc_calloc(sizeof(float)*3*side*side, "converted loop mdisps");
|
|
|
|
if (fd->disps) {
|
|
|
|
memcpy(ld->disps, disps, sizeof(float)*3*side*side);
|
|
|
|
}
|
2011-08-15 23:38:51 +00:00
|
|
|
}
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void convert_mfaces_to_mpolys(Mesh *mesh)
|
|
|
|
{
|
|
|
|
MFace *mf;
|
|
|
|
MLoop *ml;
|
|
|
|
MPoly *mp;
|
|
|
|
MEdge *me;
|
|
|
|
EdgeHash *eh;
|
|
|
|
int numTex, numCol;
|
|
|
|
int i, j, totloop;
|
|
|
|
|
|
|
|
mesh->totpoly = mesh->totface;
|
|
|
|
mesh->mpoly = MEM_callocN(sizeof(MPoly)*mesh->totpoly, "mpoly converted");
|
|
|
|
CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_ASSIGN, mesh->mpoly, mesh->totpoly);
|
|
|
|
|
|
|
|
numTex = CustomData_number_of_layers(&mesh->fdata, CD_MTFACE);
|
|
|
|
numCol = CustomData_number_of_layers(&mesh->fdata, CD_MCOL);
|
|
|
|
|
|
|
|
totloop = 0;
|
|
|
|
mf = mesh->mface;
|
|
|
|
for (i=0; i<mesh->totface; i++, mf++) {
|
|
|
|
totloop += mf->v4 ? 4 : 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh->totloop = totloop;
|
|
|
|
mesh->mloop = MEM_callocN(sizeof(MLoop)*mesh->totloop, "mloop converted");
|
|
|
|
|
|
|
|
CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_ASSIGN, mesh->mloop, totloop);
|
|
|
|
CustomData_to_bmeshpoly(&mesh->fdata, &mesh->pdata, &mesh->ldata,
|
|
|
|
mesh->totloop, mesh->totpoly);
|
|
|
|
|
|
|
|
eh = BLI_edgehash_new();
|
|
|
|
|
|
|
|
/*build edge hash*/
|
|
|
|
me = mesh->medge;
|
|
|
|
for (i=0; i<mesh->totedge; i++, me++) {
|
|
|
|
BLI_edgehash_insert(eh, me->v1, me->v2, SET_INT_IN_POINTER(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0; /*current loop index*/
|
|
|
|
ml = mesh->mloop;
|
|
|
|
mf = mesh->mface;
|
|
|
|
mp = mesh->mpoly;
|
|
|
|
for (i=0; i<mesh->totface; i++, mf++, mp++) {
|
|
|
|
mp->loopstart = j;
|
|
|
|
|
|
|
|
mp->totloop = mf->v4 ? 4 : 3;
|
|
|
|
|
|
|
|
mp->mat_nr = mf->mat_nr;
|
|
|
|
mp->flag = mf->flag;
|
|
|
|
|
|
|
|
#define ML(v1, v2) {ml->v = mf->v1; ml->e = GET_INT_FROM_POINTER(BLI_edgehash_lookup(eh, mf->v1, mf->v2)); ml++; j++;}
|
|
|
|
|
|
|
|
ML(v1, v2);
|
|
|
|
ML(v2, v3);
|
|
|
|
if (mf->v4) {
|
|
|
|
ML(v3, v4);
|
|
|
|
ML(v4, v1);
|
|
|
|
} else {
|
|
|
|
ML(v3, v1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ML
|
|
|
|
|
|
|
|
bmesh_corners_to_loops(mesh, i, mp->loopstart, numTex, numCol);
|
|
|
|
}
|
|
|
|
|
2011-09-09 14:51:30 +00:00
|
|
|
/* note, we dont convert FGons at all, these are not even real ngons,
|
|
|
|
* they have their own UV's, colors etc - its more an editing feature. */
|
2011-04-15 01:19:13 +00:00
|
|
|
|
2011-12-06 09:28:25 +00:00
|
|
|
mesh_update_customdata_pointers(mesh, TRUE);
|
2011-11-13 15:13:59 +00:00
|
|
|
|
2011-04-15 01:19:13 +00:00
|
|
|
BLI_edgehash_free(eh, NULL);
|
2005-07-17 21:24:43 +00:00
|
|
|
}
|
2005-07-22 17:03:50 +00:00
|
|
|
|
|
|
|
float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3]
|
|
|
|
{
|
2009-01-04 18:16:34 +00:00
|
|
|
int i, numVerts = me->totvert;
|
|
|
|
float (*cos)[3] = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos1");
|
2012-01-20 23:03:41 +00:00
|
|
|
|
2009-01-04 18:16:34 +00:00
|
|
|
if (numVerts_r) *numVerts_r = numVerts;
|
|
|
|
for (i=0; i<numVerts; i++)
|
2011-11-13 15:13:59 +00:00
|
|
|
copy_v3_v3(cos[i], me->mvert[i].co);
|
2012-01-20 23:03:41 +00:00
|
|
|
|
2009-01-04 18:16:34 +00:00
|
|
|
return cos;
|
2005-07-22 17:03:50 +00:00
|
|
|
}
|
2006-01-10 11:36:57 +00:00
|
|
|
|
2011-10-30 09:19:07 +00:00
|
|
|
|
|
|
|
/* ngon version wip, based on EDBM_make_uv_vert_map */
|
|
|
|
/* this replaces the non bmesh function (in trunk) which takes MTFace's, if we ever need it back we could
|
|
|
|
* but for now this replaces it because its unused. */
|
|
|
|
|
|
|
|
UvVertMap *make_uv_vert_map(struct MPoly *mpoly, struct MLoop *mloop, struct MLoopUV *mloopuv, unsigned int totpoly, unsigned int totvert, int selected, float *limit)
|
2006-01-10 11:36:57 +00:00
|
|
|
{
|
|
|
|
UvVertMap *vmap;
|
|
|
|
UvMapVert *buf;
|
2011-10-30 09:19:07 +00:00
|
|
|
MPoly *mp;
|
2006-04-03 20:31:10 +00:00
|
|
|
unsigned int a;
|
|
|
|
int i, totuv, nverts;
|
2006-01-10 11:36:57 +00:00
|
|
|
|
|
|
|
totuv = 0;
|
|
|
|
|
|
|
|
/* generate UvMapVert array */
|
2011-10-30 09:19:07 +00:00
|
|
|
mp= mpoly;
|
|
|
|
for(a=0; a<totpoly; a++, mp++)
|
|
|
|
if(!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL)))
|
|
|
|
totuv += mp->totloop;
|
|
|
|
|
2006-01-10 11:36:57 +00:00
|
|
|
if(totuv==0)
|
|
|
|
return NULL;
|
|
|
|
|
2008-03-12 19:55:37 +00:00
|
|
|
vmap= (UvVertMap*)MEM_callocN(sizeof(*vmap), "UvVertMap");
|
2006-01-10 11:36:57 +00:00
|
|
|
if (!vmap)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vmap->vert= (UvMapVert**)MEM_callocN(sizeof(*vmap->vert)*totvert, "UvMapVert*");
|
2008-03-12 19:55:37 +00:00
|
|
|
buf= vmap->buf= (UvMapVert*)MEM_callocN(sizeof(*vmap->buf)*totuv, "UvMapVert");
|
2006-01-10 11:36:57 +00:00
|
|
|
|
|
|
|
if (!vmap->vert || !vmap->buf) {
|
|
|
|
free_uv_vert_map(vmap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-30 09:19:07 +00:00
|
|
|
mp= mpoly;
|
|
|
|
for(a=0; a<totpoly; a++, mp++) {
|
|
|
|
if(!selected || (!(mp->flag & ME_HIDE) && (mp->flag & ME_FACE_SEL))) {
|
|
|
|
nverts= mp->totloop;
|
2006-01-10 11:36:57 +00:00
|
|
|
|
|
|
|
for(i=0; i<nverts; i++) {
|
|
|
|
buf->tfindex= i;
|
|
|
|
buf->f= a;
|
|
|
|
buf->separate = 0;
|
2011-10-30 09:19:07 +00:00
|
|
|
buf->next= vmap->vert[mloop[mp->loopstart + i].v];
|
|
|
|
vmap->vert[mloop[mp->loopstart + i].v]= buf;
|
2006-01-10 11:36:57 +00:00
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort individual uvs for each vert */
|
|
|
|
for(a=0; a<totvert; a++) {
|
|
|
|
UvMapVert *newvlist= NULL, *vlist=vmap->vert[a];
|
|
|
|
UvMapVert *iterv, *v, *lastv, *next;
|
|
|
|
float *uv, *uv2, uvdiff[2];
|
|
|
|
|
|
|
|
while(vlist) {
|
|
|
|
v= vlist;
|
|
|
|
vlist= vlist->next;
|
|
|
|
v->next= newvlist;
|
|
|
|
newvlist= v;
|
|
|
|
|
2011-10-30 09:19:07 +00:00
|
|
|
uv= mloopuv[mpoly[v->f].loopstart + v->tfindex].uv;
|
2006-01-10 11:36:57 +00:00
|
|
|
lastv= NULL;
|
|
|
|
iterv= vlist;
|
|
|
|
|
|
|
|
while(iterv) {
|
|
|
|
next= iterv->next;
|
|
|
|
|
2011-10-30 09:19:07 +00:00
|
|
|
uv2= mloopuv[mpoly[iterv->f].loopstart + iterv->tfindex].uv;
|
2009-11-10 20:43:45 +00:00
|
|
|
sub_v2_v2v2(uvdiff, uv2, uv);
|
2006-01-10 11:36:57 +00:00
|
|
|
|
|
|
|
|
2011-03-28 04:22:50 +00:00
|
|
|
if(fabsf(uv[0]-uv2[0]) < limit[0] && fabsf(uv[1]-uv2[1]) < limit[1]) {
|
2006-01-10 11:36:57 +00:00
|
|
|
if(lastv) lastv->next= next;
|
|
|
|
else vlist= next;
|
|
|
|
iterv->next= newvlist;
|
|
|
|
newvlist= iterv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lastv=iterv;
|
|
|
|
|
|
|
|
iterv= next;
|
|
|
|
}
|
|
|
|
|
|
|
|
newvlist->separate = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vmap->vert[a]= newvlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
UvMapVert *get_uv_map_vert(UvVertMap *vmap, unsigned int v)
|
|
|
|
{
|
|
|
|
return vmap->vert[v];
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_uv_vert_map(UvVertMap *vmap)
|
|
|
|
{
|
|
|
|
if (vmap) {
|
|
|
|
if (vmap->vert) MEM_freeN(vmap->vert);
|
|
|
|
if (vmap->buf) MEM_freeN(vmap->buf);
|
|
|
|
MEM_freeN(vmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-21 22:40:28 +00:00
|
|
|
/* Generates a map where the key is the vertex and the value is a list
|
|
|
|
of faces that use that vertex as a corner. The lists are allocated
|
|
|
|
from one memory pool. */
|
|
|
|
void create_vert_face_map(ListBase **map, IndexNode **mem, const MFace *mface, const int totvert, const int totface)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
IndexNode *node = NULL;
|
|
|
|
|
|
|
|
(*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert face map");
|
|
|
|
(*mem) = MEM_callocN(sizeof(IndexNode) * totface*4, "vert face map mem");
|
|
|
|
node = *mem;
|
|
|
|
|
|
|
|
/* Find the users */
|
|
|
|
for(i = 0; i < totface; ++i){
|
|
|
|
for(j = 0; j < (mface[i].v4?4:3); ++j, ++node) {
|
|
|
|
node->index = i;
|
|
|
|
BLI_addtail(&(*map)[((unsigned int*)(&mface[i]))[j]], node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generates a map where the key is the vertex and the value is a list
|
|
|
|
of edges that use that vertex as an endpoint. The lists are allocated
|
|
|
|
from one memory pool. */
|
|
|
|
void create_vert_edge_map(ListBase **map, IndexNode **mem, const MEdge *medge, const int totvert, const int totedge)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
IndexNode *node = NULL;
|
|
|
|
|
|
|
|
(*map) = MEM_callocN(sizeof(ListBase) * totvert, "vert edge map");
|
|
|
|
(*mem) = MEM_callocN(sizeof(IndexNode) * totedge * 2, "vert edge map mem");
|
|
|
|
node = *mem;
|
2011-04-21 13:11:51 +00:00
|
|
|
|
2009-01-21 22:40:28 +00:00
|
|
|
/* Find the users */
|
|
|
|
for(i = 0; i < totedge; ++i){
|
|
|
|
for(j = 0; j < 2; ++j, ++node) {
|
|
|
|
node->index = i;
|
|
|
|
BLI_addtail(&(*map)[((unsigned int*)(&medge[i].v1))[j]], node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
void mesh_loops_to_mface_corners(CustomData *fdata, CustomData *ldata,
|
2011-12-28 22:37:09 +00:00
|
|
|
CustomData *pdata, int lindex[4], int findex,
|
|
|
|
const int polyindex,
|
|
|
|
const int mf_len, /* 3 or 4 */
|
|
|
|
|
|
|
|
/* cache values to avoid lookups every time */
|
|
|
|
const int numTex, /* CustomData_number_of_layers(pdata, CD_MTEXPOLY) */
|
|
|
|
const int numCol, /* CustomData_number_of_layers(ldata, CD_MLOOPCOL) */
|
|
|
|
const int hasWCol /* CustomData_has_layer(ldata, CD_WEIGHT_MLOOPCOL) */
|
|
|
|
)
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
|
|
|
MTFace *texface;
|
|
|
|
MTexPoly *texpoly;
|
|
|
|
MCol *mcol;
|
|
|
|
MLoopCol *mloopcol;
|
|
|
|
MLoopUV *mloopuv;
|
2011-12-28 22:37:09 +00:00
|
|
|
int i, j;
|
2011-03-25 00:32:38 +00:00
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
for(i=0; i < numTex; i++){
|
|
|
|
texface = CustomData_get_n(fdata, CD_MTFACE, findex, i);
|
|
|
|
texpoly = CustomData_get_n(pdata, CD_MTEXPOLY, polyindex, i);
|
|
|
|
|
|
|
|
texface->tpage = texpoly->tpage;
|
|
|
|
texface->flag = texpoly->flag;
|
|
|
|
texface->transp = texpoly->transp;
|
|
|
|
texface->mode = texpoly->mode;
|
|
|
|
texface->tile = texpoly->tile;
|
|
|
|
texface->unwrap = texpoly->unwrap;
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
for (j=0; j < mf_len; j++) {
|
2009-08-30 21:30:07 +00:00
|
|
|
mloopuv = CustomData_get_n(ldata, CD_MLOOPUV, lindex[j], i);
|
|
|
|
texface->uv[j][0] = mloopuv->uv[0];
|
|
|
|
texface->uv[j][1] = mloopuv->uv[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < numCol; i++){
|
|
|
|
mcol = CustomData_get_n(fdata, CD_MCOL, findex, i);
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
for (j=0; j < mf_len; j++) {
|
2009-08-30 21:30:07 +00:00
|
|
|
mloopcol = CustomData_get_n(ldata, CD_MLOOPCOL, lindex[j], i);
|
|
|
|
mcol[j].r = mloopcol->r;
|
|
|
|
mcol[j].g = mloopcol->g;
|
|
|
|
mcol[j].b = mloopcol->b;
|
|
|
|
mcol[j].a = mloopcol->a;
|
|
|
|
}
|
|
|
|
}
|
2009-08-31 15:57:13 +00:00
|
|
|
|
|
|
|
if (hasWCol) {
|
|
|
|
mcol = CustomData_get(fdata, findex, CD_WEIGHT_MCOL);
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
for (j=0; j < mf_len; j++) {
|
2009-08-31 15:57:13 +00:00
|
|
|
mloopcol = CustomData_get(ldata, lindex[j], CD_WEIGHT_MLOOPCOL);
|
|
|
|
mcol[j].r = mloopcol->r;
|
|
|
|
mcol[j].g = mloopcol->g;
|
|
|
|
mcol[j].b = mloopcol->b;
|
|
|
|
mcol[j].a = mloopcol->a;
|
|
|
|
}
|
|
|
|
}
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 00:45:30 +00:00
|
|
|
/*
|
|
|
|
this function recreates a tesselation.
|
|
|
|
returns number of tesselation faces.
|
|
|
|
*/
|
2012-01-19 17:51:52 +00:00
|
|
|
int mesh_recalcTesselation(CustomData *fdata,
|
2009-09-01 16:05:33 +00:00
|
|
|
CustomData *ldata, CustomData *pdata,
|
2011-05-09 14:32:55 +00:00
|
|
|
MVert *mvert, int totface, int UNUSED(totloop),
|
2012-01-19 17:51:52 +00:00
|
|
|
int totpoly,
|
|
|
|
/* when teseelating to recalcilate normals after
|
|
|
|
* we can skip copying here */
|
|
|
|
const int do_face_nor_cpy)
|
2009-08-30 21:30:07 +00:00
|
|
|
{
|
2011-11-18 12:18:44 +00:00
|
|
|
|
|
|
|
/* use this to avoid locking pthread for _every_ polygon
|
|
|
|
* and calling the fill function */
|
|
|
|
|
|
|
|
#define USE_TESSFACE_SPEEDUP
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
MPoly *mp, *mpoly;
|
|
|
|
MLoop *ml, *mloop;
|
2011-11-18 12:18:44 +00:00
|
|
|
MFace *mface = NULL, *mf;
|
|
|
|
BLI_array_declare(mface);
|
2009-08-30 21:30:07 +00:00
|
|
|
EditVert *v, *lastv, *firstv;
|
|
|
|
EditFace *f;
|
2012-01-23 13:36:11 +00:00
|
|
|
int *mface_orig_index = NULL;
|
|
|
|
BLI_array_declare(mface_orig_index);
|
|
|
|
int *mface_to_poly_map = NULL;
|
|
|
|
BLI_array_declare(mface_to_poly_map);
|
2011-12-27 08:39:55 +00:00
|
|
|
int lindex[4]; /* only ever use 3 in this case */
|
2012-01-23 13:36:11 +00:00
|
|
|
int *poly_orig_index;
|
|
|
|
int poly_index, j, mface_index;
|
2011-12-28 22:37:09 +00:00
|
|
|
|
|
|
|
const int numTex = CustomData_number_of_layers(pdata, CD_MTEXPOLY);
|
|
|
|
const int numCol = CustomData_number_of_layers(ldata, CD_MLOOPCOL);
|
|
|
|
const int hasWCol = CustomData_has_layer(ldata, CD_WEIGHT_MLOOPCOL);
|
2009-08-30 21:30:07 +00:00
|
|
|
|
|
|
|
mpoly = CustomData_get_layer(pdata, CD_MPOLY);
|
|
|
|
mloop = CustomData_get_layer(ldata, CD_MLOOP);
|
|
|
|
|
2012-01-23 13:36:11 +00:00
|
|
|
/* allocate the length of totfaces, avoid many small reallocs,
|
|
|
|
* if all faces are tri's it will be correct, quads == 2x allocs */
|
|
|
|
BLI_array_reserve(mface_to_poly_map, totface);
|
|
|
|
BLI_array_reserve(mface, totface);
|
|
|
|
|
|
|
|
mface_index = 0;
|
2009-08-30 21:30:07 +00:00
|
|
|
mp = mpoly;
|
2012-01-23 13:36:11 +00:00
|
|
|
poly_orig_index = CustomData_get_layer(pdata, CD_ORIGINDEX);
|
|
|
|
for (poly_index = 0; poly_index < totpoly; poly_index++, mp++) {
|
2011-11-18 17:28:19 +00:00
|
|
|
if (mp->totloop < 3) {
|
2011-11-18 12:18:44 +00:00
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_TESSFACE_SPEEDUP
|
|
|
|
|
|
|
|
#define ML_TO_MF(i1, i2, i3) \
|
2012-01-23 13:36:11 +00:00
|
|
|
BLI_array_growone(mface_to_poly_map); \
|
2011-11-18 12:18:44 +00:00
|
|
|
BLI_array_growone(mface); \
|
2012-01-23 13:36:11 +00:00
|
|
|
mface_to_poly_map[mface_index] = poly_index; \
|
|
|
|
mf= &mface[mface_index]; \
|
2011-11-18 12:18:44 +00:00
|
|
|
/* set loop indices, transformed to vert indices later */ \
|
|
|
|
mf->v1 = mp->loopstart + i1; \
|
|
|
|
mf->v2 = mp->loopstart + i2; \
|
|
|
|
mf->v3 = mp->loopstart + i3; \
|
|
|
|
mf->v4 = 0; \
|
|
|
|
mf->mat_nr = mp->mat_nr; \
|
|
|
|
mf->flag = mp->flag; \
|
2012-01-23 13:36:11 +00:00
|
|
|
if (poly_orig_index) { \
|
|
|
|
BLI_array_append(mface_orig_index, \
|
|
|
|
poly_orig_index[poly_index]); \
|
2011-11-18 12:18:44 +00:00
|
|
|
} \
|
|
|
|
|
|
|
|
else if (mp->totloop == 3) {
|
|
|
|
ml = mloop + mp->loopstart;
|
|
|
|
ML_TO_MF(0, 1, 2)
|
2012-01-23 13:36:11 +00:00
|
|
|
mface_index++;
|
2011-11-18 12:18:44 +00:00
|
|
|
}
|
|
|
|
else if (mp->totloop == 4) {
|
|
|
|
ml = mloop + mp->loopstart;
|
|
|
|
ML_TO_MF(0, 1, 2)
|
2012-01-23 13:36:11 +00:00
|
|
|
mface_index++;
|
2011-11-18 12:18:44 +00:00
|
|
|
ML_TO_MF(0, 2, 3)
|
2012-01-23 13:36:11 +00:00
|
|
|
mface_index++;
|
2011-11-18 12:18:44 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_TESSFACE_SPEEDUP */
|
|
|
|
else {
|
2012-01-23 13:36:11 +00:00
|
|
|
int totfilltri;
|
|
|
|
|
2011-02-23 00:01:50 +00:00
|
|
|
ml = mloop + mp->loopstart;
|
2011-04-16 23:58:49 +00:00
|
|
|
|
|
|
|
BLI_begin_edgefill();
|
2010-11-10 18:42:53 +00:00
|
|
|
firstv = NULL;
|
|
|
|
lastv = NULL;
|
|
|
|
for (j=0; j<mp->totloop; j++, ml++) {
|
|
|
|
v = BLI_addfillvert(mvert[ml->v].co);
|
|
|
|
|
|
|
|
v->keyindex = mp->loopstart + j;
|
|
|
|
|
|
|
|
if (lastv)
|
|
|
|
BLI_addfilledge(lastv, v);
|
|
|
|
|
|
|
|
if (!firstv)
|
|
|
|
firstv = v;
|
|
|
|
lastv = v;
|
|
|
|
}
|
|
|
|
BLI_addfilledge(lastv, firstv);
|
|
|
|
|
2012-01-23 13:36:11 +00:00
|
|
|
totfilltri = BLI_edgefill(2);
|
|
|
|
if (totfilltri) {
|
|
|
|
BLI_array_growitems(mface_to_poly_map, totfilltri);
|
|
|
|
BLI_array_growitems(mface, totfilltri);
|
|
|
|
if (poly_orig_index) {
|
|
|
|
BLI_array_growitems(mface_orig_index, totfilltri);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (f = fillfacebase.first; f; f = f->next, mf++) {
|
|
|
|
mface_to_poly_map[mface_index] = poly_index;
|
|
|
|
mf= &mface[mface_index];
|
|
|
|
|
|
|
|
/* set loop indices, transformed to vert indices later */
|
|
|
|
mf->v1 = f->v1->keyindex;
|
|
|
|
mf->v2 = f->v2->keyindex;
|
|
|
|
mf->v3 = f->v3->keyindex;
|
|
|
|
mf->v4 = 0;
|
|
|
|
|
|
|
|
mf->mat_nr = mp->mat_nr;
|
|
|
|
mf->flag = mp->flag;
|
2011-11-13 15:13:59 +00:00
|
|
|
|
2011-12-29 03:24:28 +00:00
|
|
|
#ifdef USE_TESSFACE_SPEEDUP
|
2012-01-23 13:36:11 +00:00
|
|
|
mf->edcode = 1; /* tag for sorting loop indicies */
|
2011-12-29 03:24:28 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-23 13:36:11 +00:00
|
|
|
if (poly_orig_index) {
|
|
|
|
mface_orig_index[mface_index] = poly_orig_index[poly_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
mface_index++;
|
2011-11-13 15:13:59 +00:00
|
|
|
}
|
2010-11-10 18:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BLI_end_edgefill();
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomData_free(fdata, totface);
|
|
|
|
memset(fdata, 0, sizeof(CustomData));
|
2012-01-23 13:36:11 +00:00
|
|
|
totface = mface_index;
|
2009-08-30 21:30:07 +00:00
|
|
|
|
2011-11-18 12:18:44 +00:00
|
|
|
CustomData_add_layer(fdata, CD_MFACE, CD_ASSIGN, mface, totface);
|
2011-11-23 20:44:04 +00:00
|
|
|
|
|
|
|
/* CD_POLYINDEX will contain an array of indices from tessfaces to the polygons
|
2011-12-29 03:24:28 +00:00
|
|
|
* they are directly tesselated from */
|
2012-01-23 13:36:11 +00:00
|
|
|
CustomData_add_layer(fdata, CD_POLYINDEX, CD_ASSIGN, mface_to_poly_map, totface);
|
|
|
|
if (mface_orig_index) {
|
2011-11-23 20:44:04 +00:00
|
|
|
/* If polys had a CD_ORIGINDEX layer, then the tesselated faces will get this
|
2011-12-29 03:24:28 +00:00
|
|
|
* layer as well, pointing to polys from the original mesh (not the polys
|
|
|
|
* that just got tesselated) */
|
2012-01-23 13:36:11 +00:00
|
|
|
CustomData_add_layer(fdata, CD_ORIGINDEX, CD_ASSIGN, mface_orig_index, totface);
|
2011-11-13 15:13:59 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
CustomData_from_bmeshpoly(fdata, pdata, ldata, totface);
|
|
|
|
|
2012-01-19 17:51:52 +00:00
|
|
|
if (do_face_nor_cpy) {
|
|
|
|
/* If polys have a normals layer, copying that to faces can help
|
|
|
|
* avoid the need to recalculate normals later */
|
|
|
|
if (CustomData_has_layer(pdata, CD_NORMAL)) {
|
2012-01-19 22:04:13 +00:00
|
|
|
float (*pnors)[3] = CustomData_get_layer(pdata, CD_NORMAL);
|
|
|
|
float (*fnors)[3] = CustomData_add_layer(fdata, CD_NORMAL, CD_CALLOC, NULL, totface);
|
2012-01-23 13:36:11 +00:00
|
|
|
for (mface_index = 0; mface_index < totface; mface_index++) {
|
|
|
|
copy_v3_v3(fnors[mface_index], pnors[mface_to_poly_map[mface_index]]);
|
2012-01-19 17:51:52 +00:00
|
|
|
}
|
2011-11-13 15:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 12:18:44 +00:00
|
|
|
mf = mface;
|
2012-01-23 13:36:11 +00:00
|
|
|
for (mface_index=0; mface_index < totface; mface_index++, mf++) {
|
2011-12-29 03:24:28 +00:00
|
|
|
|
|
|
|
#ifdef USE_TESSFACE_SPEEDUP
|
|
|
|
/* skip sorting when not using ngons */
|
|
|
|
if (UNLIKELY(mf->edcode == 1))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* sort loop indices to ensure winding is correct */
|
|
|
|
if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
|
|
|
|
if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
|
|
|
|
if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
|
|
|
|
|
|
|
|
if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
|
|
|
|
if (mf->v2 > mf->v3) SWAP(int, mf->v2, mf->v3);
|
|
|
|
if (mf->v1 > mf->v2) SWAP(int, mf->v1, mf->v2);
|
|
|
|
|
|
|
|
#ifdef USE_TESSFACE_SPEEDUP
|
|
|
|
mf->edcode = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
lindex[0] = mf->v1;
|
|
|
|
lindex[1] = mf->v2;
|
|
|
|
lindex[2] = mf->v3;
|
|
|
|
|
|
|
|
/*transform loop indices to vert indices*/
|
|
|
|
mf->v1 = mloop[mf->v1].v;
|
|
|
|
mf->v2 = mloop[mf->v2].v;
|
|
|
|
mf->v3 = mloop[mf->v3].v;
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
mesh_loops_to_mface_corners(fdata, ldata, pdata,
|
2012-01-23 13:36:11 +00:00
|
|
|
lindex, mface_index, mface_to_poly_map[mface_index], 3,
|
2011-12-28 22:37:09 +00:00
|
|
|
numTex, numCol, hasWCol);
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return totface;
|
2011-11-18 16:06:20 +00:00
|
|
|
|
|
|
|
#undef USE_TESSFACE_SPEEDUP
|
|
|
|
|
2009-08-30 21:30:07 +00:00
|
|
|
}
|
2009-09-12 06:47:59 +00:00
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
|
2011-12-28 15:07:00 +00:00
|
|
|
#ifdef USE_BMESH_SAVE_AS_COMPAT
|
2011-12-27 08:39:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this function recreates a tesselation.
|
|
|
|
* returns number of tesselation faces.
|
|
|
|
*/
|
|
|
|
int mesh_mpoly_to_mface(struct CustomData *fdata, struct CustomData *ldata,
|
|
|
|
struct CustomData *pdata, int totface, int UNUSED(totloop), int totpoly)
|
|
|
|
{
|
|
|
|
MLoop *mloop;
|
|
|
|
|
|
|
|
int lindex[4];
|
|
|
|
int i;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
MPoly *mp, *mpoly;
|
|
|
|
MFace *mface = NULL, *mf;
|
|
|
|
BLI_array_declare(mface);
|
|
|
|
|
2011-12-28 22:37:09 +00:00
|
|
|
const int numTex = CustomData_number_of_layers(pdata, CD_MTEXPOLY);
|
|
|
|
const int numCol = CustomData_number_of_layers(ldata, CD_MLOOPCOL);
|
|
|
|
const int hasWCol = CustomData_has_layer(ldata, CD_WEIGHT_MLOOPCOL);
|
|
|
|
|
2011-12-27 08:39:55 +00:00
|
|
|
mpoly = CustomData_get_layer(pdata, CD_MPOLY);
|
|
|
|
mloop = CustomData_get_layer(ldata, CD_MLOOP);
|
|
|
|
|
|
|
|
mp = mpoly;
|
|
|
|
k = 0;
|
|
|
|
for (i = 0; i<totpoly; i++, mp++) {
|
|
|
|
if (ELEM(mp->totloop, 3, 4)) {
|
|
|
|
BLI_array_growone(mface);
|
|
|
|
mf = &mface[k];
|
|
|
|
|
|
|
|
mf->mat_nr = mp->mat_nr;
|
|
|
|
mf->flag = mp->flag;
|
|
|
|
|
|
|
|
mf->v1 = mp->loopstart + 0;
|
|
|
|
mf->v2 = mp->loopstart + 1;
|
|
|
|
mf->v3 = mp->loopstart + 2;
|
|
|
|
mf->v4 = (mp->totloop == 4) ? (mp->loopstart + 3) : 0;
|
|
|
|
|
|
|
|
/* abuse edcode for temp storage and clear next loop */
|
|
|
|
mf->edcode = (char)mp->totloop; /* only ever 3 or 4 */
|
|
|
|
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomData_free(fdata, totface);
|
|
|
|
memset(fdata, 0, sizeof(CustomData));
|
|
|
|
|
|
|
|
totface= k;
|
|
|
|
|
|
|
|
CustomData_add_layer(fdata, CD_MFACE, CD_ASSIGN, mface, totface);
|
|
|
|
|
|
|
|
CustomData_from_bmeshpoly(fdata, pdata, ldata, totface);
|
|
|
|
|
|
|
|
mp = mpoly;
|
|
|
|
k = 0;
|
|
|
|
for (i = 0; i<totpoly; i++, mp++) {
|
|
|
|
if (ELEM(mp->totloop, 3, 4)) {
|
|
|
|
mf = &mface[k];
|
|
|
|
|
|
|
|
if (mf->edcode == 3) {
|
|
|
|
/*sort loop indices to ensure winding is correct*/
|
|
|
|
/* NO SORT - looks like we can skip this */
|
|
|
|
|
|
|
|
lindex[0] = mf->v1;
|
|
|
|
lindex[1] = mf->v2;
|
|
|
|
lindex[2] = mf->v3;
|
|
|
|
lindex[3] = 0; /* unused */
|
|
|
|
|
|
|
|
/*transform loop indices to vert indices*/
|
|
|
|
mf->v1 = mloop[mf->v1].v;
|
|
|
|
mf->v2 = mloop[mf->v2].v;
|
|
|
|
mf->v3 = mloop[mf->v3].v;
|
|
|
|
|
|
|
|
mesh_loops_to_mface_corners(fdata, ldata, pdata,
|
2011-12-28 22:37:09 +00:00
|
|
|
lindex, k, i, 3,
|
|
|
|
numTex, numCol, hasWCol);
|
2012-01-17 04:54:57 +00:00
|
|
|
test_index_face(mf, fdata, k, 3);
|
2011-12-27 08:39:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*sort loop indices to ensure winding is correct*/
|
|
|
|
/* NO SORT - looks like we can skip this */
|
|
|
|
|
|
|
|
lindex[0] = mf->v1;
|
|
|
|
lindex[1] = mf->v2;
|
|
|
|
lindex[2] = mf->v3;
|
|
|
|
lindex[3] = mf->v4;
|
|
|
|
|
|
|
|
/*transform loop indices to vert indices*/
|
|
|
|
mf->v1 = mloop[mf->v1].v;
|
|
|
|
mf->v2 = mloop[mf->v2].v;
|
|
|
|
mf->v3 = mloop[mf->v3].v;
|
|
|
|
mf->v4 = mloop[mf->v4].v;
|
|
|
|
|
|
|
|
mesh_loops_to_mface_corners(fdata, ldata, pdata,
|
2011-12-28 22:37:09 +00:00
|
|
|
lindex, k, i, 4,
|
|
|
|
numTex, numCol, hasWCol);
|
2012-01-17 04:54:57 +00:00
|
|
|
test_index_face(mf, fdata, k, 4);
|
2011-12-27 08:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mf->edcode= 0;
|
|
|
|
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
2011-12-28 15:07:00 +00:00
|
|
|
#endif /* USE_BMESH_SAVE_AS_COMPAT */
|
2011-12-27 08:39:55 +00:00
|
|
|
|
2009-09-12 06:47:59 +00:00
|
|
|
/*
|
|
|
|
* COMPUTE POLY NORMAL
|
|
|
|
*
|
|
|
|
* Computes the normal of a planar
|
|
|
|
* polygon See Graphics Gems for
|
|
|
|
* computing newell normal.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void mesh_calc_ngon_normal(MPoly *mpoly, MLoop *loopstart,
|
2011-11-28 23:50:40 +00:00
|
|
|
MVert *mvert, float normal[3])
|
2009-09-12 06:47:59 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
MVert *v1, *v2, *v3;
|
2011-11-20 16:21:13 +00:00
|
|
|
double u[3], v[3], w[3];
|
2009-09-12 06:47:59 +00:00
|
|
|
double n[3] = {0.0, 0.0, 0.0}, l;
|
2011-05-13 13:17:30 +00:00
|
|
|
int i;
|
2009-09-12 06:47:59 +00:00
|
|
|
|
|
|
|
for(i = 0; i < mpoly->totloop; i++){
|
|
|
|
v1 = mvert + loopstart[i].v;
|
|
|
|
v2 = mvert + loopstart[(i+1)%mpoly->totloop].v;
|
|
|
|
v3 = mvert + loopstart[(i+2)%mpoly->totloop].v;
|
|
|
|
|
|
|
|
VECCOPY(u, v1->co);
|
|
|
|
VECCOPY(v, v2->co);
|
|
|
|
VECCOPY(w, v3->co);
|
|
|
|
|
|
|
|
/*this fixes some weird numerical error*/
|
|
|
|
if (i==0) {
|
|
|
|
u[0] += 0.0001f;
|
|
|
|
u[1] += 0.0001f;
|
|
|
|
u[2] += 0.0001f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* newell's method
|
|
|
|
|
|
|
|
so thats?:
|
|
|
|
(a[1] - b[1]) * (a[2] + b[2]);
|
|
|
|
a[1]*b[2] - b[1]*a[2] - b[1]*b[2] + a[1]*a[2]
|
|
|
|
|
|
|
|
odd. half of that is the cross product. . .what's the
|
|
|
|
other half?
|
|
|
|
|
|
|
|
also could be like a[1]*(b[2] + a[2]) - b[1]*(a[2] - b[2])
|
|
|
|
*/
|
|
|
|
|
|
|
|
n[0] += (u[1] - v[1]) * (u[2] + v[2]);
|
|
|
|
n[1] += (u[2] - v[2]) * (u[0] + v[0]);
|
|
|
|
n[2] += (u[0] - v[0]) * (u[1] + v[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
|
|
|
|
l = sqrt(l);
|
|
|
|
|
|
|
|
if (l == 0.0) {
|
|
|
|
normal[0] = 0.0f;
|
|
|
|
normal[1] = 0.0f;
|
|
|
|
normal[2] = 1.0f;
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else l = 1.0f / l;
|
|
|
|
|
|
|
|
n[0] *= l;
|
|
|
|
n[1] *= l;
|
|
|
|
n[2] *= l;
|
|
|
|
|
|
|
|
normal[0] = (float) n[0];
|
|
|
|
normal[1] = (float) n[1];
|
|
|
|
normal[2] = (float) n[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_calc_poly_normal(MPoly *mpoly, MLoop *loopstart,
|
2011-11-28 23:50:40 +00:00
|
|
|
MVert *mvarray, float no[3])
|
2009-09-12 06:47:59 +00:00
|
|
|
{
|
2011-11-28 23:50:40 +00:00
|
|
|
if (mpoly->totloop > 4) {
|
2009-09-12 06:47:59 +00:00
|
|
|
mesh_calc_ngon_normal(mpoly, loopstart, mvarray, no);
|
|
|
|
}
|
2011-11-28 23:50:40 +00:00
|
|
|
else if (mpoly->totloop == 3){
|
|
|
|
normal_tri_v3(no,
|
|
|
|
mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (mpoly->totloop == 4) {
|
|
|
|
normal_quad_v3(no,
|
|
|
|
mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co,
|
|
|
|
mvarray[loopstart[3].v].co
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else { /* horrible, two sided face! */
|
|
|
|
no[0] = 0.0;
|
|
|
|
no[1] = 0.0;
|
|
|
|
no[2] = 1.0;
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 14:42:11 +00:00
|
|
|
/* duplicate of function above _but_ takes coords rather then mverts */
|
|
|
|
static void mesh_calc_ngon_normal_coords(MPoly *mpoly, MLoop *loopstart,
|
|
|
|
const float (*vertex_coords)[3], float normal[3])
|
|
|
|
{
|
|
|
|
|
2012-01-06 00:08:37 +00:00
|
|
|
const float *v1, *v2, *v3;
|
2012-01-04 14:42:11 +00:00
|
|
|
double u[3], v[3], w[3];
|
|
|
|
double n[3] = {0.0, 0.0, 0.0}, l;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < mpoly->totloop; i++){
|
2012-01-06 00:08:37 +00:00
|
|
|
v1 = (const float *)(vertex_coords + loopstart[i].v);
|
|
|
|
v2 = (const float *)(vertex_coords + loopstart[(i+1)%mpoly->totloop].v);
|
|
|
|
v3 = (const float *)(vertex_coords + loopstart[(i+2)%mpoly->totloop].v);
|
2012-01-04 14:42:11 +00:00
|
|
|
|
|
|
|
VECCOPY(u, v1);
|
|
|
|
VECCOPY(v, v2);
|
|
|
|
VECCOPY(w, v3);
|
|
|
|
|
|
|
|
/*this fixes some weird numerical error*/
|
|
|
|
if (i==0) {
|
|
|
|
u[0] += 0.0001f;
|
|
|
|
u[1] += 0.0001f;
|
|
|
|
u[2] += 0.0001f;
|
|
|
|
}
|
|
|
|
|
|
|
|
n[0] += (u[1] - v[1]) * (u[2] + v[2]);
|
|
|
|
n[1] += (u[2] - v[2]) * (u[0] + v[0]);
|
|
|
|
n[2] += (u[0] - v[0]) * (u[1] + v[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
|
|
|
|
l = sqrt(l);
|
|
|
|
|
|
|
|
if (l == 0.0) {
|
|
|
|
normal[0] = 0.0f;
|
|
|
|
normal[1] = 0.0f;
|
|
|
|
normal[2] = 1.0f;
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else l = 1.0f / l;
|
|
|
|
|
|
|
|
n[0] *= l;
|
|
|
|
n[1] *= l;
|
|
|
|
n[2] *= l;
|
|
|
|
|
|
|
|
normal[0] = (float) n[0];
|
|
|
|
normal[1] = (float) n[1];
|
|
|
|
normal[2] = (float) n[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_calc_poly_normal_coords(MPoly *mpoly, MLoop *loopstart,
|
|
|
|
const float (*vertex_coords)[3], float no[3])
|
|
|
|
{
|
|
|
|
if (mpoly->totloop > 4) {
|
|
|
|
mesh_calc_ngon_normal_coords(mpoly, loopstart, vertex_coords, no);
|
|
|
|
}
|
|
|
|
else if (mpoly->totloop == 3){
|
|
|
|
normal_tri_v3(no,
|
|
|
|
vertex_coords[loopstart[0].v],
|
|
|
|
vertex_coords[loopstart[1].v],
|
|
|
|
vertex_coords[loopstart[2].v]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (mpoly->totloop == 4) {
|
|
|
|
normal_quad_v3(no,
|
|
|
|
vertex_coords[loopstart[0].v],
|
|
|
|
vertex_coords[loopstart[1].v],
|
|
|
|
vertex_coords[loopstart[2].v],
|
|
|
|
vertex_coords[loopstart[3].v]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else { /* horrible, two sided face! */
|
|
|
|
no[0] = 0.0;
|
|
|
|
no[1] = 0.0;
|
|
|
|
no[2] = 1.0;
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 23:50:40 +00:00
|
|
|
|
|
|
|
static void mesh_calc_ngon_center(MPoly *mpoly, MLoop *loopstart,
|
|
|
|
MVert *mvert, float cent[3])
|
|
|
|
{
|
2011-11-29 02:58:38 +00:00
|
|
|
const float w= 1.0f / (float)mpoly->totloop;
|
2011-11-28 23:50:40 +00:00
|
|
|
int i;
|
2009-09-12 06:47:59 +00:00
|
|
|
|
2011-11-28 23:50:40 +00:00
|
|
|
zero_v3(cent);
|
|
|
|
|
|
|
|
for (i = 0; i < mpoly->totloop; i++) {
|
2011-11-29 02:58:38 +00:00
|
|
|
madd_v3_v3fl(cent, mvert[(loopstart++)->v].co, w);
|
2009-09-12 06:47:59 +00:00
|
|
|
}
|
2011-11-28 23:50:40 +00:00
|
|
|
}
|
2009-09-12 06:47:59 +00:00
|
|
|
|
2011-11-28 23:50:40 +00:00
|
|
|
void mesh_calc_poly_center(MPoly *mpoly, MLoop *loopstart,
|
|
|
|
MVert *mvarray, float cent[3])
|
|
|
|
{
|
|
|
|
if (mpoly->totloop == 3) {
|
|
|
|
cent_tri_v3(cent,
|
|
|
|
mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (mpoly->totloop == 4) {
|
|
|
|
cent_quad_v3(cent,
|
|
|
|
mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co,
|
|
|
|
mvarray[loopstart[3].v].co
|
|
|
|
);
|
2009-09-12 06:47:59 +00:00
|
|
|
}
|
2011-11-28 23:50:40 +00:00
|
|
|
else {
|
|
|
|
mesh_calc_ngon_center(mpoly, loopstart, mvarray, cent);
|
2009-09-12 06:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-04 05:31:25 +00:00
|
|
|
|
2011-11-29 02:58:38 +00:00
|
|
|
/* note, passing polynormal is only a speedup so we can skip calculating it */
|
|
|
|
float mesh_calc_poly_area(MPoly *mpoly, MLoop *loopstart,
|
|
|
|
MVert *mvarray, float polynormal[3])
|
|
|
|
{
|
|
|
|
if (mpoly->totloop == 3) {
|
|
|
|
return area_tri_v3(mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else if (mpoly->totloop == 4) {
|
|
|
|
return area_quad_v3(mvarray[loopstart[0].v].co,
|
|
|
|
mvarray[loopstart[1].v].co,
|
|
|
|
mvarray[loopstart[2].v].co,
|
|
|
|
mvarray[loopstart[3].v].co
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i;
|
2011-11-29 06:06:59 +00:00
|
|
|
float area, polynorm_local[3], (*vertexcos)[3];
|
2011-11-29 02:58:38 +00:00
|
|
|
float *no= polynormal ? polynormal : polynorm_local;
|
2011-11-29 06:06:59 +00:00
|
|
|
BLI_array_fixedstack_declare(vertexcos, BM_NGON_STACK_SIZE, mpoly->totloop, __func__);
|
2011-11-29 02:58:38 +00:00
|
|
|
|
|
|
|
/* pack vertex cos into an array for area_poly_v3 */
|
|
|
|
for (i = 0; i < mpoly->totloop; i++) {
|
|
|
|
copy_v3_v3(vertexcos[i], mvarray[(loopstart++)->v].co);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need normal for area_poly_v3 as well */
|
|
|
|
if (polynormal == NULL) {
|
|
|
|
mesh_calc_poly_normal(mpoly, loopstart, mvarray, no);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finally calculate the area */
|
|
|
|
area = area_poly_v3(mpoly->totloop, vertexcos, no);
|
|
|
|
|
2011-11-29 06:06:59 +00:00
|
|
|
BLI_array_fixedstack_free(vertexcos);
|
2011-11-29 02:58:38 +00:00
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 11:00:36 +00:00
|
|
|
/* basic vertex data functions */
|
2010-08-03 00:56:43 +00:00
|
|
|
int minmax_mesh(Mesh *me, float min[3], float max[3])
|
2010-08-01 11:00:36 +00:00
|
|
|
{
|
|
|
|
int i= me->totvert;
|
|
|
|
MVert *mvert;
|
|
|
|
for(mvert= me->mvert; i--; mvert++) {
|
|
|
|
DO_MINMAX(mvert->co, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (me->totvert != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mesh_center_median(Mesh *me, float cent[3])
|
|
|
|
{
|
|
|
|
int i= me->totvert;
|
|
|
|
MVert *mvert;
|
|
|
|
zero_v3(cent);
|
|
|
|
for(mvert= me->mvert; i--; mvert++) {
|
|
|
|
add_v3_v3(cent, mvert->co);
|
|
|
|
}
|
2011-02-09 15:32:39 +00:00
|
|
|
/* otherwise we get NAN for 0 verts */
|
|
|
|
if(me->totvert) {
|
|
|
|
mul_v3_fl(cent, 1.0f/(float)me->totvert);
|
|
|
|
}
|
2010-08-01 11:00:36 +00:00
|
|
|
|
|
|
|
return (me->totvert != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mesh_center_bounds(Mesh *me, float cent[3])
|
|
|
|
{
|
|
|
|
float min[3], max[3];
|
2010-08-03 00:56:43 +00:00
|
|
|
INIT_MINMAX(min, max);
|
|
|
|
if(minmax_mesh(me, min, max)) {
|
2010-08-01 11:00:36 +00:00
|
|
|
mid_v3_v3v3(cent, min, max);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mesh_translate(Mesh *me, float offset[3], int do_keys)
|
|
|
|
{
|
|
|
|
int i= me->totvert;
|
|
|
|
MVert *mvert;
|
|
|
|
for(mvert= me->mvert; i--; mvert++) {
|
|
|
|
add_v3_v3(mvert->co, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-09 21:11:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_ensure_navmesh(Mesh *me)
|
|
|
|
{
|
|
|
|
if (!CustomData_has_layer(&me->fdata, CD_RECAST)) {
|
|
|
|
int i;
|
|
|
|
int numFaces = me->totface;
|
|
|
|
int* recastData;
|
|
|
|
CustomData_add_layer_named(&me->fdata, CD_RECAST, CD_CALLOC, NULL, numFaces, "recastData");
|
|
|
|
recastData = (int*)CustomData_get_layer(&me->fdata, CD_RECAST);
|
|
|
|
for (i=0; i<numFaces; i++) {
|
|
|
|
recastData[i] = i+1;
|
|
|
|
}
|
|
|
|
CustomData_add_layer_named(&me->fdata, CD_RECAST, CD_REFERENCE, recastData, numFaces, "recastData");
|
|
|
|
}
|
|
|
|
}
|