Algorithms.cc 28.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38
39
40
41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
   
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699




//=============================================================================
//
//  CLASS Geometry - IMPLEMENTATION
//
//=============================================================================

#define GEO_ALGORITHMS_C

//== INCLUDES =================================================================

#include "Algorithms.hh"
#include "../Utils/NumLimitsT.hh"
#include <math.h>

#ifdef max
#  undef max
#endif

#ifdef min
#  undef min
#endif


//----------------------------------------------------------------------------


namespace ACG {
namespace Geometry {


//== IMPLEMENTATION ========================================================== 


//== 3D STUFF ================================================================ 

  
template<typename Scalar>
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 )
{
  VectorT<Scalar,3>  vu = _v - _u,
                          wu = _w - _u,
                          pu = _p - _u;

  
  // find largest absolute coodinate of normal
  Scalar nx = vu[1]*wu[2] - vu[2]*wu[1],
         ny = vu[2]*wu[0] - vu[0]*wu[2],
         nz = vu[0]*wu[1] - vu[1]*wu[0],
         ax = fabs(nx),
         ay = fabs(ny),
         az = fabs(nz);


  unsigned char max_coord;

  if ( ax > ay ) {
    if ( ax > az ) {
      max_coord = 0;
    }
    else {
      max_coord = 2;
    }
  }
  else {
    if ( ay > az ) {
      max_coord = 1;
    }
    else {
      max_coord = 2;
    }
  }


  // solve 2D problem
  switch (max_coord) {

    case 0:
    {      
      if (1.0+ax == 1.0) return false;
      _result[1] = 1.0 + (pu[1]*wu[2]-pu[2]*wu[1])/nx - 1.0;
      _result[2] = 1.0 + (vu[1]*pu[2]-vu[2]*pu[1])/nx - 1.0;
      _result[0] = 1.0 - _result[1] - _result[2];
    }
    break;
      
    case 1:
    {
      if (1.0+ay == 1.0) return false;
      _result[1] = 1.0 + (pu[2]*wu[0]-pu[0]*wu[2])/ny - 1.0;
      _result[2] = 1.0 + (vu[2]*pu[0]-vu[0]*pu[2])/ny - 1.0;
      _result[0] = 1.0 - _result[1] - _result[2];
    }
    break;
      
    case 2:
    {
      if (1.0+az == 1.0) return false;
      _result[1] = 1.0 + (pu[0]*wu[1]-pu[1]*wu[0])/nz - 1.0;
      _result[2] = 1.0 + (vu[0]*pu[1]-vu[1]*pu[0])/nz - 1.0;
      _result[0] = 1.0 - _result[1] - _result[2];
    }
    break;    
  }

  return true;
}


//-----------------------------------------------------------------------------


template <class Vec>
typename Vec::value_type
triangleAreaSquared( const Vec& _v0,
		     const Vec& _v1,
		     const Vec& _v2 )
{
  return 0.25 * ((_v1-_v0)%(_v2-_v0)).sqrnorm();
}


//-----------------------------------------------------------------------------


template<class Vec>
typename Vec::value_type
distPointLineSquared( const Vec& _p,
		      const Vec& _v0,
		      const Vec& _v1,
		      Vec*       _min_v )
{
  Vec d1(_p-_v0), d2(_v1-_v0), min_v(_v0);
  typename Vec::value_type t = (d1|d2)/(d2|d2);

  if (t >  1.0)       d1 = _p - (min_v = _v1);
  else if (t > 0.0)   d1 = _p - (min_v = _v0 + d2*t);

  if (_min_v) *_min_v = min_v;
  return d1.sqrnorm();
}    


//-----------------------------------------------------------------------------


template <class Vec>
typename Vec::value_type
distPointTriangleSquared( const Vec& _p,
			  const Vec& _v0,
			  const Vec& _v1,
			  const Vec& _v2,
			  Vec& _nearestPoint )
{
  Vec v0v1 = _v1 - _v0;
  Vec v0v2 = _v2 - _v0;
  Vec n = v0v1 % v0v2; // not normalized !
  double d = n.sqrnorm();

  
  // Check if the triangle is degenerated
  if (d < FLT_MIN && d > -FLT_MIN) {
    std::cerr << "distPointTriangleSquared: Degenerated triangle !\n";
    return -1.0;
  }
  double invD = 1.0 / d;

  
  // these are not needed for every point, should still perform
  // better with many points against one triangle
  Vec v1v2 = _v2 - _v1;
  double inv_v0v2_2 = 1.0 / v0v2.sqrnorm();
  double inv_v0v1_2 = 1.0 / v0v1.sqrnorm();
  double inv_v1v2_2 = 1.0 / v1v2.sqrnorm();

  
  Vec v0p = _p - _v0;
  Vec t = v0p % n;
  double  s01, s02, s12;
  double a = (t | v0v2) * -invD;
  double b = (t | v0v1) * invD;

  
  if (a < 0)
  {
    // Calculate the distance to an edge or a corner vertex
    s02 = ( v0v2 | v0p ) * inv_v0v2_2;
    if (s02 < 0.0)
    {
      s01 = ( v0v1 | v0p ) * inv_v0v1_2;
      if (s01 <= 0.0) {
	v0p = _v0;
      } else if (s01 >= 1.0) {
	v0p = _v1;
      } else {
	v0p = _v0 + v0v1 * s01;
      }
    } else if (s02 > 1.0) {
      s12 = ( v1v2 | ( _p - _v1 )) * inv_v1v2_2;
      if (s12 >= 1.0) {
	v0p = _v2;
      } else if (s12 <= 0.0) {
	v0p = _v1;
      } else {
	v0p = _v1 + v1v2 * s12;
      }
    } else {
      v0p = _v0 + v0v2 * s02;
    }
  } else if (b < 0.0) {
    // Calculate the distance to an edge or a corner vertex
    s01 = ( v0v1 | v0p ) * inv_v0v1_2;
    if (s01 < 0.0)
    {
      s02 = ( v0v2 |  v0p ) * inv_v0v2_2;
      if (s02 <= 0.0) {
	v0p = _v0;
      } else if (s02 >= 1.0) {
	v0p = _v2;
      } else {
	v0p = _v0 + v0v2 * s02;
      }
    } else if (s01 > 1.0) {
      s12 = ( v1v2 | ( _p - _v1 )) * inv_v1v2_2;
      if (s12 >= 1.0) {
	v0p = _v2;
      } else if (s12 <= 0.0) {
	v0p = _v1;
      } else {
	v0p = _v1 + v1v2 * s12;
      }
    } else {
      v0p = _v0 + v0v1 * s01;
    }
  } else if (a+b > 1.0) {
    // Calculate the distance to an edge or a corner vertex
    s12 = ( v1v2 | ( _p - _v1 )) * inv_v1v2_2;
    if (s12 >= 1.0) {
      s02 = ( v0v2 | v0p ) * inv_v0v2_2;
      if (s02 <= 0.0) {
	v0p = _v0;
      } else if (s02 >= 1.0) {
	v0p = _v2;
      } else {
	v0p = _v0 + v0v2*s02;
      }
    } else if (s12 <= 0.0) {
      s01 = ( v0v1 |  v0p ) * inv_v0v1_2;
      if (s01 <= 0.0) {
	v0p = _v0;
      } else if (s01 >= 1.0) {
	v0p = _v1;
      } else {
	v0p = _v0 + v0v1 * s01;
      }
    } else {
      v0p = _v1 + v1v2 * s12;
    }
  } else {
    // Calculate the distance to an interior point of the triangle
    _nearestPoint = _p - n*((n|v0p) * invD);
    return (_nearestPoint - _p).sqrnorm();
  }
  
  _nearestPoint = v0p;

  return (_nearestPoint - _p).sqrnorm();
}


//-----------------------------------------------------------------------------


//
// Modified code of Dave Eberly (www.magic-software.com)
//

template<typename Scalar>
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 )
{
  VectorT<Scalar,3> kDiff = _v00 - _v10;
  VectorT<Scalar,3> d0 = _v01-_v00;
  VectorT<Scalar,3> d1 = _v11-_v10;

  Scalar fA00 = d0.sqrnorm();
  Scalar fA01 = -(d0|d1);
  Scalar fA11 = d1.sqrnorm();
  Scalar fB0  = (kDiff|d0);
  Scalar fC   = kDiff.sqrnorm();
  Scalar fDet = fabs(fA00*fA11-fA01*fA01);
  Scalar fB1, fS, fT, fSqrDist, fTmp;

  
  if ( fDet >= FLT_MIN )
  {
    // line segments are not parallel
    fB1 = -(kDiff|d1);
    fS = fA01*fB1-fA11*fB0;
    fT = fA01*fB0-fA00*fB1;

    
    // conservative approximation only?
    if (_fastApprox)
    {
      if      ( fS < 0.0 )  fS = 0.0;
      else if ( fS > fDet ) fS = fDet;
      if      ( fT < 0.0 )  fT = 0.0;
      else if ( fT > fDet ) fT = fDet;
    }

    
    if ( fS >= 0.0 )
    {
      if ( fS <= fDet )
      {
	if ( fT >= 0.0 )
	{
	  if ( fT <= fDet )  // region 0 (interior)
	  {
	    // minimum at two interior points of 3D lines
	    Scalar fInvDet = 1.0/fDet;
	    fS *= fInvDet;
	    fT *= fInvDet;
	    fSqrDist = fS*(fA00*fS+fA01*fT+2.0*fB0) +
	               fT*(fA01*fS+fA11*fT+2.0*fB1)+fC;
	  }
	  else  // region 3 (side)
	  {
	    fT = 1.0;
	    fTmp = fA01+fB0;
	    if ( fTmp >= 0.0 )
	    {
	      fS = 0.0;
	      fSqrDist = fA11+2.0*fB1+fC;
	    }
	    else if ( -fTmp >= fA00 )
	    {
	      fS = 1.0;
	      fSqrDist = fA00+fA11+fC+2.0*(fB1+fTmp);
	    }
	    else
	    {
	      fS = -fTmp/fA00;
	      fSqrDist = fTmp*fS+fA11+2.0*fB1+fC;
	    }
	  }
	}
	else  // region 7 (side)
	{
	  fT = 0.0;
	  if ( fB0 >= 0.0 )
	  {
	    fS = 0.0;
	    fSqrDist = fC;
	  }
	  else if ( -fB0 >= fA00 )
	  {
	    fS = 1.0;
	    fSqrDist = fA00+2.0*fB0+fC;
	  }
	  else
	  {
	    fS = -fB0/fA00;
	    fSqrDist = fB0*fS+fC;
	  }
	}
      }
      else
      {
	if ( fT >= 0.0 )
	{
	  if ( fT <= fDet )  // region 1 (side)
	  {
	    fS = 1.0;
	    fTmp = fA01+fB1;
	    if ( fTmp >= 0.0 )
	    {
	      fT = 0.0;
	      fSqrDist = fA00+2.0*fB0+fC;
	    }
	    else if ( -fTmp >= fA11 )
	    {
	      fT = 1.0;
	      fSqrDist = fA00+fA11+fC+2.0*(fB0+fTmp);
	    }
	    else
	    {
	      fT = -fTmp/fA11;
	      fSqrDist = fTmp*fT+fA00+2.0*fB0+fC;
	    }
	  }
	  else  // region 2 (corner)
	  {
	    fTmp = fA01+fB0;
	    if ( -fTmp <= fA00 )
	    {
	      fT = 1.0;
	      if ( fTmp >= 0.0 )
	      {
		fS = 0.0;
		fSqrDist = fA11+2.0*fB1+fC;
	      }
	      else
	      {
		fS = -fTmp/fA00;
		fSqrDist = fTmp*fS+fA11+2.0*fB1+fC;
	      }
	    }
	    else
	    {
	      fS = 1.0;
	      fTmp = fA01+fB1;
	      if ( fTmp >= 0.0 )
	      {
		fT = 0.0;
		fSqrDist = fA00+2.0*fB0+fC;
	      }
	      else if ( -fTmp >= fA11 )
	      {
		fT = 1.0;
		fSqrDist = fA00+fA11+fC+2.0*(fB0+fTmp);
	      }
	      else
	      {
		fT = -fTmp/fA11;
		fSqrDist = fTmp*fT+fA00+2.0*fB0+fC;
	      }
	    }
	  }
	}
	else  // region 8 (corner)
	{
	  if ( -fB0 < fA00 )
	  {
	    fT = 0.0;
	    if ( fB0 >= 0.0 )
	    {
	      fS = 0.0;
	      fSqrDist = fC;
	    }
	    else
	    {
	      fS = -fB0/fA00;
	      fSqrDist = fB0*fS+fC;
	    }
	  }
	  else
	  {
	    fS = 1.0;
	    fTmp = fA01+fB1;
	    if ( fTmp >= 0.0 )
	    {
	      fT = 0.0;
	      fSqrDist = fA00+2.0*fB0+fC;
	    }
	    else if ( -fTmp >= fA11 )
	    {
	      fT = 1.0;
	      fSqrDist = fA00+fA11+fC+2.0*(fB0+fTmp);
	    }
	    else
	    {
	      fT = -fTmp/fA11;
	      fSqrDist = fTmp*fT+fA00+2.0*fB0+fC;
	    }
	  }
	}
      }
    }
    else 
    {
      if ( fT >= 0.0 )
      {
	if ( fT <= fDet )  // region 5 (side)
	{
	  fS = 0.0;
	  if ( fB1 >= 0.0 )
	  {
	    fT = 0.0;
	    fSqrDist = fC;
	  }
	  else if ( -fB1 >= fA11 )
	  {
	    fT = 1.0;
	    fSqrDist = fA11+2.0*fB1+fC;
	  }
	  else
	  {
	    fT = -fB1/fA11;
	    fSqrDist = fB1*fT+fC;
	  }
	}
	else  // region 4 (corner)
	{
	  fTmp = fA01+fB0;
	  if ( fTmp < 0.0 )
	  {
	    fT = 1.0;
	    if ( -fTmp >= fA00 )
	    {
	      fS = 1.0;
	      fSqrDist = fA00+fA11+fC+2.0*(fB1+fTmp);
	    }
	    else
	    {
	      fS = -fTmp/fA00;
	      fSqrDist = fTmp*fS+fA11+2.0*fB1+fC;
	    }
	  }
	  else
	  {
	    fS = 0.0;
	    if ( fB1 >= 0.0 )
	    {
	      fT = 0.0;
	      fSqrDist = fC;
	    }
	    else if ( -fB1 >= fA11 )
	    {
	      fT = 1.0;
	      fSqrDist = fA11+2.0*fB1+fC;
	    }
	    else
	    {
	      fT = -fB1/fA11;
	      fSqrDist = fB1*fT+fC;
	    }
	  }
	}
      }
      else   // region 6 (corner)
      {
	if ( fB0 < 0.0 )
	{
	  fT = 0.0;
	  if ( -fB0 >= fA00 )
	  {
	    fS = 1.0;
	    fSqrDist = fA00+2.0*fB0+fC;
	  }
	  else
	  {
	    fS = -fB0/fA00;
	    fSqrDist = fB0*fS+fC;
	  }
	}
	else
	{
	  fS = 0.0;
	  if ( fB1 >= 0.0 )
	  {
	    fT = 0.0;
	    fSqrDist = fC;
	  }
	  else if ( -fB1 >= fA11 )
	  {
	    fT = 1.0;
	    fSqrDist = fA11+2.0*fB1+fC;
	  }
	  else
	  {
	    fT = -fB1/fA11;
	    fSqrDist = fB1*fT+fC;
	  }
	}
      }
    }
  }
  else
  {
    // line segments are parallel
    if ( fA01 > 0.0 )
    {
      // direction vectors form an obtuse angle
      if ( fB0 >= 0.0 )
      {
	fS = 0.0;
	fT = 0.0;
	fSqrDist = fC;
      }
      else if ( -fB0 <= fA00 )
      {
	fS = -fB0/fA00;
	fT = 0.0;
	fSqrDist = fB0*fS+fC;
      }
      else
      {
	fB1 = -(kDiff|d1);
	fS = 1.0;
	fTmp = fA00+fB0;
	if ( -fTmp >= fA01 )
	{
	  fT = 1.0;
	  fSqrDist = fA00+fA11+fC+2.0*(fA01+fB0+fB1);
	}
	else
	{
	  fT = -fTmp/fA01;
	  fSqrDist = fA00+2.0*fB0+fC+fT*(fA11*fT+2.0*(fA01+fB1));
	}
      }
    }
    else
    {
      // direction vectors form an acute angle
      if ( -fB0 >= fA00 )
      {
	fS = 1.0;
	fT = 0.0;
	fSqrDist = fA00+2.0*fB0+fC;
      }
      else if ( fB0 <= 0.0 )
      {
	fS = -fB0/fA00;
	fT = 0.0;
	fSqrDist = fB0*fS+fC;
      }
      else
      {
	fB1 = -(kDiff|d1);
	fS = 0.0;
	if ( fB0 >= -fA01 )
	{
	  fT = 1.0;
	  fSqrDist = fA11+2.0*fB1+fC;
	}
	else
	{
	  fT = -fB0/fA01;
	  fSqrDist = fC+fT*(2.0*fB1+fA11*fT);
	}
      }
    }
  }


  if (_min_v0)  *_min_v0 = _v00 + fS*d0;
  if (_min_v1)  *_min_v1 = _v10 + fT*d1;

  return fabs(fSqrDist);
}

