A class to manipulate segments given by two points of R3. More...
#include <MC_segment.hpp>

Public Member Functions | |
| MC_segment () | |
| classical constructor | |
| MC_segment (const MC_v3d &start_point, const MC_v3d &end_point) | |
| direct constructor | |
| MC_segment (const MC_segment &s) | |
| Copy constructor. | |
| ~MC_segment () | |
| MC_v3d | unit_vector () const |
| get the vector (x0,x1) / ||(x0,x1)|| | |
| MC_v3d | vector () const |
| get the vector x1-x0 | |
| MC_segment | fliped () const |
| inverse the segment (first <-> last) | |
| double | length () const |
| return the length of the segment | |
| MC_v3d | closest_point (const MC_v3d &x) const |
| return the closest point of x on the segment | |
| double | distance_to_point (const MC_v3d &x) const |
| return the distance of point x to the closest point on the segment | |
| MC_segment | closest_to_segment (const MC_segment &s) const |
| return the segment of minimal length between two segments | |
| double | distance_to_segment (const MC_segment &s) const |
| get the minimal distance between the two segments | |
| MC_v3d | plane_intersection (const MC_v3d &n, const MC_v3d &x0, int *type=0) const |
| Intersection with the plane of equation <n,x-x0>=0. | |
| bool | is_belonging (const MC_v3d &x, int *type=0) const |
| check if a point belongs to the segment | |
| bool | is_aligned (const MC_v3d &x) const |
| check if a point belongs to the direction of the line | |
| double | relative_position (const MC_v3d &x, bool *is_aligned=0) const |
| if a point is aligned with the direction, get its parameterization t such that (x0,x)=t (x0,x1) | |
| void | assert_bounds (const int &u) const |
| assert the index is either 0 or 1 | |
| MC_v3d | operator() (const int &index) const |
| direct access operator | |
| MC_v3d & | operator() (const int &index) |
| direct access operator | |
| MC_v3d | operator[] (const int &index) const |
| direct access operator | |
| MC_v3d & | operator[] (const int &index) |
| direct access operator | |
| MC_v3d | value (const double &t) const |
| get linear interpolation at time t (parameterization bw [0,1]) | |
| MC_v3d_vector | value (const MC_double_vector &t) const |
| get linear interpolation at time t (parameterization bw [0,1]) | |
| std::pair< MC_v3d_vector, MC_double_vector > | linear_sampling_intervals (const double &d_L) const |
| subsampling every d_L | |
| std::pair< MC_v3d_vector, MC_double_vector > | linear_sampling (const int &N) const |
| subsampling in N samples | |
Static Public Member Functions | |
| static std::vector< MC_segment > | build_segment_vector (const MC_v3d_vector &vertices, const std::vector< MC_int_pair > &constraints) |
| build a vector of segment from a point_set and a vector of index | |
Private Attributes | |
| MC_v3d | x0 |
| The internal data structure is two MC_v3d. | |
| MC_v3d | x1 |
Friends | |
| MC_segment | operator+ (const MC_segment &s, const MC_v3d &x) |
| translate the segment | |
| MC_segment | operator+ (const MC_v3d &x, const MC_segment &s) |
| translate the segment | |
| MC_segment | operator* (const MC_segment &s, const double &a) |
| scale the segment | |
| MC_segment | operator* (const double &a, const MC_segment &s) |
| scale the segment | |
| std::ostream & | operator<< (std::ostream &stream, const MC_segment &s) |
A class to manipulate segments given by two points of R3.
Definition at line 39 of file MC_segment.hpp.
| mesh_conv::MC_segment::MC_segment | ( | ) |
classical constructor
Definition at line 11 of file MC_segment.cpp.
Referenced by build_segment_vector(), closest_to_segment(), and fliped().

| mesh_conv::MC_segment::MC_segment | ( | const MC_segment & | s | ) |
| mesh_conv::MC_segment::~MC_segment | ( | ) |
| void mesh_conv::MC_segment::assert_bounds | ( | const int & | u | ) | const |
assert the index is either 0 or 1
Definition at line 209 of file MC_segment.cpp.
Referenced by operator()(), and operator[]().
00210 { 00211 if(u!=0 && u!=1) 00212 {std::cout<<"Error in MC_segment::assert_bounds("<<u<<"), value should be 0 or 1"<<std::endl;exit(-1);} 00213 }

