OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * $Id$ |
| 3 * |
| 4 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 5 * |
| 6 * This program is free software; you can redistribute it and/or |
| 7 * modify it under the terms of the GNU General Public License |
| 8 * as published by the Free Software Foundation; either version 2 |
| 9 * of the License, or (at your option) any later version. |
| 10 * |
| 11 * This program is distributed in the hope that it will be useful, |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 * GNU General Public License for more details. |
| 15 * |
| 16 * You should have received a copy of the GNU General Public License |
| 17 * along with this program; if not, write to the Free Software Foundation, |
| 18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 * |
| 20 * The Original Code is Copyright (C) 2011 by Nicholas Bishop. |
| 21 * |
| 22 * ***** END GPL LICENSE BLOCK ***** |
| 23 * |
| 24 */ |
| 25 |
| 26 /** \file blender/modifiers/intern/MOD_remesh.c |
| 27 * \ingroup modifiers |
| 28 */ |
| 29 |
| 30 #include "MEM_guardedalloc.h" |
| 31 |
| 32 #include "BLI_math_vector.h" |
| 33 #include "BLI_utildefines.h" |
| 34 |
| 35 #include "BKE_cdderivedmesh.h" |
| 36 #include "BKE_DerivedMesh.h" |
| 37 #include "BKE_mesh.h" |
| 38 |
| 39 #include "DNA_meshdata_types.h" |
| 40 #include "DNA_modifier_types.h" |
| 41 #include "DNA_object_types.h" |
| 42 |
| 43 #include "MOD_modifiertypes.h" |
| 44 |
| 45 #include <assert.h> |
| 46 #include <stdlib.h> |
| 47 #include <string.h> |
| 48 |
| 49 #include "dualcon.h" |
| 50 |
| 51 static void initData(ModifierData *md) |
| 52 { |
| 53 RemeshModifierData *rmd = (RemeshModifierData*) md; |
| 54 |
| 55 rmd->scale = 0.9; |
| 56 rmd->depth = 4; |
| 57 rmd->hermite_num = 1; |
| 58 rmd->flag = MOD_REMESH_FLOOD_FILL; |
| 59 rmd->mode = MOD_REMESH_SHARP_FEATURES; |
| 60 rmd->threshold = 1; |
| 61 } |
| 62 |
| 63 static void copyData(ModifierData *md, ModifierData *target) |
| 64 { |
| 65 RemeshModifierData *rmd = (RemeshModifierData*) md; |
| 66 RemeshModifierData *trmd = (RemeshModifierData*) target; |
| 67 |
| 68 trmd->threshold = rmd->threshold; |
| 69 trmd->scale = rmd->scale; |
| 70 trmd->hermite_num = rmd->hermite_num; |
| 71 trmd->depth = rmd->depth; |
| 72 trmd->flag = rmd->flag; |
| 73 trmd->mode = rmd->mode; |
| 74 } |
| 75 |
| 76 void init_dualcon_mesh(DualConInput *mesh, DerivedMesh *dm) |
| 77 { |
| 78 memset(mesh, 0, sizeof(DualConInput)); |
| 79 |
| 80 mesh->co = (void*)dm->getVertArray(dm); |
| 81 mesh->co_stride = sizeof(MVert); |
| 82 mesh->totco = dm->getNumVerts(dm); |
| 83 |
| 84 mesh->faces = (void*)dm->getFaceArray(dm); |
| 85 mesh->face_stride = sizeof(MFace); |
| 86 mesh->totface = dm->getNumFaces(dm); |
| 87 |
| 88 dm->getMinMax(dm, mesh->min, mesh->max); |
| 89 } |
| 90 |
| 91 /* simple structure to hold the output: a CDDM and two counters to |
| 92 keep track of the current elements */ |
| 93 typedef struct { |
| 94 DerivedMesh *dm; |
| 95 int curvert, curface; |
| 96 } DualConOutput; |
| 97 |
| 98 /* allocate and initialize a DualConOutput */ |
| 99 void *dualcon_alloc_output(int totvert, int totquad) |
| 100 { |
| 101 DualConOutput *output; |
| 102 |
| 103 if(!(output = MEM_callocN(sizeof(DualConOutput), |
| 104 "DualConOutput"))) |
| 105 return NULL; |
| 106 ········ |
| 107 output->dm = CDDM_new(totvert, 0, totquad); |
| 108 return output; |
| 109 } |
| 110 |
| 111 void dualcon_add_vert(void *output_v, const float co[3]) |
| 112 { |
| 113 DualConOutput *output = output_v; |
| 114 DerivedMesh *dm = output->dm; |
| 115 ········ |
| 116 assert(output->curvert < dm->getNumVerts(dm)); |
| 117 ········ |
| 118 copy_v3_v3(CDDM_get_verts(dm)[output->curvert].co, co); |
| 119 output->curvert++; |
| 120 } |
| 121 |
| 122 void dualcon_add_quad(void *output_v, const int vert_indices[4]) |
| 123 { |
| 124 DualConOutput *output = output_v; |
| 125 DerivedMesh *dm = output->dm; |
| 126 MFace *mface; |
| 127 ········ |
| 128 assert(output->curface < dm->getNumFaces(dm)); |
| 129 |
| 130 mface = &CDDM_get_faces(dm)[output->curface]; |
| 131 mface->v1 = vert_indices[0]; |
| 132 mface->v2 = vert_indices[1]; |
| 133 mface->v3 = vert_indices[2]; |
| 134 mface->v4 = vert_indices[3]; |
| 135 ········ |
| 136 if(test_index_face(mface, NULL, 0, 4)) |
| 137 output->curface++; |
| 138 } |
| 139 |
| 140 static DerivedMesh *applyModifier(ModifierData *md, |
| 141 Object *UNUSED
(ob), |
| 142 DerivedMesh *d
m, |
| 143 int UNUSED(use
RenderParams), |
| 144 int UNUSED(isF
inalCalc)) |
| 145 { |
| 146 RemeshModifierData *rmd; |
| 147 DualConOutput *output; |
| 148 DualConInput input; |
| 149 DerivedMesh *result; |
| 150 DualConFlags flags = 0; |
| 151 DualConMode mode; |
| 152 |
| 153 rmd = (RemeshModifierData*)md; |
| 154 |
| 155 init_dualcon_mesh(&input, dm); |
| 156 |
| 157 if(rmd->flag & MOD_REMESH_FLOOD_FILL) |
| 158 flags |= DUALCON_FLOOD_FILL; |
| 159 |
| 160 switch(rmd->mode) { |
| 161 case MOD_REMESH_CENTROID: |
| 162 mode = DUALCON_CENTROID; |
| 163 break; |
| 164 case MOD_REMESH_MASS_POINT: |
| 165 mode = DUALCON_MASS_POINT; |
| 166 break; |
| 167 case MOD_REMESH_SHARP_FEATURES: |
| 168 mode = DUALCON_SHARP_FEATURES; |
| 169 break; |
| 170 } |
| 171 ········ |
| 172 output = dualcon(&input, |
| 173 dualcon_alloc_output, |
| 174 dualcon_add_vert, |
| 175 dualcon_add_quad, |
| 176 flags, |
| 177 mode, |
| 178 rmd->threshold, |
| 179 rmd->hermite_num, |
| 180 rmd->scale, |
| 181 rmd->depth); |
| 182 result = output->dm; |
| 183 CDDM_lower_num_faces(result, output->curface); |
| 184 MEM_freeN(output); |
| 185 |
| 186 CDDM_calc_edges(result); |
| 187 CDDM_calc_normals(result); |
| 188 |
| 189 return result; |
| 190 } |
| 191 |
| 192 ModifierTypeInfo modifierType_Remesh = { |
| 193 /* name */ "Remesh", |
| 194 /* structName */ "RemeshModifierData", |
| 195 /* structSize */ sizeof(RemeshModifierData), |
| 196 /* type */ eModifierTypeType_Nonconstructive, |
| 197 /* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFla
g_SupportsEditmode, |
| 198 /* copyData */ copyData, |
| 199 /* deformVerts */ NULL, |
| 200 /* deformMatrices */ NULL, |
| 201 /* deformVertsEM */ NULL, |
| 202 /* deformMatricesEM */ NULL, |
| 203 /* applyModifier */ applyModifier, |
| 204 /* applyModifierEM */ NULL, |
| 205 /* initData */ initData, |
| 206 /* requiredDataMask */ NULL, |
| 207 /* freeData */ NULL, |
| 208 /* isDisabled */ NULL, |
| 209 /* updateDepgraph */ NULL, |
| 210 /* dependsOnTime */ NULL, |
| 211 /* dependsOnNormals */ NULL, |
| 212 /* foreachObjectLink */ NULL, |
| 213 /* foreachIDLink */ NULL, |
| 214 }; |
OLD | NEW |