static void face_duplilist(ListBase *lb, ID *id, Scene *scene, Object *par, float par_space_mat[][4], int level, int animated)
{
Object *ob, *ob_iter;
Base *base = NULL;
DupliObject *dob;
DerivedMesh *dm;
Mesh *me= par->data;
MTFace *mtface;
MFace *mface;
MVert *mvert;
float pmat[4][4], imat[3][3], (*orco)[3] = NULL, w;
int lay, oblay, totface, a;
Scene *sce = NULL;
Group *group = NULL;
GroupObject *go = NULL;
EditMesh *em;
float ob__obmat[4][4]; /* needed for groups where the object matrix needs to be modified */
/* simple preventing of too deep nested groups */
if(level>MAX_DUPLI_RECUR) return;
Mat4CpyMat4(pmat, par->obmat);
em = BKE_mesh_get_editmesh(me);
if(em) {
int totvert;
dm= editmesh_get_derived_cage(scene, par, em, CD_MASK_BAREMESH);
totface= dm->getNumFaces(dm);
mface= MEM_mallocN(sizeof(MFace)*totface, "mface temp");
dm->copyFaceArray(dm, mface);
totvert= dm->getNumVerts(dm);
mvert= MEM_mallocN(sizeof(MVert)*totvert, "mvert temp");
dm->copyVertArray(dm, mvert);
BKE_mesh_end_editmesh(me, em);
}
else {
dm = mesh_get_derived_deform(scene, par, CD_MASK_BAREMESH);
totface= dm->getNumFaces(dm);
mface= dm->getFaceArray(dm);
mvert= dm->getVertArray(dm);
}
if(G.rendering) {
orco= (float(*)[3])get_mesh_orco_verts(par);
transform_mesh_orco_verts(me, orco, me->totvert, 0);
mtface= me->mtface;
}
else {
orco= NULL;
mtface= NULL;
}
/* having to loop on scene OR group objects is NOT FUN */
if (GS(id->name) == ID_SCE) {
sce = (Scene *)id;
lay= sce->lay;
base= sce->base.first;
} else {
group = (Group *)id;
lay= group->layer;
go = group->gobject.first;
}
/* Start looping on Scene OR Group objects */
while (base || go) {
if (sce) {
ob_iter= base->object;
oblay = base->lay;
} else {
ob_iter= go->ob;
oblay = ob_iter->lay;
}
if (lay & oblay && scene->obedit!=ob_iter) {
ob=ob_iter->parent;
while(ob) {
if(ob==par) {
ob = ob_iter;
/* End Scene/Group object loop, below is generic */
/* par_space_mat - only used for groups so we can modify the space dupli's are in
when par_space_mat is NULL ob->obmat can be used instead of ob__obmat
*/
if(par_space_mat)
Mat4MulMat4(ob__obmat, ob->obmat, par_space_mat);
else
Mat4CpyMat4(ob__obmat, ob->obmat);
Mat3CpyMat4(imat, ob->parentinv);
/* mballs have a different dupli handling */
if(ob->type!=OB_MBALL) ob->flag |= OB_DONE; /* doesnt render */
for(a=0; a<totface; a++) {
int mv1 = mface[a].v1;
//.........这里部分代码省略.........
开发者ID:jinjoh,项目名称:NOOR,代码行数:101,代码来源:anim.c
示例2: poly_to_tri_count
/**
* This function populates an array of verts for the triangles of a mesh
* Tangent and Normals are also stored
*/
static TriTessFace *mesh_calc_tri_tessface(
Mesh *me, bool tangent, DerivedMesh *dm)
{
int i;
MVert *mvert;
TSpace *tspace;
float *precomputed_normals = NULL;
bool calculate_normal;
const int tottri = poly_to_tri_count(me->totpoly, me->totloop);
MLoopTri *looptri;
TriTessFace *triangles;
/* calculate normal for each polygon only once */
unsigned int mpoly_prev = UINT_MAX;
float no[3];
mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
looptri = MEM_mallocN(sizeof(*looptri) * tottri, __func__);
triangles = MEM_mallocN(sizeof(TriTessFace) * tottri, __func__);
if (tangent) {
DM_ensure_normals(dm);
DM_calc_loop_tangents(dm);
precomputed_normals = dm->getPolyDataArray(dm, CD_NORMAL);
calculate_normal = precomputed_normals ? false : true;
tspace = dm->getLoopDataArray(dm, CD_TANGENT);
BLI_assert(tspace);
}
BKE_mesh_recalc_looptri(
me->mloop, me->mpoly,
me->mvert,
me->totloop, me->totpoly,
looptri);
for (i = 0; i < tottri; i++) {
MLoopTri *lt = &looptri[i];
MPoly *mp = &me->mpoly[lt->poly];
triangles[i].mverts[0] = &mvert[me->mloop[lt->tri[0]].v];
triangles[i].mverts[1] = &mvert[me->mloop[lt->tri[1]].v];
triangles[i].mverts[2] = &mvert[me->mloop[lt->tri[2]].v];
triangles[i].is_smooth = (mp->flag & ME_SMOOTH) != 0;
if (tangent) {
triangles[i].tspace[0] = &tspace[lt->tri[0]];
triangles[i].tspace[1] = &tspace[lt->tri[1]];
triangles[i].tspace[2] = &tspace[lt->tri[2]];
if (calculate_normal) {
if (lt->poly != mpoly_prev) {
const MPoly *mp = &me->mpoly[lt->poly];
BKE_mesh_calc_poly_normal(mp, &me->mloop[mp->loopstart], me->mvert, no);
mpoly_prev = lt->poly;
}
copy_v3_v3(triangles[i].normal, no);
}
else {
copy_v3_v3(triangles[i].normal, &precomputed_normals[lt->poly]);
}
}
}
MEM_freeN(looptri);
return triangles;
}
/**
* Main boxpacking function accessed from other functions
* This sets boxes x,y to positive values, sorting from 0,0 outwards.
* There is no limit to the space boxes may take, only that they will be packed
* tightly into the lower left hand corner (0,0)
*
* \param boxarray: a pre allocated array of boxes.
* only the 'box->x' and 'box->y' are set, 'box->w' and 'box->h' are used,
* 'box->index' is not used at all, the only reason its there
* is that the box array is sorted by area and programs need to be able
* to have some way of writing the boxes back to the original data.
* \param len: the number of boxes in the array.
* \param r_tot_x, r_tot_y: set so you can normalize the data.
* */
void BLI_box_pack_2d(BoxPack *boxarray, const uint len, float *r_tot_x, float *r_tot_y)
{
uint box_index, verts_pack_len, i, j, k;
uint *vertex_pack_indices; /* an array of indices used for sorting verts */
bool isect;
float tot_x = 0.0f, tot_y = 0.0f;
BoxPack *box, *box_test; /*current box and another for intersection tests*/
BoxVert *vert; /* the current vert */
struct VertSortContext vs_ctx;
if (!len) {
*r_tot_x = tot_x;
*r_tot_y = tot_y;
return;
}
/* Sort boxes, biggest first */
qsort(boxarray, (size_t)len, sizeof(BoxPack), box_areasort);
/* add verts to the boxes, these are only used internally */
vert = MEM_mallocN((size_t)len * 4 * sizeof(BoxVert), "BoxPack Verts");
vertex_pack_indices = MEM_mallocN((size_t)len * 3 * sizeof(int), "BoxPack Indices");
vs_ctx.vertarray = vert;
for (box = boxarray, box_index = 0, i = 0; box_index < len; box_index++, box++) {
vert->blb = vert->brb = vert->tlb =
vert->isect_cache[0] = vert->isect_cache[1] =
vert->isect_cache[2] = vert->isect_cache[3] = NULL;
vert->free = CORNERFLAGS & ~TRF;
vert->trb = box;
vert->used = false;
vert->index = i++;
box->v[BL] = vert++;
vert->trb = vert->brb = vert->tlb =
vert->isect_cache[0] = vert->isect_cache[1] =
vert->isect_cache[2] = vert->isect_cache[3] = NULL;
vert->free = CORNERFLAGS & ~BLF;
vert->blb = box;
vert->used = false;
vert->index = i++;
box->v[TR] = vert++;
vert->trb = vert->blb = vert->tlb =
vert->isect_cache[0] = vert->isect_cache[1] =
vert->isect_cache[2] = vert->isect_cache[3] = NULL;
vert->free = CORNERFLAGS & ~BRF;
vert->brb = box;
vert->used = false;
vert->index = i++;
box->v[TL] = vert++;
vert->trb = vert->blb = vert->brb =
vert->isect_cache[0] = vert->isect_cache[1] =
vert->isect_cache[2] = vert->isect_cache[3] = NULL;
vert->free = CORNERFLAGS & ~TLF;
vert->tlb = box;
vert->used = false;
vert->index = i++;
box->v[BR] = vert++;
}
vert = NULL;
/* Pack the First box!
* then enter the main box-packing loop */
box = boxarray; /* get the first box */
/* First time, no boxes packed */
box->v[BL]->free = 0; /* Can't use any if these */
box->v[BR]->free &= ~(BLF | BRF);
box->v[TL]->free &= ~(BLF | TLF);
tot_x = box->w;
tot_y = box->h;
/* This sets all the vertex locations */
box_xmin_set(box, 0.0f);
box_ymin_set(box, 0.0f);
box->x = box->y = 0.0f;
for (i = 0; i < 4; i++) {
box->v[i]->used = true;
//.........这里部分代码省略.........
/**
* Take as inputs two sets of verts, to be processed for detection of doubles and mapping.
* Each set of verts is defined by its start within mverts array and its num_verts;
* It builds a mapping for all vertices within source, to vertices within target, or -1 if no double found
* The int doubles_map[num_verts_source] array must have been allocated by caller.
*/
static void dm_mvert_map_doubles(
int *doubles_map,
const MVert *mverts,
const int target_start,
const int target_num_verts,
const int source_start,
const int source_num_verts,
const float dist,
const bool with_follow)
{
const float dist3 = ((float)M_SQRT3 + 0.00005f) * dist; /* Just above sqrt(3) */
int i_source, i_target, i_target_low_bound, target_end, source_end;
SortVertsElem *sorted_verts_target, *sorted_verts_source;
SortVertsElem *sve_source, *sve_target, *sve_target_low_bound;
bool target_scan_completed;
target_end = target_start + target_num_verts;
source_end = source_start + source_num_verts;
/* build array of MVerts to be tested for merging */
sorted_verts_target = MEM_mallocN(sizeof(SortVertsElem) * target_num_verts, __func__);
sorted_verts_source = MEM_mallocN(sizeof(SortVertsElem) * source_num_verts, __func__);
/* Copy target vertices index and cos into SortVertsElem array */
svert_from_mvert(sorted_verts_target, mverts + target_start, target_start, target_end);
/* Copy source vertices index and cos into SortVertsElem array */
svert_from_mvert(sorted_verts_source, mverts + source_start, source_start, source_end);
/* sort arrays according to sum of vertex coordinates (sumco) */
qsort(sorted_verts_target, target_num_verts, sizeof(SortVertsElem), svert_sum_cmp);
qsort(sorted_verts_source, source_num_verts, sizeof(SortVertsElem), svert_sum_cmp);
sve_target_low_bound = sorted_verts_target;
i_target_low_bound = 0;
target_scan_completed = false;
/* Scan source vertices, in SortVertsElem sorted array, */
/* all the while maintaining the lower bound of possible doubles in target vertices */
for (i_source = 0, sve_source = sorted_verts_source;
i_source < source_num_verts;
i_source++, sve_source++)
{
bool double_found;
float sve_source_sumco;
/* If source has already been assigned to a target (in an earlier call, with other chunks) */
if (doubles_map[sve_source->vertex_num] != -1) {
continue;
}
/* If target fully scanned already, then all remaining source vertices cannot have a double */
if (target_scan_completed) {
doubles_map[sve_source->vertex_num] = -1;
continue;
}
sve_source_sumco = sum_v3(sve_source->co);
/* Skip all target vertices that are more than dist3 lower in terms of sumco */
/* and advance the overall lower bound, applicable to all remaining vertices as well. */
while ((i_target_low_bound < target_num_verts) &&
(sve_target_low_bound->sum_co < sve_source_sumco - dist3))
{
i_target_low_bound++;
sve_target_low_bound++;
}
/* If end of target list reached, then no more possible doubles */
if (i_target_low_bound >= target_num_verts) {
doubles_map[sve_source->vertex_num] = -1;
target_scan_completed = true;
continue;
}
/* Test target candidates starting at the low bound of possible doubles, ordered in terms of sumco */
i_target = i_target_low_bound;
sve_target = sve_target_low_bound;
/* i_target will scan vertices in the [v_source_sumco - dist3; v_source_sumco + dist3] range */
double_found = false;
while ((i_target < target_num_verts) &&
(sve_target->sum_co <= sve_source_sumco + dist3))
{
/* Testing distance for candidate double in target */
/* v_target is within dist3 of v_source in terms of sumco; check real distance */
if (compare_len_v3v3(sve_source->co, sve_target->co, dist)) {
/* Double found */
/* If double target is itself already mapped to other vertex,
* behavior depends on with_follow option */
int target_vertex = sve_target->vertex_num;
if (doubles_map[target_vertex] != -1) {
if (with_follow) { /* with_follow option: map to initial target */
target_vertex = doubles_map[target_vertex];
}
//.........这里部分代码省略.........
//.........这里部分代码省略.........
}
/* calculate the maximum number of copies which will fit within the
* prescribed length */
if (amd->fit_type == MOD_ARR_FITLENGTH || amd->fit_type == MOD_ARR_FITCURVE) {
float dist = len_v3(offset[3]);
if (dist > eps) {
/* this gives length = first copy start to last copy end
* add a tiny offset for floating point rounding errors */
count = (length + eps) / dist;
}
else {
/* if the offset has no translation, just make one copy */
count = 1;
}
}
if (count < 1)
count = 1;
/* The number of verts, edges, loops, polys, before eventually merging doubles */
result_nverts = chunk_nverts * count + start_cap_nverts + end_cap_nverts;
result_nedges = chunk_nedges * count + start_cap_nedges + end_cap_nedges;
result_nloops = chunk_nloops * count + start_cap_nloops + end_cap_nloops;
result_npolys = chunk_npolys * count + start_cap_npolys + end_cap_npolys;
/* Initialize a result dm */
result = CDDM_from_template(dm, result_nverts, result_nedges, 0, result_nloops, result_npolys);
result_dm_verts = CDDM_get_verts(result);
if (use_merge) {
/* Will need full_doubles_map for handling merge */
full_doubles_map = MEM_mallocN(sizeof(int) * result_nverts, "mod array doubles map");
fill_vn_i(full_doubles_map, result_nverts, -1);
}
/* copy customdata to original geometry */
DM_copy_vert_data(dm, result, 0, 0, chunk_nverts);
DM_copy_edge_data(dm, result, 0, 0, chunk_nedges);
DM_copy_loop_data(dm, result, 0, 0, chunk_nloops);
DM_copy_poly_data(dm, result, 0, 0, chunk_npolys);
/* subsurf for eg wont have mesh data in the
* now add mvert/medge/mface layers */
if (!CustomData_has_layer(&dm->vertData, CD_MVERT)) {
dm->copyVertArray(dm, result_dm_verts);
}
if (!CustomData_has_layer(&dm->edgeData, CD_MEDGE)) {
dm->copyEdgeArray(dm, CDDM_get_edges(result));
}
if (!CustomData_has_layer(&dm->polyData, CD_MPOLY)) {
dm->copyLoopArray(dm, CDDM_get_loops(result));
dm->copyPolyArray(dm, CDDM_get_polys(result));
}
/* Remember first chunk, in case of cap merge */
first_chunk_start = 0;
first_chunk_nverts = chunk_nverts;
unit_m4(current_offset);
for (c = 1; c < count; c++) {
/* copy customdata to new geometry */
DM_copy_vert_data(result, result, 0, c * chunk_nverts, chunk_nverts);
DM_copy_edge_data(result, result, 0, c * chunk_nedges, chunk_nedges);
请发表评论