| std::vector< MC_segment > mesh_conv::MC_segment::build_segment_vector | ( | const MC_v3d_vector & | vertices, | |
| const std::vector< MC_int_pair > & | constraints | |||
| ) | [static] |
build a vector of segment from a point_set and a vector of index
Definition at line 265 of file MC_segment.cpp.
References MC_segment(), and mesh_conv::MC_v3d_vector::size().
00266 { 00267 unsigned int N_vertices=vertices.size(); 00268 std::vector<MC_segment> v_seg; 00269 for(int k=0,N=constraints.size();k<N;++k) 00270 { 00271 const MC_int_pair& index=constraints[k]; 00272 if(index[0]>=N_vertices || index[1]>=N_vertices) 00273 {std::cout<<"Error in MC_segment::build_segment_vector(), index "<<index<<" is too large compared to N_vertex="<<N_vertices<<" at k="<<k<<std::endl;exit(-1);} 00274 v_seg.push_back(MC_segment(vertices[index[0]],vertices[index[1]])); 00275 } 00276 return v_seg; 00277 }

return the closest point of x on the segment
Definition at line 25 of file MC_segment.cpp.
References length(), unit_vector(), x0, and x1.
Referenced by mesh_conv::MC_triangle::closest_point(), mesh_conv::MC_polygon::closest_point(), and distance_to_point().
00026 { 00027 MC_v3d u=unit_vector(); 00028 double proj=0.0; 00029 proj = (x-x0).dot(u); 00030 if(proj<0) 00031 return x0; 00032 else if(proj>length()) 00033 return x1; 00034 else 00035 return x0+proj*u; 00036 }


