SheafSystem  0.0.0.0
chart_point.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #include "SheafSystem/chart_point.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/chart_point_1d.h"
25 #include "SheafSystem/chart_point_2d.h"
26 #include "SheafSystem/chart_point_3d.h"
27 #include "SheafSystem/error_message.h"
28 #include "SheafSystem/std_algorithm.h"
29 #include "SheafSystem/std_iomanip.h"
30 #include "SheafSystem/std_sstream.h"
31 
32 using namespace std;
33 using namespace fiber_bundle; // Workaround for bug in MS C++.
34 
35 // ===========================================================
36 // CHART_POINT FACET
37 // ===========================================================
38 
39 // PUBLIC MEMBER FUNCTIONS
40 
44 {
45  chart_point* result;
46 
47  // Preconditions:
48 
49  require((1 <= xdb) && (xdb < 4));
50 
51  // Body:
52 
53  switch(xdb)
54  {
55  case 1:
56  result = new chart_point_1d();
57  break;
58  case 2:
59  result = new chart_point_2d();
60  break;
61  case 3:
62  result = new chart_point_3d();
63  break;
64  default:
65  post_fatal_error_message("Invalid chart point dimension");
66  break;
67  }
68 
69  // Postconditions:
70 
71  ensure(result != 0);
72 
73  // Exit:
74 
75  return result;
76 }
77 
80 {
81  // Preconditions:
82 
83  // Body:
84 
85  // Nothing to do.
86 
87  // Postconditions:
88 
89  // Exit:
90 
91  return;
92 }
93 
96 chart_id() const
97 {
98  return _chart_id;
99 }
100 
101 void
104 {
105  // Preconditions:
106 
107  // Body:
108 
109  _chart_id = xchart_id;
110 
111  // Postconditions:
112 
113  ensure(chart_id() == xchart_id);
114 
115  // Exit:
116 
117  return;
118 }
119 
120 void
122 put_chart_id(const scoped_index& xchart_id)
123 {
124  // Preconditions:
125 
126  // Body:
127 
128  put_chart_id(xchart_id.hub_pod());
129 
130  // Postconditions:
131 
132  ensure(chart_id() == xchart_id.hub_pod());
133 
134  // Exit:
135 
136  return;
137 }
138 
139 int
141 db() const
142 {
143  int result = -1; // The dimension of the empty set.
144 
145  // Preconditions:
146 
147 
148  // Body:
149 
150  is_abstract();
151 
152  // Postconditions:
153 
154  ensure(result >= -1);
155 
156  // Exit:
157 
158  return result;
159 }
160 
163 local_coord(int xi) const
164 {
165  coord_type result = 0;
166 
167  // Preconditions:
168 
169  require((0 <= xi) && (xi < db()));
170 
171  // Body:
172 
173  is_abstract();
174 
175 
176  // Postconditions:
177 
178  ensure(invariant());
179 
180  // Exit:
181 
182  return result;
183 }
184 
185 void
188 {
189  // Preconditions:
190 
191  require((0 <= xi) && (xi < db()));
192 
193  // Body:
194 
195  is_abstract();
196 
197  // Postconditions:
198 
199  ensure(invariant());
200  ensure(isunordered_or_equals(local_coord(xi), xvalue));
201 
202  // Exit:
203 
204  return;
205 }
206 
210 {
211  coord_type* result = 0;
212 
213  // Preconditions:
214 
215 
216  // Body:
217 
218  is_abstract();
219 
220  // Postconditions:
221 
222  ensure(result != 0);
223 
224  // Exit:
225 
226  return result;
227 }
228 
232 {
233  const coord_type* result = 0;
234 
235  // Preconditions:
236 
237 
238  // Body:
239 
240  is_abstract();
241 
242  // Postconditions:
243 
244  ensure(result != 0);
245 
246  // Exit:
247 
248  return result;
249 }
250 
251 void
253 put_local_coords(const coord_type* xvalues, size_type xvalues_ub)
254 {
255  // Preconditions:
256 
257  require(xvalues != 0);
258  require(xvalues_ub >= db());
259 
260  // Body:
261 
262  for(size_type i=0; i<db(); ++i)
263  {
264  put_local_coord(i, xvalues[i]);
265  }
266 
267  // Postconditions:
268 
269  ensure(invariant());
270  ensure_for_all(i, 0, db(), isunordered_or_equals(local_coord(i), xvalues[i]));
271 
272  // Exit:
273 
274  return;
275 }
276 
277 void
280 {
281  // Preconditions:
282 
283 
284  // Body:
285 
286  for(size_type i=0; i<db(); ++i)
287  {
288  put_local_coord(i, 0.0);
289  }
290 
291  // Postconditions:
292 
293  ensure(invariant());
294  ensure_for_all(i, 0, db(), local_coord(i) == 0.0);
295 
296  // Exit:
297 
298  return;
299 }
300 
301 bool
303 is_valid() const
304 {
305  bool result;
306 
307  // Preconditions:
308 
309  // Body:
310 
311  result = sheaf::is_valid(_chart_id);
312 
313  // Postconditions:
314 
315  // Exit:
316 
317  return result;
318 }
319 
320 void
323 {
324  // Preconditions:
325 
326  // Body:
327 
328  _chart_id = invalid_pod_index();
329 
330  for(int i=0; i<db(); ++i)
331  {
332  put_local_coord(i, 0.0);
333  }
334 
335  // Postconditions:
336 
337  ensure(!is_valid());
338 
339  // Exit:
340 
341  return;
342 }
343 
344 // PROTECTED MEMBER FUNCTIONS
345 
348  : _chart_id(invalid_pod_index())
349 {
350  // Preconditions:
351 
352  // Body:
353 
354  // Does nothing.
355 
356  // Postconditions:
357 
358  // Exit:
359 
360  return;
361 }
362 
363 // PRIVATE MEMBER FUNCTIONS
364 
366 chart_point(const chart_point& xother)
367 {
368  // Preconditions:
369 
370  // Body:
371 
372  _chart_id = xother._chart_id;
373 
374  // Postconditions:
375 
376  ensure(invariant());
377  ensure(chart_id() == xother.chart_id());
378 
379  // Exit:
380 
381  return;
382 }
383 
384 
385 // ===========================================================
386 // ANY FACET
387 // ===========================================================
388 
389 // PUBLIC MEMBER FUNCTIONS
390 
391 bool
393 invariant() const
394 {
395  bool result = true;
396 
397  // Preconditions:
398 
399  // Body:
400 
401  if(invariant_check())
402  {
403  // Prevent recursive calls to invariant
404 
406 
407  invariance(any::invariant());
408 
409  // The following is unexecutable because db() is pure virtual.
410 
411  invariance(unexecutable(db() >= -1));
412 
413  // Finished, turn invariant checking back on.
414 
416  }
417 
418  // Postconditions:
419 
420  // Exit
421 
422  return result;
423 }
424 
425 bool
427 is_ancestor_of(const any* other) const
428 {
429 
430  // Preconditions:
431 
432  require(other != 0);
433 
434  // Body:
435 
436  // True if other conforms to this
437 
438  bool result = dynamic_cast<const chart_point*>(other) != 0;
439 
440  // Postconditions:
441 
442  // Exit:
443 
444  return result;
445 }
446 
449 operator=(const chart_point& xother)
450 {
451 
452  // Preconditions:
453 
454  // Body:
455 
456  _chart_id = xother.chart_id();
457 
458  int lthis_db = db();
459  int lother_db = xother.db();
460  int lmin_db = min(lthis_db, lother_db);
461 
462  coord_type* lthis_coords = local_coords();
463  coord_type* lother_coords = const_cast<chart_point&>(xother).local_coords();
464 
465  for(int i=0; i<lmin_db; ++i)
466  {
467  lthis_coords[i] = lother_coords[i];
468  }
469 
470  for(int i=lmin_db; i<lthis_db; ++i)
471  {
472  lthis_coords[i] = 0.0;
473  }
474 
475 
476  // Postconditions:
477 
478  ensure(invariant());
479  ensure(chart_id() == xother.chart_id());
480  ensure_for_all(i, 0, (min(db(), xother.db())), isunordered_or_equals(local_coord(i), xother.local_coord(i)));
481  ensure_for_all(i, (min(db(), xother.db())), db(), local_coord(i) == 0.0);
482 
483  // Exit
484 
485  return *this;
486 }
487 
488 bool
490 operator==(const chart_point& xother) const
491 {
492 
493  // Preconditions:
494 
495  // Body:
496 
497  bool result = (db() == xother.db());
498 
499  result = result && (chart_id() == xother.chart_id());
500 
501  for(int i=0; result && (i<db()); ++i)
502  {
503  result = (local_coord(i) == xother.local_coord(i));
504  }
505 
506  // Postconditions:
507 
508  ensure(invariant());
509 
510  // Exit
511 
512  return result;
513 }
514 
515 // PROTECTED MEMBER FUNCTIONS
516 
517 // PRIVATE MEMBER FUNCTIONS
518 
519 
520 // ===========================================================
521 // DEBUGGING FACET
522 // ===========================================================
523 
524 // PUBLIC MEMBER FUNCTIONS
525 
526 void
528 to_stream(std::ostream& xos) const
529 {
530  xos << *this;
531 }
532 
533 std::string
535 to_string() const
536 {
537  ostringstream oss;
538  oss << *this;
539  return oss.str();
540 }
541 
542 
543 // ===========================================================
544 // NON-MEMBER FUNCTIONS
545 // ===========================================================
546 
547 std::ostream&
549 operator<<(std::ostream& os, const fiber_bundle::chart_point& xpt)
550 {
551 
552  // Preconditions:
553 
554  // Body:
555 
556  os << setw(6) << xpt.chart_id();
557 
558  for(int i=0; i<xpt.db(); i++)
559  {
560  os << ", " << setw(10) << xpt.local_coord(i);
561  }
562 
563  // Postconditions:
564 
565  // Exit:
566 
567  return os;
568 }
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
chart_point_coord_type coord_type
The type of local coordinate; the scalar type for the local coordinate vector space.
Definition: chart_point.h:65
void put_local_coords(const coord_type *xvalues, size_type xvalues_ub)
Sets the local coordinates to the values in xvalues.
Definition: chart_point.cc:253
static chart_point * new_chart_point(int xdb)
Factory method; creates a chart point with db() == xdb.
Definition: chart_point.cc:43
A point in a 3D chart space.
virtual int db() const =0
The dimension of this chart.
Definition: chart_point.cc:141
pod_index_type _chart_id
The id of the chart this point is in.
Definition: chart_point.h:148
STL namespace.
void put_chart_id(pod_index_type xchart)
Sets chart_id() to xchart_id.
Definition: chart_point.cc:103
A point in chart space.
Definition: chart_point.h:52
pod_index_type chart_id() const
The id of the chart this point is in.
Definition: chart_point.cc:96
chart_point & operator=(const chart_point &xother)
Assignment operator.
Definition: chart_point.cc:449
chart_point()
Default constructor.
Definition: chart_point.cc:347
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
Definition: chart_point.cc:427
bool is_valid() const
True if this ia a valid point in a chart.
Definition: chart_point.cc:303
Abstract base class with useful features for all objects.
Definition: any.h:39
void invalidate()
Makes this invalid.
Definition: chart_point.cc:322
virtual coord_type * local_coords()=0
The array of local coordinates.
Definition: chart_point.cc:209
void put_local_coords_zero()
Sets the local coordinates to the zero vector.
Definition: chart_point.cc:279
virtual bool invariant() const
Class invariant.
Definition: chart_point.cc:393
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
void to_stream(std::ostream &xos=std::cout) const
Write instance information to an ostream (default = stdout).
Definition: chart_point.cc:528
SHEAF_DLL_SPEC void min(const vd &x0, vd_value_type &xresult, bool xauto_access)
Minimum component of x0, pre-allocated version.
Definition: vd.cc:2161
A point in a 2D chart space.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
std::string to_string() const
Get instance information as a string.
Definition: chart_point.cc:535
virtual void put_local_coord(int xi, coord_type xvalue)=0
Sets the xi-th local coordinate of this point to xvalue.
Definition: chart_point.cc:187
virtual ~chart_point()
Destructor.
Definition: chart_point.cc:79
A point in a 1D chart space.
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
bool operator==(const chart_point &xother) const
Equality operator.
Definition: chart_point.cc:490
Namespace for the fiber_bundles component of the sheaf system.
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual coord_type local_coord(int xi) const =0
The xi-th local coordinate of this point.
Definition: chart_point.cc:163
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const binary_index &xbi)
Insert binary_index& xbi into ostream& os.
Definition: binary_index.cc:35
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710
SHEAF_DLL_SPEC bool isunordered_or_equals(float x1, float x2)
True if isunordered(x1, x2) or x1 == x2.
Definition: sheaf.cc:102