[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[0a4f7f] | 8 | /*
|
---|
| 9 | * Plane.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Apr 7, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[6d5a10] | 22 | #include <cmath>
|
---|
[9b410d] | 23 | #include <limits>
|
---|
[6d5a10] | 24 |
|
---|
[ad011c] | 25 | #include "CodePatterns/Assert.hpp"
|
---|
| 26 | #include "CodePatterns/Info.hpp"
|
---|
| 27 | #include "CodePatterns/Log.hpp"
|
---|
| 28 | #include "CodePatterns/Verbose.hpp"
|
---|
[9b410d] | 29 | #include "Exceptions/MultipleSolutionsException.hpp"
|
---|
| 30 | #include "LinearAlgebra/defs.hpp"
|
---|
[cd406d] | 31 | #include "LinearAlgebra/fast_functions.hpp"
|
---|
[57f243] | 32 | #include "LinearAlgebra/Line.hpp"
|
---|
[6d5a10] | 33 | #include "LinearAlgebra/Plane.hpp"
|
---|
| 34 | #include "LinearAlgebra/Vector.hpp"
|
---|
[0a4f7f] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * generates a plane from three given vectors defining three points in space
|
---|
| 38 | */
|
---|
[2cbe97] | 39 | Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) throw(LinearDependenceException) :
|
---|
[0a4f7f] | 40 | normalVector(new Vector())
|
---|
| 41 | {
|
---|
[273382] | 42 | Vector x1 = y1 -y2;
|
---|
| 43 | Vector x2 = y3 -y2;
|
---|
[71129f] | 44 | if ((fabs(x1.Norm()) <= LINALG_MYEPSILON()) || (fabs(x2.Norm()) <= LINALG_MYEPSILON()) || (fabs(x1.Angle(x2)) <= LINALG_MYEPSILON())) {
|
---|
[0a4f7f] | 45 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 46 | }
|
---|
| 47 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
---|
| 48 | // x1.Output((ofstream *)&cout);
|
---|
| 49 | // Log() << Verbose(0) << endl;
|
---|
| 50 | // Log() << Verbose(4) << "second plane coordinates:";
|
---|
| 51 | // x2.Output((ofstream *)&cout);
|
---|
| 52 | // Log() << Verbose(0) << endl;
|
---|
| 53 |
|
---|
| 54 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
---|
| 55 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
---|
| 56 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
---|
| 57 | normalVector->Normalize();
|
---|
| 58 |
|
---|
[273382] | 59 | offset=normalVector->ScalarProduct(y1);
|
---|
[0a4f7f] | 60 | }
|
---|
| 61 | /**
|
---|
[2cbe97] | 62 | * Constructs a plane from two direction vectors and a offset.
|
---|
[0a4f7f] | 63 | */
|
---|
[fa5a6a] | 64 | Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) :
|
---|
[0a4f7f] | 65 | normalVector(new Vector()),
|
---|
| 66 | offset(_offset)
|
---|
| 67 | {
|
---|
[273382] | 68 | Vector x1 = y1;
|
---|
| 69 | Vector x2 = y2;
|
---|
[71129f] | 70 | if ((fabs(x1.Norm()) <= LINALG_MYEPSILON()) || (fabs(x2.Norm()) <= LINALG_MYEPSILON())) {
|
---|
[fa5a6a] | 71 | throw ZeroVectorException(__FILE__,__LINE__);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[71129f] | 74 | if((fabs(x1.Angle(x2)) <= LINALG_MYEPSILON())) {
|
---|
[0a4f7f] | 75 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 76 | }
|
---|
| 77 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
---|
| 78 | // x1.Output((ofstream *)&cout);
|
---|
| 79 | // Log() << Verbose(0) << endl;
|
---|
| 80 | // Log() << Verbose(4) << "second plane coordinates:";
|
---|
| 81 | // x2.Output((ofstream *)&cout);
|
---|
| 82 | // Log() << Verbose(0) << endl;
|
---|
| 83 |
|
---|
| 84 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
---|
| 85 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
---|
| 86 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
---|
| 87 | normalVector->Normalize();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[2cbe97] | 90 | Plane::Plane(const Vector &_normalVector, double _offset) throw(ZeroVectorException):
|
---|
[0a4f7f] | 91 | normalVector(new Vector(_normalVector)),
|
---|
| 92 | offset(_offset)
|
---|
[72e7fa] | 93 | {
|
---|
[2cbe97] | 94 | if(normalVector->IsZero())
|
---|
| 95 | throw ZeroVectorException(__FILE__,__LINE__);
|
---|
[72e7fa] | 96 | double factor = 1/normalVector->Norm();
|
---|
| 97 | // normalize the plane parameters
|
---|
| 98 | (*normalVector)*=factor;
|
---|
| 99 | offset*=factor;
|
---|
| 100 | }
|
---|
[0a4f7f] | 101 |
|
---|
[2cbe97] | 102 | Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) throw(ZeroVectorException):
|
---|
[0a4f7f] | 103 | normalVector(new Vector(_normalVector))
|
---|
| 104 | {
|
---|
[2cbe97] | 105 | if(normalVector->IsZero()){
|
---|
| 106 | throw ZeroVectorException(__FILE__,__LINE__);
|
---|
| 107 | }
|
---|
[3cdd16] | 108 | normalVector->Normalize();
|
---|
[273382] | 109 | offset = normalVector->ScalarProduct(_offsetVector);
|
---|
[0a4f7f] | 110 | }
|
---|
| 111 |
|
---|
[d4c9ae] | 112 | /**
|
---|
| 113 | * copy constructor
|
---|
| 114 | */
|
---|
| 115 | Plane::Plane(const Plane& plane) :
|
---|
| 116 | normalVector(new Vector(*plane.normalVector)),
|
---|
| 117 | offset(plane.offset)
|
---|
| 118 | {}
|
---|
| 119 |
|
---|
| 120 |
|
---|
[0a4f7f] | 121 | Plane::~Plane()
|
---|
| 122 | {}
|
---|
| 123 |
|
---|
[89ebc0] | 124 | Plane &Plane::operator=(const Plane &rhs){
|
---|
| 125 | if(&rhs!=this){
|
---|
| 126 | normalVector.reset(new Vector(*rhs.normalVector));
|
---|
| 127 | offset = rhs.offset;
|
---|
| 128 | }
|
---|
| 129 | return *this;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[0a4f7f] | 132 |
|
---|
[fa5a6a] | 133 | Vector Plane::getNormal() const{
|
---|
[0a4f7f] | 134 | return *normalVector;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[fa5a6a] | 137 | double Plane::getOffset() const{
|
---|
[0a4f7f] | 138 | return offset;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[45ef76] | 141 | Vector Plane::getOffsetVector() const {
|
---|
[72e7fa] | 142 | return getOffset()*getNormal();
|
---|
| 143 | }
|
---|
[c61c87] | 144 |
|
---|
[45ef76] | 145 | vector<Vector> Plane::getPointsOnPlane() const{
|
---|
[1829c4] | 146 | std::vector<Vector> res;
|
---|
[fa5a6a] | 147 | res.reserve(3);
|
---|
[1829c4] | 148 | // first point on the plane
|
---|
[fa5a6a] | 149 | res.push_back(getOffsetVector());
|
---|
| 150 | // get a vector that has direction of plane
|
---|
[c61c87] | 151 | Vector direction;
|
---|
[fa5a6a] | 152 | direction.GetOneNormalVector(getNormal());
|
---|
| 153 | res.push_back(res[0]+direction);
|
---|
| 154 | // get an orthogonal vector to direction and normal (has direction of plane)
|
---|
| 155 | direction.VectorProduct(getNormal());
|
---|
[c61c87] | 156 | direction.Normalize();
|
---|
[fa5a6a] | 157 | res.push_back(res[0] +direction);
|
---|
[c61c87] | 158 | return res;
|
---|
[1829c4] | 159 | }
|
---|
[c61c87] | 160 |
|
---|
[72e7fa] | 161 |
|
---|
[0a4f7f] | 162 | /** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
|
---|
| 163 | * According to [Bronstein] the vectorial plane equation is:
|
---|
| 164 | * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
|
---|
| 165 | * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
|
---|
| 166 | * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
|
---|
| 167 | * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
|
---|
| 168 | * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
|
---|
| 169 | * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
|
---|
| 170 | * of the line yields the intersection point on the plane.
|
---|
| 171 | * \param *Origin first vector of line
|
---|
| 172 | * \param *LineVector second vector of line
|
---|
| 173 | * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
|
---|
| 174 | */
|
---|
[27ac00] | 175 | Vector Plane::GetIntersection(const Line& line) const
|
---|
[0a4f7f] | 176 | {
|
---|
| 177 | Info FunctionInfo(__func__);
|
---|
| 178 | Vector res;
|
---|
| 179 |
|
---|
[27ac00] | 180 | double factor1 = getNormal().ScalarProduct(line.getDirection());
|
---|
[71129f] | 181 | if(fabs(factor1) <= LINALG_MYEPSILON()){
|
---|
[27ac00] | 182 | // the plane is parallel... under all circumstances this is bad luck
|
---|
| 183 | // we no have either no or infinite solutions
|
---|
| 184 | if(isContained(line.getOrigin())){
|
---|
| 185 | throw MultipleSolutionsException<Vector>(__FILE__,__LINE__,line.getOrigin());
|
---|
| 186 | }
|
---|
| 187 | else{
|
---|
| 188 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 189 | }
|
---|
[0a4f7f] | 190 | }
|
---|
| 191 |
|
---|
[27ac00] | 192 | double factor2 = getNormal().ScalarProduct(line.getOrigin());
|
---|
[0a4f7f] | 193 | double scaleFactor = (offset-factor2)/factor1;
|
---|
| 194 |
|
---|
[27ac00] | 195 | res = line.getOrigin() + scaleFactor * line.getDirection();
|
---|
[0a4f7f] | 196 |
|
---|
[27ac00] | 197 | // tests to make sure the resulting vector really is on plane and line
|
---|
| 198 | ASSERT(isContained(res),"Calculated line-Plane intersection does not lie on plane.");
|
---|
| 199 | ASSERT(line.isContained(res),"Calculated line-Plane intersection does not lie on line.");
|
---|
[0a4f7f] | 200 | return res;
|
---|
| 201 | };
|
---|
[2247a9] | 202 |
|
---|
[ccf826] | 203 | Vector Plane::mirrorVector(const Vector &rhs) const {
|
---|
| 204 | Vector helper = getVectorToPoint(rhs);
|
---|
| 205 | // substract twice the Vector to the plane
|
---|
| 206 | return rhs+2*helper;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[5589858] | 209 | Line Plane::getOrthogonalLine(const Vector &origin) const{
|
---|
| 210 | return Line(origin,getNormal());
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[c17975] | 213 | bool Plane::onSameSide(const Vector &point1,const Vector &point2) const{
|
---|
| 214 | return sign(point1.ScalarProduct(*normalVector)-offset) ==
|
---|
| 215 | sign(point2.ScalarProduct(*normalVector)-offset);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[2247a9] | 218 | /************ Methods inherited from Space ****************/
|
---|
| 219 |
|
---|
[005e18] | 220 | double Plane::distance(const Vector &point) const{
|
---|
[2247a9] | 221 | double res = point.ScalarProduct(*normalVector)-offset;
|
---|
| 222 | return fabs(res);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[005e18] | 225 | Vector Plane::getClosestPoint(const Vector &point) const{
|
---|
[fa5a6a] | 226 | double factor = point.ScalarProduct(*normalVector)-offset;
|
---|
[71129f] | 227 | if(fabs(factor) <= LINALG_MYEPSILON()){
|
---|
[2247a9] | 228 | // the point itself lies on the plane
|
---|
| 229 | return point;
|
---|
| 230 | }
|
---|
[fa5a6a] | 231 | Vector difference = factor * (*normalVector);
|
---|
| 232 | return (point - difference);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | // Operators
|
---|
| 236 |
|
---|
[82cf79] | 237 | bool operator==(const Plane &x,const Plane &y){
|
---|
| 238 | return *x.normalVector == *y.normalVector && x.offset == y.offset;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[fa5a6a] | 241 | ostream &operator << (ostream &ost,const Plane &p){
|
---|
| 242 | ost << "<" << p.getNormal() << ";x> - " << p.getOffset() << "=0";
|
---|
| 243 | return ost;
|
---|
[2247a9] | 244 | }
|
---|