Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Sphere.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 #include "Sphere.h"
17 
19 {
20  // intersection rayon/sphere
21  vec3 dist = position - ray.getPosition();
22  decimal B = ray.getDirection() * dist;
23  decimal D = B * B - dist * dist + radius * radius;
24  if (D < 0.0f)
25  {
26  return false;
27  }
28  float t0 = B - sqrtf(D);
29  float t1 = B + sqrtf(D);
30  if ((t0 > 0.1f) && (t0 < ray.getMaxt()))
31  {
32  inter.t = t0;
33  inter.p = this;
34  inter.forme = SPHERE;
35  return true;
36  }
37  if ((t1 > 0.1f) && (t1 < ray.getMaxt()))
38  {
39  inter.t = t1;
40  inter.p = this;
41  inter.forme = SPHERE;
42  return true;
43  }
44  return false;
45 }
@ SPHERE
Definition: Shape.h:38
: Describes a ray by a pair of unsigned int. The first one gives the source number (in the range 0-40...
Definition: Ray.h:38
decimal getMaxt() const
Return maxt.
Definition: Ray.h:326
vec3 getPosition() const
Return starting point ray.
Definition: Ray.h:356
vec3 getDirection() const
Return direction of the ray.
Definition: Ray.h:346
virtual bool getIntersection(Ray &ray, Intersection &inter)
Check if a ray intersect this sphere.
Definition: Sphere.cpp:18
decimal radius
Radius of the sphere.
Definition: Sphere.h:98
vec3 position
Center of the sphere.
Definition: Sphere.h:97
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:387
Intersection struct.
Definition: Shape.h:46
decimal t
Definition: Shape.h:48
FORM forme
Definition: Shape.h:50
Shape * p
Definition: Shape.h:49