700
701
702
703
704
//-----------------------------------------------------------------------------

template < typename VectorT , typename ValueT >
inline
ValueT
Jan Möbius's avatar
Jan Möbius committed
705
706
707
distPointPlane(const VectorT& _porigin,
               const VectorT& _pnormal,
               const VectorT& _point)
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
{
  assert( fabs(_pnormal.norm()) > 0.0000000001) ;
  return( ( (_point - _porigin) | _pnormal ) );
}


//-----------------------------------------------------------------------------

template < typename VectorT >
VectorT projectToEdge(const VectorT& _start , const VectorT& _end , const VectorT& _point )
{
  VectorT direction = ( _end - _start ).normalize();
  assert( fabs(direction.norm()) > 0.0000000001) ;
  const VectorT projected_point = ( ( _point - _start ) | direction ) * direction + _start;
  
  if ( ( ( projected_point - _start ) | direction ) > ( ( _end - _start ) | direction ) )
    return _end;
  
  if ( ( ( projected_point - _start ) | direction ) < 0 )
    return _start;
  
  return projected_point;
}

//-----------------------------------------------------------------------------

template < typename VectorT >
inline
VectorT
projectToPlane(const VectorT& _porigin,
               const VectorT& _pnormal,
               const VectorT& _point)
{
Jan Möbius's avatar
Jan Möbius committed
741
  return (_point - _pnormal * distPointPlane< VectorT , double >( _porigin , _pnormal , _point ) );
742
}
Jan Möbius's avatar
   
