source: trunk/source/geometry/solids/BREPS/src/G4Ray.cc @ 836

Last change on this file since 836 was 831, checked in by garnier, 16 years ago

import all except CVS

File size: 7.7 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4Ray.cc,v 1.11 2006/06/29 18:42:35 gunter Exp $
28// GEANT4 tag $Name:  $
29//
30// ----------------------------------------------------------------------
31// GEANT 4 class source file
32//
33// G4Ray.cc
34//
35// ----------------------------------------------------------------------
36
37#include "G4Ray.hh"
38#include "G4PointRat.hh"
39
40G4Ray::G4Ray()
41{
42}
43
44G4Ray::G4Ray(const G4Point3D& start0, const G4Vector3D& dir0)
45{
46  Init(start0, dir0);
47}
48
49G4Ray::~G4Ray()
50{
51}
52
53
54const G4Plane& G4Ray::GetPlane(G4int number_of_plane) const
55{
56  if(number_of_plane==1)
57    return plane2;
58  else
59    return plane1;
60}
61
62
63void G4Ray::CreatePlanes()
64{
65  // Creates two orthogonal planes(plane1,plane2) the ray (rray)
66  // situated in the intersection of the planes. The planes are
67  // used to project the surface (nurb) in two dimensions.
68 
69  G4Vector3D RayDir    = dir;
70  G4Point3D  RayOrigin = start;
71
72  G4Point3D  p1, p2, p3, p4;
73  G4Vector3D dir1, dir2;
74  G4Vector3D invdir = G4Vector3D( PINFINITY );
75 
76  if(!NearZero(RayDir.x(), SQRT_SMALL_FASTF)) 
77    invdir.setX(1.0 / RayDir.x());
78   
79  if(!NearZero(RayDir.y(), SQRT_SMALL_FASTF)) 
80    invdir.setY(1.0 / RayDir.y());
81   
82  if(!NearZero(RayDir.z(), SQRT_SMALL_FASTF)) 
83    invdir.setZ(1.0 / RayDir.z());
84
85  MatVecOrtho(dir1, RayDir);
86 
87  Vcross( dir2, RayDir, dir1);
88  Vmove(p1, RayOrigin);
89  Vadd2(p2, RayOrigin, RayDir);
90  Vadd2(p3, RayOrigin, dir1);
91  Vadd2(p4, RayOrigin, dir2);
92
93  CalcPlane3Pts( plane1, p1, p3, p2);
94  CalcPlane3Pts( plane2, p1, p2, p4);
95}
96
97
98void G4Ray::MatVecOrtho(register G4Vector3D &out,
99                        register const G4Vector3D &in )
100{
101  register G4double f;
102  G4int             i_Which;
103
104  if(NearZero(in.x(), 0.0001) && NearZero(in.y(), 0.0001) &&
105     NearZero(in.z(), 0.0001) ) 
106  {
107    Vsetall( out, 0 );
108    return;
109  }
110 
111  //      Find component closest to zero
112  f = std::fabs(in.x());
113  i_Which=0;
114 
115  if( std::fabs(in.y()) < f )
116  {
117    f = std::fabs(in.y());
118    i_Which=1;
119  }
120 
121  if( std::fabs(in.z()) < f )
122    i_Which=2;
123 
124  if(!i_Which)
125    f = std::sqrt((in.y())*(in.y())+(in.z())*(in.z()));    // hypot(in.y(),in.z())
126  else
127    if(i_Which==1)
128      f = std::sqrt((in.z())*(in.z())+(in.x())*(in.x()));  // hypot(in.z(),in.x())
129    else
130      f = std::sqrt((in.x())*(in.x())+(in.y())*(in.y()));  // hypot(in.x(),in.y())
131 
132    if( NearZero( f, SMALL ) )
133    {
134      Vsetall( out, 0 );
135      return;
136    }
137   
138    f = 1.0/f;
139   
140    if(!i_Which)
141    {
142      out.setX(0.0);
143      out.setY(-in.z()*f);
144      out.setZ( in.y()*f);
145    }
146    else
147      if(i_Which==1)
148      {
149        out.setY(0.0);
150        out.setZ(-in.x()*f);
151        out.setX( in.y()*f);
152      }
153      else
154      {
155        out.setZ(0.0);
156        out.setX(-in.z()*f);
157        out.setY( in.y()*f);
158      }
159} 
160
161
162//                      CALC_PLANE_3PTS
163//
164//  Find the equation of a G4Plane that contains three points.
165//  Note that Normal vector created is expected to point out (see vmath.h),
166//  so the vector from A to C had better be counter-clockwise
167//  (about the point A) from the vector from A to B.
168//  This follows the outward-pointing Normal convention, and the
169//  right-hand rule for cross products.
170//
171/*
172                        C
173                        *
174                        |\
175                        | \
176           ^     N      |  \
177           |      \     |   \
178           |       \    |    \
179           |C-A     \   |     \
180           |         \  |      \
181           |          \ |       \
182                       \|        \
183                        *---------*
184                        A         B
185                           ----->
186                            B-A
187*/
188//  If the points are given in the order A B C (eg, *counter*-clockwise),
189//  then the outward pointing surface Normal N = (B-A) x (C-A).
190//
191//  Explicit Return -
192//       0      OK
193//      -1      Failure.  At least two of the points were not distinct,
194//              or all three were colinear.
195//
196//  Implicit Return -
197//      G4Plane   The G4Plane equation is stored here.
198
199
200G4int G4Ray::CalcPlane3Pts(G4Plane &plane1,
201                           const G4Point3D& a,
202                           const G4Point3D& b,
203                           const G4Point3D& c )
204{
205  // Creates the two orthogonal planes which are needed in projecting the
206  // surface into 2D.
207
208  G4Vector3D    B_A;
209  G4Vector3D    C_A;
210  G4Vector3D    C_B;
211 
212  register G4double mag;
213
214  Vsub2( B_A, b, a );
215  Vsub2( C_A, c, a );
216  Vsub2( C_B, c, b );
217
218  Vcross( plane1, B_A, C_A );
219
220  //    Ensure unit length Normal
221  mag = Magnitude(plane1);
222  if( mag  <= SQRT_SMALL_FASTF )
223    return(-1);//        FAIL
224 
225  mag = 1/mag;
226 
227  G4Plane pl2(plane1);
228  Vscale( plane1, pl2, mag );
229
230  //     Find distance from the origin to the G4Plane
231  plane1.d = Vdot( plane1, a );
232 
233  return(0);    //ok
234}
235
236
237void G4Ray::RayCheck()
238{
239  // Check that the ray has a G4Vector3D...
240  if (dir==G4Vector3D(0, 0, 0)) 
241  {
242    G4cout << "\nZero direction given. Exiting...\n";
243    exit(1);
244  }
245
246  // Make sure that the vector is unit length
247  dir= dir.unit();
248  r_min = 0;
249  r_max = 0;
250}
251
252
253void G4Ray::Vcross(G4Plane &a, const G4Vector3D &b, const G4Vector3D &c) 
254{ 
255  a.a = b.y()  * c.z()  - b.z()  * c.y() ;
256  a.b = b.z()  * c.x()  - b.x()  * c.z() ;
257  a.c = b.x()  * c.y()  - b.y()  * c.x() ;
258}
259
260
261void G4Ray::Vcross(G4Vector3D &a, const G4Vector3D &b, const G4Vector3D &c) 
262{ 
263  a.setX(b.y()  * c.z()  - b.z()  * c.y()) ;
264  a.setY(b.z()  * c.x()  - b.x()  * c.z()) ;
265  a.setZ(b.x()  * c.y()  - b.y()  * c.x()) ;
266}
267
268
269void G4Ray::Vmove(G4Point3D &a, const G4Point3D &b) 
270{ 
271  a.setX(b.x());
272  a.setY(b.y());
273  a.setZ(b.z());
274}
275
276
277void G4Ray::Vadd2(G4Point3D &a, const G4Point3D &b, const G4Vector3D &c) 
278{
279  a.setX(b.x() + c.x()) ;
280  a.setY(b.y() + c.y()) ;
281  a.setZ(b.z() + c.z()) ;
282}       
283 
284
285void G4Ray::Vsub2(G4Vector3D &a, const G4Point3D &b, const G4Point3D &c) 
286{
287  a.setX(b.x() - c.x());
288  a.setY(b.y() - c.y());
289  a.setZ(b.z() - c.z());
290}
291
292
293void G4Ray::Vscale(G4Plane& a, const G4Plane& b, G4double c) 
294{ 
295  a.a = b.a * c;
296  a.b = b.b * c;
297  a.c = b.c * c;
298}
299
300
301G4double G4Ray::Vdot(const G4Plane &a, const G4Point3D &b) 
302{
303  return (a.a * b.x() + 
304          a.b * b.y() + 
305          a.c * b.z());
306}
307 
308
309G4double G4Ray::Magsq(const G4Plane &a) 
310{
311  return ( a.a * a.a + a.b * a.b + a.c *a.c );
312}
313 
314
315G4double G4Ray::Magnitude(const G4Plane &a) 
316{
317  return (std::sqrt( Magsq( a )) );
318}
Note: See TracBrowser for help on using the repository browser.