def test_path_normalization(self): """Test path normalization function.""" path1 = norm_path(PATH) alpha = radians(50) c = cos(alpha) s = sin(alpha) path2 = norm_path(array(path1) @ array([[c, -s], [s, c]])) for i in range(len(PATH)): h = hypot(path1[i][0] - path2[i][0], path1[i][1] - path2[i][1]) self.assertAlmostEqual(0, h, 6)
def __init__( self, mechanism: Dict[str, Any], path: Sequence[Sequence[_Coord]], vpoints: List[VPoint] = None, vlinks: List[VLink] = None, parent: QWidget = None ): """Input link and path data.""" super(_DynamicCanvas, self).__init__(parent) self.mechanism = mechanism self.vpoints = vpoints or [] self.vlinks = vlinks or [] self.no_mechanism = not self.vpoints or not self.vlinks use_norm = self.no_mechanism and ( self.mechanism.get('shape_only', False) or self.mechanism.get('wavelet_mode', False)) # Target path same: Dict[int, int] = self.mechanism['same'] target_path: _TargetPath = self.mechanism['target'] for i, p in target_path.items(): for j in range(i): if j in same: i -= 1 self.target_path[i] = norm_path(p) if use_norm else p self.path.path = [] for i, p in enumerate(path): if i in self.target_path and use_norm: self.path.path.append(norm_path(efd_fitting(p))) else: self.path.path.append(p) self.__index = 0 self.__interval = 1 self.__path_count = max(len(path) for path in self.path.path) - 1 self.pos: List[_Coord] = [] # Error self.error = False self.__no_error = 0 if self.no_mechanism: return # Ranges ranges: Dict[int, _Range] = self.mechanism['placement'] self.ranges.update({f"P{i}": QRectF( QPointF(values[0] - values[2], values[1] + values[2]), QSizeF(values[2] * 2, values[2] * 2) ) for i, values in ranges.items()}) # Timer self.__timer = QTimer() self.__timer.timeout.connect(self.__change_index) self.__timer.start(18)
def __norm_path(self) -> None: """Normalize current path.""" scale, ok = QInputDialog.getDouble(self, "Scale", "Length of unit vector:", 60, 0.01, 1000, 2) if ok: self.set_path(norm_path(self.current_path(), scale))