天天看點

PLY檔案格式 - Vulkan

PLY檔案格式

一、PLY簡介

        PLY檔案格式是Stanford大學開發的一套三維mesh模型資料格式,圖形學領域内很多著名的模型資料,比如Stanford的三維掃描資料庫(其中包括很多文章中會見到的Happy Buddha, Dragon, Bunny兔子),Geogia Tech的大型幾何模型庫,北卡(UNC)的電廠模型等,最初的模型都是基于這個格式的。

        PLY多邊形檔案格式的開發目标是建立一套針對多邊形模型的,結構簡單但是能夠滿足大多數圖形應用需要的模型格式,而且它允許以ASCII碼格式或二進制形式存儲檔案。PLY的開發者希望,這樣一套既簡單又靈活的檔案格式,能夠幫助開發人員避免重複開發檔案格式的問題。然而由于各種各樣的原因,在工業領域内,新的檔案格式仍然在不斷的出現,但是在圖形學的研究領域中,PLY還是種常用且重要的檔案格式。

        PLY作為一種多邊形模型資料格式,不同于三維引擎中常用的場景圖檔案格式和腳本檔案,每個PLY檔案隻用于描述一個多邊形模型對象(Object),該模型對象可以通過諸如頂點、面等資料進行描述,每一類這樣的資料被稱作一種元素(Element)。相比于現代的三維引擎中所用到的各種複雜格式,PLY實在是種簡單的不能再簡單的檔案格式,但是如果仔細研究就會發現,就像設計者所說的,這對于絕大多數的圖形應用來說已經是足夠用了。 

二、PLY結構

        PLY的檔案結構簡單:檔案頭加上元素資料清單。其中檔案頭中以行為機關描述檔案類型、格式與版本、元素類型、元素的屬性等,然後就根據在檔案頭中所列出元素類型的順序及其屬性,依次記錄各個元素的屬性資料。

        典型的PLY檔案結構: 

頭部 
        頂點清單 
        面片清單 
      (其他元素清單)      

         頭部是一系列以回車結尾的文本行,用來描述檔案的剩餘部分。頭部包含一個對每個元素類型的描述,包括元素名(如“邊”),這個元素在工程裡有多少,以及一個與這個元素關聯的不同屬性的清單。頭部還說明這個檔案是二進制的或者是ASCII的。頭部後面的是一個每個元素類型的元素清單,按照在頭部中描述的順序出現。

        下面是一個立方體的完整ASCII描述。大括号中的注釋不是檔案的一部分,它們是這個例子的注解。檔案中的注釋一般在

  “comment”開始的關鍵詞定義行裡。

[plain] view

plaincopyprint?

  1. <span style="font-size:16px;">ply   
  2. format   ascii   1.0   {   ascii/二進制,格式版本數   }   
  3. comment   made   by   anonymous   {   注釋關鍵詞說明,像其他行一樣   }   
  4. comment   this   file   is   a   cube   
  5. element   vertex   8   {   定義“vertex”(頂點)元素,在檔案中有8個   }   
  6. property   float32   x   {   頂點包含浮點坐标“x”}   
  7. property   float32   y   {   y   坐标同樣是一個頂點屬性   }   
  8. property   float32   z   {   z   也是坐标   }   
  9. element   face   6   {   在檔案裡有6個“face”(面片)   }   
  10. property   list   uint8   int32   vertex_index   {   “vertex_indices”(頂點素引)是一列整數   }   
  11. end_header   {   劃定頭部結尾   }   
  12. 0   0   0   {   頂點清單的開始   }   
  13. 0   0   1   
  14. 0   1   1   
  15. 0   1   0   
  16. 1   0   0   
  17. 1   0   1   
  18. 1   1   1   
  19. 1   1   0   
  20. 4   0   1   2   3   {   面片清單開始   }   
  21. 4   7   6   5   4   
  22. 4   0   4   5   1   
  23. 4   1   5   6   2   
  24. 4   2   6   7   3   
  25. 4   3   7   4   0 </span>  

        這個例子說明頭部的基本組成。頭部的每個部分都是一個以關鍵詞開頭,以回車結尾的ASCII串。"ply"是檔案的頭四個字元。

        跟在檔案頭部開頭之後的,是關鍵詞“format”和一個特定的ASCII或者二進制的格式,接下來是一個版本号。

        再下面是多邊形檔案中每個元素的描述,在每個元素裡還有多屬性的說明。一般元素以下面的格式描述:

        element   <元素名>   <在檔案中的個數> 

        property   <資料類型>   <屬性名-1> 

        property   <資料類型>   <屬性名-2> 

        property   <資料類型>   <屬性名-3>

        屬性羅列在“element”(元素)行後面定義,既包含屬性的資料類型,也包含屬性在每個元素中出現的次序。一個屬性可以有三種資料類型:标量,字元串和清單。屬性可能具有的标量資料類型清單如下:

        名稱      類型           位元組數 

         ------------------------------- 

         int8        字元                    1 

         uint8      非負字元           1 

         int16      短整型               2 

         uint16    非負短整型       2 

         int32      整型                   4 

         uint32    非負整型           4 

         float32   單精度浮點數   4 

         float64   雙精度浮點數   8

         這些位元組計數很重要,而且在實作過程中不能修改以使這些檔案可移植。

         使用清單資料類型的屬性定義有一種特殊的格式:property   list   <數值類型>   <數值類型>   <屬性名> ,這種格式,一個非負字元表示在屬性裡包含多少索引,接下來是一個清單包含許多整數。在這個邊長清單裡的每個整數都是一個頂點的索引。 

        另外一個立方體定義:

