ug4
shapes_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__shapes_impl__
34 #define __H__UG__shapes_impl__
35 
36 #include "shapes.h"
37 
38 namespace ug{
39 
40 template <class vector_t>
41 bool Sphere<vector_t>::contains_point(const vector_t& p) const
42 {
43  return VecDistanceSq(center, p) <= sq(radius);
44 }
45 
46 template <class vector_t>
47 bool Sphere<vector_t>::intersects(const Sphere& sphere) const
48 {
49  return VecDistanceSq(center, sphere.center)
50  <= sq(radius + sphere.radius);
51 }
52 
53 
54 
55 template <class vector_t>
56 AABox<vector_t>::AABox(const vector_t* points, size_t numPoints)
57 {
58  if(numPoints < 1){
59  VecSet(min, 0);
60  VecSet(max, 0);
61  return;
62  }
63 
64  min = max = points[0];
65 
66  for(size_t i = 1; i < numPoints; ++i){
67  const vector_t& p = points[i];
68  for(size_t j = 0; j < vector_t::Size; ++j){
69  min[j] = std::min(min[j], p[j]);
70  max[j] = std::max(max[j], p[j]);
71  }
72  }
73 }
74 
75 template <class vector_t>
76 AABox<vector_t>::AABox(const AABox& b1, const AABox& b2)
77 {
78  for(size_t j = 0; j < vector_t::Size; ++j){
79  min[j] = std::min(b1.min[j], b2.min[j]);
80  max[j] = std::max(b1.max[j], b2.max[j]);
81  }
82 }
83 
84 template <class vector_t>
86 {
87  vector_t vrad;
88  SetVec(vrad, s.radius);
89  VecSubtract(min, s.center, vrad);
90  VecAdd(max, s.center, vrad);
91 }
92 
93 template <class vector_t>
94 AABox<vector_t>::AABox(const AABox& b, const vector_t& v)
95 {
96  for(size_t j = 0; j < vector_t::Size; ++j){
97  min[j] = std::min(b.min[j], v[j]);
98  max[j] = std::max(b.max[j], v[j]);
99  }
100 }
101 
102 template <class vector_t>
103 vector_t AABox<vector_t>::center() const
104 {
105  vector_t v;
106  VecAdd(v, max, min);
107  VecScale(v, v, 0.5);
108  return v;
109 }
110 
111 template <class vector_t>
113 {
114  vector_t v;
115  VecSubtract(v, max, min);
116  return v;
117 }
118 
119 template <class vector_t>
120 bool AABox<vector_t>::contains_point(const vector_t& point) const
121 {
122  return BoxBoundProbe(point, min, max);
123 }
124 
125 template <class vector_t>
126 bool AABox<vector_t>::overlaps_line(const vector_t& point1, const vector_t& point2) const
127 {
128  vector_t lmin, lmax;
129  lmin = lmax = point1;
130  for (size_t i = 0; i < vector_t::Size; i++) {
131  lmin[i] = std::min(lmin[i], point2[i]);
132  lmax[i] = std::max(lmax[i], point2[i]);
133  }
134  return BoxBoxIntersection(lmin, lmax, min, max);
135 }
136 
137 }// end of namespace
138 
139 #endif
parameterString p
parameterString s
Definition: shapes.h:42
bool intersects(const Sphere &sphere) const
returns true if the specified sphere touches or intersects this sphere.
Definition: shapes_impl.h:47
vector_t center
Definition: shapes.h:54
number radius
Definition: shapes.h:55
bool contains_point(const vector_t &point) const
returns true if the point lies inside or on the bounds of the rectangle
Definition: shapes_impl.h:41
TNumber sq(TNumber val)
returns the square of a value (val*val)
Definition: math_util_impl.hpp:91
bool BoxBoundProbe(const vector_t &v, const vector_t &boxMin, const vector_t &boxMax)
Returns true if the point lies inside or on the boundary of the box.
Definition: math_util_impl.hpp:958
bool BoxBoxIntersection(const vector_t &box1Min, const vector_t &box1Max, const vector_t &box2Min, const vector_t &box2Max)
checks whether two boxes intersect.
Definition: math_util_impl.hpp:901
void VecSet(vector_t &vInOut, typename vector_t::value_type s)
Set each vector component to scalar (componentwise)
Definition: math_vector_functions_common_impl.hpp:539
void VecSubtract(vector_t &vOut, const vector_t &v1, const vector_t &v2)
subtracts v2 from v1 and stores the result in a vOut
Definition: math_vector_functions_common_impl.hpp:226
vector_t::value_type VecDistanceSq(const vector_t &v1, const vector_t &v2)
returns the squared distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:351
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition: math_vector_functions_common_impl.hpp:185
void VecScale(vector_t &vOut, const vector_t &v, typename vector_t::value_type s)
scales a MathVector<N>
Definition: math_vector_functions_common_impl.hpp:252
the ug namespace
Definition: shapes.h:60
AABox()
Definition: shapes.h:61
vector_t min
Definition: shapes.h:88
vector_t max
Definition: shapes.h:89
vector_t center() const
returns the center of the box
Definition: shapes_impl.h:103
bool overlaps_line(const vector_t &point1, const vector_t &point2) const
return true if the given line (segment) and the box overlap
Definition: shapes_impl.h:126
vector_t extension() const
returns the extension (width/height/depth) of the box
Definition: shapes_impl.h:112
bool contains_point(const vector_t &point) const
returns true if the given point lies in the box or on its boundary
Definition: shapes_impl.h:120