Jan Möbius committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

//-----------------------------------------------------------------------------


template<typename Scalar>
bool
circumCenter( const VectorT<Scalar,3>&  _v0,
	      const VectorT<Scalar,3>&  _v1,
	      const VectorT<Scalar,3>&  _v2,
	      VectorT<Scalar,3>&        _result )
{
  VectorT<Scalar,3>   a(_v1-_v2),
                           b(_v2-_v0),
                           c(_v0-_v1),
                           G((_v0+_v1+_v2)/3.0);
  
  Scalar d0 = -(b|c),
         d1 = -(c|a),
         d2 = -(a|b),
         e0 = d1*d2,
         e1 = d2*d0,
         e2 = d0*d1,
         e  = e0+e1+e2;

  if (e <= NumLimitsT<Scalar>::min())  return false;

  VectorT<Scalar,3>   H((e0*_v0 + e1*_v1 + e2*_v2)/e);

  _result = (Scalar)0.5 * ((Scalar)3.0*G - H);

  return true;
}



//-----------------------------------------------------------------------------


template<typename Scalar>
Scalar
circumRadiusSquared( const VectorT<Scalar,3>&  _v0,
		     const VectorT<Scalar,3>&  _v1,
		     const VectorT<Scalar,3>&  _v2 )
{
  VectorT<Scalar,3>  v0v1(_v1-_v0),
                          v0v2(_v2-_v0),
                          v1v2(_v2-_v1);

  Scalar denom = 4.0*((v0v1%v0v2).sqrnorm());
  if (denom < NumLimitsT<Scalar>::min()  && 
      denom > -NumLimitsT<Scalar>::min())  
    return NumLimitsT<Scalar>::max();

  return ( v0v1.sqrnorm() *
	   v0v2.sqrnorm() *
	   v1v2.sqrnorm() /
	   denom );
}

