#include <Segment.h>

Public Member Functions | |
| Segment () | |
| classical constructor | |
| Segment (const V_3D &start_point, const V_3D &end_point) | |
| direct constructor | |
| Segment (const Segment &s) | |
| Copy constructor. | |
| ~Segment () | |
| destructor | |
| int | set (const V_3D x0, const V_3D x1) |
| Set the values. | |
| const V_3D & | get_start () const |
| get the first point | |
| const V_3D & | get_last () const |
| get the last point | |
| V_3D | unit_vector () const |
| get the vector (x0,x1) / ||(x0,x1)|| | |
| int | flip () |
| inverse the segment (first <-> last) | |
| int | destroy () |
| empty the values | |
| double | length () const |
| return the length of the segment | |
| V_3D | closest_point (const V_3D &x) const |
| return the closest point of x on the segment | |
| double | distance_to_point (const V_3D &x) const |
| return the distance of point x to the closest point on the segment | |
| Segment | closest_to_segment (const Segment &s) const |
| return the segment of minimal length between two segments | |
| double | distance_to_segment (const Segment &s) const |
| get the minimal distance between the two segments | |
| V_3D | plane_intersection (const V_3D &n, const V_3D &x0, int *type) const |
| Intersection with the plane of equation <n,x-x0>=0. | |
| V_3D | operator() (int index) const |
| direct access operator | |
| V_3D & | operator() (int index) |
| direct access operator | |
| V_3D | operator[] (int index) const |
| direct access operator | |
| V_3D | operator[] (int index) |
| direct access operator | |
Private Attributes | |
| V_3D | x0 |
| First and last point. | |
| V_3D | x1 |
Friends | |
| ostream & | operator<< (ostream &flux, const Segment &s) |
| Print operator. | |
Calculation for embedeed Segment.
Definition at line 38 of file Segment.h.
| Segment::Segment | ( | ) |
| Segment::Segment | ( | const Segment & | s | ) |
| Segment::~Segment | ( | ) |
destructor
Definition at line 10 of file Segment.cpp.
References destroy().
00010 {destroy();}

Set the values.
Definition at line 12 of file Segment.cpp.
Referenced by Polygon::closest_point(), and closest_to_segment().

| const V_3D & Segment::get_start | ( | ) | const |
get the first point
Definition at line 13 of file Segment.cpp.
References x0.
Referenced by closest_to_segment(), OpenGL_drawer::draw_segment(), and operator<<().
00013 {return x0;}

| const V_3D & Segment::get_last | ( | ) | const |
get the last point
Definition at line 14 of file Segment.cpp.
References x1.
Referenced by closest_to_segment(), OpenGL_drawer::draw_segment(), and operator<<().
00014 {return x1;}

| V_3D Segment::unit_vector | ( | ) | const |
get the vector (x0,x1) / ||(x0,x1)||
Definition at line 19 of file Segment.cpp.
References V_3D::normalized(), x0, and x1.
Referenced by closest_point().
00020 { 00021 V_3D u=x1-x0; 00022 u = u.normalized(); 00023 return u; 00024 }


| int Segment::flip | ( | ) |
| int Segment::destroy | ( | ) |
empty the values
Definition at line 17 of file Segment.cpp.
References V_3D::set(), x0, and x1.
Referenced by ~Segment().


| double Segment::length | ( | ) | const |
return the length of the segment
Definition at line 26 of file Segment.cpp.
Referenced by closest_point(), and distance_to_segment().

return the closest point of x on the segment
Definition at line 29 of file Segment.cpp.
References length(), unit_vector(), x0, and x1.
Referenced by Triangle::closest_point(), Polygon::closest_point(), and distance_to_point().
00030 { 00031 V_3D u=unit_vector(); 00032 double proj=0.0; 00033 proj = (x-x0).dot(u); 00034 if(proj<0) 00035 return x0; 00036 else if(proj>length()) 00037 return x1; 00038 else 00039 return x0+proj*u; 00040 }


| double Segment::distance_to_point | ( | const V_3D & | x | ) | const |
return the distance of point x to the closest point on the segment
Definition at line 42 of file Segment.cpp.
References closest_point().
Referenced by Polygon::closest_point().
00043 {return (x-closest_point(x)).norm();}


