Rotation of 3d plane intoo a xy plane - python

I want to rotate a multiple points that are in the same 3D plane ax+by+cz=d,to a 2D plane xy. I was able to rotate the plane but not fully.
as you can see in this picture(the red points is circule in the xy plane, the yellow is the circule I want to rotate, and the red is the points are that I was able to rotate.
I tried to create the rotation matrix calculating the normal vector of the plane (a,b,c) and then calculating this rotation matrix! , by rotating the plane by the vector perpendicular to (a,b,c) and (0,0,1).
def grid2d_perplane(plane): # creates a 2d grid in a plane ax+by+cz=d by calculating a,b,c,d
p1 = copy.copy(plane[0])
p2 = copy.copy(plane[13])
p3 = copy.copy(plane[30])
# These two vectors are in the plane
v1 = p3 - p1
v2 = p2 - p1
# the cross product is a vector normal to the plane
cp = np.cross(v1, v2)
cp /= np.sqrt(cp[0]*cp[0] + cp[1]*cp[1] +cp[2]*cp[2])
a, b, c = cp
# This evaluates a * x3 + b * y3 + c * z3 which equals d
d = np.dot(cp, p3)
maxx = np.max(plane[:,0:1])
maxy = np.max(plane[:,1:2])
minx = np.min(plane[:,0:1])
miny = np.min(plane[:,1:2])
x = np.arange(minx*2,maxx*2,0.25)
y = np.arange(miny*2,maxy*2,0.25)
xx,yy= np.meshgrid(x,y)
zz = (d - a * xx - b * yy) / c
return xx, yy, zz, a , b , c, d
def transfor2d(p,a,b,c):
"The function will transform a set of points from a 3d plane to xy plane we have to keep the indexes intact for us to use later"
#TODO make it only rotation, and try to translate after wards
norm = np.sqrt(a**2+ b**2 +c **2)
cos = c /norm
theta = np.arccos(cos)
# if cos < 0:
# cos = -cos # this is a hack I have to test it again.
sen = np.sqrt((a**2+ b**2)/(norm**2))
theta1 = np.arcsin(sen)
print(theta,theta1)
u_one = b /norm
u_two = -a /norm
u_one_s = u_one**2
u_two_s = u_two**2
T11 = cos + (u_one_s*(1-cos))
T12 = ((u_one)*(u_two))*(1-cos)
T13 = u_two * sen
T21 = ((u_one)*(u_two))*(1-cos)
T22 = cos + (u_two_s*(1-cos))
T23 = -u_one * sen
T31 = -u_two * sen
T32 = u_one * sen
T33 = cos
matrix = np.zeros((3,3),np.float32)
matrix[0][0] = T11
matrix[0][1] = T12
matrix[0][2] = T13
matrix[1][0] = T21
matrix[1][1] = T22
matrix[1][2] = T23
matrix[2][0] = T31
matrix[2][1] = T32
matrix[2][2] = T33
m = matrix.dot(p)
return np.array([m[0],m[1],m[2]])
The expected results should to make the yellow points go to the red points. However the rotation matrix is not doing this. Can you see any errors that I'm making? I think that theoretically should work.

Related

Python given implicit equation, find points on that equation?

Context: Convert an .iges to .vtk.
I have the following equation Ax^2+Bxy+Cy^2+Dx+Ey+F=0 representing a conic section.
The parameters A~F are given. I want to find points on the conic section, so that I can connect them with lines, and make a mesh.
The reason I need the points instead of just using matplotlib Ellipse is because I'm creating a mesh not a plot.
It is in 3 dimension space, but I first get points on xy plane, and use affine transformation to send it to 3 dim.
Question: How do I find points given an implicit equation?
To avoid spending too much time on this, I wrote some code that seems to handle general ellipses. It can be expanded for other conics, depending on what is needed.
The code takes in the coefficients of a general quadratic equation of an ellipse and a number of desired points to be generated on the ellipse and generates a set of points on the ellipse.
import numpy as np
def equation(conic, points):
'''
equation of a conic with coefficients 'conic'
applied to a matrix number_of_points x 3 whose each row is the coordinates
of each point
'''
c = np.array(conic)
x = np.array([points[:,0]**2, points[:, 0]*points[:,1], points[:,1]**2, points[:,0], points[:,1], np.ones(points.shape[0])])
return c.dot(x)
def equation_to_matrix(eq):
'''
eq[0]*x**2 + eq[1]*x*y + eq[2]*y**2 + eq[3]*x + eq[4]*y + eq[5] = 0
'''
return np.array([[2*eq[0], eq[1], eq[3]],
[ eq[1], 2*eq[2], eq[4]],
[ eq[3], eq[4], 2*eq[5]]]) / 2
def solve_quadratic(a, b, c):
'''
solves
ax^2 + bx + c = 0
'''
D = b**2 - 4*a*c
D = np.sqrt(D)
return (-b-D)/(2*a), (-b+D)/(2*a)
def eigen2(S):
'''
solves the eigen-decomposition problem
for a 2x2 symmetric matrix
'''
k1, k2 = solve_quadratic(1, -S[0,0]-S[1,1], S[0,0]*S[1,1] - S[0,1]*S[1,0])
u1 = np.array([-S[0,1], S[0,0]-k1, 0])
u1 = u1 / np.sqrt(u1.dot(u1))
u2 = np.array([-u1[1], u1[0], 0])
return np.array([k1, k2]), np.array([u1, u2, np.array([0,0,1])]).T
def center(conic_matrix):
center = np.linalg.solve(conic_matrix, np.array([0,0,1]))
return center/center[2]
def find_rotation_and_translation(conic_matrix):
'''
conic = c[0]x^2 + c[1]*xy + c[2]*y^2 + c[3]*x + c[4]*y + c[5] = 0
the result is rotation U such that U.T C U = diag
'''
k, U = eigen2(conic_matrix)
U[:,2] = center(conic_matrix)
return U, k
def find_transform(conic):
C = equation_to_matrix(conic)
U, k = find_rotation_and_translation(C)
C = (U.T).dot(C.dot(U))
C = - C / C[2,2]
k = np.array([1/np.sqrt(C[0,0]), 1/np.sqrt(C[1,1]), 1])
return U.dot(np.diag(k))
def generate_points_on(conic, num_points):
'''
conic = [c[0], c[1], c[2], c[3], c[4], c[5]]
coefficients of the qudaratic equation:
conic: c[0]x^2 + c[1]*xy + c[2]*y^2 + c[3]*x + c[4]*y + c[5] = 0
result is the affine transformation (scaling, rotation, translation)
that maps the unit circle to the ellipse defined by the coefficients
'conic'
'''
cos_ = np.cos(2*np.pi* np.arange(0, num_points)/ num_points)
sin_ = np.sin(2*np.pi* np.arange(0, num_points)/ num_points)
U = find_transform(conic)
points = np.array([cos_, sin_, np.ones(num_points)])
return ((U.dot(points)).T)[:,[0,1]]
'''
Test:
'''
'''
Ellipse with equation whose coefficients are in the list E.
The ellipse has semi-major axes 2 and 1,
it is rotated 60 deg from the horizontal,
and its center is at (1, 4)
'''
E = [ 3.25, -2.59807621, 1.75, -23.40192379, 6.89230485, 39.35769515]
'''
U maps points from unit circle to points on E
'''
U = find_transform(E)
print(U)
'''
the set of points on the ellipse E
'''
p = generate_points_on(E, num_points = 20)
print(p)
'''
check that the points p lie on the ellipse E
'''
print(equation(E, p).round(10))
'''
plot
'''
fig = plt.figure()
ax = fig.add_subplot()
ax.plot(p[:,0], p[:,1], 'ro')
ax.set_aspect('equal')
plt.show()
The code below handles the case of a hyperbola. It largely adapts the code from here
import numpy as np
import matplotlib.pyplot as plt
def equation_to_matrix(eq):
'''
eq[0]*x**2 + eq[1]*x*y + eq[2]*y**2 + eq[3]*x + eq[4]*y + eq[5] = 0
'''
return np.array([[2*eq[0], eq[1], eq[3]],
[ eq[1], 2*eq[2], eq[4]],
[ eq[3], eq[4], 2*eq[5]]]) / 2
def hyp_params_from_general(coeffs):
# get the matrix of the quadratic equation
Aq = equation_to_matrix(coeffs)
# get the matrix of the quadratic form A33
A33 = Aq[:2, :2]
# determinant of A33
detA33 = np.linalg.det(A33)
if detA33 > 0:
raise ValueError('coeffs do not represent a hyperbola: det A33 must be negative!')
# get the center
x0 = -np.linalg.det(np.array([Aq[:2, 2], Aq[:2, 1]]).T) / detA33
y0 = -np.linalg.det(np.array([Aq[:2, 0], Aq[:2, 2]]).T) / detA33
# The semi-major and semi-minor axis lengths (these are not sorted).
# get discriminant of the conic section
delta = np.linalg.det(Aq)
# get the eigenvalues
k1, k2 = np.linalg.eigvals(A33)
k1isk2 = np.isclose(k1/k2, -1)
ap = np.sqrt(abs(delta/k1/detA33))
bp = np.sqrt(abs(delta/k2/detA33))
# Eccentricity.
fac = np.sqrt((Aq[0, 0] - Aq[1, 1])**2 + Aq[0, 1]**2)
if delta < 0:
nu = 1
else:
nu = -1
e = np.sqrt(2*fac/(nu*(Aq[0, 0] - Aq[1, 1]) + fac))
# slope of the asymptotes
if Aq[0, 0] == Aq[1, 1] and k1isk2:
m1 = 0.
m2 = np.nan
else:
m1 = Aq[0, 0]/(-Aq[0, 1] - np.sqrt(-detA33))
m2 = Aq[0, 0]/(-Aq[0, 1] + np.sqrt(-detA33))
# Sort the semi-major and semi-minor axis lengths but keep track of
# the original relative magnitudes of width and height.
width_gt_height = True
if ap < bp and not k1isk2:
width_gt_height = False
ap, bp = bp, ap
# The angle of anticlockwise rotation of the major-axis from x-axis.
if Aq[0, 1] == 0:
phi = 0 if Aq[0, 0] < Aq[1, 1] else np.pi/2
elif Aq[0, 0] == Aq[1, 1]:
phi = np.pi/4 # would divide by zero and arctan(inf) -> pi/4
if m1 > 0 and m2 > 0:
width_gt_height = True
else:# Aq[0, 0] > Aq[1, 1]:
phi = np.arctan(2*Aq[0, 1]/(Aq[0, 0] - Aq[1, 1])) / 2
if not width_gt_height:
# Ensure that phi is the angle to rotate to the semi-major axis.
phi += np.pi/2
phi = phi % np.pi
return x0, y0, ap, bp, phi, e, m1, m2, width_gt_height
def get_hyperbola_pts(params, npts=100, tmin=-1, tmax=1):
x0, y0, ap, bp, phi, m1, m2 = params
# A grid of the parametric variable, t.
t = np.linspace(tmin, tmax, npts)
# points
x = x0 + ap * np.cosh(t) * np.cos(phi) - bp * np.sinh(t) * np.sin(phi)
y = y0 + ap * np.cosh(t) * np.sin(phi) + bp * np.sinh(t) * np.cos(phi)
# asymptotes
ya1 = y0 + m1*(x - x0)
ya2 = y0 + m2*(x - x0)
return x, y, ya1, ya2
if __name__ == '__main__':
coeffs = [1., 6., -2., 3., 0., 0.]
x0, y0, ap, bp, phi, e, m1, m2, width_gt_height = hyp_params_from_general(coeffs)
print('x0, y0, ap, bp, phi, e, m1, m2, width_gt_height = ', x0, y0, ap, bp, phi, e, m1, m2)
x_, y_, ya1, ya2 = get_hyperbola_pts((x0, y0, ap, bp, phi, m1, m2), npts=250, tmin=-2, tmax=3)
fig, ax = plt.subplots(figsize=(16, 9))
ax.plot(x_, y_, marker='.', linewidth=0.5, c='r')
ax.plot(x_, ya1, marker='.', linewidth=0.2, c='b')
ax.plot(x_, ya2, marker='.', linewidth=0.2, c='b')
ax.grid(True, linestyle='--')

computing rotation and translation matrix with 3D and 2D Point correspondences

I have a set of 3D points and the correspondend point in 2D from a diffrent position.
The 2D points are on a 360° panorama. So i can convert them to polar -> (r,theta , phi ) with no information about r.
But r is just the distance of the transformed 3D Point:
[R|t]*xyz = xyz'
r = sqrt(xyz')
Then with the 3D point also in spherical coordinates, i can now search for R and t with this linear equation system:
x' = sin(theta) * cos(phi) * r
y' = sin(theta) * cos(phi) * r
z' = sin(theta) * cos(phi) * r
I get good results for tests with t=[0,0,0.5] and without any rotation. But if there is a rotation the results are bad.
Is this the correct approach for my problem?
How can I use solvepnp() without a camera Matrix (it is a panorama without distortion)?
I am using opt.least_squares to calculate R and t.
I solved it with two diffrent methods.
One is for small rotations and solves for R and t (12 parameter), the other method can compute even big rotations with Euler and t (6 parameter).
I am calling the opt.least_squares() two times with diffrent initial values and use the method with an better reprojection error.
The f.eul2rot is just a conversion between euler angles and the rotation matrix.
def sphere_eq(p):
xyz_points = xyz
uv_points = uv
#r11,r12,r13,r21,r22,r23,r31,r32,r33,tx,ty,tz = p
if len(p) == 12:
r11, r12, r13, r21, r22, r23, r31, r32, r33, tx, ty, tz = p
R = np.array([[r11, r12, r13],
[r21, r22, r23],
[r31, r32, r33]])
else:
gamma, beta, alpha,tx,ty,tz = p
E = [gamma, beta, alpha]
R = f.eul2rot(E)
pi = np.pi
eq_grad = ()
for i in range(len(xyz_points)):
# Point with Orgin: LASER in Cartesian and Spherical coordinates
xyz_laser = np.array([xyz_points[i,0],xyz_points[i,1],xyz_points[i,2]])
# Transformation - ROTATION MATRIX and Translation Vector
t = np.array([[tx, ty, tz]])
# Point with Orgin: CAMERA in Cartesian and Spherical coordinates
uv_camera = np.array(uv_points[i])
long_camera = ((uv_camera[0]) / w) * 2 * pi
lat_camera = ((uv_camera[1]) / h) * pi
xyz_camera = (R.dot(xyz_laser) + t)[0]
r = np.linalg.norm(xyz_laser + t)
x_eq = (xyz_camera[0] - (np.sin(lat_camera) * np.cos(long_camera) * r),)
y_eq = (xyz_camera[1] - (np.sin(lat_camera) * np.sin(long_camera) * r),)
z_eq = (xyz_camera[2] - (np.cos(lat_camera) * r),)
eq_grad = eq_grad + x_eq + y_eq + z_eq
return eq_grad
x = np.zeros(12)
x[0], x[4], x[8] = 1, 1, 1
initial_guess = [x,np.zeros(6)]
for p, x0 in enumerate(initial_guess):
x = opt.least_squares(sphere_eq, x0, '3-point', method='trf')
if len(x0) == 6:
E = np.resize(x.x[:4], 3)
R = f.eul2rot(E)
t = np.resize(x.x[4:], (3, 1))
else:
R = np.resize(x.x[:8], (3, 3))
E = f.rot2eul(R)
t = np.resize(x.x[9:], (3, 1))

Implementing a Runge-Kutta 4 method for 3 body problem in Python

I'm trying to implement an RK4 method in Python to solve for the Sun, Earth and Jupiter system. This code works for Halley's comet, when I have 4 ODEs to solve instead of 8, I've tried extending my method to 8 ODEs but now I just keep getting a straight line and have no idea why. Can someone please point out to me the stupid mistake I'm making, because I'm stumped as to why this isn't working. Here's the code:
import math
import numpy as np
import matplotlib.pyplot as plt
G = 6.67e-11
M = 1.989e+30 #mass of the sun
m1 = 5.972e+24 #mass of planet 1
m2 = 1.898e+27 #mass of planet 2
AU = 1.496e+11
a1 = 1.0*AU #distance from planet 1 to the sun
a2 = 5.2*AU #distance from planet 2 to the sun
#A = -(G*M)/(a1**3)
#B = -(G*M)/(a2**3)
##C = G/(abs(a2 - a1)**3)
#
#print("A = ", A, "B = ", B)
x_i1 = a1 #initial values for planet 1 in x and y direction
y_i1 = 0.0
v_x1i = 0.0
v_y1i = 3e+4
x_i2 = a2 #initial values for planet 2 in x and y direction
y_i2 = 0.0
v_x2i = 0.0
v_y2i = 13.07e+3
t_upper = 365*24*3600 #run the program to simulate a year
t = 0.0
t_i = 0.0
N = 10000
def f1(r, t): #function containing the relevant equations and arrays
x1 = r[0] #storing position and velocity components in x and y direction for planet 1
y1 = r[2]
v_x1 = r[1]
v_y1 = r[3]
x2 = r[4] #storing position and vecloity components in x and y direction for planet 2
y2 = r[6]
v_x2 = r[5]
v_y2 = r[7]
C = G/(((x1 - x2)**2 + (y1 - y2)**2)**1.5) #equations for 3 body problem, just to condense the maths
D = G/(((x2 - x1)**2 + (y2 - y1)**2)**1.5)
A = (G*M)/(a1**3)
B = (G*M)/(a2**3)
dvx1 = (A*x1) + ((C*m2)*(x1 - x2)) #Acceleration in x and y for each planet
dvy1 = (A*y1) + ((C*m2)*(y1 - y2))
dvx2 = (B*x2) - ((D*m1)*(x2 - x1))
dvy2 = (B*y2) - ((D*m1)*(y2 - y1))
return np.array([v_x1, dvx1, v_y1, dvy1, v_x2, dvx2, v_y2, dvy2]) #return array storing all the differential equations we want to solve
r = np.array([x_i1, v_x1i, y_i1, v_y1i, x_i2, v_x2i, y_i2, v_y2i], float) #define array with initial values
x_pnts1 = [x_i1] #array containing the position in x for planet 1
v_x_pnts1 = [v_x1i] #array containing the velocity in x for planet 1
y_pnts1 = [y_i1] #position in y for planet 1
v_y_pnts1 = [v_y1i] #velocity in y for planet 1
x_pnts2 = [x_i2] #same as above, but for planet 2
v_x_pnts2 = [v_x2i]
y_pnts2 = [y_i2]
v_y_pnts2 = [v_y2i]
t_values = [t_i]
h = t_upper/N
while t < t_upper:
k1 = h*f1(r, t) #RK4 routine, using the function defined above
k2 = h*f1(r + 0.5*k1, t+h/2)
k3 = h*f1(r + 0.5*k2, t+h/2)
k4 = h*f1(r + k3, t+h)
r += (k1 + 2*k2 + 2*k3 + k4)/6
x_pnts1.append(r[0]) #append the relevant arrays with the values from the RK4 routine
v_x_pnts1.append(r[1])
y_pnts1.append(r[2])
v_y_pnts1.append(r[3])
x_pnts2.append(r[4])
v_x_pnts2.append(r[5])
y_pnts2.append(r[6])
v_y_pnts2.append(r[7])
t += h
t_values.append(t)
aphelion_e = np.max(x_pnts1)
perihelion_e = abs(np.min(x_pnts1))
print("Perihelion of earth = ", perihelion_e/AU, "AU")
print("Aphelion of earth = ", aphelion_e/AU, "AU")
plt.plot(x_pnts1, y_pnts1)
plt.plot(0, 0, "o", 15)
plt.show()

cartesian coordinates : generate coordinates of 4 points around another point

I'd like to place 4 points around a point on a sphere (cartesian coordinates: x y z), it doesn't matter how far these 4 points are from the center point (straight line distance or spherical distance) but I'd like these 4 points to be the same distance D from the center point (ideally the 5 points should have a + or x shape, so one north, one south, one east and one south).
I could do it by changing one variable (x, y or z) then keeping another the same and calculating the last variable based on the formula x * x + y * y + z * z = radius * radius but that didn't give good results. I could also maybe use the pythagorean theorem to get the distance between each of the 4 points and the center but I think there is a better formula that I don't know (and couldn't find by doing my research).
Thank you.
Some math
AFAIU your problem is that you have a sphere and a point on the sphere and you want to add 4 more points on the same sphere that would form a kind of a cross on the surface of the sphere around the target point.
I think it is easier to think about this problem in terms of vectors. You have a vector from the center of the sphere to your target point V of size R. All the point lying on the distance d from the target point form another sphere. The crossing of two sphere is a circle. Obviously this circle lies in a plane that is orthogonal to V. Solving a simple system of equations you can find that the distance from the target point to that plane is d^2/(2*R). So the vector from the center of the original sphere to the center of the circle:
Vc = V * (1 - d^2/(2*R^2))
and the radius of that circle is
Rc = sqrt(d^2 - (d^2/(2*R))**2)
So now to select 4 points, you need to select two orthogonal unit vectors lying in that plane D1 and D2. Then 4 points would be Vc + Rc*D1, Vc - Rc*D1, Vc + Rc*D2, and Vc - Rc*D2. To do this you may first select D1 fixing z =0 and switch x and y in Vc
D1 = (Vy/sqrt(Vx^2+Vy^2), -Vx/sqrt(Vx^2+Vy^2), 0)
and then find D2 as a result of cross-product of V and D1. This will work unless unless Vx = Vy = 0 (i.e. V goes along the z-axis) but in that case you can select
D1 = (1,0,0)
D2 = (0,1,0)
Some code
And here is some Python code that implements that math:
def cross_product(v1, v2):
return (v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0])
def find_marks(sphereCenter, target, d):
lsc = list(sphereCenter)
lt0 = list(target)
lt1 = map(lambda c1, c0: (c1 - c0), lt0, lsc) # shift everything as if sphereCenter is (0,0,0)
rs2 = sum(map(lambda x: x ** 2, lt1)) # spehere radius**2
rs = rs2 ** 0.5
dv = d ** 2 / 2.0 / rs
dvf = d ** 2 / 2.0 / rs2
lcc = map(lambda c: c * (1 - dvf), lt1) # center of the circle in the orthogonal plane
rc = (d ** 2 - dv ** 2) ** 0.5 # orthogonal circle radius
relEps = 0.0001
absEps = relEps * rs
dir1 = (lt1[1], -lt1[0], 0) # select any direction orthogonal to the original vector
dl1 = (lt1[0] ** 2 + lt1[1] ** 2) ** 0.5
# if original vector is (0,0, z) then we've got dir1 = (0,0,0) but we can use (1,0,0) as our vector
if abs(dl1) < absEps:
dir1 = (rc, 0, 0)
dir2 = (0, rc, 0)
else:
dir1 = map(lambda c: rc * c / dl1, dir1)
dir2 = cross_product(lt1, dir1)
dl2 = sum(map(lambda c: c ** 2, dir2)) ** 0.5
dir2 = map(lambda c: rc * c / dl2, dir2)
p1 = map(lambda c0, c1, c2: c0 + c1 + c2, lsc, lcc, dir1)
p2 = map(lambda c0, c1, c2: c0 + c1 + c2, lsc, lcc, dir2)
p3 = map(lambda c0, c1, c2: c0 + c1 - c2, lsc, lcc, dir1)
p4 = map(lambda c0, c1, c2: c0 + c1 - c2, lsc, lcc, dir2)
return [tuple(p1), tuple(p2), tuple(p3), tuple(p4)]
For an extreme case
find_marks((0, 0, 0), (12, 5, 0), 13.0 * 2 ** 0.5)
i.e. for a circle of radius 13 with a center at (0,0,0), the target point lying on the big circle in the plane parallel to the xy-plane and d = sqrt(2)*R, the answer is
[(4.999999999999996, -12.000000000000004, 0.0),
(-5.329070518200751e-15, -2.220446049250313e-15, -13.0),
(-5.000000000000006, 12.0, 0.0),
(-5.329070518200751e-15, -2.220446049250313e-15, 13.0)]
So two points (2-nd and 4-th) are just two z-extremes and the other two are 90° rotations of the target point in the xy-plane which looks quite OK.
For a less extreme example:
find_marks((1, 2, 3), (13, 7, 3), 1)
which is the previous example with d reduced to 1 and with the original center moved to (1,2,3)
[(13.34882784191617, 6.06281317940119, 3.0),
(12.964497041420119, 6.985207100591716, 2.000739918710263),
(12.580166240924067, 7.907601021782242, 3.0),
(12.964497041420119, 6.985207100591716, 3.999260081289737)]
which also looks plausible

how to generate rectangle from a plane equation ax+by+cz=d?

given a plane equation, how can you generate four points that comprise a rectangle? I only have the plane equation ax+by+cz=d.
I am following the approach listed here Find Corners of Rectangle, Given Plane equation, height and width
#generate horizontal vector U
temp_normal=np.array([a,b,c])
temp_vertical=np.array([0,0,1])
U=np.cross(temp_normal, temp_vertical)
# for corner 3 and 4
neg_U=np.multiply([-1.0, -1.0, -1.0], U)
#generate vertical vector W
W=np.cross(temp_normal,U)
#for corner 2 and 4
neg_W=np.multiply([-1.0, -1.0, -1.0], W)
#make the four corners
#C1 = P0 + (width / 2) * U + (height / 2) * W
C1=np.sum([centroid,np.multiply(U, width_array),np.multiply(W, height_array)], axis=0)
corner1=C1.tolist()
#C2 = P0 + (width / 2) * U - (height / 2) * W
C2=np.sum([centroid,np.multiply(U, width_array),np.multiply(neg_W, height_array)], axis=0)
corner2=C2.tolist()
#C3 = P0 - (width / 2) * U + (height / 2) * W
C3=np.sum([centroid,np.multiply(neg_U, width_array),np.multiply(W, height_array)], axis=0)
corner3=C3.tolist()
#C4 = P0 - (width / 2) * U - (height / 2) * W
C4=np.sum([centroid,np.multiply(neg_U, width_array),np.multiply(neg_W, height_array)], axis=0)
self.theLw.WriteLine("C4 is " +str(type(C4))+" "+str(C4.tolist()))
corner4=C4.tolist()
corners_list.append([corner1, corner2, corner3, corner4])
Find a vector inside that plane using the equation. Find a second one inside that plane, perpendicular to the first one, using the cross-product (of the first and a normal vector to the plane). Then add these vectors (with +- signs, 4 possibilities) to generate 4 corners.
Edit: to help you a bit more:
(a,b,c) is the vector normal to the plane;
(0,0,d/c), (0,d/b,0) and (d/a,0,0) are points belonging to the plane, i.e. for instance b1 = (0,d/b,-d/c) is a vector tangent to the plane;
The cross-product of two vectors returns a vector that is perpendicular to both. So the product b2 = (a,b,c) x (0,d/b,-d/c) is a vector tangent to the plane, perpendicular to the other one. With that, you have constructed a normal basis of the plane [b1,b2].
Start from a point, say (0,0,d/c), and add b1+b2, b1-b2, -b1+b2, -b1-b2 to have 4 corners.
Ok here is the answer:
import numpy as np
a = 2; b = 3; c = 4; d = 5
n = np.array([a,b,c])
x1 = np.array([0,0,d/c])
x2 = np.array([0,d/b,0])
def is_equal(n,m):
return n-m < 1e-10
def is_on_the_plane(v):
return is_equal(v[0]*a + v[1]*b + v[2]*c, d)
assert is_on_the_plane(x1)
assert is_on_the_plane(x2)
# Get the normal basis
b1 = x2 - x1
b2 = np.cross(n, b1)
c1 = x1 + b1 + b2
c2 = x1 + b1 - b2
c3 = x1 - b1 + b2
c4 = x1 - b1 - b2
assert is_on_the_plane(c1)
assert is_on_the_plane(c2)
assert is_on_the_plane(c3)
assert is_on_the_plane(c4)
assert is_equal(np.dot(c1-c3, c1-x2), 0)
assert is_equal(np.dot(c2-c1, c2-c4), 0)
# etc. :
# c3 c1
#
# x1
#
# c4 c2
It is actually a square, but you can surely find out how to make it a less specific rectangle.

Categories