GLVis  v4.2
Accurate and flexible finite element visualization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
geom_utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-443271.
4 //
5 // This file is part of the GLVis visualization tool and library. For more
6 // information and source code availability see https://glvis.org.
7 //
8 // GLVis is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef GLVIS_GEOM_UTILS_HPP
13 #define GLVIS_GEOM_UTILS_HPP
14 #include "mfem.hpp"
15 
16 // Some inline functions
17 
18 inline void LinearCombination(const double a, const double x[],
19  const double b, const double y[], double z[])
20 {
21  z[0] = a*x[0] + b*y[0];
22  z[1] = a*x[1] + b*y[1];
23  z[2] = a*x[2] + b*y[2];
24 }
25 
26 inline double InnerProd(const double a[], const double b[])
27 {
28  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
29 }
30 
31 inline void CrossProd(const double a[], const double b[], double cp[])
32 {
33  cp[0] = a[1] * b[2] - a[2] * b[1];
34  cp[1] = a[2] * b[0] - a[0] * b[2];
35  cp[2] = a[0] * b[1] - a[1] * b[0];
36 }
37 
38 inline int Normalize(double v[])
39 {
40  double len = sqrt(InnerProd(v, v));
41  if (len > 0.0)
42  {
43  len = 1.0 / len;
44  }
45  else
46  {
47  return 1;
48  }
49  for (int i = 0; i < 3; i++)
50  {
51  v[i] *= len;
52  }
53  return 0;
54 }
55 
56 inline int Normalize(mfem::DenseMatrix &normals)
57 {
58  int err = 0;
59  for (int i = 0; i < normals.Width(); i++)
60  {
61  err += Normalize(&normals(0, i));
62  }
63  return err;
64 }
65 // 'v' must define three vectors that add up to the zero vector
66 // that is v[0], v[1], and v[2] are the sides of a triangle
67 inline int UnitCrossProd(double v[][3], double nor[])
68 {
69  // normalize the three vectors
70  for (int i = 0; i < 3; i++)
71  if (Normalize(v[i]))
72  {
73  return 1;
74  }
75 
76  // find the pair that forms an angle closest to pi/2, i.e. having
77  // the longest cross product:
78  double cp[3][3], max_a = 0.;
79  int k = 0;
80  for (int i = 0; i < 3; i++)
81  {
82  CrossProd(v[(i+1)%3], v[(i+2)%3], cp[i]);
83  double a = sqrt(InnerProd(cp[i], cp[i]));
84  if (max_a < a)
85  {
86  max_a = a, k = i;
87  }
88  }
89  if (max_a == 0.)
90  {
91  return 1;
92  }
93  for (int i = 0; i < 3; i++)
94  {
95  nor[i] = cp[k][i] / max_a;
96  }
97 
98  return 0;
99 }
100 
101 inline int Compute3DUnitNormal(const double p1[], const double p2[],
102  const double p3[], double nor[])
103 {
104  double v[3][3];
105 
106  for (int i = 0; i < 3; i++)
107  {
108  v[0][i] = p2[i] - p1[i];
109  v[1][i] = p3[i] - p2[i];
110  v[2][i] = p1[i] - p3[i];
111  }
112 
113  return UnitCrossProd(v, nor);
114 }
115 
116 inline int Compute3DUnitNormal (const double p1[], const double p2[],
117  const double p3[], const double p4[], double nor[])
118 {
119  double v[3][3];
120 
121  for (int i = 0; i < 3; i++)
122  {
123  // cross product of the two diagonals:
124  /*
125  v[0][i] = p3[i] - p1[i];
126  v[1][i] = p4[i] - p2[i];
127  v[2][i] = (p1[i] + p2[i]) - (p3[i] + p4[i]);
128  */
129 
130  // cross product of the two vectors connecting the midpoints of the
131  // two pairs of opposing sides; this gives the normal vector in the
132  // midpoint of the quad:
133  v[0][i] = 0.5 * ((p2[i] + p3[i]) - (p1[i] + p4[i]));
134  v[1][i] = 0.5 * ((p4[i] + p3[i]) - (p1[i] + p2[i]));
135  v[2][i] = p1[i] - p3[i];
136  }
137 
138  return UnitCrossProd(v, nor);
139 }
140 
141 inline int ProjectVector(double v[], const double n[])
142 {
143  // project 'v' on the plane with normal given by 'n' and then normalize 'v'
144  LinearCombination(InnerProd(n, n), v, -InnerProd(v, n), n, v);
145  return Normalize(v);
146 }
147 
148 #endif // GLVIS_GEOM_UTILS_HPP
int ProjectVector(double v[], const double n[])
Definition: geom_utils.hpp:141
int Normalize(double v[])
Definition: geom_utils.hpp:38
int Compute3DUnitNormal(const double p1[], const double p2[], const double p3[], double nor[])
Definition: geom_utils.hpp:101
void CrossProd(const double a[], const double b[], double cp[])
Definition: geom_utils.hpp:31
double InnerProd(const double a[], const double b[])
Definition: geom_utils.hpp:26
int UnitCrossProd(double v[][3], double nor[])
Definition: geom_utils.hpp:67
void LinearCombination(const double a, const double x[], const double b, const double y[], double z[])
Definition: geom_utils.hpp:18