Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
plane.cpp
Go to the documentation of this file.
3/*
4Copyright (c) 2000 Igor B. Smirnov
5
6The file can be used, copied, modified, and distributed
7according to the terms of GNU Lesser General Public License version 2.1
8as published by the Free Software Foundation,
9and provided that the above copyright notice, this permission notice,
10and notices about any modifications of the original text
11appear in all copies and in supporting documentation.
12The file is provided "as is" without express or implied warranty.
13*/
14
15namespace Heed {
16
17// **** plane ****
18
19absref absref::* plane::aref[2] = {(absref absref::*)&plane::piv,
20 (absref absref::*)&plane::dir};
21
23 return absref_transmit(2, aref);
24}
25
26plane::plane(const straight& sl, const point& pt) : piv(sl.Gpiv()), dir() {
27 pvecerror("plane::plane( const straight& sl, const point& pt)");
28 int i = sl.check_point_in(pt, 0.0);
29 if (i != 0)
30 vecerror = 1;
31 else
32 dir = unit_vec(sl.Gdir() || (pt - sl.Gpiv()));
33}
34plane::plane(const straight& sl1, const straight& sl2, vfloat prec)
35 : piv(sl1.Gpiv()), dir() {
37 "plane::plane( const straight& sl1, const straight& sl2, vfloat prec)");
38 point pt = sl1.cross(sl2, prec);
39 if (vecerror == 0) {
40 piv = pt;
41 dir = unit_vec(sl1.Gdir() || sl2.Gdir());
42 } else if (vecerror == 2) {
43 // different parallel lines
44 vecerror = 0;
45 dir = unit_vec(sl1.Gdir() || (sl2.Gpiv() - sl1.Gpiv()));
46 }
47 // otherwise vecerror != 0
48}
49
50int operator==(const plane& pl1, const plane& pl2) {
51 pvecerror("int operator==(const plane &pl1, const plane &pl2)");
52
53 if (!(pl1.dir == pl2.dir || pl1.dir == -pl2.dir)) return 0;
54 if (pl1.piv == pl2.piv) return 1;
55 if (pl1.check_point_in(pl2.piv, 0) == 1)
56 return 1;
57 else
58 return 0;
59}
60
61bool apeq(const plane& pl1, const plane& pl2, vfloat prec) {
62 pvecerror("bool apeq(const plane &pl1, const plane &pl2, vfloat prec)");
63 if (check_par(pl1.dir, pl2.dir, prec) == 0) return false;
64 if (apeq(pl1.piv, pl2.piv, prec)) return true;
65 return (pl1.check_point_in(pl2.piv, prec) == 1);
66}
67
68int plane::check_point_in(const point& fp, vfloat prec) const {
69 pvecerror("int plane::check_point_in(point fp, vfloat prec)");
70 vfloat f = distance(fp);
71 if (f < prec) return 1;
72 return 0;
73}
74
75point plane::cross(const straight& sl) const {
76 pvecerror("point plane::cross(straight &sl)");
77 point slpiv = sl.Gpiv();
78 vec sldir = sl.Gdir();
79 vfloat r = dir * sldir;
80 if (r == 0.0) {
81 if (slpiv == piv || check_perp((piv - slpiv), dir, 0.0) == 1) {
82 // Line is in plane
83 vecerror = 3;
84 } else {
85 vecerror = 2;
86 }
87 return point();
88 }
89
90 vfloat t = (piv.v - slpiv.v) * dir;
91 return point(slpiv.v + t / r * sldir);
92}
93
94straight plane::cross(const plane& pl) const {
95 pvecerror("point plane::cross(plane &pl)");
96 point plpiv = pl.Gpiv();
97 vec pldir = pl.Gdir();
98 vec a = dir || pldir; // direction of the overall straight lines
99 if (a.length() == 0) {
100 if (plpiv == piv || check_par(pldir, dir, 0.0) != 0) {
101 // planes coincide
102 vecerror = 3;
103 return straight();
104 } else {
105 vecerror = 2;
106 return straight();
107 }
108 }
109 a = unit_vec(a);
110 vec c = a || dir; // perpend. for ov. str.
111 straight st(piv, c);
112 point pt = pl.cross(st); // one point on ov. str.
113 return straight(pt, a); // overall straight
114}
115
116int plane::cross(const polyline& pll, point* crpt, int& qcrpt, polyline* crpll,
117 int& qcrpll, vfloat prec) const {
118 pvecerror("int plane::cross(polyline &pll, ...");
119
120 qcrpt = 0;
121 qcrpll = 0;
122 for (int n = 0; n < pll.qsl; n++) {
123 point cpt = cross(pll.sl[n]);
124 if (vecerror == 3) // the line is in the plane
125 crpll[qcrpll++] = polyline(&(pll.pt[n]), 2);
126 else if (vecerror != 0)
127 vecerror = 0;
128 else {
129 vec v1 = cpt - pll.pt[n];
130 if (v1.length() < prec) {
131 if (n == 0) {
132 // otherwise it is probably included on the previous step
133 crpt[qcrpt++] = cpt;
134 }
135 } else {
136 vec v2 = cpt - pll.pt[n + 1];
137 if (v2.length() < prec)
138 crpt[qcrpt++] = cpt;
139 else if (check_par(v1, v2, prec) == -1)
140 // anti-parallel vectors, point inside borders
141 crpt[qcrpt++] = cpt;
142 }
143 }
144 }
145 if (qcrpt > 0 || qcrpll > 0)
146 return 1;
147 else
148 return 0;
149}
150
151vfloat plane::distance(const point& fpt) const {
152 pvecerror("vfloat plane::distance(point& fpt)");
153 if (fpt == piv) return 0.0;
154 vec v = fpt - piv;
155 return fabs(v * dir); // dir is unit length vector
156}
157
158std::ostream& operator<<(std::ostream& file, const plane& pl) {
159 Ifile << "plane:\n";
160 indn.n += 2;
161 file << pl.piv << pl.dir;
162 indn.n -= 2;
163 return file;
164}
165}
Plane, defined by defined by a point and a vector normal to the plane.
Definition: plane.h:24
point cross(const straight &sl) const
Definition: plane.cpp:75
friend bool apeq(const plane &pl1, const plane &pl2, vfloat prec)
Definition: plane.cpp:61
plane()
Definition: plane.h:40
vec Gdir() const
Definition: plane.h:33
static absref absref::* aref[2]
Definition: plane.h:37
point piv
Origin point, pivot.
Definition: plane.h:27
point Gpiv() const
Definition: plane.h:32
vec dir
Direction of normal, unit vector.
Definition: plane.h:29
int check_point_in(const point &fp, vfloat prec) const
Return 1 if a point is in the plane (within precision prec).
Definition: plane.cpp:68
vfloat distance(const point &fpt) const
Definition: plane.cpp:151
virtual absref_transmit get_components() override
Definition: plane.cpp:22
Point.
Definition: vec.h:368
vec v
Definition: vec.h:370
Polyline.
Definition: polyline.h:23
point * pt
Definition: polyline.h:26
straight * sl
Definition: polyline.h:28
Straight line, as combination of vector and point.
Definition: straight.h:24
point cross(const straight &sl, vfloat prec) const
Definition: straight.cpp:53
point Gpiv() const
Definition: straight.h:32
vec Gdir() const
Definition: straight.h:33
int check_point_in(const point &fp, vfloat prec) const
Definition: straight.cpp:47
Definition: vec.h:179
vfloat length() const
Definition: vec.h:196
Definition: BGMesh.cpp:6
std::ostream & operator<<(std::ostream &file, const BGMesh &bgm)
Definition: BGMesh.cpp:37
int vecerror
Definition: vec.cpp:29
bool apeq(const circumf &f1, const circumf &f2, vfloat prec)
Definition: circumf.cpp:44
int operator==(const circumf &f1, const circumf &f2)
Definition: circumf.cpp:34
DoubleAc fabs(const DoubleAc &f)
Definition: DoubleAc.h:615
indentation indn
Definition: prstream.cpp:15
double vfloat
Definition: vfloat.h:16
#define Ifile
Definition: prstream.h:195
#define pvecerror(string)
Definition: vec.h:28