DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_BoxGeometry.cpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #include "DTK_BoxGeometry.hpp"
42 #include "DTK_BoxGeometryImpl.hpp"
43 #include "DTK_DBC.hpp"
44 
45 namespace DataTransferKit
46 {
47 //---------------------------------------------------------------------------//
52 {
53  this->b_entity_impl = Teuchos::rcp( new BoxGeometryImpl() );
54 }
55 
56 //---------------------------------------------------------------------------//
72 BoxGeometry::BoxGeometry( const EntityId global_id, const int owner_rank,
73  const int block_id, const double x_min,
74  const double y_min, const double z_min,
75  const double x_max, const double y_max,
76  const double z_max )
77 {
78  this->b_entity_impl = Teuchos::rcp(
79  new BoxGeometryImpl( global_id, owner_rank, block_id, x_min, y_min,
80  z_min, x_max, y_max, z_max ) );
81 }
82 
83 //---------------------------------------------------------------------------//
89 BoxGeometry::BoxGeometry( const EntityId global_id, const int owner_rank,
90  const int block_id,
91  const Teuchos::Tuple<double, 6> &bounds )
92 {
93  this->b_entity_impl = Teuchos::rcp(
94  new BoxGeometryImpl( global_id, owner_rank, block_id, bounds ) );
95 }
96 
97 //---------------------------------------------------------------------------//
102 
103 //---------------------------------------------------------------------------//
114  const BoxGeometry &box_B )
115 {
116  Teuchos::Tuple<double, 6> bounds_A;
117  box_A.boundingBox( bounds_A );
118  Teuchos::Tuple<double, 6> bounds_B;
119  box_B.boundingBox( bounds_B );
120 
121  return !( ( bounds_A[0] > bounds_B[3] || bounds_A[3] < bounds_B[0] ) ||
122  ( bounds_A[1] > bounds_B[4] || bounds_A[4] < bounds_B[1] ) ||
123  ( bounds_A[2] > bounds_B[5] || bounds_A[5] < bounds_B[2] ) );
124 }
125 
126 //---------------------------------------------------------------------------//
143  const BoxGeometry &box_B,
144  BoxGeometry &box_intersection )
145 {
146  // Check for intersection.
147  if ( !checkForIntersection( box_A, box_B ) )
148  {
149  return false;
150  }
151 
152  Teuchos::Tuple<double, 6> bounds_A;
153  box_A.boundingBox( bounds_A );
154  Teuchos::Tuple<double, 6> bounds_B;
155  box_B.boundingBox( bounds_B );
156 
157  double x_min, y_min, z_min, x_max, y_max, z_max;
158 
159  // Get overlap in X.
160  if ( bounds_A[0] > bounds_B[0] )
161  {
162  x_min = bounds_A[0];
163  }
164  else
165  {
166  x_min = bounds_B[0];
167  }
168  if ( bounds_A[3] > bounds_B[3] )
169  {
170  x_max = bounds_B[3];
171  }
172  else
173  {
174  x_max = bounds_A[3];
175  }
176 
177  // Get overlap in Y.
178  if ( bounds_A[1] > bounds_B[1] )
179  {
180  y_min = bounds_A[1];
181  }
182  else
183  {
184  y_min = bounds_B[1];
185  }
186  if ( bounds_A[4] > bounds_B[4] )
187  {
188  y_max = bounds_B[4];
189  }
190  else
191  {
192  y_max = bounds_A[4];
193  }
194 
195  // Get overlap in Z.
196  if ( bounds_A[2] > bounds_B[2] )
197  {
198  z_min = bounds_A[2];
199  }
200  else
201  {
202  z_min = bounds_B[2];
203  }
204  if ( bounds_A[5] > bounds_B[5] )
205  {
206  z_max = bounds_B[5];
207  }
208  else
209  {
210  z_max = bounds_A[5];
211  }
212 
213  box_intersection = BoxGeometry( dtk_invalid_entity_id, box_A.ownerRank(), 0,
214  x_min, y_min, z_min, x_max, y_max, z_max );
215  return true;
216 }
217 
218 //---------------------------------------------------------------------------//
231  const BoxGeometry &box_B, BoxGeometry &box_union )
232 {
233  Teuchos::Tuple<double, 6> bounds_A;
234  box_A.boundingBox( bounds_A );
235  Teuchos::Tuple<double, 6> bounds_B;
236  box_B.boundingBox( bounds_B );
237 
238  double x_min, y_min, z_min, x_max, y_max, z_max;
239 
240  // Get overlap in X.
241  if ( bounds_A[0] < bounds_B[0] )
242  {
243  x_min = bounds_A[0];
244  }
245  else
246  {
247  x_min = bounds_B[0];
248  }
249  if ( bounds_A[3] < bounds_B[3] )
250  {
251  x_max = bounds_B[3];
252  }
253  else
254  {
255  x_max = bounds_A[3];
256  }
257 
258  // Get overlap in Y.
259  if ( bounds_A[1] < bounds_B[1] )
260  {
261  y_min = bounds_A[1];
262  }
263  else
264  {
265  y_min = bounds_B[1];
266  }
267  if ( bounds_A[4] < bounds_B[4] )
268  {
269  y_max = bounds_B[4];
270  }
271  else
272  {
273  y_max = bounds_A[4];
274  }
275 
276  // Get overlap in Z.
277  if ( bounds_A[2] < bounds_B[2] )
278  {
279  z_min = bounds_A[2];
280  }
281  else
282  {
283  z_min = bounds_B[2];
284  }
285  if ( bounds_A[5] < bounds_B[5] )
286  {
287  z_max = bounds_B[5];
288  }
289  else
290  {
291  z_max = bounds_A[5];
292  }
293 
294  box_union = BoxGeometry( dtk_invalid_entity_id, box_A.ownerRank(), 0, x_min,
295  y_min, z_min, x_max, y_max, z_max );
296 }
297 
298 //---------------------------------------------------------------------------//
303 {
304  *this = *this + rhs;
305  return *this;
306 }
307 
308 //---------------------------------------------------------------------------//
313 BoxGeometry operator+( const BoxGeometry &box_1, const BoxGeometry &box_2 )
314 {
315  BoxGeometry union_box;
316  BoxGeometry::uniteBoxes( box_1, box_2, union_box );
317  return union_box;
318 }
319 
320 //---------------------------------------------------------------------------//
326 std::ostream &operator<<( std::ostream &os,
328 {
329  Teuchos::Tuple<double, 6> bounds;
330  b.boundingBox( bounds );
331 
332  os << "BoxGeometry: d_global_id=" << b.id()
333  << ",d_owner_rank=" << b.ownerRank() << ",d_x_min=" << bounds[0]
334  << ",d_y_min=" << bounds[1] << ",d_z_min=" << bounds[2]
335  << ",d_x_max=" << bounds[3] << ",d_y_max=" << bounds[4]
336  << ",d_z_max=" << bounds[5];
337 
338  return os;
339 }
340 
341 //---------------------------------------------------------------------------//
347 double BoxGeometry::measure() const
348 {
349  return Teuchos::rcp_dynamic_cast<BoxGeometryImpl>( this->b_entity_impl )
350  ->measure();
351 }
352 
353 //---------------------------------------------------------------------------//
359 void BoxGeometry::centroid( const Teuchos::ArrayView<double> &centroid ) const
360 {
361  Teuchos::rcp_dynamic_cast<BoxGeometryImpl>( this->b_entity_impl )
362  ->centroid( centroid );
363 }
364 
365 //---------------------------------------------------------------------------//
370  const Teuchos::ArrayView<const double> &point,
371  const Teuchos::ArrayView<double> &reference_point ) const
372 {
373  return Teuchos::rcp_dynamic_cast<BoxGeometryImpl>( this->b_entity_impl )
374  ->mapToReferenceFrame( point, reference_point );
375 }
376 
377 //---------------------------------------------------------------------------//
383  const double tolerance,
384  const Teuchos::ArrayView<const double> &reference_point ) const
385 {
386  return Teuchos::rcp_dynamic_cast<BoxGeometryImpl>( this->b_entity_impl )
387  ->checkPointInclusion( tolerance, reference_point );
388 }
389 
390 //---------------------------------------------------------------------------//
395  const Teuchos::ArrayView<const double> &reference_point,
396  const Teuchos::ArrayView<double> &point ) const
397 {
398  Teuchos::rcp_dynamic_cast<BoxGeometryImpl>( this->b_entity_impl )
399  ->mapToPhysicalFrame( reference_point, point );
400 }
401 
402 //---------------------------------------------------------------------------//
403 
404 } // end namespace DataTransferKit
405 
406 //---------------------------------------------------------------------------//
407 // end DTK_BoxGeometry.cpp
408 //---------------------------------------------------------------------------//
static const EntityId dtk_invalid_entity_id
Invalid entity id.
Definition: DTK_Types.hpp:53
static void uniteBoxes(const BoxGeometry &box_A, const BoxGeometry &box_B, BoxGeometry &box_union)
Static function for box union.
void boundingBox(Teuchos::Tuple< double, 6 > &bounds) const
Return the Cartesian bounding box around an entity.
Definition: DTK_Entity.cpp:114
bool checkPointInclusion(const double tolerance, const Teuchos::ArrayView< const double > &reference_point) const override
Determine if a reference point is in the parameterized space of an entity.
BoxGeometry operator+(const BoxGeometry &box_1, const BoxGeometry &box_2)
Addition operator. Adding two boxes together is the same as computing their union.
int ownerRank() const
Get the parallel rank that owns the entity.
Definition: DTK_Entity.cpp:90
void centroid(const Teuchos::ArrayView< double > &centroid) const override
Get the centroid of the box.
Axis-aligned Cartesian box container.
std::ostream & operator<<(std::ostream &os, const DataTransferKit::BoxGeometry &b)
Print the box description to an ostream.
Assertions and Design-by-Contract for error handling.
static bool checkForIntersection(const BoxGeometry &box_A, const BoxGeometry &box_B)
Static function for box intersection test.
BoxGeometry()
Default constructor.
BoxGeometry implementation.
unsigned long int EntityId
Entity id type.
Definition: DTK_Types.hpp:50
Axis-aligned Cartesian box container implementation.
BoxGeometry & operator+=(const BoxGeometry &rhs)
Compound assignment overload.
bool mapToReferenceFrame(const Teuchos::ArrayView< const double > &point, const Teuchos::ArrayView< double > &reference_point) const override
Map a point to the reference space of an entity. Return the.
void mapToPhysicalFrame(const Teuchos::ArrayView< const double > &reference_point, const Teuchos::ArrayView< double > &point) const override
Map a reference point to the physical space of an entity.
static bool intersectBoxes(const BoxGeometry &box_A, const BoxGeometry &box_B, BoxGeometry &box_intersection)
Static function for box intersection. Return false if they do not intersect.
DTK_BasicEntitySet.cpp.
BoxGeometry declaration.
double measure() const override
Compute the measure of the box.
EntityId id() const
Client interface.
Definition: DTK_Entity.cpp:82