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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup edtransform
22  */
23 
24 #include <stdlib.h>
25 
26 #include "BLI_math.h"
27 
28 #include "BKE_context.h"
29 #include "BKE_editmesh.h"
30 #include "BKE_mesh.h"
31 #include "BKE_unit.h"
32 
33 #include "ED_screen.h"
34 
35 #include "UI_interface.h"
36 
37 #include "transform.h"
38 #include "transform_mode.h"
39 #include "transform_snap.h"
40 
41 /* -------------------------------------------------------------------- */
42 /* Transform (Normal Rotation) */
43 
44 /** \name Transform Normal Rotation
45  * \{ */
46 
storeCustomLNorValue(TransDataContainer * tc,BMesh * bm)47 static void storeCustomLNorValue(TransDataContainer *tc, BMesh *bm)
48 {
49   BMLoopNorEditDataArray *lnors_ed_arr = BM_loop_normal_editdata_array_init(bm, false);
50   // BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
51 
52   tc->custom.mode.data = lnors_ed_arr;
53   tc->custom.mode.free_cb = freeCustomNormalArray;
54 }
55 
freeCustomNormalArray(TransInfo * t,TransDataContainer * tc,TransCustomData * custom_data)56 void freeCustomNormalArray(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
57 {
58   BMLoopNorEditDataArray *lnors_ed_arr = custom_data->data;
59 
60   if (t->state == TRANS_CANCEL) {
61     BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
62     BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
63     BMesh *bm = em->bm;
64 
65     /* Restore custom loop normal on cancel */
66     for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
67       BKE_lnor_space_custom_normal_to_data(
68           bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], lnor_ed->niloc, lnor_ed->clnors_data);
69     }
70   }
71 
72   BM_loop_normal_editdata_array_free(lnors_ed_arr);
73 
74   tc->custom.mode.data = NULL;
75   tc->custom.mode.free_cb = NULL;
76 }
77 
78 /* Works by getting custom normal from clnor_data, transform, then store */
applyNormalRotation(TransInfo * t,const int UNUSED (mval[2]))79 static void applyNormalRotation(TransInfo *t, const int UNUSED(mval[2]))
80 {
81   char str[UI_MAX_DRAW_STR];
82 
83   float axis_final[3];
84   copy_v3_v3(axis_final, t->spacemtx[t->orient_axis]);
85 
86   if ((t->con.mode & CON_APPLY) && t->con.applyRot) {
87     t->con.applyRot(t, NULL, NULL, axis_final, NULL);
88   }
89 
90   FOREACH_TRANS_DATA_CONTAINER (t, tc) {
91     BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
92     BMesh *bm = em->bm;
93 
94     BMLoopNorEditDataArray *lnors_ed_arr = tc->custom.mode.data;
95     BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
96 
97     float axis[3];
98     float mat[3][3];
99     float angle = t->values[0];
100     copy_v3_v3(axis, axis_final);
101 
102     transform_snap_increment(t, &angle);
103 
104     applySnapping(t, &angle);
105 
106     applyNumInput(&t->num, &angle);
107 
108     headerRotation(t, str, angle);
109 
110     axis_angle_normalized_to_mat3(mat, axis, angle);
111 
112     for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
113       mul_v3_m3v3(lnor_ed->nloc, mat, lnor_ed->niloc);
114 
115       BKE_lnor_space_custom_normal_to_data(
116           bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], lnor_ed->nloc, lnor_ed->clnors_data);
117     }
118 
119     t->values_final[0] = angle;
120   }
121 
122   recalcData(t);
123 
124   ED_area_status_text(t->area, str);
125 }
126 
initNormalRotation(TransInfo * t)127 void initNormalRotation(TransInfo *t)
128 {
129   t->mode = TFM_NORMAL_ROTATION;
130   t->transform = applyNormalRotation;
131 
132   setInputPostFct(&t->mouse, postInputRotation);
133   initMouseInputMode(t, &t->mouse, INPUT_ANGLE);
134 
135   t->idx_max = 0;
136   t->num.idx_max = 0;
137   t->snap[0] = DEG2RAD(5.0);
138   t->snap[1] = DEG2RAD(1.0);
139 
140   copy_v3_fl(t->num.val_inc, t->snap[1]);
141   t->num.unit_sys = t->scene->unit.system;
142   t->num.unit_use_radians = (t->scene->unit.system_rotation == USER_UNIT_ROT_RADIANS);
143   t->num.unit_type[0] = B_UNIT_ROTATION;
144 
145   FOREACH_TRANS_DATA_CONTAINER (t, tc) {
146     BMEditMesh *em = BKE_editmesh_from_object(tc->obedit);
147     BMesh *bm = em->bm;
148 
149     BKE_editmesh_ensure_autosmooth(em, tc->obedit->data);
150     BKE_editmesh_lnorspace_update(em, tc->obedit->data);
151 
152     storeCustomLNorValue(tc, bm);
153   }
154 }
155 /** \} */
156