library_geometries.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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" {
  291. err = d.decPolylist(child, mesh)
  292. if err != nil {
  293. return err
  294. }
  295. continue
  296. }
  297. // Decodes triangles
  298. if child.Name.Local == "triangles" {
  299. err = d.decTriangles(child, mesh)
  300. if err != nil {
  301. return err
  302. }
  303. continue
  304. }
  305. }
  306. }
  307. func (d *Decoder) decVertices(start xml.StartElement, mesh *Mesh) error {
  308. mesh.Vertices.Id = findAttrib(start, "id").Value
  309. mesh.Vertices.Name = findAttrib(start, "name").Value
  310. for {
  311. // Get next child
  312. child, _, err := d.decNextChild(start)
  313. if err != nil || child.Name.Local == "" {
  314. return err
  315. }
  316. // input
  317. if child.Name.Local == "input" {
  318. inp, err := d.decInput(child)
  319. if err != nil {
  320. return err
  321. }
  322. mesh.Vertices.Input = append(mesh.Vertices.Input, inp)
  323. }
  324. }
  325. }
  326. func (d *Decoder) decInput(start xml.StartElement) (Input, error) {
  327. var inp Input
  328. inp.Semantic = findAttrib(start, "semantic").Value
  329. inp.Source = findAttrib(start, "source").Value
  330. return inp, nil
  331. }
  332. func (d *Decoder) decLines(start xml.StartElement, mesh *Mesh) error {
  333. ln := &Lines{}
  334. ln.Name = findAttrib(start, "name").Value
  335. ln.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  336. ln.Material = findAttrib(start, "material").Value
  337. mesh.PrimitiveElements = append(mesh.PrimitiveElements, ln)
  338. for {
  339. // Get next child
  340. child, data, err := d.decNextChild(start)
  341. if err != nil || child.Name.Local == "" {
  342. return err
  343. }
  344. // Decode input shared
  345. if child.Name.Local == "input" {
  346. inp, err := d.decInputShared(child)
  347. if err != nil {
  348. return err
  349. }
  350. ln.Input = append(ln.Input, inp)
  351. continue
  352. }
  353. // Decode p (primitive)
  354. if child.Name.Local == "p" {
  355. p, err := d.decPrimitive(child, data)
  356. if err != nil {
  357. return err
  358. }
  359. ln.P = p
  360. }
  361. }
  362. }
  363. func (d *Decoder) decPolylist(start xml.StartElement, mesh *Mesh) error {
  364. pl := &Polylist{}
  365. pl.Name = findAttrib(start, "name").Value
  366. pl.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  367. pl.Material = findAttrib(start, "material").Value
  368. mesh.PrimitiveElements = append(mesh.PrimitiveElements, pl)
  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) decTriangles(start xml.StartElement, mesh *Mesh) error {
  404. tr := &Triangles{}
  405. tr.Name = findAttrib(start, "name").Value
  406. tr.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  407. tr.Material = findAttrib(start, "material").Value
  408. mesh.PrimitiveElements = append(mesh.PrimitiveElements, tr)
  409. for {
  410. // Get next child
  411. child, data, err := d.decNextChild(start)
  412. if err != nil || child.Name.Local == "" {
  413. return err
  414. }
  415. // Decode input shared
  416. if child.Name.Local == "input" {
  417. inp, err := d.decInputShared(child)
  418. if err != nil {
  419. return err
  420. }
  421. tr.Input = append(tr.Input, inp)
  422. continue
  423. }
  424. // Decode p (primitive)
  425. if child.Name.Local == "p" {
  426. p, err := d.decPrimitive(child, data)
  427. if err != nil {
  428. return err
  429. }
  430. tr.P = p
  431. }
  432. }
  433. }
  434. func (d *Decoder) decInputShared(start xml.StartElement) (InputShared, error) {
  435. var inp InputShared
  436. inp.Offset, _ = strconv.Atoi(findAttrib(start, "offset").Value)
  437. inp.Semantic = findAttrib(start, "semantic").Value
  438. inp.Source = findAttrib(start, "source").Value
  439. inp.Set, _ = strconv.Atoi(findAttrib(start, "set").Value)
  440. return inp, nil
  441. }
  442. func (d *Decoder) decVcount(start xml.StartElement, data []byte, size int) ([]int, error) {
  443. vcount := make([]int, size)
  444. var br bytesReader
  445. br.Init(data)
  446. idx := 0
  447. for {
  448. tok := br.TokenNext()
  449. if tok == nil {
  450. break
  451. }
  452. v, err := strconv.Atoi(string(tok))
  453. if err != nil {
  454. return nil, err
  455. }
  456. vcount[idx] = v
  457. idx++
  458. }
  459. return vcount, nil
  460. }
  461. func (d *Decoder) decPrimitive(start xml.StartElement, data []byte) ([]int, error) {
  462. p := make([]int, 0)
  463. var br bytesReader
  464. br.Init(data)
  465. idx := 0
  466. for {
  467. tok := br.TokenNext()
  468. if tok == nil {
  469. break
  470. }
  471. v, err := strconv.Atoi(string(tok))
  472. if err != nil {
  473. return nil, err
  474. }
  475. p = append(p, v)
  476. idx++
  477. }
  478. return p, nil
  479. }