802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//-----------------------------------------------------------------------------

template<class VectorT>
int isObtuse(const VectorT& _p0,
             const VectorT& _p1,
             const VectorT& _p2 )
{
  const double a0 = ((_p1-_p0)|(_p2-_p0));

  if ( a0<0.0) return 1;
  else
  {
    const double a1 = ((_p0-_p1)|(_p2-_p1));

    if (a1 < 0.0 ) return 2;
    else
    {
      const double a2 = ((_p0-_p2)|(_p1-_p2));

      if (a2 < 0.0 ) return 3;
      else return 0;
    }
  }
}
Jan Möbius's avatar
   
Jan Möbius committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958

//-----------------------------------------------------------------------------


template<typename Scalar>
bool
minSphere(const VectorT<Scalar,3>&  _v0,
	  const VectorT<Scalar,3>&  _v1,
	  const VectorT<Scalar,3>&  _v2,
	  VectorT<Scalar,3>&        _center,
	  Scalar&                        _radius)
{
  VectorT<Scalar,3>   a(_v1-_v2),
                           b(_v2-_v0),
                           c(_v0-_v1);

  Scalar d0 = -(b|c),
         d1 = -(c|a),
         d2 = -(a|b);

  
  // obtuse angle ?
  if (d2 < NumLimitsT<Scalar>::min())
  {
    _center = (_v0+_v1)*0.5;
    _radius = 0.5 * c.norm();
    return true;
  }
  if (d0 < NumLimitsT<Scalar>::min())
  {
    _center = (_v1+_v2)*0.5;
    _radius = 0.5 * a.norm();
    return true;
  }
  if (d1 < NumLimitsT<Scalar>::min())
  {
    _center = (_v2+_v0)*0.5;
    _radius = 0.5 * b.norm();
    return true;
  }
  

  // acute angle
  VectorT<Scalar,3>   G((_v0+_v1+_v2)/3.0);
  
  Scalar e0 = d1*d2,
         e1 = d2*d0,
         e2 = d0*d1,
         e  = e0+e1+e2;

  if ( e <= NumLimitsT<Scalar>::min() )  return false;

  VectorT<Scalar,3>   H((e0*_v0 + e1*_v1 + e2*_v2)/e);

  _center = (Scalar)0.5 * ((Scalar)3.0*G - H);
  _radius = (_center-_v0).norm();
  
  return true;
}


