NEST  2.6.0,not_revisioned_source_dir@0
mask_impl.h
Go to the documentation of this file.
1 /*
2  * mask_impl.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef MASK_IMPL_H
24 #define MASK_IMPL_H
25 
26 #include "mask.h"
27 
28 namespace nest
29 {
30 
31  template<int D>
33  {
34  const Mask * other_d = dynamic_cast<const Mask*>(&other);
35  if (other_d==0) {
36  throw BadProperty("Masks must have same number of dimensions.");
37  }
38  return new IntersectionMask<D>(*this,*other_d);
39  }
40 
41  template<int D>
43  {
44  const Mask * other_d = dynamic_cast<const Mask*>(&other);
45  if (other_d==0) {
46  throw BadProperty("Masks must have same number of dimensions.");
47  }
48  return new UnionMask<D>(*this,*other_d);
49  }
50 
51  template<int D>
53  {
54  const Mask * other_d = dynamic_cast<const Mask*>(&other);
55  if (other_d==0) {
56  throw BadProperty("Masks must have same number of dimensions.");
57  }
58  return new DifferenceMask<D>(*this,*other_d);
59  }
60 
61  template<int D>
62  bool Mask<D>::inside(const std::vector<double_t> &pt) const
63  {
64  return inside(Position<D>(pt));
65  }
66 
67  template<int D>
68  bool Mask<D>::outside(const Box<D> &b) const
69  {
70  Box<D> bb = get_bbox();
71  for(int i=0;i<D;++i) {
72  if ((b.upper_right[i]<bb.lower_left[i]) || (b.lower_left[i]>bb.upper_right[i]))
73  return true;
74  }
75  return false;
76  }
77 
78  template<int D>
79  bool BoxMask<D>::inside(const Position<D> &p) const
80  {
81  return (p>=lower_left_) && (p<=upper_right_);
82  }
83 
84  template<int D>
85  bool BoxMask<D>::inside(const Box<D> &b) const
86  {
87  return (b.lower_left>=lower_left_) && (b.upper_right<=upper_right_);
88  }
89 
90  template<int D>
91  bool BoxMask<D>::outside(const Box<D> &b) const
92  {
93  for(int i=0;i<D;++i) {
94  if ((b.upper_right[i]<lower_left_[i]) || (b.lower_left[i]>upper_right_[i]))
95  return true;
96  }
97  return false;
98  }
99 
100  template<int D>
102  {
103  return Box<D>(lower_left_, upper_right_);
104  }
105 
106  template<int D>
108  {
109  return new BoxMask(*this);
110  }
111 
112  template<int D>
114  {
116  DictionaryDatum maskd(new Dictionary);
117  def<DictionaryDatum>(d, get_name(), maskd);
118  def<std::vector<double_t> >(maskd, names::lower_left, lower_left_);
119  def<std::vector<double_t> >(maskd, names::upper_right, upper_right_);
120  return d;
121  }
122 
123  template<int D>
124  bool BallMask<D>::inside(const Position<D> &p) const
125  {
126  return (p-center_).length() <= radius_;
127  }
128 
129  template<>
130  bool BallMask<2>::inside(const Box<2> &b) const
131  {
133 
134  // Test if all corners are inside circle
135 
136  if (!inside(p)) return false; // (0,0)
137  p[0] = b.upper_right[0];
138  if (!inside(p)) return false; // (0,1)
139  p[1] = b.upper_right[1];
140  if (!inside(p)) return false; // (1,1)
141  p[0] = b.lower_left[0];
142  if (!inside(p)) return false; // (1,0)
143 
144  return true;
145  }
146 
147  template<>
148  bool BallMask<3>::inside(const Box<3> &b) const
149  {
151 
152  // Test if all corners are inside sphere
153 
154  if (!inside(p)) return false; // (0,0,0)
155  p[0] = b.upper_right[0];
156  if (!inside(p)) return false; // (0,0,1)
157  p[1] = b.upper_right[1];
158  if (!inside(p)) return false; // (0,1,1)
159  p[0] = b.lower_left[0];
160  if (!inside(p)) return false; // (0,1,0)
161  p[2] = b.upper_right[2];
162  if (!inside(p)) return false; // (1,1,0)
163  p[0] = b.upper_right[0];
164  if (!inside(p)) return false; // (1,1,1)
165  p[1] = b.lower_left[1];
166  if (!inside(p)) return false; // (1,0,1)
167  p[0] = b.lower_left[0];
168  if (!inside(p)) return false; // (1,0,0)
169 
170  return true;
171  }
172 
173  template<int D>
174  bool BallMask<D>::outside(const Box<D> &b) const
175  {
176  // Currently only checks if the box is outside the bounding box of
177  // the ball. This could be made more refined.
178  for (int i=0; i<D; ++i) {
179  if ((b.upper_right[i] < center_[i]-radius_) ||
180  (b.lower_left[i] > center_[i]+radius_))
181  return true;
182  }
183  return false;
184  }
185 
186  template<int D>
188  {
189  Box<D> bb(center_, center_);
190  for (int i=0; i<D; ++i) {
191  bb.lower_left[i] -= radius_;
192  bb.upper_right[i] += radius_;
193  }
194  return bb;
195  }
196 
197  template<int D>
199  {
200  return new BallMask(*this);
201  }
202 
203  template<int D>
205  {
207  DictionaryDatum maskd(new Dictionary);
208  def<DictionaryDatum>(d, get_name(), maskd);
209  def<double_t>(maskd, names::radius, radius_);
210  def<std::vector<double_t> >(maskd, names::anchor, center_);
211  return d;
212  }
213 
214  template<int D>
216  {
217  return mask1_->inside(p) && mask2_->inside(p);
218  }
219 
220  template<int D>
222  {
223  return mask1_->inside(b) && mask2_->inside(b);
224  }
225 
226  template<int D>
228  {
229  return mask1_->outside(b) || mask2_->outside(b);
230  }
231 
232  template<int D>
234  {
235  Box<D> bb = mask1_->get_bbox();
236  Box<D> bb2 = mask2_->get_bbox();
237  for (int i=0; i<D; ++i) {
238  if (bb2.lower_left[i] > bb.lower_left[i])
239  bb.lower_left[i] = bb2.lower_left[i];
240  if (bb2.upper_right[i] < bb.upper_right[i])
241  bb.upper_right[i] = bb2.upper_right[i];
242  }
243  return bb;
244  }
245 
246  template<int D>
248  {
249  return new IntersectionMask(*this);
250  }
251 
252  template<int D>
253  bool UnionMask<D>::inside(const Position<D> &p) const
254  {
255  return mask1_->inside(p) || mask2_->inside(p);
256  }
257 
258  template<int D>
259  bool UnionMask<D>::inside(const Box<D> &b) const
260  {
261  return mask1_->inside(b) || mask2_->inside(b);
262  }
263 
264  template<int D>
265  bool UnionMask<D>::outside(const Box<D> &b) const
266  {
267  return mask1_->outside(b) && mask2_->outside(b);
268  }
269 
270  template<int D>
272  {
273  Box<D> bb = mask1_->get_bbox();
274  Box<D> bb2 = mask2_->get_bbox();
275  for (int i=0; i<D; ++i) {
276  if (bb2.lower_left[i] < bb.lower_left[i])
277  bb.lower_left[i] = bb2.lower_left[i];
278  if (bb2.upper_right[i] > bb.upper_right[i])
279  bb.upper_right[i] = bb2.upper_right[i];
280  }
281  return bb;
282  }
283 
284  template<int D>
286  {
287  return new UnionMask(*this);
288  }
289 
290  template<int D>
292  {
293  return mask1_->inside(p) && !mask2_->inside(p);
294  }
295 
296  template<int D>
297  bool DifferenceMask<D>::inside(const Box<D> &b) const
298  {
299  return mask1_->inside(b) && mask2_->outside(b);
300  }
301 
302  template<int D>
304  {
305  return mask1_->outside(b) || mask2_->inside(b);
306  }
307 
308  template<int D>
310  {
311  return mask1_->get_bbox();
312  }
313 
314  template<int D>
316  {
317  return new DifferenceMask(*this);
318  }
319 
320  template<int D>
322  {
323  return m_->inside(-p);
324  }
325 
326  template<int D>
327  bool ConverseMask<D>::inside(const Box<D> &b) const
328  {
329  return m_->inside(Box<D>(-b.upper_right,-b.lower_left));
330  }
331 
332  template<int D>
333  bool ConverseMask<D>::outside(const Box<D> &b) const
334  {
335  return m_->outside(Box<D>(-b.upper_right,-b.lower_left));
336  }
337 
338  template<int D>
340  {
341  Box<D> bb = m_->get_bbox();
342  return Box<D>(-bb.upper_right, -bb.lower_left);
343  }
344 
345  template<int D>
347  {
348  return new ConverseMask(*this);
349  }
350 
351  template<int D>
353  {
354  return m_->inside(p-anchor_);
355  }
356 
357  template<int D>
358  bool AnchoredMask<D>::inside(const Box<D> &b) const
359  {
360  return m_->inside(Box<D>(b.lower_left-anchor_,b.upper_right-anchor_));
361  }
362 
363  template<int D>
364  bool AnchoredMask<D>::outside(const Box<D> &b) const
365  {
366  return m_->outside(Box<D>(b.lower_left-anchor_,b.upper_right-anchor_));
367  }
368 
369  template<int D>
371  {
372  Box<D> bb = m_->get_bbox();
373  return Box<D>(bb.lower_left+anchor_, bb.upper_right+anchor_);
374  }
375 
376  template<int D>
378  {
379  return new AnchoredMask(*this);
380  }
381 
382  template<int D>
384  {
385  DictionaryDatum d = m_->get_dict();
386  def<std::vector<double_t> >(d, names::anchor, anchor_);
387  return d;
388  }
389 
390 } // namespace nest
391 
392 #endif
bool outside(const Box< D > &b) const
Definition: mask_impl.h:91
Mask combining two masks with a minus operation, the difference.
Definition: mask.h:371
virtual bool outside(const Box< D > &b) const
Definition: mask_impl.h:68
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:271
Mask oriented in the opposite direction.
Definition: mask.h:411
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
bool inside(const Position< D > &p) const
Definition: mask_impl.h:253
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:233
Mask shifted by an anchor.
Definition: mask.h:446
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:198
bool inside(const Position< D > &p) const
Definition: mask_impl.h:79
DictionaryDatum get_dict() const
Definition: mask_impl.h:383
const Name lower_left("lower_left")
Definition: topology_names.h:66
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:101
bool inside(const Position< D > &p) const
Definition: mask_impl.h:291
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:187
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:377
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:315
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:107
Mask defining a circular or spherical region.
Definition: mask.h:235
virtual bool inside(const Position< D > &) const =0
bool inside(const Position< D > &p) const
Definition: mask_impl.h:321
Mask defining a box region.
Definition: mask.h:182
const Name radius("radius")
Definition: topology_names.h:68
Abstract base class for masks with given dimension.
Definition: mask.h:90
A class that associates names and tokens.
Definition: dict.h:45
bool inside(const Position< D > &p) const
Definition: mask_impl.h:352
const Name anchor("anchor")
Definition: topology_names.h:50
bool outside(const Box< D > &b) const
Definition: mask_impl.h:174
const Name other("other")
Node type.
Definition: nest_names.h:216
A box is defined by the lower left corner (minimum coordinates) and the upper right corner (maximum c...
Definition: position.h:289
AbstractMask * union_mask(const AbstractMask &other) const
Create the union of this mask with another.
Definition: mask_impl.h:42
bool outside(const Box< D > &b) const
Definition: mask_impl.h:333
bool outside(const Box< D > &b) const
Definition: mask_impl.h:227
Exception to be thrown if a status parameter is incomplete or inconsistent.
Definition: exceptions.h:420
bool outside(const Box< D > &b) const
Definition: mask_impl.h:265
Position< D > upper_right
Definition: position.h:297
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:339
bool outside(const Box< D > &b) const
Definition: mask_impl.h:364
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:346
AbstractMask * minus_mask(const AbstractMask &other) const
Create the difference of this mask and another.
Definition: mask_impl.h:52
const Name b("b")
Specific to Brette & Gerstner 2005 (aeif_cond-*)
Definition: nest_names.h:58
AbstractMask * intersect_mask(const AbstractMask &other) const
Create the intersection of this mask with another.
Definition: mask_impl.h:32
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:285
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:370
Mask combining two masks with a Boolean AND, the intersection.
Definition: mask.h:293
Position< D > lower_left
Definition: position.h:296
bool outside(const Box< D > &b) const
Definition: mask_impl.h:303
Mask combining two masks with a Boolean OR, the sum.
Definition: mask.h:332
Abstract base class for masks with unspecified dimension.
Definition: mask.h:41
bool inside(const Position< D > &p) const
Definition: mask_impl.h:215
Mask< D > * clone() const
Clone method.
Definition: mask_impl.h:247
DictionaryDatum get_dict() const
Definition: mask_impl.h:204
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
const Name upper_right("upper_right")
Definition: topology_names.h:67
bool inside(const Position< D > &p) const
Definition: mask_impl.h:124
Box< D > get_bbox() const
The whole mask is inside (i.e., false everywhere outside) the bounding box.
Definition: mask_impl.h:309
DictionaryDatum get_dict() const
Definition: mask_impl.h:113