obj.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 obj
  5. import (
  6. "bufio"
  7. "errors"
  8. "fmt"
  9. "github.com/g3n/engine/core"
  10. "github.com/g3n/engine/geometry"
  11. "github.com/g3n/engine/gls"
  12. "github.com/g3n/engine/graphic"
  13. "github.com/g3n/engine/material"
  14. "github.com/g3n/engine/math32"
  15. "io"
  16. "math"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. "strings"
  21. )
  22. // Decoder contains all decoded data from the obj and mtl files
  23. type Decoder struct {
  24. Objects []Object // decoded objects
  25. Matlib string // name of the material lib
  26. Materials map[string]*Material // maps material name to object
  27. Vertices math32.ArrayF32 // vertices positions array
  28. Normals math32.ArrayF32 // vertices normals
  29. Uvs math32.ArrayF32 // vertices texture coordinates
  30. Warnings []string // warning messages
  31. line uint // current line number
  32. objCurrent *Object // current object
  33. matCurrent *Material // current material
  34. smoothCurrent bool // current smooth state
  35. }
  36. // Object contains all information about one decoded object
  37. type Object struct {
  38. Name string // Object name
  39. Faces []Face // Faces
  40. materials []string // Materials used in this object
  41. }
  42. // Face contains all information about an object face
  43. type Face struct {
  44. Vertices []int // Indices to the face vertices
  45. Uvs []int // Indices to the face UV coordinates
  46. Normals []int // Indices to the face normals
  47. Material string // Material name
  48. Smooth bool // Smooth face
  49. }
  50. // Material contains all information about an object material
  51. type Material struct {
  52. Name string // Material name
  53. Illum int // Illumination model
  54. Opacity float32 // Opacity factor
  55. Refraction float32 // Refraction factor
  56. Shininess float32 // Shininess (specular exponent)
  57. Ambient math32.Color // Ambient color reflectivity
  58. Diffuse math32.Color // Diffuse color reflectivity
  59. Specular math32.Color // Specular color reflectivity
  60. Emissive math32.Color // Emissive color
  61. MapKd string // Texture file linked to diffuse color
  62. }
  63. // Local constants
  64. const (
  65. blanks = "\r\n\t "
  66. invINDEX = math.MaxUint32
  67. objType = "obj"
  68. mtlType = "mtl"
  69. )
  70. // Decode decodes the specified obj and mtl files returning a decoder
  71. // object and an error.
  72. func Decode(objpath string, mtlpath string) (*Decoder, error) {
  73. // Opens obj file
  74. fobj, err := os.Open(objpath)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer fobj.Close()
  79. // If path of material file not supplied,
  80. // try to use the base name of the obj file
  81. if len(mtlpath) == 0 {
  82. dir, objfile := filepath.Split(objpath)
  83. ext := filepath.Ext(objfile)
  84. mtlpath = dir + objfile[:len(objfile)-len(ext)] + ".mtl"
  85. }
  86. // Opens mtl file
  87. fmtl, err := os.Open(mtlpath)
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer fmtl.Close()
  92. return DecodeReader(fobj, fmtl)
  93. }
  94. // DecodeReader decodes the specified obj and mtl readers returning a decoder
  95. // object and an error.
  96. func DecodeReader(objreader, mtlreader io.Reader) (*Decoder, error) {
  97. dec := new(Decoder)
  98. dec.Objects = make([]Object, 0)
  99. dec.Warnings = make([]string, 0)
  100. dec.Materials = make(map[string]*Material)
  101. dec.Vertices = math32.NewArrayF32(0, 0)
  102. dec.Normals = math32.NewArrayF32(0, 0)
  103. dec.Uvs = math32.NewArrayF32(0, 0)
  104. dec.line = 1
  105. // Parses obj lines
  106. err := dec.parse(objreader, dec.parseObjLine)
  107. if err != nil {
  108. return nil, err
  109. }
  110. // Parses mtl lines
  111. dec.matCurrent = nil
  112. dec.line = 1
  113. err = dec.parse(mtlreader, dec.parseMtlLine)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return dec, nil
  118. }
  119. // NewGroup creates and returns a group containing as children meshes
  120. // with all the decoded objects.
  121. // A group is returned even if there is only one object decoded.
  122. func (dec *Decoder) NewGroup() (*core.Node, error) {
  123. group := core.NewNode()
  124. for i := 0; i < len(dec.Objects); i++ {
  125. mesh, err := dec.NewMesh(&dec.Objects[i])
  126. if err != nil {
  127. return nil, err
  128. }
  129. group.Add(mesh)
  130. }
  131. return group, nil
  132. }
  133. // NewMesh creates and returns a mesh from an specified decoded object.
  134. func (dec *Decoder) NewMesh(obj *Object) (*graphic.Mesh, error) {
  135. // Creates object geometry
  136. geom, err := dec.NewGeometry(obj)
  137. if err != nil {
  138. return nil, err
  139. }
  140. // Single material
  141. if geom.GroupCount() == 1 {
  142. matName := obj.materials[0]
  143. matDesc := dec.Materials[matName]
  144. // Creates material
  145. mat := material.NewPhong(&matDesc.Diffuse)
  146. ambientColor := mat.AmbientColor()
  147. mat.SetAmbientColor(ambientColor.Multiply(&matDesc.Ambient))
  148. mat.SetSpecularColor(&matDesc.Specular)
  149. mat.SetShininess(matDesc.Shininess)
  150. return graphic.NewMesh(geom, mat), nil
  151. }
  152. // Multi material
  153. mesh := graphic.NewMesh(geom, nil)
  154. for idx := 0; idx < geom.GroupCount(); idx++ {
  155. group := geom.GroupAt(idx)
  156. matName := obj.materials[group.Matindex]
  157. matDesc := dec.Materials[matName]
  158. // Creates material
  159. matGroup := material.NewPhong(&matDesc.Diffuse)
  160. ambientColor := matGroup.AmbientColor()
  161. matGroup.SetAmbientColor(ambientColor.Multiply(&matDesc.Ambient))
  162. matGroup.SetSpecularColor(&matDesc.Specular)
  163. matGroup.SetShininess(matDesc.Shininess)
  164. mesh.AddGroupMaterial(matGroup, idx)
  165. }
  166. return mesh, nil
  167. }
  168. // NewGeometry generates and returns a geometry from the specified object
  169. func (dec *Decoder) NewGeometry(obj *Object) (*geometry.Geometry, error) {
  170. geom := geometry.NewGeometry()
  171. // Create buffers
  172. positions := math32.NewArrayF32(0, 0)
  173. normals := math32.NewArrayF32(0, 0)
  174. uvs := math32.NewArrayF32(0, 0)
  175. indices := math32.NewArrayU32(0, 0)
  176. // copy all vertex info from the decoded Object, face and index to the geometry
  177. copyVertex := func(face *Face, idx int) {
  178. var vec3 math32.Vector3
  179. var vec2 math32.Vector2
  180. pos := positions.Size() / 3
  181. // Copy vertex position and append to geometry
  182. dec.Vertices.GetVector3(3*face.Vertices[idx], &vec3)
  183. positions.AppendVector3(&vec3)
  184. // Copy vertex normal and append to geometry
  185. if face.Normals[idx] != invINDEX {
  186. dec.Normals.GetVector3(3*face.Normals[idx], &vec3)
  187. normals.AppendVector3(&vec3)
  188. }
  189. // Copy vertex uv and append to geometry
  190. if face.Uvs[idx] != invINDEX {
  191. dec.Uvs.GetVector2(2*face.Uvs[idx], &vec2)
  192. uvs.AppendVector2(&vec2)
  193. }
  194. indices.Append(uint32(pos))
  195. }
  196. var group *geometry.Group
  197. matName := ""
  198. matIndex := 0
  199. for _, face := range obj.Faces {
  200. // If face material changed, starts a new group
  201. if face.Material != matName {
  202. group = geom.AddGroup(indices.Size(), 0, matIndex)
  203. matName = face.Material
  204. matIndex++
  205. }
  206. // Copy face vertices to geometry
  207. for idx := 1; idx < len(face.Vertices)-1; idx++ {
  208. copyVertex(&face, 0)
  209. copyVertex(&face, idx)
  210. copyVertex(&face, idx+1)
  211. group.Count += 3
  212. }
  213. }
  214. geom.SetIndices(indices)
  215. geom.AddVBO(gls.NewVBO().AddAttrib("VertexPosition", 3).SetBuffer(positions))
  216. geom.AddVBO(gls.NewVBO().AddAttrib("VertexNormal", 3).SetBuffer(normals))
  217. geom.AddVBO(gls.NewVBO().AddAttrib("VertexTexcoord", 2).SetBuffer(uvs))
  218. return geom, nil
  219. }
  220. // parse reads the lines from the specified reader and dispatch them
  221. // to the specified line parser.
  222. func (dec *Decoder) parse(reader io.Reader, parseLine func(string) error) error {
  223. bufin := bufio.NewReader(reader)
  224. dec.line = 1
  225. for {
  226. // Reads next line and abort on errors (not EOF)
  227. line, err := bufin.ReadString('\n')
  228. if err != nil && err != io.EOF {
  229. return err
  230. }
  231. // Parses the line
  232. line = strings.Trim(line, blanks)
  233. perr := parseLine(line)
  234. if perr != nil {
  235. return perr
  236. }
  237. // If EOF ends of parsing.
  238. if err == io.EOF {
  239. break
  240. }
  241. dec.line++
  242. }
  243. return nil
  244. }
  245. // Parses obj file line, dispatching to specific parsers
  246. func (dec *Decoder) parseObjLine(line string) error {
  247. // Ignore empty lines
  248. fields := strings.Split(line, " ")
  249. if len(fields) == 0 {
  250. return nil
  251. }
  252. // Ignore comment lines
  253. ltype := fields[0]
  254. if strings.HasPrefix(ltype, "#") {
  255. return nil
  256. }
  257. switch ltype {
  258. case "mtllib":
  259. return dec.parseMatlib(fields[1:])
  260. case "o":
  261. return dec.parseObject(fields[1:])
  262. case "v":
  263. return dec.parseVertex(fields[1:])
  264. case "vn":
  265. return dec.parseNormal(fields[1:])
  266. case "vt":
  267. return dec.parseTex(fields[1:])
  268. case "f":
  269. return dec.parseFace(fields[1:])
  270. case "usemtl":
  271. return dec.parseUsemtl(fields[1:])
  272. case "s":
  273. return dec.parseSmooth(fields[1:])
  274. default:
  275. dec.appendWarn(objType, "field not supported: "+ltype)
  276. }
  277. return nil
  278. }
  279. // Parses a mtllib line:
  280. // mtllib <name>
  281. func (dec *Decoder) parseMatlib(fields []string) error {
  282. if len(fields) < 1 {
  283. return errors.New("Object line (o) with less than 2 fields")
  284. }
  285. dec.Matlib = fields[0]
  286. return nil
  287. }
  288. // Parses an object line:
  289. // o <name>
  290. func (dec *Decoder) parseObject(fields []string) error {
  291. if len(fields) < 1 {
  292. return errors.New("Object line (o) with less than 2 fields")
  293. }
  294. var ob Object
  295. ob.Name = fields[0]
  296. ob.Faces = make([]Face, 0)
  297. ob.materials = make([]string, 0)
  298. dec.Objects = append(dec.Objects, ob)
  299. dec.objCurrent = &dec.Objects[len(dec.Objects)-1]
  300. return nil
  301. }
  302. // Parses a vertex position line
  303. // v <x> <y> <z> [w]
  304. func (dec *Decoder) parseVertex(fields []string) error {
  305. if len(fields) < 3 {
  306. return errors.New("Less than 3 vertices in 'v' line")
  307. }
  308. for _, f := range fields[:3] {
  309. val, err := strconv.ParseFloat(f, 32)
  310. if err != nil {
  311. return err
  312. }
  313. dec.Vertices.Append(float32(val))
  314. }
  315. return nil
  316. }
  317. // Parses a vertex normal line
  318. // vn <x> <y> <z>
  319. func (dec *Decoder) parseNormal(fields []string) error {
  320. if len(fields) < 3 {
  321. return errors.New("Less than 3 normals in 'vn' line")
  322. }
  323. for _, f := range fields[:3] {
  324. val, err := strconv.ParseFloat(f, 32)
  325. if err != nil {
  326. return err
  327. }
  328. dec.Normals.Append(float32(val))
  329. }
  330. return nil
  331. }
  332. // Parses a vertex texture coordinate line:
  333. // vt <u> <v> <w>
  334. func (dec *Decoder) parseTex(fields []string) error {
  335. if len(fields) < 2 {
  336. return errors.New("Less than 2 texture coords. in 'vt' line")
  337. }
  338. for _, f := range fields[:2] {
  339. val, err := strconv.ParseFloat(f, 32)
  340. if err != nil {
  341. return err
  342. }
  343. dec.Uvs.Append(float32(val))
  344. }
  345. return nil
  346. }
  347. // parseFace parses a face decription line:
  348. // f v1[/vt1][/vn1] v2[/vt2][/vn2] v3[/vt3][/vn3] ...
  349. func (dec *Decoder) parseFace(fields []string) error {
  350. if len(fields) < 3 {
  351. return dec.formatError("Face line with less 3 fields")
  352. }
  353. if dec.matCurrent == nil {
  354. return dec.formatError("No material defined")
  355. }
  356. var face Face
  357. face.Vertices = make([]int, len(fields))
  358. face.Uvs = make([]int, len(fields))
  359. face.Normals = make([]int, len(fields))
  360. face.Material = dec.matCurrent.Name
  361. face.Smooth = dec.smoothCurrent
  362. for pos, f := range fields {
  363. // Separate the current field in its components: v vt vn
  364. vfields := strings.Split(f, "/")
  365. if len(vfields) < 1 {
  366. return dec.formatError("Face field with no parts")
  367. }
  368. // Get the index of this vertex position (must always exist)
  369. val, err := strconv.ParseUint(vfields[0], 10, 32)
  370. if err != nil {
  371. return err
  372. }
  373. if val < 1 {
  374. return dec.formatError("Face vertex position index value less than 1")
  375. }
  376. face.Vertices[pos] = int(val - 1)
  377. // Get the index of this vertex UV coordinate (optional)
  378. if len(vfields) > 1 && len(vfields[1]) > 0 {
  379. val, err := strconv.ParseUint(vfields[1], 10, 32)
  380. if err != nil {
  381. return err
  382. }
  383. if val < 1 {
  384. return dec.formatError("Face uv index value less than 1")
  385. }
  386. face.Uvs[pos] = int(val - 1)
  387. } else {
  388. face.Uvs[pos] = invINDEX
  389. }
  390. // Get the index of this vertex normal (optional)
  391. if len(vfields) >= 3 {
  392. val, err = strconv.ParseUint(vfields[2], 10, 32)
  393. if err != nil {
  394. return err
  395. }
  396. if val < 1 {
  397. return dec.formatError("Face normal index value less than 1")
  398. }
  399. face.Normals[pos] = int(val - 1)
  400. } else {
  401. face.Normals[pos] = invINDEX
  402. }
  403. }
  404. // Appends this face to the current object
  405. dec.objCurrent.Faces = append(dec.objCurrent.Faces, face)
  406. return nil
  407. }
  408. // parseUsemtl parses a "usemtl" decription line:
  409. // usemtl <name>
  410. func (dec *Decoder) parseUsemtl(fields []string) error {
  411. if len(fields) < 1 {
  412. return dec.formatError("Usemtl with no fields")
  413. }
  414. // Checks if this material has already been parsed
  415. name := fields[0]
  416. mat := dec.Materials[name]
  417. // Creates material descriptor
  418. if mat == nil {
  419. mat = new(Material)
  420. mat.Name = name
  421. dec.Materials[name] = mat
  422. }
  423. dec.objCurrent.materials = append(dec.objCurrent.materials, name)
  424. // Set this as the current material
  425. dec.matCurrent = mat
  426. return nil
  427. }
  428. // parseSmooth parses a "s" decription line:
  429. // s <0|1>
  430. func (dec *Decoder) parseSmooth(fields []string) error {
  431. if len(fields) < 1 {
  432. return dec.formatError("'s' with no fields")
  433. }
  434. if fields[0] == "0" || fields[0] == "off" {
  435. dec.smoothCurrent = false
  436. return nil
  437. }
  438. if fields[0] == "1" || fields[0] == "on" {
  439. dec.smoothCurrent = true
  440. return nil
  441. }
  442. return dec.formatError("'s' with invalid value")
  443. }
  444. /******************************************************************************
  445. mtl parse functions
  446. */
  447. // Parses material file line, dispatching to specific parsers
  448. func (dec *Decoder) parseMtlLine(line string) error {
  449. // Ignore empty lines
  450. fields := strings.Split(line, " ")
  451. if len(fields) == 0 {
  452. return nil
  453. }
  454. // Ignore comment lines
  455. ltype := fields[0]
  456. if strings.HasPrefix(ltype, "#") {
  457. return nil
  458. }
  459. switch ltype {
  460. case "newmtl":
  461. return dec.parseNewmtl(fields[1:])
  462. case "d":
  463. return dec.parseDissolve(fields[1:])
  464. case "Ka":
  465. return dec.parseKa(fields[1:])
  466. case "Kd":
  467. return dec.parseKd(fields[1:])
  468. case "Ke":
  469. return dec.parseKe(fields[1:])
  470. case "Ks":
  471. return dec.parseKs(fields[1:])
  472. case "Ni":
  473. return dec.parseNi(fields[1:])
  474. case "Ns":
  475. return dec.parseNs(fields[1:])
  476. case "illum":
  477. return dec.parseIllum(fields[1:])
  478. case "map_Kd":
  479. return dec.parseMapKd(fields[1:])
  480. default:
  481. dec.appendWarn(mtlType, "field not supported: "+ltype)
  482. }
  483. return nil
  484. }
  485. // Parses new material definition
  486. // newmtl <mat_name>
  487. func (dec *Decoder) parseNewmtl(fields []string) error {
  488. if len(fields) < 1 {
  489. return dec.formatError("newmtl with no fields")
  490. }
  491. // Checks if material has already been seen
  492. name := fields[0]
  493. mat := dec.Materials[name]
  494. // Creates material descriptor
  495. if mat == nil {
  496. mat = new(Material)
  497. mat.Name = name
  498. dec.Materials[name] = mat
  499. }
  500. dec.matCurrent = mat
  501. return nil
  502. }
  503. // Parses the dissolve factor (opacity)
  504. // d <factor>
  505. func (dec *Decoder) parseDissolve(fields []string) error {
  506. if len(fields) < 1 {
  507. return dec.formatError("'d' with no fields")
  508. }
  509. val, err := strconv.ParseFloat(fields[0], 32)
  510. if err != nil {
  511. return dec.formatError("'d' parse float error")
  512. }
  513. dec.matCurrent.Opacity = float32(val)
  514. return nil
  515. }
  516. // Parses ambient reflectivity:
  517. // Ka r g b
  518. func (dec *Decoder) parseKa(fields []string) error {
  519. if len(fields) < 3 {
  520. return dec.formatError("'Ka' with less than 3 fields")
  521. }
  522. var colors [3]float32
  523. for pos, f := range fields[:3] {
  524. val, err := strconv.ParseFloat(f, 32)
  525. if err != nil {
  526. return err
  527. }
  528. colors[pos] = float32(val)
  529. }
  530. dec.matCurrent.Ambient.Set(colors[0], colors[1], colors[2])
  531. return nil
  532. }
  533. // Parses diffuse reflectivity:
  534. // Kd r g b
  535. func (dec *Decoder) parseKd(fields []string) error {
  536. if len(fields) < 3 {
  537. return dec.formatError("'Kd' with less than 3 fields")
  538. }
  539. var colors [3]float32
  540. for pos, f := range fields[:3] {
  541. val, err := strconv.ParseFloat(f, 32)
  542. if err != nil {
  543. return err
  544. }
  545. colors[pos] = float32(val)
  546. }
  547. dec.matCurrent.Diffuse.Set(colors[0], colors[1], colors[2])
  548. return nil
  549. }
  550. // Parses emissive color:
  551. // Ke r g b
  552. func (dec *Decoder) parseKe(fields []string) error {
  553. if len(fields) < 3 {
  554. return dec.formatError("'Ke' with less than 3 fields")
  555. }
  556. var colors [3]float32
  557. for pos, f := range fields[:3] {
  558. val, err := strconv.ParseFloat(f, 32)
  559. if err != nil {
  560. return err
  561. }
  562. colors[pos] = float32(val)
  563. }
  564. dec.matCurrent.Emissive.Set(colors[0], colors[1], colors[2])
  565. return nil
  566. }
  567. // Parses specular reflectivity:
  568. // Ks r g b
  569. func (dec *Decoder) parseKs(fields []string) error {
  570. if len(fields) < 3 {
  571. return dec.formatError("'Ks' with less than 3 fields")
  572. }
  573. var colors [3]float32
  574. for pos, f := range fields[:3] {
  575. val, err := strconv.ParseFloat(f, 32)
  576. if err != nil {
  577. return err
  578. }
  579. colors[pos] = float32(val)
  580. }
  581. dec.matCurrent.Specular.Set(colors[0], colors[1], colors[2])
  582. return nil
  583. }
  584. // Parses optical density, also known as index of refraction
  585. // Ni <optical_density>
  586. func (dec *Decoder) parseNi(fields []string) error {
  587. if len(fields) < 1 {
  588. return dec.formatError("'Ni' with no fields")
  589. }
  590. val, err := strconv.ParseFloat(fields[0], 32)
  591. if err != nil {
  592. return dec.formatError("'d' parse float error")
  593. }
  594. dec.matCurrent.Refraction = float32(val)
  595. return nil
  596. }
  597. // Parses specular exponent
  598. // Ns <specular_exponent>
  599. func (dec *Decoder) parseNs(fields []string) error {
  600. if len(fields) < 1 {
  601. return dec.formatError("'Ns' with no fields")
  602. }
  603. val, err := strconv.ParseFloat(fields[0], 32)
  604. if err != nil {
  605. return dec.formatError("'d' parse float error")
  606. }
  607. dec.matCurrent.Shininess = float32(val)
  608. return nil
  609. }
  610. // Parses illumination model (0 to 10)
  611. // illum <ilum_#>
  612. func (dec *Decoder) parseIllum(fields []string) error {
  613. if len(fields) < 1 {
  614. return dec.formatError("'illum' with no fields")
  615. }
  616. val, err := strconv.ParseUint(fields[0], 10, 32)
  617. if err != nil {
  618. return dec.formatError("'d' parse int error")
  619. }
  620. dec.matCurrent.Illum = int(val)
  621. return nil
  622. }
  623. // Parses color texture linked to the diffuse reflectivity of the material
  624. // map_Kd [-options] <filename>
  625. func (dec *Decoder) parseMapKd(fields []string) error {
  626. if len(fields) < 1 {
  627. return dec.formatError("No fields")
  628. }
  629. dec.matCurrent.MapKd = fields[0]
  630. return nil
  631. }
  632. func (dec *Decoder) formatError(msg string) error {
  633. return fmt.Errorf("%s in line:%d", msg, dec.line)
  634. }
  635. func (dec *Decoder) appendWarn(ftype string, msg string) {
  636. wline := fmt.Sprintf("%s(%d): %s", ftype, dec.line, msg)
  637. dec.Warnings = append(dec.Warnings, wline)
  638. }