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