Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add triangle/ray and gltf/ray intersect function with tests #777

Merged
merged 88 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
ad512a3
add gltf ray intersect function with tests
Dec 12, 2023
12d6885
add more intersection tests
Dec 17, 2023
ed52c05
Merge branch 'main' into gltf-ray-intersect
Jan 2, 2024
c6bcf97
update and comment intersection functions
Jan 2, 2024
b765a73
update from main
csciguy8 May 6, 2024
0ad3192
revert changes
csciguy8 May 6, 2024
e55105c
revert draco change
csciguy8 May 6, 2024
6143a7e
revert ktx software change
csciguy8 May 6, 2024
54da172
Add more hit result info to ::intersectRayGltfModel
csciguy8 May 15, 2024
5bcfe34
Let intersectRayGltfModel HitResult return meshId correctly
csciguy8 May 15, 2024
ccce2f0
Let intersectRayGltfModel HitResult return primitiveId correctly
csciguy8 May 15, 2024
a01ef4c
Store primitiveToWorld transform in the hit result
csciguy8 May 15, 2024
54b352b
Add missing initializers
csciguy8 May 15, 2024
1fed700
Fix primitiveToWorld not getting copied to hit result
csciguy8 May 15, 2024
48db4d0
Rework tests to also traverse into model using hit results
csciguy8 May 15, 2024
39fc592
Fix check to not generate a warning
csciguy8 May 15, 2024
fad02b5
Fix casting warnings
csciguy8 May 15, 2024
f13d733
Fix bug where intersect test would hit tris behind the ray. Add more …
csciguy8 May 16, 2024
8ba0507
Fix warning
csciguy8 May 16, 2024
c35b8d3
Undo unnecessary changes
csciguy8 May 16, 2024
cd8edc2
Update code documentation
csciguy8 May 16, 2024
90755af
Rename getPointAlongRay to pointFromDistance
csciguy8 May 16, 2024
2924ede
Add tests for intersections with model with transformation matrix
csciguy8 May 16, 2024
bc8a918
Add some test glbs
csciguy8 May 17, 2024
bef7cbf
update test generation scripts
csciguy8 May 21, 2024
a0b49ce
Explicitly declare triangle mode
csciguy8 May 21, 2024
25331a9
Add cube with triangle strip test
csciguy8 May 21, 2024
018c764
Remove unneeded test files
csciguy8 May 21, 2024
c5ebb81
Add cube fan example and generation script
csciguy8 May 22, 2024
2e5ff62
switch to using new unit cube test model
csciguy8 May 22, 2024
1c73911
Stub out the rest of the unit cube tests
csciguy8 May 22, 2024
938e5cd
Remove unnecessary test asset
csciguy8 May 22, 2024
7af2344
update format
csciguy8 May 22, 2024
41ee75b
Flesh out non-indexed code path
csciguy8 May 22, 2024
dc0f4d6
Add non-indexed cube test and fix logic bugs
csciguy8 May 23, 2024
b0a92ff
Add proper support for triangle strips and enable test
csciguy8 May 23, 2024
331cd18
Add indexed triangle strip test cube
csciguy8 May 23, 2024
aedb8cc
Add proper support for indexed triangle strips and enable test
csciguy8 May 23, 2024
1318561
Add triangle fan support and enable test
csciguy8 May 23, 2024
7ea3f2e
Add indexed triangle fan test cube
csciguy8 May 23, 2024
e3320e4
Add proper support for indexed triangle fans and enable test
csciguy8 May 23, 2024
1734847
Fix bug where ray AABB test would always miss (missing .transform call)
csciguy8 May 28, 2024
9b0640d
Update code doc
csciguy8 May 28, 2024
cab5d1e
Fix bug where a scaled gltf would produce incorrect hit results. Add …
csciguy8 May 30, 2024
5a50038
Update code doc
csciguy8 May 30, 2024
84eb490
Use ::createAccessorView to clean up handling of index format variations
csciguy8 May 31, 2024
905eb94
let findClosestIndexedRayHit specify a templated AccessorView for ind…
csciguy8 May 31, 2024
a9c5658
Add checks for invalid indices. Fix casting for AccessorView operations
csciguy8 May 31, 2024
402a485
Ignore float component types for index buffers
csciguy8 May 31, 2024
d4ec50d
Add rejection of non-float positions
csciguy8 May 31, 2024
5d72dcf
Fix bug where intersecting gltfs with nodes with different scaling co…
csciguy8 May 31, 2024
b0b48bd
Update comments from peer review
csciguy8 May 31, 2024
dcee2c8
warning fix
csciguy8 May 31, 2024
aa95514
update from main
csciguy8 May 31, 2024
847763e
formatting
csciguy8 May 31, 2024
014a3c3
revert unintentional change
csciguy8 May 31, 2024
5092d26
More warnings fixes and cleanup
csciguy8 May 31, 2024
dfea268
Remove redundant move (build warning)
csciguy8 May 31, 2024
2fd0b3a
Add quantized cube test data
csciguy8 Jun 12, 2024
7b58901
Add support for gltf quantized mesh extension (non-float positions)
csciguy8 Jun 12, 2024
1854ecf
Fix bug where first triangle in strip would get skipped
csciguy8 Jun 12, 2024
31aeae7
Change iteration cadence to protect against incomplete triangles
csciguy8 Jun 12, 2024
7bdd210
Change index component types for more unit test coverage
csciguy8 Jun 12, 2024
7c291b5
Add cube translation test generated from script
csciguy8 Jun 12, 2024
1f6ac44
Merge branch 'main' into gltf-ray-intersect
csciguy8 Jun 12, 2024
df5a3a5
Rework result of intersect function to include warnings for malformed…
csciguy8 Jun 13, 2024
6731089
Add intentionally invalid models, and corresponding unit tests
csciguy8 Jun 13, 2024
728668a
Merge branch 'main' into gltf-ray-intersect
csciguy8 Jul 8, 2024
aece17c
Update CHANGES.md
csciguy8 Jul 8, 2024
ca25ad4
Change int to int32_t when specifying an ID
csciguy8 Jul 8, 2024
5754226
Update CHANGES.md
csciguy8 Jul 8, 2024
4f47b7b
update cullBackFaces doc
csciguy8 Jul 8, 2024
04f17a3
Change some occurrences of double 1 to 1.0
csciguy8 Jul 8, 2024
9a4073f
Add some missing this-> code
csciguy8 Jul 8, 2024
16d18a1
Remove duplicate implementations of ::readFile
csciguy8 Jul 8, 2024
888f611
Remove unneeded decltypes
csciguy8 Jul 9, 2024
7cda07f
Remove occurrences of including <glm/mat4x4.hpp> in header files
csciguy8 Jul 9, 2024
523f74f
Optimize by normalizing a vec3 instead of a vec4
csciguy8 Jul 9, 2024
2e6695d
Update documentation and hit result cleanup
csciguy8 Jul 9, 2024
7f2d18b
Replace assert with getSafe for index accessor
csciguy8 Jul 9, 2024
3b6c5a4
Rewrite to not rely on != 1.0 checks
csciguy8 Jul 9, 2024
8b9b001
Merge branch 'main' into gltf-ray-intersect
kring Jul 23, 2024
d03181a
Revert changes to ForEachPrimitiveInSceneXXX callbacks
csciguy8 Jul 24, 2024
a4fab06
Revert another unneeded change
csciguy8 Jul 24, 2024
5cb0d5e
formatting
csciguy8 Jul 24, 2024
7b5382e
let rayTriangleParametric return an optional
csciguy8 Jul 24, 2024
6213047
Change rayAABBParametric to return a std optional
csciguy8 Jul 25, 2024
1fa8ee4
Get rid of more t return params, update docs
csciguy8 Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

