八叉樹(Octree)
轉自: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建立了一個簡單的圍牆:
View Code
其中兩輪差速的機器人BubbleRob通過接近感測器來進行簡單的避障行走。感知階段呼叫simReadProximitySensor來獲取接近感測器資訊,如果檢測到障礙物result返回1,沒有檢測到障礙物返回0,出錯返回-1。同時計算感測器到障礙物的最小距離,結果儲存在distance中。注意探測只會在探測區域(Detection Volume)內進行,所以注意設定感測器的屬性(探測體形狀、探測距離、角度等)。
number result,number distance = simReadProximitySensor(number sensorHandle)
參考: