Files
o3de/Code/Legacy/CryCommon/Cry_GeoIntersect.h
T
Artur K 45b2336dce Legacy cleanup (#3383)
* WIP - small legacy cleanup

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* More cleanups + build fixes

Use AZstd instead of std types in a few places.
Remove m_nameTable.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Legacy code cleanups

Remove unused methods using legacy functionality

* EditorViewportWidget::AdjustObjectPosition
* DisplayContext - remove `renderer` pointer
* DisplayContext - log errors when functions using `renderer` are called
* CTrackGizmo::DrawAxis - log errors when function uses `renderer`.
* Legacy CCamera - remove Project, Unproject and CalcScreenBounds
* Remove all unused methods from Cry_GeoDistance.h/Cry_GeoIntersect.h
* Remove Lineseg_Triangle from Cry_GeoOverlap.h
* IEntityRenderState.h - remove unused types
* SMeshColor remove Lerp method and associated constructor.
* IMaterial.h - remove unused types and a few methods
* IRenderMesh.h - remove a few unused methods and use int8 instead of
byte
* IRender.h - remove almost all of the contents
* IShader.h - remove unused types and a few methods
* IStatObj.h - remove unused types and a few methods
* SSystemGlobalEnvironment - remove `renderer` pointer
* IRenderGraph - remove 2 unused methods
* physinterface.h - remove almost all of the contents
* CXmlUtils no longer inherits ISystemEventListener
* CXmlNode no longer has custom new/delete
* Remove IRenderer from some test mocks.

Removed files:
* CryName.h
* Cry_MatrixDiag.h
* Cry_XOptimise.h
* HeapAllocator.h
* IRendererMock.h
* PoolAllocator.h

Things to consider:
* Remove GetMemoryUsage & friends.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Apply review suggestions

IMovieSystem.h - remove unused includes.

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Move unreachable code to `#if 0` block

This is hopefully temporary measure until the original functionality is
re-implemented

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Fix bad merge I messed up.

Fix bad merge I messed up, by re-removing FrameProfiler.h from crycommon_files.cmake (this was removed in an earlier commit this morning: https://github.com/o3de/o3de/pull/3394).

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Update Code/Framework/AzCore/AzCore/std/string/string_view.h

Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* implement review suggestion

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* following review, using LYSHINE_ATOM_TODO to guard

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* Remove commented out include

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

* EditorViewportWidget.cpp: Convert commented out code to  guarded one

Signed-off-by: nemerle <96597+nemerle@users.noreply.github.com>

Co-authored-by: bosnichd <bosnichd@amazon.com>
Co-authored-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-08-25 15:04:38 -06:00

219 lines
8.8 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
// Description : Common intersection-tests
#pragma once
#include <Cry_Geo.h>
namespace Intersect {
inline bool Ray_Plane(const Ray& ray, const Plane_tpl<f32>& plane, Vec3& output, bool bSingleSidePlane = true)
{
float cosine = plane.n | ray.direction;
//REJECTION 1: if "line-direction" is perpendicular to "plane-normal", an intersection is not possible! That means ray is parallel
// to the plane
//REJECTION 2: if bSingleSidePlane == true we deal with single-sided planes. That means
// if "line-direction" is pointing in the same direction as "the plane-normal",
// an intersection is not possible!
if ((cosine == 0.0f) || // normal is orthogonal to vector, cant intersect
(bSingleSidePlane && (cosine > 0.0f))) // we are trying to find an intersection in the same direction as the plane normal
{
return false;
}
float numer = plane.DistFromPlane(ray.origin);
float fLength = -numer / cosine;
output = ray.origin + (ray.direction * fLength);
//skip, if cutting-point is "behind" ray.origin
if (fLength < 0.0f)
{
return false;
}
return true; //intersection occurred
}
/*
* calculates intersection between a ray and a triangle.
* IMPORTANT: this is a single-sided intersection test. That means its not sufficient
* that the triangle and rayt overlap, its also important that the triangle
* is "visible" when you from the origin along the ray-direction.
*
* If you need a double-sided test, you'll have to call this function twice with
* reversed order of triangle vertices.
*
* return values
* if there is an intertection the functions return "true" and stores the
* 3d-intersection point in "output". if the function returns "false" the value in
* "output" is undefined
*/
inline bool Ray_Triangle(const Ray& ray, const Vec3& v0, const Vec3& v1, const Vec3& v2, Vec3& output)
{
const float Epsilon = 0.0000001f;
Vec3 edgeA = v1 - v0;
Vec3 edgeB = v2 - v0;
Vec3 dir = ray.direction;
Vec3 p = dir.Cross(edgeA);
Vec3 t = ray.origin - v0;
Vec3 q = t.Cross(edgeB);
float dot = edgeB.Dot(p);
float u = t.Dot(p);
float v = dir.Dot(q);
float DotGreaterThanEpsilon = dot - Epsilon;
float VGreaterEqualThanZero = v;
float UGreaterEqualThanZero = u;
float UVLessThanDot = dot - (u + v);
float ULessThanDot = dot - u;
float UVGreaterEqualThanZero = (float)fsel(VGreaterEqualThanZero, UGreaterEqualThanZero, VGreaterEqualThanZero);
float UUVLessThanDot = (float)fsel(UVLessThanDot, ULessThanDot, UVLessThanDot);
float BothGood = (float)fsel(UVGreaterEqualThanZero, UUVLessThanDot, UVGreaterEqualThanZero);
float AllGood = (float)fsel(DotGreaterThanEpsilon, BothGood, DotGreaterThanEpsilon);
if (AllGood < 0.0f)
{
return false;
}
float dt = edgeA.Dot(q) / dot;
Vec3 result = (dir * dt) + ray.origin;
output = result;
float AfterStart = (result - ray.origin).Dot(dir);
return AfterStart >= 0.0f;
}
//----------------------------------------------------------------------------------
// Ray_AABB
//
// just ONE intersection point is calculated, and thats the entry point -
// Lineseg and AABB are assumed to be in the same space
//
//--- 0x00 = no intersection (output undefined) --------------------------
//--- 0x01 = intersection (intersection point in output) --------------
//--- 0x02 = start of Lineseg is inside the AABB (ls.start is output)
//----------------------------------------------------------------------------------
inline uint8 Ray_AABB(const Ray& ray, const AABB& aabb, Vec3& output1)
{
uint8 cflags;
float cosine;
Vec3 cut;
//--------------------------------------------------------------------------------------
//---- check if "ray.origin" is inside of AABB ---------------------------
//--------------------------------------------------------------------------------------
cflags = (ray.origin.x >= aabb.min.x) << 0;
cflags |= (ray.origin.x <= aabb.max.x) << 1;
cflags |= (ray.origin.y >= aabb.min.y) << 2;
cflags |= (ray.origin.y <= aabb.max.y) << 3;
cflags |= (ray.origin.z >= aabb.min.z) << 4;
cflags |= (ray.origin.z <= aabb.max.z) << 5;
if (cflags == 0x3f)
{
output1 = ray.origin;
return 0x02;
}
//--------------------------------------------------------------------------------------
//---- check intersection with planes ------------------------------
//--------------------------------------------------------------------------------------
for (int i = 0; i < 3; i++)
{
if ((ray.direction[i] > 0) && (ray.origin[i] < aabb.min[i]))
{
cosine = (-ray.origin[i] + aabb.min[i]) / ray.direction[i];
cut[i] = aabb.min[i];
cut[incm3(i)] = ray.origin[incm3(i)] + (ray.direction[incm3(i)] * cosine);
cut[decm3(i)] = ray.origin[decm3(i)] + (ray.direction[decm3(i)] * cosine);
if ((cut[incm3(i)] > aabb.min[incm3(i)]) && (cut[incm3(i)] < aabb.max[incm3(i)]) && (cut[decm3(i)] > aabb.min[decm3(i)]) && (cut[decm3(i)] < aabb.max[decm3(i)]))
{
output1 = cut;
return 0x01;
}
}
if ((ray.direction[i] < 0) && (ray.origin[i] > aabb.max[i]))
{
cosine = (+ray.origin[i] - aabb.max[i]) / ray.direction[i];
cut[i] = aabb.max[i];
cut[incm3(i)] = ray.origin[incm3(i)] - (ray.direction[incm3(i)] * cosine);
cut[decm3(i)] = ray.origin[decm3(i)] - (ray.direction[decm3(i)] * cosine);
if ((cut[incm3(i)] > aabb.min[incm3(i)]) && (cut[incm3(i)] < aabb.max[incm3(i)]) && (cut[decm3(i)] > aabb.min[decm3(i)]) && (cut[decm3(i)] < aabb.max[decm3(i)]))
{
output1 = cut;
return 0x01;
}
}
}
return 0x00;//no intersection
}
//----------------------------------------------------------------------------------
//--- 0x00 = no intersection --------------------------
//--- 0x01 = not possible --
//--- 0x02 = one intersection, lineseg has just an EXIT point but no ENTRY point (ls.start is inside the sphere) --
//--- 0x03 = two intersection, lineseg has ENTRY and EXIT point --
//----------------------------------------------------------------------------------
inline unsigned char Ray_Sphere(const Ray& ray, const ::Sphere& s, Vec3& i0, Vec3& i1)
{
Vec3 end = ray.origin + ray.direction;
float a = ray.direction | ray.direction;
float b = (ray.direction | (ray.origin - s.center)) * 2.0f;
float c = ((ray.origin - s.center) | (ray.origin - s.center)) - (s.radius * s.radius);
float desc = (b * b) - (4 * a * c);
unsigned char intersection = 0;
if (desc >= 0.0f)
{
float lamba0 = (-b - sqrt_tpl(desc)) / (2.0f * a);
// _stprintf(d3dApp.token,"lamba0: %20.12f",lamba0);
// d3dApp.m_pFont->DrawText( 2, d3dApp.PrintY, D3DCOLOR_ARGB(255,255,255,0), d3dApp.token ); d3dApp.PrintY+=20;
if (lamba0 > 0.0f)
{
i0 = ray.origin + ((end - ray.origin) * lamba0);
intersection = 1;
}
float lamba1 = (-b + sqrt_tpl(desc)) / (2.0f * a);
// _stprintf(d3dApp.token,"lamba1: %20.12f",lamba1);
// d3dApp.m_pFont->DrawText( 2, d3dApp.PrintY, D3DCOLOR_ARGB(255,255,255,0), d3dApp.token ); d3dApp.PrintY+=20;
if (lamba1 > 0.0f)
{
i1 = ray.origin + ((end - ray.origin) * lamba1);
intersection |= 2;
}
}
return intersection;
}
inline bool Ray_SphereFirst(const Ray& ray, const ::Sphere& s, Vec3& intPoint)
{
Vec3 p2;
unsigned char res = Ray_Sphere(ray, s, intPoint, p2);
if (res == 2)
{
intPoint = p2;
}
if (res > 1)
{
return true;
}
return false;
}
} //Intersect