Coverage for trimesh/exchange/xyz.py: 95%

38 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-12 17:18 +0000

1import numpy as np 

2 

3from .. import util 

4from ..points import PointCloud 

5 

6 

7def load_xyz(file_obj, delimiter=None, **kwargs): 

8 """ 

9 Load an XYZ file into a PointCloud. 

10 

11 Parameters 

12 ------------ 

13 file_obj : an open file-like object 

14 Source data, ASCII XYZ 

15 delimiter : None or string 

16 Characters used to separate the columns of the file 

17 If not passed will use whitespace or commas 

18 

19 Returns 

20 ---------- 

21 kwargs : dict 

22 Data which can be passed to PointCloud constructor 

23 """ 

24 # read the whole file into memory as a string 

25 raw = util.decode_text(file_obj.read()).strip() 

26 # get the first line to look at, including files with only one point 

27 first, _, _ = raw.partition("\n") 

28 first = first.strip() 

29 if delimiter is None and "," in first: 

30 # if no delimiter passed and file has commas 

31 delimiter = "," 

32 if delimiter is not None: 

33 # replace delimiter with whitespace so split works 

34 raw = raw.replace(delimiter, " ") 

35 first = first.replace(delimiter, " ") 

36 

37 # count columns after normalizing the delimiter 

38 columns = len(first.split()) 

39 if columns < 3: 

40 raise ValueError("not enough columns in xyz file!") 

41 

42 # use string splitting to get array 

43 array = np.array(raw.split(), dtype=np.float64) 

44 # reshape to column count 

45 # if file has different numbers of values 

46 # per row this will fail as it should 

47 data = array.reshape((-1, columns)) 

48 

49 # start with no colors 

50 colors = None 

51 # vertices are the first three columns 

52 vertices = data[:, :3] 

53 if columns == 6: 

54 # RGB colors 

55 colors = np.array(data[:, 3:], dtype=np.uint8) 

56 colors = np.concatenate( 

57 (colors, np.ones((len(data), 1), dtype=np.uint8) * 255), axis=1 

58 ) 

59 elif columns >= 7: 

60 # extract RGBA colors 

61 colors = np.array(data[:, 3:7], dtype=np.uint8) 

62 # add extracted colors and vertices to kwargs 

63 kwargs.update({"vertices": vertices, "colors": colors}) 

64 

65 return kwargs 

66 

67 

68def export_xyz(cloud, write_colors=True, delimiter=None): 

69 """ 

70 Export a PointCloud object to an XYZ format string. 

71 

72 Parameters 

73 ------------- 

74 cloud : trimesh.PointCloud 

75 Geometry in space 

76 write_colors : bool 

77 Write colors or not 

78 delimiter : None or str 

79 What to separate columns with 

80 

81 Returns 

82 -------------- 

83 export : str 

84 Pointcloud in XYZ format 

85 """ 

86 if not isinstance(cloud, PointCloud): 

87 raise ValueError("object must be PointCloud") 

88 

89 # compile data into a blob 

90 data = cloud.vertices 

91 if write_colors and hasattr(cloud, "colors") and len(cloud.colors) == len(data): 

92 # stack colors and vertices 

93 data = np.hstack((data, cloud.colors)) 

94 

95 # if delimiter not passed use whitespace 

96 if delimiter is None: 

97 delimiter = " " 

98 # stack blob into XYZ format 

99 export = util.array_to_string(data, col_delim=delimiter) 

100 

101 return export 

102 

103 

104_xyz_loaders = {"xyz": load_xyz} 

105_xyz_exporters = {"xyz": export_xyz}