4 * $Id: editparticle.c $
6 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version. The Blender
12 * Foundation also sells licenses for use in proprietary software under
13 * the Blender License. See http://www.blender.org/BL/ for information
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * The Original Code is Copyright (C) 2007 by Janne Karhu.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): none yet.
32 * ***** END GPL/BL DUAL LICENSE BLOCK *****
39 #include "MEM_guardedalloc.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_force.h"
46 #include "DNA_object_types.h"
47 #include "DNA_vec_types.h"
48 #include "DNA_userdef_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
53 #include "BKE_bad_level_calls.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_depsgraph.h"
57 #include "BKE_global.h"
58 #include "BKE_object.h"
60 #include "BKE_modifier.h"
61 #include "BKE_particle.h"
62 #include "BKE_scene.h"
63 #include "BKE_utildefines.h"
67 #include "BLI_arithb.h"
68 #include "BLI_blenlib.h"
69 #include "BLI_dynstr.h"
70 #include "BLI_kdtree.h"
76 #include "BIF_glutil.h"
77 #include "BIF_graphics.h"
78 #include "BIF_editparticle.h"
79 #include "BIF_editview.h"
80 #include "BIF_interface.h"
81 #include "BIF_meshtools.h"
82 #include "BIF_mywindow.h"
83 #include "BIF_resources.h"
84 #include "BIF_screen.h"
85 #include "BIF_space.h"
86 #include "BIF_toolbox.h"
90 #include "BDR_editobject.h" //rightmouse_transform()
91 #include "BDR_drawobject.h"
96 static void ParticleUndo_clear(ParticleSystem *psys);
98 #define LOOP_PARTICLES(i,pa) for(i=0, pa=psys->particles; i<totpart; i++, pa++)
99 #define LOOP_KEYS(k,key) if(psys->edit)for(k=0, key=psys->edit->keys[i]; k<pa->totkey; k++, key++)
101 void PE_free_particle_edit(ParticleSystem *psys)
103 ParticleEdit *edit=psys->edit;
104 int i, totpart=psys->totpart;
108 ParticleUndo_clear(psys);
111 for(i=0; i<totpart; i++){
113 MEM_freeN(edit->keys[i]);
115 MEM_freeN(edit->keys);
118 if(edit->mirror_cache)
119 MEM_freeN(edit->mirror_cache);
121 if(edit->emitter_cosnos){
122 MEM_freeN(edit->emitter_cosnos);
123 edit->emitter_cosnos=0;
126 if(edit->emitter_field){
127 BLI_kdtree_free(edit->emitter_field);
128 edit->emitter_field=0;
135 /************************************************/
136 /* Edit Mode Helpers */
137 /************************************************/
138 int PE_can_edit(ParticleSystem *psys)
140 return (psys && psys->edit && (G.f & G_PARTICLEEDIT));
143 ParticleEditSettings *PE_settings()
145 return &G.scene->toolsettings->particle;
148 void PE_change_act(void *ob_v, void *act_v)
151 ParticleSystem *psys;
152 short act = *((short*)act_v) - 1;
154 if((psys=psys_get_current(ob)))
155 psys->flag &= ~PSYS_CURRENT;
158 if((psys=BLI_findlink(&ob->particlesystem,act))) {
159 psys->flag |= PSYS_CURRENT;
161 if(psys_check_enabled(ob, psys)) {
162 if(G.f & G_PARTICLEEDIT && !psys->edit)
163 PE_create_particle_edit(ob, psys);
164 PE_recalc_world_cos(ob, psys);
170 /* always gets atleast the first particlesystem even if PSYS_CURRENT flag is not set */
171 ParticleSystem *PE_get_current(Object *ob)
173 ParticleSystem *psys;
178 psys= ob->particlesystem.first;
180 if(psys->flag & PSYS_CURRENT)
185 if(psys==NULL && ob->particlesystem.first){
186 psys=ob->particlesystem.first;
187 psys->flag |= PSYS_CURRENT;
190 if(psys && psys_check_enabled(ob, psys) && ob == OBACT && (G.f & G_PARTICLEEDIT))
191 if(psys->part->type == PART_HAIR && psys->flag & PSYS_EDITED)
192 if(psys->edit == NULL)
193 PE_create_particle_edit(ob, psys);
197 /* returns -1 if no system has PSYS_CURRENT flag */
198 short PE_get_current_num(Object *ob)
201 ParticleSystem *psys = ob->particlesystem.first;
204 if(psys->flag & PSYS_CURRENT)
213 void PE_hide_keys_time(ParticleSystem *psys, float cfra)
216 ParticleEditKey *key;
217 ParticleEditSettings *pset=PE_settings();
218 int i,k,totpart=psys->totpart;
220 if(pset->draw_timed && G.scene->selectmode==SCE_SELECT_POINT){
221 LOOP_PARTICLES(i,pa){
223 if(fabs(cfra-*key->time) < pset->draw_timed)
224 key->flag &= ~PEK_HIDE;
226 key->flag |= PEK_HIDE;
227 key->flag &= ~PEK_SELECT;
233 LOOP_PARTICLES(i,pa){
235 key->flag &= ~PEK_HIDE;
241 static int key_inside_circle(short mco[2], float rad, float co[3], float *distance)
246 project_short(co,vertco);
247 dx=(float)(mco[0]-vertco[0]);
248 dy=(float)(mco[1]-vertco[1]);
249 dist=(float)sqrt((double)(dx*dx + dy*dy));
252 if(distance) *distance=dist;
258 static int key_inside_rect(rcti *rect, float co[3])
262 project_short(co,vertco);
264 if(vertco[0] > rect->xmin && vertco[0] < rect->xmax &&
265 vertco[1] > rect->ymin && vertco[1] < rect->ymax)
270 static int test_key_depth(float *co, bglMats *mats){
275 if((G.vd->flag & V3D_ZBUF_SELECT)==0) return 1;
277 gluProject(co[0],co[1],co[2], mats->modelview, mats->projection,
278 (GLint *)mats->viewport, &ux, &uy, &uz );
280 project_short(co,wco);
285 if(G.vd->depths && x<G.vd->depths->w && y<G.vd->depths->h){
286 if((float)uz>G.vd->depths->depths[y*G.vd->depths->w+x])
292 x+= (short)curarea->winrct.xmin;
293 y+= (short)curarea->winrct.ymin;
295 glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
304 static int particle_is_selected(ParticleSystem *psys, ParticleData *pa)
306 ParticleEditKey *key;
309 if(pa->flag&PARS_HIDE) return 0;
312 i= pa - psys->particles;
314 if(key->flag&PEK_SELECT)
320 /*-----iterators over editable particles-----*/
321 static void for_mouse_hit_keys(int nearest, ParticleSystem *psys, void (*func)(ParticleSystem *psys, int pa_index, int key_index, void *userData), void *userData){
322 /* these are allways the first in this userData */
323 struct { short *mval; float rad; rcti *rect;} *data = userData;
325 ParticleEditKey *key;
327 int i,k, totpart, nearest_pa=-1, nearest_key=-1;
328 float dist=data->rad;
330 if(psys==0 || G.scene->selectmode==SCE_SELECT_PATH) return;
332 totpart=psys->totpart;
336 LOOP_PARTICLES(i,pa){
337 if(pa->flag & PARS_HIDE) continue;
339 if(G.scene->selectmode==SCE_SELECT_END){
340 key=psys->edit->keys[i]+pa->totkey-1;
343 if(key_inside_circle(data->mval,dist,key->world_co,&dist) && test_key_depth(key->world_co,&mats)){
345 nearest_key=pa->totkey-1;
348 else if(((data->mval)?
349 key_inside_circle(data->mval,data->rad,key->world_co,0):
350 key_inside_rect(data->rect,key->world_co)) && test_key_depth(key->world_co,&mats))
351 func(psys,i,pa->totkey-1,userData);
354 key=psys->edit->keys[i];
357 if(key->flag&PEK_HIDE) continue;
360 if(key_inside_circle(data->mval,dist,key->world_co,&dist) && test_key_depth(key->world_co,&mats)){
365 else if(((data->mval)?
366 key_inside_circle(data->mval,data->rad,key->world_co,0):
367 key_inside_rect(data->rect,key->world_co)) && test_key_depth(key->world_co,&mats))
368 func(psys,i,k,userData);
372 if(nearest && nearest_pa>-1){
373 func(psys,nearest_pa,nearest_key,userData);
376 static void foreach_mouse_hit_element(int selected, ParticleSystem *psys,void (*func)(ParticleSystem *psys, int index, void *userData), void *userData){
377 /* these are allways the first in this userData */
378 struct { short *mval; float rad; rcti* rect; float dist;} *data = userData;
380 ParticleEditKey *key;
386 totpart=psys->totpart;
390 if(G.scene->selectmode==SCE_SELECT_PATH)
393 LOOP_PARTICLES(i,pa){
394 if(pa->flag & PARS_HIDE) continue;
396 if(G.scene->selectmode==SCE_SELECT_END){
397 key=psys->edit->keys[i]+pa->totkey-1;
398 if(key_inside_circle(data->mval,data->rad,key->world_co,&data->dist) && (selected==0 || key->flag&PEK_SELECT) && test_key_depth(key->world_co,&mats))
399 func(psys,i,userData);
403 if(key->flag&PEK_HIDE) continue;
405 if(key_inside_circle(data->mval,data->rad,key->world_co,&data->dist) && (selected==0 || key->flag&PEK_SELECT) && test_key_depth(key->world_co,&mats)){
406 func(psys,i,userData);
413 static void foreach_mouse_hit_key(int selected, ParticleSystem *psys,void (*func)(ParticleSystem *psys, float mat[][4], float imat[][4], int bel_index, int key_index, void *userData), void *userData){
414 /* these are allways the first in this userData */
415 struct { Object *ob; short *mval; float rad; rcti* rect; float dist;} *data = userData;
417 ParticleEditKey *key;
418 ParticleSystemModifierData *psmd=0;
421 float mat[4][4], imat[4][4];
425 psmd=psys_get_modifier(data->ob,psys);
427 totpart=psys->totpart;
431 if(G.scene->selectmode==SCE_SELECT_PATH)
437 LOOP_PARTICLES(i,pa){
438 if(pa->flag & PARS_HIDE) continue;
440 psys_mat_hair_to_global(data->ob, psmd->dm, psys->part->from, pa, mat);
441 //psys_geometry_mat(psmd->dm,pa,tmat);
442 //Mat4MulMat4(mat,tmat,data->ob->obmat);
443 Mat4Invert(imat,mat);
445 if(G.scene->selectmode==SCE_SELECT_END){
446 key=psys->edit->keys[i]+pa->totkey-1;
447 if(key_inside_circle(data->mval,data->rad,key->world_co,&data->dist) && (selected==0 || key->flag&PEK_SELECT) && test_key_depth(key->world_co,&mats))
448 func(psys,mat,imat,i,pa->totkey-1,userData);
452 if(key->flag&PEK_HIDE) continue;
454 if(key_inside_circle(data->mval,data->rad,key->world_co,&data->dist) && (selected==0 || key->flag&PEK_SELECT) && test_key_depth(key->world_co,&mats)){
455 func(psys,mat,imat,i,k,userData);
461 static void foreach_selected_element(ParticleSystem *psys, void (*func)(ParticleSystem *psys, int index, void *userData), void *userData){
467 totpart=psys->totpart;
470 if(particle_is_selected(psys, pa))
471 func(psys,i,userData);
473 static void foreach_selected_key(ParticleSystem *psys, void (*func)(ParticleSystem *psys, int pa_index, int key_index, void *userData), void *userData){
475 ParticleEditKey *key;
480 totpart=psys->totpart;
482 LOOP_PARTICLES(i,pa){
483 if(pa->flag&PARS_HIDE) continue;
485 key=psys->edit->keys[i];
487 if(key->flag&PEK_SELECT)
488 func(psys,i,k,userData);
492 void PE_foreach_element(ParticleSystem *psys, void (*func)(ParticleSystem *psys, int index, void *userData), void *userData)
498 totpart=psys->totpart;
500 for(i=0; i<totpart; i++)
501 func(psys,i,userData);
503 static int count_selected_keys(ParticleSystem *psys)
506 ParticleEditKey *key;
507 int i,k,totpart,sel=0;
509 if(psys==0) return 0;
511 totpart=psys->totpart;
513 LOOP_PARTICLES(i,pa){
514 if(pa->flag&PARS_HIDE) continue;
516 key=psys->edit->keys[i];
517 if(G.scene->selectmode==SCE_SELECT_POINT){
518 for(k=0; k<pa->totkey; k++,key++){
519 if(key->flag&PEK_SELECT)
523 else if(G.scene->selectmode==SCE_SELECT_END){
525 if(key->flag&PEK_SELECT)
532 /************************************************/
533 /* Particle Edit Mirroring */
534 /************************************************/
536 static void PE_update_mirror_cache(Object *ob, ParticleSystem *psys)
540 ParticleSystemModifierData *psmd;
542 KDTreeNearest nearest;
543 float mat[4][4], co[3];
544 int i, index, totpart;
547 psmd= psys_get_modifier(ob, psys);
548 totpart= psys->totpart;
550 tree= BLI_kdtree_new(totpart);
552 /* insert particles into kd tree */
553 LOOP_PARTICLES(i,pa) {
554 psys_mat_hair_to_orco(ob, psmd->dm, psys->part->from, pa, mat);
555 VECCOPY(co, pa->hair[0].co);
556 Mat4MulVecfl(mat, co);
557 BLI_kdtree_insert(tree, i, co, NULL);
560 BLI_kdtree_balance(tree);
562 /* lookup particles and set in mirror cache */
563 if(!edit->mirror_cache)
564 edit->mirror_cache= MEM_callocN(sizeof(int)*totpart, "PE mirror cache");
566 LOOP_PARTICLES(i,pa) {
567 psys_mat_hair_to_orco(ob, psmd->dm, psys->part->from, pa, mat);
568 VECCOPY(co, pa->hair[0].co);
569 Mat4MulVecfl(mat, co);
572 index= BLI_kdtree_find_nearest(tree, co, NULL, &nearest);
574 /* this needs a custom threshold still, duplicated for editmode mirror */
575 if(index != -1 && index != i && (nearest.dist <= 0.0002f))
576 edit->mirror_cache[i]= index;
578 edit->mirror_cache[i]= -1;
581 /* make sure mirrors are in two directions */
582 LOOP_PARTICLES(i,pa) {
583 if(edit->mirror_cache[i]) {
584 index= edit->mirror_cache[i];
585 if(edit->mirror_cache[index] != i)
586 edit->mirror_cache[i]= -1;
590 BLI_kdtree_free(tree);
593 static void PE_mirror_particle(Object *ob, DerivedMesh *dm, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
595 HairKey *hkey, *mhkey;
596 ParticleEditKey *key, *mkey;
598 float mat[4][4], mmat[4][4], immat[4][4];
602 i= pa - psys->particles;
604 if(!edit->mirror_cache)
605 PE_update_mirror_cache(ob, psys);
607 /* find mirrored particle if needed */
609 mi= edit->mirror_cache[i];
612 mpa= psys->particles + mi;
615 mi= mpa - psys->particles;
617 /* make sure they have the same amount of keys */
618 if(pa->totkey != mpa->totkey) {
619 if(mpa->hair) MEM_freeN(mpa->hair);
620 if(edit->keys[mi]) MEM_freeN(edit->keys[mi]);
622 mpa->hair= MEM_dupallocN(pa->hair);
623 edit->keys[mi]= MEM_dupallocN(edit->keys[i]);
624 mpa->totkey= pa->totkey;
627 mkey= edit->keys[mi];
628 for(k=0; k<mpa->totkey; k++, mkey++, mhkey++) {
630 mkey->time= &mhkey->time;
631 mkey->flag &= PEK_SELECT;
635 /* mirror positions and tags */
636 psys_mat_hair_to_orco(ob, dm, psys->part->from, pa, mat);
637 psys_mat_hair_to_orco(ob, dm, psys->part->from, mpa, mmat);
638 Mat4Invert(immat, mmat);
643 mkey= edit->keys[mi];
644 for(k=0; k<pa->totkey; k++, hkey++, mhkey++, key++, mkey++) {
645 VECCOPY(mhkey->co, hkey->co);
646 Mat4MulVecfl(mat, mhkey->co);
647 mhkey->co[0]= -mhkey->co[0];
648 Mat4MulVecfl(immat, mhkey->co);
650 if(key->flag & PEK_TAG)
651 mkey->flag |= PEK_TAG;
654 if(pa->flag & PARS_TAG)
655 mpa->flag |= PARS_TAG;
656 if(pa->flag & PARS_EDIT_RECALC)
657 mpa->flag |= PARS_EDIT_RECALC;
660 static void PE_apply_mirror(Object *ob, ParticleSystem *psys)
664 ParticleSystemModifierData *psmd;
668 psmd= psys_get_modifier(ob, psys);
669 totpart= psys->totpart;
671 /* we delay settings the PARS_EDIT_RECALC for mirrored particles
672 * to avoid doing mirror twice */
673 LOOP_PARTICLES(i,pa) {
674 if(pa->flag & PARS_EDIT_RECALC) {
675 PE_mirror_particle(ob, psmd->dm, psys, pa, NULL);
677 if(edit->mirror_cache[i] != -1)
678 psys->particles[edit->mirror_cache[i]].flag &= ~PARS_EDIT_RECALC;
683 if(pa->flag & PARS_EDIT_RECALC)
684 if(edit->mirror_cache[i] != -1)
685 psys->particles[edit->mirror_cache[i]].flag |= PARS_EDIT_RECALC;
687 edit->totkeys= psys_count_keys(psys);
690 /************************************************/
691 /* Edit Calculation */
692 /************************************************/
693 /* tries to stop edited particles from going through the emitter's surface */
694 static void PE_deflect_emitter(Object *ob, ParticleSystem *psys)
698 ParticleEditKey *key;
699 ParticleEditSettings *pset = PE_settings();
700 ParticleSystemModifierData *psmd = psys_get_modifier(ob,psys);
701 int i,k,totpart,index;
702 float *vec, *nor, dvec[3], dot, dist_1st;
703 float hairimat[4][4], hairmat[4][4];
708 if((pset->flag & PE_DEFLECT_EMITTER)==0)
712 totpart=psys->totpart;
714 LOOP_PARTICLES(i,pa){
715 if(!(pa->flag & PARS_EDIT_RECALC))
718 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, pa, hairmat);
721 Mat4MulVecfl(hairmat, key->co);
725 //LOOP_PARTICLES(i,pa){
726 key=psys->edit->keys[i]+1;
728 dist_1st=VecLenf((key-1)->co,key->co);
729 dist_1st*=0.75f*pset->emitterdist;
731 for(k=1; k<pa->totkey; k++, key++){
732 index= BLI_kdtree_find_nearest(edit->emitter_field,key->co,NULL,NULL);
734 vec=edit->emitter_cosnos +index*6;
737 VecSubf(dvec, key->co, vec);
745 VecMulf(dvec,dist_1st-dot);
746 VecAddf(key->co,key->co,dvec);
751 VecMulf(dvec,dist_1st-dot);
752 VecAddf(key->co,key->co,dvec);
759 //LOOP_PARTICLES(i,pa){
761 Mat4Invert(hairimat,hairmat);
764 Mat4MulVecfl(hairimat, key->co);
768 /* force set distances between neighbouring keys */
769 void PE_apply_lengths(ParticleSystem *psys)
773 ParticleEditKey *key;
774 ParticleEditSettings *pset=PE_settings();
781 if((pset->flag & PE_KEEP_LENGTHS)==0)
785 totpart=psys->totpart;
787 LOOP_PARTICLES(i,pa){
788 if(!(pa->flag & PARS_EDIT_RECALC))
791 for(k=1, key=edit->keys[i] + 1; k<pa->totkey; k++, key++){
792 VecSubf(dv1, key->co, (key - 1)->co);
794 VecMulf(dv1, (key - 1)->length);
795 VecAddf(key->co, (key - 1)->co, dv1);
799 /* try to find a nice solution to keep distances between neighbouring keys */
800 static void PE_iterate_lengths(ParticleSystem *psys)
804 ParticleEditKey *key;
805 ParticleEditSettings *pset=PE_settings();
808 float dv0[3] = {0.0f, 0.0f, 0.0f};
809 float dv1[3] = {0.0f, 0.0f, 0.0f};
810 float dv2[3] = {0.0f, 0.0f, 0.0f};
815 if((pset->flag & PE_KEEP_LENGTHS)==0)
819 totpart=psys->totpart;
821 LOOP_PARTICLES(i,pa){
822 if(!(pa->flag & PARS_EDIT_RECALC))
825 for(j=1; j<pa->totkey; j++){
826 float mul = 1.0f / (float)pa->totkey;
828 if(pset->flag & PE_LOCK_FIRST){
829 key = edit->keys[i] + 1;
831 dv1[0] = dv1[1] = dv1[2] = 0.0;
836 dv0[0] = dv0[1] = dv0[2] = 0.0;
839 for(; k<pa->totkey; k++, key++){
841 VecSubf(dv0, (key - 1)->co, key->co);
842 tlen = Normalize(dv0);
843 VecMulf(dv0, (mul * (tlen - (key - 1)->length)));
846 if(k < pa->totkey - 1){
847 VecSubf(dv2, (key + 1)->co, key->co);
848 tlen = Normalize(dv2);
849 VecMulf(dv2, mul * (tlen - key->length));
853 VecAddf((key-1)->co,(key-1)->co,dv1);
861 /* set current distances to be kept between neighbouting keys */
862 static void recalc_lengths(ParticleSystem *psys)
865 ParticleEditKey *key;
871 totpart = psys->totpart;
873 LOOP_PARTICLES(i,pa){
874 key = psys->edit->keys[i];
875 for(k=0; k<pa->totkey-1; k++, key++){
876 key->length = VecLenf(key->co, (key + 1)->co);
880 /* calculate and store key locations in world coordinates */
881 void PE_recalc_world_cos(Object *ob, ParticleSystem *psys)
883 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
885 ParticleEditKey *key;
892 totpart = psys->totpart;
894 LOOP_PARTICLES(i,pa){
895 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat);
898 VECCOPY(key->world_co,key->co);
899 Mat4MulVecfl(hairmat, key->world_co);
903 /* calculate a tree for finding nearest emitter's vertice */
904 static void recalc_emitter_field(Object *ob, ParticleSystem *psys)
906 DerivedMesh *dm=psys_get_modifier(ob,psys)->dm;
907 ParticleEdit *edit = psys->edit;
911 int i, totface, totvert;
913 if(edit->emitter_cosnos)
914 MEM_freeN(edit->emitter_cosnos);
916 BLI_kdtree_free(edit->emitter_field);
918 totface=dm->getNumFaces(dm);
919 totvert=dm->getNumVerts(dm);
921 edit->emitter_cosnos=MEM_callocN(totface*6*sizeof(float),"emitter cosnos");
923 edit->emitter_field= BLI_kdtree_new(totface);
925 vec=edit->emitter_cosnos;
928 mvert=dm->getVertDataArray(dm,CD_MVERT);
929 for(i=0; i<totface; i++, vec+=6, nor+=6){
930 mface=dm->getFaceData(dm,i,CD_MFACE);
932 mvert=dm->getVertData(dm,mface->v1,CD_MVERT);
933 VECCOPY(vec,mvert->co);
934 VECCOPY(nor,mvert->no);
936 mvert=dm->getVertData(dm,mface->v2,CD_MVERT);
937 VECADD(vec,vec,mvert->co);
938 VECADD(nor,nor,mvert->no);
940 mvert=dm->getVertData(dm,mface->v3,CD_MVERT);
941 VECADD(vec,vec,mvert->co);
942 VECADD(nor,nor,mvert->no);
945 mvert=dm->getVertData(dm,mface->v4,CD_MVERT);
946 VECADD(vec,vec,mvert->co);
947 VECADD(nor,nor,mvert->no);
952 VecMulf(vec,0.3333f);
956 BLI_kdtree_insert(edit->emitter_field, i, vec, NULL);
959 BLI_kdtree_balance(edit->emitter_field);
962 void PE_update_selection(Object *ob, int useflag)
964 ParticleSystem *psys= PE_get_current(ob);
965 ParticleEdit *edit= psys->edit;
966 ParticleEditSettings *pset= PE_settings();
967 ParticleSettings *part= psys->part;
970 ParticleEditKey *key;
974 totpart= psys->totpart;
976 /* flag all particles to be updated if not using flag */
979 pa->flag |= PARS_EDIT_RECALC;
981 /* flush edit key flag to hair key flag to preserve selection
983 LOOP_PARTICLES(i,pa) {
986 for(k=0, hkey=pa->hair; k<pa->totkey; k++, hkey++, key++)
987 hkey->editflag= key->flag;
990 psys_cache_paths(ob, psys, CFRA, 1);
992 if(part->childtype && (pset->flag & PE_SHOW_CHILD))
993 psys_cache_child_paths(ob, psys, cfra, 1);
995 /* disable update flag */
997 pa->flag &= ~PARS_EDIT_RECALC;
1000 void PE_update_object(Object *ob, int useflag)
1002 ParticleSystem *psys= PE_get_current(ob);
1003 ParticleEditSettings *pset= PE_settings();
1004 ParticleSettings *part= psys->part;
1007 int i, totpart= psys->totpart;
1009 /* flag all particles to be updated if not using flag */
1011 LOOP_PARTICLES(i,pa)
1012 pa->flag |= PARS_EDIT_RECALC;
1014 /* do post process on particle edit keys */
1015 PE_iterate_lengths(psys);
1016 PE_deflect_emitter(ob,psys);
1017 PE_apply_lengths(psys);
1018 if(pset->flag & PE_X_MIRROR)
1019 PE_apply_mirror(ob,psys);
1020 PE_recalc_world_cos(ob,psys);
1021 PE_hide_keys_time(psys,cfra);
1023 /* regenerate path caches */
1024 psys_cache_paths(ob, psys, cfra, 1);
1026 if(part->childtype && (pset->flag & PE_SHOW_CHILD))
1027 psys_cache_child_paths(ob, psys, cfra, 1);
1029 /* disable update flag */
1030 LOOP_PARTICLES(i,pa)
1031 pa->flag &= ~PARS_EDIT_RECALC;
1034 /* initialize needed data for bake edit */
1035 void PE_create_particle_edit(Object *ob, ParticleSystem *psys)
1037 ParticleEdit *edit=psys->edit;
1039 ParticleEditKey *key;
1041 int i,k, totpart=psys->totpart, alloc=1;
1043 if((psys->flag & PSYS_EDITED)==0)
1047 int newtotkeys = psys_count_keys(psys);
1048 if(newtotkeys == edit->totkeys)
1054 error("ParticleEdit exists allready! Poke jahka!");
1055 PE_free_particle_edit(psys);
1058 edit=psys->edit=MEM_callocN(sizeof(ParticleEdit), "PE_create_particle_edit");
1060 edit->keys=MEM_callocN(totpart*sizeof(ParticleEditKey*),"ParticleEditKey array");
1062 LOOP_PARTICLES(i,pa){
1063 key = edit->keys[i] = MEM_callocN(pa->totkey*sizeof(ParticleEditKey),"ParticleEditKeys");
1064 for(k=0, hkey=pa->hair; k<pa->totkey; k++, hkey++, key++){
1066 key->time = &hkey->time;
1067 key->flag= hkey->editflag;
1071 edit->totkeys = psys_count_keys(psys);
1074 recalc_lengths(psys);
1075 recalc_emitter_field(ob, psys);
1076 PE_recalc_world_cos(ob, psys);
1079 ParticleUndo_clear(psys);
1080 PE_undo_push("Original");
1084 /* toggle particle mode on & off */
1085 void PE_set_particle_edit(void)
1088 ParticleSystem *psys = PE_get_current(ob);
1090 scrarea_queue_headredraw(curarea);
1092 //if(!ob || ob->id.lib) return; /* is the id.lib test needed? -jahka*/
1093 if(ob==0 || psys==0) return;
1096 if(ob->particlesystem.first){
1097 psys=ob->particlesystem.first;
1098 psys->flag |= PSYS_CURRENT;
1104 if((G.f & G_PARTICLEEDIT)==0){
1105 if(psys && psys->part->type == PART_HAIR && psys->flag & PSYS_EDITED) {
1106 if(psys_check_enabled(ob, psys)) {
1108 PE_create_particle_edit(ob, psys);
1109 PE_recalc_world_cos(ob, psys);
1113 G.f |= G_PARTICLEEDIT;
1116 G.f &= ~G_PARTICLEEDIT;
1119 psys->softflag |= OB_SB_REDO;
1122 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1124 allqueue(REDRAWVIEW3D, 1); /* including header */
1125 allqueue(REDRAWBUTSOBJECT, 0);
1127 /************************************************/
1128 /* Edit Selections */
1129 /************************************************/
1130 /*-----selection callbacks-----*/
1131 static void select_key(ParticleSystem *psys, int pa_index, int key_index, void *userData)
1133 struct { short *mval; float rad; rcti* rect; int select; } *data = userData;
1134 ParticleData *pa = psys->particles + pa_index;
1135 ParticleEditKey *key = psys->edit->keys[pa_index] + key_index;
1138 key->flag|=PEK_SELECT;
1140 key->flag&=~PEK_SELECT;
1142 pa->flag |= PARS_EDIT_RECALC;
1144 static void select_keys(ParticleSystem *psys, int pa_index, int key_index, void *userData)
1146 struct { short *mval; float rad; rcti* rect; int select; } *data = userData;
1147 ParticleData *pa = psys->particles + pa_index;
1148 ParticleEditKey *key = psys->edit->keys[pa_index];
1151 for(k=0; k<pa->totkey; k++,key++){
1153 key->flag|=PEK_SELECT;
1155 key->flag&=~PEK_SELECT;
1158 pa->flag |= PARS_EDIT_RECALC;
1160 static void toggle_key_select(ParticleSystem *psys, int pa_index, int key_index, void *userData)
1162 ParticleData *pa = psys->particles + pa_index;
1164 if(psys->edit->keys[pa_index][key_index].flag&PEK_SELECT)
1165 psys->edit->keys[pa_index][key_index].flag&=~PEK_SELECT;
1167 psys->edit->keys[pa_index][key_index].flag|=PEK_SELECT;
1169 pa->flag |= PARS_EDIT_RECALC;
1171 static void select_root(ParticleSystem *psys, int index, void *userData)
1173 psys->edit->keys[index]->flag |= PEK_SELECT;
1176 static void select_tip(ParticleSystem *psys, int index, void *userData)
1178 ParticleData *pa = psys->particles + index;
1179 ParticleEditKey *key = psys->edit->keys[index] + pa->totkey-1;
1181 key->flag |= PEK_SELECT;
1183 static void select_more_keys(ParticleSystem *psys, int index, void *userData)
1185 ParticleEdit *edit = psys->edit;
1186 ParticleData *pa = psys->particles+index;
1187 ParticleEditKey *key;
1190 for(k=0,key=edit->keys[index]; k<pa->totkey; k++,key++){
1191 if(key->flag&PEK_SELECT) continue;
1194 if((key+1)->flag&PEK_SELECT)
1195 key->flag |= PEK_TO_SELECT;
1197 else if(k==pa->totkey-1){
1198 if((key-1)->flag&PEK_SELECT)
1199 key->flag |= PEK_TO_SELECT;
1202 if(((key-1)->flag | (key+1)->flag) & PEK_SELECT)
1203 key->flag |= PEK_TO_SELECT;
1207 for(k=0,key=edit->keys[index]; k<pa->totkey; k++,key++){
1208 if(key->flag&PEK_TO_SELECT){
1209 key->flag &= ~PEK_TO_SELECT;
1210 key->flag |= PEK_SELECT;
1215 static void select_less_keys(ParticleSystem *psys, int index, void *userData)
1217 ParticleEdit *edit = psys->edit;
1218 ParticleData *pa = psys->particles+index;
1219 ParticleEditKey *key;
1222 for(k=0,key=edit->keys[index]; k<pa->totkey; k++,key++){
1223 if((key->flag&PEK_SELECT)==0) continue;
1226 if(((key+1)->flag&PEK_SELECT)==0)
1227 key->flag |= PEK_TO_SELECT;
1229 else if(k==pa->totkey-1){
1230 if(((key-1)->flag&PEK_SELECT)==0)
1231 key->flag |= PEK_TO_SELECT;
1234 if((((key-1)->flag & (key+1)->flag) & PEK_SELECT)==0)
1235 key->flag |= PEK_TO_SELECT;
1239 for(k=0,key=edit->keys[index]; k<pa->totkey; k++,key++){
1240 if(key->flag&PEK_TO_SELECT)
1241 key->flag &= ~(PEK_TO_SELECT|PEK_SELECT);
1245 /*-----using above callbacks-----*/
1246 void PE_deselectall(void)
1249 ParticleSystem *psys = PE_get_current(ob);
1250 ParticleEdit *edit = 0;
1252 ParticleEditKey *key;
1253 int i,k,totpart, sel = 0;
1255 if(!PE_can_edit(psys)) return;
1259 totpart = psys->totpart;
1261 LOOP_PARTICLES(i,pa){
1262 if(pa->flag & PARS_HIDE) continue;
1264 if(key->flag&PEK_SELECT){
1266 key->flag &= ~PEK_SELECT;
1267 pa->flag |= PARS_EDIT_RECALC;
1273 LOOP_PARTICLES(i,pa){
1274 if(pa->flag & PARS_HIDE) continue;
1276 if(!(key->flag & PEK_SELECT)) {
1277 key->flag |= PEK_SELECT;
1278 pa->flag |= PARS_EDIT_RECALC;
1284 PE_update_selection(ob, 1);
1286 BIF_undo_push("(De)select all keys");
1287 allqueue(REDRAWVIEW3D, 1);
1289 void PE_mouse_particles(void)
1291 struct { short *mval; float rad; rcti* rect; int select; } data;
1293 ParticleSystem *psys = PE_get_current(ob);
1294 ParticleEdit *edit = 0;
1296 ParticleEditKey *key;
1300 if(!PE_can_edit(psys)) return;
1304 totpart = psys->totpart;
1307 glReadBuffer(GL_BACK);
1308 glDrawBuffer(GL_BACK);
1311 if(G.qual != LR_SHIFTKEY)
1312 LOOP_PARTICLES(i,pa){
1313 if(pa->flag & PARS_HIDE) continue;
1315 if(key->flag & PEK_SELECT) {
1316 key->flag &= ~PEK_SELECT;
1317 pa->flag |= PARS_EDIT_RECALC;
1322 getmouseco_areawin(mval);
1329 for_mouse_hit_keys(1,psys,toggle_key_select,&data);
1331 PE_update_selection(ob, 1);
1333 rightmouse_transform();
1335 allqueue(REDRAWVIEW3D, 1);
1337 void PE_select_root()
1340 ParticleSystem *psys = PE_get_current(ob);
1342 if(!PE_can_edit(psys)) return;
1344 PE_foreach_element(psys,select_root,NULL);
1345 BIF_undo_push("Select first");
1347 void PE_select_tip()
1350 ParticleSystem *psys = PE_get_current(ob);
1352 if(!PE_can_edit(psys)) return;
1354 PE_foreach_element(psys,select_tip,NULL);
1355 BIF_undo_push("Select last");
1357 void PE_select_linked(void)
1359 struct { short *mval; float rad; rcti* rect; int select; } data;
1361 ParticleSystem *psys = PE_get_current(ob);
1364 if(!PE_can_edit(psys)) return;
1366 getmouseco_areawin(mval);
1371 data.select=(G.qual != LR_SHIFTKEY);
1373 for_mouse_hit_keys(1,psys,select_keys,&data);
1375 PE_update_selection(ob, 1);
1377 BIF_undo_push("Select linked keys");
1379 allqueue(REDRAWVIEW3D, 1);
1382 void PE_borderselect(void)
1384 struct { short *mval; float rad; rcti* rect; int select; } data;
1386 ParticleSystem *psys = PE_get_current(ob);
1390 if(!PE_can_edit(psys)) return;
1393 val= get_border(&rect, 3);
1400 data.select=(val==LEFTMOUSE);
1402 for_mouse_hit_keys(0,psys,select_key,&data);
1404 PE_update_selection(ob, 1);
1406 BIF_undo_push("Select keys");
1408 allqueue(REDRAWVIEW3D, 1);
1411 void PE_selectionCB(short selecting, Object *editobj, short *mval, float rad)
1413 struct { short *mval; float rad; rcti* rect; int select; } data;
1414 ParticleSystem *psys = PE_get_current(OBACT);
1416 if(!PE_can_edit(psys)) return;
1421 data.select=(selecting==LEFTMOUSE);
1423 for_mouse_hit_keys(0,psys,select_key,&data);
1425 draw_sel_circle(0, 0, 0, 0, 0); /* signal */
1428 void PE_do_lasso_select(short mcords[][2], short moves, short select)
1431 ParticleSystem *psys = PE_get_current(ob);
1432 ParticleSystemModifierData *psmd;
1435 ParticleEditKey *key;
1436 float co[3], mat[4][4];
1440 if(!PE_can_edit(psys)) return;
1442 psmd= psys_get_modifier(ob, psys);
1444 totpart=psys->totpart;
1446 LOOP_PARTICLES(i,pa){
1447 if(pa->flag & PARS_HIDE) continue;
1449 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, mat);
1451 if(G.scene->selectmode==SCE_SELECT_POINT){
1453 VECCOPY(co, key->co);
1454 Mat4MulVecfl(mat, co);
1455 project_short(co,vertco);
1456 if(lasso_inside(mcords,moves,vertco[0],vertco[1])){
1457 if(select && !(key->flag & PEK_SELECT)) {
1458 key->flag|=PEK_SELECT;
1459 pa->flag |= PARS_EDIT_RECALC;
1461 else if(key->flag & PEK_SELECT) {
1462 key->flag&=~PEK_SELECT;
1463 pa->flag |= PARS_EDIT_RECALC;
1468 else if(G.scene->selectmode==SCE_SELECT_END){
1469 key = edit->keys[i] + pa->totkey - 1;
1471 VECCOPY(co, key->co);
1472 Mat4MulVecfl(mat, co);
1473 project_short(co,vertco);
1474 if(lasso_inside(mcords,moves,vertco[0],vertco[1])){
1475 if(select && !(key->flag & PEK_SELECT)) {
1476 key->flag|=PEK_SELECT;
1477 pa->flag |= PARS_EDIT_RECALC;
1479 else if(key->flag & PEK_SELECT) {
1480 key->flag&=~PEK_SELECT;
1481 pa->flag |= PARS_EDIT_RECALC;
1487 PE_update_selection(ob, 1);
1489 BIF_undo_push("Lasso select particles");
1491 allqueue(REDRAWVIEW3D, 1);
1493 void PE_hide(int mode)
1495 ParticleSystem *psys = PE_get_current(OBACT);
1500 if(!PE_can_edit(psys)) return;
1503 totpart = psys->totpart;
1505 if(mode == 0){ /* reveal all particles */
1506 LOOP_PARTICLES(i,pa){
1507 pa->flag &= ~PARS_HIDE;
1510 else if(mode == 1){ /* hide unselected particles */
1511 LOOP_PARTICLES(i,pa)
1512 if(particle_is_selected(psys, pa))
1513 pa->flag |= PARS_HIDE;
1515 else{ /* hide selected particles */
1516 LOOP_PARTICLES(i,pa)
1517 if(particle_is_selected(psys, pa))
1518 pa->flag |= PARS_HIDE;
1521 BIF_undo_push("(Un)hide elements");
1523 allqueue(REDRAWVIEW3D, 1);
1525 void PE_select_less(void)
1527 ParticleSystem *psys = PE_get_current(OBACT);
1529 if(!PE_can_edit(psys)) return;
1531 PE_foreach_element(psys,select_less_keys,NULL);
1533 BIF_undo_push("Select less");
1534 allqueue(REDRAWVIEW3D, 1);
1536 void PE_select_more(void)
1538 ParticleSystem *psys = PE_get_current(OBACT);
1540 if(!PE_can_edit(psys)) return;
1542 PE_foreach_element(psys,select_more_keys,NULL);
1544 BIF_undo_push("Select more");
1545 allqueue(REDRAWVIEW3D, 1);
1547 /************************************************/
1549 /************************************************/
1550 static void rekey_element(ParticleSystem *psys, int index, void *userData)
1552 struct { Object *ob; float dval; } *data = userData;
1553 ParticleData *pa = psys->particles + index;
1554 ParticleEdit *edit = psys->edit;
1555 ParticleEditSettings *pset = PE_settings();
1557 HairKey *key, *new_keys;
1558 ParticleEditKey *ekey;
1559 float dval, sta, end;
1562 pa->flag |= PARS_REKEY;
1564 key = new_keys = MEM_callocN(pset->totrekey * sizeof(HairKey),"Hair re-key keys");
1566 /* root and tip stay the same */
1567 VECCOPY(key->co, pa->hair->co);
1568 VECCOPY((key + pset->totrekey - 1)->co, (pa->hair + pa->totkey - 1)->co);
1570 sta = key->time = pa->hair->time;
1571 end = (key + pset->totrekey - 1)->time = (pa->hair + pa->totkey - 1)->time;
1572 dval = (end - sta) / (float)(pset->totrekey - 1);
1574 /* interpolate new keys from old ones */
1575 for(k=1,key++; k<pset->totrekey-1; k++,key++) {
1576 state.time = (float)k / (float)(pset->totrekey-1);
1577 psys_get_particle_on_path(data->ob, psys, index, &state, 0);
1578 VECCOPY(key->co, state.co);
1579 key->time = sta + k * dval;
1584 MEM_freeN(pa->hair);
1585 pa->hair = new_keys;
1587 pa->totkey=pset->totrekey;
1589 if(edit->keys[index])
1590 MEM_freeN(edit->keys[index]);
1591 ekey = edit->keys[index] = MEM_callocN(pa->totkey * sizeof(ParticleEditKey),"Hair re-key edit keys");
1593 for(k=0, key=pa->hair; k<pa->totkey; k++, key++, ekey++) {
1595 ekey->time = &key->time;
1598 pa->flag &= ~PARS_REKEY;
1599 pa->flag |= PARS_EDIT_RECALC;
1604 ParticleSystem *psys = PE_get_current(ob);
1605 ParticleEditSettings *pset = PE_settings();
1606 struct { Object *ob; float dval; } data;
1608 if(!PE_can_edit(psys)) return;
1611 data.dval = 1.0f / (float)(pset->totrekey-1);
1613 foreach_selected_element(psys,rekey_element,&data);
1615 psys->edit->totkeys = psys_count_keys(psys);
1617 recalc_lengths(psys);
1619 PE_update_object(ob, 1);
1621 BIF_undo_push("Re-key particles");
1623 static void rekey_element_to_time(int index, float path_time)
1626 ParticleSystem *psys = PE_get_current(ob);
1627 ParticleEdit *edit=0;
1630 HairKey *new_keys, *key;
1631 ParticleEditKey *ekey;
1638 pa = psys->particles + index;
1640 pa->flag |= PARS_REKEY;
1642 key = new_keys = MEM_dupallocN(pa->hair);
1644 /* interpolate new keys from old ones (roots stay the same) */
1645 for(k=1, key++; k < pa->totkey; k++, key++) {
1646 state.time = path_time * (float)k / (float)(pa->totkey-1);
1647 psys_get_particle_on_path(ob, psys, index, &state, 0);
1648 VECCOPY(key->co, state.co);
1651 /* replace hair keys */
1653 MEM_freeN(pa->hair);
1654 pa->hair = new_keys;
1656 /* update edit pointers */
1657 for(k=0, key=pa->hair, ekey=edit->keys[index]; k<pa->totkey; k++, key++, ekey++) {
1659 ekey->time = &key->time;
1662 pa->flag &= ~PARS_REKEY;
1664 static int remove_tagged_elements(Object *ob, ParticleSystem *psys)
1666 ParticleEdit *edit = psys->edit;
1667 ParticleEditSettings *pset = PE_settings();
1668 ParticleData *pa, *npa=0, *new_pars=0;
1669 ParticleEditKey **key, **nkey=0, **new_keys=0;
1670 ParticleSystemModifierData *psmd;
1671 int i, totpart, new_totpart = psys->totpart, removed = 0;
1673 if(pset->flag & PE_X_MIRROR) {
1675 psmd = psys_get_modifier(ob, psys);
1676 totpart = psys->totpart;
1678 LOOP_PARTICLES(i,pa)
1679 if(pa->flag & PARS_TAG)
1680 PE_mirror_particle(ob, psmd->dm, psys, pa, NULL);
1683 for(i=0, pa=psys->particles; i<psys->totpart; i++, pa++) {
1684 if(pa->flag & PARS_TAG) {
1690 if(new_totpart != psys->totpart) {
1692 npa = new_pars = MEM_callocN(new_totpart * sizeof(ParticleData), "ParticleData array");
1693 nkey = new_keys = MEM_callocN(new_totpart * sizeof(ParticleEditKey *), "ParticleEditKey array");
1696 pa = psys->particles;
1698 for(i=0; i<psys->totpart; i++, pa++, key++) {
1699 if(pa->flag & PARS_TAG) {
1703 MEM_freeN(pa->hair);
1706 memcpy(npa, pa, sizeof(ParticleData));
1707 memcpy(nkey, key, sizeof(ParticleEditKey*));
1713 if(psys->particles) MEM_freeN(psys->particles);
1714 psys->particles = new_pars;
1716 if(edit->keys) MEM_freeN(edit->keys);
1717 edit->keys = new_keys;
1719 if(edit->mirror_cache) {
1720 MEM_freeN(edit->mirror_cache);
1721 edit->mirror_cache = NULL;
1724 psys->totpart = new_totpart;
1726 edit->totkeys = psys_count_keys(psys);
1731 static void remove_tagged_keys(Object *ob, ParticleSystem *psys)
1733 ParticleEdit *edit = psys->edit;
1734 ParticleEditSettings *pset = PE_settings();
1736 HairKey *key, *nkey, *new_keys=0;
1737 ParticleEditKey *ekey;
1738 ParticleSystemModifierData *psmd;
1739 int i, k, totpart = psys->totpart;
1742 if(pset->flag & PE_X_MIRROR) {
1743 /* mirror key tags */
1744 psmd = psys_get_modifier(ob, psys);
1746 LOOP_PARTICLES(i,pa) {
1748 if(ekey->flag & PEK_TAG) {
1749 PE_mirror_particle(ob, psmd->dm, psys, pa, NULL);
1756 LOOP_PARTICLES(i,pa) {
1757 new_totkey = pa->totkey;
1759 if(ekey->flag & PEK_TAG)
1762 /* we can't have elements with less than two keys*/
1764 pa->flag |= PARS_TAG;
1766 remove_tagged_elements(ob, psys);
1768 totpart = psys->totpart;
1770 LOOP_PARTICLES(i,pa) {
1771 new_totkey = pa->totkey;
1773 if(ekey->flag & PEK_TAG)
1776 if(new_totkey != pa->totkey) {
1778 nkey = new_keys = MEM_callocN(new_totkey*sizeof(HairKey), "HairKeys");
1780 for(k=0, ekey=edit->keys[i]; k<new_totkey; k++, key++, nkey++, ekey++) {
1781 while(ekey->flag & PEK_TAG && key < pa->hair + pa->totkey) {
1786 if(key < pa->hair + pa->totkey) {
1787 VECCOPY(nkey->co, key->co);
1788 nkey->time = key->time;
1789 nkey->weight = key->weight;
1793 MEM_freeN(pa->hair);
1795 pa->hair = new_keys;
1797 pa->totkey=new_totkey;
1800 MEM_freeN(edit->keys[i]);
1801 ekey = edit->keys[i] = MEM_callocN(new_totkey*sizeof(ParticleEditKey), "particle edit keys");
1803 for(k=0, key=pa->hair; k<pa->totkey; k++, key++, ekey++) {
1805 ekey->time = &key->time;
1810 edit->totkeys = psys_count_keys(psys);
1812 /* works like normal edit mode subdivide, inserts keys between neighbouring selected keys */
1813 static void subdivide_element(ParticleSystem *psys, int index, void *userData)
1815 struct { Object *ob; } *data = userData;
1816 ParticleEdit *edit = psys->edit;
1817 ParticleData *pa = psys->particles + index;
1820 HairKey *key, *nkey, *new_keys;
1821 ParticleEditKey *ekey, *nekey, *new_ekeys;
1827 for(k=0, ekey=edit->keys[index]; k<pa->totkey-1; k++,ekey++){
1828 if(ekey->flag&PEK_SELECT && (ekey+1)->flag&PEK_SELECT)
1832 if(totnewkey==0) return;
1834 pa->flag |= PARS_REKEY;
1836 nkey = new_keys = MEM_callocN((pa->totkey+totnewkey)*(sizeof(HairKey)),"Hair subdivide keys");
1837 nekey = new_ekeys = MEM_callocN((pa->totkey+totnewkey)*(sizeof(ParticleEditKey)),"Hair subdivide edit keys");
1838 endtime = pa->hair[pa->totkey-1].time;
1840 for(k=0, key=pa->hair, ekey=edit->keys[index]; k<pa->totkey-1; k++, key++, ekey++){
1842 memcpy(nkey,key,sizeof(HairKey));
1843 memcpy(nekey,ekey,sizeof(ParticleEditKey));
1845 nekey->co = nkey->co;
1846 nekey->time = &nkey->time;
1851 if(ekey->flag & PEK_SELECT && (ekey+1)->flag & PEK_SELECT){
1852 nkey->time= (key->time + (key+1)->time)*0.5f;
1853 state.time = (endtime != 0.0f)? nkey->time/endtime: 0.0f;
1854 psys_get_particle_on_path(data->ob, psys, index, &state, 0);
1855 VECCOPY(nkey->co, state.co);
1857 nekey->co= nkey->co;
1858 nekey->time= &nkey->time;
1859 nekey->flag |= PEK_SELECT;
1865 /*tip still not copied*/
1866 memcpy(nkey,key,sizeof(HairKey));
1867 memcpy(nekey,ekey,sizeof(ParticleEditKey));
1869 nekey->co = nkey->co;
1870 nekey->time = &nkey->time;
1873 MEM_freeN(pa->hair);
1874 pa->hair = new_keys;
1876 if(edit->keys[index])
1877 MEM_freeN(edit->keys[index]);
1879 edit->keys[index] = new_ekeys;
1881 pa->totkey += totnewkey;
1882 pa->flag |= PARS_EDIT_RECALC;
1883 pa->flag &= ~PARS_REKEY;
1885 void PE_subdivide(void)
1888 ParticleSystem *psys = PE_get_current(ob);
1889 struct { Object *ob; } data;
1891 if(!PE_can_edit(psys)) return;
1894 PE_foreach_element(psys,subdivide_element,&data);
1896 psys->edit->totkeys = psys_count_keys(psys);
1898 recalc_lengths(psys);
1899 PE_recalc_world_cos(ob, psys);
1901 PE_update_object(ob, 1);
1903 BIF_undo_push("Subdivide hair(s)");
1905 void PE_remove_doubles(void)
1908 ParticleSystem *psys=PE_get_current(ob);
1909 ParticleEditSettings *pset=PE_settings();
1912 ParticleSystemModifierData *psmd;
1914 KDTreeNearest nearest[10];
1915 float mat[4][4], co[3];
1916 int i, n, totn, removed, totpart, flag, totremoved;
1918 if(!PE_can_edit(psys)) return;
1921 psmd= psys_get_modifier(ob, psys);
1927 totpart= psys->totpart;
1928 tree=BLI_kdtree_new(totpart);
1930 /* insert particles into kd tree */
1931 LOOP_PARTICLES(i,pa) {
1932 if(particle_is_selected(psys, pa)) {
1933 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, pa, mat);
1934 VECCOPY(co, pa->hair[0].co);
1935 Mat4MulVecfl(mat, co);
1936 BLI_kdtree_insert(tree, i, co, NULL);
1940 BLI_kdtree_balance(tree);
1942 /* tag particles to be removed */
1943 LOOP_PARTICLES(i,pa) {
1944 if(particle_is_selected(psys, pa)) {
1945 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, pa, mat);
1946 VECCOPY(co, pa->hair[0].co);
1947 Mat4MulVecfl(mat, co);
1949 totn= BLI_kdtree_find_n_nearest(tree,10,co,NULL,nearest);
1951 for(n=0; n<totn; n++) {
1952 /* this needs a custom threshold still */
1953 if(nearest[n].index > i && nearest[n].dist < 0.0002f) {
1954 if(!(pa->flag & PARS_TAG)) {
1955 pa->flag |= PARS_TAG;
1963 BLI_kdtree_free(tree);
1965 /* remove tagged particles - don't do mirror here! */
1967 pset->flag &= ~PE_X_MIRROR;
1968 remove_tagged_elements(ob, psys);
1970 totremoved += removed;
1974 notice("Removed: %d", totremoved);
1976 PE_recalc_world_cos(ob, psys);
1977 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1978 allqueue(REDRAWVIEW3D, 1);
1979 BIF_undo_push("Remove double particles");
1982 /************************************************/
1984 /************************************************/
1985 static void brush_comb(ParticleSystem *psys, float mat[][4], float imat[][4], int pa_index, int key_index, void *userData)
1987 struct {Object *ob; short *mval; float rad; rcti* rect; float dist; float *dvec; float combfac;} *data = userData;
1988 ParticleData *pa= &psys->particles[pa_index];
1989 ParticleEditSettings *pset= PE_settings();
1990 HairKey *key = pa->hair + key_index;
1993 if(pset->flag & PE_LOCK_FIRST && key_index == 0) return;
1995 fac = (float)pow((double)(1.0f - data->dist / data->rad), (double)data->combfac);
1997 VECCOPY(cvec,data->dvec);
1998 Mat4Mul3Vecfl(imat,cvec);
2000 VECADD(key->co, key->co, cvec);
2002 pa->flag |= PARS_EDIT_RECALC;
2004 static void brush_cut(ParticleSystem *psys, int index, void *userData)
2006 struct { short *mval; float rad; rcti* rect; int selected; float cutfac;} *data = userData;
2007 ParticleData *pa= &psys->particles[index];
2008 ParticleCacheKey *key = psys->pathcache[index];
2009 float rad2, cut_time = 1.0;
2010 float x0, x1, v0, v1, o0, o1, xo0, xo1, d, dv;
2011 int k, cut, keys = (int)pow(2.0, (double)psys->part->draw_step);
2014 /* blunt scissors */
2015 if(BLI_frand() > data->cutfac) return;
2017 rad2 = data->rad * data->rad;
2021 project_short(key->co, vertco);
2022 x0 = (float)vertco[0];
2023 x1 = (float)vertco[1];
2025 o0 = (float)data->mval[0];
2026 o1 = (float)data->mval[1];
2031 /* check if root is inside circle */
2032 if(xo0*xo0 + xo1*xo1 < rad2) {
2037 /* calculate path time closest to root that was inside the circle */
2038 for(k=1, key++; k<=keys; k++, key++){
2039 project_short(key->co, vertco);
2041 v0 = (float)vertco[0] - x0;
2042 v1 = (float)vertco[1] - x1;
2046 d = (v0*xo1 - v1*xo0);
2048 d = dv * rad2 - d*d;
2053 cut_time = -(v0*xo0 + v1*xo1 + d);
2055 if(cut_time > 0.0f) {
2058 if(cut_time < 1.0f) {
2059 cut_time += (float)(k-1);
2060 cut_time /= (float)keys;
2067 x0 = (float)vertco[0];
2068 x1 = (float)vertco[1];
2076 if(cut_time < 0.0f) {
2077 pa->flag |= PARS_TAG;
2080 rekey_element_to_time(index, cut_time);
2081 pa->flag |= PARS_EDIT_RECALC;
2085 static void brush_length(ParticleSystem *psys, int index, void *userData)
2087 struct { short *mval; float rad; rcti* rect; float dist; float growfac; } *data = userData;
2088 ParticleData *pa = &psys->particles[index];
2090 float dvec[3],pvec[3];
2094 VECCOPY(pvec,key->co);
2096 for(k=1, key++; k<pa->totkey; k++,key++){
2097 VECSUB(dvec,key->co,pvec);
2098 VECCOPY(pvec,key->co);
2099 VecMulf(dvec,data->growfac);
2100 VECADD(key->co,(key-1)->co,dvec);
2103 pa->flag |= PARS_EDIT_RECALC;
2105 static void brush_puff(ParticleSystem *psys, int index, void *userData)
2107 struct { short *mval; float rad; rcti* rect; float dist;
2108 Object *ob; DerivedMesh *dm; float pufffac; int invert; } *data = userData;
2109 ParticleData *pa = &psys->particles[index];
2110 ParticleEdit *edit = psys->edit;
2112 float mat[4][4], imat[4][4];
2113 float lastco[3], rootco[3], co[3], nor[3], kco[3], dco[3], fac, length;
2116 psys_mat_hair_to_global(data->ob, data->dm, psys->part->from, pa, mat);
2117 Mat4Invert(imat,mat);
2119 /* find root coordinate and normal on emitter */
2121 VECCOPY(co, key->co);
2122 Mat4MulVecfl(mat, co);
2124 index= BLI_kdtree_find_nearest(edit->emitter_field, co, NULL, NULL);
2125 if(index == -1) return;
2127 VECCOPY(rootco, co);
2128 VecCopyf(nor, &psys->edit->emitter_cosnos[index*6+3]);
2132 fac= (float)pow((double)(1.0f - data->dist / data->rad), (double)data->pufffac);
2137 for(k=1, key++; k<pa->totkey; k++, key++){
2138 /* compute position as if hair was standing up straight */
2139 VECCOPY(lastco, co);
2140 VECCOPY(co, key->co);
2141 Mat4MulVecfl(mat, co);
2142 length += VecLenf(lastco, co);
2144 VECADDFAC(kco, rootco, nor, length);
2146 /* blend between the current and straight position */
2147 VECSUB(dco, kco, co);
2148 VECADDFAC(co, co, dco, fac);
2150 VECCOPY(key->co, co);
2151 Mat4MulVecfl(imat, key->co);
2154 pa->flag |= PARS_EDIT_RECALC;
2156 static void brush_smooth_get(ParticleSystem *psys, float mat[][4], float imat[][4], int pa_index, int key_index, void *userData)
2158 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float vec[3]; int tot; float smoothfac;} *data = userData;
2159 ParticleData *pa= &psys->particles[pa_index];
2160 HairKey *key = pa->hair + key_index;
2165 VecSubf(dvec,key->co,(key-1)->co);
2166 Mat4Mul3Vecfl(mat,dvec);
2167 VECADD(data->vec,data->vec,dvec);
2171 static void brush_smooth_do(ParticleSystem *psys, float mat[][4], float imat[][4], int pa_index, int key_index, void *userData)
2173 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float vec[3]; int tot; float smoothfac;} *data = userData;
2174 ParticleData *pa= &psys->particles[pa_index];
2175 HairKey *key = pa->hair + key_index;
2176 float vec[3], dvec[3];
2179 VECCOPY(vec,data->vec);
2180 Mat4Mul3Vecfl(imat,vec);
2182 VecSubf(dvec,key->co,(key-1)->co);
2184 VECSUB(dvec,vec,dvec);
2185 VecMulf(dvec,data->smoothfac);
2187 VECADD(key->co,key->co,dvec);
2190 pa->flag |= PARS_EDIT_RECALC;
2192 #define EXPERIMENTAL_DEFORM_ONLY_PAINTING 1
2193 static void brush_add(Object *ob, ParticleSystem *psys, short *mval, short number)
2195 ParticleData *add_pars = MEM_callocN(number*sizeof(ParticleData),"ParticleData add");
2196 ParticleSystemModifierData *psmd = psys_get_modifier(ob,psys);
2197 ParticleEditSettings *pset= PE_settings();
2198 ParticleEdit *edit = psys->edit;
2199 int i, k, n = 0, totpart = psys->totpart;
2200 short dmx = 0, dmy = 0;
2201 short mx = mval[0] - curarea->winx / 2, my = mval[1] - curarea->winy / 2;
2202 float co1[3], co2[3], vec[4], min_d, imat[4][4];
2203 float framestep, timestep = psys_get_timestep(psys->part);
2204 short size = pset->brush[PE_BRUSH_ADD].size;
2205 short size2 = size*size;
2206 #if EXPERIMENTAL_DEFORM_ONLY_PAINTING
2209 Mat4Invert(imat,ob->obmat);
2211 BLI_srandom(psys->seed+mval[0]+mval[1]);
2213 /* painting onto the deformed mesh, could be an option? */
2214 #if EXPERIMENTAL_DEFORM_ONLY_PAINTING
2215 if (psmd->dm->deformedOnly)
2218 dm = mesh_get_derived_deform(ob, CD_MASK_BAREMESH);
2220 for(i=0; i<number; i++){
2223 while(dmx*dmx+dmy*dmy>size2){
2224 dmx=(short)((2.0f*BLI_frand()-1.0f)*size);
2225 dmy=(short)((2.0f*BLI_frand()-1.0f)*size);
2229 /* create intersection coordinates in view Z direction at mouse coordinates */
2230 /* Thanks to who ever wrote the "Mouse Location 3D Space" tutorial in "Blender 3D: Blending Into Python/Cookbook". */
2232 vec[0]= (2.0f*(mx+dmx)/curarea->winx);
2233 vec[1]= (2.0f*(my+dmy)/curarea->winy);
2237 Mat4MulVec4fl(G.vd->persinv, vec);
2238 VecMulf(vec, 1.0f/vec[3]);
2240 VECCOPY(co1, G.vd->viewinv[3]);
2241 VECSUB(vec, vec, co1);
2244 VECADDFAC(co1, G.vd->viewinv[3], vec, G.vd->near);
2245 VECADDFAC(co2, G.vd->viewinv[3], vec, G.vd->far);
2248 vec[0] = 2.0f*(mx+dmx)/curarea->winx;
2249 vec[1] = 2.0f*(my+dmy)/curarea->winy;
2253 Mat4MulVec4fl(G.vd->persinv,vec);
2255 VECADDFAC(co1,vec,G.vd->viewinv[2],1000.0f);
2256 VECADDFAC(co2,vec,G.vd->viewinv[2],-1000.0f);
2259 Mat4MulVecfl(imat,co1);
2260 Mat4MulVecfl(imat,co2);
2263 /* warning, returns the derived mesh face */
2264 #if EXPERIMENTAL_DEFORM_ONLY_PAINTING
2265 if(psys_intersect_dm(ob,dm,0,co1,co2,&min_d,&add_pars[n].num,add_pars[n].fuv,0,0,0,0)) {
2266 add_pars[n].num_dmcache= psys_particle_dm_face_lookup(ob,psmd->dm,add_pars[n].num,add_pars[n].fuv,NULL);
2271 if (psmd->dm->deformedOnly) {
2272 if(psys_intersect_dm(ob,psmd->dm,0,co1,co2,&min_d,&add_pars[n].num,add_pars[n].fuv,0,0,0,0)){
2276 /* we need to test against the cage mesh, because 1) its faster and 2) then we can avoid converting the fuv back which is not simple */
2277 if(psys_intersect_dm(ob,psmd->dm,0,co1,co2,&min_d,&add_pars[n].num,add_pars[n].fuv,0,0,0,0)){
2279 float fuv_mod[3] = {0.0, 0.0, 0.0};
2280 OrigSpaceFace *osface;
2282 mface= psmd->dm->getFaceData(psmd->dm,add_pars[n].num,CD_MFACE);
2283 osface= psmd->dm->getFaceData(psmd->dm, add_pars[n].num, CD_ORIGSPACE);
2285 add_pars[n].fuv[2]=0.0;
2287 /* use the original index for num and the derived index for num_dmcache */
2288 add_pars[n].num_dmcache = add_pars[n].num;
2289 add_pars[n].num = *(int *)psmd->dm->getFaceData(psmd->dm, add_pars[n].num, CD_ORIGINDEX);
2291 /* This is totally unaceptable code (fakeing mesh dara) but changing the target function isnt really nice either, do this temporarily */
2292 if (1) { /* Evilness*/
2294 MVert mvert_fake[4];
2296 //test1 = add_pars[n].num_dmcache;
2297 //test2 = add_pars[n].num;
2299 mvert_fake[0].co[2] = mvert_fake[1].co[2] = mvert_fake[2].co[2] = mvert_fake[3].co[2] = 0.0;
2311 Vec2Copyf(mvert_fake[0].co, osface->uv[0]);
2312 Vec2Copyf(mvert_fake[1].co, osface->uv[1]);
2313 Vec2Copyf(mvert_fake[2].co, osface->uv[2]);
2314 Vec2Copyf(mvert_fake[3].co, osface->uv[3]);
2315 //printf("before %f %f %i %i\n", add_pars[n].fuv[0], add_pars[n].fuv[1], test1, test2);
2316 psys_interpolate_face(&mvert_fake, &mface_fake, NULL, &add_pars[n].fuv, &fuv_mod, NULL, NULL, NULL);
2318 /* Apply as the UV */
2319 Vec2Copyf(add_pars[n].fuv, fuv_mod);
2320 //printf("after %f %f\n", add_pars[n].fuv[0], add_pars[n].fuv[1]);
2322 /* Make a fake face, for calculating the derived face's fuv on the original face */
2323 //PointInFace2DUV(mface->v4, osface->uv[0], osface->uv[1], osface->uv[2], osface->uv[3], add_pars[n].fuv, fuv_mod);
2324 //Vec2Copyf(add_pars[n].fuv, fuv_mod);
2333 int newtotpart=totpart+n;
2334 float hairmat[4][4], cur_co[3];
2336 ParticleData *pa, *new_pars = MEM_callocN(newtotpart*sizeof(ParticleData),"ParticleData new");
2337 ParticleEditKey *ekey, **key, **new_keys = MEM_callocN(newtotpart*sizeof(ParticleEditKey *),"ParticleEditKey array new");
2340 /* save existing elements */
2341 memcpy(new_pars, psys->particles, totpart * sizeof(ParticleData));
2342 memcpy(new_keys, edit->keys, totpart * sizeof(ParticleEditKey*));
2344 /* change old arrays to new ones */
2345 if(psys->particles) MEM_freeN(psys->particles);
2346 psys->particles = new_pars;
2348 if(edit->keys) MEM_freeN(edit->keys);
2349 edit->keys = new_keys;
2351 if(edit->mirror_cache) {
2352 MEM_freeN(edit->mirror_cache);
2353 edit->mirror_cache = NULL;
2356 /* create tree for interpolation */
2357 if(pset->flag & PE_INTERPOLATE_ADDED && psys->totpart){
2358 tree=BLI_kdtree_new(psys->totpart);
2360 for(i=0, pa=psys->particles; i<totpart; i++, pa++) {
2361 psys_particle_on_dm(ob,psmd->dm,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,cur_co,0,0,0,0,0);
2362 BLI_kdtree_insert(tree, i, cur_co, NULL);
2365 BLI_kdtree_balance(tree);
2368 psys->totpart = newtotpart;
2370 /* create new elements */
2371 pa = psys->particles + totpart;
2372 key = edit->keys + totpart;
2374 for(i=totpart; i<newtotpart; i++, pa++, key++){
2375 memcpy(pa, add_pars + i - totpart, sizeof(ParticleData));
2376 pa->hair = MEM_callocN(pset->totaddkey * sizeof(HairKey), "BakeKey key add");
2377 ekey = *key = MEM_callocN(pset->totaddkey * sizeof(ParticleEditKey), "ParticleEditKey add");
2378 pa->totkey = pset->totaddkey;
2380 for(k=0, hkey=pa->hair; k<pa->totkey; k++, hkey++, ekey++) {
2381 ekey->co = hkey->co;
2382 ekey->time = &hkey->time;
2385 initialize_particle(pa,i,ob,psys,psmd);
2386 reset_particle(pa,psys,psmd,ob,0.0,1.0,0,0,0);
2387 pa->flag |= PARS_EDIT_RECALC;
2388 if(pset->flag & PE_X_MIRROR)
2389 pa->flag |= PARS_TAG; /* signal for duplicate */
2391 framestep = pa->lifetime/(float)(pset->totaddkey-1);
2396 KDTreeNearest ptn[3];
2398 float maxd, mind, dd, totw=0.0, weight[3];
2400 psys_particle_on_dm(ob,psmd->dm,psys->part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,0,0);
2401 maxw = BLI_kdtree_find_n_nearest(tree,3,co1,NULL,ptn);
2403 maxd = ptn[maxw-1].dist;
2407 for(w=0; w<maxw; w++){
2408 weight[w] = (float)pow(2.0, (double)(-6.0f * ptn[w].dist / maxd));
2415 for(w=0; w<maxw; w++)
2418 for(k=0; k<pset->totaddkey; k++) {
2419 hkey = pa->hair + k;
2420 hkey->time = pa->time + k * framestep;
2422 key[0].time = hkey->time/ 100.0f;
2423 psys_get_particle_on_path(ob, psys, ptn[0].index, key, 0);
2424 VecMulf(key[0].co, weight[0]);
2427 key[1].time = key[0].time;
2428 psys_get_particle_on_path(ob, psys, ptn[1].index, key + 1, 0);
2429 VecMulf(key[1].co, weight[1]);
2430 VECADD(key[0].co, key[0].co, key[1].co);
2433 key[2].time = key[0].time;
2434 psys_get_particle_on_path(ob, psys, ptn[2].index, key + 2, 0);
2435 VecMulf(key[2].co, weight[2]);
2436 VECADD(key[0].co, key[0].co, key[2].co);
2441 VECSUB(co1, pa->state.co, key[0].co);
2443 VECADD(pa->hair[k].co, key[0].co, co1);
2445 pa->hair[k].time = key[0].time;
2449 for(k=0, hkey=pa->hair; k<pset->totaddkey; k++, hkey++) {
2450 VECADDFAC(hkey->co, pa->state.co, pa->state.vel, k * framestep * timestep);
2451 pa->hair[k].time += k * framestep;
2454 for(k=0, hkey=pa->hair; k<pset->totaddkey; k++, hkey++) {
2455 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, hairmat);
2456 Mat4Invert(imat,hairmat);
2457 Mat4MulVecfl(imat, hkey->co);
2460 edit->totkeys = psys_count_keys(psys);
2463 BLI_kdtree_free(tree);
2466 MEM_freeN(add_pars);
2468 /* painting onto the deformed mesh, could be an option? */
2469 #if EXPERIMENTAL_DEFORM_ONLY_PAINTING
2470 if (!psmd->dm->deformedOnly)
2474 static void brush_weight(ParticleSystem *psys, float mat[][4], float imat[][4], int pa_index, int key_index, void *userData)
2476 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float weightfac;} *data = userData;
2479 /* roots have full weight allways */
2481 pa= &psys->particles[pa_index];
2482 pa->hair[key_index].weight = data->weightfac;
2483 pa->flag |= PARS_EDIT_RECALC;
2487 /* returns 0 if no brush was used */
2488 int PE_brush_particles(void)
2491 ParticleSystem *psys = PE_get_current(ob);
2493 ParticleEditSettings *pset = PE_settings();
2494 ParticleSystemModifierData *psmd;
2495 ParticleBrushData *brush;
2496 float vec1[3], vec2[3];
2497 short mval[2], mvalo[2], firsttime = 1, dx, dy;
2498 int selected = 0, flip, removed = 0;
2500 if(!PE_can_edit(psys)) return 0;
2503 psmd= psys_get_modifier(ob, psys);
2505 flip= (get_qual() == LR_SHIFTKEY);
2507 if(pset->brushtype<0) return 0;
2508 brush= &pset->brush[pset->brushtype];
2510 initgrabz(ob->obmat[3][0], ob->obmat[3][1], ob->obmat[3][2]);
2512 getmouseco_areawin(mvalo);
2514 mval[0] = mvalo[0]; mval[1] = mvalo[1];
2516 while(get_mbut() & L_MOUSE){
2518 glReadBuffer(GL_BACK);
2519 glDrawBuffer(GL_BACK);
2522 dx=mval[0]-mvalo[0];
2523 dy=mval[1]-mvalo[1];
2524 if(((pset->brushtype == PE_BRUSH_ADD) ?
2525 (sqrt(dx * dx + dy * dy) > pset->brush[PE_BRUSH_ADD].step) : (dx != 0 || dy != 0))
2529 selected = (short)count_selected_keys(psys);
2531 switch(pset->brushtype){
2534 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float *dvec; float combfac;} data;
2538 data.rad = (float)brush->size;
2540 data.combfac = (float)(brush->strength - 50) / 50.0f;
2541 if(data.combfac < 0.0f)
2542 data.combfac = 1.0f - 9.0f * data.combfac;
2544 data.combfac = 1.0f - data.combfac;
2546 Mat4Invert(ob->imat, ob->obmat);
2548 window_to_3d(vec1, mvalo[0], mvalo[1]);
2549 window_to_3d(vec2, mval[0], mval[1]);
2550 VECSUB(vec1, vec2, vec1);
2553 foreach_mouse_hit_key(selected, psys,brush_comb, &data);
2558 struct { short *mval; float rad; rcti* rect; int selected; float cutfac;} data;
2561 data.rad = (float)brush->size;
2563 data.selected = selected;
2565 data.cutfac = (float)(brush->strength / 100.0f);
2568 foreach_selected_element(psys, brush_cut, &data);
2570 PE_foreach_element(psys, brush_cut, &data);
2572 removed= remove_tagged_elements(ob, psys);
2573 if(pset->flag & PE_KEEP_LENGTHS)
2574 recalc_lengths(psys);
2577 case PE_BRUSH_LENGTH:
2579 struct { short *mval; float rad; rcti* rect; float dist; float growfac; } data;
2583 data.rad = (float)brush->size;
2584 data.growfac = (float)brush->strength / 5000.0f;
2586 if(brush->invert ^ flip)
2587 data.growfac = 1.0f - data.growfac;
2589 data.growfac = 1.0f + data.growfac;
2591 foreach_mouse_hit_element(selected, psys, brush_length, &data);
2593 if(pset->flag & PE_KEEP_LENGTHS)
2594 recalc_lengths(psys);
2599 struct { short *mval; float rad; rcti* rect; float dist;
2600 Object *ob; DerivedMesh *dm; float pufffac; int invert; } data;
2605 data.rad = (float)brush->size;
2607 data.pufffac = (float)(brush->strength - 50) / 50.0f;
2608 if(data.pufffac < 0.0f)
2609 data.pufffac = 1.0f - 9.0f * data.pufffac;
2611 data.pufffac = 1.0f - data.pufffac;
2613 data.invert= (brush->invert ^ flip);
2614 Mat4Invert(ob->imat, ob->obmat);
2616 foreach_mouse_hit_element(selected, psys, brush_puff, &data);
2620 if(psys->part->from==PART_FROM_FACE){
2621 brush_add(ob, psys, mval, brush->strength);
2622 if(pset->flag & PE_KEEP_LENGTHS)
2623 recalc_lengths(psys);
2626 case PE_BRUSH_WEIGHT:
2628 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float weightfac;} data;
2632 data.rad = (float)brush->size;
2634 data.weightfac = (float)(brush->strength / 100.0f);
2636 foreach_mouse_hit_key(selected, psys, brush_weight, &data);
2639 case PE_BRUSH_SMOOTH:
2641 struct { Object *ob; short *mval; float rad; rcti* rect; float dist; float vec[3]; int tot; float smoothfac;} data;
2645 data.rad = (float)brush->size;
2647 data.vec[0] = data.vec[1] = data.vec[2] = 0.0f;
2650 data.smoothfac = (float)(brush->strength / 100.0f);
2652 Mat4Invert(ob->imat, ob->obmat);
2654 foreach_mouse_hit_key(selected, psys, brush_smooth_get, &data);
2657 VecMulf(data.vec, 1.0f / (float)data.tot);
2658 foreach_mouse_hit_key(selected, psys, brush_smooth_do, &data);
2664 if((pset->flag & PE_KEEP_LENGTHS)==0)
2665 recalc_lengths(psys);
2667 if(pset->brushtype == PE_BRUSH_ADD || removed) {
2668 if(pset->brushtype == PE_BRUSH_ADD && (pset->flag & PE_X_MIRROR))
2670 PE_recalc_world_cos(ob,psys);
2671 psys_free_path_cache(psys);
2672 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2675 PE_update_object(ob, 1);
2685 getmouseco_areawin(mval);
2687 allqueue(REDRAWVIEW3D, 1);
2689 BIF_undo_push("Brush edit particles");
2693 static void set_delete_particle(ParticleSystem *psys, int index, void *userData)
2695 psys->particles[index].flag |= PARS_TAG;
2697 static void set_delete_particle_key(ParticleSystem *psys, int pa_index, int key_index, void *userData)
2699 psys->edit->keys[pa_index][key_index].flag |= PEK_TAG;
2701 void PE_delete_particle(void)
2704 ParticleSystem *psys = PE_get_current(ob);
2707 if(!PE_can_edit(psys)) return;
2709 event= pupmenu("Erase %t|Particle%x2|Key%x1");
2714 foreach_selected_key(psys, set_delete_particle_key, 0);
2715 remove_tagged_keys(ob, psys);
2716 recalc_lengths(psys);
2719 foreach_selected_element(psys, set_delete_particle, 0);
2720 remove_tagged_elements(ob, psys);
2721 recalc_lengths(psys);
2724 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2725 allqueue(REDRAWVIEW3D, 1);
2726 BIF_undo_push("Delete particles/keys");
2729 void PE_mirror_x(int tagged)
2732 Mesh *me= (Mesh*)(ob->data);
2733 ParticleSystemModifierData *psmd;
2734 ParticleSystem *psys = PE_get_current(ob);
2736 ParticleData *pa, *newpa, *new_pars;
2737 ParticleEditKey *ekey, **newkey, **key, **new_keys;
2740 int i, k, rotation, totpart, newtotpart;
2742 if(!PE_can_edit(psys)) return;
2745 psmd= psys_get_modifier(ob, psys);
2747 mirrorfaces= mesh_get_x_mirror_faces(ob);
2749 if(!edit->mirror_cache)
2750 PE_update_mirror_cache(ob, psys);
2752 totpart= psys->totpart;
2753 newtotpart= psys->totpart;
2754 LOOP_PARTICLES(i,pa) {
2755 if(pa->flag&PARS_HIDE) continue;
2758 if(particle_is_selected(psys, pa)) {
2759 if(edit->mirror_cache[i] != -1) {
2760 /* already has a mirror, don't need to duplicate */
2761 PE_mirror_particle(ob, psmd->dm, psys, pa, NULL);
2765 pa->flag |= PARS_TAG;
2769 if((pa->flag & PARS_TAG) && mirrorfaces[pa->num*2] != -1)
2773 if(newtotpart != psys->totpart) {
2774 /* allocate new arrays and copy existing */
2775 new_pars= MEM_callocN(newtotpart*sizeof(ParticleData), "ParticleData new");
2776 new_keys= MEM_callocN(newtotpart*sizeof(ParticleEditKey*), "ParticleEditKey new");
2778 memcpy(new_pars, psys->particles, totpart*sizeof(ParticleData));
2779 memcpy(new_keys, edit->keys, totpart*sizeof(ParticleEditKey*));
2781 if(psys->particles) MEM_freeN(psys->particles);
2782 psys->particles= new_pars;
2784 if(edit->keys) MEM_freeN(edit->keys);
2785 edit->keys= new_keys;
2787 if(edit->mirror_cache) {
2788 MEM_freeN(edit->mirror_cache);
2789 edit->mirror_cache= NULL;
2792 psys->totpart= newtotpart;
2794 /* create new elements */
2795 pa= psys->particles;
2796 newpa= psys->particles + totpart;
2798 newkey= edit->keys + totpart;
2800 for(i=0; i<totpart; i++, pa++, key++) {
2801 if(pa->flag&PARS_HIDE) continue;
2803 if(!(pa->flag & PARS_TAG) || mirrorfaces[pa->num*2] == -1)
2808 if(pa->hair) newpa->hair= MEM_dupallocN(pa->hair);
2809 if(pa->keys) newpa->keys= MEM_dupallocN(pa->keys);
2810 if(*key) *newkey= MEM_dupallocN(*key);
2812 /* rotate weights according to vertex index rotation */
2813 rotation= mirrorfaces[pa->num*2+1];
2814 newpa->fuv[0]= pa->fuv[2];
2815 newpa->fuv[1]= pa->fuv[1];
2816 newpa->fuv[2]= pa->fuv[0];
2817 newpa->fuv[3]= pa->fuv[3];
2818 while(rotation-- > 0)
2819 if(me->mface[pa->num].v4)
2820 SHIFT4(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2], newpa->fuv[3])
2822 SHIFT3(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2])
2824 /* assign face inddex */
2825 newpa->num= mirrorfaces[pa->num*2];
2826 newpa->num_dmcache= psys_particle_dm_face_lookup(ob,psmd->dm,newpa->num,newpa->fuv, NULL);
2828 /* update edit key pointers */
2830 for(k=0, hkey=newpa->hair; k<newpa->totkey; k++, hkey++, ekey++) {
2832 ekey->time= &hkey->time;
2835 /* map key positions as mirror over x axis */
2836 PE_mirror_particle(ob, psmd->dm, psys, pa, newpa);
2842 edit->totkeys = psys_count_keys(psys);
2845 for(pa=psys->particles, i=0; i<psys->totpart; i++, pa++)
2846 pa->flag &= ~PARS_TAG;
2848 MEM_freeN(mirrorfaces);
2851 PE_recalc_world_cos(ob,psys);
2852 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2853 allqueue(REDRAWVIEW3D, 1);
2854 BIF_undo_push("Mirror particles");
2858 void PE_selectbrush_menu(void)
2860 ParticleEditSettings *pset= PE_settings();
2863 pupmenu_set_active(pset->brushtype);
2865 val= pupmenu("Select Brush%t|None %x0|Comb %x1|Smooth %x7|Weight %x6|Add %x5|Length %x3|Puff %x4|Cut %x2");
2868 pset->brushtype= val-1;
2869 allqueue(REDRAWVIEW3D, 1);
2873 /************************************************/
2874 /* Particle Edit Undo */
2875 /************************************************/
2876 static void free_ParticleUndo(ParticleUndo *undo)
2881 for(i=0, pa=undo->particles; i<undo->totpart; i++, pa++) {
2883 MEM_freeN(pa->hair);
2885 MEM_freeN(undo->keys[i]);
2888 MEM_freeN(undo->keys);
2891 MEM_freeN(undo->particles);
2893 //if(undo->emitter_cosnos)
2894 // MEM_freeN(undo->emitter_cosnos);
2896 static void make_ParticleUndo(ParticleSystem *psys, ParticleUndo *undo)
2898 ParticleData *pa,*upa;
2901 undo->totpart = psys->totpart;
2902 undo->totkeys = psys->edit->totkeys;
2904 upa = undo->particles = MEM_dupallocN(psys->particles);
2905 undo->keys = MEM_dupallocN(psys->edit->keys);
2907 for(i=0, pa=psys->particles; i<undo->totpart; i++, pa++, upa++) {
2908 upa->hair = MEM_dupallocN(pa->hair);
2909 undo->keys[i] = MEM_dupallocN(psys->edit->keys[i]);
2910 /* no need to update edit key->co & key->time pointers here */
2913 static void get_ParticleUndo(ParticleSystem *psys, ParticleUndo *undo)
2915 ParticleData *pa, *upa;
2916 ParticleEditKey *key;
2918 int i, k, totpart = psys->totpart;
2920 LOOP_PARTICLES(i,pa) {
2922 MEM_freeN(pa->hair);
2924 if(psys->edit->keys[i])
2925 MEM_freeN(psys->edit->keys[i]);
2928 MEM_freeN(psys->particles);
2929 if(psys->edit->keys)
2930 MEM_freeN(psys->edit->keys);
2931 if(psys->edit->mirror_cache) {
2932 MEM_freeN(psys->edit->mirror_cache);
2933 psys->edit->mirror_cache= NULL;
2936 pa = psys->particles = MEM_dupallocN(undo->particles);
2937 psys->edit->keys = MEM_dupallocN(undo->keys);
2939 for(i=0,upa=undo->particles; i<undo->totpart; i++, upa++, pa++){
2940 hkey = pa->hair = MEM_dupallocN(upa->hair);
2941 key = psys->edit->keys[i] = MEM_dupallocN(undo->keys[i]);
2942 for(k=0; k<pa->totkey; k++, hkey++, key++) {
2944 key->time = &hkey->time;
2948 psys->totpart = undo->totpart;
2949 psys->edit->totkeys = undo->totkeys;
2951 void PE_undo_push(char *str)
2953 ParticleSystem *psys = PE_get_current(OBACT);
2954 ParticleEdit *edit = 0;
2958 if(!PE_can_edit(psys)) return;
2961 /* remove all undos after (also when curundo==NULL) */
2962 while(edit->undo.last != edit->curundo) {
2963 undo= edit->undo.last;
2964 BLI_remlink(&edit->undo, undo);
2965 free_ParticleUndo(undo);
2970 edit->curundo= undo= MEM_callocN(sizeof(ParticleUndo), "particle undo file");
2971 strncpy(undo->name, str, 64-1);
2972 BLI_addtail(&edit->undo, undo);
2974 /* and limit amount to the maximum */
2976 undo= edit->undo.last;
2979 if(nr==U.undosteps) break;
2983 while(edit->undo.first!=undo) {
2984 ParticleUndo *first= edit->undo.first;
2985 BLI_remlink(&edit->undo, first);
2986 free_ParticleUndo(first);
2992 make_ParticleUndo(psys,edit->curundo);
2994 void PE_undo_step(int step)
2996 ParticleSystem *psys = PE_get_current(OBACT);
2997 ParticleEdit *edit = 0;
2999 if(!PE_can_edit(psys)) return;
3003 get_ParticleUndo(psys,edit->curundo);
3007 if(edit->curundo==NULL || edit->curundo->prev==NULL) error("No more steps to undo");
3009 if(G.f & G_DEBUG) printf("undo %s\n", edit->curundo->name);
3010 edit->curundo= edit->curundo->prev;
3011 get_ParticleUndo(psys, edit->curundo);
3015 /* curundo has to remain current situation! */
3017 if(edit->curundo==NULL || edit->curundo->next==NULL) error("No more steps to redo");
3019 get_ParticleUndo(psys, edit->curundo->next);
3020 edit->curundo= edit->curundo->next;
3021 if(G.f & G_DEBUG) printf("redo %s\n", edit->curundo->name);
3025 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
3026 allqueue(REDRAWVIEW3D, 1);
3027 allqueue(REDRAWBUTSEDIT, 0);
3028 allqueue(REDRAWIMAGE, 0);
3030 static void ParticleUndo_number(ParticleEdit *edit, int nr)
3035 for(undo= edit->undo.first; undo; undo= undo->next, a++) {
3038 edit->curundo= undo;
3041 static void ParticleUndo_clear(ParticleSystem *psys)
3052 undo= edit->undo.first;
3054 free_ParticleUndo(undo);
3057 BLI_freelistN(&edit->undo);
3058 edit->curundo= NULL;
3068 void PE_undo_menu(void)
3070 ParticleSystem *psys = PE_get_current(OBACT);
3071 ParticleEdit *edit = 0;
3077 if(!PE_can_edit(psys)) return;
3080 ds= BLI_dynstr_new();
3082 BLI_dynstr_append(ds, "Particlemode Undo History %t");
3084 for(undo= edit->undo.first; undo; undo= undo->next) {
3085 BLI_dynstr_append(ds, "|");
3086 BLI_dynstr_append(ds, undo->name);
3089 menu= BLI_dynstr_get_cstring(ds);
3090 BLI_dynstr_free(ds);
3092 event= pupmenu_col(menu, 20);
3095 if(event>0) ParticleUndo_number(edit,event);
3098 void PE_get_colors(char sel[4], char nosel[4])
3100 BIF_GetThemeColor3ubv(TH_EDGE_SELECT, sel);
3101 BIF_GetThemeColor3ubv(TH_WIRE, nosel);
3104 int PE_minmax(float *min, float *max)
3107 ParticleSystem *psys = PE_get_current(ob);
3108 ParticleSystemModifierData *psmd;
3110 ParticleEditKey *key;
3111 float co[3], mat[4][4];
3112 int i, k, totpart, ok = 0;
3114 if(!PE_can_edit(psys)) return ok;
3116 psmd= psys_get_modifier(ob, psys);
3117 totpart= psys->totpart;
3119 LOOP_PARTICLES(i,pa){
3120 if(pa->flag&PARS_HIDE) continue;
3122 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, pa, mat);
3125 if(key->flag&PEK_SELECT) {
3126 VECCOPY(co, key->co);
3127 Mat4MulVecfl(mat, co);
3128 DO_MINMAX(co, min, max);
3135 minmax_object(ob, min, max);