Pymecavideo 8.0
Étude cinématique à l'aide de vidéos
vecteur.py
1# -*- coding: utf-8 -*-
2"""
3 vecteur.py is a module of pymecavideo.
4 pymecavideo is a program to track moving points in a video frameset
5 Copyright (C) 2007 Jean-Baptiste Butet <ashashiwa@gmail.com>
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19"""
20
21"""
22vecteur.py implements some operations for 2D vectors, using tuples
23"""
24
25
26
27
28import math
29
30from PyQt6.QtCore import QPointF
31
32class vecteur:
33 """
34 une classe pour des vecteurs 2D ; les coordonnées sont flottantes, et
35 on peut accéder à celles-ci par les propriétés self.xx et self.yy
36
37 Paramètres du constructeur
38
39 @param x une abscisse, nulle par défaut
40 @param y une ordonnée, nulle par défaut
41 @param qPoint (None par défaut) ; si ce paramètre est d'un type
42 qui possède les méthodes .x() et .y(), il sert à créer le vecteur
43 de façon prioritaire.
44 """
45 def __init__(self, x=0, y=0, qPoint=None):
46 if qPoint:
47 self.value = (qPoint.x(), qPoint.y())
48 return
49 self.precision = 4 # nb de chiffres significatifs
50 self.value = (self.signif(float(x), self.precision),
51 self.signif(float(y), self.precision))
52 return
53
54 def copy(self):
55 return vecteur(self.xx, self.yy)
56
57 @property
58 def x(self):
59 return self.value[0]
60
61 @property
62 def y(self):
63 return self.value[1]
64
65 def manhattanLength(self):
66 """
67 @return la longueur « de mahattan »
68 """
69 return abs(self.xx) + abs(self.yy)
70
71 def miroirY(self):
72 """
73 change le signe de l'ordonnée ; utile comme l'axe y de l'écran
74 est dirigé vers le bas.
75 """
76 self.value = (self.value[0], - self.value[1])
77 return
78
79 def redresse(self, video):
80 """
81 ajuste les signes des coordonnées
82 @param video un objet qui a les propriétés sens_X et sens_Y (+-1)
83 """
84 self.value = (video.sens_X * self.value[0],
85 - video.sens_Y * self.value[1])
86 return
87
88 def __getitem__(self, i):
89 # print "Utilisation de vecteur.__getitem__ déconseillée"
90 return self.value[i]
91
92 def setValue(self, x=None, y=None):
93 if x == None:
94 x = self.value[0]
95 if y == None:
96 y = self.value[1]
97 self.value = (self.signif(float(x), self.precision),
98 self.signif(float(y), self.precision))
99
100 def rounded(self):
101 self.value = (math.floor(
102 self.value[0] + 0.5), math.floor(self.value[1] + 0.5))
103
104 def __add__(self, v):
105 x = self.xx + v.x
106 y = self.yy + v.y
107 return vecteur(x, y)
108
109 def __eq__(self, v):
110 return v is not None and self.xx == v.x and self.yy == v.y
111
112 def __sub__(self, v):
113 x = self.xx - v.x
114 y = self.yy - v.y
115 return vecteur(x, y)
116
117 def __mul__(self, v):
118 if type(v) == type(self):
119 # produit scalaire de deux vecteurs
120 return self.xx * v.x + self.yy + v.y
121 else:
122 # produit du vecteur par un nombre
123 x = float(v) * self.xx
124 y = float(v) * self.yy
125 return vecteur(self.signif(x, self.precision), self.signif(y, self.precision))
126
127 def __str__(self):
128 return "(%5f, %5f)" % (self.xx, self.yy)
129
130 def toIntStr(self):
131 return f"({round(self.x)}, {round(self.y)})"
132
133 def __repr__(self):
134 return "vecteur %s" % self
135
136 @property
137 def norme(self):
138 return math.sqrt(self.xx * self.xx + self.yy * self.yy)
139
140 @property
141 def anglePolaire(self):
142 return math.atan2(self.yy, self.xx)
143
144 def minXY(self, v):
145 if v == None:
146 return self
147 else:
148 if self.xx > v.x:
149 x = v.x
150 else:
151 x = self.xx
152 if self.yy > v.y:
153 y = v.y
154 else:
155 y = self.yy
156 return vecteur(x, y)
157
158 def maxXY(self, v):
159 if v == None:
160 return self
161 else:
162 if self.xx < v.x:
163 x = v.x
164 else:
165 x = self.xx
166 if self.yy < v.y:
167 y = v.y
168 else:
169 y = self.yy
170 return vecteur(x, y)
171
172 def rotate(self, angle, largeur, hauteur):
173 x1, y1 = self.xx, self.yy
174 if angle % 360 == 90:
175 return vecteur(hauteur-y1, x1)
176 elif angle % 360 == 270:
177 return vecteur(y1, largeur-x1)
178 elif angle % 360 == 0:
179 return vecteur(x1, y1)
180 elif angle % 360 == 180:
181 return vecteur(-x1, -y1)
182
183 def signif(self, x, digit):
184 if x == 0:
185 return 0
186 return round(x, digit - int(math.floor(math.log10(abs(x)))) - 1)
187
188 def homothetie(self, ratio):
189 return vecteur(self.xx*ratio, self.yy*ratio)
190
191 def toQPointF(self):
192 """
193 transformation en QPointF
194 """
195 return QPointF(self.xx, self.yy)
une classe pour des vecteurs 2D ; les coordonnées sont flottantes, et on peut accéder à celles-ci par...
Definition: vecteur.py:44
def toQPointF(self)
transformation en QPointF
Definition: vecteur.py:194
def signif(self, x, digit)
Definition: vecteur.py:183
def x(self)
Definition: vecteur.py:58
def manhattanLength(self)
Definition: vecteur.py:68
def redresse(self, video)
ajuste les signes des coordonnées
Definition: vecteur.py:83
def y(self)
Definition: vecteur.py:62
def miroirY(self)
change le signe de l'ordonnée ; utile comme l'axe y de l'écran est dirigé vers le bas.
Definition: vecteur.py:75