RSTT: rstt/SLBM/include/Triangle.h Source File

RSTT 3.1.0 Regional Seismic Travel Time

Triangle.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government retains certain rights in this software.
6 //-
7 //- BSD Open Source License
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37
38 #ifndef Triangle_H
39 #define Triangle_H
40
41 // **** _SYSTEM INCLUDES_ ******************************************************
42 #include <vector>
43 #include <map>
44 #include <cmath>
45 #include <string>
46 #include <iostream>
47 #include <fstream>
48
49
50 using namespace std;
51
52 // **** _LOCAL INCLUDES_ *******************************************************
53
54 #include "SLBMGlobals.h"
55 #include "GridProfile.h"
56 #include "SLBMException.h"
57
58 // **** _BEGIN SLBM NAMESPACE_ **************************************************
59
60 namespace slbm {
61
62 class SLBM_EXP_IMP Triangle
63 {
64
65 public:
66
70 Triangle();
71
72 Triangle(const int& index, GridProfile* loc0,
73 GridProfile* loc1, GridProfile* loc2);
74
78 ~Triangle();
79
83 Triangle(const Triangle &other);
84
88 Triangle& operator=(const Triangle& other);
89
90 int getIndex() { return index; };
91
98 GridProfile* getNode(const int& i) { return nodes[i]; };
99
105 void setNeighbor(const int& i, Triangle* neighbor) { neighbors[i] = neighbor; };
106
112 Triangle* getNeighbor(const int& i) { return neighbors[i]; };
113
125 Triangle* walk(const Location& position, vector<double>& coefficients);
126
136 void findNodeNeighbors(const int& node0, set<int>& neighborNodes);
137
145 int findNeighborIndex(Triangle* neighbor);
146
147 private:
148
149 int index;
150
151 GridProfile* nodes[3];
152
153 Triangle* neighbors[3];
154
155 void findNodeNeighbors(const int& node0,
156 set<int>& neighborNodes,
157 set<Triangle*> visitedTriangles);
158
159 };
160
161 inline Triangle* Triangle::walk(const Location& position, vector<double>& coeff)
162 {
163 int i,j;
164 for (i=0; i<3; i++)
165 {
166 j = (i + 1) % 3;
167 // compute the scalar triple product:
168 // position dot (nodes[j] cross nodes[j+1])
169 coeff[i] = position.scalarTripleProduct(*nodes[j], *nodes[(j+1)%3]);
170 if (coeff[i] > 1e-15)
171 {
172 // positive means that position does not reside in this triangle.
173 // It resides on the other side of edge[i] of this triangle.
174 // Recursively walk into triangle neighbors[j]
175 return neighbors[j]->walk(position, coeff);
176 }
177 }
178
179 // this is the triangle in which position resides.
180 double sum = 0;
181 for (i=0; i<3; i++)
182 {
183 if (coeff[i] > 0.) coeff[i] = 0.;
184 sum += coeff[i];
185 }
186
187 for (i=0; i<3; i++)
188 coeff[i] /= sum;
189
190 return this;
191 }
192
193 inline void Triangle::findNodeNeighbors(const int& node0, set<int>& neighborNodes)
194 {
195 set<Triangle*> visitedTriangles;
196 findNodeNeighbors(node0, neighborNodes, visitedTriangles);
197 }
198
199 inline void Triangle::findNodeNeighbors(const int& node0, set<int>& neighborNodes,
200 set<Triangle*> visitedTriangles)
201 {
202 visitedTriangles.insert(this);
203 // iterate over the 3 nodes of this triangle
204 for (int i=0; i<3; i++)
205 {
206 // if the node we are searching for is one of the corners
207 // of this node, then add the other 2 nodes to neighborNodes
208 // and then recursively visit each of this triangles neighbors
209 // to see they also contain the node being sought.
210 if (nodes[i]->getNodeId() == node0)
211 {
212 for (int j=0; j<3; j++)
213 if (nodes[j]->getNodeId() != node0)
214 neighborNodes.insert(nodes[j]->getNodeId());
215 for (int j=0; j<3; j++)
216 if (visitedTriangles.find(neighbors[j]) == visitedTriangles.end())
217 neighbors[j]->findNodeNeighbors(node0, neighborNodes, visitedTriangles);
218 }
219 }
220 }
221
222 inline int Triangle::findNeighborIndex(Triangle* neighbor)
223 {
224 for (int i=0; i<3; i++)
225 if (neighbor->getNeighbor(i) == this)
226 return i;
227 return -1;
228 }
229
230 } // end slbm namespace
231
232 #endif // Grid_H
slbm::Location
The Location Class manages a single point in/on the Earth, which is described by the GRS80 ellipsoid.
Definition: Location.h:78
SLBMException.h
slbm::Triangle::Triangle
Triangle(const int &index, GridProfile *loc0, GridProfile *loc1, GridProfile *loc2)
slbm::Triangle::getNode
GridProfile * getNode(const int &i)
Retrieve a handle to one of the 3 nodes that defines the corners of this triangle.
Definition: Triangle.h:98
slbm::Triangle::~Triangle
~Triangle()
Default destructor.
SLBMGlobals.h
slbm::GridProfile
Manages all information related to a single node in a Grid object.
Definition: GridProfile.h:71
slbm::Triangle::getNeighbor
Triangle * getNeighbor(const int &i)
Definition: Triangle.h:112
slbm::Triangle::Triangle
Triangle()
Default constructor.
slbm::Triangle::getIndex
int getIndex()
Definition: Triangle.h:90
slbm::Location::scalarTripleProduct
double scalarTripleProduct(const Location &loc1, const Location &loc2) const
Definition: Location.h:727
GridProfile.h
SLBM_EXP_IMP
#define SLBM_EXP_IMP
Definition: SLBMGlobals.h:180
slbm::Triangle::operator=
Triangle & operator=(const Triangle &other)
Equal operator.
slbm::Triangle::Triangle
Triangle(const Triangle &other)
Copy constructor.
slbm
Definition: CrustalProfile.h:59
slbm::Triangle
Definition: Triangle.h:63
slbm::Triangle::setNeighbor
void setNeighbor(const int &i, Triangle *neighbor)
Definition: Triangle.h:105