library_geometries.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. return nil
  225. }
  226. // decGeometry receives the start element of a geometry and
  227. // decodes all its children and appends the decoded geometry
  228. // to the specified slice.
  229. func (d *Decoder) decGeometry(start xml.StartElement, lg *LibraryGeometries) error {
  230. // Get geometry id and name attributes
  231. geom := &Geometry{}
  232. geom.Id = findAttrib(start, "id").Value
  233. geom.Name = findAttrib(start, "name").Value
  234. lg.Geometry = append(lg.Geometry, geom)
  235. // Decodes geometry children
  236. for {
  237. // Get next child
  238. child, _, err := d.decNextChild(start)
  239. if err != nil || child.Name.Local == "" {
  240. return err
  241. }
  242. // Decode mesh
  243. if child.Name.Local == "mesh" {
  244. err = d.decMesh(child, geom)
  245. if err != nil {
  246. return err
  247. }
  248. continue
  249. }
  250. }
  251. return nil
  252. }
  253. // decMesh decodes the mesh from the specified geometry
  254. func (d *Decoder) decMesh(start xml.StartElement, geom *Geometry) error {
  255. // Associates this mesh to the parent geometry
  256. mesh := &Mesh{}
  257. geom.GeometricElement = mesh
  258. // Decodes mesh children
  259. for {
  260. // Get next child
  261. child, _, err := d.decNextChild(start)
  262. if err != nil || child.Name.Local == "" {
  263. return err
  264. }
  265. //log.Debug("decMesh(%s): %s", start.Name.Local, child.Name.Local)
  266. // Decodes source
  267. if child.Name.Local == "source" {
  268. source, err := d.decSource(child)
  269. if err != nil {
  270. return err
  271. }
  272. mesh.Source = append(mesh.Source, source)
  273. continue
  274. }
  275. // Decodes vertices
  276. if child.Name.Local == "vertices" {
  277. err = d.decVertices(child, mesh)
  278. if err != nil {
  279. return err
  280. }
  281. continue
  282. }
  283. // Decodes lines
  284. if child.Name.Local == "lines" {
  285. err = d.decLines(child, mesh)
  286. if err != nil {
  287. return err
  288. }
  289. continue
  290. }
  291. // Decodes polylist
  292. if child.Name.Local == "polylist" {
  293. err = d.decPolylist(child, mesh)
  294. if err != nil {
  295. return err
  296. }
  297. continue
  298. }
  299. }
  300. return nil
  301. }
  302. func (d *Decoder) decVertices(start xml.StartElement, mesh *Mesh) error {
  303. mesh.Vertices.Id = findAttrib(start, "id").Value
  304. mesh.Vertices.Name = findAttrib(start, "name").Value
  305. for {
  306. // Get next child
  307. child, _, err := d.decNextChild(start)
  308. if err != nil || child.Name.Local == "" {
  309. return err
  310. }
  311. // input
  312. if child.Name.Local == "input" {
  313. inp, err := d.decInput(child)
  314. if err != nil {
  315. return err
  316. }
  317. mesh.Vertices.Input = append(mesh.Vertices.Input, inp)
  318. }
  319. }
  320. return nil
  321. }
  322. func (d *Decoder) decInput(start xml.StartElement) (Input, error) {
  323. var inp Input
  324. inp.Semantic = findAttrib(start, "semantic").Value
  325. inp.Source = findAttrib(start, "source").Value
  326. return inp, nil
  327. }
  328. func (d *Decoder) decLines(start xml.StartElement, mesh *Mesh) error {
  329. ln := &Lines{}
  330. ln.Name = findAttrib(start, "name").Value
  331. ln.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  332. ln.Material = findAttrib(start, "material").Value
  333. mesh.PrimitiveElements = append(mesh.PrimitiveElements, ln)
  334. for {
  335. // Get next child
  336. child, data, err := d.decNextChild(start)
  337. if err != nil || child.Name.Local == "" {
  338. return err
  339. }
  340. // Decode input shared
  341. if child.Name.Local == "input" {
  342. inp, err := d.decInputShared(child)
  343. if err != nil {
  344. return err
  345. }
  346. ln.Input = append(ln.Input, inp)
  347. continue
  348. }
  349. // Decode p (primitive)
  350. if child.Name.Local == "p" {
  351. p, err := d.decPrimitive(child, data)
  352. if err != nil {
  353. return err
  354. }
  355. ln.P = p
  356. }
  357. }
  358. return nil
  359. }
  360. func (d *Decoder) decPolylist(start xml.StartElement, mesh *Mesh) error {
  361. pl := &Polylist{}
  362. pl.Name = findAttrib(start, "name").Value
  363. pl.Count, _ = strconv.Atoi(findAttrib(start, "count").Value)
  364. pl.Material = findAttrib(start, "material").Value
  365. mesh.PrimitiveElements = append(mesh.PrimitiveElements, pl)
  366. for {
  367. // Get next child
  368. child, data, err := d.decNextChild(start)
  369. if err != nil || child.Name.Local == "" {
  370. return err
  371. }
  372. // Decode input shared
  373. if child.Name.Local == "input" {
  374. inp, err := d.decInputShared(child)
  375. if err != nil {
  376. return err
  377. }
  378. pl.Input = append(pl.Input, inp)
  379. continue
  380. }
  381. // Decode vcount
  382. if child.Name.Local == "vcount" {
  383. vc, err := d.decVcount(child, data, pl.Count)
  384. if err != nil {
  385. return err
  386. }
  387. pl.Vcount = vc
  388. continue
  389. }
  390. // Decode p (primitive)
  391. if child.Name.Local == "p" {
  392. p, err := d.decPrimitive(child, data)
  393. if err != nil {
  394. return err
  395. }
  396. pl.P = p
  397. }
  398. }
  399. return nil
  400. }
  401. func (d *Decoder) decInputShared(start xml.StartElement) (InputShared, error) {
  402. var inp InputShared
  403. inp.Offset, _ = strconv.Atoi(findAttrib(start, "offset").Value)
  404. inp.Semantic = findAttrib(start, "semantic").Value
  405. inp.Source = findAttrib(start, "source").Value
  406. inp.Set, _ = strconv.Atoi(findAttrib(start, "set").Value)
  407. return inp, nil
  408. }
  409. func (d *Decoder) decVcount(start xml.StartElement, data []byte, size int) ([]int, error) {
  410. vcount := make([]int, size)
  411. var br bytesReader
  412. br.Init(data)
  413. idx := 0
  414. for {
  415. tok := br.TokenNext()
  416. if tok == nil {
  417. break
  418. }
  419. v, err := strconv.Atoi(string(tok))
  420. if err != nil {
  421. return nil, err
  422. }
  423. vcount[idx] = v
  424. idx++
  425. }
  426. return vcount, nil
  427. }
  428. func (d *Decoder) decPrimitive(start xml.StartElement, data []byte) ([]int, error) {
  429. p := make([]int, 0)
  430. var br bytesReader
  431. br.Init(data)
  432. idx := 0
  433. for {
  434. tok := br.TokenNext()
  435. if tok == nil {
  436. break
  437. }
  438. v, err := strconv.Atoi(string(tok))
  439. if err != nil {
  440. return nil, err
  441. }
  442. p = append(p, v)
  443. idx++
  444. }
  445. return p, nil
  446. }