Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Algorithms.hh
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  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 #ifndef GEO_ALGORITHMS_HH
54 #define GEO_ALGORITHMS_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include <cfloat>
60 #include <ACG/Math/VectorT.hh>
61 #include <vector>
62 #include <iostream>
63 
64 
65 namespace ACG {
66 namespace Geometry {
67 
68 
69 //== 3D STUFF =================================================================
70 
71 
72 
74 template<typename Scalar>
75 bool
77  const VectorT<Scalar,3>& _v1,
78  const VectorT<Scalar,3>& _v2,
79  const VectorT<Scalar,3>& _v3,
80  VectorT<Scalar,3>& _result );
81 
82 
84 template<typename Scalar>
85 Scalar
87  const VectorT<Scalar,3>& _v1,
88  const VectorT<Scalar,3>& _v2,
89  const VectorT<Scalar,3>& _v3 )
90 {
92  return circumCenter(_v0, _v1, _v2, _v3, cc) ? (cc-_v0).sqrnorm() : FLT_MAX;
93 }
94 
95 
97 template<typename Scalar>
98 Scalar
100  const VectorT<Scalar,3>& _v1,
101  const VectorT<Scalar,3>& _v2,
102  const VectorT<Scalar,3>& _v3 )
103 {
104  return sqrt(circumRadiusSquared(_v0, _v1, _v2, _v3));
105 }
106 
119 template<typename Scalar>
120 bool edgeConvexPolygonIntersection(std::vector<VectorT<Scalar,3> > _polygon_points,
121  VectorT<Scalar,3> _v0,
122  VectorT<Scalar,3> _v1,
123  VectorT<Scalar,3> &_result);
124 
125 
140 template<typename Scalar>
141 bool
143  const VectorT<Scalar,3>& _v1,
144  VectorT<Scalar,3>& _axis,
145  Scalar& _angle,
146  bool _degree = true);
147 
148 
157 template <typename Scalar>
159 perpendicular( const VectorT<Scalar,3>& _v );
160 
161 
178 template<typename Vec>
179 bool
180 triangleIntersection( const Vec& _o,
181  const Vec& _dir,
182  const Vec& _v0,
183  const Vec& _v1,
184  const Vec& _v2,
185  typename Vec::value_type& _t,
186  typename Vec::value_type& _u,
187  typename Vec::value_type& _v );
188 
189 
202 template<typename Vec>
203 bool
204 axisAlignedBBIntersection( const Vec& _o,
205  const Vec& _dir,
206  const Vec& _bbmin,
207  const Vec& _bbmax,
208  typename Vec::value_type& _t0,
209  typename Vec::value_type& _t1 );
210 
211 
212 //== 2D STUFF =================================================================
213 
215 template<typename Scalar>
216 Scalar
218  const VectorT<Scalar,2>& _v0,
219  const VectorT<Scalar,2>& _v1 )
220 {
221  VectorT<Scalar,2> d1(_p-_v0), d2(_v1-_v0);
222  return (d1[0]*d2[1]-d1[1]*d2[0]);
223 }
224 
225 
227 template<typename Scalar>
228 bool
230  const VectorT<Scalar,2>& _v1,
231  const VectorT<Scalar,2>& _v2 )
232 {
233  return ( pointLineOrientation(_v0, _v1, _v2) < Scalar(0.0) );
234 }
235 
236 
238 template<typename Scalar>
239 bool
241  const VectorT<Scalar,2>& _v1,
242  const VectorT<Scalar,2>& _v2 )
243 {
244  return ( pointLineOrientation(_v0, _v1, _v2) > Scalar(0.0) );
245 }
246 
247 
249 template<typename Scalar>
250 bool
252  const VectorT<Scalar,2>& _v1,
253  const VectorT<Scalar,2>& _v2,
254  const VectorT<Scalar,2>& _v3,
255  Scalar& _t1,
256  Scalar& _t2 );
257 
258 
259 //===========================================================================
262 //===========================================================================
263 
264 
273 template<class Vec>
274 typename Vec::value_type
275 distPointLineSquared( const Vec& _p,
276  const Vec& _v0,
277  const Vec& _v1,
278  Vec* _min_v=0);
279 
280 
281 
293 template<class Vec>
294 typename Vec::value_type
295 distPointLine( const Vec& _p,
296  const Vec& _v0,
297  const Vec& _v1,
298  Vec* _min_v=0 )
299 { return sqrt(distPointLineSquared(_p, _v0, _v1, _min_v)); }
300 
301 
303 template <class Vec>
304 typename Vec::value_type
305 distPointTriangleSquared( const Vec& _p,
306  const Vec& _v0,
307  const Vec& _v1,
308  const Vec& _v2,
309  Vec& _nearestPoint );
310 
322 template <class Vec>
323 typename Vec::value_type
324 distPointTriangleSquaredStable( const Vec& _p,
325  const Vec& _v0,
326  const Vec& _v1,
327  const Vec& _v2,
328  Vec& _nearestPoint );
329 
331 template <class Vec>
332 typename Vec::value_type
333 distPointTriangle( const Vec& _p,
334  const Vec& _v0,
335  const Vec& _v1,
336  const Vec& _v2,
337  Vec& _nearestPoint )
338 { return sqrt(distPointTriangleSquared(_p, _v0, _v1, _v2, _nearestPoint)); }
339 
350 template <class Vec>
351 typename Vec::value_type
352 distPointTriangleStable( const Vec& _p,
353  const Vec& _v0,
354  const Vec& _v1,
355  const Vec& _v2,
356  Vec& _nearestPoint )
357 { return sqrt(distPointTriangleSquaredStable(_p, _v0, _v1, _v2, _nearestPoint)); }
358 
367 template < typename VectorT , typename ValueT >
368 inline
369 ValueT
370 distPointPlane(const VectorT& _porigin,
371  const VectorT& _pnormal,
372  const VectorT& _point);
373 
374 
377 //===========================================================================
380 //===========================================================================
381 
383 template<typename Scalar>
384 Scalar
386  const VectorT<Scalar,3>& _v01,
387  const VectorT<Scalar,3>& _v10,
388  const VectorT<Scalar,3>& _v11,
389  VectorT<Scalar,3>* _min_v0=0,
390  VectorT<Scalar,3>* _min_v1=0,
391  bool _fastApprox=false );
392 
393 
395 template<typename Scalar>
396 Scalar
398  const VectorT<Scalar,3>& _v01,
399  const VectorT<Scalar,3>& _v10,
400  const VectorT<Scalar,3>& _v11,
401  VectorT<Scalar,3>* _min_v0=0,
402  VectorT<Scalar,3>* _min_v1=0 )
403 {
404  return sqrt(distLineLineSquared(_v00, _v01, _v10, _v11,
405  _min_v0, _min_v1));
406 }
407 
411 //===========================================================================
414 //===========================================================================
415 
416 
423 template < typename VectorT >
424 VectorT projectToEdge(const VectorT& _start ,
425  const VectorT& _end ,
426  const VectorT& _point );
427 
428 
435 template < typename VectorT >
436 inline
437 VectorT
438 projectToPlane(const VectorT& _porigin,
439  const VectorT& _pnormal,
440  const VectorT& _point);
441 
444 //===========================================================================
447 //===========================================================================
448 
453 template<typename Scalar>
455 bool
456 baryCoord( const VectorT<Scalar,2>& _p,
457  const VectorT<Scalar,2>& _u,
458  const VectorT<Scalar,2>& _v,
459  const VectorT<Scalar,2>& _w,
460  VectorT<Scalar,3>& _result );
461 
462 
464 template<typename Scalar>
465 bool
467  const VectorT<Scalar,2>& _u,
468  const VectorT<Scalar,2>& _v,
469  const VectorT<Scalar,2>& _w )
470 {
471  VectorT<Scalar,3> bary;
472  if (baryCoord(_p, _u, _v, _w, bary))
473  return ((bary[0]>0.0) && (bary[1]>0.0) && (bary[2]>0.0));
474  else {
475  std::cerr << "point in triangle error\n";
476  return false;
477  }
478 }
479 
480 template<typename Scalar>
481 bool
482 circumCenter( const VectorT<Scalar,2>& _v0,
483  const VectorT<Scalar,2>& _v1,
484  const VectorT<Scalar,2>& _v2,
485  VectorT<Scalar,2>& _result );
486 
489 //===========================================================================
492 //===========================================================================
493 
496 template<typename Scalar>
497 bool
498 baryCoord( const VectorT<Scalar,3>& _p,
499  const VectorT<Scalar,3>& _u,
500  const VectorT<Scalar,3>& _v,
501  const VectorT<Scalar,3>& _w,
502  VectorT<Scalar,3>& _result );
503 
504 
506 template<typename Scalar>
507 bool
508 minSphere(const VectorT<Scalar,3>& _v0,
509  const VectorT<Scalar,3>& _v1,
510  const VectorT<Scalar,3>& _v2,
511  VectorT<Scalar,3>& _center,
512  Scalar& _radius);
513 
514 
516 template<typename Scalar>
517 Scalar
519  const VectorT<Scalar,3>& _v1,
520  const VectorT<Scalar,3>& _v2 );
521 
522 
524 template<typename Scalar>
525 Scalar
527  const VectorT<Scalar,3>& _v1,
528  const VectorT<Scalar,3>& _v2 )
529 {
530  return sqrt(minRadiusSquared(_v0, _v1, _v2));
531 }
532 
533 
535 template<typename Scalar>
536 bool
537 circumCenter( const VectorT<Scalar,3>& _v0,
538  const VectorT<Scalar,3>& _v1,
539  const VectorT<Scalar,3>& _v2,
540  VectorT<Scalar,3>& _result );
541 
542 
544 template<typename Scalar>
545 Scalar
547  const VectorT<Scalar,3>& _v1,
548  const VectorT<Scalar,3>& _v2 );
549 
550 
552 template<typename Scalar>
553 Scalar
555  const VectorT<Scalar,3>& _v1,
556  const VectorT<Scalar,3>& _v2 )
557 {
558  return sqrt(circumRadiusSquared(_v0, _v1, _v2));
559 }
560 
568 template<class VectorT>
569 int isObtuse(const VectorT& _p0,
570  const VectorT& _p1,
571  const VectorT& _p2 );
572 
575 //===========================================================================
578 //===========================================================================
579 
580 
587 template <class Vec>
588 typename Vec::value_type
589 triangleAreaSquared( const Vec& _v0,
590  const Vec& _v1,
591  const Vec& _v2 );
592 
593 
600 template <class Vec>
601 typename Vec::value_type
602 triangleArea( const Vec& _v0,
603  const Vec& _v1,
604  const Vec& _v2 )
605 {
606  return sqrt(triangleAreaSquared(_v0,_v1,_v2));
607 }
608 
609 
616 template <typename Scalar, int N>
617 Scalar
618 aspectRatio( const VectorT<Scalar, N>& _v0,
619  const VectorT<Scalar, N>& _v1,
620  const VectorT<Scalar, N>& _v2 );
621 
628 template <typename Scalar, int N>
629 Scalar
630 roundness( const VectorT<Scalar, N>& _v0,
631  const VectorT<Scalar, N>& _v1,
632  const VectorT<Scalar, N>& _v2 );
633 
636 //=============================================================================
637 } // namespace Geometry
638 } // namespace ACG
639 //=============================================================================
640 #if defined(INCLUDE_TEMPLATES) && !defined(GEO_ALGORITHMS_C)
641 #define GEO_ALGORITHMS_TEMPLATES
642 #include "Algorithms.cc"
643 #endif
644 //=============================================================================
645 #endif // GEO_ALGORITHMS_HH defined
646 //=============================================================================
647 
VectorT< Scalar, 3 > perpendicular(const VectorT< Scalar, 3 > &v)
find a vector that's perpendicular to _v
Definition: Algorithms.cc:1162
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
bool isCCW(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2)
are 3 vertices in counterclockwise order? in 2D
Definition: Algorithms.hh:229
bool edgeConvexPolygonIntersection(std::vector< VectorT< Scalar, 3 > > _polygon_points, VectorT< Scalar, 3 > _v0, VectorT< Scalar, 3 > _v1, VectorT< Scalar, 3 > &_result)
Get intersection point of a ray and a convex polygon.
Definition: Algorithms.cc:182
VectorT projectToPlane(const VectorT &_porigin, const VectorT &_pnormal, const VectorT &_point)
projects a point to a plane
Definition: Algorithms.cc:904
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
bool minSphere(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, VectorT< Scalar, 3 > &_center, Scalar &_radius)
construct min. enclosing sphere
Definition: Algorithms.cc:1040
Scalar aspectRatio(const VectorT< Scalar, N > &_v0, const VectorT< Scalar, N > &_v1, const VectorT< Scalar, N > &_v2)
return aspect ratio (length/height) of triangle
Definition: Algorithms.cc:1266
Scalar circumRadiusSquared(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return squared radius of circumcircle of triangle (_v0,_v1,_v2)
Definition: Algorithms.cc:950
Scalar pointLineOrientation(const VectorT< Scalar, 2 > &_p, const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1)
orientation of point _p w.r.t. line through _v0,_v1 in 2D
Definition: Algorithms.hh:217
Scalar circumRadius(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, const VectorT< Scalar, 3 > &_v3)
return radius of circumcircle of tetrahedron (_v0,_v1,_v2,_v3)
Definition: Algorithms.hh:99
ValueT distPointPlane(const VectorT &_porigin, const VectorT &_pnormal, const VectorT &_point)
Checks the distance from a point to a plane.
Definition: Algorithms.cc:872
int isObtuse(const VectorT &_p0, const VectorT &_p1, const VectorT &_p2)
Definition: Algorithms.cc:1013
Scalar distLineLineSquared(const VectorT< Scalar, 3 > &_v00, const VectorT< Scalar, 3 > &_v01, const VectorT< Scalar, 3 > &_v10, const VectorT< Scalar, 3 > &_v11, VectorT< Scalar, 3 > *_min_v0, VectorT< Scalar, 3 > *_min_v1, bool _fastApprox)
squared distance of lines (_v00, _v01) and (_v10, _v11)
Definition: Algorithms.cc:496
bool isCW(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2)
are 3 vertices in clockwise order? in 2D
Definition: Algorithms.hh:240
bool baryCoord(const VectorT< Scalar, 3 > &_p, const VectorT< Scalar, 3 > &_u, const VectorT< Scalar, 3 > &_v, const VectorT< Scalar, 3 > &_w, VectorT< Scalar, 3 > &_result)
Definition: Algorithms.cc:93
Vec::value_type triangleArea(const Vec &_v0, const Vec &_v1, const Vec &_v2)
return area of triangle (_v0, _v1, _v2)
Definition: Algorithms.hh:602
Vec::value_type distPointTriangleStable(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
distance from point _p to triangle (_v0, _v1, _v2)
Definition: Algorithms.hh:352
Scalar minRadius(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return radius of min. enclosing circle of triangle (_v0,_v1,_v2)
Definition: Algorithms.hh:526
bool lineIntersection(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2, const VectorT< Scalar, 2 > &_v3, Scalar &_t1, Scalar &_t2)
intersect two line segments (_v0,_v1) and (_v2,_v3)
Definition: Algorithms.cc:1215
Vec::value_type distPointLine(const Vec &_p, const Vec &_v0, const Vec &_v1, Vec *_min_v=0)
Compute distance from point to line segment.
Definition: Algorithms.hh:295
Scalar roundness(const VectorT< Scalar, N > &_v0, const VectorT< Scalar, N > &_v1, const VectorT< Scalar, N > &_v2)
return roundness of triangle: 1=equilateral, 0=colinear
Definition: Algorithms.cc:1302
Vec::value_type distPointTriangleSquaredStable(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
squared distance from point _p to triangle (_v0, _v1, _v2)
Definition: Algorithms.cc:476
bool rotationOfTwoVectors(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, VectorT< Scalar, 3 > &_axis, Scalar &_angle, bool _degree)
Get rotation axis and signed angle of rotation between two vectors.
Definition: Algorithms.cc:973
Scalar minRadiusSquared(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return squared radius of min. enclosing circle of triangle (_v0,_v1,_v2)
Definition: Algorithms.cc:1100
bool isInTriangle(const VectorT< Scalar, 2 > &_p, const VectorT< Scalar, 2 > &_u, const VectorT< Scalar, 2 > &_v, const VectorT< Scalar, 2 > &_w)
is point _p in triangle (_v0,_v1,_v2) in 2D
Definition: Algorithms.hh:466
Vec::value_type distPointTriangle(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
distance from point _p to triangle (_v0, _v1, _v2)
Definition: Algorithms.hh:333
Vec::value_type triangleAreaSquared(const Vec &_v0, const Vec &_v1, const Vec &_v2)
return squared area of triangle (_v0, _v1, _v2)
Definition: Algorithms.cc:173
bool axisAlignedBBIntersection(const Vec &_o, const Vec &_dir, const Vec &_bbmin, const Vec &_bbmax, typename Vec::value_type &tmin, typename Vec::value_type &tmax)
Intersect a ray and an axis aligned bounding box.
Definition: Algorithms.cc:1367
Vec::value_type distPointLineSquared(const Vec &_p, const Vec &_v0, const Vec &_v1, Vec *_min_v)
squared distance from point _p to line segment (_v0,_v1)
Definition: Algorithms.cc:300
bool circumCenter(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, VectorT< Scalar, 3 > &_result)
return circumcenter of triangle (_v0,_v1,_v2)
Definition: Algorithms.cc:916
Scalar distLineLine(const VectorT< Scalar, 3 > &_v00, const VectorT< Scalar, 3 > &_v01, const VectorT< Scalar, 3 > &_v10, const VectorT< Scalar, 3 > &_v11, VectorT< Scalar, 3 > *_min_v0=0, VectorT< Scalar, 3 > *_min_v1=0)
distance of lines (_v00, _v01) and (_v10, _v11)
Definition: Algorithms.hh:397
VectorT projectToEdge(const VectorT &_start, const VectorT &_end, const VectorT &_point)
Definition: Algorithms.cc:884