Welcome to mirror list, hosted at ThFree Co, Russian Federation.

remesh_blocks.c « intern « geometry « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9004bd292be499ffb043e1bbf19f977538656a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * 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.
 */

/** \file
 * \ingroup geo
 */

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_customdata_types.h"

#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"

#include "BLI_math_vector.h"
#include "BLI_threads.h"

#include "GEO_mesh_remesh_blocks.h" /* own include */

#include "MEM_guardedalloc.h"

#ifdef WITH_REMESH_DUALCON
#  include "dualcon.h"
#endif

static void init_dualcon_mesh(DualConInput *input, const Mesh *mesh)
{
  memset(input, 0, sizeof(DualConInput));

  input->co = (void *)mesh->mvert;
  input->co_stride = sizeof(MVert);
  input->totco = mesh->totvert;

  input->mloop = (void *)mesh->mloop;
  input->loop_stride = sizeof(MLoop);

  BKE_mesh_runtime_looptri_ensure(mesh);
  input->looptri = (void *)mesh->runtime.looptris.array;
  input->tri_stride = sizeof(MLoopTri);
  input->tottri = mesh->runtime.looptris.len;

  INIT_MINMAX(input->min, input->max);
  BKE_mesh_minmax(mesh, input->min, input->max);
}

/* Simple structure to hold the output: a CDDM and two counters to
 * keep track of the current elements. */
typedef struct {
  Mesh *mesh;
  int curvert, curface;
} DualConOutput;

/* Allocate and initialize a DualConOutput. */
static void *dualcon_alloc_output(int totvert, int totquad)
{
  DualConOutput *output;

  if (!(output = MEM_callocN(sizeof(DualConOutput), "DualConOutput"))) {
    return NULL;
  }

  output->mesh = BKE_mesh_new_nomain(totvert, 0, 0, 4 * totquad, totquad);
  return output;
}

static void dualcon_add_vert(void *output_v, const float co[3])
{
  DualConOutput *output = output_v;
  Mesh *mesh = output->mesh;

  BLI_assert(output->curvert < mesh->totvert);

  copy_v3_v3(mesh->mvert[output->curvert].co, co);
  output->curvert++;
}

static void dualcon_add_quad(void *output_v, const int vert_indices[4])
{
  DualConOutput *output = output_v;
  Mesh *mesh = output->mesh;
  MLoop *mloop;
  MPoly *cur_poly;
  int i;

  BLI_assert(output->curface < mesh->totpoly);

  mloop = mesh->mloop;
  cur_poly = &mesh->mpoly[output->curface];

  cur_poly->loopstart = output->curface * 4;
  cur_poly->totloop = 4;
  for (i = 0; i < 4; i++) {
    mloop[output->curface * 4 + i].v = vert_indices[i];
  }

  output->curface++;
}

Mesh *GEO_mesh_remesh_blocks(const Mesh *mesh,
                             const char remesh_flag,
                             const eRemeshBlocksMode remesh_mode,
                             const float threshold,
                             const int hermite_num,
                             const float scale,
                             const int depth)
{
#ifdef WITH_REMESH_DUALCON

  DualConOutput *output;
  DualConInput input;
  Mesh *result;
  DualConFlags flags = 0;
  DualConMode mode = 0;

  /* Dualcon modes. */
  init_dualcon_mesh(&input, mesh);

  if (remesh_flag & MOD_REMESH_FLOOD_FILL) {
    flags |= DUALCON_FLOOD_FILL;
  }

  switch (remesh_mode) {
    case REMESH_BLOCKS_CENTROID:
      mode = DUALCON_CENTROID;
      break;
    case REMESH_BLOCKS_MASS_POINT:
      mode = DUALCON_MASS_POINT;
      break;
    case REMESH_BLOCKS_SHARP_FEATURES:
      mode = DUALCON_SHARP_FEATURES;
      break;
  }
  /* TODO(jbakker): Dualcon crashes when run in parallel. Could be related to incorrect
   * input data or that the library isn't thread safe.
   * This was identified when changing the task isolation's during T76553. */
  static ThreadMutex dualcon_mutex = BLI_MUTEX_INITIALIZER;
  BLI_mutex_lock(&dualcon_mutex);
  output = dualcon(&input,
                   dualcon_alloc_output,
                   dualcon_add_vert,
                   dualcon_add_quad,
                   flags,
                   mode,
                   threshold,
                   hermite_num,
                   scale,
                   depth);
  BLI_mutex_unlock(&dualcon_mutex);

  result = output->mesh;
  MEM_freeN(output);

  return result;
#else
  return BKE_mesh_new_nomain(0,0,0,0,0);
#endif
}