1
  2
  3
  4
  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
 37
 38
 39
 40
 41
 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
//-------------------------------------------------------------------------
// Filename      : GeoTet.cpp
//
// Purpose       : Tet class used for geometry operations.  See also GeoNode.
//
// Description   : light-weight entity used for computational geometry 
//                 tools.  Don't load this up with extra stuff!
//
// Creator       : Steve Owen
//
// Creation Date : 8/13/2003
//
// Owner         : Steve Owen
//-------------------------------------------------------------------------

#include "GeoTet.hpp"
#include "GeoNode.hpp"
#include "GfxDebug.hpp"
#include "CubitMessage.hpp"

#ifndef SQR
#define SQR(x) ((x) * (x))
#endif

//--------------------------------------------------------------------------
// Function: GeoTet
// Description: constructor
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
GeoTet::GeoTet( GeoNode *nodes[4] ) : isMarked(0), isInside(0)
{
  mNodes[0] = nodes[0];
  mNodes[1] = nodes[1];
  mNodes[2] = nodes[2];
  mNodes[3] = nodes[3];
}

//--------------------------------------------------------------------------
// Function: GeoTet
// Description: destructor
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
GeoTet::~GeoTet()
{
}

//--------------------------------------------------------------------------
// Function: tet_nodes
// Description: return the nodes on this tet
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
void GeoTet::tet_nodes( GeoNode *&na, GeoNode *&nb, GeoNode *&nc, GeoNode *&nd )
{
  na = mNodes[0];
  nb = mNodes[1];
  nc = mNodes[2];
  nd = mNodes[3];
}

//--------------------------------------------------------------------------
// Function: tet_face_nodes
// Description: return the on a face of this tet.
// Author: mlstate
// Date: 9/08/2004
//---------------------------------------------------------------------------
void GeoTet::tet_face_nodes( int face_index, GeoNode *&na, GeoNode *&nb, GeoNode *&nc )
{
    int node_face_idx[4][3] = { { 1, 2, 3 },
                                { 3, 2, 0 },
                                { 0, 1, 3 },
                                { 2, 1, 0 } };
    na = mNodes[node_face_idx[face_index][0]];
    nb = mNodes[node_face_idx[face_index][1]];
    nc = mNodes[node_face_idx[face_index][2]];

}

//--------------------------------------------------------------------------
// Function: get_connected_tet
// Description: return the adjacent tet at the face of face_indx.
// Author: mlstate
// Date: 9/08/2004
//---------------------------------------------------------------------------
GeoTet *GeoTet::get_connected_tet( int face_indx )
{
    GeoNode *n1, *n2, *n3;
    tet_face_nodes( face_indx, n1, n2, n3 );
    return get_connected_tet( n1, n2, n3 );
}

//--------------------------------------------------------------------------
// Function: get_connected_tet
// Description: return the adjacent tet at the face defined by the three nodes
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
GeoTet *GeoTet::get_connected_tet( GeoNode *na, GeoNode *nb, GeoNode *nc )
{
  GeoTet *adj_tet = NULL;
  int ii;

  // get the tets adjacent 
  DLIList<GeoTet *> *tet_list_ptr = na->tet_list();
  GeoTet *tet_ptr;
  for (ii=0; ii<tet_list_ptr->size() && !adj_tet; ii++)
  {
    tet_ptr = tet_list_ptr->get_and_step();
    if (tet_ptr != this)
    {
      if (tet_ptr->node_index( nb ) >= 0 &&
          tet_ptr->node_index( nc ) >= 0)
      {
        adj_tet = tet_ptr;
      }
    }
  }

  return adj_tet;
}

//--------------------------------------------------------------------------
// Function: node_index
// Description: return the index of the node in the tet
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
int GeoTet::node_index( GeoNode *node_ptr )
{
  int ii;
  for (ii=0; ii<4; ii++)
  {
    if (mNodes[ii] == node_ptr)
      return ii;
  }
  return -1;
}

