Ignore:
Timestamp:
Apr 30, 2010, 1:33:12 PM (16 years ago)
Author:
Tillmann Crueger <crueger@…>
Children:
25e17e9
Parents:
4eee8f
Message:

Added unittests for planes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/Plane.cpp

    r4eee8f r59e7832  
    4444 * If no offset is given a plane through origin is assumed
    4545 */
    46 Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(LinearDependenceException) :
     46Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) :
    4747    normalVector(new Vector()),
    4848    offset(_offset)
     
    5050  Vector x1 = y1;
    5151  Vector x2 = y2;
    52   if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
     52  if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON)) {
     53    throw ZeroVectorException(__FILE__,__LINE__);
     54  }
     55
     56  if((fabs(x1.Angle(x2)) < MYEPSILON)) {
    5357    throw LinearDependenceException(__FILE__,__LINE__);
    5458  }
     
    9296
    9397
    94 Vector Plane::getNormal(){
     98Vector Plane::getNormal() const{
    9599  return *normalVector;
    96100}
    97101
    98 double Plane::getOffset(){
     102double Plane::getOffset() const{
    99103  return offset;
    100104}
     
    106110vector<Vector> Plane::getPointsOnPlane(){
    107111  std::vector<Vector> res;
     112  res.reserve(3);
    108113  // first point on the plane
    109   res[0] = getOffsetVector();
    110   // first is orthogonal to the plane...
    111   // an orthogonal vector to this one lies on the plane
     114  res.push_back(getOffsetVector());
     115  // get a vector that has direction of plane
    112116  Vector direction;
    113   direction.GetOneNormalVector(res[0]);
    114   res[1] = res[0]+direction;
    115   // get an orthogonal vector to direction and offset (lies on the plane)
    116   direction.VectorProduct(res[0]);
     117  direction.GetOneNormalVector(getNormal());
     118  res.push_back(res[0]+direction);
     119  // get an orthogonal vector to direction and normal (has direction of plane)
     120  direction.VectorProduct(getNormal());
    117121  direction.Normalize();
    118   res[2] = res[0] +direction;
     122  res.push_back(res[0] +direction);
    119123  return res;
    120124}
     
    178182
    179183Vector Plane::getClosestPoint(const Vector &point) const{
    180   Vector difference = distance(point) * (*normalVector);
    181   if(difference.IsZero()){
     184  double factor = point.ScalarProduct(*normalVector)-offset;
     185  if(fabs(factor) < MYEPSILON){
    182186    // the point itself lies on the plane
    183187    return point;
    184188  }
    185   // get the direction this vector is pointing
    186   double sign = difference.ScalarProduct(*normalVector);
    187   // sign cannot be zero, since normalVector and difference are both != zero
    188   sign = sign/fabs(sign);
    189   return (point - (sign * difference));
     189  Vector difference = factor * (*normalVector);
     190  return (point - difference);
    190191}
     192
     193// Operators
     194
     195ostream &operator << (ostream &ost,const Plane &p){
     196  ost << "<" << p.getNormal() << ";x> - " << p.getOffset() << "=0";
     197  return ost;
     198}
Note: See TracChangeset for help on using the changeset viewer.