4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2007 by Janne Karhu.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
35 #include "MEM_guardedalloc.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_view3d_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_space_types.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_dynstr.h"
47 #include "BLI_kdtree.h"
49 #include "BLI_utildefines.h"
51 #include "BKE_DerivedMesh.h"
52 #include "BKE_depsgraph.h"
54 #include "BKE_context.h"
55 #include "BKE_global.h"
56 #include "BKE_object.h"
58 #include "BKE_modifier.h"
59 #include "BKE_particle.h"
60 #include "BKE_report.h"
61 #include "BKE_scene.h"
63 #include "BKE_pointcache.h"
66 #include "BIF_glutil.h"
69 #include "ED_particle.h"
70 #include "ED_view3d.h"
72 #include "UI_resources.h"
77 #include "RNA_access.h"
78 #include "RNA_define.h"
80 #include "physics_intern.h"
82 static void PE_create_particle_edit(Scene *scene, Object *ob, PointCache *cache, ParticleSystem *psys);
83 static void PTCacheUndo_clear(PTCacheEdit *edit);
84 static void recalc_emitter_field(Object *ob, ParticleSystem *psys);
86 #define KEY_K PTCacheEditKey *key; int k
87 #define POINT_P PTCacheEditPoint *point; int p
88 #define LOOP_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++)
89 #define LOOP_VISIBLE_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++) if(!(point->flag & PEP_HIDE))
90 #define LOOP_SELECTED_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++) if(point_is_selected(point))
91 #define LOOP_UNSELECTED_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++) if(!point_is_selected(point))
92 #define LOOP_EDITED_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++) if(point->flag & PEP_EDIT_RECALC)
93 #define LOOP_TAGGED_POINTS for(p=0, point=edit->points; p<edit->totpoint; p++, point++) if(point->flag & PEP_TAG)
94 #define LOOP_KEYS for(k=0, key=point->keys; k<point->totkey; k++, key++)
95 #define LOOP_VISIBLE_KEYS for(k=0, key=point->keys; k<point->totkey; k++, key++) if(!(key->flag & PEK_HIDE))
96 #define LOOP_SELECTED_KEYS for(k=0, key=point->keys; k<point->totkey; k++, key++) if((key->flag & PEK_SELECT) && !(key->flag & PEK_HIDE))
97 #define LOOP_TAGGED_KEYS for(k=0, key=point->keys; k<point->totkey; k++, key++) if(key->flag & PEK_TAG)
99 #define KEY_WCO (key->flag & PEK_USE_WCO ? key->world_co : key->co)
101 /**************************** utilities *******************************/
103 int PE_poll(bContext *C)
105 Scene *scene= CTX_data_scene(C);
106 Object *ob= CTX_data_active_object(C);
108 if(!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT))
111 return (PE_get_current(scene, ob) != NULL);
114 int PE_hair_poll(bContext *C)
116 Scene *scene= CTX_data_scene(C);
117 Object *ob= CTX_data_active_object(C);
120 if(!scene || !ob || !(ob->mode & OB_MODE_PARTICLE_EDIT))
123 edit= PE_get_current(scene, ob);
125 return (edit && edit->psys);
128 int PE_poll_view3d(bContext *C)
130 return PE_poll(C) && CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
131 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
134 void PE_free_ptcache_edit(PTCacheEdit *edit)
140 PTCacheUndo_clear(edit);
145 MEM_freeN(point->keys);
148 MEM_freeN(edit->points);
151 if(edit->mirror_cache)
152 MEM_freeN(edit->mirror_cache);
154 if(edit->emitter_cosnos) {
155 MEM_freeN(edit->emitter_cosnos);
156 edit->emitter_cosnos= 0;
159 if(edit->emitter_field) {
160 BLI_kdtree_free(edit->emitter_field);
161 edit->emitter_field= 0;
164 psys_free_path_cache(edit->psys, edit);
169 /************************************************/
170 /* Edit Mode Helpers */
171 /************************************************/
173 int PE_start_edit(PTCacheEdit *edit)
178 edit->psys->flag |= PSYS_EDITED;
185 ParticleEditSettings *PE_settings(Scene *scene)
187 return scene->toolsettings ? &scene->toolsettings->particle : NULL;
190 /* always gets atleast the first particlesystem even if PSYS_CURRENT flag is not set
192 * note: this function runs on poll, therefor it can runs many times a second
194 static PTCacheEdit *pe_get_current(Scene *scene, Object *ob, int create)
196 ParticleEditSettings *pset= PE_settings(scene);
197 PTCacheEdit *edit = NULL;
201 if(pset==NULL || ob==NULL)
207 BKE_ptcache_ids_from_object(&pidlist, ob, NULL, 0);
209 /* in the case of only one editable thing, set pset->edittype accordingly */
210 if(pidlist.first && pidlist.first == pidlist.last) {
213 case PTCACHE_TYPE_PARTICLES:
214 pset->edittype = PE_TYPE_PARTICLES;
216 case PTCACHE_TYPE_SOFTBODY:
217 pset->edittype = PE_TYPE_SOFTBODY;
219 case PTCACHE_TYPE_CLOTH:
220 pset->edittype = PE_TYPE_CLOTH;
225 for(pid=pidlist.first; pid; pid=pid->next) {
226 if(pset->edittype == PE_TYPE_PARTICLES && pid->type == PTCACHE_TYPE_PARTICLES) {
227 ParticleSystem *psys = pid->calldata;
229 if(psys->flag & PSYS_CURRENT) {
230 if(psys->part && psys->part->type == PART_HAIR) {
231 if(psys->flag & PSYS_HAIR_DYNAMICS && psys->pointcache->flag & PTCACHE_BAKED) {
232 if(create && !psys->pointcache->edit)
233 PE_create_particle_edit(scene, ob, pid->cache, NULL);
234 edit = pid->cache->edit;
237 if(create && !psys->edit && psys->flag & PSYS_HAIR_DONE)
238 PE_create_particle_edit(scene, ob, NULL, psys);
243 if(create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit)
244 PE_create_particle_edit(scene, ob, pid->cache, psys);
245 edit = pid->cache->edit;
251 else if(pset->edittype == PE_TYPE_SOFTBODY && pid->type == PTCACHE_TYPE_SOFTBODY) {
252 if(create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit)
253 PE_create_particle_edit(scene, ob, pid->cache, NULL);
254 edit = pid->cache->edit;
257 else if(pset->edittype == PE_TYPE_CLOTH && pid->type == PTCACHE_TYPE_CLOTH) {
258 if(create && pid->cache->flag & PTCACHE_BAKED && !pid->cache->edit)
259 PE_create_particle_edit(scene, ob, pid->cache, NULL);
260 edit = pid->cache->edit;
268 BLI_freelistN(&pidlist);
273 PTCacheEdit *PE_get_current(Scene *scene, Object *ob)
275 return pe_get_current(scene, ob, 0);
278 PTCacheEdit *PE_create_current(Scene *scene, Object *ob)
280 return pe_get_current(scene, ob, 1);
283 void PE_current_changed(Scene *scene, Object *ob)
285 if(ob->mode == OB_MODE_PARTICLE_EDIT)
286 PE_create_current(scene, ob);
289 void PE_hide_keys_time(Scene *scene, PTCacheEdit *edit, float cfra)
291 ParticleEditSettings *pset=PE_settings(scene);
295 if(pset->flag & PE_FADE_TIME && pset->selectmode==SCE_SELECT_POINT) {
298 if(fabs(cfra-*key->time) < pset->fade_frames)
299 key->flag &= ~PEK_HIDE;
301 key->flag |= PEK_HIDE;
302 //key->flag &= ~PEK_SELECT;
310 key->flag &= ~PEK_HIDE;
316 static int pe_x_mirror(Object *ob)
318 if(ob->type == OB_MESH)
319 return (((Mesh*)ob->data)->editflag & ME_EDIT_MIRROR_X);
324 /****************** common struct passed to callbacks ******************/
326 typedef struct PEData {
356 static void PE_set_data(bContext *C, PEData *data)
358 memset(data, 0, sizeof(*data));
360 data->scene= CTX_data_scene(C);
361 data->ob= CTX_data_active_object(C);
362 data->edit= PE_get_current(data->scene, data->ob);
365 static void PE_set_view3d_data(bContext *C, PEData *data)
367 PE_set_data(C, data);
369 view3d_set_viewcontext(C, &data->vc);
370 /* note, the object argument means the modelview matrix does not account for the objects matrix, use viewmat rather then (obmat * viewmat) */
371 view3d_get_transformation(data->vc.ar, data->vc.rv3d, NULL, &data->mats);
373 if((data->vc.v3d->drawtype>OB_WIRE) && (data->vc.v3d->flag & V3D_ZBUF_SELECT)) {
374 if(data->vc.v3d->flag & V3D_INVALID_BACKBUF) {
375 /* needed or else the draw matrix can be incorrect */
376 view3d_operator_needs_opengl(C);
378 view3d_validate_backbuf(&data->vc);
379 /* we may need to force an update here by setting the rv3d as dirty
380 * for now it seems ok, but take care!:
381 * rv3d->depths->dirty = 1; */
382 view3d_update_depths(data->vc.ar);
387 /*************************** selection utilities *******************************/
389 static int key_test_depth(PEData *data, float co[3])
391 View3D *v3d= data->vc.v3d;
397 if((v3d->drawtype<=OB_WIRE) || (v3d->flag & V3D_ZBUF_SELECT)==0)
400 project_short(data->vc.ar, co, wco);
402 if(wco[0] == IS_CLIPPED)
405 gluProject(co[0],co[1],co[2], data->mats.modelview, data->mats.projection,
406 (GLint *)data->mats.viewport, &ux, &uy, &uz);
411 #if 0 /* works well but too slow on some systems [#23118] */
412 x+= (short)data->vc.ar->winrct.xmin;
413 y+= (short)data->vc.ar->winrct.ymin;
415 /* PE_set_view3d_data calls this. no need to call here */
416 /* view3d_validate_backbuf(&data->vc); */
417 glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
418 #else /* faster to use depths, these are calculated in PE_set_view3d_data */
420 ViewDepths *vd = data->vc.rv3d->depths;
421 assert(vd && vd->depths);
422 /* we know its not clipped */
423 depth= vd->depths[y * vd->w + x];
427 if((float)uz - 0.00001 > depth)
433 static int key_inside_circle(PEData *data, float rad, float co[3], float *distance)
438 project_short(data->vc.ar, co, sco);
440 if(sco[0] == IS_CLIPPED)
443 dx= data->mval[0] - sco[0];
444 dy= data->mval[1] - sco[1];
445 dist= sqrt(dx*dx + dy*dy);
450 if(key_test_depth(data, co)) {
460 static int key_inside_rect(PEData *data, float co[3])
464 project_short(data->vc.ar, co,sco);
466 if(sco[0] == IS_CLIPPED)
469 if(sco[0] > data->rect->xmin && sco[0] < data->rect->xmax &&
470 sco[1] > data->rect->ymin && sco[1] < data->rect->ymax)
471 return key_test_depth(data, co);
476 static int key_inside_test(PEData *data, float co[3])
479 return key_inside_circle(data, data->rad, co, NULL);
481 return key_inside_rect(data, co);
484 static int point_is_selected(PTCacheEditPoint *point)
489 if(point->flag & PEP_HIDE)
501 /*************************** iterators *******************************/
503 typedef void (*ForPointFunc)(PEData *data, int point_index);
504 typedef void (*ForKeyFunc)(PEData *data, int point_index, int key_index);
505 typedef void (*ForKeyMatFunc)(PEData *data, float mat[][4], float imat[][4], int point_index, int key_index, PTCacheEditKey *key);
507 static void for_mouse_hit_keys(PEData *data, ForKeyFunc func, int nearest)
509 ParticleEditSettings *pset= PE_settings(data->scene);
510 PTCacheEdit *edit= data->edit;
512 int nearest_point, nearest_key;
513 float dist= data->rad;
515 /* in path select mode we have no keys */
516 if(pset->selectmode==SCE_SELECT_PATH)
522 LOOP_VISIBLE_POINTS {
523 if(pset->selectmode == SCE_SELECT_END) {
524 /* only do end keys */
525 key= point->keys + point->totkey-1;
528 if(key_inside_circle(data, dist, KEY_WCO, &dist)) {
530 nearest_key= point->totkey-1;
533 else if(key_inside_test(data, KEY_WCO))
534 func(data, p, point->totkey-1);
540 if(key_inside_circle(data, dist, KEY_WCO, &dist)) {
545 else if(key_inside_test(data, KEY_WCO))
551 /* do nearest only */
552 if(nearest && nearest_point > -1)
553 func(data, nearest_point, nearest_key);
556 static void foreach_mouse_hit_point(PEData *data, ForPointFunc func, int selected)
558 ParticleEditSettings *pset= PE_settings(data->scene);
559 PTCacheEdit *edit= data->edit;
562 /* all is selected in path mode */
563 if(pset->selectmode==SCE_SELECT_PATH)
566 LOOP_VISIBLE_POINTS {
567 if(pset->selectmode==SCE_SELECT_END) {
568 /* only do end keys */
569 key= point->keys + point->totkey - 1;
571 if(selected==0 || key->flag & PEK_SELECT)
572 if(key_inside_circle(data, data->rad, KEY_WCO, &data->dist))
578 if(selected==0 || key->flag & PEK_SELECT) {
579 if(key_inside_circle(data, data->rad, KEY_WCO, &data->dist)) {
589 static void foreach_mouse_hit_key(PEData *data, ForKeyMatFunc func, int selected)
591 PTCacheEdit *edit = data->edit;
592 ParticleSystem *psys = edit->psys;
593 ParticleSystemModifierData *psmd = NULL;
594 ParticleEditSettings *pset= PE_settings(data->scene);
596 float mat[4][4]= MAT4_UNITY, imat[4][4]= MAT4_UNITY;
599 psmd= psys_get_modifier(data->ob, edit->psys);
601 /* all is selected in path mode */
602 if(pset->selectmode==SCE_SELECT_PATH)
605 LOOP_VISIBLE_POINTS {
606 if(pset->selectmode==SCE_SELECT_END) {
607 /* only do end keys */
608 key= point->keys + point->totkey-1;
610 if(selected==0 || key->flag & PEK_SELECT) {
611 if(key_inside_circle(data, data->rad, KEY_WCO, &data->dist)) {
612 if(edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
613 psys_mat_hair_to_global(data->ob, psmd->dm, psys->part->from, psys->particles + p, mat);
614 invert_m4_m4(imat,mat);
617 func(data, mat, imat, p, point->totkey-1, key);
624 if(selected==0 || key->flag & PEK_SELECT) {
625 if(key_inside_circle(data, data->rad, KEY_WCO, &data->dist)) {
626 if(edit->psys && !(edit->psys->flag & PSYS_GLOBAL_HAIR)) {
627 psys_mat_hair_to_global(data->ob, psmd->dm, psys->part->from, psys->particles + p, mat);
628 invert_m4_m4(imat,mat);
631 func(data, mat, imat, p, k, key);
639 static void foreach_selected_point(PEData *data, ForPointFunc func)
641 PTCacheEdit *edit = data->edit;
644 LOOP_SELECTED_POINTS {
649 static void foreach_selected_key(PEData *data, ForKeyFunc func)
651 PTCacheEdit *edit = data->edit;
654 LOOP_VISIBLE_POINTS {
661 static void foreach_point(PEData *data, ForPointFunc func)
663 PTCacheEdit *edit = data->edit;
671 static int count_selected_keys(Scene *scene, PTCacheEdit *edit)
673 ParticleEditSettings *pset= PE_settings(scene);
677 LOOP_VISIBLE_POINTS {
678 if(pset->selectmode==SCE_SELECT_POINT) {
683 else if(pset->selectmode==SCE_SELECT_END) {
684 key = point->keys + point->totkey - 1;
685 if(key->flag & PEK_SELECT)
693 /************************************************/
694 /* Particle Edit Mirroring */
695 /************************************************/
697 static void PE_update_mirror_cache(Object *ob, ParticleSystem *psys)
700 ParticleSystemModifierData *psmd;
702 KDTreeNearest nearest;
705 float mat[4][4], co[3];
709 psmd= psys_get_modifier(ob, psys);
710 totpart= psys->totpart;
715 tree= BLI_kdtree_new(totpart);
717 /* insert particles into kd tree */
720 psys_mat_hair_to_orco(ob, psmd->dm, psys->part->from, pa, mat);
721 VECCOPY(co, key->co);
723 BLI_kdtree_insert(tree, p, co, NULL);
726 BLI_kdtree_balance(tree);
728 /* lookup particles and set in mirror cache */
729 if(!edit->mirror_cache)
730 edit->mirror_cache= MEM_callocN(sizeof(int)*totpart, "PE mirror cache");
734 psys_mat_hair_to_orco(ob, psmd->dm, psys->part->from, pa, mat);
735 VECCOPY(co, key->co);
739 index= BLI_kdtree_find_nearest(tree, co, NULL, &nearest);
741 /* this needs a custom threshold still, duplicated for editmode mirror */
742 if(index != -1 && index != p && (nearest.dist <= 0.0002f))
743 edit->mirror_cache[p]= index;
745 edit->mirror_cache[p]= -1;
748 /* make sure mirrors are in two directions */
750 if(edit->mirror_cache[p]) {
751 index= edit->mirror_cache[p];
752 if(edit->mirror_cache[index] != p)
753 edit->mirror_cache[p]= -1;
757 BLI_kdtree_free(tree);
760 static void PE_mirror_particle(Object *ob, DerivedMesh *dm, ParticleSystem *psys, ParticleData *pa, ParticleData *mpa)
762 HairKey *hkey, *mhkey;
763 PTCacheEditPoint *point, *mpoint;
764 PTCacheEditKey *key, *mkey;
766 float mat[4][4], mmat[4][4], immat[4][4];
770 i= pa - psys->particles;
772 /* find mirrored particle if needed */
774 if(!edit->mirror_cache)
775 PE_update_mirror_cache(ob, psys);
777 mi= edit->mirror_cache[i];
780 mpa= psys->particles + mi;
783 mi= mpa - psys->particles;
785 point = edit->points + i;
786 mpoint = edit->points + mi;
788 /* make sure they have the same amount of keys */
789 if(pa->totkey != mpa->totkey) {
790 if(mpa->hair) MEM_freeN(mpa->hair);
791 if(mpoint->keys) MEM_freeN(mpoint->keys);
793 mpa->hair= MEM_dupallocN(pa->hair);
794 mpa->totkey= pa->totkey;
795 mpoint->keys= MEM_dupallocN(point->keys);
796 mpoint->totkey= point->totkey;
800 for(k=0; k<mpa->totkey; k++, mkey++, mhkey++) {
802 mkey->time= &mhkey->time;
803 mkey->flag &= ~PEK_SELECT;
807 /* mirror positions and tags */
808 psys_mat_hair_to_orco(ob, dm, psys->part->from, pa, mat);
809 psys_mat_hair_to_orco(ob, dm, psys->part->from, mpa, mmat);
810 invert_m4_m4(immat, mmat);
816 for(k=0; k<pa->totkey; k++, hkey++, mhkey++, key++, mkey++) {
817 VECCOPY(mhkey->co, hkey->co);
818 mul_m4_v3(mat, mhkey->co);
819 mhkey->co[0]= -mhkey->co[0];
820 mul_m4_v3(immat, mhkey->co);
822 if(key->flag & PEK_TAG)
823 mkey->flag |= PEK_TAG;
826 if(point->flag & PEP_TAG)
827 mpoint->flag |= PEP_TAG;
828 if(point->flag & PEP_EDIT_RECALC)
829 mpoint->flag |= PEP_EDIT_RECALC;
832 static void PE_apply_mirror(Object *ob, ParticleSystem *psys)
835 ParticleSystemModifierData *psmd;
842 psmd= psys_get_modifier(ob, psys);
847 if(!edit->mirror_cache)
848 PE_update_mirror_cache(ob, psys);
850 /* we delay settings the PARS_EDIT_RECALC for mirrored particles
851 * to avoid doing mirror twice */
853 if(point->flag & PEP_EDIT_RECALC) {
854 PE_mirror_particle(ob, psmd->dm, psys, psys->particles + p, NULL);
856 if(edit->mirror_cache[p] != -1)
857 edit->points[edit->mirror_cache[p]].flag &= ~PEP_EDIT_RECALC;
862 if(point->flag & PEP_EDIT_RECALC)
863 if(edit->mirror_cache[p] != -1)
864 edit->points[edit->mirror_cache[p]].flag |= PEP_EDIT_RECALC;
868 /************************************************/
869 /* Edit Calculation */
870 /************************************************/
871 /* tries to stop edited particles from going through the emitter's surface */
872 static void pe_deflect_emitter(Scene *scene, Object *ob, PTCacheEdit *edit)
874 ParticleEditSettings *pset= PE_settings(scene);
875 ParticleSystem *psys;
876 ParticleSystemModifierData *psmd;
879 float *vec, *nor, dvec[3], dot, dist_1st=0.0f;
880 float hairimat[4][4], hairmat[4][4];
882 if(edit==NULL || edit->psys==NULL || (pset->flag & PE_DEFLECT_EMITTER)==0 || (edit->psys->flag & PSYS_GLOBAL_HAIR))
886 psmd = psys_get_modifier(ob,psys);
892 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, psys->particles + p, hairmat);
895 mul_m4_v3(hairmat, key->co);
900 dist_1st = len_v3v3((key+1)->co, key->co);
901 dist_1st *= 0.75f * pset->emitterdist;
904 index= BLI_kdtree_find_nearest(edit->emitter_field,key->co,NULL,NULL);
906 vec=edit->emitter_cosnos +index*6;
909 sub_v3_v3v3(dvec, key->co, vec);
911 dot=dot_v3v3(dvec,nor);
917 mul_v3_fl(dvec,dist_1st-dot);
918 add_v3_v3(key->co, dvec);
923 mul_v3_fl(dvec,dist_1st-dot);
924 add_v3_v3(key->co, dvec);
931 invert_m4_m4(hairimat,hairmat);
934 mul_m4_v3(hairimat, key->co);
938 /* force set distances between neighbouring keys */
939 void PE_apply_lengths(Scene *scene, PTCacheEdit *edit)
942 ParticleEditSettings *pset=PE_settings(scene);
946 if(edit==0 || (pset->flag & PE_KEEP_LENGTHS)==0)
949 if(edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR)
955 sub_v3_v3v3(dv1, key->co, (key - 1)->co);
957 mul_v3_fl(dv1, (key - 1)->length);
958 add_v3_v3v3(key->co, (key - 1)->co, dv1);
963 /* try to find a nice solution to keep distances between neighbouring keys */
964 static void pe_iterate_lengths(Scene *scene, PTCacheEdit *edit)
966 ParticleEditSettings *pset=PE_settings(scene);
971 float dv0[3]= {0.0f, 0.0f, 0.0f};
972 float dv1[3]= {0.0f, 0.0f, 0.0f};
973 float dv2[3]= {0.0f, 0.0f, 0.0f};
975 if(edit==0 || (pset->flag & PE_KEEP_LENGTHS)==0)
978 if(edit->psys && edit->psys->flag & PSYS_GLOBAL_HAIR)
982 for(j=1; j<point->totkey; j++) {
983 float mul= 1.0f / (float)point->totkey;
985 if(pset->flag & PE_LOCK_FIRST) {
986 key= point->keys + 1;
988 dv1[0]= dv1[1]= dv1[2]= 0.0;
993 dv0[0]= dv0[1]= dv0[2]= 0.0;
996 for(; k<point->totkey; k++, key++) {
998 sub_v3_v3v3(dv0, (key - 1)->co, key->co);
999 tlen= normalize_v3(dv0);
1000 mul_v3_fl(dv0, (mul * (tlen - (key - 1)->length)));
1003 if(k < point->totkey - 1) {
1004 sub_v3_v3v3(dv2, (key + 1)->co, key->co);
1005 tlen= normalize_v3(dv2);
1006 mul_v3_fl(dv2, mul * (tlen - key->length));
1010 add_v3_v3((key-1)->co, dv1);
1013 VECADD(dv1,dv0,dv2);
1018 /* set current distances to be kept between neighbouting keys */
1019 static void recalc_lengths(PTCacheEdit *edit)
1026 LOOP_EDITED_POINTS {
1028 for(k=0; k<point->totkey-1; k++, key++) {
1029 key->length= len_v3v3(key->co, (key + 1)->co);
1034 /* calculate a tree for finding nearest emitter's vertice */
1035 static void recalc_emitter_field(Object *ob, ParticleSystem *psys)
1037 DerivedMesh *dm=psys_get_modifier(ob,psys)->dm;
1038 PTCacheEdit *edit= psys->edit;
1042 int i, totface, totvert;
1047 if(edit->emitter_cosnos)
1048 MEM_freeN(edit->emitter_cosnos);
1050 BLI_kdtree_free(edit->emitter_field);
1052 totface=dm->getNumFaces(dm);
1053 totvert=dm->getNumVerts(dm);
1055 edit->emitter_cosnos=MEM_callocN(totface*6*sizeof(float),"emitter cosnos");
1057 edit->emitter_field= BLI_kdtree_new(totface);
1059 vec=edit->emitter_cosnos;
1062 mvert=dm->getVertDataArray(dm,CD_MVERT);
1063 for(i=0; i<totface; i++, vec+=6, nor+=6) {
1064 mface=dm->getFaceData(dm,i,CD_MFACE);
1066 mvert=dm->getVertData(dm,mface->v1,CD_MVERT);
1067 VECCOPY(vec,mvert->co);
1068 VECCOPY(nor,mvert->no);
1070 mvert=dm->getVertData(dm,mface->v2,CD_MVERT);
1071 VECADD(vec,vec,mvert->co);
1072 VECADD(nor,nor,mvert->no);
1074 mvert=dm->getVertData(dm,mface->v3,CD_MVERT);
1075 VECADD(vec,vec,mvert->co);
1076 VECADD(nor,nor,mvert->no);
1079 mvert=dm->getVertData(dm,mface->v4,CD_MVERT);
1080 VECADD(vec,vec,mvert->co);
1081 VECADD(nor,nor,mvert->no);
1083 mul_v3_fl(vec,0.25);
1086 mul_v3_fl(vec,0.3333f);
1090 BLI_kdtree_insert(edit->emitter_field, i, vec, NULL);
1093 BLI_kdtree_balance(edit->emitter_field);
1096 static void PE_update_selection(Scene *scene, Object *ob, int useflag)
1098 PTCacheEdit *edit= PE_get_current(scene, ob);
1102 /* flag all particles to be updated if not using flag */
1105 point->flag |= PEP_EDIT_RECALC;
1107 /* flush edit key flag to hair key flag to preserve selection
1109 if(edit->psys) LOOP_POINTS {
1110 hkey = edit->psys->particles[p].hair;
1112 hkey->editflag= key->flag;
1117 psys_cache_edit_paths(scene, ob, edit, CFRA);
1120 /* disable update flag */
1122 point->flag &= ~PEP_EDIT_RECALC;
1125 static void update_world_cos(Object *ob, PTCacheEdit *edit)
1127 ParticleSystem *psys = edit->psys;
1128 ParticleSystemModifierData *psmd= psys_get_modifier(ob, psys);
1130 float hairmat[4][4];
1132 if(psys==0 || psys->edit==0 || psmd->dm==NULL)
1136 if(!(psys->flag & PSYS_GLOBAL_HAIR))
1137 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles+p, hairmat);
1140 VECCOPY(key->world_co,key->co);
1141 if(!(psys->flag & PSYS_GLOBAL_HAIR))
1142 mul_m4_v3(hairmat, key->world_co);
1146 static void update_velocities(PTCacheEdit *edit)
1148 /*TODO: get frs_sec properly */
1149 float vec1[3], vec2[3], frs_sec, dfra;
1152 /* hair doesn't use velocities */
1153 if(edit->psys || !edit->points || !edit->points->keys->vel)
1156 frs_sec = edit->pid.flag & PTCACHE_VEL_PER_SEC ? 25.0f : 1.0f;
1158 LOOP_EDITED_POINTS {
1161 dfra = *(key+1)->time - *key->time;
1166 VECSUB(key->vel, (key+1)->co, key->co);
1168 if(point->totkey>2) {
1169 VECSUB(vec1, (key+1)->co, (key+2)->co);
1170 project_v3_v3v3(vec2, vec1, key->vel);
1171 VECSUB(vec2, vec1, vec2);
1172 VECADDFAC(key->vel, key->vel, vec2, 0.5f);
1175 else if(k==point->totkey-1) {
1176 dfra = *key->time - *(key-1)->time;
1181 VECSUB(key->vel, key->co, (key-1)->co);
1183 if(point->totkey>2) {
1184 VECSUB(vec1, (key-2)->co, (key-1)->co);
1185 project_v3_v3v3(vec2, vec1, key->vel);
1186 VECSUB(vec2, vec1, vec2);
1187 VECADDFAC(key->vel, key->vel, vec2, 0.5f);
1191 dfra = *(key+1)->time - *(key-1)->time;
1196 VECSUB(key->vel, (key+1)->co, (key-1)->co);
1198 mul_v3_fl(key->vel, frs_sec/dfra);
1203 void PE_update_object(Scene *scene, Object *ob, int useflag)
1205 /* use this to do partial particle updates, not usable when adding or
1206 removing, then a full redo is necessary and calling this may crash */
1207 ParticleEditSettings *pset= PE_settings(scene);
1208 PTCacheEdit *edit = PE_get_current(scene, ob);
1214 /* flag all particles to be updated if not using flag */
1217 point->flag |= PEP_EDIT_RECALC;
1220 /* do post process on particle edit keys */
1221 pe_iterate_lengths(scene, edit);
1222 pe_deflect_emitter(scene, ob, edit);
1223 PE_apply_lengths(scene, edit);
1225 PE_apply_mirror(ob,edit->psys);
1227 update_world_cos(ob, edit);
1228 if(pset->flag & PE_AUTO_VELOCITY)
1229 update_velocities(edit);
1230 PE_hide_keys_time(scene, edit, CFRA);
1232 /* regenerate path caches */
1233 psys_cache_edit_paths(scene, ob, edit, CFRA);
1235 /* disable update flag */
1237 point->flag &= ~PEP_EDIT_RECALC;
1241 edit->psys->flag &= ~PSYS_HAIR_UPDATED;
1244 /************************************************/
1245 /* Edit Selections */
1246 /************************************************/
1248 /*-----selection callbacks-----*/
1250 static void select_key(PEData *data, int point_index, int key_index)
1252 PTCacheEdit *edit = data->edit;
1253 PTCacheEditPoint *point = edit->points + point_index;
1254 PTCacheEditKey *key = point->keys + key_index;
1257 key->flag |= PEK_SELECT;
1259 key->flag &= ~PEK_SELECT;
1261 point->flag |= PEP_EDIT_RECALC;
1264 static void select_keys(PEData *data, int point_index, int UNUSED(key_index))
1266 PTCacheEdit *edit = data->edit;
1267 PTCacheEditPoint *point = edit->points + point_index;
1272 key->flag |= PEK_SELECT;
1274 key->flag &= ~PEK_SELECT;
1277 point->flag |= PEP_EDIT_RECALC;
1280 static void toggle_key_select(PEData *data, int point_index, int key_index)
1282 PTCacheEdit *edit = data->edit;
1283 PTCacheEditPoint *point = edit->points + point_index;
1284 PTCacheEditKey *key = point->keys + key_index;
1286 key->flag ^= PEK_SELECT;
1287 point->flag |= PEP_EDIT_RECALC;
1290 /************************ de select all operator ************************/
1292 static int select_all_exec(bContext *C, wmOperator *op)
1294 Scene *scene= CTX_data_scene(C);
1295 Object *ob= CTX_data_active_object(C);
1296 PTCacheEdit *edit= PE_get_current(scene, ob);
1298 int action = RNA_enum_get(op->ptr, "action");
1300 if (action == SEL_TOGGLE) {
1301 action = SEL_SELECT;
1302 LOOP_VISIBLE_POINTS {
1303 LOOP_SELECTED_KEYS {
1304 action = SEL_DESELECT;
1308 if (action == SEL_DESELECT)
1313 LOOP_VISIBLE_POINTS {
1317 if ((key->flag & PEK_SELECT) == 0) {
1318 key->flag |= PEK_SELECT;
1319 point->flag |= PEP_EDIT_RECALC;
1323 if (key->flag & PEK_SELECT) {
1324 key->flag &= ~PEK_SELECT;
1325 point->flag |= PEP_EDIT_RECALC;
1329 if ((key->flag & PEK_SELECT) == 0) {
1330 key->flag |= PEK_SELECT;
1331 point->flag |= PEP_EDIT_RECALC;
1333 key->flag &= ~PEK_SELECT;
1334 point->flag |= PEP_EDIT_RECALC;
1341 PE_update_selection(scene, ob, 1);
1342 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1344 return OPERATOR_FINISHED;
1347 void PARTICLE_OT_select_all(wmOperatorType *ot)
1350 ot->name= "Selection of all particles";
1351 ot->idname= "PARTICLE_OT_select_all";
1354 ot->exec= select_all_exec;
1358 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1360 WM_operator_properties_select_all(ot);
1363 /************************ pick select operator ************************/
1365 int PE_mouse_particles(bContext *C, short *mval, int extend)
1368 Scene *scene= CTX_data_scene(C);
1369 Object *ob= CTX_data_active_object(C);
1370 PTCacheEdit *edit= PE_get_current(scene, ob);
1373 if(!PE_start_edit(edit))
1374 return OPERATOR_CANCELLED;
1377 LOOP_VISIBLE_POINTS {
1378 LOOP_SELECTED_KEYS {
1379 key->flag &= ~PEK_SELECT;
1380 point->flag |= PEP_EDIT_RECALC;
1385 PE_set_view3d_data(C, &data);
1389 for_mouse_hit_keys(&data, toggle_key_select, 1); /* nearest only */
1391 PE_update_selection(scene, ob, 1);
1392 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1394 return OPERATOR_FINISHED;
1397 /************************ select first operator ************************/
1399 static void select_root(PEData *data, int point_index)
1401 if (data->edit->points[point_index].flag & PEP_HIDE)
1404 data->edit->points[point_index].keys->flag |= PEK_SELECT;
1405 data->edit->points[point_index].flag |= PEP_EDIT_RECALC; /* redraw selection only */
1408 static int select_roots_exec(bContext *C, wmOperator *UNUSED(op))
1412 PE_set_data(C, &data);
1413 foreach_point(&data, select_root);
1415 PE_update_selection(data.scene, data.ob, 1);
1416 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1418 return OPERATOR_FINISHED;
1421 void PARTICLE_OT_select_roots(wmOperatorType *ot)
1424 ot->name= "Select Roots";
1425 ot->idname= "PARTICLE_OT_select_roots";
1428 ot->exec= select_roots_exec;
1432 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1435 /************************ select last operator ************************/
1437 static void select_tip(PEData *data, int point_index)
1439 PTCacheEditPoint *point = data->edit->points + point_index;
1441 if (point->flag & PEP_HIDE)
1444 point->keys[point->totkey - 1].flag |= PEK_SELECT;
1445 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
1448 static int select_tips_exec(bContext *C, wmOperator *UNUSED(op))
1452 PE_set_data(C, &data);
1453 foreach_point(&data, select_tip);
1455 PE_update_selection(data.scene, data.ob, 1);
1456 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1458 return OPERATOR_FINISHED;
1461 void PARTICLE_OT_select_tips(wmOperatorType *ot)
1464 ot->name= "Select Tips";
1465 ot->idname= "PARTICLE_OT_select_tips";
1468 ot->exec= select_tips_exec;
1472 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1475 /************************ select linked operator ************************/
1477 static int select_linked_exec(bContext *C, wmOperator *op)
1483 RNA_int_get_array(op->ptr, "location", location);
1484 mval[0]= location[0];
1485 mval[1]= location[1];
1487 PE_set_view3d_data(C, &data);
1490 data.select= !RNA_boolean_get(op->ptr, "deselect");
1492 for_mouse_hit_keys(&data, select_keys, 1); /* nearest only */
1493 PE_update_selection(data.scene, data.ob, 1);
1494 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1496 return OPERATOR_FINISHED;
1499 static int select_linked_invoke(bContext *C, wmOperator *op, wmEvent *event)
1501 ARegion *ar= CTX_wm_region(C);
1504 location[0]= event->x - ar->winrct.xmin;
1505 location[1]= event->y - ar->winrct.ymin;
1506 RNA_int_set_array(op->ptr, "location", location);
1508 return select_linked_exec(C, op);
1511 void PARTICLE_OT_select_linked(wmOperatorType *ot)
1514 ot->name= "Select Linked";
1515 ot->idname= "PARTICLE_OT_select_linked";
1518 ot->exec= select_linked_exec;
1519 ot->invoke= select_linked_invoke;
1520 ot->poll= PE_poll_view3d;
1523 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1526 RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect linked keys rather than selecting them.");
1527 RNA_def_int_vector(ot->srna, "location", 2, NULL, 0, INT_MAX, "Location", "", 0, 16384);
1530 /************************ border select operator ************************/
1531 void PE_deselect_all_visible(PTCacheEdit *edit)
1535 LOOP_VISIBLE_POINTS {
1536 LOOP_SELECTED_KEYS {
1537 key->flag &= ~PEK_SELECT;
1538 point->flag |= PEP_EDIT_RECALC;
1543 int PE_border_select(bContext *C, rcti *rect, int select, int extend)
1545 Scene *scene= CTX_data_scene(C);
1546 Object *ob= CTX_data_active_object(C);
1547 PTCacheEdit *edit= PE_get_current(scene, ob);
1550 if(!PE_start_edit(edit))
1551 return OPERATOR_CANCELLED;
1553 if (extend == 0 && select)
1554 PE_deselect_all_visible(edit);
1556 PE_set_view3d_data(C, &data);
1558 data.select= select;
1560 for_mouse_hit_keys(&data, select_key, 0);
1562 PE_update_selection(scene, ob, 1);
1563 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1565 return OPERATOR_FINISHED;
1568 /************************ circle select operator ************************/
1570 int PE_circle_select(bContext *C, int selecting, short *mval, float rad)
1572 Scene *scene= CTX_data_scene(C);
1573 Object *ob= CTX_data_active_object(C);
1574 PTCacheEdit *edit= PE_get_current(scene, ob);
1577 if(!PE_start_edit(edit))
1578 return OPERATOR_FINISHED;
1580 PE_set_view3d_data(C, &data);
1583 data.select= selecting;
1585 for_mouse_hit_keys(&data, select_key, 0);
1587 PE_update_selection(scene, ob, 1);
1588 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1590 return OPERATOR_FINISHED;
1593 /************************ lasso select operator ************************/
1595 int PE_lasso_select(bContext *C, short mcords[][2], short moves, short extend, short select)
1597 Scene *scene= CTX_data_scene(C);
1598 Object *ob= CTX_data_active_object(C);
1599 ARegion *ar= CTX_wm_region(C);
1600 ParticleEditSettings *pset= PE_settings(scene);
1601 PTCacheEdit *edit = PE_get_current(scene, ob);
1602 ParticleSystem *psys = edit->psys;
1603 ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
1605 float co[3], mat[4][4]= MAT4_UNITY;
1610 if(!PE_start_edit(edit))
1611 return OPERATOR_CANCELLED;
1613 if (extend == 0 && select)
1614 PE_deselect_all_visible(edit);
1616 /* only for depths */
1617 PE_set_view3d_data(C, &data);
1619 LOOP_VISIBLE_POINTS {
1620 if(edit->psys && !(psys->flag & PSYS_GLOBAL_HAIR))
1621 psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + p, mat);
1623 if(pset->selectmode==SCE_SELECT_POINT) {
1625 VECCOPY(co, key->co);
1627 project_short(ar, co, vertco);
1628 if((vertco[0] != IS_CLIPPED) && lasso_inside(mcords,moves,vertco[0],vertco[1]) && key_test_depth(&data, co)) {
1629 if(select && !(key->flag & PEK_SELECT)) {
1630 key->flag |= PEK_SELECT;
1631 point->flag |= PEP_EDIT_RECALC;
1633 else if(key->flag & PEK_SELECT) {
1634 key->flag &= ~PEK_SELECT;
1635 point->flag |= PEP_EDIT_RECALC;
1640 else if(pset->selectmode==SCE_SELECT_END) {
1641 key= point->keys + point->totkey - 1;
1643 VECCOPY(co, key->co);
1645 project_short(ar, co,vertco);
1646 if((vertco[0] != IS_CLIPPED) && lasso_inside(mcords,moves,vertco[0],vertco[1]) && key_test_depth(&data, co)) {
1647 if(select && !(key->flag & PEK_SELECT)) {
1648 key->flag |= PEK_SELECT;
1649 point->flag |= PEP_EDIT_RECALC;
1651 else if(key->flag & PEK_SELECT) {
1652 key->flag &= ~PEK_SELECT;
1653 point->flag |= PEP_EDIT_RECALC;
1659 PE_update_selection(scene, ob, 1);
1660 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1662 return OPERATOR_FINISHED;
1665 /*************************** hide operator **************************/
1667 static int hide_exec(bContext *C, wmOperator *op)
1669 Object *ob= CTX_data_active_object(C);
1670 Scene *scene= CTX_data_scene(C);
1671 PTCacheEdit *edit= PE_get_current(scene, ob);
1674 if(RNA_enum_get(op->ptr, "unselected")) {
1675 LOOP_UNSELECTED_POINTS {
1676 point->flag |= PEP_HIDE;
1677 point->flag |= PEP_EDIT_RECALC;
1680 key->flag &= ~PEK_SELECT;
1684 LOOP_SELECTED_POINTS {
1685 point->flag |= PEP_HIDE;
1686 point->flag |= PEP_EDIT_RECALC;
1689 key->flag &= ~PEK_SELECT;
1693 PE_update_selection(scene, ob, 1);
1694 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1696 return OPERATOR_FINISHED;
1699 void PARTICLE_OT_hide(wmOperatorType *ot)
1702 ot->name= "Hide Selected";
1703 ot->idname= "PARTICLE_OT_hide";
1706 ot->exec= hide_exec;
1710 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1713 RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected.");
1716 /*************************** reveal operator **************************/
1718 static int reveal_exec(bContext *C, wmOperator *UNUSED(op))
1720 Object *ob= CTX_data_active_object(C);
1721 Scene *scene= CTX_data_scene(C);
1722 PTCacheEdit *edit= PE_get_current(scene, ob);
1726 if(point->flag & PEP_HIDE) {
1727 point->flag &= ~PEP_HIDE;
1728 point->flag |= PEP_EDIT_RECALC;
1731 key->flag |= PEK_SELECT;
1735 PE_update_selection(scene, ob, 1);
1736 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, ob);
1738 return OPERATOR_FINISHED;
1741 void PARTICLE_OT_reveal(wmOperatorType *ot)
1745 ot->idname= "PARTICLE_OT_reveal";
1748 ot->exec= reveal_exec;
1752 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1755 /************************ select less operator ************************/
1757 static void select_less_keys(PEData *data, int point_index)
1759 PTCacheEdit *edit= data->edit;
1760 PTCacheEditPoint *point = edit->points + point_index;
1763 LOOP_SELECTED_KEYS {
1765 if(((key+1)->flag&PEK_SELECT)==0)
1766 key->flag |= PEK_TAG;
1768 else if(k==point->totkey-1) {
1769 if(((key-1)->flag&PEK_SELECT)==0)
1770 key->flag |= PEK_TAG;
1773 if((((key-1)->flag & (key+1)->flag) & PEK_SELECT)==0)
1774 key->flag |= PEK_TAG;
1779 if(key->flag&PEK_TAG) {
1780 key->flag &= ~(PEK_TAG|PEK_SELECT);
1781 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
1786 static int select_less_exec(bContext *C, wmOperator *UNUSED(op))
1790 PE_set_data(C, &data);
1791 foreach_point(&data, select_less_keys);
1793 PE_update_selection(data.scene, data.ob, 1);
1794 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1796 return OPERATOR_FINISHED;
1799 void PARTICLE_OT_select_less(wmOperatorType *ot)
1802 ot->name= "Select Less";
1803 ot->idname= "PARTICLE_OT_select_less";
1806 ot->exec= select_less_exec;
1810 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1813 /************************ select more operator ************************/
1815 static void select_more_keys(PEData *data, int point_index)
1817 PTCacheEdit *edit= data->edit;
1818 PTCacheEditPoint *point = edit->points + point_index;
1822 if(key->flag & PEK_SELECT) continue;
1825 if((key+1)->flag&PEK_SELECT)
1826 key->flag |= PEK_TAG;
1828 else if(k==point->totkey-1) {
1829 if((key-1)->flag&PEK_SELECT)
1830 key->flag |= PEK_TAG;
1833 if(((key-1)->flag | (key+1)->flag) & PEK_SELECT)
1834 key->flag |= PEK_TAG;
1839 if(key->flag&PEK_TAG) {
1840 key->flag &= ~PEK_TAG;
1841 key->flag |= PEK_SELECT;
1842 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
1847 static int select_more_exec(bContext *C, wmOperator *UNUSED(op))
1851 PE_set_data(C, &data);
1852 foreach_point(&data, select_more_keys);
1854 PE_update_selection(data.scene, data.ob, 1);
1855 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1857 return OPERATOR_FINISHED;
1860 void PARTICLE_OT_select_more(wmOperatorType *ot)
1863 ot->name= "Select More";
1864 ot->idname= "PARTICLE_OT_select_more";
1867 ot->exec= select_more_exec;
1871 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1874 static int select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
1880 PE_set_data(C, &data);
1882 edit= PE_get_current(data.scene, data.ob);
1884 LOOP_VISIBLE_POINTS {
1886 key->flag ^= PEK_SELECT;
1887 point->flag |= PEP_EDIT_RECALC; /* redraw selection only */
1891 PE_update_selection(data.scene, data.ob, 1);
1892 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
1894 return OPERATOR_FINISHED;
1897 void PARTICLE_OT_select_inverse(wmOperatorType *ot)
1900 ot->name= "Select Inverse";
1901 ot->idname= "PARTICLE_OT_select_inverse";
1904 ot->exec= select_inverse_exec;
1908 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1911 /************************ rekey operator ************************/
1913 static void rekey_particle(PEData *data, int pa_index)
1915 PTCacheEdit *edit= data->edit;
1916 ParticleSystem *psys= edit->psys;
1917 ParticleSimulationData sim= {0};
1918 ParticleData *pa= psys->particles + pa_index;
1919 PTCacheEditPoint *point = edit->points + pa_index;
1921 HairKey *key, *new_keys, *okey;
1922 PTCacheEditKey *ekey;
1923 float dval, sta, end;
1926 sim.scene= data->scene;
1928 sim.psys= edit->psys;
1930 pa->flag |= PARS_REKEY;
1932 key= new_keys= MEM_callocN(data->totrekey * sizeof(HairKey),"Hair re-key keys");
1935 /* root and tip stay the same */
1936 VECCOPY(key->co, okey->co);
1937 VECCOPY((key + data->totrekey - 1)->co, (okey + pa->totkey - 1)->co);
1939 sta= key->time= okey->time;
1940 end= (key + data->totrekey - 1)->time= (okey + pa->totkey - 1)->time;
1941 dval= (end - sta) / (float)(data->totrekey - 1);
1943 /* interpolate new keys from old ones */
1944 for(k=1,key++; k<data->totrekey-1; k++,key++) {
1945 state.time= (float)k / (float)(data->totrekey-1);
1946 psys_get_particle_on_path(&sim, pa_index, &state, 0);
1947 VECCOPY(key->co, state.co);
1948 key->time= sta + k * dval;
1953 MEM_freeN(pa->hair);
1956 point->totkey=pa->totkey=data->totrekey;
1960 MEM_freeN(point->keys);
1961 ekey= point->keys= MEM_callocN(pa->totkey * sizeof(PTCacheEditKey),"Hair re-key edit keys");
1963 for(k=0, key=pa->hair; k<pa->totkey; k++, key++, ekey++) {
1965 ekey->time= &key->time;
1966 if(!(psys->flag & PSYS_GLOBAL_HAIR))
1967 ekey->flag |= PEK_USE_WCO;
1970 pa->flag &= ~PARS_REKEY;
1971 point->flag |= PEP_EDIT_RECALC;
1974 static int rekey_exec(bContext *C, wmOperator *op)
1978 PE_set_data(C, &data);
1980 data.dval= 1.0f / (float)(data.totrekey-1);
1981 data.totrekey= RNA_int_get(op->ptr, "keys");
1983 foreach_selected_point(&data, rekey_particle);
1985 recalc_lengths(data.edit);
1986 PE_update_object(data.scene, data.ob, 1);
1987 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
1989 return OPERATOR_FINISHED;
1992 void PARTICLE_OT_rekey(wmOperatorType *ot)
1996 ot->idname= "PARTICLE_OT_rekey";
1999 ot->exec= rekey_exec;
2000 ot->invoke= WM_operator_props_popup;
2004 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2007 RNA_def_int(ot->srna, "keys", 2, 2, INT_MAX, "Number of Keys", "", 2, 100);
2010 static void rekey_particle_to_time(Scene *scene, Object *ob, int pa_index, float path_time)
2012 PTCacheEdit *edit= PE_get_current(scene, ob);
2013 ParticleSystem *psys;
2014 ParticleSimulationData sim= {0};
2017 HairKey *new_keys, *key;
2018 PTCacheEditKey *ekey;
2021 if(!edit || !edit->psys) return;
2029 pa= psys->particles + pa_index;
2031 pa->flag |= PARS_REKEY;
2033 key= new_keys= MEM_dupallocN(pa->hair);
2035 /* interpolate new keys from old ones (roots stay the same) */
2036 for(k=1, key++; k < pa->totkey; k++, key++) {
2037 state.time= path_time * (float)k / (float)(pa->totkey-1);
2038 psys_get_particle_on_path(&sim, pa_index, &state, 0);
2039 VECCOPY(key->co, state.co);
2042 /* replace hair keys */
2044 MEM_freeN(pa->hair);
2047 /* update edit pointers */
2048 for(k=0, key=pa->hair, ekey=edit->points[pa_index].keys; k<pa->totkey; k++, key++, ekey++) {
2050 ekey->time= &key->time;
2053 pa->flag &= ~PARS_REKEY;
2056 /************************* utilities **************************/
2058 static int remove_tagged_particles(Object *ob, ParticleSystem *psys, int mirror)
2060 PTCacheEdit *edit = psys->edit;
2061 ParticleData *pa, *npa=0, *new_pars=0;
2063 PTCacheEditPoint *npoint=0, *new_points=0;
2064 ParticleSystemModifierData *psmd;
2065 int i, totpart, new_totpart= psys->totpart, removed= 0;
2069 psmd= psys_get_modifier(ob, psys);
2070 totpart= psys->totpart;
2072 LOOP_TAGGED_POINTS {
2073 PE_mirror_particle(ob, psmd->dm, psys, psys->particles + p, NULL);
2077 LOOP_TAGGED_POINTS {
2082 if(new_totpart != psys->totpart) {
2084 npa= new_pars= MEM_callocN(new_totpart * sizeof(ParticleData), "ParticleData array");
2085 npoint= new_points= MEM_callocN(new_totpart * sizeof(PTCacheEditPoint), "PTCacheEditKey array");
2088 pa= psys->particles;
2089 point= edit->points;
2090 for(i=0; i<psys->totpart; i++, pa++, point++) {
2091 if(point->flag & PEP_TAG) {
2093 MEM_freeN(point->keys);
2095 MEM_freeN(pa->hair);
2098 memcpy(npa, pa, sizeof(ParticleData));
2099 memcpy(npoint, point, sizeof(PTCacheEditPoint));
2105 if(psys->particles) MEM_freeN(psys->particles);
2106 psys->particles= new_pars;
2108 if(edit->points) MEM_freeN(edit->points);
2109 edit->points= new_points;
2111 if(edit->mirror_cache) {
2112 MEM_freeN(edit->mirror_cache);
2113 edit->mirror_cache= NULL;
2117 MEM_freeN(psys->child);
2122 edit->totpoint= psys->totpart= new_totpart;
2128 static void remove_tagged_keys(Object *ob, ParticleSystem *psys)
2130 PTCacheEdit *edit= psys->edit;
2132 HairKey *hkey, *nhkey, *new_hkeys=0;
2134 PTCacheEditKey *nkey, *new_keys;
2135 ParticleSystemModifierData *psmd;
2138 if(pe_x_mirror(ob)) {
2139 /* mirror key tags */
2140 psmd= psys_get_modifier(ob, psys);
2144 PE_mirror_particle(ob, psmd->dm, psys, psys->particles + p, NULL);
2151 new_totkey= point->totkey;
2155 /* we can't have elements with less than two keys*/
2157 point->flag |= PEP_TAG;
2159 remove_tagged_particles(ob, psys, pe_x_mirror(ob));
2162 pa = psys->particles + p;
2163 new_totkey= pa->totkey;
2169 if(new_totkey != pa->totkey) {
2170 nhkey= new_hkeys= MEM_callocN(new_totkey*sizeof(HairKey), "HairKeys");
2171 nkey= new_keys= MEM_callocN(new_totkey*sizeof(PTCacheEditKey), "particle edit keys");
2175 while(key->flag & PEK_TAG && hkey < pa->hair + pa->totkey) {
2180 if(hkey < pa->hair + pa->totkey) {
2181 VECCOPY(nhkey->co, hkey->co);
2182 nhkey->editflag = hkey->editflag;
2183 nhkey->time= hkey->time;
2184 nhkey->weight= hkey->weight;
2186 nkey->co= nhkey->co;
2187 nkey->time= &nhkey->time;
2188 /* these can be copied from old edit keys */
2189 nkey->flag = key->flag;
2190 nkey->ftime = key->ftime;
2191 nkey->length = key->length;
2192 VECCOPY(nkey->world_co, key->world_co);
2200 MEM_freeN(pa->hair);
2203 MEM_freeN(point->keys);
2205 pa->hair= new_hkeys;
2206 point->keys= new_keys;
2208 point->totkey= pa->totkey= new_totkey;
2210 /* flag for recalculating length */
2211 point->flag |= PEP_EDIT_RECALC;
2216 /************************ subdivide opertor *********************/
2218 /* works like normal edit mode subdivide, inserts keys between neighbouring selected keys */
2219 static void subdivide_particle(PEData *data, int pa_index)
2221 PTCacheEdit *edit= data->edit;
2222 ParticleSystem *psys= edit->psys;
2223 ParticleSimulationData sim= {0};
2224 ParticleData *pa= psys->particles + pa_index;
2225 PTCacheEditPoint *point = edit->points + pa_index;
2227 HairKey *key, *nkey, *new_keys;
2228 PTCacheEditKey *ekey, *nekey, *new_ekeys;
2234 sim.scene= data->scene;
2236 sim.psys= edit->psys;
2238 for(k=0, ekey=point->keys; k<pa->totkey-1; k++,ekey++) {
2239 if(ekey->flag&PEK_SELECT && (ekey+1)->flag&PEK_SELECT)
2243 if(totnewkey==0) return;
2245 pa->flag |= PARS_REKEY;
2247 nkey= new_keys= MEM_callocN((pa->totkey+totnewkey)*(sizeof(HairKey)),"Hair subdivide keys");
2248 nekey= new_ekeys= MEM_callocN((pa->totkey+totnewkey)*(sizeof(PTCacheEditKey)),"Hair subdivide edit keys");
2251 endtime= key[pa->totkey-1].time;
2253 for(k=0, ekey=point->keys; k<pa->totkey-1; k++, key++, ekey++) {
2255 memcpy(nkey,key,sizeof(HairKey));
2256 memcpy(nekey,ekey,sizeof(PTCacheEditKey));
2258 nekey->co= nkey->co;
2259 nekey->time= &nkey->time;
2264 if(ekey->flag & PEK_SELECT && (ekey+1)->flag & PEK_SELECT) {
2265 nkey->time= (key->time + (key+1)->time)*0.5f;
2266 state.time= (endtime != 0.0f)? nkey->time/endtime: 0.0f;
2267 psys_get_particle_on_path(&sim, pa_index, &state, 0);
2268 VECCOPY(nkey->co, state.co);
2270 nekey->co= nkey->co;
2271 nekey->time= &nkey->time;
2272 nekey->flag |= PEK_SELECT;
2273 if(!(psys->flag & PSYS_GLOBAL_HAIR))
2274 nekey->flag |= PEK_USE_WCO;
2280 /*tip still not copied*/
2281 memcpy(nkey,key,sizeof(HairKey));
2282 memcpy(nekey,ekey,sizeof(PTCacheEditKey));
2284 nekey->co= nkey->co;
2285 nekey->time= &nkey->time;
2288 MEM_freeN(pa->hair);
2292 MEM_freeN(point->keys);
2293 point->keys= new_ekeys;
2295 point->totkey = pa->totkey = pa->totkey + totnewkey;
2296 point->flag |= PEP_EDIT_RECALC;
2297 pa->flag &= ~PARS_REKEY;
2300 static int subdivide_exec(bContext *C, wmOperator *UNUSED(op))
2304 PE_set_data(C, &data);
2305 foreach_point(&data, subdivide_particle);
2307 recalc_lengths(data.edit);
2308 PE_update_object(data.scene, data.ob, 1);
2309 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
2311 return OPERATOR_FINISHED;
2314 void PARTICLE_OT_subdivide(wmOperatorType *ot)
2317 ot->name= "Subdivide";
2318 ot->idname= "PARTICLE_OT_subdivide";
2321 ot->exec= subdivide_exec;
2325 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2328 /************************ remove doubles opertor *********************/
2330 static int remove_doubles_exec(bContext *C, wmOperator *op)
2332 Scene *scene= CTX_data_scene(C);
2333 Object *ob= CTX_data_active_object(C);
2334 PTCacheEdit *edit= PE_get_current(scene, ob);
2335 ParticleSystem *psys = edit->psys;
2336 ParticleSystemModifierData *psmd;
2338 KDTreeNearest nearest[10];
2340 float mat[4][4], co[3], threshold= RNA_float_get(op->ptr, "threshold");
2341 int n, totn, removed, totremoved;
2343 if(psys->flag & PSYS_GLOBAL_HAIR)
2344 return OPERATOR_CANCELLED;
2347 psmd= psys_get_modifier(ob, psys);
2353 tree=BLI_kdtree_new(psys->totpart);
2355 /* insert particles into kd tree */
2356 LOOP_SELECTED_POINTS {
2357 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, psys->particles+p, mat);
2358 VECCOPY(co, point->keys->co);
2360 BLI_kdtree_insert(tree, p, co, NULL);
2363 BLI_kdtree_balance(tree);
2365 /* tag particles to be removed */
2366 LOOP_SELECTED_POINTS {
2367 psys_mat_hair_to_object(ob, psmd->dm, psys->part->from, psys->particles+p, mat);
2368 VECCOPY(co, point->keys->co);
2371 totn= BLI_kdtree_find_n_nearest(tree,10,co,NULL,nearest);
2373 for(n=0; n<totn; n++) {
2374 /* this needs a custom threshold still */
2375 if(nearest[n].index > p && nearest[n].dist < threshold) {
2376 if(!(point->flag & PEP_TAG)) {
2377 point->flag |= PEP_TAG;
2384 BLI_kdtree_free(tree);
2386 /* remove tagged particles - don't do mirror here! */
2387 remove_tagged_particles(ob, psys, 0);
2388 totremoved += removed;
2392 return OPERATOR_CANCELLED;
2394 BKE_reportf(op->reports, RPT_INFO, "Remove %d double particles.", totremoved);
2396 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2397 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
2399 return OPERATOR_FINISHED;
2402 void PARTICLE_OT_remove_doubles(wmOperatorType *ot)
2405 ot->name= "Remove Doubles";
2406 ot->idname= "PARTICLE_OT_remove_doubles";
2409 ot->exec= remove_doubles_exec;
2413 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2416 RNA_def_float(ot->srna, "threshold", 0.0002f, 0.0f, FLT_MAX, "Threshold", "Threshold distance withing which particles are removed", 0.00001f, 0.1f);
2420 static int weight_set_exec(bContext *C, wmOperator *op)
2422 Scene *scene= CTX_data_scene(C);
2423 ParticleEditSettings *pset= PE_settings(scene);
2424 Object *ob= CTX_data_active_object(C);
2425 PTCacheEdit *edit= PE_get_current(scene, ob);
2426 ParticleSystem *psys = edit->psys;
2431 ParticleBrushData *brush= &pset->brush[pset->brushtype];
2432 float factor= RNA_float_get(op->ptr, "factor");
2434 weight= brush->strength;
2437 LOOP_SELECTED_POINTS {
2438 ParticleData *pa= psys->particles + p;
2440 LOOP_SELECTED_KEYS {
2442 hkey->weight= interpf(weight, hkey->weight, factor);
2446 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2447 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
2449 return OPERATOR_FINISHED;
2452 void PARTICLE_OT_weight_set(wmOperatorType *ot)
2455 ot->name= "Weight Set";
2456 ot->idname= "PARTICLE_OT_weight_set";
2459 ot->exec= weight_set_exec;
2463 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2465 RNA_def_float(ot->srna, "factor", 1, 0, 1, "Factor", "", 0, 1);
2468 /************************ cursor drawing *******************************/
2470 static void brush_drawcursor(bContext *C, int x, int y, void *UNUSED(customdata))
2472 ParticleEditSettings *pset= PE_settings(CTX_data_scene(C));
2473 ParticleBrushData *brush;
2475 if(pset->brushtype < 0)
2478 brush= &pset->brush[pset->brushtype];
2483 glTranslatef((float)x, (float)y, 0.0f);
2485 glColor4ub(255, 255, 255, 128);
2486 glEnable(GL_LINE_SMOOTH );
2488 glutil_draw_lined_arc(0.0, M_PI*2.0, brush->size, 40);
2489 glDisable(GL_BLEND);
2490 glDisable(GL_LINE_SMOOTH );
2496 static void toggle_particle_cursor(bContext *C, int enable)
2498 ParticleEditSettings *pset= PE_settings(CTX_data_scene(C));
2500 if(pset->paintcursor && !enable) {
2501 WM_paint_cursor_end(CTX_wm_manager(C), pset->paintcursor);
2502 pset->paintcursor = NULL;
2505 pset->paintcursor= WM_paint_cursor_activate(CTX_wm_manager(C), PE_poll_view3d, brush_drawcursor, NULL);
2508 /********************* radial control operator *********************/
2510 static int brush_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
2512 ParticleEditSettings *pset= PE_settings(CTX_data_scene(C));
2513 ParticleBrushData *brush;
2514 int mode = RNA_enum_get(op->ptr, "mode");
2515 float original_value=1.0f;
2517 if(pset->brushtype < 0)
2518 return OPERATOR_CANCELLED;
2520 brush= &pset->brush[pset->brushtype];
2522 toggle_particle_cursor(C, 0);
2524 if(mode == WM_RADIALCONTROL_SIZE)
2525 original_value = brush->size;
2526 else if(mode == WM_RADIALCONTROL_STRENGTH)
2527 original_value = brush->strength;
2529 RNA_float_set(op->ptr, "initial_value", original_value);
2531 return WM_radial_control_invoke(C, op, event);
2534 static int brush_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
2536 int ret = WM_radial_control_modal(C, op, event);
2538 if(ret != OPERATOR_RUNNING_MODAL)
2539 toggle_particle_cursor(C, 1);
2544 static int brush_radial_control_exec(bContext *C, wmOperator *op)
2546 ParticleEditSettings *pset= PE_settings(CTX_data_scene(C));
2547 ParticleBrushData *brush;
2548 int mode = RNA_enum_get(op->ptr, "mode");
2549 float new_value = RNA_float_get(op->ptr, "new_value");
2551 if(pset->brushtype < 0)
2552 return OPERATOR_CANCELLED;
2554 brush= &pset->brush[pset->brushtype];
2556 if(mode == WM_RADIALCONTROL_SIZE)
2557 brush->size= new_value;
2558 else if(mode == WM_RADIALCONTROL_STRENGTH)
2559 brush->strength= new_value;
2561 WM_event_add_notifier(C, NC_WINDOW, NULL);
2563 return OPERATOR_FINISHED;
2566 void PARTICLE_OT_brush_radial_control(wmOperatorType *ot)
2568 WM_OT_radial_control_partial(ot);
2570 ot->name= "Brush Radial Control";
2571 ot->idname= "PARTICLE_OT_brush_radial_control";
2573 ot->invoke= brush_radial_control_invoke;
2574 ot->modal= brush_radial_control_modal;
2575 ot->exec= brush_radial_control_exec;
2579 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2582 /*************************** delete operator **************************/
2584 enum { DEL_PARTICLE, DEL_KEY };
2586 static EnumPropertyItem delete_type_items[]= {
2587 {DEL_PARTICLE, "PARTICLE", 0, "Particle", ""},
2588 {DEL_KEY, "KEY", 0, "Key", ""},
2589 {0, NULL, 0, NULL, NULL}};
2591 static void set_delete_particle(PEData *data, int pa_index)
2593 PTCacheEdit *edit= data->edit;
2595 edit->points[pa_index].flag |= PEP_TAG;
2598 static void set_delete_particle_key(PEData *data, int pa_index, int key_index)
2600 PTCacheEdit *edit= data->edit;
2602 edit->points[pa_index].keys[key_index].flag |= PEK_TAG;
2605 static int delete_exec(bContext *C, wmOperator *op)
2608 int type= RNA_enum_get(op->ptr, "type");
2610 PE_set_data(C, &data);
2612 if(type == DEL_KEY) {
2613 foreach_selected_key(&data, set_delete_particle_key);
2614 remove_tagged_keys(data.ob, data.edit->psys);
2615 recalc_lengths(data.edit);
2617 else if(type == DEL_PARTICLE) {
2618 foreach_selected_point(&data, set_delete_particle);
2619 remove_tagged_particles(data.ob, data.edit->psys, pe_x_mirror(data.ob));
2620 recalc_lengths(data.edit);
2623 DAG_id_tag_update(&data.ob->id, OB_RECALC_DATA);
2624 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, data.ob);
2626 return OPERATOR_FINISHED;
2629 void PARTICLE_OT_delete(wmOperatorType *ot)
2633 ot->idname= "PARTICLE_OT_delete";
2636 ot->exec= delete_exec;
2637 ot->invoke= WM_menu_invoke;
2638 ot->poll= PE_hair_poll;
2641 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2644 ot->prop= RNA_def_enum(ot->srna, "type", delete_type_items, DEL_PARTICLE, "Type", "Delete a full particle or only keys.");
2647 /*************************** mirror operator **************************/
2649 static void PE_mirror_x(Scene *scene, Object *ob, int tagged)
2651 Mesh *me= (Mesh*)(ob->data);
2652 ParticleSystemModifierData *psmd;
2653 PTCacheEdit *edit= PE_get_current(scene, ob);
2654 ParticleSystem *psys = edit->psys;
2655 ParticleData *pa, *newpa, *new_pars;
2656 PTCacheEditPoint *newpoint, *new_points;
2660 int rotation, totpart, newtotpart;
2662 if(psys->flag & PSYS_GLOBAL_HAIR)
2665 psmd= psys_get_modifier(ob, psys);
2669 mirrorfaces= mesh_get_x_mirror_faces(ob, NULL);
2671 if(!edit->mirror_cache)
2672 PE_update_mirror_cache(ob, psys);
2674 totpart= psys->totpart;
2675 newtotpart= psys->totpart;
2676 LOOP_VISIBLE_POINTS {
2677 pa = psys->particles + p;
2679 if(point_is_selected(point)) {
2680 if(edit->mirror_cache[p] != -1) {
2681 /* already has a mirror, don't need to duplicate */
2682 PE_mirror_particle(ob, psmd->dm, psys, pa, NULL);
2686 point->flag |= PEP_TAG;
2690 if((point->flag & PEP_TAG) && mirrorfaces[pa->num*2] != -1)
2694 if(newtotpart != psys->totpart) {
2695 /* allocate new arrays and copy existing */
2696 new_pars= MEM_callocN(newtotpart*sizeof(ParticleData), "ParticleData new");
2697 new_points= MEM_callocN(newtotpart*sizeof(PTCacheEditPoint), "PTCacheEditPoint new");
2699 if(psys->particles) {
2700 memcpy(new_pars, psys->particles, totpart*sizeof(ParticleData));
2701 MEM_freeN(psys->particles);
2703 psys->particles= new_pars;
2706 memcpy(new_points, edit->points, totpart*sizeof(PTCacheEditPoint));
2707 MEM_freeN(edit->points);
2709 edit->points= new_points;
2711 if(edit->mirror_cache) {
2712 MEM_freeN(edit->mirror_cache);
2713 edit->mirror_cache= NULL;
2716 edit->totpoint= psys->totpart= newtotpart;
2718 /* create new elements */
2719 newpa= psys->particles + totpart;
2720 newpoint= edit->points + totpart;
2722 for(p=0, point=edit->points; p<totpart; p++, point++) {
2723 pa = psys->particles + p;
2725 if(point->flag & PEP_HIDE)
2727 if(!(point->flag & PEP_TAG) || mirrorfaces[pa->num*2] == -1)
2733 if(pa->hair) newpa->hair= MEM_dupallocN(pa->hair);
2734 if(point->keys) newpoint->keys= MEM_dupallocN(point->keys);
2736 /* rotate weights according to vertex index rotation */
2737 rotation= mirrorfaces[pa->num*2+1];
2738 newpa->fuv[0]= pa->fuv[2];
2739 newpa->fuv[1]= pa->fuv[1];
2740 newpa->fuv[2]= pa->fuv[0];
2741 newpa->fuv[3]= pa->fuv[3];
2742 while(rotation-- > 0)
2743 if(me->mface[pa->num].v4)
2744 SHIFT4(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2], newpa->fuv[3])
2746 SHIFT3(float, newpa->fuv[0], newpa->fuv[1], newpa->fuv[2])
2748 /* assign face inddex */
2749 newpa->num= mirrorfaces[pa->num*2];
2750 newpa->num_dmcache= psys_particle_dm_face_lookup(ob,psmd->dm,newpa->num,newpa->fuv, NULL);
2752 /* update edit key pointers */
2753 key= newpoint->keys;
2754 for(k=0, hkey=newpa->hair; k<newpa->totkey; k++, hkey++, key++) {
2756 key->time= &hkey->time;
2759 /* map key positions as mirror over x axis */
2760 PE_mirror_particle(ob, psmd->dm, psys, pa, newpa);
2768 point->flag &= ~PEP_TAG;
2771 MEM_freeN(mirrorfaces);
2774 static int mirror_exec(bContext *C, wmOperator *UNUSED(op))
2776 Scene *scene= CTX_data_scene(C);
2777 Object *ob= CTX_data_active_object(C);
2778 PTCacheEdit *edit= PE_get_current(scene, ob);
2780 PE_mirror_x(scene, ob, 0);
2782 update_world_cos(ob, edit);
2783 WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_EDITED, ob);
2784 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2786 return OPERATOR_FINISHED;
2789 void PARTICLE_OT_mirror(wmOperatorType *ot)
2793 ot->idname= "PARTICLE_OT_mirror";
2796 ot->exec= mirror_exec;
2800 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2803 /************************* brush edit callbacks ********************/
2805 static void brush_comb(PEData *data, float UNUSED(mat[][4]), float imat[][4], int point_index, int key_index, PTCacheEditKey *key)
2807 ParticleEditSettings *pset= PE_settings(data->scene);
2810 if(pset->flag & PE_LOCK_FIRST && key_index == 0) return;
2812 fac= (float)pow((double)(1.0f - data->dist / data->rad), (double)data->combfac);
2814 VECCOPY(cvec,data->dvec);
2815 mul_mat3_m4_v3(imat,cvec);
2816 mul_v3_fl(cvec, fac);
2817 VECADD(key->co, key->co, cvec);
2819 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
2822 static void brush_cut(PEData *data, int pa_index)
2824 PTCacheEdit *edit = data->edit;
2825 ARegion *ar= data->vc.ar;
2826 Object *ob= data->ob;
2827 ParticleEditSettings *pset= PE_settings(data->scene);
2828 ParticleCacheKey *key= edit->pathcache[pa_index];
2829 float rad2, cut_time= 1.0;
2830 float x0, x1, v0, v1, o0, o1, xo0, xo1, d, dv;
2831 int k, cut, keys= (int)pow(2.0, (double)pset->draw_step);
2834 /* blunt scissors */
2835 if(BLI_frand() > data->cutfac) return;
2837 /* don't cut hidden */
2838 if(edit->points[pa_index].flag & PEP_HIDE)
2841 rad2= data->rad * data->rad;
2845 project_short_noclip(ar, key->co, vertco);
2846 x0= (float)vertco[0];
2847 x1= (float)vertco[1];
2849 o0= (float)data->mval[0];
2850 o1= (float)data->mval[1];
2855 /* check if root is inside circle */
2856 if(xo0*xo0 + xo1*xo1 < rad2 && key_test_depth(data, key->co)) {
2861 /* calculate path time closest to root that was inside the circle */
2862 for(k=1, key++; k<=keys; k++, key++) {
2863 project_short_noclip(ar, key->co, vertco);
2865 if(key_test_depth(data, key->co) == 0) {
2866 x0= (float)vertco[0];
2867 x1= (float)vertco[1];
2874 v0= (float)vertco[0] - x0;
2875 v1= (float)vertco[1] - x1;
2879 d= (v0*xo1 - v1*xo0);
2886 cut_time= -(v0*xo0 + v1*xo1 + d);
2888 if(cut_time > 0.0f) {
2891 if(cut_time < 1.0f) {
2892 cut_time += (float)(k-1);
2893 cut_time /= (float)keys;
2900 x0= (float)vertco[0];
2901 x1= (float)vertco[1];
2909 if(cut_time < 0.0f) {
2910 edit->points[pa_index].flag |= PEP_TAG;
2913 rekey_particle_to_time(data->scene, ob, pa_index, cut_time);
2914 edit->points[pa_index].flag |= PEP_EDIT_RECALC;
2919 static void brush_length(PEData *data, int point_index)
2921 PTCacheEdit *edit= data->edit;
2922 PTCacheEditPoint *point = edit->points + point_index;
2924 float dvec[3],pvec[3] = {0.0f, 0.0f, 0.0f};
2928 VECCOPY(pvec,key->co);
2931 VECSUB(dvec,key->co,pvec);
2932 VECCOPY(pvec,key->co);
2933 mul_v3_fl(dvec,data->growfac);
2934 VECADD(key->co,(key-1)->co,dvec);
2938 point->flag |= PEP_EDIT_RECALC;
2941 static void brush_puff(PEData *data, int point_index)
2943 PTCacheEdit *edit = data->edit;
2944 ParticleSystem *psys = edit->psys;
2945 PTCacheEditPoint *point = edit->points + point_index;
2947 float mat[4][4], imat[4][4];
2949 float lastco[3], rootco[3] = {0.0f, 0.0f, 0.0f}, co[3], nor[3], kco[3], dco[3], ofs[3] = {0.0f, 0.0f, 0.0f}, fac=0.0f, length=0.0f;
2950 int puff_volume = 0;
2954 ParticleEditSettings *pset= PE_settings(data->scene);
2955 ParticleBrushData *brush= &pset->brush[pset->brushtype];
2956 puff_volume = brush->flag & PE_BRUSH_DATA_PUFF_VOLUME;
2959 if(psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
2960 psys_mat_hair_to_global(data->ob, data->dm, psys->part->from, psys->particles + point_index, mat);
2961 invert_m4_m4(imat,mat);
2970 /* find root coordinate and normal on emitter */
2971 VECCOPY(co, key->co);
2973 mul_v3_m4v3(kco, data->ob->imat, co); /* use 'kco' as the object space version of worldspace 'co', ob->imat is set before calling */
2975 point_index= BLI_kdtree_find_nearest(edit->emitter_field, kco, NULL, NULL);
2976 if(point_index == -1) return;
2978 VECCOPY(rootco, co);
2979 copy_v3_v3(nor, &edit->emitter_cosnos[point_index*6+3]);
2980 mul_mat3_m4_v3(data->ob->obmat, nor); /* normal into worldspace */
2985 fac= (float)pow((double)(1.0f - data->dist / data->rad), (double)data->pufffac);
2991 /* compute position as if hair was standing up straight.
2993 VECCOPY(lastco, co);
2994 VECCOPY(co, key->co);
2996 length += len_v3v3(lastco, co);
2997 if((data->select==0 || (key->flag & PEK_SELECT)) && !(key->flag & PEK_HIDE)) {
2998 VECADDFAC(kco, rootco, nor, length);
3000 /* blend between the current and straight position */
3001 VECSUB(dco, kco, co);
3002 VECADDFAC(co, co, dco, fac);
3004 /* re-use dco to compare before and after translation and add to the offset */
3005 VECCOPY(dco, key->co);
3007 mul_v3_m4v3(key->co, imat, co);
3010 /* accumulate the total distance moved to apply to unselected
3011 * keys that come after */
3012 ofs[0] += key->co[0] - dco[0];
3013 ofs[1] += key->co[1] - dco[1];
3014 ofs[2] += key->co[2] - dco[2];
3022 /* this is simple but looks bad, adds annoying kinks */
3023 add_v3_v3(key->co, ofs);
3025 /* translate (not rotate) the rest of the hair if its not selected */
3026 if(ofs[0] || ofs[1] || ofs[2]) {
3027 #if 0 /* kindof works but looks worse then whats below */
3029 /* Move the unselected point on a vector based on the
3030 * hair direction and the offset */
3032 VECSUB(dco, lastco, co);
3033 mul_mat3_m4_v3(imat, dco); /* into particle space */
3035 /* move the point allong a vector perpendicular to the
3036 * hairs direction, reduces odd kinks, */
3037 cross_v3_v3v3(c1, ofs, dco);
3038 cross_v3_v3v3(c2, c1, dco);
3040 mul_v3_fl(c2, len_v3(ofs));
3041 add_v3_v3(key->co, c2);
3043 /* Move the unselected point on a vector based on the
3044 * the normal of the closest geometry */
3045 float oco[3], onor[3];
3046 VECCOPY(oco, key->co);
3047 mul_m4_v3(mat, oco);
3048 mul_v3_m4v3(kco, data->ob->imat, oco); /* use 'kco' as the object space version of worldspace 'co', ob->imat is set before calling */
3050 point_index= BLI_kdtree_find_nearest(edit->emitter_field, kco, NULL, NULL);
3051 if(point_index != -1) {
3052 copy_v3_v3(onor, &edit->emitter_cosnos[point_index*6+3]);
3053 mul_mat3_m4_v3(data->ob->obmat, onor); /* normal into worldspace */
3054 mul_mat3_m4_v3(imat, onor); /* worldspace into particle space */
3058 mul_v3_fl(onor, len_v3(ofs));
3059 add_v3_v3(key->co, onor);
3070 point->flag |= PEP_EDIT_RECALC;
3074 static void brush_weight(PEData *data, float UNUSED(mat[][4]), float UNUSED(imat[][4]), int point_index, int key_index, PTCacheEditKey *UNUSED(key))
3076 /* roots have full weight allways */
3078 PTCacheEdit *edit = data->edit;
3079 ParticleSystem *psys = edit->psys;
3081 ParticleData *pa= psys->particles + point_index;
3082 pa->hair[key_index].weight = data->weightfac;
3084 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
3088 static void brush_smooth_get(PEData *data, float mat[][4], float UNUSED(imat[][4]), int UNUSED(point_index), int key_index, PTCacheEditKey *key)
3093 sub_v3_v3v3(dvec,key->co,(key-1)->co);
3094 mul_mat3_m4_v3(mat,dvec);
3095 VECADD(data->vec,data->vec,dvec);
3100 static void brush_smooth_do(PEData *data, float UNUSED(mat[][4]), float imat[][4], int point_index, int key_index, PTCacheEditKey *key)
3102 float vec[3], dvec[3];
3105 VECCOPY(vec,data->vec);
3106 mul_mat3_m4_v3(imat,vec);
3108 sub_v3_v3v3(dvec,key->co,(key-1)->co);
3110 VECSUB(dvec,vec,dvec);
3111 mul_v3_fl(dvec,data->smoothfac);
3113 VECADD(key->co,key->co,dvec);
3116 (data->edit->points + point_index)->flag |= PEP_EDIT_RECALC;
3119 static int brush_add(PEData *data, short number)
3121 Scene *scene= data->scene;
3122 Object *ob= data->ob;
3123 PTCacheEdit *edit = data->edit;
3124 ParticleSystem *psys= edit->psys;
3125 ParticleData *add_pars= MEM_callocN(number*sizeof(ParticleData),"ParticleData add");
3126 ParticleSystemModifierData *psmd= psys_get_modifier(ob,psys);
3127 ParticleSimulationData sim= {0};
3128 ParticleEditSettings *pset= PE_settings(scene);
3129 int i, k, n= 0, totpart= psys->totpart;
3131 short dmx= 0, dmy= 0;
3132 float co1[3], co2[3], min_d, imat[4][4];
3133 float framestep, timestep;
3134 short size= pset->brush[PE_BRUSH_ADD].size;
3135 short size2= size*size;
3137 invert_m4_m4(imat,ob->obmat);
3139 if(psys->flag & PSYS_GLOBAL_HAIR)
3142 BLI_srandom(psys->seed+data->mval[0]+data->mval[1]);
3149 timestep= psys_get_timestep(&sim);
3151 /* painting onto the deformed mesh, could be an option? */
3152 if(psmd->dm->deformedOnly)
3155 dm= mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
3157 for(i=0; i<number; i++) {
3160 while(dmx*dmx+dmy*dmy>size2) {
3161 dmx=(short)((2.0f*BLI_frand()-1.0f)*size);
3162 dmy=(short)((2.0f*BLI_frand()-1.0f)*size);
3166 mco[0]= data->mval[0] + dmx;
3167 mco[1]= data->mval[1] + dmy;
3168 viewline(data->vc.ar, data->vc.v3d, mco, co1, co2);
3170 mul_m4_v3(imat,co1);
3171 mul_m4_v3(imat,co2);
3174 /* warning, returns the derived mesh face */
3175 if(psys_intersect_dm(scene, ob,dm,0,co1,co2,&min_d,&add_pars[n].num,add_pars[n].fuv,0,0,0,0)) {