RINGMesh  Version 5.0.0
A programming library for geological model meshes
box.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #pragma once
37 
38 #include <ringmesh/basic/common.h>
39 
45 namespace RINGMesh
46 {
47  template < index_t DIMENSION >
48  class Box
49  {
50  public:
51  bool initialized() const
52  {
53  return initialized_;
54  }
55 
56  void clear()
57  {
58  initialized_ = false;
59  }
60 
61  const vecn< DIMENSION >& min() const
62  {
63  return min_;
64  }
65 
66  const vecn< DIMENSION >& max() const
67  {
68  return max_;
69  }
70 
72  {
73  return 0.5 * ( min() + max() );
74  }
75 
77  {
78  return max() - min();
79  }
80 
81  void add_point( const vecn< DIMENSION >& p );
82 
83  void add_box( const Box< DIMENSION >& b )
84  {
85  if( b.initialized() )
86  {
87  add_point( b.min() );
88  add_point( b.max() );
89  }
90  }
91 
92  bool bboxes_overlap( const Box< DIMENSION >& B ) const
93  {
94  for( auto c : range( DIMENSION ) )
95  {
96  if( max()[c] < B.min()[c] )
97  {
98  return false;
99  }
100  if( min()[c] > B.max()[c] )
101  {
102  return false;
103  }
104  }
105  return true;
106  }
107 
109  {
110  Box< DIMENSION > result{ *this };
111  result.add_box( B );
112  return result;
113  }
114 
122  std::tuple< bool, Box< DIMENSION > > bbox_intersection(
123  const Box< DIMENSION >& B ) const
124  {
125  if( !bboxes_overlap( B ) )
126  {
127  return std::make_tuple( false, Box() );
128  }
129 
130  Box< DIMENSION > result;
131  vecn< DIMENSION > minimal_max;
132  vecn< DIMENSION > maximal_min;
133  for( auto c : range( DIMENSION ) )
134  {
135  minimal_max[c] = std::min( this->max()[c], B.max()[c] );
136  maximal_min[c] = std::max( this->min()[c], B.min()[c] );
137  }
138  result.add_point( maximal_min );
139  result.add_point( minimal_max );
140  return std::make_tuple( true, result );
141  }
142 
143  bool contains( const vecn< DIMENSION >& b ) const
144  {
145  for( auto c : range( DIMENSION ) )
146  {
147  if( b[c] < min()[c] )
148  {
149  return false;
150  }
151  if( b[c] > max()[c] )
152  {
153  return false;
154  }
155  }
156  return true;
157  }
158 
159  double distance_to_center( const vecn< DIMENSION >& p ) const;
160 
161  double signed_distance( const vecn< DIMENSION >& p ) const;
162 
163  private:
164  bool initialized_{ false };
167  };
168  ALIAS_2D_AND_3D( Box );
169 
170 } // namespace RINGMesh
GEO::vecng< DIMENSION, double > vecn
Definition: types.h:74
bool contains(const vecn< DIMENSION > &b) const
Definition: box.h:143
std::tuple< bool, Box< DIMENSION > > bbox_intersection(const Box< DIMENSION > &B) const
Definition: box.h:122
bool bboxes_overlap(const Box< DIMENSION > &B) const
Definition: box.h:92
ALIAS_2D_AND_3D(Box)
vecn< DIMENSION > max_
Definition: box.h:166
Box< DIMENSION > bbox_union(const Box< DIMENSION > &B) const
Definition: box.h:108
void add_box(const Box< DIMENSION > &b)
Definition: box.h:83
vecn< DIMENSION > center() const
Definition: box.h:71
bool initialized_
Definition: box.h:164
void add_point(const vecn< DIMENSION > &p)
Definition: box.cpp:57
void clear()
Definition: box.h:56
const vecn< DIMENSION > & max() const
Definition: box.h:66
vecn< DIMENSION > min_
Definition: box.h:165
const vecn< DIMENSION > & min() const
Definition: box.h:61
vecn< DIMENSION > diagonal() const
Definition: box.h:76
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
bool initialized() const
Definition: box.h:51
double distance_to_center(const vecn< DIMENSION > &p) const
Definition: box.cpp:108
double signed_distance(const vecn< DIMENSION > &p) const
Definition: box.cpp:76