Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_plane.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2010-2017 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_plane.h"
17 
18 #include "a_debug.h"
19 #include <sstream>
20 
21 //---------------------------------------------------------------------------
22 const std::string a_plane::help()
23 {
24  std::ostringstream o;
25  o << "********" << std::endl;
26  o << "a_plane:" << std::endl;
27  o << "********" << std::endl;
28  o << "This is a 'plane' class" << std::endl;
29  o << std::endl;
30  o << "Create a plane:" << std::endl;
31  o << " a_plane p p1 p2 p3" << std::endl;
32  o << " (p: name of the plane)" << std::endl;
33  o << " (p1, p2, p3: 3 points defining the plane)" << std::endl;
34  o << std::endl;
35  o << "Commands:" << std::endl;
36  o << "n: returns normal to the plane." << std::endl;
37  o << "dist _pt: returns distance to a point." << std::endl;
38  o << "closest _pt: point on the plane closest to p." << std::endl;
39  o << "intersect _seg: intersection of plane with a segment" << std::endl;
40  o << "intersect _plane: intersection of plane with another plane" << std::endl;
41  o << "contains _pt: check whether point is in plane" << std::endl;
42  o << a_geom_base::help();
43  return o.str();
44 }
45 //---------------------------------------------------------------------------
46 a_plane::a_plane(const a_point& p1, const a_point& p2, const a_point& p3)
47 {
48  a_point n = cross(p2-p1,p3-p1).normalise();
49  double d = n*p1;
50  this->X(n.x());
51  this->Y(n.y());
52  this->Z(n.z());
53  this->T(d);
54 }
55 //---------------------------------------------------------------------------
57 {
58  return a_point(this->X(),this->Y(),this->Z());
59 }
60 //---------------------------------------------------------------------------
62 {
63  return this->d0()*this->n();
64 }
65 //---------------------------------------------------------------------------
66 double a_plane::dist(const a_point& p) const
67 {
68  return fabs(this->T()-this->n()*p);
69 }
70 //---------------------------------------------------------------------------
72 {
73  return p + (this->dist(p))*(this->n());
74 }
75 //---------------------------------------------------------------------------
77 {
78  a_point p1 = s.p1();
79  a_point n = this->n();
80  a_point v = s.dir();
81  double cos_angle = n*v;
82  if (fabs(cos_angle) < verysmall_)
84  double dist = this->T()-n*p1;
85  v *= dist/cos_angle;
86  return (p1+v);
87 }
88 //---------------------------------------------------------------------------
90 {
91  a_point n1 = this->n();
92  a_point n2 = p.n();
93  //check whether planes are parallel
94  double cos_angle = n1*n2;
95  if (fabs(cos_angle) < verysmall_)
97  //direction of interesction
98  a_point d = cross(n1,n2);
99  //direction in planes perp to direction of intersection
100  a_point s1 = cross(n1,d);
101  a_point s2 = cross(n2,d);
102  //plane points closest to origin
103  a_point p1 = this->p0();
104  a_point p2 = p.p0();
105  a_segment seg1(p1,p1+s1);
106  a_segment seg2(p2,p2+s2);
107  double k1,k2;
108  a_segment seg = seg1.intersect(seg2,k1,k2);
109  a_point c = seg.c();
110  return a_segment(c,c+d);
111 }
112 //---------------------------------------------------------------------------
113 bool a_plane::contains(const a_point& p) const
114 {
115  if (this->dist(p)<verysmall_)
116  return true;
117  return false;
118 }
119 //---------------------------------------------------------------------------
121 {
122  //not very clean, a_point should be a a_3dh!
123  //wrong sense of rotation with a_coord, need to check other uses
124  a_point t(p(0,3),p(1,3),p(2,3));
125  a_point n = this->n();
126  double disp = t*n;
127  this->T(this->T()-disp);
128  p(0,3) = p(1,3) = p(2,3) = 0.;
129  a_3dh n2(n.x(),n.y(),n.z());
130  a_3dh nn = p*n2;
131  this->X(nn.x());
132  this->Y(nn.y());
133  this->Z(nn.z());
134 }
135 //***************************************************************************
136 //---------------------------------------------------------------------------
137 void a_plane::read(std::istream &in)
138 {
139  double x,y,z,t;
140  in >> x >> y >> z >> t;
141  this->X(x);
142  this->Y(y);
143  this->Z(z);
144  this->T(t);
145 }
146 //---------------------------------------------------------------------------
147 void a_plane::write(std::ostream &o) const
148 {
149  o << this->X() << " ";
150  o << this->Y() << " ";
151  o << this->Z() << " ";
152  o << this->T() << " ";
153 }
154 //-operator>>----------------------------------------------------------------
155 std::istream& operator>> (std::istream& i, a_plane & m)
156 {
157  m.read(i);
158  return i;
159 }
160 //-operator<<----------------------------------------------------------------
161 std::ostream& operator<< (std::ostream& o, const a_plane & m)
162 {
163  m.write(o);
164  return o;
165 }
166 
std::ostream & operator<<(std::ostream &o, const a_plane &m)
Definition: a_plane.cxx:161
std::istream & operator>>(std::istream &i, a_plane &m)
Definition: a_plane.cxx:155
a_point cross(const a_point &a, const a_point &b)
Definition: a_point.cxx:284
an homogeneous 3d matrix class
Definition: a_3dh.h:30
double y() const
Definition: a_3dh.h:51
double x() const
Definition: a_3dh.h:50
double Z() const
Definition: a_3dh.h:48
double X() const
Definition: a_3dh.h:46
double T() const
Definition: a_3dh.h:49
double z() const
Definition: a_3dh.h:52
double Y() const
Definition: a_3dh.h:47
double verysmall_
Definition: a_base.h:62
a coordinate system in the Points&Forces file format
Definition: a_coord.h:33
static const std::string help()
Definition: a_geom_base.cxx:21
a plane
Definition: a_plane.h:32
bool contains(const a_point &p) const
check whether point is in plane
Definition: a_plane.cxx:113
static const std::string help()
Definition: a_plane.cxx:22
virtual void read(std::istream &i)
Definition: a_plane.cxx:137
double d0() const
closest distance to origin
Definition: a_plane.h:43
a_plane()
Definition: a_plane.h:34
double dist(const a_point &p) const
distance between p and plane
Definition: a_plane.cxx:66
a_point intersect(const a_segment &s) const
intersection of plane with a segment
Definition: a_plane.cxx:76
void move(a_coord c)
Definition: a_plane.cxx:120
a_point closest(const a_point &p) const
point on the plane closest to p
Definition: a_plane.cxx:71
virtual void write(std::ostream &o) const
Definition: a_plane.cxx:147
a_point p0() const
closest point to origin
Definition: a_plane.cxx:61
a_point n() const
normal to the plane (direction: given by rotation p1-p2-p3)
Definition: a_plane.cxx:56
a_point & normalise()
Definition: a_point.cxx:190
double z() const
Definition: a_point.h:56
double y() const
Definition: a_point.h:55
double x() const
Definition: a_point.h:54
a segment
Definition: a_segment.h:29
a_point dir() const
get direction defined by segment
Definition: a_segment.h:44
a_point c() const
center of segment
Definition: a_segment.h:46
a_segment intersect(const a_segment &s, double &m1, double &m2) const
intersection between two segments, returns a segment between the closest point between the two lines ...
Definition: a_segment.cxx:139
a_point p1() const
get first apex of segment
Definition: a_segment.h:38