KiCad PCB EDA Suite
raypacket.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include "raypacket.h"
31 #include "../3d_fastmath.h"
32 #include <wx/debug.h>
33 
34 
35 static void RAYPACKET_GenerateFrustum( FRUSTUM* m_Frustum, RAY* m_ray )
36 {
37  m_Frustum->GenerateFrustum(
38  m_ray[ 0 * RAYPACKET_DIM + 0 ],
39  m_ray[ 0 * RAYPACKET_DIM + (RAYPACKET_DIM - 1) ],
40  m_ray[ (RAYPACKET_DIM - 1) * RAYPACKET_DIM + 0 ],
41  m_ray[ (RAYPACKET_DIM - 1) * RAYPACKET_DIM + (RAYPACKET_DIM - 1) ] );
42 }
43 
44 
45 RAYPACKET::RAYPACKET( const CAMERA& aCamera, const SFVEC2I& aWindowsPosition )
46 {
47  unsigned int i = 0;
48 
49  for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
50  {
51  for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
52  {
53  SFVEC3F rayOrigin;
54  SFVEC3F rayDir;
55 
56  aCamera.MakeRay( SFVEC2I( aWindowsPosition.x + x, aWindowsPosition.y + y ),
57  rayOrigin, rayDir );
58 
59  m_ray[i].Init( rayOrigin, rayDir );
60 
61  i++;
62  }
63  }
64 
65  wxASSERT( i == RAYPACKET_RAYS_PER_PACKET );
66 
68 }
69 
70 
71 RAYPACKET::RAYPACKET( const CAMERA& aCamera, const SFVEC2F& aWindowsPosition )
72 {
73  RAYPACKET_InitRays( aCamera, aWindowsPosition, m_ray );
74 
76 }
77 
78 
79 RAYPACKET::RAYPACKET( const CAMERA& aCamera, const SFVEC2F& aWindowsPosition,
80  const SFVEC2F& a2DWindowsPosDisplacementFactor )
81 {
82  RAYPACKET_InitRays_with2DDisplacement( aCamera, aWindowsPosition,
83  a2DWindowsPosDisplacementFactor, m_ray );
84 
86 }
87 
88 
89 RAYPACKET::RAYPACKET( const CAMERA& aCamera, const SFVEC2I& aWindowsPosition,
90  const SFVEC3F& aDirectionDisplacementFactor )
91 {
92  unsigned int i = 0;
93 
94  for( unsigned int y = 0; y < RAYPACKET_DIM; ++y )
95  {
96  for( unsigned int x = 0; x < RAYPACKET_DIM; ++x )
97  {
98  SFVEC3F rayOrigin;
99  SFVEC3F rayDir;
100 
101  aCamera.MakeRay( SFVEC2I( aWindowsPosition.x + x, aWindowsPosition.y + y ),
102  rayOrigin, rayDir );
103 
104  const SFVEC3F randVector = SFVEC3F( Fast_RandFloat() * aDirectionDisplacementFactor.x,
105  Fast_RandFloat() * aDirectionDisplacementFactor.y,
106  Fast_RandFloat() * aDirectionDisplacementFactor.z );
107 
108  m_ray[i].Init( rayOrigin, glm::normalize( rayDir + randVector ) );
109 
110  i++;
111  }
112  }
113 
114  wxASSERT( i == RAYPACKET_RAYS_PER_PACKET );
115 
117 }
118 
119 
120 RAYPACKET::RAYPACKET( const CAMERA& aCamera, const SFVEC2I& aWindowsPosition,
121  unsigned int aPixelMultiple )
122 {
123  unsigned int i = 0;
124 
125  for( unsigned int y = 0; y < RAYPACKET_DIM; y++ )
126  {
127  for( unsigned int x = 0; x < RAYPACKET_DIM; x++ )
128  {
129  SFVEC3F rayOrigin;
130  SFVEC3F rayDir;
131 
132  aCamera.MakeRay( SFVEC2I( aWindowsPosition.x + x * aPixelMultiple,
133  aWindowsPosition.y + y * aPixelMultiple),
134  rayOrigin, rayDir );
135 
136  m_ray[i].Init( rayOrigin, rayDir );
137 
138  i++;
139  }
140  }
141 
142  wxASSERT( i == RAYPACKET_RAYS_PER_PACKET );
143 
145 }
146 
147 
148 void RAYPACKET_InitRays( const CAMERA& aCamera, const SFVEC2F& aWindowsPosition, RAY* aRayPck )
149 {
150  for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
151  {
152  for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
153  {
154  SFVEC3F rayOrigin;
155  SFVEC3F rayDir;
156 
157  aCamera.MakeRay( SFVEC2F( aWindowsPosition.x + (float)x,
158  aWindowsPosition.y + (float)y ),
159  rayOrigin, rayDir );
160 
161  aRayPck[i].Init( rayOrigin, rayDir );
162  }
163  }
164 }
165 
166 
167 void RAYPACKET_InitRays_with2DDisplacement( const CAMERA& aCamera, const SFVEC2F& aWindowsPosition,
168  const SFVEC2F& a2DWindowsPosDisplacementFactor,
169  RAY* aRayPck )
170 {
171  for( unsigned int y = 0, i = 0; y < RAYPACKET_DIM; ++y )
172  {
173  for( unsigned int x = 0; x < RAYPACKET_DIM; ++x, ++i )
174  {
175  SFVEC3F rayOrigin;
176  SFVEC3F rayDir;
177 
178  aCamera.MakeRay( SFVEC2F( aWindowsPosition.x +(float)x +
179  Fast_RandFloat() * a2DWindowsPosDisplacementFactor.x,
180  aWindowsPosition.y + (float)y +
181  Fast_RandFloat() * a2DWindowsPosDisplacementFactor.y ),
182  rayOrigin, rayDir );
183 
184  aRayPck[i].Init( rayOrigin, rayDir );
185  }
186  }
187 }
#define RAYPACKET_DIM
Definition: raypacket.h:37
RAY m_ray[RAYPACKET_RAYS_PER_PACKET]
Definition: raypacket.h:59
void Init(const SFVEC3F &o, const SFVEC3F &d)
Definition: ray.cpp:41
Definition: ray.h:67
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:39
float Fast_RandFloat()
Definition: 3d_fastmath.cpp:45
void RAYPACKET_InitRays_with2DDisplacement(const CAMERA &aCamera, const SFVEC2F &aWindowsPosition, const SFVEC2F &a2DWindowsPosDisplacementFactor, RAY *aRayPck)
Definition: raypacket.cpp:167
A class used to derive camera objects from.
Definition: camera.h:77
static void RAYPACKET_GenerateFrustum(FRUSTUM *m_Frustum, RAY *m_ray)
Definition: raypacket.cpp:35
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
void MakeRay(const SFVEC2I &aWindowPos, SFVEC3F &aOutOrigin, SFVEC3F &aOutDirection) const
Make a ray based on a windows screen position.
Definition: camera.cpp:308
FRUSTUM m_Frustum
Definition: raypacket.h:58
#define RAYPACKET_RAYS_PER_PACKET
Definition: raypacket.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
RAYPACKET(const CAMERA &aCamera, const SFVEC2I &aWindowsPosition)
Definition: raypacket.cpp:45
void GenerateFrustum(const RAY &topLeft, const RAY &topRight, const RAY &bottomLeft, const RAY &bottomRight)
Definition: frustum.cpp:41
void RAYPACKET_InitRays(const CAMERA &aCamera, const SFVEC2F &aWindowsPosition, RAY *aRayPck)
Definition: raypacket.cpp:148