-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.py
370 lines (283 loc) · 9.29 KB
/
profiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 8 15:35:57 2022
Sets profiles to be applied to beams and rails
@author: leonardo
"""
import numpy as np
import matplotlib.pyplot as plt
import helper_funcs as hf
from bodiesc import rigidBody
class profile(object):
"""Generic profile class."""
def __init__(self, name):
self.name = name
self.body = None
self.refPointCoordinates = np.zeros(2)
# print('Profile {} created.'.format(name))
def setParent(self, body):
"""
Set the parent body.
Parameters
----------
body : body
The body to which the profile belongs.
Returns
-------
None.
"""
self.parent = body
def copyMe(self):
"""
Create a copy of the profile.
Returns
-------
TYPE
DESCRIPTION.
"""
return profile(self.name)
class planarProfile(profile):
"""
Planar profile class.
Points are given in (x,y) form, aligned to the reference marker. This means
that the z axis of the reference marker is ignored, therefore the user
should set the marker accordingly.
"""
def __init__(self, name, fileName='', convPar=1):
"""
Initialize profile object.
Parameters
----------
name : string
Name of the profile.
fileName : string, optional
Path to the profile file.
convPar : integer, optional
Convexity parameter. Used to make spline interpolations. The default is 1.
Returns
-------
None.
"""
super().__init__(name)
self.convPar = convPar
if fileName != '':
self.setProfilePointsFromFile(fileName)
def setProfilePointsFromFile(self, fileName, colDelimiter=';'):
"""
Load data points from a text file (in csv-like format).
Parameters
----------
fileName : TYPE
Text file name address.
colDelimiter : TYPE, optional
File delimiter. The default is ';'.
Returns
-------
None.
"""
self.setProfilePoints(np.genfromtxt(fileName, delimiter=colDelimiter))
def setProfilePoints(self, pArray):
self.points = pArray
self.convexSubsets = []
pShape = self.points.shape
if pShape[1] > pShape[0]:
self.points.tranpose()
self.nPoints = self.points.shape[0]
def centerProfile(self):
shift = np.zeros(2)
shift[0] = 0.5 * (self.points[:, 0].max() + self.points[:, 0].min())
shift[1] = self.points[:, 1].max()
self.points -= shift
def offsetPoints(self, offset):
"""
Offsets all points.
Receives the offset vector [dx,dy] and offsets all points with
this vector. This permanently changes the position of the points
and is supposed to be used to adjust the initial configuration.
To get current point positions considering the reference marker,
use function getCurrentPosition().
Returns
-------
None.
"""
self.points += offset
def mirror(self, ax=0):
self.points[:, ax] *= -1
def mirrorVert(self):
"""
Mirror points around the vertical axis.
Returns
-------
None.
"""
self.mirror(0)
def mirrorHoriz(self):
"""
Mirror points around the horizontal axis.
Returns
-------
None.
"""
self.mirror(1)
def rotatePoints(self, angleInRad):
"""
Apply rotation to profile points.
This method rotates all points with respect to the origin using a
angleInRad angle. Then the profile points are update to the rotated
values.
Parameters
----------
angleInRad : double
The rotation angle.
Returns
-------
None.
"""
R = np.array([[np.cos(angleInRad), np.sin(angleInRad)],
[-np.sin(angleInRad), np.cos(angleInRad)]])
self.points = self.points.dot(R)
def plotMe(self, ax=None):
if ax == None:
ax = plt.figure().add_subplot(projection='3d')
currentPoints = self.getCurrentPosition()
ax.plot(currentPoints[:, 0], currentPoints[:, 1], currentPoints[:, 2])
ax.grid(True)
return ax
def plotSubsets(self, ax=None):
self.createConvexSubsets()
if ax == None:
ax = plt.axes()
for cs in self.convexSubsets:
ax.plot(cs[:, 0], cs[:, 1])
ax.grid(True)
def createConvexSubsets(self):
self.convexSubsets = []
rot = self.convPar
currSet = []
currSet.append(self.points[0])
for i in range(1, self.nPoints-1):
firstPoint = currSet[0]
currPoint = self.points[i]
prevPoint = self.points[i-1]
nextPoint = self.points[i+1]
npm1np = currPoint - prevPoint
npnpp1 = nextPoint - currPoint
n0np = currPoint - firstPoint
angSign = np.sign(np.cross(npm1np, npnpp1))
currSet.append(self.points[i])
if angSign * rot < 0.01:
self.convexSubsets.append(np.array(currSet))
currSet = []
currSet.append(self.points[i])
self.convexSubsets.append(np.array(currSet))
return self.convexSubsets
def setReferenceMarker(self, _marker):
"""
Sets the reference marker of the profile.
This function gets the reference marker, which idealy belongs to the
parent body, and sets it to the profile.
The purpose of this function is to be called by the Body.addProfile
method.
"""
self.referenceMarker = _marker
def getCurrentPosition(self):
"""
Obtain the position of the profile on the parent body reference system.
Using an affine transformation, returns the position of the profile
points on the parent body reference system.
Returns
-------
currentPoints : TYPE
DESCRIPTION.
"""
return (self.points.dot(self.referenceMarker.orientation[0:2, :])
+ self.referenceMarker.position)
def find_convex_hull(self, points):
"""
Find the indices of the points that form the convex hull.
Parameters
----------
points : TYPE
DESCRIPTION.
Returns
-------
TYPE
DESCRIPTION.
"""
# Sort the points lexicographically
sorted_points = np.lexsort(points.T)
# Create arrays to hold the upper and lower hulls
lower_hull = [sorted_points[0]]
upper_hull = [sorted_points[0]]
# Compute the lower and upper hulls
for point in sorted_points[1:]:
# Update the lower hull
while len(lower_hull) > 1 and np.cross(
points[lower_hull[-1]] - points[lower_hull[-2]],
points[point] - points[lower_hull[-2]]
) <= 0:
lower_hull.pop()
lower_hull.append(point)
# Update the upper hull
while len(upper_hull) > 1 and np.cross(
points[upper_hull[-1]] - points[upper_hull[-2]],
points[point] - points[upper_hull[-2]]
) >= 0:
upper_hull.pop()
upper_hull.append(point)
# Combine the lower and upper hulls
convex_hull = lower_hull + upper_hull[1:-1][::-1]
return points[convex_hull]
def setConvexHull(self):
self.convexHull = self.find_convex_hull(self.points)
return self.convexHull
if __name__ == '__main__':
b = rigidBody('Trilho')
w = planarProfile('wheel')
r = planarProfile('rail', convPar=-1)
b.addProfile(r)
w.setProfilePointsFromFile('./design2.pro')
r.setProfilePointsFromFile('./tr68.pro')
r.centerProfile()
r.rotatePoints(np.arctan(1/40))
w.mirrorHoriz()
w.offsetPoints(np.array([-0.08, 0.0032]))
ax = plt.gca()
# w.plotMe(ax)
# r.plotMe(ax)
ax.axis('equal')
ch = r.setConvexHull()
wr = wheelRailContact()
wr.setWheel(w)
wr.setRail(r)
pw, pr, iw, ir, n, g = wr.searchContactPoint()
# ax.plot(*pw,'o')
# ax.plot(*pr,'o')
# ax.quiver(pr[0],pr[1],*n)
w.createConvexSubsets()
import gjk
rail = 0
wheel = 1
cSubsets = {rail: None, wheel: None}
cPoints = {rail: None, wheel: None}
minDist = np.inf
plt.fill(ch[:, 0], ch[:, 1], edgecolor='blue')
for rSubset in [ch]:
for wSubset in w.convexSubsets:
if rSubset[-1, 0] > wSubset[1, 0]:
pass
pRail, pWheel, n, d = gjk.gjk(
rSubset, wSubset, np.array([0., -1.]))
plt.plot(wSubset[:, 0], wSubset[:, 1])
plt.arrow(pWheel[0], pWheel[1], pRail[0] -
pWheel[0], pRail[1]-pWheel[1])
# print(d)
if d < minDist:
minDist = d
cSubsets[rail] = rSubset
cSubsets[wheel] = wSubset
cPoints[rail] = pRail
cPoints[wheel] = pWheel
cNormal = n
print(minDist)