轉載自:http://www.cnblogs.com/21207-iHome/p/7098000.html
八叉樹(Octree)是一種用於描述三維空間的樹狀數據結構。想象一個立方體,我們最少可以切成多少個相同等分的小立方體?答案就是8個。再想象我們有一個房間,房間里某個角落藏着一枚金幣,我們想很快的把金幣找出來,怎么找最高效?我們可以把房間當成一個立方體,先切成八個小立方體,然后排除掉沒有放任何東西的小立方體,再把有可能藏金幣的小立方體繼續切八等份….如此下去,平均在Log8(房間內的所有物品數)的時間內就可找到金幣。因此,八叉樹就是用在3D空間中的場景管理,可以很快地知道物體在3D場景中的位置,或偵測與其它物體是否有碰撞以及是否在可視范圍內。
VREP軟件中可以在場景里創建八叉樹(Add→Octree),通常用於簡化表達復雜的形體或點雲。An octree is an object that represents a spacial partitioning. It is made up by a tree data structure in which each node has exactly eight children. Occupied leaf nodes are represented as voxels. Octrees can be used to offer a simplified representation for shapes or point clouds, or can act as an occupancy grid/space:
Octrees are collidable, measurable and detectable objects. This means that octrees:
- can be used in collision detections with other collidable objects.
- can be used in minimum distance calculations with other measurable objects.
- can be detected by proximity sensors.
函數simInsertVoxelsIntoOctree可以向八叉樹中插入Voxels (三維像素),它是一種基於體積概念的像素,通常的普通像素只需要X、Y軸兩個坐標來定位它在空間中的方位,而它還需要加進一個額外的Z軸坐標,相當於空間中一個非常小的立方體。
simInsertVoxelsIntoOctree(number octreeHandle,number options,table points,table color=nil,table tag=nil)
下面代碼通過Octree創建了一個簡單的圍牆:
if (sim_call_type==sim_childscriptcall_initialization) then octree=simGetObjectAssociatedWithScript(sim_handle_self) local p = {-1, 1, 0.05} for i=0,20,1 do color = {255*math.random(),255*math.random(),255*math.random()} simInsertVoxelsIntoOctree(octree, 0, {p[1],p[2]-2*i/20,p[3]}, color, nil) simInsertVoxelsIntoOctree(octree, 0, {p[1]+2*i/20,p[2],p[3]}, color, nil) simInsertVoxelsIntoOctree(octree, 0, {p[1]+2*i/20,p[2]-2,p[3]}, color, nil) simInsertVoxelsIntoOctree(octree, 0, {p[1]+2,p[2]-2*i/20,p[3]}, color, nil) end end if (sim_call_type==sim_childscriptcall_cleanup) then -- Put some restoration code here simRemoveVoxelsFromOctree(octree, 0, nil) end
其中兩輪差速的機器人BubbleRob通過接近傳感器來進行簡單的避障行走。感知階段調用simReadProximitySensor來獲取接近傳感器信息,如果檢測到障礙物result返回1,沒有檢測到障礙物返回0,出錯返回-1。同時計算傳感器到障礙物的最小距離,結果保存在distance中。注意探測只會在探測區域(Detection Volume)內進行,所以注意設置傳感器的屬性(探測體形狀、探測距離、角度等)。
number result,number distance = simReadProximitySensor(number sensorHandle)
參考: