Coverage for trimesh/constants.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-24 04:40 +0000

1from dataclasses import dataclass 

2 

3import numpy as np 

4 

5from .util import decimal_to_digits, log, now 

6 

7 

8@dataclass 

9class ToleranceMesh: 

10 """ 

11 ToleranceMesh objects hold tolerance information about meshes. 

12 

13 Parameters 

14 ---------------- 

15 tol.zero : float 

16 Floating point numbers smaller than this are considered zero 

17 tol.merge : float 

18 When merging vertices, consider vertices closer than this 

19 to be the same vertex. Here we use the same value (1e-8) 

20 as SolidWorks uses, according to their documentation. 

21 tol.planar : float 

22 The maximum distance from a plane a point can be and 

23 still be considered to be on the plane 

24 tol.facet_threshold : float 

25 Threshold for two facets to be considered coplanar 

26 tol.strict : bool 

27 If True, run additional in- process checks (slower) 

28 """ 

29 

30 # set our zero for floating point comparison to 100x 

31 # the resolution of float64 which works out to 1e-13 

32 zero: float = np.finfo(np.float64).resolution * 100 

33 

34 # vertices closer than this should be merged 

35 merge: float = 1e-8 

36 

37 # peak to valley flatness to be considered planar 

38 planar: float = 1e-5 

39 

40 # coplanar threshold: ratio of (radius / span) ** 2 

41 facet_threshold: int = 5000 

42 

43 # should additional slow checks be run inside functions 

44 strict: bool = False 

45 

46 

47@dataclass 

48class TolerancePath: 

49 """ 

50 TolerancePath objects contain tolerance information used in 

51 Path objects. 

52 

53 Parameters 

54 --------------- 

55 tol.zero : float 

56 Floating point numbers smaller than this are considered zero 

57 tol.merge : float 

58 When merging vertices, consider vertices closer than this 

59 to be the same vertex. Here we use the same value (1e-8) 

60 as SolidWorks uses, according to their documentation. 

61 tol.planar : float 

62 The maximum distance from a plane a point can be and 

63 still be considered to be on the plane 

64 tol.seg_frac : float 

65 When simplifying line segments what percentage of the drawing 

66 scale can a segment be and have a curve fitted 

67 tol.seg_angle : float 

68 When simplifying line segments to arcs, what angle 

69 can a segment span to be acceptable. 

70 tol.aspect_frac : float 

71 When simplifying line segments to closed arcs (circles) 

72 what percentage can the aspect ratio differfrom 1:1 

73 before escaping the fit early 

74 tol.radius_frac : float 

75 When simplifying line segments to arcs, what percentage 

76 of the fit radius can vertices deviate to be acceptable 

77 tol.radius_min : 

78 When simplifying line segments to arcs, what is the minimum 

79 radius multiplied by document scale for an acceptable fit 

80 tol.radius_max : 

81 When simplifying line segments to arcs, what is the maximum 

82 radius multiplied by document scale for an acceptable fit 

83 tol.tangent : 

84 When simplifying line segments to curves, what is the maximum 

85 angle the end sections can deviate from tangent that is 

86 acceptable. 

87 """ 

88 

89 zero: float = 1e-12 

90 merge: float = 1e-5 

91 

92 planar: float = 1e-5 

93 seg_frac: float = 0.125 

94 seg_angle: float = float(np.radians(50)) 

95 seg_angle_min: float = float(np.radians(1)) 

96 seg_angle_frac: float = 0.5 

97 aspect_frac: float = 0.1 

98 radius_frac: float = 0.02 

99 radius_min: float = 1e-4 

100 radius_max: float = 50.0 

101 tangent: float = float(np.radians(20)) 

102 strict: bool = False 

103 

104 @property 

105 def merge_digits(self) -> int: 

106 return decimal_to_digits(self.merge) 

107 

108 

109@dataclass 

110class ResolutionPath: 

111 """ 

112 res.seg_frac : float 

113 When discretizing curves, what percentage of the drawing 

114 scale should we aim to make a single segment 

115 res.seg_angle : float 

116 When discretizing curves, what angle should a section span 

117 res.max_sections : int 

118 When discretizing splines, what is the maximum number 

119 of segments per control point 

120 res.min_sections : int 

121 When discretizing splines, what is the minimum number 

122 of segments per control point 

123 res.export : str 

124 Format string to use when exporting floating point vertices 

125 """ 

126 

127 seg_frac: float = 0.05 

128 seg_angle: float = 0.08 

129 max_sections: float = 500.0 

130 min_sections: float = 20.0 

131 export: str = "0.10f" 

132 

133 

134# instantiate mesh tolerances with defaults 

135tol = ToleranceMesh() 

136 

137# instantiate path tolerances with defaults 

138tol_path = TolerancePath() 

139res_path = ResolutionPath() 

140 

141 

142def log_time(method): 

143 """ 

144 A decorator for methods which will time the method 

145 and then emit a log.debug message with the method name 

146 and how long it took to execute. 

147 """ 

148 

149 def timed(*args, **kwargs): 

150 tic = now() 

151 result = method(*args, **kwargs) 

152 log.debug("%s executed in %.4f seconds.", method.__name__, now() - tic) 

153 

154 return result 

155 

156 timed.__name__ = method.__name__ 

157 timed.__doc__ = method.__doc__ 

158 return timed