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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
|
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2019 Blender Foundation. All rights reserved. */
/** \file
* \ingroup edobj
*/
#include <cctype>
#include <cfloat>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_utildefines.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_userdef_types.h"
#include "BLT_translation.h"
#include "BKE_context.h"
#include "BKE_customdata.h"
#include "BKE_global.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mirror.h"
#include "BKE_mesh_remesh_voxel.h"
#include "BKE_mesh_runtime.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_shrinkwrap.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"
#include "ED_mesh.h"
#include "ED_object.h"
#include "ED_screen.h"
#include "ED_sculpt.h"
#include "ED_space_api.h"
#include "ED_undo.h"
#include "ED_view3d.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_matrix.h"
#include "GPU_state.h"
#include "WM_api.h"
#include "WM_message.h"
#include "WM_toolsystem.h"
#include "WM_types.h"
#include "UI_interface.h"
#include "BLF_api.h"
#include "object_intern.h" /* own include */
/* TODO(sebpa): unstable, can lead to unrecoverable errors. */
// #define USE_MESH_CURVATURE
/* -------------------------------------------------------------------- */
/** \name Voxel Remesh Operator
* \{ */
static bool object_remesh_poll(bContext *C)
{
Object *ob = CTX_data_active_object(C);
if (ob == nullptr || ob->data == nullptr) {
return false;
}
if (ID_IS_LINKED(ob) || ID_IS_LINKED(ob->data) || ID_IS_OVERRIDE_LIBRARY(ob->data)) {
CTX_wm_operator_poll_msg_set(C, "The remesher cannot worked on linked or override data");
return false;
}
if (BKE_object_is_in_editmode(ob)) {
CTX_wm_operator_poll_msg_set(C, "The remesher cannot run from edit mode");
return false;
}
if (ob->mode == OB_MODE_SCULPT && ob->sculpt->bm) {
CTX_wm_operator_poll_msg_set(C, "The remesher cannot run with dyntopo activated");
return false;
}
if (BKE_modifiers_uses_multires(ob)) {
CTX_wm_operator_poll_msg_set(
C, "The remesher cannot run with a Multires modifier in the modifier stack");
return false;
}
return ED_operator_object_active_editable_mesh(C);
}
static int voxel_remesh_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_active_object(C);
Mesh *mesh = static_cast<Mesh *>(ob->data);
if (mesh->remesh_voxel_size <= 0.0f) {
BKE_report(op->reports, RPT_ERROR, "Voxel remesher cannot run with a voxel size of 0.0");
return OPERATOR_CANCELLED;
}
if (mesh->totpoly == 0) {
return OPERATOR_CANCELLED;
}
/* Output mesh will be all smooth or all flat shading. */
const bool smooth_normals = mesh->mpoly[0].flag & ME_SMOOTH;
float isovalue = 0.0f;
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME) {
isovalue = mesh->remesh_voxel_size * 0.3f;
}
Mesh *new_mesh = BKE_mesh_remesh_voxel(
mesh, mesh->remesh_voxel_size, mesh->remesh_voxel_adaptivity, isovalue);
if (!new_mesh) {
BKE_report(op->reports, RPT_ERROR, "Voxel remesher failed to create mesh");
return OPERATOR_CANCELLED;
}
if (ob->mode == OB_MODE_SCULPT) {
ED_sculpt_undo_geometry_begin(ob, op->type->name);
}
if (mesh->flag & ME_REMESH_FIX_POLES && mesh->remesh_voxel_adaptivity <= 0.0f) {
Mesh *mesh_fixed_poles = BKE_mesh_remesh_voxel_fix_poles(new_mesh);
BKE_id_free(nullptr, new_mesh);
new_mesh = mesh_fixed_poles;
}
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME || mesh->flag & ME_REMESH_REPROJECT_PAINT_MASK ||
mesh->flag & ME_REMESH_REPROJECT_SCULPT_FACE_SETS) {
BKE_mesh_runtime_clear_geometry(mesh);
}
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME) {
BKE_shrinkwrap_remesh_target_project(new_mesh, mesh, ob);
}
if (mesh->flag & ME_REMESH_REPROJECT_PAINT_MASK) {
BKE_mesh_remesh_reproject_paint_mask(new_mesh, mesh);
}
if (mesh->flag & ME_REMESH_REPROJECT_SCULPT_FACE_SETS) {
BKE_remesh_reproject_sculpt_face_sets(new_mesh, mesh);
}
if (mesh->flag & ME_REMESH_REPROJECT_VERTEX_COLORS) {
BKE_mesh_runtime_clear_geometry(mesh);
BKE_remesh_reproject_vertex_paint(new_mesh, mesh);
}
BKE_mesh_nomain_to_mesh(new_mesh, mesh, ob, &CD_MASK_MESH, true);
if (smooth_normals) {
BKE_mesh_smooth_flag_set(static_cast<Mesh *>(ob->data), true);
}
if (ob->mode == OB_MODE_SCULPT) {
BKE_sculpt_ensure_orig_mesh_data(CTX_data_scene(C), ob);
ED_sculpt_undo_geometry_end(ob);
}
BKE_mesh_batch_cache_dirty_tag(static_cast<Mesh *>(ob->data), BKE_MESH_BATCH_DIRTY_ALL);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
return OPERATOR_FINISHED;
}
void OBJECT_OT_voxel_remesh(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Voxel Remesh";
ot->description =
"Calculates a new manifold mesh based on the volume of the current mesh. All data layers "
"will be lost";
ot->idname = "OBJECT_OT_voxel_remesh";
/* api callbacks */
ot->poll = object_remesh_poll;
ot->exec = voxel_remesh_exec;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Voxel Size Operator
* \{ */
#define VOXEL_SIZE_EDIT_MAX_GRIDS_LINES 500
#define VOXEL_SIZE_EDIT_MAX_STR_LEN 20
struct VoxelSizeEditCustomData {
void *draw_handle;
Object *active_object;
float init_mval[2];
float slow_mval[2];
bool slow_mode;
float init_voxel_size;
float slow_voxel_size;
float voxel_size;
float preview_plane[4][3];
float text_mat[4][4];
};
static void voxel_size_parallel_lines_draw(uint pos3d,
const float initial_co[3],
const float end_co[3],
const float length_co[3],
const float spacing)
{
const float total_len = len_v3v3(initial_co, end_co);
const int tot_lines = (int)(total_len / spacing);
const int tot_lines_half = (tot_lines / 2) + 1;
float spacing_dir[3], lines_start[3];
float line_dir[3];
sub_v3_v3v3(spacing_dir, end_co, initial_co);
normalize_v3(spacing_dir);
sub_v3_v3v3(line_dir, length_co, initial_co);
if (tot_lines > VOXEL_SIZE_EDIT_MAX_GRIDS_LINES || tot_lines <= 1) {
return;
}
mid_v3_v3v3(lines_start, initial_co, end_co);
immBegin(GPU_PRIM_LINES, (uint)tot_lines_half * 2);
for (int i = 0; i < tot_lines_half; i++) {
float line_start[3];
float line_end[3];
madd_v3_v3v3fl(line_start, lines_start, spacing_dir, spacing * i);
add_v3_v3v3(line_end, line_start, line_dir);
immVertex3fv(pos3d, line_start);
immVertex3fv(pos3d, line_end);
}
immEnd();
mul_v3_fl(spacing_dir, -1.0f);
immBegin(GPU_PRIM_LINES, (uint)(tot_lines_half - 1) * 2);
for (int i = 1; i < tot_lines_half; i++) {
float line_start[3];
float line_end[3];
madd_v3_v3v3fl(line_start, lines_start, spacing_dir, spacing * i);
add_v3_v3v3(line_end, line_start, line_dir);
immVertex3fv(pos3d, line_start);
immVertex3fv(pos3d, line_end);
}
immEnd();
}
static void voxel_size_edit_draw(const bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg)
{
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(arg);
GPU_blend(GPU_BLEND_ALPHA);
GPU_line_smooth(true);
uint pos3d = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
GPU_matrix_push();
GPU_matrix_mul(cd->active_object->obmat);
/* Draw Rect */
immUniformColor4f(0.9f, 0.9f, 0.9f, 0.8f);
GPU_line_width(3.0f);
immBegin(GPU_PRIM_LINES, 8);
immVertex3fv(pos3d, cd->preview_plane[0]);
immVertex3fv(pos3d, cd->preview_plane[1]);
immVertex3fv(pos3d, cd->preview_plane[1]);
immVertex3fv(pos3d, cd->preview_plane[2]);
immVertex3fv(pos3d, cd->preview_plane[2]);
immVertex3fv(pos3d, cd->preview_plane[3]);
immVertex3fv(pos3d, cd->preview_plane[3]);
immVertex3fv(pos3d, cd->preview_plane[0]);
immEnd();
/* Draw Grid */
GPU_line_width(1.0f);
const float total_len = len_v3v3(cd->preview_plane[0], cd->preview_plane[1]);
const int tot_lines = (int)(total_len / cd->voxel_size);
/* Smooth-step to reduce the alpha of the grid as the line number increases. */
const float a = VOXEL_SIZE_EDIT_MAX_GRIDS_LINES * 0.1f;
const float b = VOXEL_SIZE_EDIT_MAX_GRIDS_LINES;
const float x = clamp_f((tot_lines - a) / (b - a), 0.0f, 1.0);
const float alpha_factor = 1.0f - (x * x * (3.0f - 2.0f * x));
immUniformColor4f(0.9f, 0.9f, 0.9f, 0.75f * alpha_factor);
voxel_size_parallel_lines_draw(
pos3d, cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[3], cd->voxel_size);
voxel_size_parallel_lines_draw(
pos3d, cd->preview_plane[1], cd->preview_plane[2], cd->preview_plane[0], cd->voxel_size);
/* Draw text */
const uiStyle *style = UI_style_get();
const uiFontStyle *fstyle = &style->widget;
const int fontid = fstyle->uifont_id;
float strwidth, strheight;
short fstyle_points = fstyle->points;
char str[VOXEL_SIZE_EDIT_MAX_STR_LEN];
short strdrawlen = 0;
BLI_snprintf(str, VOXEL_SIZE_EDIT_MAX_STR_LEN, "%.4f", cd->voxel_size);
strdrawlen = BLI_strlen_utf8(str);
immUnbindProgram();
GPU_matrix_push();
GPU_matrix_mul(cd->text_mat);
BLF_size(fontid, 10.0f * fstyle_points, U.dpi);
BLF_color3f(fontid, 1.0f, 1.0f, 1.0f);
BLF_width_and_height(fontid, str, strdrawlen, &strwidth, &strheight);
BLF_position(fontid, -0.5f * strwidth, -0.5f * strheight, 0.0f);
BLF_draw(fontid, str, strdrawlen);
GPU_matrix_pop();
GPU_matrix_pop();
GPU_blend(GPU_BLEND_NONE);
GPU_line_smooth(false);
}
static void voxel_size_edit_cancel(bContext *C, wmOperator *op)
{
ARegion *region = CTX_wm_region(C);
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(op->customdata);
ED_region_draw_cb_exit(region->type, cd->draw_handle);
MEM_freeN(op->customdata);
ED_workspace_status_text(C, nullptr);
}
static int voxel_size_edit_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
ARegion *region = CTX_wm_region(C);
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(op->customdata);
Object *active_object = cd->active_object;
Mesh *mesh = (Mesh *)active_object->data;
/* Cancel modal operator */
if ((event->type == EVT_ESCKEY && event->val == KM_PRESS) ||
(event->type == RIGHTMOUSE && event->val == KM_PRESS)) {
voxel_size_edit_cancel(C, op);
ED_region_tag_redraw(region);
return OPERATOR_FINISHED;
}
/* Finish modal operator */
if ((event->type == LEFTMOUSE && event->val == KM_RELEASE) ||
(event->type == EVT_RETKEY && event->val == KM_PRESS) ||
(event->type == EVT_PADENTER && event->val == KM_PRESS)) {
ED_region_draw_cb_exit(region->type, cd->draw_handle);
mesh->remesh_voxel_size = cd->voxel_size;
MEM_freeN(op->customdata);
ED_region_tag_redraw(region);
ED_workspace_status_text(C, nullptr);
return OPERATOR_FINISHED;
}
const float mval[2] = {float(event->mval[0]), float(event->mval[1])};
float d = cd->init_mval[0] - mval[0];
if (cd->slow_mode) {
d = cd->slow_mval[0] - mval[0];
}
if (event->modifier & KM_CTRL) {
/* Linear mode, enables jumping to any voxel size. */
d = d * 0.0005f;
}
else {
/* Multiply d by the initial voxel size to prevent uncontrollable speeds when using low voxel
* sizes. */
/* When the voxel size is slower, it needs more precision. */
d = d * min_ff(pow2f(cd->init_voxel_size), 0.1f) * 0.05f;
}
if (cd->slow_mode) {
cd->voxel_size = cd->slow_voxel_size + d * 0.05f;
}
else {
cd->voxel_size = cd->init_voxel_size + d;
}
if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_PRESS) {
cd->slow_mode = true;
copy_v2_v2(cd->slow_mval, mval);
cd->slow_voxel_size = cd->voxel_size;
}
if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_RELEASE) {
cd->slow_mode = false;
cd->slow_voxel_size = 0.0f;
}
cd->voxel_size = clamp_f(cd->voxel_size, 0.0001f, 1.0f);
ED_region_tag_redraw(region);
return OPERATOR_RUNNING_MODAL;
}
static int voxel_size_edit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
ARegion *region = CTX_wm_region(C);
Object *active_object = CTX_data_active_object(C);
Mesh *mesh = (Mesh *)active_object->data;
VoxelSizeEditCustomData *cd = MEM_cnew<VoxelSizeEditCustomData>(
"Voxel Size Edit OP Custom Data");
/* Initial operator Custom Data setup. */
cd->draw_handle = ED_region_draw_cb_activate(
region->type, voxel_size_edit_draw, cd, REGION_DRAW_POST_VIEW);
cd->active_object = active_object;
cd->init_mval[0] = event->mval[0];
cd->init_mval[1] = event->mval[1];
cd->init_voxel_size = mesh->remesh_voxel_size;
cd->voxel_size = mesh->remesh_voxel_size;
op->customdata = cd;
/* Select the front facing face of the mesh bounding box. */
const BoundBox *bb = BKE_mesh_boundbox_get(cd->active_object);
/* Indices of the Bounding Box faces. */
const int BB_faces[6][4] = {
{3, 0, 4, 7},
{1, 2, 6, 5},
{3, 2, 1, 0},
{4, 5, 6, 7},
{0, 1, 5, 4},
{2, 3, 7, 6},
};
copy_v3_v3(cd->preview_plane[0], bb->vec[BB_faces[0][0]]);
copy_v3_v3(cd->preview_plane[1], bb->vec[BB_faces[0][1]]);
copy_v3_v3(cd->preview_plane[2], bb->vec[BB_faces[0][2]]);
copy_v3_v3(cd->preview_plane[3], bb->vec[BB_faces[0][3]]);
RegionView3D *rv3d = CTX_wm_region_view3d(C);
float mat[3][3];
float current_normal[3];
float view_normal[3] = {0.0f, 0.0f, 1.0f};
/* Calculate the view normal. */
invert_m4_m4(active_object->imat, active_object->obmat);
copy_m3_m4(mat, rv3d->viewinv);
mul_m3_v3(mat, view_normal);
copy_m3_m4(mat, active_object->imat);
mul_m3_v3(mat, view_normal);
normalize_v3(view_normal);
normal_tri_v3(current_normal, cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[2]);
float min_dot = dot_v3v3(current_normal, view_normal);
float current_dot = 1;
/* Check if there is a face that is more aligned towards the view. */
for (int i = 0; i < 6; i++) {
normal_tri_v3(
current_normal, bb->vec[BB_faces[i][0]], bb->vec[BB_faces[i][1]], bb->vec[BB_faces[i][2]]);
current_dot = dot_v3v3(current_normal, view_normal);
if (current_dot < min_dot) {
min_dot = current_dot;
copy_v3_v3(cd->preview_plane[0], bb->vec[BB_faces[i][0]]);
copy_v3_v3(cd->preview_plane[1], bb->vec[BB_faces[i][1]]);
copy_v3_v3(cd->preview_plane[2], bb->vec[BB_faces[i][2]]);
copy_v3_v3(cd->preview_plane[3], bb->vec[BB_faces[i][3]]);
}
}
/* Matrix calculation to position the text in 3D space. */
float text_pos[3];
float scale_mat[4][4];
float d_a[3], d_b[3];
float d_a_proj[2], d_b_proj[2];
float preview_plane_proj[4][2];
const float y_axis_proj[2] = {0.0f, 1.0f};
mid_v3_v3v3(text_pos, cd->preview_plane[0], cd->preview_plane[2]);
/* Project the selected face in the previous step of the Bounding Box. */
for (int i = 0; i < 4; i++) {
float preview_plane_world_space[3];
mul_v3_m4v3(preview_plane_world_space, active_object->obmat, cd->preview_plane[i]);
ED_view3d_project_v2(region, preview_plane_world_space, preview_plane_proj[i]);
}
/* Get the initial X and Y axis of the basis from the edges of the Bounding Box face. */
sub_v3_v3v3(d_a, cd->preview_plane[1], cd->preview_plane[0]);
sub_v3_v3v3(d_b, cd->preview_plane[3], cd->preview_plane[0]);
normalize_v3(d_a);
normalize_v3(d_b);
/* Project the X and Y axis. */
sub_v2_v2v2(d_a_proj, preview_plane_proj[1], preview_plane_proj[0]);
sub_v2_v2v2(d_b_proj, preview_plane_proj[3], preview_plane_proj[0]);
normalize_v2(d_a_proj);
normalize_v2(d_b_proj);
unit_m4(cd->text_mat);
/* Select the axis that is aligned with the view Y axis to use it as the basis Y. */
if (fabsf(dot_v2v2(d_a_proj, y_axis_proj)) > fabsf(dot_v2v2(d_b_proj, y_axis_proj))) {
copy_v3_v3(cd->text_mat[0], d_b);
copy_v3_v3(cd->text_mat[1], d_a);
/* Flip the X and Y basis vectors to make sure they always point upwards and to the right. */
if (d_b_proj[0] < 0.0f) {
mul_v3_fl(cd->text_mat[0], -1.0f);
}
if (d_a_proj[1] < 0.0f) {
mul_v3_fl(cd->text_mat[1], -1.0f);
}
}
else {
copy_v3_v3(cd->text_mat[0], d_a);
copy_v3_v3(cd->text_mat[1], d_b);
if (d_a_proj[0] < 0.0f) {
mul_v3_fl(cd->text_mat[0], -1.0f);
}
if (d_b_proj[1] < 0.0f) {
mul_v3_fl(cd->text_mat[1], -1.0f);
}
}
/* Use the Bounding Box face normal as the basis Z. */
normal_tri_v3(cd->text_mat[2], cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[2]);
/* Write the text position into the matrix. */
copy_v3_v3(cd->text_mat[3], text_pos);
/* Scale the text. */
float text_pos_word_space[3];
mul_v3_m4v3(text_pos_word_space, active_object->obmat, text_pos);
const float pixelsize = ED_view3d_pixel_size(rv3d, text_pos_word_space);
scale_m4_fl(scale_mat, pixelsize * 0.5f);
mul_m4_m4_post(cd->text_mat, scale_mat);
WM_event_add_modal_handler(C, op);
ED_region_tag_redraw(region);
const char *status_str = TIP_(
"Move the mouse to change the voxel size. LMB: confirm size, ESC/RMB: cancel");
ED_workspace_status_text(C, status_str);
return OPERATOR_RUNNING_MODAL;
}
static bool voxel_size_edit_poll(bContext *C)
{
return CTX_wm_region_view3d(C) && object_remesh_poll(C);
}
void OBJECT_OT_voxel_size_edit(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Edit Voxel Size";
ot->description = "Modify the mesh voxel size interactively used in the voxel remesher";
ot->idname = "OBJECT_OT_voxel_size_edit";
/* api callbacks */
ot->poll = voxel_size_edit_poll;
ot->invoke = voxel_size_edit_invoke;
ot->modal = voxel_size_edit_modal;
ot->cancel = voxel_size_edit_cancel;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Quadriflow Remesh Operator
* \{ */
#define QUADRIFLOW_MIRROR_BISECT_TOLERANCE 0.005f
enum {
QUADRIFLOW_REMESH_RATIO = 1,
QUADRIFLOW_REMESH_EDGE_LENGTH,
QUADRIFLOW_REMESH_FACES,
};
enum eSymmetryAxes {
SYMMETRY_AXES_X = (1 << 0),
SYMMETRY_AXES_Y = (1 << 1),
SYMMETRY_AXES_Z = (1 << 2),
};
struct QuadriFlowJob {
/* from wmJob */
struct Object *owner;
short *stop, *do_update;
float *progress;
Scene *scene;
int target_faces;
int seed;
bool use_mesh_symmetry;
eSymmetryAxes symmetry_axes;
bool use_preserve_sharp;
bool use_preserve_boundary;
bool use_mesh_curvature;
bool preserve_paint_mask;
bool smooth_normals;
int success;
bool is_nonblocking_job;
};
static bool mesh_is_manifold_consistent(Mesh *mesh)
{
/* In this check we count boundary edges as manifold. Additionally, we also
* check that the direction of the faces are consistent and doesn't suddenly
* flip
*/
bool is_manifold_consistent = true;
const MLoop *mloop = mesh->mloop;
char *edge_faces = (char *)MEM_callocN(mesh->totedge * sizeof(char), "remesh_manifold_check");
int *edge_vert = (int *)MEM_malloc_arrayN(
mesh->totedge, sizeof(uint), "remesh_consistent_check");
for (uint i = 0; i < mesh->totedge; i++) {
edge_vert[i] = -1;
}
for (uint loop_idx = 0; loop_idx < mesh->totloop; loop_idx++) {
const MLoop *loop = &mloop[loop_idx];
edge_faces[loop->e] += 1;
if (edge_faces[loop->e] > 2) {
is_manifold_consistent = false;
break;
}
if (edge_vert[loop->e] == -1) {
edge_vert[loop->e] = loop->v;
}
else if (edge_vert[loop->e] == loop->v) {
/* Mesh has flips in the surface so it is non consistent */
is_manifold_consistent = false;
break;
}
}
if (is_manifold_consistent) {
for (uint i = 0; i < mesh->totedge; i++) {
/* Check for wire edges. */
if (edge_faces[i] == 0) {
is_manifold_consistent = false;
break;
}
/* Check for zero length edges */
MVert *v1 = &mesh->mvert[mesh->medge[i].v1];
MVert *v2 = &mesh->mvert[mesh->medge[i].v2];
if (compare_v3v3(v1->co, v2->co, 1e-4f)) {
is_manifold_consistent = false;
break;
}
}
}
MEM_freeN(edge_faces);
MEM_freeN(edge_vert);
return is_manifold_consistent;
}
static void quadriflow_free_job(void *customdata)
{
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
MEM_freeN(qj);
}
/* called by quadriflowjob, only to check job 'stop' value */
static int quadriflow_break_job(void *customdata)
{
QuadriFlowJob *qj = (QuadriFlowJob *)customdata;
// return *(qj->stop);
/* this is not nice yet, need to make the jobs list template better
* for identifying/acting upon various different jobs */
/* but for now we'll reuse the render break... */
bool should_break = (G.is_break);
if (should_break) {
qj->success = -1;
}
return should_break;
}
/** Called by ocean-bake, #wmJob sends notifier. */
static void quadriflow_update_job(void *customdata, float progress, int *cancel)
{
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
if (quadriflow_break_job(qj)) {
*cancel = 1;
}
else {
*cancel = 0;
}
*(qj->do_update) = true;
*(qj->progress) = progress;
}
static Mesh *remesh_symmetry_bisect(Mesh *mesh, eSymmetryAxes symmetry_axes)
{
MirrorModifierData mmd = {{nullptr}};
mmd.tolerance = QUADRIFLOW_MIRROR_BISECT_TOLERANCE;
Mesh *mesh_bisect, *mesh_bisect_temp;
mesh_bisect = BKE_mesh_copy_for_eval(mesh, false);
int axis;
float plane_co[3], plane_no[3];
zero_v3(plane_co);
for (char i = 0; i < 3; i++) {
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
if (symmetry_axes & symm_it) {
axis = i;
mmd.flag = 0;
mmd.flag &= MOD_MIR_BISECT_AXIS_X << i;
zero_v3(plane_no);
plane_no[axis] = -1.0f;
mesh_bisect_temp = mesh_bisect;
mesh_bisect = BKE_mesh_mirror_bisect_on_mirror_plane_for_modifier(
&mmd, mesh_bisect, axis, plane_co, plane_no);
if (mesh_bisect_temp != mesh_bisect) {
BKE_id_free(nullptr, mesh_bisect_temp);
}
}
}
BKE_id_free(nullptr, mesh);
return mesh_bisect;
}
static Mesh *remesh_symmetry_mirror(Object *ob, Mesh *mesh, eSymmetryAxes symmetry_axes)
{
MirrorModifierData mmd = {{nullptr}};
mmd.tolerance = QUADRIFLOW_MIRROR_BISECT_TOLERANCE;
Mesh *mesh_mirror, *mesh_mirror_temp;
mesh_mirror = mesh;
int axis;
for (char i = 0; i < 3; i++) {
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
if (symmetry_axes & symm_it) {
axis = i;
mmd.flag = 0;
mmd.flag &= MOD_MIR_AXIS_X << i;
mesh_mirror_temp = mesh_mirror;
mesh_mirror = BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(
&mmd, ob, mesh_mirror, axis, true);
if (mesh_mirror_temp != mesh_mirror) {
BKE_id_free(nullptr, mesh_mirror_temp);
}
}
}
return mesh_mirror;
}
static void quadriflow_start_job(void *customdata, short *stop, short *do_update, float *progress)
{
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
qj->stop = stop;
qj->do_update = do_update;
qj->progress = progress;
qj->success = 1;
if (qj->is_nonblocking_job) {
G.is_break = false; /* XXX shared with render - replace with job 'stop' switch */
}
Object *ob = qj->owner;
Mesh *mesh = static_cast<Mesh *>(ob->data);
Mesh *new_mesh;
Mesh *bisect_mesh;
/* Check if the mesh is manifold. Quadriflow requires manifold meshes */
if (!mesh_is_manifold_consistent(mesh)) {
qj->success = -2;
return;
}
/* Run Quadriflow bisect operations on a copy of the mesh to keep the code readable without
* freeing the original ID */
bisect_mesh = BKE_mesh_copy_for_eval(mesh, false);
/* Bisect the input mesh using the paint symmetry settings */
bisect_mesh = remesh_symmetry_bisect(bisect_mesh, qj->symmetry_axes);
new_mesh = BKE_mesh_remesh_quadriflow(bisect_mesh,
qj->target_faces,
qj->seed,
qj->use_preserve_sharp,
(qj->use_preserve_boundary || qj->use_mesh_symmetry),
#ifdef USE_MESH_CURVATURE
qj->use_mesh_curvature,
#else
false,
#endif
quadriflow_update_job,
(void *)qj);
BKE_id_free(nullptr, bisect_mesh);
if (new_mesh == nullptr) {
*do_update = true;
*stop = 0;
if (qj->success == 1) {
/* This is not a user cancellation event. */
qj->success = 0;
}
return;
}
/* Mirror the Quadriflow result to build the final mesh */
new_mesh = remesh_symmetry_mirror(qj->owner, new_mesh, qj->symmetry_axes);
if (ob->mode == OB_MODE_SCULPT) {
ED_sculpt_undo_geometry_begin(ob, "QuadriFlow Remesh");
}
if (qj->preserve_paint_mask) {
BKE_mesh_runtime_clear_geometry(mesh);
BKE_mesh_remesh_reproject_paint_mask(new_mesh, mesh);
}
BKE_mesh_nomain_to_mesh(new_mesh, mesh, ob, &CD_MASK_MESH, true);
if (qj->smooth_normals) {
BKE_mesh_smooth_flag_set(static_cast<Mesh *>(ob->data), true);
}
if (ob->mode == OB_MODE_SCULPT) {
BKE_sculpt_ensure_orig_mesh_data(qj->scene, ob);
ED_sculpt_undo_geometry_end(ob);
}
BKE_mesh_batch_cache_dirty_tag(static_cast<Mesh *>(ob->data), BKE_MESH_BATCH_DIRTY_ALL);
*do_update = true;
*stop = 0;
}
static void quadriflow_end_job(void *customdata)
{
QuadriFlowJob *qj = (QuadriFlowJob *)customdata;
Object *ob = qj->owner;
if (qj->is_nonblocking_job) {
WM_set_locked_interface(static_cast<wmWindowManager *>(G_MAIN->wm.first), false);
}
switch (qj->success) {
case 1:
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_reportf(RPT_INFO, "QuadriFlow: Remeshing completed");
break;
case 0:
WM_reportf(RPT_ERROR, "QuadriFlow: Remeshing failed");
break;
case -1:
WM_report(RPT_WARNING, "QuadriFlow: Remeshing cancelled");
break;
case -2:
WM_report(RPT_WARNING,
"QuadriFlow: The mesh needs to be manifold and have face normals that point in a "
"consistent direction");
break;
}
}
static int quadriflow_remesh_exec(bContext *C, wmOperator *op)
{
QuadriFlowJob *job = (QuadriFlowJob *)MEM_mallocN(sizeof(QuadriFlowJob), "QuadriFlowJob");
job->owner = CTX_data_active_object(C);
job->scene = CTX_data_scene(C);
job->target_faces = RNA_int_get(op->ptr, "target_faces");
job->seed = RNA_int_get(op->ptr, "seed");
job->use_mesh_symmetry = RNA_boolean_get(op->ptr, "use_mesh_symmetry");
job->use_preserve_sharp = RNA_boolean_get(op->ptr, "use_preserve_sharp");
job->use_preserve_boundary = RNA_boolean_get(op->ptr, "use_preserve_boundary");
#ifdef USE_MESH_CURVATURE
job->use_mesh_curvature = RNA_boolean_get(op->ptr, "use_mesh_curvature");
#endif
job->preserve_paint_mask = RNA_boolean_get(op->ptr, "preserve_paint_mask");
job->smooth_normals = RNA_boolean_get(op->ptr, "smooth_normals");
/* Update the target face count if symmetry is enabled */
Object *ob = CTX_data_active_object(C);
if (ob && job->use_mesh_symmetry) {
Mesh *mesh = BKE_mesh_from_object(ob);
job->symmetry_axes = (eSymmetryAxes)mesh->symmetry;
for (char i = 0; i < 3; i++) {
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
if (job->symmetry_axes & symm_it) {
job->target_faces = job->target_faces / 2;
}
}
}
else {
job->use_mesh_symmetry = false;
job->symmetry_axes = (eSymmetryAxes)0;
}
if (op->flag == 0) {
/* This is called directly from the exec operator, this operation is now blocking */
job->is_nonblocking_job = false;
short stop = 0, do_update = true;
float progress;
quadriflow_start_job(job, &stop, &do_update, &progress);
quadriflow_end_job(job);
quadriflow_free_job(job);
}
else {
/* Non blocking call. For when the operator has been called from the GUI. */
job->is_nonblocking_job = true;
wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
CTX_wm_window(C),
CTX_data_scene(C),
"QuadriFlow Remesh",
WM_JOB_PROGRESS,
WM_JOB_TYPE_QUADRIFLOW_REMESH);
WM_jobs_customdata_set(wm_job, job, quadriflow_free_job);
WM_jobs_timer(wm_job, 0.1, NC_GEOM | ND_DATA, NC_GEOM | ND_DATA);
WM_jobs_callbacks(wm_job, quadriflow_start_job, nullptr, nullptr, quadriflow_end_job);
WM_set_locked_interface(CTX_wm_manager(C), true);
WM_jobs_start(CTX_wm_manager(C), wm_job);
}
return OPERATOR_FINISHED;
}
static bool quadriflow_check(bContext *C, wmOperator *op)
{
int mode = RNA_enum_get(op->ptr, "mode");
if (mode == QUADRIFLOW_REMESH_EDGE_LENGTH) {
float area = RNA_float_get(op->ptr, "mesh_area");
if (area < 0.0f) {
Object *ob = CTX_data_active_object(C);
area = BKE_mesh_calc_area(static_cast<const Mesh *>(ob->data));
RNA_float_set(op->ptr, "mesh_area", area);
}
int num_faces;
float edge_len = RNA_float_get(op->ptr, "target_edge_length");
num_faces = area / (edge_len * edge_len);
RNA_int_set(op->ptr, "target_faces", num_faces);
}
else if (mode == QUADRIFLOW_REMESH_RATIO) {
Object *ob = CTX_data_active_object(C);
Mesh *mesh = static_cast<Mesh *>(ob->data);
int num_faces;
float ratio = RNA_float_get(op->ptr, "target_ratio");
num_faces = mesh->totpoly * ratio;
RNA_int_set(op->ptr, "target_faces", num_faces);
}
return true;
}
/* Hide the target variables if they are not active */
static bool quadriflow_poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop)
{
const char *prop_id = RNA_property_identifier(prop);
if (STRPREFIX(prop_id, "target")) {
int mode = RNA_enum_get(op->ptr, "mode");
if (STREQ(prop_id, "target_edge_length") && mode != QUADRIFLOW_REMESH_EDGE_LENGTH) {
return false;
}
if (STREQ(prop_id, "target_faces")) {
if (mode != QUADRIFLOW_REMESH_FACES) {
/* Make sure we can edit the target_faces value even if it doesn't start as EDITABLE */
float area = RNA_float_get(op->ptr, "mesh_area");
if (area < -0.8f) {
area += 0.2f;
/* Make sure we have up to date values from the start */
RNA_def_property_flag((PropertyRNA *)prop, PROP_EDITABLE);
quadriflow_check((bContext *)C, op);
}
/* Only disable input */
RNA_def_property_clear_flag((PropertyRNA *)prop, PROP_EDITABLE);
}
else {
RNA_def_property_flag((PropertyRNA *)prop, PROP_EDITABLE);
}
}
else if (STREQ(prop_id, "target_ratio") && mode != QUADRIFLOW_REMESH_RATIO) {
return false;
}
}
return true;
}
static const EnumPropertyItem mode_type_items[] = {
{QUADRIFLOW_REMESH_RATIO,
"RATIO",
0,
"Ratio",
"Specify target number of faces relative to the current mesh"},
{QUADRIFLOW_REMESH_EDGE_LENGTH,
"EDGE",
0,
"Edge Length",
"Input target edge length in the new mesh"},
{QUADRIFLOW_REMESH_FACES, "FACES", 0, "Faces", "Input target number of faces in the new mesh"},
{0, nullptr, 0, nullptr, nullptr},
};
void OBJECT_OT_quadriflow_remesh(wmOperatorType *ot)
{
/* identifiers */
ot->name = "QuadriFlow Remesh";
ot->description =
"Create a new quad based mesh using the surface data of the current mesh. All data "
"layers will be lost";
ot->idname = "OBJECT_OT_quadriflow_remesh";
/* api callbacks */
ot->poll = object_remesh_poll;
ot->poll_property = quadriflow_poll_property;
ot->check = quadriflow_check;
ot->invoke = WM_operator_props_popup_confirm;
ot->exec = quadriflow_remesh_exec;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
PropertyRNA *prop;
/* properties */
RNA_def_boolean(ot->srna,
"use_mesh_symmetry",
true,
"Use Mesh Symmetry",
"Generates a symmetrical mesh using the mesh symmetry configuration");
RNA_def_boolean(ot->srna,
"use_preserve_sharp",
false,
"Preserve Sharp",
"Try to preserve sharp features on the mesh");
RNA_def_boolean(ot->srna,
"use_preserve_boundary",
false,
"Preserve Mesh Boundary",
"Try to preserve mesh boundary on the mesh");
#ifdef USE_MESH_CURVATURE
RNA_def_boolean(ot->srna,
"use_mesh_curvature",
false,
"Use Mesh Curvature",
"Take the mesh curvature into account when remeshing");
#endif
RNA_def_boolean(ot->srna,
"preserve_paint_mask",
false,
"Preserve Paint Mask",
"Reproject the paint mask onto the new mesh");
RNA_def_boolean(ot->srna,
"smooth_normals",
false,
"Smooth Normals",
"Set the output mesh normals to smooth");
RNA_def_enum(ot->srna,
"mode",
mode_type_items,
QUADRIFLOW_REMESH_FACES,
"Mode",
"How to specify the amount of detail for the new mesh");
prop = RNA_def_float(ot->srna,
"target_ratio",
1,
0,
FLT_MAX,
"Ratio",
"Relative number of faces compared to the current mesh",
0.0f,
1.0f);
prop = RNA_def_float(ot->srna,
"target_edge_length",
0.1f,
0.0000001f,
FLT_MAX,
"Edge Length",
"Target edge length in the new mesh",
0.00001f,
1.0f);
prop = RNA_def_int(ot->srna,
"target_faces",
4000,
1,
INT_MAX,
"Number of Faces",
"Approximate number of faces (quads) in the new mesh",
1,
INT_MAX);
prop = RNA_def_float(
ot->srna,
"mesh_area",
-1.0f,
-FLT_MAX,
FLT_MAX,
"Old Object Face Area",
"This property is only used to cache the object area for later calculations",
0.0f,
FLT_MAX);
RNA_def_property_flag(prop, static_cast<PropertyFlag>(PROP_HIDDEN | PROP_SKIP_SAVE));
RNA_def_int(ot->srna,
"seed",
0,
0,
INT_MAX,
"Seed",
"Random seed to use with the solver. Different seeds will cause the remesher to "
"come up with different quad layouts on the mesh",
0,
255);
}
/** \} */
|