48 #ifndef MUI_GEOMETRY_H
49 #define MUI_GEOMETRY_H
53 #include "../config.h"
54 #include "../storage/stream.h"
55 #include "../storage/stream_string.h"
68 template<
typename CONFIG>
class shape;
69 template<
typename CONFIG>
class any_shape;
70 template<
typename CONFIG>
class box;
73 template<
typename CONFIG>
91 template<typename CONFIG>
96 : content(rhs.content ? rhs.content->
clone() : 0) {}
104 explicit operator bool() const noexcept {
return static_cast<bool>(content); }
105 bool empty() const noexcept {
return !
static_cast<bool>(content); }
115 stream << static_cast<std::int8_t>(obj.
type());
116 if( !obj.
empty() ) obj.
get().serialize(stream);
124 auto tmp = shape_factory<CONFIG>(
static_cast<shape_type>(tp));
125 tmp->deserialize(stream);
126 obj.content.swap(tmp);
132 std::unique_ptr<shape<CONFIG> > content;
136 template<
typename CONFIG>
138 typedef typename CONFIG::point_type coordinate_type;
139 typedef typename CONFIG::REAL REAL;
142 point(
const coordinate_type& c__) : center(c__) {}
154 coordinate_type center;
157 template<
typename CONFIG>
159 typedef typename CONFIG::point_type coordinate_type;
160 typedef typename CONFIG::REAL REAL;
163 sphere(
const coordinate_type& c__, REAL r__) : center(c__), radius(r__) {}
183 coordinate_type center;
187 template<
typename CONFIG>
189 typedef typename CONFIG::point_type coordinate_type;
190 typedef typename CONFIG::REAL REAL;
193 box(
const coordinate_type& p1,
const coordinate_type& p2){
196 for(
uint i=0; i<CONFIG::D; ++i ){
223 template<
typename CONFIG>
225 typedef typename CONFIG::point_type coordinate_type;
226 typedef typename CONFIG::REAL REAL;
260 default: ptr = 0;
break;
262 return std::unique_ptr<shape<CONFIG> >(ptr);
266 #define CASE(LHS, RHS) case shape_type::RHS : \
267 return collide(static_cast<const LHS<CONFIG>&>(lhs), static_cast<const RHS<CONFIG>&>(rhs))
268 template<
typename CONFIG>
bool collide_impl_(
const shape<CONFIG>& lhs,
const shape<CONFIG>& rhs)
274 auto& obj =
static_cast<const or_set<CONFIG>&
>(lhs);
282 default:
return true;
288 default:
return true;
293 default:
return true;
303 return static_cast<std::int8_t
>(lhs.
type()) <=
static_cast<std::int8_t
>(rhs.
type()) ?
304 collide_impl_(lhs,rhs):
305 collide_impl_(rhs,lhs);
316 if( lhs.
empty() )
return true;
322 if( rhs.
empty() )
return true;
339 for(
uint i=0; i<CONFIG::D; ++i )
351 template<
typename REAL>
352 REAL minimum_of_quadratic(REAL b, REAL c, REAL x0, REAL x1 )
356 return std::max<REAL>( ( x0 <= p0 && p0 <= x1 )?
358 std::min(x0*x0+b*x0, x1*x1+b*x1) + c, 0.0 );
364 auto dist = minimum_of_quadratic(-(a+a),a*a,rhs.
get_min()[0],rhs.
get_max()[0]);
365 for(
uint i=1; i<CONFIG::D; ++i ){
367 dist += minimum_of_quadratic(-(a+a),a*a,rhs.
get_min()[i],rhs.
get_max()[i]);
374 for(
uint i=0; i<CONFIG::D; ++i ) {
383 if( content )
return content->bbox();
386 for(
uint i = 0; i<CONFIG::D; ++i ) {
387 bx.
get_min()[i] = -std::numeric_limits<typename CONFIG::REAL>::infinity();
388 bx.
get_max()[i] = std::numeric_limits<typename CONFIG::REAL>::infinity();
402 for(
uint i=0; i<CONFIG::D; ++i ){
403 bx.
get_min()[i] = center[i]-radius;
404 bx.
get_max()[i] = center[i]+radius;
409 template<
typename CONFIG>
418 for(
uint i=0; i<CONFIG::D; ++i ){
Definition: geometry.h:92
bool empty() const noexcept
Definition: geometry.h:105
friend ostream & operator<<(ostream &stream, const any_shape &obj)
Definition: geometry.h:114
any_shape(const any_shape &rhs)
Definition: geometry.h:95
void swap(any_shape &rhs) noexcept
Definition: geometry.h:106
const shape< CONFIG > & get() const
Definition: geometry.h:109
shape< CONFIG > & get()
Definition: geometry.h:108
any_shape & operator=(any_shape rhs)
Definition: geometry.h:99
friend istream & operator>>(istream &stream, any_shape &obj)
Definition: geometry.h:119
box< CONFIG > bbox() const
Definition: geometry.h:381
shape_type type() const noexcept
Definition: geometry.h:111
any_shape(any_shape &&) noexcept=default
Definition: geometry.h:188
void deserialize(istream &stream)
Definition: geometry.h:215
coordinate_type & get_max()
Definition: geometry.h:205
box(const coordinate_type &p1, const coordinate_type &p2)
Definition: geometry.h:193
coordinate_type get_min() const
Definition: geometry.h:202
shape_type type() const noexcept
Definition: geometry.h:208
coordinate_type & get_min()
Definition: geometry.h:203
box< CONFIG > bbox() const
Definition: geometry.h:410
shape< CONFIG > * clone() const
Definition: geometry.h:207
coordinate_type get_max() const
Definition: geometry.h:204
void serialize(ostream &stream) const
Definition: geometry.h:211
Definition: geometry.h:224
const any_shape< CONFIG > & left() const
Definition: geometry.h:231
shape_type type() const noexcept
Definition: geometry.h:237
shape< CONFIG > * clone() const
Definition: geometry.h:236
any_shape< CONFIG > & right()
Definition: geometry.h:234
any_shape< CONFIG > & left()
Definition: geometry.h:232
box< CONFIG > bbox() const
Definition: geometry.h:415
const any_shape< CONFIG > & right() const
Definition: geometry.h:233
void deserialize(istream &stream)
Definition: geometry.h:244
void serialize(ostream &stream) const
Definition: geometry.h:240
or_set(any_shape< CONFIG > obj1, any_shape< CONFIG > &obj2)
Definition: geometry.h:229
Definition: geometry.h:137
coordinate_type & get_center()
Definition: geometry.h:144
point(const coordinate_type &c__)
Definition: geometry.h:142
shape< CONFIG > * clone() const
Definition: geometry.h:147
box< CONFIG > bbox() const
Definition: geometry.h:394
void deserialize(istream &stream)
Definition: geometry.h:152
shape_type type() const noexcept
Definition: geometry.h:148
void serialize(ostream &stream) const
Definition: geometry.h:151
coordinate_type get_center() const
Definition: geometry.h:145
Definition: geometry.h:74
virtual shape_type type() const noexcept=0
virtual shape * clone() const =0
virtual void serialize(ostream &stream) const =0
shape(shape &&) noexcept=default
virtual box< CONFIG > bbox() const =0
virtual void deserialize(istream &stream)=0
Definition: geometry.h:158
box< CONFIG > bbox() const
Definition: geometry.h:399
coordinate_type get_center() const
Definition: geometry.h:166
shape_type type() const noexcept
Definition: geometry.h:171
REAL & get_radius()
Definition: geometry.h:167
coordinate_type & get_center()
Definition: geometry.h:165
void serialize(ostream &stream) const
Definition: geometry.h:174
void deserialize(istream &stream)
Definition: geometry.h:178
shape< CONFIG > * clone() const
Definition: geometry.h:170
sphere(const coordinate_type &c__, REAL r__)
Definition: geometry.h:163
REAL get_radius() const
Definition: geometry.h:168
#define CASE(LHS, RHS)
Definition: geometry.h:266
u u u u u u min
Definition: dim.h:289
ostream & operator<<(ostream &stream, const shape< CONFIG > &obj)
Definition: geometry.h:425
std::unique_ptr< shape< CONFIG > > shape_factory(shape_type)
Definition: geometry.h:252
shape_type
Definition: geometry.h:60
istream & operator>>(istream &stream, shape< CONFIG > &obj)
Definition: geometry.h:431
bool collide(const shape< CONFIG > &lhs, const shape< CONFIG > &rhs)
Definition: geometry.h:301
any_shape< CONFIG > get_universe_set()
Definition: geometry.h:134
or_set< CONFIG > operator|(any_shape< CONFIG > lhs, any_shape< CONFIG > rhs)
Definition: geometry.h:437
SCALAR max(vexpr< E, SCALAR, D > const &u)
Definition: point.h:350
unsigned int uint
Definition: util.h:94
SCALAR normsq(vexpr< E, SCALAR, D > const &u)
Definition: point.h:380
SCALAR min(vexpr< E, SCALAR, D > const &u)
Definition: point.h:356