History

remove PlaneShape dependency from GeometryUtils

also add unit tests for indRayRectangleIntersection()

Andrew Meadows committed 11 years ago

Commit ef5e7e91 parent c7f367b3 files · +222 −20Browse files

Showing the changes to tests/shared/src/GeometryUtilTests.cpp (1 of 3 files). Show all

Unified · Split

tests/shared/src/GeometryUtilTests.cpp+160−0
@@ -0,0 +1,160 @@
1//
2// GeometryUtilTests.cpp
3// tests/physics/src
4//
5// Created by Andrew Meadows on 2015.07.27
6// Copyright 2015 High Fidelity, Inc.
7//
8// Distributed under the Apache License, Version 2.0.
9// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10//
11
12#include <iostream>
13
14#include "GeometryUtilTests.h"
15
16#include <GeometryUtil.h>
17#include <NumericalConstants.h>
18#include <StreamUtils.h>
19
20#include <../QTestExtensions.h>
21
22
23QTEST_MAIN(GeometryUtilTests)
24
25float getErrorDifference(const float& a, const float& b) {
26 return fabsf(a - b);
27}
28
29float getErrorDifference(const glm::vec3& a, const glm::vec3& b) {
30 return glm::distance(a, b);
31}
32
33void GeometryUtilTests::testLocalRayRectangleIntersection() {
34 glm::vec3 xAxis(1.0f, 0.0f, 0.0f);
35 glm::vec3 yAxis(0.0f, 1.0f, 0.0f);
36 glm::vec3 zAxis(0.0f, 0.0f, 1.0f);
37
38 // create a rectangle in the local frame on the XY plane with normal along -zAxis
39 // (this is the assumed local orientation of the rectangle in findRayRectangleIntersection())
40 glm::vec2 rectDimensions(3.0f, 5.0f);
41 glm::vec3 rectCenter(0.0f, 0.0f, 0.0f);
42 glm::quat rectRotation = glm::quat(); // identity
43
44 // create points for generating rays that hit the plane and don't
45 glm::vec3 rayStart(1.0f, 2.0f, 3.0f);
46 float delta = 0.1f;
47
48 { // verify hit
49 glm::vec3 rayEnd = rectCenter + rectRotation * ((0.5f * rectDimensions.x - delta) * xAxis);
50 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
51 float expectedDistance = glm::length(rayEnd - rayStart);
52
53 float distance = FLT_MAX;
54 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
55 QCOMPARE(hit, true);
56 QCOMPARE_WITH_ABS_ERROR(distance, expectedDistance, EPSILON);
57 }
58
59 { // verify miss
60 glm::vec3 rayEnd = rectCenter + rectRotation * ((0.5f * rectDimensions.y + delta) * yAxis);
61 glm::vec3 rayMissDirection = glm::normalize(rayEnd - rayStart);
62 float distance = FLT_MAX;
63 bool hit = findRayRectangleIntersection(rayStart, rayMissDirection, rectRotation, rectCenter, rectDimensions, distance);
64 QCOMPARE(hit, false);
65 QCOMPARE(distance, FLT_MAX); // distance should be unchanged
66 }
67
68 { // hit with co-planer line
69 float yFraction = 0.25f;
70 rayStart = rectCenter + rectRotation * (rectDimensions.x * xAxis + yFraction * rectDimensions.y * yAxis);
71
72 glm::vec3 rayEnd = rectCenter - rectRotation * (rectDimensions.x * xAxis + yFraction * rectDimensions.y * yAxis);
73 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
74 float expectedDistance = rectDimensions.x;
75
76 float distance = FLT_MAX;
77 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
78 QCOMPARE(hit, true);
79 QCOMPARE_WITH_ABS_ERROR(distance, expectedDistance, EPSILON);
80 }
81
82 { // miss with co-planer line
83 float yFraction = 0.75f;
84 rayStart = rectCenter + rectRotation * (rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
85
86 glm::vec3 rayEnd = rectCenter + rectRotation * (- rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
87 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
88
89 float distance = FLT_MAX;
90 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
91 QCOMPARE(hit, false);
92 QCOMPARE(distance, FLT_MAX); // distance should be unchanged
93 }
94}
95
96void GeometryUtilTests::testWorldRayRectangleIntersection() {
97 glm::vec3 xAxis(1.0f, 0.0f, 0.0f);
98 glm::vec3 yAxis(0.0f, 1.0f, 0.0f);
99 glm::vec3 zAxis(0.0f, 0.0f, 1.0f);
100
101 // create a rectangle in the local frame on the XY plane with normal along -zAxis
102 // (this is the assumed local orientation of the rectangle in findRayRectangleIntersection())
103 // and then rotate and shift everything into the world frame
104 glm::vec2 rectDimensions(3.0f, 5.0f);
105 glm::vec3 rectCenter(0.7f, 0.5f, -0.3f);
106 glm::quat rectRotation = glm::quat(); // identity
107
108
109 // create points for generating rays that hit the plane and don't
110 glm::vec3 rayStart(1.0f, 2.0f, 3.0f);
111 float delta = 0.1f;
112
113 { // verify hit
114 glm::vec3 rayEnd = rectCenter + rectRotation * ((0.5f * rectDimensions.x - delta) * xAxis);
115 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
116 float expectedDistance = glm::length(rayEnd - rayStart);
117
118 float distance = FLT_MAX;
119 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
120 QCOMPARE(hit, true);
121 QCOMPARE_WITH_ABS_ERROR(distance, expectedDistance, EPSILON);
122 }
123
124 { // verify miss
125 glm::vec3 rayEnd = rectCenter + rectRotation * ((0.5f * rectDimensions.y + delta) * yAxis);
126 glm::vec3 rayMissDirection = glm::normalize(rayEnd - rayStart);
127 float distance = FLT_MAX;
128 bool hit = findRayRectangleIntersection(rayStart, rayMissDirection, rectRotation, rectCenter, rectDimensions, distance);
129 QCOMPARE(hit, false);
130 QCOMPARE(distance, FLT_MAX); // distance should be unchanged
131 }
132
133 { // hit with co-planer line
134 float yFraction = 0.25f;
135 rayStart = rectCenter + rectRotation * (rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
136
137 glm::vec3 rayEnd = rectCenter - rectRotation * (rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
138 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
139 float expectedDistance = rectDimensions.x;
140
141 float distance = FLT_MAX;
142 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
143 QCOMPARE(hit, true);
144 QCOMPARE_WITH_ABS_ERROR(distance, expectedDistance, EPSILON);
145 }
146
147 { // miss with co-planer line
148 float yFraction = 0.75f;
149 rayStart = rectCenter + rectRotation * (rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
150
151 glm::vec3 rayEnd = rectCenter + rectRotation * (-rectDimensions.x * xAxis + (yFraction * rectDimensions.y) * yAxis);
152 glm::vec3 rayHitDirection = glm::normalize(rayEnd - rayStart);
153
154 float distance = FLT_MAX;
155 bool hit = findRayRectangleIntersection(rayStart, rayHitDirection, rectRotation, rectCenter, rectDimensions, distance);
156 QCOMPARE(hit, false);
157 QCOMPARE(distance, FLT_MAX); // distance should be unchanged
158 }
159}
160

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t