Guest User

Untitled

a guest
Apr 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. // Solution 1: Usage
  2. let string = matrix.serialized
  3. let newMatrix = matrix_float4x4(string)
  4.  
  5. // Solution 2: Usage
  6. let string = serialize(matrix)
  7. let newMatrix = deserializeMatrix(string)
  8.  
  9. // Solution 1: Implementation
  10.  
  11. extension simd_float4 {
  12. var serialized: String {
  13. return [x, y, z, w].map(String.init).joined(separator: ",")
  14. }
  15.  
  16. init(_ string: String) {
  17. let points = string.split(separator: ",").flatMap(Float.init)
  18. self.init(points)
  19. }
  20. }
  21.  
  22. extension matrix_float4x4 {
  23. var serialized: String {
  24. let cs = [columns.0, columns.1, columns.2, columns.3]
  25. return cs.map { $0.serialized }.joined(separator: "|")
  26. }
  27.  
  28. init(_ string: String) {
  29. let columns = string.split(separator: "|").map(String.init).map(simd_float4.init)
  30. self.init(columns)
  31. }
  32. }
  33.  
  34. // Solution 2: Implementation
  35.  
  36. func serialize(_ sf: simd_float4) -> String {
  37. let points = [sf.x, sf.y, sf.z, sf.w]
  38. return points.map(String.init).joined(separator: ",")
  39. }
  40.  
  41. func serialize(_ m: matrix_float4x4) -> String {
  42. let columns = [m.columns.0, m.columns.1, m.columns.2, m.columns.3]
  43. return columns.map(serialize).joined(separator: "|")
  44. }
  45.  
  46. func deserializeFloat4(_ str: String) -> simd_float4 {
  47. let points = str.split(separator: ",").flatMap(Float.init)
  48. return simd_float4(points)
  49. }
  50.  
  51. func deserializeMatrix(_ str: String) -> matrix_float4x4 {
  52. let columns = str.split(separator: "|").map(String.init).map(deserializeFloat4)
  53. return matrix_float4x4(columns)
  54. }
Add Comment
Please, Sign In to add comment