//-----------------------------------------------------------------------------


template<typename Scalar>
Scalar
minRadiusSquared( const VectorT<Scalar,3>&  _v0,
		  const VectorT<Scalar,3>&  _v1,
		  const VectorT<Scalar,3>&  _v2 )
{
  VectorT<Scalar,3>  v0v1(_v1-_v0),
                          v0v2(_v2-_v0),
                          v1v2(_v2-_v1);

  Scalar denom = 4.0*((v0v1%v0v2).sqrnorm());
  if (denom < NumLimitsT<Scalar>::min() && 
      denom > -NumLimitsT<Scalar>::min())  
    return NumLimitsT<Scalar>::max();

  Scalar  l0 = v0v1.sqrnorm(),
          l1 = v0v2.sqrnorm(),
          l2 = v1v2.sqrnorm(),
          l3 = l0*l1*l2/denom;
  
  return  std::max(std::max(l0, l1), std::max(l2, l3));
}


//-----------------------------------------------------------------------------


template<typename Scalar>
bool
circumCenter( const VectorT<Scalar,3>&  _v0,
	      const VectorT<Scalar,3>&  _v1,
	      const VectorT<Scalar,3>&  _v2,
	      const VectorT<Scalar,3>&  _v3,
	      VectorT<Scalar,3>&        _result )
{
  VectorT<Scalar,3>
    v0v1(_v1-_v0),
    v0v2(_v2-_v0),
    v0v3(_v3-_v0);

  Scalar  denom = ((v0v1[0]*v0v2[1]*v0v3[2] +
		    v0v1[1]*v0v2[2]*v0v3[0] +
		    v0v1[2]*v0v2[0]*v0v3[1]) -
		   (v0v1[0]*v0v2[2]*v0v3[1] +
		    v0v1[1]*v0v2[0]*v0v3[2] +
		    v0v1[2]*v0v2[1]*v0v3[0])) * 2.0;

  if (denom < NumLimitsT<Scalar>::min() && 
      denom > -NumLimitsT<Scalar>::min())  return false;

  
  _result = _v0 + (( v0v3.sqrnorm()*(v0v1%v0v2) +
		     v0v2.sqrnorm()*(v0v3%v0v1) +
		     v0v1.sqrnorm()*(v0v2%v0v3) ) / denom);
  
  return true;
}


