1 /* *****************************************************************
2     MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4     Copyright 2010 Sandia National Laboratories.  Developed at the
5     University of Wisconsin--Madison under SNL contract number
6     624796.  The U.S. Government and the University of Wisconsin
7     retain certain rights to this software.
8 
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Lesser General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13 
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Lesser General Public License for more details.
18 
19     You should have received a copy of the GNU Lesser General Public License
20     (lgpl.txt) along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 
23     (2010) kraftche@cae.wisc.edu
24 
25   ***************************************************************** */
26 
27 
28 /** \file LineDomainTest.cpp
29  *  \brief UnitTests for LineDomain class
30  *  \author Jason Kraftcheck
31  */
32 
33 #include "UnitUtil.hpp"
34 #include "MeshDomain1D.hpp"
35 
36 namespace MBMesquite {
37 
38 class LineDomainTest: public CppUnit::TestFixture
39 {
40   CPPUNIT_TEST_SUITE(LineDomainTest);
41   CPPUNIT_TEST (test_snap_to);
42   CPPUNIT_TEST (test_arc_length);
43   CPPUNIT_TEST (test_position_from_length);
44   CPPUNIT_TEST_SUITE_END();
45 
46 public:
47 
48   void test_snap_to();
49   void test_arc_length();
50   void test_position_from_length();
51 };
52 
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(LineDomainTest, "LineDomainTest");
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(LineDomainTest, "Unit");
55 
test_snap_to()56 void LineDomainTest::test_snap_to()
57 {
58   const Vector3D base1(1,1,1);
59   const Vector3D dir1(-1,-2,-3);
60   LineDomain dom1( base1, dir1 );
61 
62   Vector3D point(0,0,0), result(point);
63   dom1.snap_to( 0, result );
64   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, ((result - base1) * dir1).length(), 1e-6 ); // on the line
65   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, ((point - result) % dir1), 1e-6 ); // moved perp to line
66 
67   point = Vector3D(10,11,12);
68   result = point;
69   dom1.snap_to( 0, result );
70   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, ((result - base1) * dir1).length(), 1e-6 ); // on the line
71   CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, ((point - result) % dir1), 1e-6 ); // moved perp to line
72 }
73 
test_arc_length()74 void LineDomainTest::test_arc_length()
75 {
76   MsqPrintError err(std::cerr);
77 
78   const Vector3D base1(1,1,1);
79   const Vector3D dir1(-1,-2,-3);
80   LineDomain dom1( base1, dir1 );
81 
82   double l1 = 1.0, l2 = M_PI;
83   Vector3D p1 = base1 + l1/dir1.length() * dir1;
84   Vector3D p2 = base1 + l2/dir1.length() * dir1;
85   double len = dom1.arc_length( p1.to_array(), p2.to_array(), err );
86   ASSERT_NO_ERROR(err);
87   CPPUNIT_ASSERT_DOUBLES_EQUAL( l2 - l1, len, 1e-6 );
88   len = dom1.arc_length( p2.to_array(), p1.to_array(), err );
89   ASSERT_NO_ERROR(err);
90   CPPUNIT_ASSERT_DOUBLES_EQUAL( l1 - l2, len, 1e-6 );
91 }
92 
test_position_from_length()93 void LineDomainTest::test_position_from_length()
94 {
95   MsqPrintError err(std::cerr);
96 
97   const Vector3D base1(1,1,1);
98   const Vector3D dir1(-1,-2,-3);
99   LineDomain dom1( base1, dir1 );
100   const Vector3D unit = dir1 / dir1.length();
101   double l1 = 1.5;
102   Vector3D result;
103   dom1.position_from_length( base1.to_array(), l1, result.to_array(), err );
104   ASSERT_NO_ERROR(err);
105   CPPUNIT_ASSERT_VECTORS_EQUAL( base1 + l1 * unit, result, 1e-6 );
106 
107   double l2 = 3.333;
108   Vector3D result2;
109   dom1.position_from_length( result.to_array(), l2, result2.to_array(), err );
110   ASSERT_NO_ERROR(err);
111   CPPUNIT_ASSERT_VECTORS_EQUAL( result + l2 * unit, result2, 1e-6 );
112 }
113 
114 } // namespace MBMesquite
115