| MC_segment mesh_conv::MC_segment::closest_to_segment | ( | const MC_segment & | s | ) | const |
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 39 of file MC_segment.cpp.
References mesh_conv::MC_v3d::dot(), MC_segment(), vector(), and x0.
Referenced by distance_to_segment().
00040 { 00041 //****************************************************// 00042 // 1. Search the closest position between the two lines 00043 // = line perpendicular to both segments 00044 //****************************************************// 00045 00046 00047 // useful vectors 00048 MC_v3d u1,u2; 00049 MC_v3d A12; 00050 00051 u1 = vector(); 00052 u2 = s.vector(); 00053 A12 = s[0]-x0; 00054 00055 double det=0.0; 00056 det = powf(float(u1.dot(u2)),2.0) - u1.dot(u1)*u2.dot(u2); 00057 00058 // check if the lines are parallel 00059 double epsilon=0.00001; 00060 if(fabs(det)<epsilon) 00061 {std::cout<<"Error in MC_segment::closest_to_segment(), lines are almost parallel"<<std::endl;exit(-1);} 00062 00063 // distance of the intersection 00064 double s1=0.0,s2=0.0; 00065 s1 = 1/det*(-(A12.dot(u1))*(u2.dot(u2)) + (A12.dot(u2))*(u1.dot(u2)) ); 00066 s2 = 1/det*(-(A12.dot(u1))*(u2.dot(u1)) + (A12.dot(u2))*(u1.dot(u1)) ); 00067 00068 MC_v3d A1=x0,A2=s[0]; 00069 MC_v3d P1;P1 = A1 + s1*u1; 00070 MC_v3d P2;P2 = A2 + s2*u2; 00071 00072 // check if the points (P1,P2) are inside the two segments 00073 double dot_p1=0.0,dot_p2=0.0; 00074 dot_p1 = (P1-A1).dot(u1); 00075 dot_p2 = (P2-A2).dot(u2); 00076 00077 MC_segment seg; 00078 int is_valid_p1=0,is_valid_p2=0; 00079 if(dot_p1>=0 && dot_p1<=u1.dot(u1)) 00080 is_valid_p1=1; 00081 if(dot_p2>=0 && dot_p2<=u2.dot(u2)) 00082 is_valid_p2=1; 00083 00084 if(is_valid_p1==1 && is_valid_p2==1) 00085 seg=MC_segment(P1,P2); 00086 else 00087 { 00088 // else the closest points are at the extremities 00089 // 9 possibilities in this cases: check all of these 00090 MC_v3d y0[3]={A1,A1+u1,P1}; 00091 MC_v3d y1[3]={A2,A2+u2,P2}; 00092 double min_dist=99999.99; 00093 double current_dist=0.0; 00094 int min_k1=-1,min_k2=-1; 00095 for(int k1=0;k1<3;k1++) 00096 for(int k2=0;k2<3;k2++) 00097 { 00098 current_dist=(y0[k1]-y1[k2]).norm(); 00099 if(current_dist<min_dist) 00100 if(k1!=2 || (k1==2 && is_valid_p1==1)) 00101 if(k2!=2 || (k2==2 && is_valid_p2==1)) 00102 {min_dist=current_dist;min_k1=k1;min_k2=k2;} 00103 } 00104 00105 // 9 cases 00106 if(min_k1==0 && min_k2==0) {seg=MC_segment(A1,A2);} 00107 else if(min_k1==0 && min_k2==1) {seg=MC_segment(A1,A2+u2);} 00108 else if(min_k1==0 && min_k2==2) {seg=MC_segment(A1,P2);} 00109 00110 else if(min_k1==1 && min_k2==0) {seg=MC_segment(A1+u1,A2);} 00111 else if(min_k1==1 && min_k2==1) {seg=MC_segment(A1+u1,A2+u2);} 00112 else if(min_k1==1 && min_k2==2) {seg=MC_segment(A1+u1,P2);} 00113 00114 else if(min_k1==2 && min_k2==0) {seg=MC_segment(P1,A2);} 00115 else if(min_k1==2 && min_k2==1) {seg=MC_segment(P1,A2+u2);} 00116 else if(min_k1==2 && min_k2==2) {seg=MC_segment(P1,P2);} 00117 00118 } 00119 00120 return seg; 00121 00122 }


| double mesh_conv::MC_segment::distance_to_point | ( | const MC_v3d & | x | ) | const |
return the distance of point x to the closest point on the segment
Definition at line 38 of file MC_segment.cpp.
References closest_point().
Referenced by mesh_conv::MC_polygon::closest_point().
00038 {return (x-closest_point(x)).norm();}


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

| MC_segment mesh_conv::MC_segment::fliped | ( | ) | const |
inverse the segment (first <-> last)
Definition at line 21 of file MC_segment.cpp.
References MC_segment(), x0, and x1.
00021 {return MC_segment(x1,x0);}

| bool mesh_conv::MC_segment::is_aligned | ( | const MC_v3d & | x | ) | const |
check if a point belongs to the direction of the line
does not need to be inside the segment
Definition at line 191 of file MC_segment.cpp.
References unit_vector(), and x0.
Referenced by relative_position().
00192 { 00193 double epsilon=0.0001; 00194 return ((x-x0)-(x-x0).dot(unit_vector())*unit_vector()).norm()<epsilon; 00195 }


| bool mesh_conv::MC_segment::is_belonging | ( | const MC_v3d & | x, | |
| int * | type = 0 | |||
| ) | const |
check if a point belongs to the segment
| x | the position of the point | |
| the | type of belonging (1: inside the segment, -1: aligned but outside of it, -2: not aligned with the direction) |
Definition at line 174 of file MC_segment.cpp.
References relative_position().
00175 { 00176 bool belongs=0; 00177 double t = relative_position(x,&belongs); 00178 if(belongs!=1) 00179 { 00180 if(type!=0) *type=-2; 00181 return false; 00182 } 00183 else if(t<0 || t>1) 00184 { 00185 if(type!=0) *type=-1; 00186 return false; 00187 } 00188 if(type!=0) *type=1; 00189 return true; 00190 }

| double mesh_conv::MC_segment::length | ( | ) | const |
return the length of the segment
Definition at line 24 of file MC_segment.cpp.
References mesh_conv::MC_v3d::norm(), and vector().
Referenced by mesh_conv::MC_curve::barycenter(), mesh_conv::MC_mesh_index_vector::build_arrow(), closest_point(), distance_to_segment(), linear_sampling_intervals(), and relative_position().
00024 {return vector().norm();}


| std::pair< MC_v3d_vector, MC_double_vector > mesh_conv::MC_segment::linear_sampling | ( | const int & | N | ) | const |
subsampling in N samples
| N | the number of sections |
Definition at line 241 of file MC_segment.cpp.
References x0, x1, mesh_conv::MC_v3d_vector::zeros(), and mesh_conv::MC_double_vector::zeros().
Referenced by linear_sampling_intervals().
00242 { 00243 MC_double_vector t_value=MC_double_vector::zeros(N); 00244 MC_v3d_vector sampling=MC_v3d_vector::zeros(N); 00245 for(int k=0;k<N;k++) 00246 { 00247 double u=double(k)/double(N-1); 00248 sampling(k) = (1-u)*x0 + u*x1; 00249 t_value(k)=u; 00250 } 00251 return std::pair<MC_v3d_vector,MC_double_vector> (sampling,t_value); 00252 }


| std::pair< MC_v3d_vector, MC_double_vector > mesh_conv::MC_segment::linear_sampling_intervals | ( | const double & | d_L | ) | const |
subsampling every d_L
| d_L | the approximate sampling length |
does not constraint exactly d_L, but make sure it is an exact number of subdivision
Definition at line 230 of file MC_segment.cpp.
References length(), and linear_sampling().
00231 { 00232 double epsilon=0.00001; 00233 if(d_L<=epsilon) 00234 {std::cout<<"Error in MC_segment::linear_sampling_intervals("<<d_L<<"), interval is too low"<<std::endl;exit(-1);} 00235 00236 double total_length=length(); 00237 int N_subdiv=int(total_length/d_L+1); 00238 00239 return linear_sampling(N_subdiv); 00240 }

| MC_v3d & mesh_conv::MC_segment::operator() | ( | const int & | index | ) |
direct access operator
Definition at line 216 of file MC_segment.cpp.
References assert_bounds(), x0, and x1.
00217 {assert_bounds(index); if(index==0)return x0;if(index==1) return x1;exit(-1);}

| MC_v3d mesh_conv::MC_segment::operator() | ( | const int & | index | ) | const |
direct access operator
Definition at line 214 of file MC_segment.cpp.
References assert_bounds(), x0, and x1.
00215 {assert_bounds(index); if(index==0)return x0;if(index==1) return x1;exit(-1);}

| MC_v3d & mesh_conv::MC_segment::operator[] | ( | const int & | index | ) |
direct access operator
Definition at line 220 of file MC_segment.cpp.
References assert_bounds(), x0, and x1.
00221 {assert_bounds(index); if(index==0)return x0;if(index==1) return x1;exit(-1);}

| MC_v3d mesh_conv::MC_segment::operator[] | ( | const int & | index | ) | const |
direct access operator
Definition at line 218 of file MC_segment.cpp.
References assert_bounds(), x0, and x1.
00219 {assert_bounds(index); if(index==0)return x0;if(index==1) return x1;exit(-1);}

| MC_v3d mesh_conv::MC_segment::plane_intersection | ( | const MC_v3d & | n, | |
| const MC_v3d & | x0, | |||
| int * | type = 0 | |||
| ) | const |
Intersection with the plane of equation <n,x-x0>=0.
| n | the normal of the plane | |
| x0 | a point within the plane | |
| type | a pointer to return the type if needed (default NULL) |
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 128 of file MC_segment.cpp.
References mesh_conv::MC_v3d::dot(), x0, and x1.
Referenced by mesh_conv::MC_triangle::segment_intersection().
00129 { 00130 double epsilon=0.00001; 00131 //first check if the segment is parallel to the plane 00132 MC_v3d AB=x1-x0; 00133 if(fabs(AB.dot(n))<epsilon) 00134 { 00135 //plane is \\ to the segment 00136 //now test if the plane pass through the line 00137 if(fabs(n.dot(_x0-x0))<epsilon)//pass through 00138 { 00139 if(type!=0) 00140 *type=3; 00141 return MC_v3d(-1,-1,-1); 00142 } 00143 else//no intersection 00144 { 00145 if(type!=0) 00146 *type=0; 00147 return MC_v3d(-1,-1,-1); 00148 } 00149 } 00150 00151 //We are sure that the plane is not \\ to the segment 00152 double t=n.dot(_x0-x0)/AB.dot(n); 00153 00154 MC_v3d intersection=x0+t*AB; 00155 00156 00157 00158 //check if the intersection is inside the segment 00159 if(t>=0 && t<=1) 00160 { 00161 //check if its close to a vertex or not 00162 if( ((intersection-x0).norm()<epsilon) || ((intersection-x1).norm()<epsilon) ) 00163 {if(type!=0) *type=2;} 00164 else 00165 {if(type!=0) *type=1;} 00166 return intersection; 00167 } 00168 00169 //else outside the segment => no intersection 00170 if(type!=0) *type=0; 00171 return intersection; 00172 }


| double mesh_conv::MC_segment::relative_position | ( | const MC_v3d & | x, | |
| bool * | is_aligned = 0 | |||
| ) | const |
if a point is aligned with the direction, get its parameterization t such that (x0,x)=t (x0,x1)
| x | the current position | |
| is_aligned | return 1 if the point belongs to the direction, 0 if not (default NULL) |
Definition at line 196 of file MC_segment.cpp.
References is_aligned(), length(), unit_vector(), and x0.
Referenced by mesh_conv::MC_polygon::barycentric_coordinates(), is_belonging(), and mesh_conv::MC_mesh_index_vector::segment_intersection().
00197 { 00198 // check if it is aligned 00199 if(is_aligned(x)==false) 00200 { 00201 if(_is_aligned!=0) 00202 *_is_aligned=false; 00203 return 0.0;} 00204 00205 if(_is_aligned!=0) 00206 *_is_aligned=true; 00207 return (x-x0).dot(unit_vector())/length(); 00208 }


| MC_v3d mesh_conv::MC_segment::unit_vector | ( | ) | const |
get the vector (x0,x1) / ||(x0,x1)||
Definition at line 19 of file MC_segment.cpp.
References mesh_conv::MC_v3d::normalized(), and vector().
Referenced by mesh_conv::MC_mesh_index_vector::build_arrow(), closest_point(), is_aligned(), mesh_conv::MC_v3d::project_on_line(), and relative_position().
00019 {return vector().normalized();}


| MC_v3d_vector mesh_conv::MC_segment::value | ( | const MC_double_vector & | t | ) | const |
| MC_v3d mesh_conv::MC_segment::value | ( | const double & | t | ) | const |
get linear interpolation at time t (parameterization bw [0,1])
| t | the time of the interpolation |
Definition at line 223 of file MC_segment.cpp.
Referenced by mesh_conv::MC_curve::value().

| MC_v3d mesh_conv::MC_segment::vector | ( | ) | const |
get the vector x1-x0
Definition at line 20 of file MC_segment.cpp.
Referenced by closest_to_segment(), length(), and unit_vector().

| MC_segment operator* | ( | const double & | a, | |
| const MC_segment & | s | |||
| ) | [friend] |
scale the segment
| MC_segment operator* | ( | const MC_segment & | s, | |
| const double & | a | |||
| ) | [friend] |
scale the segment
| MC_segment operator+ | ( | const MC_v3d & | x, | |
| const MC_segment & | s | |||
| ) | [friend] |
translate the segment
| MC_segment operator+ | ( | const MC_segment & | s, | |
| const MC_v3d & | x | |||
| ) | [friend] |
translate the segment
| std::ostream& operator<< | ( | std::ostream & | stream, | |
| const MC_segment & | s | |||
| ) | [friend] |
Ostream operator
MC_v3d mesh_conv::MC_segment::x0 [private] |
The internal data structure is two MC_v3d.
Definition at line 235 of file MC_segment.hpp.
Referenced by closest_point(), closest_to_segment(), fliped(), is_aligned(), linear_sampling(), MC_segment(), operator()(), operator[](), plane_intersection(), relative_position(), value(), and vector().
MC_v3d mesh_conv::MC_segment::x1 [private] |
Definition at line 235 of file MC_segment.hpp.
Referenced by closest_point(), fliped(), linear_sampling(), MC_segment(), operator()(), operator[](), plane_intersection(), value(), and vector().
1.6.1