MOAB: Mesh Oriented datABase  (version 5.3.0)
measure.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Lawrence Livermore National Laboratory under
00003  * contract number B545069 with the University of Wisconsin - Madison.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024  */
00025 
00026 #include "moab/CartVect.hpp"
00027 #include "measure.hpp"
00028 using namespace moab;
00029 
00030 inline static double tet_volume( const CartVect& v0, const CartVect& v1, const CartVect& v2, const CartVect& v3 )
00031 {
00032     return 1. / 6. * ( ( ( v1 - v0 ) * ( v2 - v0 ) ) % ( v3 - v0 ) );
00033 }
00034 
00035 double edge_length( const double* start_vtx_coords, const double* end_vtx_coords )
00036 {
00037     const CartVect* start = reinterpret_cast< const CartVect* >( start_vtx_coords );
00038     const CartVect* end   = reinterpret_cast< const CartVect* >( end_vtx_coords );
00039     return ( *start - *end ).length();
00040 }
00041 
00042 double measure( moab::EntityType type, int num_vertices, const double* vertex_coordinates )
00043 {
00044     const CartVect* coords = reinterpret_cast< const CartVect* >( vertex_coordinates );
00045     switch( type )
00046     {
00047         case moab::MBEDGE:
00048             return ( coords[0] - coords[1] ).length();
00049         case moab::MBTRI:
00050             return 0.5 * ( ( coords[1] - coords[0] ) * ( coords[2] - coords[0] ) ).length();
00051         case moab::MBQUAD:
00052             num_vertices = 4;
00053         case moab::MBPOLYGON: {
00054             CartVect mid( 0, 0, 0 );
00055             for( int i = 0; i < num_vertices; ++i )
00056                 mid += coords[i];
00057             mid /= num_vertices;
00058 
00059             double sum = 0.0;
00060             for( int i = 0; i < num_vertices; ++i )
00061             {
00062                 int j = ( i + 1 ) % num_vertices;
00063                 sum += ( ( mid - coords[i] ) * ( mid - coords[j] ) ).length();
00064             }
00065             return 0.5 * sum;
00066         }
00067         case moab::MBTET:
00068             return tet_volume( coords[0], coords[1], coords[2], coords[3] );
00069         case moab::MBPYRAMID:
00070             return tet_volume( coords[0], coords[1], coords[2], coords[4] ) +
00071                    tet_volume( coords[0], coords[2], coords[3], coords[4] );
00072         case moab::MBPRISM:
00073             return tet_volume( coords[0], coords[1], coords[2], coords[5] ) +
00074                    tet_volume( coords[3], coords[5], coords[4], coords[0] ) +
00075                    tet_volume( coords[1], coords[4], coords[5], coords[0] );
00076         case moab::MBHEX:
00077             return tet_volume( coords[0], coords[1], coords[3], coords[4] ) +
00078                    tet_volume( coords[7], coords[3], coords[6], coords[4] ) +
00079                    tet_volume( coords[4], coords[5], coords[1], coords[6] ) +
00080                    tet_volume( coords[1], coords[6], coords[3], coords[4] ) +
00081                    tet_volume( coords[2], coords[6], coords[3], coords[1] );
00082         default:
00083             return 0.0;
00084     }
00085 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines