From 62ed3c65e3a8cbbf7fc6f303b0c72aa28a04d80c Mon Sep 17 00:00:00 2001 From: nobonobo Date: Wed, 11 May 2016 06:26:39 +0900 Subject: [PATCH 1/5] trimesh bug fix --- ode.go | 17 +++-------------- trimesh.go | 2 +- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/ode.go b/ode.go index 8a68777..2d14cc7 100644 --- a/ode.go +++ b/ode.go @@ -183,7 +183,7 @@ type VertexList Matrix // NewVertexList returns a new VertexList instance. func NewVertexList(size int, vals ...float64) VertexList { - return VertexList(NewMatrix(size, 3, 1, vals...)) + return VertexList(NewMatrix(size, 3, 4, vals...)) } // PlaneList represents a list of plane definitions. @@ -195,22 +195,11 @@ func NewPlaneList(size int, vals ...float64) PlaneList { } // TriVertexIndexList represents a list of triangle vertex indices. -type TriVertexIndexList [][]uint32 +type TriVertexIndexList []uint32 // NewTriVertexIndexList returns a new TriVertexIndexList instance. func NewTriVertexIndexList(size int, indices ...uint32) TriVertexIndexList { - list := make(TriVertexIndexList, size) - elts := make([]uint32, 3*size) - for i := range list { - list[i], elts = elts[:3], elts[3:] - n := 3 - if len(indices) < 3 { - n = len(indices) - } - copy(list[i], indices[:n]) - indices = indices[n:] - } - return list + return indices[:size*3] } // PolygonList represents a list of polygon definitions diff --git a/trimesh.go b/trimesh.go index a3ece0c..0c0a2b9 100644 --- a/trimesh.go +++ b/trimesh.go @@ -38,7 +38,7 @@ func (t TriMeshData) Destroy() { // Build builds a triangle mesh from the given data. func (t TriMeshData) Build(verts VertexList, tris TriVertexIndexList) { C.dGeomTriMeshDataBuildSimple(t.c(), (*C.dReal)(&verts[0][0]), C.int(len(verts)), - (*C.dTriIndex)(&tris[0][0]), C.int(len(tris))) + (*C.dTriIndex)(&tris[0]), C.int(len(tris))) } // Preprocess preprocesses the triangle mesh data. From 52df271890d04ccf01725543d96e40b8a8f9bb50 Mon Sep 17 00:00:00 2001 From: nobonobo Date: Thu, 12 May 2016 07:12:08 +0900 Subject: [PATCH 2/5] avoidance for mark and sweep by GC --- trimesh.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/trimesh.go b/trimesh.go index 0c0a2b9..64d0754 100644 --- a/trimesh.go +++ b/trimesh.go @@ -25,6 +25,11 @@ func (t TriMeshData) c() C.dTriMeshDataID { return C.dTriMeshDataID(unsafe.Pointer(t)) } +var ( + vertexListMap = map[int]VertexList{} + indexListMap = map[int]TriVertexIndexList{} +) + // NewTriMeshData returns a new TriMeshData instance. func NewTriMeshData() TriMeshData { return cToTriMeshData(C.dGeomTriMeshDataCreate()) @@ -32,13 +37,19 @@ func NewTriMeshData() TriMeshData { // Destroy destroys the triangle mesh data. func (t TriMeshData) Destroy() { + delete(vertexListMap, int(t)) + delete(indexListMap, int(t)) C.dGeomTriMeshDataDestroy(t.c()) } // Build builds a triangle mesh from the given data. func (t TriMeshData) Build(verts VertexList, tris TriVertexIndexList) { + delete(vertexListMap, int(t)) + delete(indexListMap, int(t)) C.dGeomTriMeshDataBuildSimple(t.c(), (*C.dReal)(&verts[0][0]), C.int(len(verts)), (*C.dTriIndex)(&tris[0]), C.int(len(tris))) + vertexListMap[int(t)] = verts // avoid GC mark and sweep + indexListMap[int(t)] = tris // avoid GC mark and sweep } // Preprocess preprocesses the triangle mesh data. From f91a45804b8175d38d61da7a4e25d7e73c4a7438 Mon Sep 17 00:00:00 2001 From: nobonobo Date: Fri, 13 May 2016 00:04:08 +0900 Subject: [PATCH 3/5] trimesh fix --- ode.go | 15 +++++++++++++-- space.go | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ode.go b/ode.go index 2d14cc7..18b32ef 100644 --- a/ode.go +++ b/ode.go @@ -195,11 +195,22 @@ func NewPlaneList(size int, vals ...float64) PlaneList { } // TriVertexIndexList represents a list of triangle vertex indices. -type TriVertexIndexList []uint32 +type TriVertexIndexList [][]uint32 // NewTriVertexIndexList returns a new TriVertexIndexList instance. func NewTriVertexIndexList(size int, indices ...uint32) TriVertexIndexList { - return indices[:size*3] + list := make(TriVertexIndexList, size) + elts := make([]uint32, 3*size) + for i := range list { + list[i], elts = elts[:3], elts[3:] + n := 3 + if len(indices) < 3 { + n = len(indices) + } + copy(list[i], indices[:n]) + indices = indices[n:] + } + return list } // PolygonList represents a list of polygon definitions diff --git a/space.go b/space.go index ce6ee4f..d537485 100644 --- a/space.go +++ b/space.go @@ -42,6 +42,7 @@ type Space interface { NewCapsule(radius, length float64) Capsule NewCylinder(radius, length float64) Cylinder NewRay(length float64) Ray + NewTriMesh(data TriMeshData) TriMesh NewHeightfield(data HeightfieldData, placeable bool) Heightfield NewSimpleSpace() SimpleSpace NewHashSpace() HashSpace From 26000a5e5acc8bb79471889076ff65b1404e1afc Mon Sep 17 00:00:00 2001 From: Irieda Noboru Date: Tue, 26 Apr 2016 18:40:38 +0900 Subject: [PATCH 4/5] cgocheck avoidance for nearCallbackData --- ode.go | 4 +++- space.go | 4 +++- utils.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 utils.go diff --git a/ode.go b/ode.go index 18b32ef..a4cd8a9 100644 --- a/ode.go +++ b/ode.go @@ -40,9 +40,11 @@ type nearCallbackData struct { fn NearCallback } +var nearCallbackDataMap = NewCGOMap() + //export nearCallback func nearCallback(data unsafe.Pointer, obj1, obj2 C.dGeomID) { - cbData := (*nearCallbackData)(data) + cbData := nearCallbackDataMap.Get(int(uintptr(data))).(*nearCallbackData) cbData.fn(cbData.data, cToGeom(obj1), cToGeom(obj2)) } diff --git a/space.go b/space.go index d537485..a8550c7 100644 --- a/space.go +++ b/space.go @@ -153,7 +153,9 @@ func (s SpaceBase) Geom(index int) Geom { // Collide tests for collision between contained objects. func (s SpaceBase) Collide(data interface{}, cb NearCallback) { cbData := &nearCallbackData{fn: cb, data: data} - C.dSpaceCollide(s.c(), unsafe.Pointer(cbData), + index := nearCallbackDataMap.Set(cbData) + defer nearCallbackDataMap.Delete(index) + C.dSpaceCollide(s.c(), unsafe.Pointer(uintptr(index)), (*C.dNearCallback)(C.callNearCallback)) } diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..b18a331 --- /dev/null +++ b/utils.go @@ -0,0 +1,30 @@ +package ode + +type CGOMap struct { + index int + pointers map[int]interface{} +} + +func NewCGOMap() CGOMap { + var m CGOMap + m.pointers = make(map[int]interface{}) + return m +} + +func (m CGOMap) Get(index int) interface{} { + p := m.pointers[index] + if p == nil { + panic("couldn't retrieve the pointer") + } + return p +} + +func (m CGOMap) Set(p interface{}) int { + m.index += 1 + m.pointers[m.index] = p + return m.index +} + +func (m CGOMap) Delete(index int) { + delete(m.pointers, index) +} From 5ea355b829b9d33ba32f7f1fd9a8974ed58b9c5d Mon Sep 17 00:00:00 2001 From: nobonobo Date: Fri, 13 May 2016 00:07:59 +0900 Subject: [PATCH 5/5] trimesh fix --- trimesh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trimesh.go b/trimesh.go index 64d0754..404dac6 100644 --- a/trimesh.go +++ b/trimesh.go @@ -47,7 +47,7 @@ func (t TriMeshData) Build(verts VertexList, tris TriVertexIndexList) { delete(vertexListMap, int(t)) delete(indexListMap, int(t)) C.dGeomTriMeshDataBuildSimple(t.c(), (*C.dReal)(&verts[0][0]), C.int(len(verts)), - (*C.dTriIndex)(&tris[0]), C.int(len(tris))) + (*C.dTriIndex)(&tris[0][0]), C.int(len(tris)*3)) vertexListMap[int(t)] = verts // avoid GC mark and sweep indexListMap[int(t)] = tris // avoid GC mark and sweep }