Commit: 4729279fd5d279de5a7bbcaf6d92112a5102a591 Author: Martin Felke Date: Fri Nov 16 10:32:29 2018 +0100 Branches: temp-fracture-modifier-2.8 https://developer.blender.org/rB4729279fd5d279de5a7bbcaf6d92112a5102a591
Merge remote-tracking branch 'origin/blender2.8' into fracture_modifier-2.8 # Conflicts: # release/scripts/startup/bl_operators/presets.py # source/blender/depsgraph/intern/builder/deg_builder_relations.cc =================================================================== =================================================================== diff --cc source/blender/blenkernel/intern/fracture.c index 07d051e73d0,00000000000..14e5ab73cac mode 100644,000000..100644 --- a/source/blender/blenkernel/intern/fracture.c +++ b/source/blender/blenkernel/intern/fracture.c @@@ -1,3397 -1,0 +1,3398 @@@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Copyright (C) 2014, 2018 by Martin Felke. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/blenkernel/intern/fracture.c + * \ingroup blenkernel + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "MEM_guardedalloc.h" + +#include "BKE_collection.h" +#include "BKE_customdata.h" +#include "BKE_deform.h" +#include "BKE_fracture.h" +#include "BKE_fracture_util.h" +#include "BKE_global.h" +#include "BKE_material.h" +#include "BKE_main.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object.h" +#include "BKE_particle.h" +#include "BKE_pointcache.h" +#include "BKE_rigidbody.h" + +#include "BLI_edgehash.h" +#include "BLI_kdtree.h" +#include "BLI_listbase.h" +#include "BLI_math_vector.h" +#include "BLI_mempool.h" +#include "BLI_path_util.h" +#include "BLI_rand.h" +#include "BLI_string.h" +#include "BLI_sort.h" +#include "BLI_task.h" +#include "BLI_utildefines.h" + +#include "DNA_scene_types.h" +#include "DNA_fracture_types.h" +#include "DNA_gpencil_types.h" +#include "DNA_collection_types.h" +#include "DNA_material_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_modifier_types.h" +#include "DNA_rigidbody_types.h" +#include "DNA_particle_types.h" + +#include "DNA_object_types.h" +#include "DEG_depsgraph_query.h" + +#include "bmesh.h" + +#include "RBI_api.h" + +/* debug timing */ +#define USE_DEBUG_TIMER + +#ifdef USE_DEBUG_TIMER +#include "PIL_time.h" +#endif + +#ifdef WITH_VORO +#include "../../../../extern/voro++/src/c_interface.hh" +#endif + +/* prototypes */ +static MeshIsland *parse_cell(cell c); +static void parse_cell_verts(cell c, MVert *mvert, int totvert); +static void parse_cell_polys(cell c, MPoly *mpoly, int totpoly); +static void parse_cell_loops(cell c, MLoop *mloop, MPoly *mpoly, int totpoly); +static void parse_cell_neighbors(cell c, int *neighbors, int totpoly); +static void do_island_index_map(FractureModifierData *fmd, Object *obj); +static void fracture_meshisland_custom(FractureModifierData *fmd, Object *obj, MeshIsland* mii, Main* bmain, Scene* scene, int frame, Depsgraph *depsgraph); +void BKE_fracture_postprocess_meshisland(FractureModifierData *fmd, Object* ob, MeshIsland*mi, Mesh** temp_meshs, int count, + Main* bmain, Scene* scene, int frame); + + +static void fracture_meshisland_add(FractureModifierData *fmd, MeshIsland *mi) +{ + MVert *mv; + int i; + + mul_m4_v3(fmd->shared->splinter_matrix, mi->centroid); + for (i = 0, mv = mi->mesh->mvert; i < mi->mesh->totvert; i++, mv++ ) + { + mul_m4_v3(fmd->shared->splinter_matrix, mv->co); + } + + BLI_addtail(&fmd->shared->mesh_islands, mi); +} + + +static int mesh_sortsize(const void *s1, const void *s2, void* UNUSED(context)) +{ + Mesh **me1 = (Mesh **)s1; + Mesh **me2 = (Mesh **)s2; + + float size1[3], size2[3], loc[3]; + float val_a, val_b; + + if ((*me1 == NULL) || (*me2 == NULL)) { + return -1; + } + + BKE_fracture_mesh_boundbox_calc(*me1, loc, size1); + BKE_fracture_mesh_boundbox_calc(*me2, loc, size2); + + //squared diameter + val_a = size1[0]*size1[0] + size1[1]*size1[1] + size1[2]*size1[2]; + val_b = size2[0]*size2[0] + size2[1]*size2[1] + size2[2]*size2[2]; + + /* sort */ + if (val_a < val_b) return -1; + else if (val_a > val_b) return 1; + return 0; +} + +#if 0 +/* copied from mesh_evaluate.c */ +/** + * Calculate the volume and volume-weighted centroid of the volume formed by the polygon and the origin. + * Results will be negative if the origin is "outside" the polygon + * (+ve normal side), but the polygon may be non-planar with no effect. + * + * Method from: + * - http://forums.cgsociety.org/archive/index.php?t-756235.html + * - http://www.globalspec.com/reference/52702/203279/4-8-the-centroid-of-a-tetrahedron + * + * \note volume is 6x actual volume, and centroid is 4x actual volume-weighted centroid + * (so division can be done once at the end) + * \note results will have bias if polygon is non-planar. + */ +static float mesh_calc_poly_volume_and_weighted_centroid( + const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray, + float r_cent[3]) +{ + const float *v_pivot, *v_step1; + float total_volume = 0.0f; + + zero_v3(r_cent); + + v_pivot = mvarray[loopstart[0].v].co; + v_step1 = mvarray[loopstart[1].v].co; + + for (int i = 2; i < mpoly->totloop; i++) { + const float *v_step2 = mvarray[loopstart[i].v].co; + + /* Calculate the 6x volume of the tetrahedron formed by the 3 vertices + * of the triangle and the origin as the fourth vertex */ + float v_cross[3]; + cross_v3_v3v3(v_cross, v_pivot, v_step1); + const float tetra_volume = dot_v3v3 (v_cross, v_step2); + total_volume += tetra_volume; + + /* Calculate the centroid of the tetrahedron formed by the 3 vertices + * of the triangle and the origin as the fourth vertex. + * The centroid is simply the average of the 4 vertices. + * + * Note that the vector is 4x the actual centroid so the division can be done once at the end. */ + for (uint j = 0; j < 3; j++) { + r_cent[j] += tetra_volume * (v_pivot[j] + v_step1[j] + v_step2[j]); + } + + v_step1 = v_step2; + } + + return total_volume; +} + +#endif + +/* note, results won't be correct if polygon is non-planar */ +/* copied from mesh_evaluate.c */ +static float mesh_calc_poly_planar_area_centroid( + const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray, + float r_cent[3]) +{ + int i; + float tri_area; + float total_area = 0.0f; + float v1[3], v2[3], v3[3], normal[3], tri_cent[3]; + + BKE_mesh_calc_poly_normal(mpoly, loopstart, mvarray, normal); + copy_v3_v3(v1, mvarray[loopstart[0].v].co); + copy_v3_v3(v2, mvarray[loopstart[1].v].co); + zero_v3(r_cent); + + for (i = 2; i < mpoly->totloop; i++) { + copy_v3_v3(v3, mvarray[loopstart[i].v].co); + + tri_area = area_tri_signed_v3(v1, v2, v3, normal); + total_area += tri_area; + + mid_v3_v3v3v3(tri_cent, v1, v2, v3); + madd_v3_v3fl(r_cent, tri_cent, tri_area); + + copy_v3_v3(v2, v3); + } + + mul_v3_fl(r_cent, 1.0f / total_area); + + return total_area; +} + +// old method, keep for now in case new has different results +/* modified from BKE_mesh_center_centroid */ +bool BKE_fracture_mesh_center_centroid_area(Mesh *shard, float cent[3]) +{ + int i = shard->totpoly; + MPoly *mpoly; + float poly_area; + float total_area = 0.0f; + float poly_cent[3]; + + zero_v3(cent); + + /* calculate a weighted average of polygon centroids */ + for (mpoly = shard->mpoly; i--; mpoly++) { + BKE_mesh_calc_poly_center(mpoly, shard->mloop + mpoly->loopstart, shard->mvert, poly_cent); +// poly_area = BKE_mesh_calc_poly_area(mpoly, shard->mloop + mpoly->loopstart, shard->mvert); + poly_area = mesh_calc_poly_planar_area_centroid(mpoly, shard->mloop + mpoly->loopstart, shard->mvert, + poly_cent); + madd_v3_v3fl(cent, poly_cent, poly_area); + total_area += poly_area; + } + /* otherwise we get NAN for 0 polys */ + if (shard->totpoly) { + mul_v3_fl(cent, 1.0f / total_area); + } + + /* zero area faces cause this, fallback to median */ + if (UNLIKELY(!is_finite_v3(cent))) { + return BKE_mesh_center_median(shard, cent); + } + + return (shard->totpoly != 0); +} + +static void calculate_fast_bisect(FractureModifierData *fmd, Mesh* me, BisectContext *ctx) +{ + float factor = 1 - fmd->orthogonality_factor; + float vec[3]; + float loc[3], size[3]; + int max_axis; + + BKE_fracture_mesh_boundbox_calc(me, loc, size); + + //make a random vector (interpret as cutter plane) + vec[0] = BLI_thread_frand(0) * 2 - 1; + vec[1] = BLI_thread_frand(0) * 2 - 1; + vec[2] = BLI_thread_frand(0) * 2 - 1; + + //multiply two minor dimensions with a factor to emphasize the max dimension + max_axis = axis_dominant_v3_single(size); + switch (max_axis) { + case 0: + vec[1] *= factor; + vec[2] *= factor; + break; + case 1: + vec[0] *= factor; + vec[2] *= factor; + break; + case 2: + vec[0] *= factor; + vec[1] *= factor; + break; + } + + copy_v3_v3(ctx->normal, vec); + BKE_fracture_mesh_center_centroid_area(me, ctx->centroid); +} + +static void calculate_fractal(FractureModifierData* fmd, Mesh* me, BooleanContext *ctx) +{ + float factor = 1 - fmd->orthogonality_factor; + float radius; + float size[3]; + float quat[4]; + float loc[3], vec[3]; + float one[3] = {1.0f, 1.0f, 1.0f}; + float matrix[4][4]; + int max_axis; + + BKE_fracture_mesh_boundbox_calc(me, loc, size); + radius = sqrt(size[0]*size[0] + size[1]*size[1] + size[2]*size[2]) * 1.5f; + + vec[0] = BLI_thread_frand(0) * 2 - 1; + vec[1] = BLI_thread_frand(0) * 2 - 1; + vec[2] = BLI_thread_frand(0) * 2 - 1; + + //multiply two minor dimensions with a factor to emphasize the max dimension + max_axis = axis_dominant_v3_single(size); + switch (max_axis) { + case 0: + vec[1] *= factor; + vec[2] *= factor; + break; + @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs