Intrepid
Intrepid_MiniTensor_Geometry.t.h
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #if !defined(Intrepid_MiniTensor_Geometry_t_h)
43 #define Intrepid_MiniTensor_Geometry_t_h
44 
45 #include <iterator>
46 
47 namespace Intrepid {
48 
49 //
50 // Length of a segment
51 //
52 template<typename T>
53 T
54 length(Vector<T> const & p0, Vector<T> const & p1)
55 {
56  Vector<T> v = p1 - p0;
57  return norm(v);
58 }
59 
60 //
61 // Area of a triangle
62 //
63 template<typename T>
64 T
65 area(Vector<T> const & p0, Vector<T> const & p1,
66  Vector<T> const & p2)
67 {
68  Vector<T> u = p1 - p0;
69  Vector<T> v = p2 - p0;
70  T a = 0.5 * norm(cross(u,v));
71  return a;
72 }
73 
74 //
75 // Area of a quadrilateral, assumed planar. If not planar, returns
76 // the sum of the areas of the two triangles p0,p1,p2 and p0,p2,p3
77 //
78 template<typename T>
79 T
80 area(Vector<T> const & p0, Vector<T> const & p1,
81  Vector<T> const & p2, Vector<T> const & p3)
82 {
83  return area(p0, p1, p2) + area(p0, p2, p3);
84 }
85 
86 //
87 // Volume of tetrahedron
88 //
89 template<typename T>
90 T
91 volume(Vector<T> const & p0, Vector<T> const & p1,
92  Vector<T> const & p2, Vector<T> const & p3)
93 {
94  // Area of base triangle
95  T A = area(p0, p1, p2);
96 
97  // Height
98  Vector<T> u = p1 - p0;
99  Vector<T> v = p2 - p0;
100  Vector<T> n = cross(u, v);
101  n = n / norm(n);
102  Vector<T> w = p3 - p0;
103  T h = std::abs(dot(w, n));
104 
105  // Volume
106  T V = A * h / 3.0;
107  return V;
108 }
109 
110 //
111 // Volume of pyramid of quadrilateral base
112 // Base is assumed planar
113 // Base is p0,p1,p2,p3
114 // Apex is p4
115 //
116 template<typename T>
117 T
118 volume(Vector<T> const & p0, Vector<T> const & p1,
119  Vector<T> const & p2, Vector<T> const & p3,
120  Vector<T> const & p4)
121 {
122  // Area of base quadrilateral
123  T A = area(p0, p1, p2, p3);
124 
125  // Height
126  Vector<T> u = p1 - p0;
127  Vector<T> v = p2 - p0;
128  Vector<T> n = cross(u, v);
129  n = n / norm(n);
130  Vector<T> w = p4 - p0;
131  T h = std::abs(dot(w, n));
132 
133  // Volume
134  T V = A * h / 3.0;
135  return V;
136 }
137 
138 //
139 // Volume of hexahedron
140 // Assumption: all faces are planar
141 // Decompose into 3 pyramids
142 //
143 template<typename T>
144 T
145 volume(Vector<T> const & p0, Vector<T> const & p1,
146  Vector<T> const & p2, Vector<T> const & p3,
147  Vector<T> const & p4, Vector<T> const & p5,
148  Vector<T> const & p6, Vector<T> const & p7)
149 {
150  // 1st pyramid
151  T V1 = volume(p4, p7, p6, p5, p0);
152 
153  // 2nd pyramid
154  T V2 = volume(p3, p2, p6, p7, p0);
155 
156  // 3rd pyramid
157  T V3 = volume(p1, p5, p6, p2, p0);
158 
159  return V1 + V2 + V3;
160 }
161 
162 //
163 // Centroids of segment, triangle, tetrahedron, quadrilateral
164 // and hexahedron
165 // For these we can just take the average of the vertices.
166 // WARNING: This is not the center of mass.
167 //
168 template<typename T>
169 Vector<T>
170 centroid(std::vector<Vector<T> > const & points)
171 {
172  Vector<T> C(points[0].get_dimension());
173  C.clear();
174  typedef typename std::vector<Vector<T> >::size_type sizeT;
175  sizeT n = points.size();
176 
177  for (sizeT i = 0; i < n; ++i) {
178  C += points[i];
179  }
180  return C / static_cast<T>(n);
181 }
182 
183 //
184 // The surface normal of a face
185 // Input: 3 independent nodes on the face
186 // Output: unit normal vector
187 //
188 template<typename T>
189 Vector<T>
190 normal(Vector<T> const & p0,
191  Vector<T> const & p1,
192  Vector<T> const & p2)
193  {
194  // Construct 2 independent vectors
195  Vector<T> v0 = p1 - p0;
196  Vector<T> v1 = p2 - p0;
197 
198  Vector<T> n = cross(v0,v1);
199  n = n / norm(n);
200  return n;
201  }
202 
203 //
204 // Given 3 points p0, p1, p2 that define a plane
205 // determine if point p is in the same side of the normal
206 // to the plane as defined by the right hand rule.
207 //
208 template<typename T>
209 bool
210 in_normal_side(
211  Vector<T> const & p,
212  Vector<T> const & p0,
213  Vector<T> const & p1,
214  Vector<T> const & p2)
215 {
216  Vector<T> v0 = p1 - p0;
217  Vector<T> v1 = p2 - p0;
218 
219  Vector<T> n = cross(v0, v1);
220  Vector<T> v = p - p0;
221 
222  T s = dot(v, n);
223 
224  if (s < 0.0) return false;
225 
226  return true;
227 }
228 
229 //
230 // Given two iterators to a container of points,
231 // find the associated bounding box.
232 // \param start, end: define sequence of points
233 // \return vectors that define the bounding box
234 //
235 template<typename T, typename I>
236 std::pair< Vector<T>, Vector<T> >
237 bounding_box(I start, I end)
238 {
239  I
240  it = start;
241 
242  Vector<T>
243  min = (*it);
244 
245  Vector<T>
246  max = min;
247 
248  const Index
249  N = min.get_dimension();
250 
251  ++it;
252 
253  for (; it != end; ++it) {
254 
255  Vector<T> const &
256  point = (*it);
257 
258  for (Index i = 0; i < N; ++i) {
259  const T s = point(i);
260  if (s < min(i)) min(i) = s;
261  if (s > max(i)) max(i) = s;
262  }
263 
264  }
265 
266  return std::make_pair(min, max);
267 }
268 
269 //
270 // Determine if a given point is inside a bounding box.
271 // \param p the point
272 // \param min, max points defining the box
273 // \return whether the point is inside
274 //
275 template<typename T>
276 bool
277 in_box(
278  Vector<T> const & p,
279  Vector<T> const & min,
280  Vector<T> const & max)
281 {
282  const Index
283  N = p.get_dimension();
284 
285  assert(min.get_dimension() == N);
286  assert(max.get_dimension() == N);
287 
288  for (Index i = 0; i < N; ++i) {
289  T const & s = p(i);
290  if (s < min(i)) return false;
291  if (s > max(i)) return false;
292  }
293 
294  return true;
295 }
296 
297 //
298 // Generate random point inside bounding box
299 // \param min, max the bounding box
300 // \return p point inside box
301 //
302 template<typename T>
303 Vector<T>
304 random_in_box(Vector<T> const & min, Vector<T> const & max)
305 {
306  const Index
307  N = min.get_dimension();
308 
309  assert(max.get_dimension() == N);
310 
311  Vector<T> p(N);
312 
313  for (Index i = 0; i < N; ++i) {
314  p(i) = (max(i) - min(i)) * T(std::rand())/T(RAND_MAX) + min(i);
315  }
316 
317  return p;
318 }
319 
320 //
321 // Given 4 points p0, p1, p2, p3 that define a tetrahedron
322 // determine if point p is inside it.
323 //
324 template<typename T>
325 bool
326 in_tetrahedron(
327  Vector<T> const & p,
328  Vector<T> const & p0,
329  Vector<T> const & p1,
330  Vector<T> const & p2,
331  Vector<T> const & p3)
332 {
333  if (in_normal_side(p, p0, p1, p2) == false) {
334 
335  return false;
336 
337  } else if (in_normal_side(p, p0, p3, p1) == false) {
338 
339  return false;
340 
341  } else if (in_normal_side(p, p1, p3, p2) == false) {
342 
343  return false;
344 
345  } else if (in_normal_side(p, p2, p3, p0) == false) {
346 
347  return false;
348 
349  }
350 
351  return true;
352 }
353 
354 //
355 // Given 8 points that define a hexahedron
356 // determine if point p is inside it.
357 // Assumption: faces are planar
358 //
359 template<typename T>
360 bool
361 in_hexahedron(
362  Vector<T> const & p,
363  Vector<T> const & p0,
364  Vector<T> const & p1,
365  Vector<T> const & p2,
366  Vector<T> const & p3,
367  Vector<T> const & p4,
368  Vector<T> const & p5,
369  Vector<T> const & p6,
370  Vector<T> const & p7)
371 {
372  if (in_normal_side(p, p0, p1, p2) == false) {
373 
374  return false;
375 
376  } else if (in_normal_side(p, p0, p4, p5) == false) {
377 
378  return false;
379 
380  } else if (in_normal_side(p, p1, p5, p6) == false) {
381 
382  return false;
383 
384  } else if (in_normal_side(p, p2, p6, p7) == false) {
385 
386  return false;
387 
388  } else if (in_normal_side(p, p3, p7, p4) == false) {
389 
390  return false;
391 
392  } else if (in_normal_side(p, p4, p7, p6) == false) {
393 
394  return false;
395 
396  }
397 
398  return true;
399 }
400 
401 //
402 // Closest point
403 // \param p the point
404 // \param n vector of points to test
405 // \return index to closest point
406 //
407 template<typename T>
408 typename std::vector< Vector<T> >::size_type
409 closest_point(Vector<T> const & p, std::vector< Vector<T> > const & n)
410 {
411  assert(n.size() > 0);
412 
413  typename std::vector< Vector<T> >::size_type
414  index = 0;
415 
416  const Vector<double>
417  v0 = p - n[0];
418 
419  T
420  min = norm_square(v0);
421 
422  for (typename std::vector< Vector<T> >::size_type i = 1;
423  i < n.size();
424  ++i) {
425 
426  const Vector<double>
427  vi = p - n[i];
428 
429  const T
430  s = norm_square(vi);
431 
432  if (s < min) {
433  min = s;
434  index = i;
435  }
436 
437  }
438 
439  return index;
440 }
441 
442 // Median of a sequence defined by random
443 // access iterators. Undefined for empty set.
444 // \param begin, end Iterators that define the sequence
445 // \return median of sequence
446 //
447 template<typename T, typename Iterator>
448 T
449 median(Iterator begin, Iterator end)
450 {
451  // Firewall
452  if (begin == end) {
453  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
454  std::cerr << std::endl;
455  std::cerr << "Median undefined for empty set." << std::endl;
456  exit(1);
457  }
458 
459  Index const
460  size = std::distance(begin, end);
461 
462  T
463  median;
464 
465  Index
466  mid_index = size / 2;
467 
468  Iterator
469  mid_iterator = begin + mid_index;
470 
471  std::partial_sort(begin, mid_iterator, end);
472 
473  if (size % 2 == 0) {
474 
475  // Even number of elements
476  T
477  b = *mid_iterator;
478 
479  Iterator
480  previous = mid_iterator - 1;
481 
482  T
483  a = *previous;
484 
485  median = (a + b) / 2.0;
486 
487  } else {
488 
489  // Odd number of elements
490  median = *mid_iterator;
491 
492  }
493 
494  return median;
495 
496 }
497 
498 //
499 // Given quadrilateral nodes and a position
500 // in parametric coordinates, interpolate.
501 // \param xi position in parametric coordinates
502 // \param p0 ... corner nodes
503 // \return interpolated position
504 //
505 template<typename T>
506 Vector<T>
507 interpolate_quadrilateral(
508  Vector<T> & xi,
509  Vector<T> const & p0,
510  Vector<T> const & p1,
511  Vector<T> const & p2,
512  Vector<T> const & p3)
513  {
514 
515  T
516  N0 = 0.25 * (1 - xi(0)) * (1 - xi(1));
517 
518  T
519  N1 = 0.25 * (1 + xi(0)) * (1 - xi(1));
520 
521  T
522  N2 = 0.25 * (1 + xi(0)) * (1 + xi(1));
523 
524  T
525  N3 = 0.25 * (1 - xi(0)) * (1 + xi(1));
526 
527  const Vector<T>
528  p = N0 * p0 + N1 * p1 + N2 * p2 + N3 * p3;
529 
530  return p;
531  }
532 
533 //
534 // Given triangle nodes and a position
535 // in parametric coordinates, interpolate.
536 // \param xi position in parametric coordinates
537 // \param p0 ... corner nodes
538 // \return interpolated position
539 //
540 template<typename T>
541 Vector<T>
542 interpolate_triangle(
543  Vector<T> & xi,
544  Vector<T> const & p0,
545  Vector<T> const & p1,
546  Vector<T> const & p2)
547  {
548  xi(2) = 1.0 - xi(0) - xi(1);
549 
550  const Vector<T>
551  p = xi(0) * p0 + xi(1) * p1 + xi(2) * p2;
552 
553  return p;
554  }
555 
556 //
557 // Given hexahedron nodes and a position
558 // in parametric coordinates, interpolate.
559 // \param xi position in parametric coordinates
560 // \param p0 ... corner nodes
561 // \return interpolated position
562 //
563 template<typename T>
564 Vector<T>
565 interpolate_hexahedron(
566  Vector<T> & xi,
567  Vector<T> const & p0,
568  Vector<T> const & p1,
569  Vector<T> const & p2,
570  Vector<T> const & p3,
571  Vector<T> const & p4,
572  Vector<T> const & p5,
573  Vector<T> const & p6,
574  Vector<T> const & p7)
575  {
576 
577  T
578  N0 = 0.125 * (1 - xi(0)) * (1 - xi(1)) * (1 - xi(2));
579 
580  T
581  N1 = 0.125 * (1 + xi(0)) * (1 - xi(1)) * (1 - xi(2));
582 
583  T
584  N2 = 0.125 * (1 + xi(0)) * (1 + xi(1)) * (1 - xi(2));
585 
586  T
587  N3 = 0.125 * (1 - xi(0)) * (1 + xi(1)) * (1 - xi(2));
588 
589  T
590  N4 = 0.125 * (1 - xi(0)) * (1 - xi(1)) * (1 + xi(2));
591 
592  T
593  N5 = 0.125 * (1 + xi(0)) * (1 - xi(1)) * (1 + xi(2));
594 
595  T
596  N6 = 0.125 * (1 + xi(0)) * (1 + xi(1)) * (1 + xi(2));
597 
598  T
599  N7 = 0.125 * (1 - xi(0)) * (1 + xi(1)) * (1 + xi(2));
600 
601  const Vector<T>
602  p =
603  N0 * p0 + N1 * p1 + N2 * p2 + N3 * p3 +
604  N4 * p4 + N5 * p5 + N6 * p6 + N7 * p7;
605 
606  return p;
607  }
608 
609 //
610 // Given tetrahedron nodes and a position
611 // in parametric coordinates, interpolate.
612 // \param xi position in parametric coordinates
613 // \param p0 ... corner nodes
614 // \return interpolated position
615 //
616 template<typename T>
617 Vector<T>
618 interpolate_tetrahedron(
619  Vector<T> & xi,
620  Vector<T> const & p0,
621  Vector<T> const & p1,
622  Vector<T> const & p2,
623  Vector<T> const & p3)
624  {
625  xi(3) = 1.0 - xi(0) - xi(1) - xi(2);
626 
627  const Vector<T>
628  p = xi(0) * p0 + xi(1) * p1 + xi(2) * p2 + xi(3) * p3;
629 
630  return p;
631  }
632 
641 template<typename T>
642 Vector<T>
643 interpolate_element(
644  ELEMENT::Type element_type,
645  Vector<T> & xi,
646  std::vector< Vector<T> > const & v)
647  {
648  Vector<double> p;
649 
650  switch (element_type) {
651 
652  case ELEMENT::TRIANGULAR:
653  p = interpolate_triangle(xi, v[0], v[1], v[2]);
654  break;
655 
656  case ELEMENT::QUADRILATERAL:
657  p = interpolate_quadrilateral(xi, v[0], v[1], v[2], v[3]);
658  break;
659 
660  case ELEMENT::TETRAHEDRAL:
661  p = interpolate_tetrahedron(xi, v[0], v[1], v[2], v[3]);
662  break;
663 
664  case ELEMENT::HEXAHEDRAL:
665  p = interpolate_hexahedron(
666  xi, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
667  break;
668 
669  default:
670  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
671  std::cerr << std::endl;
672  std::cerr << "Unknown element type in interpolation.";
673  std::cerr << std::endl;
674  exit(1);
675  break;
676 
677  }
678 
679  return p;
680 
681  }
682 
683 //
684 // Given a vector of points, determine
685 // distances between all of them.
686 // \param vector of points
687 // \return distance matrix
688 //
689 template<typename T>
690 std::vector< std::vector<T> >
691 distance_matrix(std::vector< Vector<T> > const & points)
692 {
693  const Index
694  number_points = points.size();
695 
696  std::vector< std::vector<T> >
697  distances(number_points);
698 
699  for (Index i = 0; i < number_points; ++i) {
700 
701  distances[i].resize(number_points);
702 
703  distances[i][i] = 0.0;
704 
705  for (Index j = i + 1; j < number_points; ++j) {
706 
707  const T
708  distance = norm(points[i] - points[j]);
709 
710  distances[i][j] = distance;
711  distances[j][i] = distance;
712 
713  }
714 
715  }
716 
717  return distances;
718 }
719 
720 //
721 // Given a distance matrix, determine the minimum
722 // distance between two distinct points.
723 // \param distance matrix
724 // \return minimum distance
725 //
726 template<typename T>
727 std::vector<T>
728 minimum_distances(std::vector< std::vector<T> > const & distances)
729 {
730  const Index
731  number_points = distances.size();
732 
733  std::vector<T>
734  minima(number_points);
735 
736  // First row
737  T
738  minimum = distances[0][1];
739 
740  for (Index j = 2; j < number_points; ++j) {
741  minimum = std::min(minimum, distances[0][j]);
742  }
743 
744  minima[0] = minimum;
745 
746  // Remaining rows
747  for (Index i = 1; i < number_points; ++i) {
748 
749  minimum = distances[i][0];
750 
751  for (Index j = 1; j < number_points; ++j) {
752 
753  if (i == j) continue;
754 
755  minimum = std::min(minimum, distances[i][j]);
756 
757  }
758 
759  minima[i] = minimum;
760 
761  }
762 
763  return minima;
764 }
765 
766 } // namespace Intrepid
767 
768 #endif // Intrepid_MiniTensor_Geometry_t_h