Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / draw / modes / edit_mesh_mode_text.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw
20  */
21
22 #include "BLI_math.h"
23 #include "BLI_string.h"
24
25 #include "BKE_editmesh.h"
26 #include "BKE_global.h"
27 #include "BKE_unit.h"
28
29 #include "ED_view3d.h"
30
31 #include "DNA_mesh_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_view3d_types.h"
36
37 #include "UI_resources.h"
38
39 #include "draw_manager_text.h"
40
41 #include "edit_mesh_mode_intern.h" /* own include */
42
43 /* Copied from drawobject.c */
44 void DRW_edit_mesh_mode_text_measure_stats(
45         ARegion *ar, View3D *v3d,
46         Object *ob, const UnitSettings *unit)
47 {
48         /* Do not use ascii when using non-default unit system, some unit chars are utf8 (micro, square, etc.).
49          * See bug #36090.
50          */
51         struct DRWTextStore *dt = DRW_text_cache_ensure();
52         const short txt_flag = DRW_TEXT_CACHE_GLOBALSPACE | (unit->system ? 0 : DRW_TEXT_CACHE_ASCII);
53         Mesh *me = ob->data;
54         BMEditMesh *em = me->edit_btmesh;
55         float v1[3], v2[3], v3[3], vmid[3], fvec[3];
56         char numstr[32]; /* Stores the measurement display text here */
57         size_t numstr_len;
58         const char *conv_float; /* Use a float conversion matching the grid size */
59         uchar col[4] = {0, 0, 0, 255}; /* color of the text to draw */
60         float area; /* area of the face */
61         float grid = unit->system ? unit->scale_length : v3d->grid;
62         const bool do_global = (v3d->flag & V3D_GLOBAL_STATS) != 0;
63         const bool do_moving = (G.moving & G_TRANSFORM_EDIT) != 0;
64         /* when 2 edge-info options are enabled, space apart */
65         const bool do_edge_textpair = (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_LEN) && (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_ANG);
66         const short edge_texpair_sep = (short)(5.0f * U.ui_scale);
67         float clip_planes[4][4];
68         /* allow for displaying shape keys and deform mods */
69         BMIter iter;
70
71         /* make the precision of the display value proportionate to the gridsize */
72
73         if (grid <= 0.01f) conv_float = "%.6g";
74         else if (grid <= 0.1f) conv_float = "%.5g";
75         else if (grid <= 1.0f) conv_float = "%.4g";
76         else if (grid <= 10.0f) conv_float = "%.3g";
77         else conv_float = "%.2g";
78
79         if (v3d->overlay.edit_flag & (V3D_OVERLAY_EDIT_EDGE_LEN | V3D_OVERLAY_EDIT_EDGE_ANG | V3D_OVERLAY_EDIT_INDICES)) {
80                 BoundBox bb;
81                 const rcti rect = {0, ar->winx, 0, ar->winy};
82
83                 ED_view3d_clipping_calc(&bb, clip_planes, ar, em->ob, &rect);
84         }
85
86         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_LEN) {
87                 BMEdge *eed;
88
89                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
90
91                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
92                         /* draw selected edges, or edges next to selected verts while dragging */
93                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT) ||
94                             (do_moving && (BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
95                                            BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))))
96                         {
97                                 float v1_clip[3], v2_clip[3];
98
99                                 copy_v3_v3(v1, eed->v1->co);
100                                 copy_v3_v3(v2, eed->v2->co);
101
102                                 if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
103
104                                         mid_v3_v3v3(vmid, v1_clip, v2_clip);
105                                         mul_m4_v3(ob->obmat, vmid);
106
107                                         if (do_global) {
108                                                 mul_mat3_m4_v3(ob->obmat, v1);
109                                                 mul_mat3_m4_v3(ob->obmat, v2);
110                                         }
111
112                                         if (unit->system) {
113                                                 numstr_len = bUnit_AsString2(
114                                                         numstr, sizeof(numstr), len_v3v3(v1, v2) * unit->scale_length, 3,
115                                                         B_UNIT_LENGTH, unit, false);
116                                         }
117                                         else {
118                                                 numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), conv_float, len_v3v3(v1, v2));
119                                         }
120
121                                         DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0,
122                                                            (do_edge_textpair) ? edge_texpair_sep : 0,
123                                                            txt_flag, col);
124                                 }
125                         }
126                 }
127         }
128
129         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_EDGE_ANG) {
130                 const bool is_rad = (unit->system_rotation == USER_UNIT_ROT_RADIANS);
131                 BMEdge *eed;
132
133                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGEANG, col);
134
135                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
136                         BMLoop *l_a, *l_b;
137                         if (BM_edge_loop_pair(eed, &l_a, &l_b)) {
138                                 /* draw selected edges, or edges next to selected verts while dragging */
139                                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT) ||
140                                     (do_moving &&
141                                      (BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
142                                       BM_elem_flag_test(eed->v2, BM_ELEM_SELECT) ||
143                                       /* special case, this is useful to show when verts connected to
144                                        * this edge via a face are being transformed */
145                                       BM_elem_flag_test(l_a->next->next->v, BM_ELEM_SELECT) ||
146                                       BM_elem_flag_test(l_a->prev->v, BM_ELEM_SELECT)       ||
147                                       BM_elem_flag_test(l_b->next->next->v, BM_ELEM_SELECT) ||
148                                       BM_elem_flag_test(l_b->prev->v, BM_ELEM_SELECT)
149                                       )))
150                                 {
151                                         float v1_clip[3], v2_clip[3];
152
153                                         copy_v3_v3(v1, eed->v1->co);
154                                         copy_v3_v3(v2, eed->v2->co);
155
156                                         if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
157                                                 float no_a[3], no_b[3];
158                                                 float angle;
159
160                                                 mid_v3_v3v3(vmid, v1_clip, v2_clip);
161                                                 mul_m4_v3(ob->obmat, vmid);
162
163                                                 copy_v3_v3(no_a, l_a->f->no);
164                                                 copy_v3_v3(no_b, l_b->f->no);
165
166                                                 if (do_global) {
167                                                         mul_mat3_m4_v3(ob->imat, no_a);
168                                                         mul_mat3_m4_v3(ob->imat, no_b);
169                                                         normalize_v3(no_a);
170                                                         normalize_v3(no_b);
171                                                 }
172
173                                                 angle = angle_normalized_v3v3(no_a, no_b);
174
175                                                 numstr_len = BLI_snprintf_rlen(
176                                                         numstr, sizeof(numstr), "%.3f%s", (is_rad) ? angle : RAD2DEGF(angle),
177                                                                                           (is_rad) ? "r" : "°");
178
179                                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0,
180                                                                    (do_edge_textpair) ? -edge_texpair_sep : 0,
181                                                                    txt_flag, col);
182                                         }
183                                 }
184                         }
185                 }
186         }
187
188         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_AREA) {
189                 /* would be nice to use BM_face_calc_area, but that is for 2d faces
190                  * so instead add up tessellation triangle areas */
191
192                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
193
194                 int i, n, numtri;
195                 BMFace *f = NULL;
196                 BM_ITER_MESH_INDEX(f, &iter, em->bm, BM_FACES_OF_MESH, i) {
197                         if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
198                                 n = 0;
199                                 numtri = f->len - 2;
200                                 area = 0;
201                                 zero_v3(vmid);
202                                 BMLoop *(*l)[3] = &em->looptris[poly_to_tri_count(i, BM_elem_index_get(f->l_first))];
203                                 for (int j = 0; j < numtri; j++) {
204                                         copy_v3_v3(v1, l[j][0]->v->co);
205                                         copy_v3_v3(v2, l[j][1]->v->co);
206                                         copy_v3_v3(v3, l[j][2]->v->co);
207
208                                         add_v3_v3(vmid, v1);
209                                         add_v3_v3(vmid, v2);
210                                         add_v3_v3(vmid, v3);
211                                         n += 3;
212
213                                         if (do_global) {
214                                                 mul_mat3_m4_v3(ob->obmat, v1);
215                                                 mul_mat3_m4_v3(ob->obmat, v2);
216                                                 mul_mat3_m4_v3(ob->obmat, v3);
217                                         }
218
219                                         area += area_tri_v3(v1, v2, v3);
220                                 }
221
222                                 mul_v3_fl(vmid, 1.0f / (float)n);
223                                 mul_m4_v3(ob->obmat, vmid);
224
225                                 if (unit->system) {
226                                         numstr_len = bUnit_AsString2(
227                                                 numstr, sizeof(numstr),
228                                                 (double)(area * unit->scale_length * unit->scale_length),
229                                                 3, B_UNIT_AREA, unit, false);
230                                 }
231                                 else {
232                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), conv_float, area);
233                                 }
234
235                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col);
236                         }
237                 }
238         }
239
240         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_FACE_ANG) {
241                 BMFace *efa;
242                 const bool is_rad = (unit->system_rotation == USER_UNIT_ROT_RADIANS);
243
244                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
245
246                 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
247                         const bool is_face_sel = BM_elem_flag_test_bool(efa, BM_ELEM_SELECT);
248
249                         if (is_face_sel || do_moving) {
250                                 BMIter liter;
251                                 BMLoop *loop;
252                                 bool is_first = true;
253
254                                 BM_ITER_ELEM (loop, &liter, efa, BM_LOOPS_OF_FACE) {
255                                         if (is_face_sel ||
256                                             (do_moving &&
257                                              (BM_elem_flag_test(loop->v, BM_ELEM_SELECT) ||
258                                               BM_elem_flag_test(loop->prev->v, BM_ELEM_SELECT) ||
259                                               BM_elem_flag_test(loop->next->v, BM_ELEM_SELECT))))
260                                         {
261                                                 float v2_local[3];
262
263                                                 /* lazy init center calc */
264                                                 if (is_first) {
265                                                         BM_face_calc_center_bounds(efa, vmid);
266                                                         is_first = false;
267                                                 }
268                                                 copy_v3_v3(v1, loop->prev->v->co);
269                                                 copy_v3_v3(v2, loop->v->co);
270                                                 copy_v3_v3(v3, loop->next->v->co);
271
272                                                 copy_v3_v3(v2_local, v2);
273
274                                                 if (do_global) {
275                                                         mul_mat3_m4_v3(ob->obmat, v1);
276                                                         mul_mat3_m4_v3(ob->obmat, v2);
277                                                         mul_mat3_m4_v3(ob->obmat, v3);
278                                                 }
279
280                                                 float angle = angle_v3v3v3(v1, v2, v3);
281
282                                                 numstr_len = BLI_snprintf_rlen(
283                                                         numstr, sizeof(numstr), "%.3f%s", (is_rad) ? angle : RAD2DEGF(angle),
284                                                                                           (is_rad) ? "r" : "°");
285                                                 interp_v3_v3v3(fvec, vmid, v2_local, 0.8f);
286                                                 mul_m4_v3(ob->obmat, fvec);
287                                                 DRW_text_cache_add(dt, fvec, numstr, numstr_len, 0, 0, txt_flag, col);
288                                         }
289                                 }
290                         }
291                 }
292         }
293
294         /* This option is for mesh ops and addons debugging; only available in UI if Blender starts with --debug */
295         if (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_INDICES) {
296                 int i;
297
298                 /* For now, reuse an appropriate theme color */
299                 UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
300
301                 if (em->selectmode & SCE_SELECT_VERTEX) {
302                         BMVert *v;
303
304                         BM_ITER_MESH_INDEX(v, &iter, em->bm, BM_VERTS_OF_MESH, i) {
305                                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
306                                         float vec[3];
307                                         mul_v3_m4v3(vec, ob->obmat, v->co);
308
309                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
310                                         DRW_text_cache_add(dt, vec, numstr, numstr_len, 0, 0, txt_flag, col);
311                                 }
312                         }
313                 }
314
315                 if (em->selectmode & SCE_SELECT_EDGE) {
316                         BMEdge *e;
317
318                         BM_ITER_MESH_INDEX(e, &iter, em->bm, BM_EDGES_OF_MESH, i) {
319                                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
320                                         float v1_clip[3], v2_clip[3];
321
322                                         copy_v3_v3(v1, e->v1->co);
323                                         copy_v3_v3(v2, e->v2->co);
324
325                                         if (clip_segment_v3_plane_n(v1, v2, clip_planes, 4, v1_clip, v2_clip)) {
326                                                 mid_v3_v3v3(vmid, v1_clip, v2_clip);
327                                                 mul_m4_v3(ob->obmat, vmid);
328
329                                                 numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
330                                                 DRW_text_cache_add(dt, vmid, numstr, numstr_len, 0, 0, txt_flag, col);
331                                         }
332                                 }
333                         }
334                 }
335
336                 if (em->selectmode & SCE_SELECT_FACE) {
337                         BMFace *f;
338
339                         BM_ITER_MESH_INDEX(f, &iter, em->bm, BM_FACES_OF_MESH, i) {
340                                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
341                                         BM_face_calc_center_median(f, v1);
342                                         mul_m4_v3(ob->obmat, v1);
343
344                                         numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), "%d", i);
345                                         DRW_text_cache_add(dt, v1, numstr, numstr_len, 0, 0, txt_flag, col);
346                                 }
347                         }
348                 }
349         }
350 }