Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / Ice / IceRay.h
1 /*
2  *      ICE / OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose, 
10 including commercial applications, and to alter it and redistribute it freely, 
11 subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
16 */
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18 /**
19  *      Contains code for rays.
20  *      \file           IceRay.h
21  *      \author         Pierre Terdiman
22  *      \date           April, 4, 2000
23  */
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 // Include Guard
28 #ifndef __ICERAY_H__
29 #define __ICERAY_H__
30
31         class ICEMATHS_API Ray
32         {
33                 public:
34                 //! Constructor
35                 inline_                                 Ray()                                                                                                                           {}
36                 //! Constructor
37                 inline_                                 Ray(const Point& orig, const Point& dir) : mOrig(orig), mDir(dir)       {}
38                 //! Copy constructor
39                 inline_                                 Ray(const Ray& ray) : mOrig(ray.mOrig), mDir(ray.mDir)                          {}
40                 //! Destructor
41                 inline_                                 ~Ray()                                                                                                                          {}
42
43                                                 float   SquareDistance(const Point& point, float* t=null)       const;
44                 inline_                 float   Distance(const Point& point, float* t=null)                     const                   { return sqrtf(SquareDistance(point, t));       }
45
46                                                 Point   mOrig;          //!< Ray origin
47                                                 Point   mDir;           //!< Normalized direction
48         };
49
50         inline_ void ComputeReflexionVector(Point& reflected, const Point& incoming_dir, const Point& outward_normal)
51         {
52                 reflected = incoming_dir - outward_normal * 2.0f * (incoming_dir|outward_normal);
53         }
54
55         inline_ void ComputeReflexionVector(Point& reflected, const Point& source, const Point& impact, const Point& normal)
56         {
57                 Point V = impact - source;
58                 reflected = V - normal * 2.0f * (V|normal);
59         }
60
61         inline_ void DecomposeVector(Point& normal_compo, Point& tangent_compo, const Point& outward_dir, const Point& outward_normal)
62         {
63                 normal_compo = outward_normal * (outward_dir|outward_normal);
64                 tangent_compo = outward_dir - normal_compo;
65         }
66
67         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68         /**
69          *      Transforms a direction vector from world space to local space
70          *      \param          local_dir       [out] direction vector in local space
71          *      \param          world_dir       [in] direction vector in world space
72          *      \param          world           [in] world transform
73          */
74         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75         inline_ void ComputeLocalDirection(Point& local_dir, const Point& world_dir, const Matrix4x4& world)
76         {
77                 // Get world direction back in local space
78 //              Matrix3x3 InvWorld = world;
79 //              local_dir = InvWorld * world_dir;
80                 local_dir = Matrix3x3(world) * world_dir;
81         }
82
83         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
84         /**
85          *      Transforms a position vector from world space to local space
86          *      \param          local_pt        [out] position vector in local space
87          *      \param          world_pt        [in] position vector in world space
88          *      \param          world           [in] world transform
89          */
90         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
91         inline_ void ComputeLocalPoint(Point& local_pt, const Point& world_pt, const Matrix4x4& world)
92         {
93                 // Get world vertex back in local space
94                 Matrix4x4 InvWorld = world;
95                 InvWorld.Invert();
96                 local_pt = world_pt * InvWorld;
97         }
98
99         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
100         /**
101          *      Transforms a ray from world space to local space
102          *      \param          local_ray       [out] ray in local space
103          *      \param          world_ray       [in] ray in world space
104          *      \param          world           [in] world transform
105          */
106         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107         inline_ void ComputeLocalRay(Ray& local_ray, const Ray& world_ray, const Matrix4x4& world)
108         {
109                 // Get world ray back in local space
110                 ComputeLocalDirection(local_ray.mDir, world_ray.mDir, world);
111                 ComputeLocalPoint(local_ray.mOrig, world_ray.mOrig, world);
112         }
113
114 #endif // __ICERAY_H__