//-----------------------------------------------------------------------------


template <typename Scalar>
VectorT<Scalar,3>
perpendicular( const VectorT<Scalar,3>&  v )
{
  if (fabs(v[0]) < fabs(v[1]))
  {
    if (fabs(v[0]) < fabs(v[2]))
David Bommes's avatar
David Bommes committed
959
      return VectorT<Scalar,3>( Scalar(1.0) - v[0]*v[0], -v[0]*v[1], -v[0]*v[2]).normalize();
Jan Möbius's avatar
   
Jan Möbius committed
960
961
962
963
  }
  else
    {
      if (fabs(v[1]) < fabs(v[2]))
David Bommes's avatar
David Bommes committed
964
	return VectorT<Scalar,3>(-v[1]*v[0], Scalar(1.0) - v[1]*v[1], -v[1]*v[2]).normalize();
Jan Möbius's avatar
   
Jan Möbius committed
965
966
    }
  
David Bommes's avatar
David Bommes committed
967
  return VectorT<Scalar,3>(-v[2]*v[0], -v[2]*v[1], Scalar(1.0) - v[2]*v[2]).normalize();
Jan Möbius's avatar
   
Jan Möbius committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
}



//== 2D STUFF ================================================================ 



template<typename Scalar>
bool
baryCoord( const VectorT<Scalar,2> & _p,
	   const VectorT<Scalar,2> & _u,
	   const VectorT<Scalar,2> & _v,
	   const VectorT<Scalar,2> & _w,
	   VectorT<Scalar,3> & _result )
{
  Scalar  v0(_v[0]-_u[0]), v1(_v[1]-_u[1]),
          w0(_w[0]-_u[0]), w1(_w[1]-_u[1]),
          p0(_p[0]-_u[0]), p1(_p[1]-_u[1]),
          denom = v0*w1-v1*w0;

  if (1.0+fabs(denom) == 1.0) {
    std::cerr << "degen tri: ("
	      << _u << "), ("
	      << _v << "), ("
	      << _w << ")\n";
    return false;
  }
  
  _result[1] = 1.0 + ((p0*w1-p1*w0)/denom) - 1.0;
  _result[2] = 1.0 + ((v0*p1-v1*p0)/denom) - 1.0;
  _result[0] = 1.0 - _result[1] - _result[2];

  return true;
}


