Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYPointParcours.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) 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.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "TYPointParcours.h"
21 
22 #include <math.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 
29 
31 {
32  TYPointParcours res = P2;
33  res.x -= P1.x;
34  res.y -= P1.y;
35  return res;
36 }
37 
39 {
40  double zCross = (SR.x * SP.y) - (SR.y * SP.x);
41  return zCross;
42 }
43 
45 {
46  return (x * x + y * y);
47 }
48 
50 {
51  return Confondus(this, &p);
52 }
53 
55 {
56  double XDiff = (P1->x - P2->x);
57  double YDiff = (P1->y - P2->y);
58  double dNormeCarre = XDiff * XDiff + YDiff * YDiff;
59  return dNormeCarre < _dSeuilDistanceCarre;
60 }
61 
63  TYPointParcours& R)
64 {
65  // On calcul a peu de chose pres l'abscisse curviligne de P sur [SR]
66  //"a peu de chose pres" = c'est en fait le carre de l'abscisse, en gardant son signe
67  // permet d'avoir une relation d'ordre sur [SR] (sqrt est inutile pour cela)
68  // P est-il de l'autre côté de S par rapport a R ?
69  bool bDuMemeCoteQueR = Scalaire(S, R, S, P) >= 0;
70  TYPointParcours SR = vecteur2D(S, R);
71  TYPointParcours SP = vecteur2D(S, P);
72  double dNormeCSR = SR.normeCarree();
73  if (0 == dNormeCSR)
74  {
75  return 0; // evitons les divisions par zero...
76  }
77  double dNormeCSP = SP.normeCarree();
78  double dAbscisseCarree = (bDuMemeCoteQueR ? dNormeCSP / dNormeCSR : -dNormeCSP / dNormeCSR);
79  return dAbscisseCarree;
80 }
81 
83 {
84  return vecteur1.x * vecteur2.x + vecteur1.y * vecteur2.y;
85 }
86 
88  TYPointParcours& P4)
89 {
90  // return (P2.x - P1.x)*(P4.x - P3.x) + (P2.y - P1.y)*(P4.y - P3.y);
91  TYPointParcours aTYPointParcours_P1P2 = vecteur2D(P1, P2), aTYPointParcours_P3P4 = vecteur2D(P3, P4);
92  return Scalaire(aTYPointParcours_P1P2, aTYPointParcours_P3P4);
93 }
94 
97  bool bP3OrP4MustNotCoincideWithP)
98 {
99  // 2. P2 & P3 sont-ils du meme côté de P1P2 ?
100  TYPointParcours P1P2 = vecteur2D(P1, P2);
101  TYPointParcours P1P3 = vecteur2D(P1, P3);
102  TYPointParcours P1P4 = vecteur2D(P1, P4);
103  double detP3 = ZCross(P1P2, P1P3);
104  double detP4 = ZCross(P1P2, P1P4);
105  if (detP3 && detP4 && (detP3 * detP4) > 0)
106  {
107  return false;
108  }
109 
110  bool bIntersectionDroites = IntersectionDroites(P1, P2, P3, P4, P);
111  if (!bIntersectionDroites)
112  {
113  return false;
114  }
115  // P est-il confondu avec P1 ?
116  if (Confondus(&P1, &P))
117  {
118  return false;
119  }
120 
121  // P est-il confondu avec P2 ?
122  if (Confondus(&P2, &P))
123  {
124  return false;
125  }
126 
127  // P est-il confondu avec P3 ?
128  if (Confondus(&P3, &P) && bP3OrP4MustNotCoincideWithP)
129  {
130  return false;
131  }
132 
133  // P est-il confondu avec P4 ?
134  if (Confondus(&P4, &P) && bP3OrP4MustNotCoincideWithP)
135  {
136  return false;
137  }
138 
139  // Le point P est-il de l'autre côté de P1 par rapport a P2 ?
140  if (Scalaire(P1, P, P1, P2) <= 0)
141  {
142  return false;
143  }
144 
145  // Le point P est-il sur P1P2 ?
146  TYPointParcours P1P = vecteur2D(P1, P);
147  if (P1P.normeCarree() >= P1P2.normeCarree())
148  {
149  return false;
150  }
151 
152  return true;
153 }
154 
157 {
158  P.x = P1.x;
159  P.y = P1.y;
160  P.z = P1.z; // conservons l'indentifiant
161  double alpha = NAN;
162  // Eq de droite P1P2
163  if (P2.x == P1.x)
164  {
165  // droite verticale
166  P.x = P1.x;
167  if (P4.x == P3.x)
168  {
169  return false; // colineaire
170  }
171  // P3P = alphaP3P4
172  alpha = (P.x - P3.x) / (P4.x - P3.x);
173  P.y = P3.y + alpha * (P4.y - P3.y);
174  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
175  P.z = P3.z + alpha * (P4.z - P3.z);
176  return true;
177  }
178  else if (P2.y == P1.y)
179  {
180  // droite horizontale
181  P.y = P1.y;
182  if (P4.y == P3.y)
183  {
184  return false; // colineaire
185  }
186  // P3P = alphaP3P4
187  alpha = (P.y - P3.y) / (P4.y - P3.y);
188  P.x = P3.x + alpha * (P4.x - P3.x);
189  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
190  P.z = P3.z + alpha * (P4.z - P3.z);
191  return true;
192  }
193  else if ((P4.y == P3.y) || (P4.x == P3.x))
194  {
195  return IntersectionDroites(P3, P4, P1, P2, P);
196  }
197 
198  double a = (P2.y - P1.y) / (P2.x - P1.x);
199  double b = -a * P1.x + P1.y;
200  double ap = (P4.y - P3.y) / (P4.x - P3.x);
201  double bp = -ap * P3.x + P3.y;
202 
203  if (a == ap)
204  {
205  return false; // colineaire
206  }
207 
208  P.x = (bp - b) / (a - ap);
209  P.y = a * P.x + b;
210 
211  // Calculons la 3e coordonnee; on sait que les vecteurs ne sont pas colineaires
212  P.z = P1.z + (P.x - P1.x) * (P2.z - P1.z) / (P2.x - P1.x);
213 
214  return true;
215 }
#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.