##### Breaking Changes :mega:

- `ForEachPrimitiveInSceneCallback` now passes `meshId` and `primitiveId` to the caller. Existing callbacks will need to add these new parameters
- `AccessorWriter` constructor now takes `std::byte*` instead of `uint8_t*`.

##### Additions :tada:

- Added `rayTriangle` intersection function that returns the intersection point between a ray and a triangle.
- Added `intersectRayGltfModel` intersection function that returns the first intersection point between a ray and a glTF model.
- Added `convertAccessorComponentTypeToPropertyComponentType`, which converts integer glTF accessor component types to their best-fitting `PropertyComponentType`.

### v0.37.0 - 2024-07-01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <CesiumGltf/Model.h>
#include <CesiumGltfReader/GltfReader.h>

#include <glm/mat4x4.hpp>
#include <gsl/span>

#include <optional>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <glm/mat4x4.hpp>
#include <glm/fwd.hpp>

#include <optional>

Expand Down
2 changes: 2 additions & 0 deletions Cesium3DTilesContent/src/TileTransform.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <Cesium3DTiles/Tile.h>
#include <Cesium3DTilesContent/TileTransform.h>

#include <glm/mat4x4.hpp>

namespace Cesium3DTilesContent {

std::optional<glm::dmat4>
Expand Down
1 change: 1 addition & 0 deletions Cesium3DTilesContent/test/TestTileTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <CesiumUtility/Math.h>

#include <catch2/catch.hpp>
#include <glm/mat4x4.hpp>

using namespace CesiumUtility;

Expand Down
16 changes: 1 addition & 15 deletions Cesium3DTilesReader/test/TestTilesetReader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Cesium3DTiles/Extension3dTilesBoundingVolumeS2.h>
#include <Cesium3DTilesReader/TilesetReader.h>
#include <CesiumJsonReader/JsonReader.h>
#include <CesiumNativeTests/readFile.h>

#include <catch2/catch.hpp>
#include <glm/vec3.hpp>
Expand All @@ -11,21 +12,6 @@
#include <fstream>
#include <string>

namespace {
std::vector<std::byte> readFile(const std::filesystem::path& fileName) {
std::ifstream file(fileName, std::ios::binary | std::ios::ate);
REQUIRE(file);

std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<std::byte> buffer(static_cast<size_t>(size));
file.read(reinterpret_cast<char*>(buffer.data()), size);

return buffer;
}
} // namespace

TEST_CASE("Reads tileset JSON") {
using namespace std::string_literals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <CesiumAsync/Future.h>
#include <CesiumRasterOverlays/IPrepareRasterOverlayRendererResources.h>

#include <glm/mat4x4.hpp>
#include <glm/vec2.hpp>
#include <gsl/span>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <CesiumUtility/DoublyLinkedList.h>

#include <glm/common.hpp>
#include <glm/mat4x4.hpp>
#include <gsl/span>

#include <atomic>
Expand Down
14 changes: 1 addition & 13 deletions Cesium3DTilesSelection/test/TestTilesetSelectionAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <CesiumNativeTests/SimpleAssetRequest.h>
#include <CesiumNativeTests/SimpleAssetResponse.h>
#include <CesiumNativeTests/SimpleTaskProcessor.h>
#include <CesiumNativeTests/readFile.h>
#include <CesiumUtility/Math.h>

#include <catch2/catch.hpp>
Expand All @@ -25,19 +26,6 @@ using namespace CesiumGeospatial;
using namespace CesiumUtility;
using namespace CesiumNativeTests;

static std::vector<std::byte> readFile(const std::filesystem::path& fileName) {
std::ifstream file(fileName, std::ios::binary | std::ios::ate);
REQUIRE(file);

std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

std::vector<std::byte> buffer(static_cast<size_t>(size));
file.read(reinterpret_cast<char*>(buffer.data()), size);

return buffer;
}

static bool doesTileMeetSSE(
const ViewState& viewState,
const Tile& tile,
Expand Down
123 changes: 123 additions & 0 deletions CesiumGeometry/include/CesiumGeometry/IntersectionTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
namespace CesiumGeometry {
class Ray;
class Plane;
struct AxisAlignedBox;
class OrientedBoundingBox;
class BoundingSphere;

/**
* @brief Functions for computing the intersection between geometries such as
Expand Down Expand Up @@ -99,6 +102,126 @@ class CESIUMGEOMETRY_API IntersectionTests final {
const glm::dvec3& triangleVertB,
const glm::dvec3& triangleVertC,
glm::dvec3& barycentricCoordinates) noexcept;

/**
* @brief Tests if a ray hits a triangle and returns the hit point.
*
* @param ray The ray.
* @param p0 The first vertex of the triangle.
* @param p1 The second vertex of the triangle.
* @param p2 The third vertex of the triangle.
* @param cullBackFaces Ignore triangles that face away from ray. Front faces
* use CCW winding order.
* @return The point of intersection, or `std::nullopt` if there is no
* intersection.
*/
static std::optional<glm::dvec3> rayTriangle(
const Ray& ray,
const glm::dvec3& p0,
const glm::dvec3& p1,
const glm::dvec3& p2,
bool cullBackFaces = false);

/**
* @brief Tests if an infinite ray hits a triangle and returns the parametric
* hit position.
*
* The return parameter is positive if the intersection point is in front of
* the ray origin, negative if it is behind it, or zero if the two points
* coincide.
*
* @param ray The ray.
* @param p0 The first vertex of the triangle.
* @param p1 The second vertex of the triangle.
* @param p2 The third vertex of the triangle.
* @param cullBackFaces Ignore triangles that face away from ray. Front faces
* use CCW winding order.
* @return optional hit parametric value, if one is detected.
*/
static std::optional<double> rayTriangleParametric(
const Ray& ray,
const glm::dvec3& p0,
const glm::dvec3& p1,
const glm::dvec3& p2,
bool cullBackFaces = false);

/**
* @brief Computes the intersection of a ray and an axis aligned bounding box.
*
* @param ray The ray.
* @param aabb The axis aligned bounding box.
* @return The point of intersection, or `std::nullopt` if there is no
* intersection.
*/
static std::optional<glm::dvec3>
rayAABB(const Ray& ray, const AxisAlignedBox& aabb);

/**
* @brief Computes the intersection of an infinite ray and an axis aligned
* bounding box and returns the parametric hit position.
*
* The return value is positive if the intersection point is in front of the
* ray origin, negative if it is behind it, or zero if the two points
* coincide.
*
* @param ray The ray.
* @param aabb The axis aligned bounding box.
* @return optional hit parametric value, if one is detected.
*/
static std::optional<double>
rayAABBParametric(const Ray& ray, const AxisAlignedBox& aabb);

/**
* @brief Computes the intersection of a ray and an oriented bounding box.
*
* @param ray The ray.
* @param obb The oriented bounding box.
* @return The point of intersection, or `std::nullopt` if there is no
* intersection.
*/
static std::optional<glm::dvec3>
rayOBB(const Ray& ray, const OrientedBoundingBox& obb);

/**
* @brief Computes the intersection of an infinite ray and an oriented
* bounding box and returns the parametric hit position.
*
* The return parameter is positive if the intersection point is in front of
* the ray origin, negative if it is behind it, or zero if the two points
* coincide.
*
* @param ray The ray.
* @param obb The oriented bounding box.
* @return optional hit parametric value, if one is detected.
*/
static std::optional<double>
rayOBBParametric(const Ray& ray, const OrientedBoundingBox& obb);

/**
* @brief Computes the intersection of a ray and a bounding sphere.
*
* @param ray The ray.
* @param sphere The bounding sphere.
* @return The point of intersection, or `std::nullopt` if there is no
* intersection.
*/
static std::optional<glm::dvec3>
raySphere(const Ray& ray, const BoundingSphere& sphere);

/**
* @brief Computes the intersection of an infinite ray and a bounding sphere
* and returns the parametric hit position.
*
* The return parameter is positive if the intersection point is in front of
* the ray origin, negative if it is behind it, or zero if the two points
* coincide.
*
* @param ray The ray.
* @param sphere The bounding sphere.
* @return optional hit parametric value, if one is detected.
*/
static std::optional<double>
raySphereParametric(const Ray& ray, const BoundingSphere& sphere);
};

} // namespace CesiumGeometry
19 changes: 19 additions & 0 deletions CesiumGeometry/include/CesiumGeometry/Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Library.h"

#include <glm/fwd.hpp>
#include <glm/vec3.hpp>

namespace CesiumGeometry {
Expand Down Expand Up @@ -32,6 +33,24 @@ class CESIUMGEOMETRY_API Ray final {
*/
const glm::dvec3& getDirection() const noexcept { return this->_direction; }

/**
* @brief Calculates a point on the ray that corresponds to the given
* distance from origin. Can be positive, negative, or 0.
*
* @param distance Desired distance from origin
* @return The point along the ray.
*/
glm::dvec3 pointFromDistance(double distance) const noexcept;

/**
* @brief Transforms the ray using a given 4x4 transformation matrix.
*
* @param transformation The 4x4 transformation matrix used to transform the
* ray.
* @return The transformed ray.
*/
Ray transform(const glm::dmat4x4& transformation) const noexcept;

/**
* @brief Constructs a new ray with its direction opposite this one.
*/
Expand Down
1 change: 0 additions & 1 deletion CesiumGeometry/include/CesiumGeometry/Transforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Library.h"

#include <glm/fwd.hpp>
#include <glm/mat4x4.hpp>

namespace CesiumGeometry {

Expand Down
Loading
Loading