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 #include <gtest/gtest.h>
43 
44 #include <ACG/Math/VectorT.hh>
45 #include <ACG/Geometry/Algorithms.hh>
46 
47 class ALGORITHM_TEST_BASE : public testing::Test {
48 
49  protected:
50 
51  // This function is called before each test is run
52  virtual void SetUp() {
53 
54  }
55 
56  // This function is called after all tests are through
57  virtual void TearDown() {
58 
59  // Do some final stuff with the member data here...
60  }
61 
62 };
63 
64 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection ) {
65 
66 
67  // ==============================================
68  // Triangle intersection algorithm
69  // ==============================================
70 
71  /* All in z = 0 plane :
72 
73  (0,1)
74  p2
75  | \
76  | \
77  | \
78  | \
79  | \
80  | \
81  p0 === p1
82  (0,0) (1,0)
83  */
84 
85  // Triangle points
86  ACG::Vec3f p0 ( 0.0,0.0,0.0);
87  ACG::Vec3f p1 ( 1.0,0.0,0.0);
88  ACG::Vec3f p2 ( 0.0,1.0,0.0);
89 
90  // Shooting ray origin and direction
91  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
92  ACG::Vec3f direction( 0.0f, 0.0f, 1.0f);
93 
94  float distance,u,v;
95  bool result = ACG::Geometry::triangleIntersection(origin, direction,
96  p0, p1, p2,
97  distance,u,v);
98 
99  EXPECT_TRUE( result ) << "Intersection failed!";
100 
101  EXPECT_EQ( 1.0f, distance ) << "Wrong distance!" << std::endl;
102  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
103  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
104 
105 }
106 
107 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_FlippedTriangleOrientation ) {
108 
109 
110  // ==============================================
111  // Triangle intersection algorithm
112  // ==============================================
113 
114  /* All in z = 0 plane :
115 
116  (0,1)
117  p2
118  | \
119  | \
120  | \
121  | \
122  | \
123  | \
124  p0 === p1
125  (0,0) (1,0)
126  */
127 
128  // Triangle points
129  ACG::Vec3f p0 ( 0.0,0.0,0.0);
130  ACG::Vec3f p1 ( 1.0,0.0,0.0);
131  ACG::Vec3f p2 ( 0.0,1.0,0.0);
132 
133  // Shooting ray origin and direction
134  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
135  ACG::Vec3f direction( 0.0, 0.0, 1.0);
136 
137  float distance,u,v;
138  bool result = ACG::Geometry::triangleIntersection(origin, direction,
139  p0, p2, p1,
140  distance,u,v);
141 
142  EXPECT_TRUE( result ) << "Intersection failed!";
143 
144  EXPECT_EQ( 1.0f, distance ) << "Wrong distance!" << std::endl;
145  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
146  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
147 
148 }
149 
150 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_NegativeShootingDirection ) {
151 
152 
153  // ==============================================
154  // Triangle intersection algorithm
155  // ==============================================
156 
157  /* All in z = 0 plane :
158 
159  (0,1)
160  p2
161  | \
162  | \
163  | \
164  | \
165  | \
166  | \
167  p0 === p1
168  (0,0) (1,0)
169  */
170 
171  // Triangle points
172  ACG::Vec3f p0 ( 0.0,0.0,0.0);
173  ACG::Vec3f p1 ( 1.0,0.0,0.0);
174  ACG::Vec3f p2 ( 0.0,1.0,0.0);
175 
176  // Shooting ray origin and direction
177  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
178  ACG::Vec3f direction( 0.0f, 0.0f, -1.0f);
179 
180  float distance,u,v;
181  bool result = ACG::Geometry::triangleIntersection(origin, direction,
182  p0, p1, p2,
183  distance,u,v);
184 
185  EXPECT_TRUE( result ) << "Intersection failed!";
186 
187  EXPECT_EQ( -1.0f, distance ) << "Wrong distance!" << std::endl;
188  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
189  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
190 
191 }
192 
193 TEST_F(ALGORITHM_TEST_BASE, triangleIntersection_NegativeShootingDirection_FlippedTriangleOrientation ) {
194 
195 
196  // ==============================================
197  // Triangle intersection algorithm
198  // ==============================================
199 
200  /* All in z = 0 plane :
201 
202  (0,1)
203  p2
204  | \
205  | \
206  | \
207  | \
208  | \
209  | \
210  p0 === p1
211  (0,0) (1,0)
212  */
213 
214  // Triangle points
215  ACG::Vec3f p0 ( 0.0,0.0,0.0);
216  ACG::Vec3f p1 ( 1.0,0.0,0.0);
217  ACG::Vec3f p2 ( 0.0,1.0,0.0);
218 
219  // Shooting ray origin and direction
220  ACG::Vec3f origin( 0.1f, 0.1f, -1.0f);
221  ACG::Vec3f direction( 0.0f, 0.0f, -1.0f);
222 
223  float distance,u,v;
224  bool result = ACG::Geometry::triangleIntersection(origin, direction,
225  p0, p2, p1,
226  distance,u,v);
227 
228  EXPECT_TRUE( result ) << "Intersection failed!";
229 
230  EXPECT_EQ( -1.0f, distance ) << "Wrong distance!" << std::endl;
231  EXPECT_EQ( 0.1f, u ) << "Wrong u!" << std::endl;
232  EXPECT_EQ( 0.1f, v ) << "Wrong v!" << std::endl;
233 
234 }
235 
236 
237 
238 
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:1307