Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPointParcours.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
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  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "TYPointParcours.h"
21 
22 #include <math.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 
30 
32 {
33  TYPointParcours res = P2;
34  res.x -= P1.x;
35  res.y -= P1.y;
36  return res;
37 }
38 
40 {
41  double zCross = (SR.x * SP.y) - (SR.y * SP.x);
42  return zCross;
43 }
44 
46 {
47  return (x * x + y * y);
48 }
49 
51 {
52  return Confondus(this, &p);
53 }
54 
56 {
57  double XDiff = (P1->x - P2->x);
58  double YDiff = (P1->y - P2->y);
59  double dNormeCarre = XDiff * XDiff + YDiff * YDiff;
60  return dNormeCarre < _dSeuilDistanceCarre;
61 }
62 
64  TYPointParcours& R)
65 {
66  // On calcul a peu de chose pres l'abscisse curviligne de P sur [SR]
67  //"a peu de chose pres" = c'est en fait le carre de l'abscisse, en gardant son signe
68  // permet d'avoir une relation d'ordre sur [SR] (sqrt est inutile pour cela)
69  // P est-il de l'autre côté de S par rapport a R ?
70  bool bDuMemeCoteQueR = Scalaire(S, R, S, P) >= 0;
71  TYPointParcours SR = vecteur2D(S, R);
72  TYPointParcours SP = vecteur2D(S, P);
73  double dNormeCSR = SR.normeCarree();
74  if (0 == dNormeCSR)
75  {
76  return 0; // evitons les divisions par zero...
77  }
78  double dNormeCSP = SP.normeCarree();
79  double dAbscisseCarree = (bDuMemeCoteQueR ? dNormeCSP / dNormeCSR : -dNormeCSP / dNormeCSR);
80  return dAbscisseCarree;
81 }
82 
84 {
85  return vecteur1.x * vecteur2.x + vecteur1.y * vecteur2.y;
86 }
87 
89  TYPointParcours& P4)
90 {
91  // return (P2.x - P1.x)*(P4.x - P3.x) + (P2.y - P1.y)*(P4.y - P3.y);
92  TYPointParcours aTYPointParcours_P1P2 = vecteur2D(P1, P2), aTYPointParcours_P3P4 = vecteur2D(P3, P4);
93  return Scalaire(aTYPointParcours_P1P2, aTYPointParcours_P3P4);
94 }
95 
98  bool bP3OrP4MustNotCoincideWithP)
99 {
100  // 2. P2 & P3 sont-ils du meme côté de P1P2 ?
101  TYPointParcours P1P2 = vecteur2D(P1, P2);
102  TYPointParcours P1P3 = vecteur2D(P1, P3);
103  TYPointParcours P1P4 = vecteur2D(P1, P4);
104  double detP3 = ZCross(P1P2, P1P3);
105  double detP4 = ZCross(P1P2, P1P4);
106  if (detP3 && detP4 && (detP3 * detP4) > 0)
107  {
108  return false;
109  }
110 
111  bool bIntersectionDroites = IntersectionDroites(P1, P2, P3, P4, P);
112  if (!bIntersectionDroites)
113  {
114  return false;
115  }
116  // P est-il confondu avec P1 ?
117  if (Confondus(&P1, &P))
118  {
119  return false;
120  }
121 
122  // P est-il confondu avec P2 ?
123  if (Confondus(&P2, &P))
124  {
125  return false;
126  }
127 
128  // P est-il confondu avec P3 ?
129  if (Confondus(&P3, &P) && bP3OrP4MustNotCoincideWithP)
130  {
131  return false;
132  }
133 
134  // P est-il confondu avec P4 ?
135  if (Confondus(&P4, &P) && bP3OrP4MustNotCoincideWithP)
136  {
137  return false;
138  }
139 
140  // Le point P est-il de l'autre côté de P1 par rapport a P2 ?
141  if (Scalaire(P1, P, P1, P2) <= 0)
142  {
143  return false;
144  }
145 
146  // Le point P est-il sur P1P2 ?
147  TYPointParcours P1P = vecteur2D(P1, P);
148  if (P1P.normeCarree() >= P1P2.normeCarree())
149  {
150  return false;
151  }
152 
153  return true;
154 }
155 
158 {
159  P.x = P1.x;
160  P.y = P1.y;
161  P.z = P1.z; // conservons l'indentifiant
162  double alpha = NAN;
163  // Eq de droite P1P2
164  if (P2.x == P1.x)
165  {
166  // droite verticale
167  P.x = P1.x;
168  if (P4.x == P3.x)
169  {
170  return false; // colineaire
171  }
172  // P3P = alphaP3P4
173  alpha = (P.x - P3.x) / (P4.x - P3.x);
174  P.y = P3.y + alpha * (P4.y - P3.y);
175  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
176  P.z = P3.z + alpha * (P4.z - P3.z);
177  return true;
178  }
179  else if (P2.y == P1.y)
180  {
181  // droite horizontale
182  P.y = P1.y;
183  if (P4.y == P3.y)
184  {
185  return false; // colineaire
186  }
187  // P3P = alphaP3P4
188  alpha = (P.y - P3.y) / (P4.y - P3.y);
189  P.x = P3.x + alpha * (P4.x - P3.x);
190  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
191  P.z = P3.z + alpha * (P4.z - P3.z);
192  return true;
193  }
194  else if ((P4.y == P3.y) || (P4.x == P3.x))
195  {
196  return IntersectionDroites(P3, P4, P1, P2, P);
197  }
198 
199  double a = (P2.y - P1.y) / (P2.x - P1.x);
200  double b = -a * P1.x + P1.y;
201  double ap = (P4.y - P3.y) / (P4.x - P3.x);
202  double bp = -ap * P3.x + P3.y;
203 
204  if (a == ap)
205  {
206  return false; // colineaire
207  }
208 
209  P.x = (bp - b) / (a - ap);
210  P.y = a * P.x + b;
211 
212  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
213  P.z = P1.z + (P.x - P1.x) * (P2.z - P1.z) / (P2.x - P1.x);
214 
215  return true;
216 }
#define SEUIL_DISTANCE_POINTS_CONFONDUS
Below a distance of 2 centimeters, the points are considered on the same location.
Point of a path.
double z
z coordinate of the point
static bool Confondus(TYPointParcours *P1, TYPointParcours *P2)
static bool IntersectionSegments(TYPointParcours &P1, TYPointParcours &P2, TYPointParcours &P3, TYPointParcours &P4, TYPointParcours &P, bool bP3OrP4MustNotCoincideWithP=false)
Return true if [P1P2] intersects [P3P4] if P1 or P2 coincide with the intersection point P,...
static double ZCross(TYPointParcours SR, TYPointParcours SP)
Return cross product applied to SR and SP points.
double y
y coordinate of the point
bool operator==(TYPointParcours &p)
Return true if this point and p are on same location.
static double Scalaire(TYPointParcours &P1, TYPointParcours &P2, TYPointParcours &P3, TYPointParcours &P4)
Compute the scalar product of the vector P1P2 and P3P4.
double x
x coordinate of the point
double normeCarree()
Return x^2+y^2.
static const double _dSeuilDistanceCarre
static bool IntersectionDroites(TYPointParcours &P1, TYPointParcours &P2, TYPointParcours &P3, TYPointParcours &P4, TYPointParcours &P)
static double AbscisseCurviligneCarreSurSR(TYPointParcours &P, TYPointParcours &S, TYPointParcours &R)
Return the square of the curvilinear abscissa of point P on [SR].
static TYPointParcours vecteur2D(TYPointParcours &P1, TYPointParcours &P2)
Compute the 2 dimensional vector P1P2 in the XY plane.