00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <math.h>
00026
00027 #include <gfx/Quad3>
00028
00029 #include <gfx/Plane>
00030
00031 using gfx::Quad3;
00032 using gfx::Point3;
00033 using gfx::Segment3;
00034 using gfx::Triangle3;
00035 using gfx::Plane;
00036
00037
00038 Quad3::Quad3(const Point3& c1, const Point3& c2, const Point3& c3, const Point3& c4)
00039 : c1(c1), c2(c2), c3(c3), c4(c4)
00040 {
00041
00042 Plane p(c1,c2,c3);
00043 Assertm(p.contains(c4),"corners are coplanar");
00044 }
00045
00046
00047 Real Quad3::distanceTo(const Point3& p) const
00048 {
00049 return (p-pointClosestTo(p)).length();
00050 }
00051
00052
00053 Point3 Quad3::pointClosestTo(const Point3& p) const
00054 {
00055 Triangle3 t1(c1,c2,c3);
00056 Triangle3 t2(c3,c4,c1);
00057
00058 Point3 p1 = t1.pointClosestTo(p);
00059 Point3 p2 = t2.pointClosestTo(p);
00060
00061 if ((p-p1).norm() < (p-p2).norm())
00062 return p1;
00063 else
00064 return p2;
00065 }
00066
00067
00068 Segment3 Quad3::shortestSegmentBetween(const Segment3& s) const
00069 {
00070 Triangle3 t1(c1,c2,c3);
00071 Triangle3 t2(c3,c4,c1);
00072
00073 Segment3 s1 = t1.shortestSegmentBetween(s);
00074 Segment3 s2 = t2.shortestSegmentBetween(s);
00075
00076 if (s1.norm() < s2.norm())
00077 return s1;
00078 else
00079 return s2;
00080 }
00081
00082
00083 Real Quad3::distanceTo(const Segment3& s) const
00084 {
00085 return shortestSegmentBetween(s).length();
00086 }
00087
00088
00089 Segment3 Quad3::shortestSegmentBetween(const Triangle3& t) const
00090 {
00091 Triangle3 t1(c1,c2,c3);
00092 Triangle3 t2(c3,c4,c1);
00093
00094 Segment3 s1 = t1.shortestSegmentBetween(t);
00095 Segment3 s2 = t2.shortestSegmentBetween(t);
00096
00097 if (s1.norm() < s2.norm())
00098 return s1;
00099 else
00100 return s2;
00101 }
00102
00103
00104 Real Quad3::distanceTo(const Triangle3& t) const
00105 {
00106 return shortestSegmentBetween(t).length();
00107 }
00108
00109
00110 Segment3 Quad3::shortestSegmentBetween(const Quad3& q) const
00111 {
00112 Triangle3 t1(c1,c2,c3);
00113 Triangle3 t2(c3,c4,c1);
00114
00115 Segment3 s1 = q.shortestSegmentBetween(t1);
00116 Segment3 s2 = q.shortestSegmentBetween(t2);
00117
00118 if (s1.norm() < s2.norm())
00119 return s1;
00120 else
00121 return s2;
00122 }
00123
00124
00125 Real Quad3::distanceTo(const Quad3& q) const
00126 {
00127 return shortestSegmentBetween(q).length();
00128 }