Changeset 4e4940 for molecuilder/src/vector.cpp
- Timestamp:
- Aug 3, 2009, 8:10:09 AM (16 years ago)
- Children:
- 0e2190, 834ff3
- Parents:
- 3dc682
- File:
-
- 1 edited
-
molecuilder/src/vector.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/vector.cpp
r3dc682 r4e4940 196 196 }; 197 197 198 /** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset. 199 * According to [Bronstein] the vectorial plane equation is: 200 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$, 201 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and 202 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$, 203 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where 204 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize 205 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization 206 * of the line yields the intersection point on the plane. 207 * \param *out output stream for debugging 208 * \param *PlaneNormal Plane's normal vector 209 * \param *PlaneOffset Plane's offset vector 210 * \param *LineVector first vector of line 211 * \param *LineVector2 second vector of line 212 * \return true - \a this contains intersection point on return, false - line is parallel to plane 213 */ 214 bool Vector::GetIntersectionWithPlane(ofstream *out, Vector *PlaneNormal, Vector *PlaneOffset, Vector *LineVector, Vector *LineVector2) 215 { 216 double factor; 217 Vector Direction; 218 219 // find intersection of a line defined by Offset and Direction with a plane defined by triangle 220 Direction.CopyVector(LineVector2); 221 Direction.SubtractVector(LineVector); 222 if (Direction.ScalarProduct(PlaneNormal) < MYEPSILON) 223 return false; 224 factor = LineVector->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal)); 225 Direction.Scale(factor); 226 CopyVector(LineVector); 227 SubtractVector(&Direction); 228 229 // test whether resulting vector really is on plane 230 Direction.CopyVector(this); 231 Direction.SubtractVector(PlaneOffset); 232 if (Direction.ScalarProduct(PlaneNormal) > MYEPSILON) 233 return true; 234 else 235 return false; 236 }; 237 238 /** Calculates the intersection of the two lines that are both on the same plane. 239 * Note that we do not check whether they are on the same plane. 240 * \param *out output stream for debugging 241 * \param *Line1a first vector of first line 242 * \param *Line1b second vector of first line 243 * \param *Line2a first vector of second line 244 * \param *Line2b second vector of second line 245 * \return true - \a this will contain the intersection on return, false - lines are parallel 246 */ 247 bool Vector::GetIntersectionOfTwoLinesOnPlane(ofstream *out, Vector *Line1a, Vector *Line1b, Vector *Line2a, Vector *Line2b) 248 { 249 double k1,a1,h1,b1,k2,a2,h2,b2; 250 // equation for line 1 251 if (fabs(Line1a->x[0] - Line2a->x[0]) < MYEPSILON) { 252 k1 = 0; 253 h1 = 0; 254 } else { 255 k1 = (Line1a->x[1] - Line2a->x[1])/(Line1a->x[0] - Line2a->x[0]); 256 h1 = (Line1a->x[2] - Line2a->x[2])/(Line1a->x[0] - Line2a->x[0]); 257 } 258 a1 = 0.5*((Line1a->x[1] + Line2a->x[1]) - k1*(Line1a->x[0] + Line2a->x[0])); 259 b1 = 0.5*((Line1a->x[2] + Line2a->x[2]) - h1*(Line1a->x[0] + Line2a->x[0])); 260 261 // equation for line 2 262 if (fabs(Line2a->x[0] - Line2a->x[0]) < MYEPSILON) { 263 k1 = 0; 264 h1 = 0; 265 } else { 266 k1 = (Line2a->x[1] - Line2a->x[1])/(Line2a->x[0] - Line2a->x[0]); 267 h1 = (Line2a->x[2] - Line2a->x[2])/(Line2a->x[0] - Line2a->x[0]); 268 } 269 a1 = 0.5*((Line2a->x[1] + Line2a->x[1]) - k1*(Line2a->x[0] + Line2a->x[0])); 270 b1 = 0.5*((Line2a->x[2] + Line2a->x[2]) - h1*(Line2a->x[0] + Line2a->x[0])); 271 272 // calculate cross point 273 if (fabs((a1-a2)*(h1-h2) - (b1-b2)*(k1-k2)) < MYEPSILON) { 274 x[0] = (a2-a1)/(k1-k2); 275 x[1] = (k1*a2-k2*a1)/(k1-k2); 276 x[2] = (h1*b2-h2*b1)/(h1-h2); 277 *out << Verbose(4) << "Lines do intersect at " << this << "." << endl; 278 return true; 279 } else { 280 *out << Verbose(4) << "Lines do not intersect." << endl; 281 return false; 282 } 283 }; 284 198 285 /** Calculates the projection of a vector onto another \a *y. 199 286 * \param *y array to second vector … … 453 540 }; 454 541 455 /** Do a matrix multiplication with \a *matrix' inverse.542 /** Do a matrix multiplication with the \a *A' inverse. 456 543 * \param *matrix NDIM_NDIM array 457 544 */
Note:
See TracChangeset
for help on using the changeset viewer.
