-
Notifications
You must be signed in to change notification settings - Fork 2
/
coordsTransform.py
executable file
·81 lines (63 loc) · 2.21 KB
/
coordsTransform.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
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 24 17:57:51 2018
@author: 286
WARNING: THIS IS QUICK AND DIRTY.
I DID A LITTLE TESTING BUT IT LIKELY NEEDS SOME WORK.
"""
import numpy as np
class CoordTransformer:
#
# A
#
# y
#
#
# O x
#
#
#
# B C
#
# Use as follows!
# import coordsTransform
# inst = coordsTransform.CoordTransformer([[0,50,10],[-50,-50,-10],[50,-50,-10]])
# print(inst.Transform([0,0,0]))
# print(inst.Transform([0,-50,0]))
# print(inst.Transform([0,0,50]))
m2 = None
newCenter = None
def __init__(self, ptsList):
# Convert pts list to np arrays
npPtsList = self.PtsToNPs(ptsList)
# Get new center
self.newCenter = self.GetCenter(npPtsList[0],npPtsList[1],npPtsList[2])
# Get new coordinate axes
newCoordAxes = self.NewCoordAxes(npPtsList[0],npPtsList[1],npPtsList[2])
self.m2 = np.array(newCoordAxes).T
# Main function to call
def Transform(self, coordsToTransform):
# Convert object points to np arrays
coordsToTransform = self.PtsToNPs(coordsToTransform)
# Compute transformed coords
newPt = self.newCenter + self.m2.dot(coordsToTransform)
return newPt # convert back to list for return
# Compute new coordinate axes given 3 points on the plane
# @ ptA, ptB, and ptC are numpy arrays of coords for 3 points
def NewCoordAxes(self, ptA, ptB, ptC):
ptO = self.newCenter
vectOA = ptA - ptO
vectOy = vectOA / np.linalg.norm(vectOA)
vectBC = ptC - ptB
vectOx = vectBC / np.linalg.norm(vectBC)
vectOz = np.cross(vectOx,vectOy)
# N2 is the cross product between two normalized vectors
return (vectOx, vectOy, vectOz)
# Convert list of points to np array
def PtsToNPs(self, pt):
return np.array(pt)
# Get the new origin - very simplistic, may want to replace
def GetCenter(self, npA, npB, npC):
midBC = (npB+npC)/2
center = (npA+midBC)/2
return center