//-----------------------------------------------------------------------------


template<typename Scalar>
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 )
{      
  _t1 = ((_v0[1]-_v2[1])*(_v3[0]-_v2[0])-(_v0[0]-_v2[0])*(_v3[1]-_v2[1])) /
        ((_v1[0]-_v0[0])*(_v3[1]-_v2[1])-(_v1[1]-_v0[1])*(_v3[0]-_v2[0]));
      
  _t2 = ((_v0[1]-_v2[1])*(_v1[0]-_v0[0])-(_v0[0]-_v2[0])*(_v1[1]-_v0[1])) /
    ((_v1[0]-_v0[0])*(_v3[1]-_v2[1])-(_v1[1]-_v0[1])*(_v3[0]-_v2[0]));
      
  return ((_t1>0.0) && (_t1<1.0) && (_t2>0.0) && (_t2<1.0));
}


//-----------------------------------------------------------------------------

template<typename Scalar>
bool
circumCenter( const VectorT<Scalar,2>&  _v0,
	      const VectorT<Scalar,2>&  _v1,
	      const VectorT<Scalar,2>&  _v2,
	      VectorT<Scalar,2>&        _result )
{
  Scalar x0(_v0[0]), y0(_v0[1]), xy0(x0*x0+y0*y0),
         x1(_v1[0]), y1(_v1[1]), xy1(x1*x1+y1*y1),
         x2(_v2[0]), y2(_v2[1]), xy2(x2*x2+y2*y2),
         a(x0*y1 - x0*y2 - x1*y0 + x1*y2 + x2*y0 - x2*y1),
         b(xy0*y1 - xy0*y2 - xy1*y0 + xy1*y2 + xy2*y0 - xy2*y1),
         c(xy0*x1 - xy0*x2 - xy1*x0 + xy1*x2 + xy2*x0 - xy2*x1);

  if (Scalar(1.0)+a == Scalar(1.0)) {
    std::cerr << "circumcircle: colinear points\n";
    return false;
  }

  _result[0] = 0.5*b/a;
  _result[1] = -0.5*c/a;
  
  return true;
}



//== N-DIM STUFF ============================================================== 


template <typename Scalar, int N>
Scalar
aspectRatio( const VectorT<Scalar, N>& _v0,
	     const VectorT<Scalar, N>& _v1,
	     const VectorT<Scalar, N>& _v2 )
{
  Scalar d0 = (_v0-_v1).sqrnorm(),
         d1 = (_v1-_v2).sqrnorm(),
         d2 = (_v2-_v0).sqrnorm();

  if (d0 < d1) {
    if (d0 < d2) {
      if (d1 < d2) return sqrt(d2/d0);
      else   	   return sqrt(d1/d0);
    }
    else           return sqrt(d1/d2);
  }
  else {
    if (d1 < d2) {
      if (d0 < d2) return sqrt(d2/d1);
      else   	   return sqrt(d0/d1);
    }
    else           return sqrt(d0/d2);
  }    
}


//-----------------------------------------------------------------------------


