Code_TYMPAN  4.4.0
Industrial site acoustic simulation
Sphere.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 #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:381
Intersection struct.
Definition: Shape.h:46
decimal t
Definition: Shape.h:48
FORM forme
Definition: Shape.h:50
Shape * p
Definition: Shape.h:49