library_geometries.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package collada
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "strconv"
  10. )
  11. //
  12. // LibraryGeometries
  13. //
  14. type LibraryGeometries struct {
  15. Asset *Asset
  16. Geometry []*Geometry
  17. }
  18. // Dump prints out information about the LibraryGeometries
  19. func (lg *LibraryGeometries) Dump(out io.Writer, indent int) {
  20. if lg == nil {
  21. return
  22. }
  23. fmt.Fprintf(out, "%sLibraryGeometries:\n", sIndent(indent))
  24. ind := indent + step
  25. if lg.Asset != nil {
  26. lg.Asset.Dump(out, ind)
  27. }
  28. for _, g := range lg.Geometry {
  29. g.Dump(out, ind)
  30. }
  31. }
  32. //
  33. // Geometry
  34. //
  35. type Geometry struct {
  36. Id string // Geometry id (optional)
  37. Name string // Geometry name (optional)
  38. GeometricElement interface{} // Geometry type object (Mesh|others)
  39. }
  40. // Dump prints out information about the Geometry
  41. func (g *Geometry) Dump(out io.Writer, indent int) {
  42. fmt.Fprintf(out, "%sGeometry id:%s name:%s\n", sIndent(indent), g.Id, g.Name)
  43. ind := indent + step
  44. switch gt := g.GeometricElement.(type) {
  45. case *Mesh:
  46. gt.Dump(out, ind)
  47. break
  48. }
  49. }
  50. //
  51. // Mesh
  52. //
  53. type Mesh struct {
  54. Source []*Source // One or more sources Sources
  55. Vertices Vertices // Vertices positions
  56. PrimitiveElements []interface{} // Geometry primitives (polylist|others)
  57. }
  58. // Dump prints out information about the Mesh
  59. func (m *Mesh) Dump(out io.Writer, indent int) {
  60. fmt.Fprintf(out, "%sMesh:\n", sIndent(indent))
  61. ind := indent + step
  62. for _, s := range m.Source {
  63. s.Dump(out, ind)
  64. }
  65. m.Vertices.Dump(out, ind)
  66. for _, pe := range m.PrimitiveElements {
  67. switch pt := pe.(type) {
  68. case *Lines:
  69. pt.Dump(out, ind)
  70. case *Polylist:
  71. pt.Dump(out, ind)
  72. }
  73. }
  74. }
  75. //
  76. // Vertices
  77. //
  78. type Vertices struct {
  79. Id string
  80. Name string
  81. Input []Input
  82. }
  83. // Dump prints out information about the Vertices
  84. func (v *Vertices) Dump(out io.Writer, indent int) {
  85. fmt.Fprintf(out, "%sVertices id:%s name:%s\n", sIndent(indent), v.Id, v.Name)
  86. for _, inp := range v.Input {
  87. inp.Dump(out, indent+step)
  88. }
  89. }
  90. //
  91. // Input
  92. //
  93. type Input struct {
  94. Semantic string
  95. Source string // source URL
  96. }
  97. // Dump prints out information about the Input
  98. func (i *Input) Dump(out io.Writer, indent int) {
  99. fmt.Fprintf(out, "%sInput semantic:%s source:%s\n", sIndent(indent), i.Semantic, i.Source)
  100. }
  101. //
  102. // Polylist
  103. //
  104. type Polylist struct {
  105. Name string
  106. Count int
  107. Material string
  108. Input []InputShared
  109. Vcount []int
  110. P []int
  111. }
  112. // Dump prints out information about the Polylist
  113. func (pl *Polylist) Dump(out io.Writer, indent int) {
  114. fmt.Fprintf(out, "%sPolylist name:%s count:%d material:%s\n", sIndent(indent), pl.Name, pl.Count, pl.Material)
  115. ind := indent + step
  116. for _, is := range pl.Input {
  117. is.Dump(out, ind)
  118. }
  119. fmt.Fprintf(out, "%sVcount(%d):%v\n", sIndent(ind), len(pl.Vcount), intsToString(pl.Vcount, 20))
  120. fmt.Fprintf(out, "%sP(%d):%v\n", sIndent(ind), len(pl.P), intsToString(pl.P, 20))
  121. }
  122. //
  123. // InputShared
  124. //
  125. type InputShared struct {
  126. Offset int
  127. Semantic string
  128. Source string // source URL
  129. Set int
  130. }
  131. //
  132. // Triangles
  133. //
  134. type Triangles struct {
  135. Name string
  136. Count int
  137. Material string
  138. Input []InputShared
  139. P []int
  140. }
  141. //
  142. // Lines
  143. //
  144. type Lines struct {
  145. Name string
  146. Count int
  147. Material string
  148. Input []InputShared
  149. P []int
  150. }
  151. // Dump prints out information about the Lines
  152. func (ln *Lines) Dump(out io.Writer, indent int) {
  153. fmt.Fprintf(out, "%sLines name:%s count:%d material:%s\n", sIndent(indent), ln.Name, ln.Count, ln.Material)
  154. ind := indent + step
  155. for _, is := range ln.Input {
  156. is.Dump(out, ind)
  157. }
  158. fmt.Fprintf(out, "%sP(%d):%v\n", sIndent(ind), len(ln.P), intsToString(ln.P, 20))
  159. }
  160. //
  161. // LineStrips
  162. //
  163. type LineStrips struct {
  164. Name string
  165. Count int
  166. Material string
  167. Input []InputShared
  168. P []int
  169. }
  170. //
  171. // Trifans
  172. //
  173. type Trifans struct {
  174. Name string
  175. Count int
  176. Material string
  177. Input []InputShared
  178. P []int
  179. }
  180. //
  181. // Tristrips
  182. //
  183. type Tristrips struct {
  184. Name string
  185. Count int
  186. Material string
  187. Input []InputShared
  188. P []int
  189. }
  190. // Dump prints out information about the Tristrips
  191. func (is *InputShared) Dump(out io.Writer, indent int) {
  192. fmt.Fprintf(out, "%sInputShared offset:%d semantic:%s source:%s set:%d\n",
  193. sIndent(indent), is.Offset, is.Semantic, is.Source, is.Set)
  194. }
  195. // Decodes "library_geometry" children
  196. func (d *Decoder) decLibraryGeometries(start xml.StartElement, dom *Collada) error {
  197. lg := new(LibraryGeometries)
  198. dom.LibraryGeometries = lg
  199. for {
  200. // Get next child START
  201. child, _, err := d.decNextChild(start)
  202. if err != nil || child.Name.Local == "" {
  203. return err
  204. }
  205. // Decode optional asset
  206. if child.Name.Local == "asset" {
  207. lg.Asset = new(Asset)
  208. err = d.decAsset(child, lg.Asset)
  209. if err != nil {
  210. return err
  211. }
  212. continue
  213. }
  214. //log.Debug("decLibraryGeometries: %s", child.Name.Local)
  215. // Decode geometry
  216. if child.Name.Local == "geometry" {
  217. err = d.decGeometry(child, lg)
  218. if err != nil {
  219. return err
  220. }
  221. continue
  222. }
  223. }
  224. }
  225. // decGeometry receives the start element of a geometry and
  226. // decodes all its children and appends the decoded geometry
  227. // to the specified slice.
  228. func (d *Decoder) decGeometry(start xml.StartElement, lg *LibraryGeometries) error {
  229. // Get geometry id and name attributes
  230. geom := &Geometry{}
  231. geom.Id = findAttrib(start, "id").Value
  232. geom.Name = findAttrib(start, "name").Value
  233. lg.Geometry = append(lg.Geometry, geom)
  234. // Decodes geometry children
  235. for {
  236. // Get next child
  237. child, _, err := d.decNextChild(start)
  238. if err != nil || child.Name.Local == "" {
  239. return err
  240. }
  241. // Decode mesh
  242. if child.Name.Local == "mesh" {
  243. err = d.decMesh(child, geom)
  244. if err != nil {
  245. return err
  246. }
  247. continue
  248. }
  249. }
  250. }
  251. // decMesh decodes the mesh from the specified geometry
  252. func (d *Decoder) decMesh(start xml.StartElement, geom *Geometry) error {
  253. // Associates this mesh to the parent geometry
  254. mesh := &Mesh{}
  255. geom.GeometricElement = mesh
  256. // Decodes mesh children
  257. for {
  258. // Get next child
  259. child, _, err := d.decNextChild(start)
  260. if err != nil || child.Name.Local == "" {
  261. return err
  262. }
  263. //log.Debug("decMesh(%s): %s", start.Name.Local, child.Name.Local)
  264. // Decodes source
  265. if child.Name.Local == "source" {
  266. source, err := d.decSource(child)
  267. if err != nil {
  268. return err
  269. }
  270. mesh.Source = append(mesh.Source, source)
  271. continue
  272. }
  273. // Decodes vertices
  274. if child.Name.Local == "vertices" {
  275. err = d.decVertices(child, mesh)
  276. if err != nil {
  277. return err
  278. }
  279. continue
  280. }
  281. // Decodes lines
  282. if child.Name.Local == "lines" {
  283. err = d.decLines(child, mesh)
  284. if err != nil {
  285. return err
  286. }
  287. continue
  288. }
  289. // Decodes polylist
  290. if child.Name.Local == "polylist" || child.Name.Local == "triangles" {
  291. err = d.decPolylist(child, mesh)
  292. if err != nil {
  293. return err
  294. }
  295. continue
  296. }
  297. }
  298. }
  299. func (d *Decoder) decVertices(start xml.StartElement, mesh *Mesh) error {
  300. mesh.Vertices.Id = findAttrib(start, "id").Value
  301. mesh.Vertices.Name = findAttrib(start, "name").Value
  302. for {
  303. // Get next child
  304. child, _, err := d.decNextChild(start)
  305. if err != nil || child.Name.Local == "" {
  306. return err
  307. }
  308. // input
  309. if child.Name.Local == "input" {
  310. inp, err := d.decInput(child)
  311. if err != nil {
  312. return err
  313. }
  314. mesh.Vertices.Input = append(mesh.Vertices.Input, inp)
  315. }
  316. }
  317. }
  318. func (d *Decoder) decInput(start xml.StartElement) (Input, error) {
  319. var inp Input
  320. inp.Semantic = findAttrib(start, "semantic").Value
  321. inp.Source = findAttrib(start, "source").Value
  322. return inp, nil
  323. }
  324. func (d *Decoder) decLines(start xml.StartElement, mesh *Mesh) error {
  325. ln := &Lines{}
  326. ln.Name = findAttrib(start, "name").Value
  327. ln.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  328. ln.Material = findAttrib(start, "material").Value
  329. mesh.PrimitiveElements = append(mesh.PrimitiveElements, ln)
  330. for {
  331. // Get next child
  332. child, data, err := d.decNextChild(start)
  333. if err != nil || child.Name.Local == "" {
  334. return err
  335. }
  336. // Decode input shared
  337. if child.Name.Local == "input" {
  338. inp, err := d.decInputShared(child)
  339. if err != nil {
  340. return err
  341. }
  342. ln.Input = append(ln.Input, inp)
  343. continue
  344. }
  345. // Decode p (primitive)
  346. if child.Name.Local == "p" {
  347. p, err := d.decPrimitive(child, data)
  348. if err != nil {
  349. return err
  350. }
  351. ln.P = p
  352. }
  353. }
  354. }
  355. func (d *Decoder) decPolylist(start xml.StartElement, mesh *Mesh) error {
  356. pl := &Polylist{}
  357. pl.Name = findAttrib(start, "name").Value
  358. pl.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  359. pl.Material = findAttrib(start, "material").Value
  360. mesh.PrimitiveElements = append(mesh.PrimitiveElements, pl)
  361. // blender exporter now (since v2.79) exports meshes as <Triangles> when all contained polygons are tris
  362. // https://developer.blender.org/rBc9b95c28f64e9d7421b00cbf8ed4ecddd6471ae5
  363. if start.Name.Local == "triangles" {
  364. pl.Vcount = make([]int, pl.Count)
  365. for i := range pl.Vcount {
  366. pl.Vcount[i] = 3
  367. }
  368. }
  369. for {
  370. // Get next child
  371. child, data, err := d.decNextChild(start)
  372. if err != nil || child.Name.Local == "" {
  373. return err
  374. }
  375. // Decode input shared
  376. if child.Name.Local == "input" {
  377. inp, err := d.decInputShared(child)
  378. if err != nil {
  379. return err
  380. }
  381. pl.Input = append(pl.Input, inp)
  382. continue
  383. }
  384. // Decode vcount
  385. if child.Name.Local == "vcount" {
  386. vc, err := d.decVcount(child, data, pl.Count)
  387. if err != nil {
  388. return err
  389. }
  390. pl.Vcount = vc
  391. continue
  392. }
  393. // Decode p (primitive)
  394. if child.Name.Local == "p" {
  395. p, err := d.decPrimitive(child, data)
  396. if err != nil {
  397. return err
  398. }
  399. pl.P = p
  400. }
  401. }
  402. }
  403. func (d *Decoder) decInputShared(start xml.StartElement) (InputShared, error) {
  404. var inp InputShared
  405. inp.Offset, _ = strconv.Atoi(findAttrib(start, "offset").Value)
  406. inp.Semantic = findAttrib(start, "semantic").Value
  407. inp.Source = findAttrib(start, "source").Value
  408. inp.Set, _ = strconv.Atoi(findAttrib(start, "set").Value)
  409. return inp, nil
  410. }
  411. func (d *Decoder) decVcount(start xml.StartElement, data []byte, size int) ([]int, error) {
  412. vcount := make([]int, size)
  413. var br bytesReader
  414. br.Init(data)
  415. idx := 0
  416. for {
  417. tok := br.TokenNext()
  418. if tok == nil {
  419. break
  420. }
  421. v, err := strconv.Atoi(string(tok))
  422. if err != nil {
  423. return nil, err
  424. }
  425. vcount[idx] = v
  426. idx++
  427. }
  428. return vcount, nil
  429. }
  430. func (d *Decoder) decPrimitive(start xml.StartElement, data []byte) ([]int, error) {
  431. p := make([]int, 0)
  432. var br bytesReader
  433. br.Init(data)
  434. idx := 0
  435. for {
  436. tok := br.TokenNext()
  437. if tok == nil {
  438. break
  439. }
  440. v, err := strconv.Atoi(string(tok))
  441. if err != nil {
  442. return nil, err
  443. }
  444. p = append(p, v)
  445. idx++
  446. }
  447. return p, nil
  448. }