//--------------------------------------------------------------------------
// Function: opposite_edge
// Description: return nodes on the opposite edge of the tet.  a_node and 
//              b_node must be on this tet.  c_node and d_node are in
//              no particular order
// Author: sjowen
// Date: 01/29/2004
//---------------------------------------------------------------------------
void GeoTet::opposite_edge( GeoNode *a_node, GeoNode *b_node, 
                            GeoNode *&c_node, GeoNode *&d_node )
{
  int ii;
  c_node = d_node = NULL;
  for (ii=0; ii<4; ii++)
  {
    if (mNodes[ii] != a_node && mNodes[ii] != b_node)
    {
      if (!c_node)
      {
        c_node = mNodes[ii];
      }
      else if(!d_node)
      {
        d_node = mNodes[ii];
      }
      else
      {
        PRINT_ERROR("a_node or b_node are not on this tet.\n");
        return;
      }
    }
  }
}

//--------------------------------------------------------------------------
// Function: dgtet
// Description: global debug function to draw a GeoTet 
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
void dgtet( GeoTet *tet )<--- The function 'dgtet' is never used.
{
  GfxDebug::draw_geotet( tet, CUBIT_YELLOW_INDEX );
  GfxDebug::flush();
}

//--------------------------------------------------------------------------
// Function: dgnode
// Description: global debug function to draw a GeoNode 
// Author: sjowen
// Date: 8/13/2003
//---------------------------------------------------------------------------
void dgnode( GeoNode *node )<--- The function 'dgnode' is never used.
{
  GfxDebug::draw_geonode( node, CUBIT_RED_INDEX );
  GfxDebug::flush();
}

CubitStatus GeoTet::circumsphere( CubitVector &center, double *radius )
{
  double reltol = DBL_EPSILON * 100.0;

  CubitVector a = mNodes[0]->coordinates();
  CubitVector b = mNodes[1]->coordinates();
  CubitVector c = mNodes[2]->coordinates();
  CubitVector d = mNodes[3]->coordinates();

  CubitVector da = a - d;
  CubitVector db = b - d;
  CubitVector dc = c - d;

  double rhsa = 0.5*(SQR(da.x()) + SQR(da.y()) + SQR(da.z()));
  double rhsb = 0.5*(SQR(db.x()) + SQR(db.y()) + SQR(db.z()));
  double rhsc = 0.5*(SQR(dc.x()) + SQR(dc.y()) + SQR(dc.z()));

  double cpa = db.y()*dc.z() - dc.y()*db.z();
  double cpb = dc.y()*da.z() - da.y()*dc.z();
  double cpc = da.y()*db.z() - db.y()*da.z();

  double det = da.x()*cpa + db.x()*cpb + dc.x()*cpc;

  double xmax = CUBIT_MAX(fabs(a.x()),fabs(b.x()));
         xmax = CUBIT_MAX(xmax,fabs(c.x())); 
         xmax = CUBIT_MAX(xmax,fabs(d.x()));
  double ymax = CUBIT_MAX(fabs(a.y()),fabs(b.y()));
         ymax = CUBIT_MAX(ymax,fabs(c.y())); 
         ymax = CUBIT_MAX(ymax,fabs(d.y()));
  double zmax = CUBIT_MAX(fabs(a.z()),fabs(b.z()));
         zmax = CUBIT_MAX(zmax,fabs(c.z())); 
         zmax = CUBIT_MAX(zmax,fabs(d.z()));
  double tolabs = reltol*xmax*ymax*zmax;
  if (fabs(det) <= tolabs) {
    return CUBIT_FAILURE;
  }
  center.x( (rhsa*cpa + rhsb*cpb + rhsc*cpc)/det );
  cpa = db.x()*rhsc - dc.x()*rhsb;
  cpb = dc.x()*rhsa - da.x()*rhsc;
  cpc = da.x()*rhsb - db.x()*rhsa;
  center.y( (da.z()*cpa + db.z()*cpb + dc.z()*cpc)/det );
  center.z( -(da.y()*cpa + db.y()*cpb + dc.y()*cpc)/det );
  center += d;

  if ( radius )
  {
      double radsq = SQR(center.x()) + SQR(center.y()) + SQR(center.z());
      *radius = sqrt( radsq );
  }

  return CUBIT_SUCCESS;
}

// EOF