[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [48199] branches/soc-2012-sushi/source/ blender: Initial work on generalised snapping system in snap.c and BKE_snap .h

luke frisken l.frisken at gmail.com
Fri Jun 22 17:42:55 CEST 2012


Revision: 48199
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48199
Author:   lfrisken
Date:     2012-06-22 15:42:55 +0000 (Fri, 22 Jun 2012)
Log Message:
-----------
Initial work on generalised snapping system in snap.c and BKE_snap.h
Also I had to add some includes in bmesh_inline.h and
bmesh_operator_api.h in order to get this code compiling.

Modified Paths:
--------------
    branches/soc-2012-sushi/source/blender/blenkernel/CMakeLists.txt
    branches/soc-2012-sushi/source/blender/bmesh/intern/bmesh_inline.h
    branches/soc-2012-sushi/source/blender/bmesh/intern/bmesh_operator_api.h

Added Paths:
-----------
    branches/soc-2012-sushi/source/blender/blenkernel/BKE_snap.h
    branches/soc-2012-sushi/source/blender/blenkernel/intern/snap.c

Added: branches/soc-2012-sushi/source/blender/blenkernel/BKE_snap.h
===================================================================
--- branches/soc-2012-sushi/source/blender/blenkernel/BKE_snap.h	                        (rev 0)
+++ branches/soc-2012-sushi/source/blender/blenkernel/BKE_snap.h	2012-06-22 15:42:55 UTC (rev 48199)
@@ -0,0 +1,32 @@
+#ifndef __BKE_SNAP_H__
+#define __BKE_SNAP_H__
+
+typedef struct{
+	//perhaps more info later I might guess
+	float location[3];
+	float normal[3];
+}SnapPoint;
+
+typedef struct Snap Snap;
+
+typedef enum{
+	SNAPMESH_DATA_TYPE_DerivedMesh,
+	SNAPMESH_DATA_TYPE_BMEditMesh
+}SnapMesh_data_type;
+
+typedef enum{
+	SNAPMESH_TYPE_VERTEX,
+	SNAPMESH_TYPE_FACE,
+	SNAPMESH_TYPE_FACE_PLANE,
+	SNAPMESH_TYPE_EDGE,
+	SNAPMESH_TYPE_EDGE_MIDPOINT,
+	SNAPMESH_TYPE_EDGE_PARALLEL
+}SnapMesh_type;
+
+typedef enum{
+	SNAPMESH_DAT_vert,
+	SNAPMESH_DAT_edge,
+	SNAPMESH_DAT_face
+}SnapMesh_data_array_type;
+
+#endif // __BKE_SNAP_H__

Modified: branches/soc-2012-sushi/source/blender/blenkernel/CMakeLists.txt
===================================================================
--- branches/soc-2012-sushi/source/blender/blenkernel/CMakeLists.txt	2012-06-22 15:40:49 UTC (rev 48198)
+++ branches/soc-2012-sushi/source/blender/blenkernel/CMakeLists.txt	2012-06-22 15:42:55 UTC (rev 48199)
@@ -43,6 +43,7 @@
 	../../../intern/memutil
 	../../../intern/mikktspace
 	../../../intern/opennl/extern
+	../editors/include
 
 	# XXX - BAD LEVEL CALL WM_api.h
 	../windowmanager
@@ -129,6 +130,7 @@
 	intern/shrinkwrap.c
 	intern/sketch.c
 	intern/smoke.c
+	intern/snap.c
 	intern/softbody.c
 	intern/sound.c
 	intern/speaker.c
@@ -213,6 +215,7 @@
 	BKE_shrinkwrap.h
 	BKE_sketch.h
 	BKE_smoke.h
+	BKE_snap.h
 	BKE_softbody.h
 	BKE_sound.h
 	BKE_speaker.h

Added: branches/soc-2012-sushi/source/blender/blenkernel/intern/snap.c
===================================================================
--- branches/soc-2012-sushi/source/blender/blenkernel/intern/snap.c	                        (rev 0)
+++ branches/soc-2012-sushi/source/blender/blenkernel/intern/snap.c	2012-06-22 15:42:55 UTC (rev 48199)
@@ -0,0 +1,464 @@
+#include "BKE_snap.h"
+
+#include "BKE_mesh.h"
+#include "BKE_tessmesh.h"
+#include "BKE_DerivedMesh.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_object_types.h"
+#include "DNA_view3d_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_mesh_types.h"
+
+#include "ED_view3d.h"
+
+#include "ED_mesh.h"
+
+#include "BLI_math_vector.h"
+
+#include <stdlib.h>
+#include <math.h>
+#include <float.h>
+
+/* -- PROTOTYPES -- */
+void Snap_draw_default(Snap *s);
+void SnapMesh_run(Snap *s);
+
+
+
+struct Snap{
+	int snap_found; //boolean whether snap point has been found
+	SnapPoint snap_point;
+
+	//TODO: all extra data in Snap should be subject to streamlining and change
+	//in the near future, as soon as I get something working. A lot of quick
+	//not-so-well-thought-out bits of code here that will need more thought later.
+
+	int retval; //temporary variable for linking in with transform code
+
+	/*external data pointers*/
+	Scene* scene;
+	Object* ob;
+	View3D* v3d;
+	ARegion* ar;
+	int mval[2];
+
+	/*internal calculation data*/
+	float ray_start[3], ray_normal[3];
+	float ray_start_local[3], ray_normal_local[3];
+	int min_distance;
+	int r_dist;
+
+	//matrix storage
+	float imat[4][4];
+	float timat[3][3];
+
+
+	//snap type -- type of snap to calculate
+	//geom_data_type
+
+	//transform
+
+	void* snap_data;
+
+	void (*run)(Snap*); //function pointer
+	void (*draw)(Snap*); //function pointer
+	//void callback for doing transform -- function pointer
+};
+
+
+Snap* Snap_create(Scene *scene, Object* ob, View3D *v3d, ARegion *ar, int mval[2]){
+	Snap* s = (Snap*)MEM_mallocN(sizeof(Snap), "snap");
+
+	s->scene = scene;
+	s->ob = ob;
+	s->v3d = v3d;
+	s->ar = ar;
+	copy_v2_v2_int(s->mval, mval);
+
+	s->min_distance = 30; //TODO: change to a user defined value;
+
+	s->snap_found = 0;
+	s->run = NULL;
+	s->draw = NULL;
+	s->snap_data = NULL;
+	return s;
+}
+
+void Snap_setmval(Snap* s, int mval[2]){
+	copy_v2_v2_int(s->mval, mval);
+}
+
+void Snap_calc_rays(Snap* s){
+	ED_view3d_win_to_ray(s->ar, s->v3d, s->mval, s->ray_start, s->ray_normal);
+}
+
+void Snap_calc_matrix(Snap* s){
+	invert_m4_m4(s->imat, s->ob->obmat);
+
+	copy_m3_m4(s->timat, s->imat);
+	transpose_m3(s->timat);
+
+	copy_v3_v3(s->ray_start_local, s->ray_start);
+	copy_v3_v3(s->ray_normal_local, s->ray_normal);
+
+	//for edit mode only? should be placed in calc_rays?
+	mul_m4_v3(s->imat, s->ray_start_local);
+	mul_mat3_m4_v3(s->imat, s->ray_normal_local);
+}
+
+void Snap_free(Snap* s){
+	MEM_freeN(s);
+}
+
+void Snap_run(Snap* s){
+	s->run(s);
+}
+
+void Snap_draw(Snap* s){
+	s->draw(s);
+}
+
+void Snap_draw_default(Snap *s){
+
+}
+
+
+
+/* -- MESH SNAPPING -- */
+
+typedef struct {
+	SnapMesh_type sm_type;// -- type of snap to calculate -- can be combination of several
+
+	SnapMesh_data_type data_type; //the type of data stored in mesh_data pointer.
+	void* mesh_data;
+
+	int *dm_index_array; //for use when using derivedmesh data type;
+
+	//function mesh iterator -- hmm actually let's just make this SnapMesh_mesh_itorator()
+
+}SnapMesh_data;
+
+Snap* SnapMesh_create(	void* mesh_data,
+						SnapMesh_data_type data_type,
+						SnapMesh_type sm_type,
+						Scene *scene, Object *ob, View3D *v3d, ARegion *ar, int mval[2]){
+	Snap* sm = Snap_create(scene, ob, v3d, ar, mval);
+	SnapMesh_data* sm_data;
+	sm->run = SnapMesh_run; //not sure whether to split into the seperate SnapMesh types here or not
+	//leaving generic SnapMesh_run for now.
+
+	sm->draw = Snap_draw_default; //default drawing function. should put an optional over-ride here.
+
+
+	sm_data = (SnapMesh_data*)MEM_mallocN(sizeof(SnapMesh_data), "snapmesh_data");
+
+	sm_data->sm_type = sm_type;
+
+	//put an early exit flag somewhere here for the case when there is no geometry in mesh_data;
+	sm_data->data_type = data_type;
+	sm_data->mesh_data = mesh_data;
+	sm_data->dm_index_array = NULL;
+
+	sm->snap_data = (void*)sm_data;
+
+	return sm;
+}
+
+void SnapMesh_free(Snap* sm){
+	MEM_freeN(sm->snap_data);
+	Snap_free(sm);
+}
+
+/* Mesh Data Functions */
+
+void copy_mvert(MVert* out, MVert* in){
+	copy_v3_v3(out->co, in->co);
+	copy_v3_v3_short(out->no, in->no);
+	out->flag = in->flag;
+	out->bweight = in->bweight;
+}
+
+//copied from editderivedmesh.c
+void bmvert_to_mvert(BMesh *bm, BMVert *bmv, MVert *mv)
+{
+	copy_v3_v3(mv->co, bmv->co);
+	normal_float_to_short_v3(mv->no, bmv->no);
+
+	mv->flag = BM_vert_flag_to_mflag(bmv);
+
+	if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
+		mv->bweight = (unsigned char) (BM_elem_float_data_get(&bm->vdata, bmv, CD_BWEIGHT) * 255.0f);
+	}
+}
+
+void bmedge_to_medge(BMesh *bm, BMEdge *ee, MEdge *edge_r)
+{
+
+}
+
+void SnapMesh_data_index_init(Snap *sm, SnapMesh_data_array_type array_type){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+	switch(sm_data->data_type){
+	//hmm probably not the best for performance
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		switch(array_type){
+		case SNAPMESH_DAT_vert:
+			em = (BMEditMesh*)sm_data->mesh_data;
+			EDBM_index_arrays_init(em, 1, 0, 0);
+		case SNAPMESH_DAT_edge:
+			em = (BMEditMesh*)sm_data->mesh_data;
+			EDBM_index_arrays_init(em, 0, 1, 0);
+		case SNAPMESH_DAT_face:
+			em = (BMEditMesh*)sm_data->mesh_data;
+			EDBM_index_arrays_init(em, 0, 0, 1);
+		}
+
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		sm_data->dm_index_array = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
+	}
+}
+
+int SnapMesh_data_getNumVerts(Snap *sm){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+
+	switch(sm_data->data_type){
+	//hmm probably not the best for performance
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		em = (BMEditMesh*)sm_data->mesh_data;
+		return em->bm->totvert;
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		return dm->getNumVerts(dm);
+		//verts[i];
+	}
+	return 0;
+}
+
+int SnapMesh_data_getNumEdges(Snap *sm){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+
+	switch(sm_data->data_type){
+	//hmm probably not the best for performance
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		em = (BMEditMesh*)sm_data->mesh_data;
+		return em->bm->totedge;
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		return dm->getNumEdges(dm);
+		//verts[i];
+	}
+	return 0;
+}
+
+int SnapMesh_data_getNumFaces(Snap *sm){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+
+	switch(sm_data->data_type){
+	//hmm probably not the best for performance
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		em = (BMEditMesh*)sm_data->mesh_data;
+		return em->bm->totface;
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		return dm->getNumPolys(dm);
+	}
+	return 0;
+}
+
+/* I've chosen to have MVert and MEdge as the format in use in snapping because it's
+  much easier to convert from BMVert to MVert than the reverse*/
+inline void SnapMesh_data_getVert(Snap *sm, int index, MVert* mv){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	MVert *verts;
+	BMVert* bmv;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+
+	switch(sm_data->data_type){
+	//hmm probably not the best for performance
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		em = (BMEditMesh*)sm_data->mesh_data;
+		bmv = EDBM_vert_at_index(em, index);
+		bmvert_to_mvert(em->bm, bmv, mv);
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		verts = dm->getVertArray(dm);
+		copy_mvert(mv, &(verts[index]));
+	}
+}
+
+//if vert is not hidden and not selected return 1 else 0
+int SnapMesh_data_VertCheck(Snap *sm, int index){
+	SnapMesh_data* sm_data = (SnapMesh_data*)sm->snap_data;
+	MVert *verts;
+	BMVert* bmv;
+	DerivedMesh* dm;
+	BMEditMesh* em;
+
+	switch(sm_data->data_type){
+	case SNAPMESH_DATA_TYPE_BMEditMesh:
+		bmv = EDBM_vert_at_index(em, index);
+		if(BM_elem_flag_test(bmv, BM_ELEM_HIDDEN) || BM_elem_flag_test(bmv, BM_ELEM_SELECT)){
+			return 0;
+		}else{
+			return 1;
+		}
+	case SNAPMESH_DATA_TYPE_DerivedMesh:
+		dm = (DerivedMesh*)sm_data->mesh_data;
+		verts = dm->getVertArray(dm);
+		if((verts[index].flag & 0) || (verts[index].flag & ME_HIDE)){
+			return 0;
+		}else{
+			return 1;
+		}
+	default:
+		return 0;
+	}
+}
+inline void SnapMesh_data_getEdge(SnapMesh_data *sm_data, SnapMesh_data_type data_type,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list