return the segment of minimal length between two segments
There is only 2 posibilities 1. The closest point links the extremities 2. The closest point is the perpendicular to both segments
(The first point of the returned segment belongs to the current segment)
Definition at line 47 of file Segment.cpp.
References V_3D::dot(), get_last(), get_start(), and set().
Referenced by distance_to_segment().
00048 { 00049 00050 // 1. Search the closest position between the two lines 00051 // = line perpendicular to both segments 00052 //****************************************************// 00053 00054 00055 // useful vectors 00056 V_3D u1,u2; 00057 V_3D A12; 00058 00059 u1 = get_last()-get_start(); 00060 u2 = s.get_last()-s.get_start(); 00061 A12 = s.get_start()-get_start(); 00062 00063 double det=0.0; 00064 det = powf(u1.dot(u2),2.0) - u1.dot(u1)*u2.dot(u2); 00065 00066 // check if the lines are parallel 00067 double epsilon=0.00001; 00068 if(fabs(det)<epsilon) 00069 {printf("Error in closest_point_in_segment, lines are almost parallel\n");exit(-1);} 00070 00071 // distance of the intersection 00072 double s1=0.0,s2=0.0; 00073 s1 = 1/det*(-(A12.dot(u1))*(u2.dot(u2)) + (A12.dot(u2))*(u1.dot(u2)) ); 00074 s2 = 1/det*(-(A12.dot(u1))*(u2.dot(u1)) + (A12.dot(u2))*(u1.dot(u1)) ); 00075 00076 V_3D A1=get_start(),A2=s.get_start(); 00077 V_3D P1;P1 = A1 + s1*u1; 00078 V_3D P2;P2 = A2 + s2*u2; 00079 00080 // check if the points (P1,P2) are inside the two segments 00081 double dot_p1=0.0,dot_p2=0.0; 00082 dot_p1 = (P1-A1).dot(u1); 00083 dot_p2 = (P2-A2).dot(u2); 00084 00085 Segment seg; 00086 int is_valid_p1=0,is_valid_p2=0; 00087 if(dot_p1>=0 && dot_p1<=u1.dot(u1)) 00088 is_valid_p1=1; 00089 if(dot_p2>=0 && dot_p2<=u2.dot(u2)) 00090 is_valid_p2=1; 00091 00092 if(is_valid_p1==1 && is_valid_p2==1) 00093 seg.set(P1,P2); 00094 else 00095 { 00096 // else the closest points are at the extremities 00097 // 9 possibilities in this cases: 00098 V_3D y0[3]={A1,A1+u1,P1}; 00099 V_3D y1[3]={A2,A2+u2,P2}; 00100 double min_dist=99999.99; 00101 double current_dist=0.0; 00102 int min_k1=-1,min_k2=-1; 00103 for(int k1=0;k1<3;k1++) 00104 for(int k2=0;k2<3;k2++) 00105 { 00106 current_dist=(y0[k1]-y1[k2]).norm(); 00107 if(current_dist<min_dist) 00108 if(k1!=2 || (k1==2 && is_valid_p1==1)) 00109 if(k2!=2 || (k2==2 && is_valid_p2==1)) 00110 {min_dist=current_dist;min_k1=k1;min_k2=k2;} 00111 } 00112 00113 // 9 cases 00114 if(min_k1==0 && min_k2==0) {seg.set(A1,A2);} 00115 else if(min_k1==0 && min_k2==1) {seg.set(A1,A2+u2);} 00116 else if(min_k1==0 && min_k2==2) {seg.set(A1,P2);} 00117 00118 else if(min_k1==1 && min_k2==0) {seg.set(A1+u1,A2);} 00119 else if(min_k1==1 && min_k2==1) {seg.set(A1+u1,A2+u2);} 00120 else if(min_k1==1 && min_k2==2) {seg.set(A1+u1,P2);} 00121 00122 else if(min_k1==2 && min_k2==0) {seg.set(P1,A2);} 00123 else if(min_k1==2 && min_k2==1) {seg.set(P1,A2+u2);} 00124 else if(min_k1==2 && min_k2==2) {seg.set(P1,P2);} 00125 00126 } 00127 00128 return seg; 00129 00130 00131 }


| double Segment::distance_to_segment | ( | const Segment & | s | ) | const |
get the minimal distance between the two segments
based on the function closest_point_in_segment
Definition at line 158 of file Segment.cpp.
References closest_to_segment(), and length().
00159 {Segment closest = closest_to_segment(s);return closest.length();}

Intersection with the plane of equation <n,x-x0>=0.
The plane is defined by its normal n, and a point x0, so its equation is <n,x-x0>=0 4 possibilities: _ no intersections (type 0) _ plane intersect on one interior points (type 1) _ plane intersect on a vertex (type 2) _ plane contains the segment (type 3)
Solving the system <n,x-x0>=0 & A+t*u=x leads to t=<n,x0-A>/<n,u>
Definition at line 164 of file Segment.cpp.
References V_3D::dot(), x0, and x1.
Referenced by Triangle::plane_intersection().
00165 { 00166 00167 double epsilon=0.0001; 00168 //first check if the segment is parallel to the plane 00169 V_3D AB=x1-x0; 00170 if(fabs(AB.dot(n))<epsilon) 00171 { 00172 //plane is \\ to the segment 00173 //now test if the plane pass through the line 00174 if(n.dot(_x0-x0)<epsilon)//pass through 00175 { 00176 *type=3; 00177 return V_3D(-1,-1,-1); 00178 } 00179 else//no intersection 00180 { 00181 *type=0; 00182 return V_3D(-1,-1,-1); 00183 } 00184 } 00185 00186 //We are sure that the plane is not \\ to the segment 00187 double t=n.dot(_x0-x0)/AB.dot(n); 00188 00189 V_3D intersection=x0+t*AB; 00190 00191 00192 00193 //check if the intersection is inside the segment 00194 if(t>=0 && t<=1) 00195 { 00196 //check if its close to a vertex or not 00197 if( (intersection-x0).norm()<epsilon || (intersection-x1).norm()<epsilon) 00198 *type=2; 00199 else 00200 *type=1; 00201 return intersection; 00202 } 00203 00204 //else outside the segment => no intersection 00205 *type=0; 00206 return intersection; 00207 00208 }


| V_3D Segment::operator() | ( | int | index | ) | const |
| V_3D & Segment::operator() | ( | int | index | ) |
| V_3D Segment::operator[] | ( | int | index | ) | const |
| V_3D Segment::operator[] | ( | int | index | ) |
| ostream& operator<< | ( | ostream & | flux, | |
| const Segment & | s | |||
| ) | [friend] |
V_3D Segment::x0 [private] |
First and last point.
Definition at line 139 of file Segment.h.
Referenced by closest_point(), destroy(), flip(), get_start(), length(), operator()(), operator[](), plane_intersection(), Segment(), set(), and unit_vector().
V_3D Segment::x1 [private] |
Definition at line 139 of file Segment.h.
Referenced by closest_point(), destroy(), flip(), get_last(), length(), operator()(), operator[](), plane_intersection(), Segment(), set(), and unit_vector().
1.5.6