[plain] view

plaincopyprint?

  1. ply   
  2. format   ascii   1.0   
  3. comment   author:   anonymous   
  4. comment   object:   another   cube   
  5. element   vertex   8   
  6. property   float32   x   
  7. property   float32   y   
  8. property   float32   z   
  9. property   red   uint8   {   頂點顔色開始   }   
  10. property   green   uint8   
  11. property   blue   uint8   
  12. element   face   7   
  13. property   list   uint8   int32   vertex_index   {   每個面片的頂點個數   }   
  14. element   edge   5   {   物體裡有5條邊   }   
  15. property   int32   vertex1   {   邊的第一個頂點的索引   }   
  16. property   int32   vertex2   {   第二個頂點的索引   }   
  17. property   uint8   red   {   邊顔色開始   }   
  18. property   uint8   green   
  19. property   uint8   blue   
  20. end_header   
  21. 0   0   0   255   0   0   {   頂點清單開始   }   
  22. 0   0   1   255   0   0   
  23. 0   1   1   255   0   0   
  24. 0   1   0   255   0   0   
  25. 1   0   0   0   0   255   
  26. 1   0   1   0   0   255   
  27. 1   1   1   0   0   255   
  28. 1   1   0   0   0   255   
  29. 3   0   1   2   {   面片清單開始,從一個三角形開始   }   
  30. 3   0   2   3   {   另一個三角形   }   
  31. 4   7   6   5   4   {   現在是一些四邊形   }   
  32. 4   0   4   5   1   
  33. 4   1   5   6   2   
  34. 4   2   6   7   3   
  35. 4   3   7   4   0   
  36. 0   1   255   255   255   {   邊清單開始,從白邊開始   }   
  37. 1   2   255   255   255   
  38. 2   3   255   255   255   
  39. 3   0   255   255   255   
  40. 2   0   0   0   0   {   以一個黑線結束   }   

         這個檔案為每個頂點指定一個紅、綠、藍值。

       為了說明變長vertex_index(頂點索引)的能力,物體的頭兩個面片是兩個三角形而不是一個四邊形。這意味着物體的面片數是7。這個物體還包括一個邊清單。每條邊包括兩個指向說明邊的頂點的指針。每條邊也有一種顔色。上面定義的五條邊指定了顔色,使檔案裡的兩個三角形高亮。前四條邊白色,它們包圍兩個三角形。最後一條邊是黑的,他是分割三角形的邊。

三、使用者定義元素

        上面的例子顯示了頂點、面片和邊三種元素的用法。PLY   格式同樣允許使用者定義它們自己的元素。定義新元素的格式于頂點、面片和邊相同。這是頭部定義材料屬性的部分:

[plain] view

plaincopyprint?

  1. element   material   6   
  2. property   ambient_red   uint8   {   環繞顔色   }   
  3. property   ambient_green   uint8   
  4. property   ambient_blue   uint8   
  5. property   ambient_coeff   float32   
  6. property   diffuse_red   uint8   {   擴散(diffuse)顔色   }   
  7. property   diffuse_green   uint8   
  8. property   diffuse_blue   uint8   
  9. property   diffuse_coeff   float32   
  10. property   specular_red   uint8   {   鏡面(specular)顔色   }   
  11. property   specular_green   uint8   
  12. property   specular_blue   uint8   
  13. property   specular_coeff   float32   
  14. property   specular_power   float32   {   Phong   指數   }   

        這些行應該在頭部頂點、面片和邊的說明後直接出現。如果我們希望每個頂點有一個材質說明,我們可以将這行加在頂點屬性末尾:property   material_index   int32

       這個整數現在是一個到檔案内包含的材質清單的索引。這可能誘使一個新應用的作者編制一些信的元素儲存在PLY檔案中。

轉載自:http://blog.csdn.net/lxfyzx/article/details/4997627

                http://blog.csdn.net/lxfyzx/article/details/4997780

以下是常用的elements 和 properties 原位址 http://www.mathworks.com/matlabcentral/fx_files/5459/1/content/ply.htm

Common Elements and Properties

While the PLY format has the flexibility to define many types of elements and properities, a common set of elements are understood between programs to communicate common 3D data types. Turk suggests elements and

property names that programs should try to make standard.

Element Property Data Type Property Description
vertex

x

y

z

float

float

float

x,y,z coordinates

nx

ny

nz

float

float

float

x,y,z components of normal

red

green

blue

alpha

uchar

uchar

uchar

uchar

vertex color

amount of transparency

material_index int index to list of materials
face vertex_indices list of int indices to vertices

back_red

back_green

back_blue

uchar

uchar

uchar

backside color
edge

vertex1

vertex2

int

int

index to vertex

index to other vertex

crease_tag uchar crease in subdivision surface
material

red

green

blue

alpha

uchar

uchar

uchar

uchar

material color

amount of transparency

reflect_coeff

refract_coeff

refract_index

extinct_coeff

float

float

float

float

amount of light reflected

amount of light transmitted

index of refraction

extinction coefficient

* - required "core" properties in red

For most applications, the minimum necessary information is vertex and face data. To make it easier for programs to interpret PLY files, the element properties listed in red should always be included. If there is

no face data (as in the case of point-cloud data) the face element could be defined with an element count of zero. The other elements and properties are suggested names for often used information like material parameters and edge information.

PLY檔案格式 - Vulkan