Developer Documentation
Algorithms_test.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*
43  * BSP_test.cc
44  *
45  * Created on: 31.07.2012
46  * Author: moebius
47  */
48 
49 #include <gtest/gtest.h>
50 
51 #include <ACG/Math/VectorT.hh>
52 #include <ACG/Geometry/Algorithms.hh>
53 
54 class ALGORITHM_TEST_BASE : public testing::Test {
55 
56  protected:
57 
58  // This function is called before each test is run
59  virtual void SetUp() {
60 
61  }
62 
63  // This function is called after all tests are through
64  virtual void TearDown() {
65 
66  // Do some final stuff with the member data here...
67  }
68 
69 };
70 
71 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection ) {
72 
73 
74  // ==============================================
75  // Triangle intersection algorithm
76  // ==============================================
77 
78  /* All in z = 0 plane :
79 
80  (0,1)
81  p2
82  | \
83  | \
84  | \
85  | \
86  | \
87  | \
88  p0 === p1
89  (0,0) (1,0)
90  */
91 
92  // Triangle points
93  ACG::Vec3f p0 ( 0.0,0.0,0.0);
94  ACG::Vec3f p1 ( 1.0,0.0,0.0);
95  ACG::Vec3f p2 ( 0.0,1.0,0.0);
96 
97  // Shooting ray origin and direction
98  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
99  ACG::Vec3f direction( 0.0f, 0.0f, 1.0f);
100 
101  float distance,u,v;
102  bool result = ACG::Geometry::triangleIntersection(origin, direction,
103  p0, p1, p2,
104  distance,u,v);
105 
106  EXPECT_TRUE( result ) << "Intersection failed!";
107 
108  EXPECT_EQ( 1.0f, distance ) << "Wrong distance!" << std::endl;
109  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
110  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
111 
112 }
113 
114 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_FlippedTriangleOrientation ) {
115 
116 
117  // ==============================================
118  // Triangle intersection algorithm
119  // ==============================================
120 
121  /* All in z = 0 plane :
122 
123  (0,1)
124  p2
125  | \
126  | \
127  | \
128  | \
129  | \
130  | \
131  p0 === p1
132  (0,0) (1,0)
133  */
134 
135  // Triangle points
136  ACG::Vec3f p0 ( 0.0,0.0,0.0);
137  ACG::Vec3f p1 ( 1.0,0.0,0.0);
138  ACG::Vec3f p2 ( 0.0,1.0,0.0);
139 
140  // Shooting ray origin and direction
141  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
142  ACG::Vec3f direction( 0.0, 0.0, 1.0);
143 
144  float distance,u,v;
145  bool result = ACG::Geometry::triangleIntersection(origin, direction,
146  p0, p2, p1,
147  distance,u,v);
148 
149  EXPECT_TRUE( result ) << "Intersection failed!";
150 
151  EXPECT_EQ( 1.0f, distance ) << "Wrong distance!" << std::endl;
152  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
153  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
154 
155 }
156 
157 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_NegativeShootingDirection ) {
158 
159 
160  // ==============================================
161  // Triangle intersection algorithm
162  // ==============================================
163 
164  /* All in z = 0 plane :
165 
166  (0,1)
167  p2
168  | \
169  | \
170  | \
171  | \
172  | \
173  | \
174  p0 === p1
175  (0,0) (1,0)
176  */
177 
178  // Triangle points
179  ACG::Vec3f p0 ( 0.0,0.0,0.0);
180  ACG::Vec3f p1 ( 1.0,0.0,0.0);
181  ACG::Vec3f p2 ( 0.0,1.0,0.0);
182 
183  // Shooting ray origin and direction
184  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
185  ACG::Vec3f direction( 0.0f, 0.0f, -1.0f);
186 
187  float distance,u,v;
188  bool result = ACG::Geometry::triangleIntersection(origin, direction,
189  p0, p1, p2,
190  distance,u,v);
191 
192  EXPECT_TRUE( result ) << "Intersection failed!";
193 
194  EXPECT_EQ( -1.0f, distance ) << "Wrong distance!" << std::endl;
195  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
196  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
197 
198 }
199 
200 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_NegativeShootingDirection_FlippedTriangleOrientation ) {
201 
202 
203  // ==============================================
204  // Triangle intersection algorithm
205  // ==============================================
206 
207  /* All in z = 0 plane :
208 
209  (0,1)
210  p2
211  | \
212  | \
213  | \
214  | \
215  | \
216  | \
217  p0 === p1
218  (0,0) (1,0)
219  */
220 
221  // Triangle points
222  ACG::Vec3f p0 ( 0.0,0.0,0.0);
223  ACG::Vec3f p1 ( 1.0,0.0,0.0);
224  ACG::Vec3f p2 ( 0.0,1.0,0.0);
225 
226  // Shooting ray origin and direction
227  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
228  ACG::Vec3f direction( 0.0f, 0.0f, -1.0f);
229 
230  float distance,u,v;
231  bool result = ACG::Geometry::triangleIntersection(origin, direction,
232  p0, p2, p1,
233  distance,u,v);
234 
235  EXPECT_TRUE( result ) << "Intersection failed!";
236 
237  EXPECT_EQ( -1.0f, distance ) << "Wrong distance!" << std::endl;
238  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
239  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
240 
241 }
242 
243 
244 
245 
bool triangleIntersection(const Vec &_o, const Vec &_dir, const Vec &_v0, const Vec &_v1, const Vec &_v2, typename Vec::value_type &_t, typename Vec::value_type &_u, typename Vec::value_type &_v)
Intersect a ray and a triangle.
Definition: Algorithms.cc:1317