template <typename Scalar, int N>
Scalar
roundness( const VectorT<Scalar, N>& _v0,
	   const VectorT<Scalar, N>& _v1,
	   const VectorT<Scalar, N>& _v2 )
{
  const double FOUR_ROOT3 = 6.928203230275509;

  double area = 0.5*((_v1-_v0)%(_v2-_v0)).norm();

  return (Scalar) (FOUR_ROOT3 * area / ((_v0-_v1).sqrnorm() +
					(_v1-_v2).sqrnorm() +
					(_v2-_v0).sqrnorm() ));
}

1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
template<typename Vec>
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 )
{
    //This code effectively replicates the method described by Moeller et al. in "Fast, Minimum Storage Ray-Triangle Intersection".
    Vec edge1, edge2, tvec, pvec, qvec;
    typename Vec::value_type det, inv_det;

    //find vectors for two edges sharing v0
    edge1 = _v1-_v0;
    edge2 = _v2-_v0;

    //begin calculating determinant - also used to calculate u parameter
    pvec = _dir % edge2;

    //if determinant is near zero, the ray lies in plane of triangle
    det = edge1 | pvec;

    if (det > -0.000001 && det < 0.000001)
        return false;
    inv_det = 1.0 / det;

    //calculate distance from vert0 to ray origin
    tvec = _o - _v0;

    //calculate U parameter and test bounds
    _u = (tvec | pvec) * inv_det;
    if (_u < 0.0 || _u > 1.0)
        return false;

    //prepare to test V parameter
    qvec = tvec % edge1;

    //calculate V parameter and test bounds
    _v = (_dir | qvec) * inv_det;
    if (_v < 0.0 || _u + _v > 1.0)
        return false;

    //Intersection found! Calculate t and exit...
    _t = (edge2 | qvec) * inv_det;
    return true;
}

template<typename Vec>
bool
axisAlignedBBIntersection( const Vec&  _o,
                  const Vec&  _dir,
                  const Vec& _bbmin,
                  const Vec& _bbmax,
                  typename Vec::value_type& tmin,
                  typename Vec::value_type& tmax )
{
    /*
    * Ray-box intersection using IEEE numerical properties to ensure that the
    * test is both robust and efficient, as described in:
    *
    *      Amy Williams, Steve Barrus, R. Keith Morley, and Peter Shirley
    *      "An Efficient and Robust Ray-Box Intersection Algorithm"
    *      Journal of graphics tools, 10(1):49-54, 2005
    *
    */
    typename Vec::value_type tymin, tymax, tzmin, tzmax;
    Vec inv_dir;

    inv_dir[0] = 1/_dir[0];
    inv_dir[1] = 1/_dir[1];
    inv_dir[2] = 1/_dir[2];

    if (inv_dir[0] >= 0) {
        tmin = (_bbmin[0] - _o[0]) * inv_dir[0];
        tmax = (_bbmax[0] - _o[0]) * inv_dir[0];
    }
    else {
        tmin = (_bbmax[0] - _o[0]) * inv_dir[0];
        tmax = (_bbmin[0] - _o[0]) * inv_dir[0];
    }

    if (inv_dir[1] >= 0) {
        tymin = (_bbmin[1] - _o[1]) * inv_dir[1];
        tymax = (_bbmax[1] - _o[1]) * inv_dir[1];
    }
    else {
        tymin = (_bbmax[1] - _o[1]) * inv_dir[1];
        tymax = (_bbmin[1] - _o[1]) * inv_dir[1];
    }

    if ( (tmin > tymax) || (tymin > tmax) )
        return false;
    if (tymin > tmin)
        tmin = tymin;
    if (tymax < tmax)
        tmax = tymax;

    if (inv_dir[2] >= 0) {
        tzmin = (_bbmin[2] - _o[2]) * inv_dir[2];
        tzmax = (_bbmax[2] - _o[2]) * inv_dir[2];
    }
    else {
        tzmin = (_bbmax[2] - _o[2]) * inv_dir[2];
        tzmax = (_bbmin[2] - _o[2]) * inv_dir[2];
    }

    if ( (tmin > tzmax) || (tzmin > tmax) )
        return false;
    if (tzmin > tmin)
        tmin = tzmin;
    if (tzmax < tmax)
        tmax = tzmax;
    
    return true;
}
Jan Möbius's avatar
   
Jan Möbius committed
1222
1223
1224
1225
1226

//=============================================================================
} // namespace Geometry
} // namespace ACG
//=============================================================================