Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / modules / tools / zprobe / Plane3D.cpp
CommitLineData
0e44e7d7
JM
1#include "Plane3D.h"
2
3Plane3D::Plane3D(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
4{
5 // get the normal of the plane
6 Vector3 ab = v1.sub(v2);
7 Vector3 ac = v1.sub(v3);
8
9 Vector3 cp = ab.cross(ac);
10 normal = cp.unit();
11
12 // ax+by+cz+d=0
13 // solve for d
ff8ade36 14 d = -normal.dot(v1);
0e44e7d7
JM
15}
16
230079d4
JM
17typedef union { float f; uint32_t u; } conv_t;
18// ctor used to restore a saved plane
19Plane3D::Plane3D(uint32_t a, uint32_t b, uint32_t c, uint32_t d)
20{
21 conv_t ca, cb, cc, cd;
22 ca.u= a; cb.u= b; cc.u= c; cd.u= d;
bfcf42fe 23 this->normal = Vector3(ca.f, cb.f, cc.f);
230079d4
JM
24 this->d= cd.f;
25}
26
27void Plane3D::encode(uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
28{
29 conv_t ca, cb, cc, cd;
30 ca.f= this->normal[0];
31 cb.f= this->normal[1];
32 cc.f= this->normal[2];
33 cd.f= this->d;
34 a= ca.u; b= cb.u; c= cc.u; d= cd.u;
35}
36
0e44e7d7
JM
37// solve for z given x and y
38// z= (-ax - by - d)/c
39float Plane3D::getz(float x, float y)
40{
41 return ((-normal[0] * x) - (normal[1] * y) - d) / normal[2];
42}
43
44Vector3 Plane3D::getNormal() const
45{
46 return normal;
47}