The Original CHomP Software
cellvar.h
Go to the documentation of this file.
1
3
15
16// Copyright (C) 1997-2020 by Pawel Pilarczyk.
17//
18// This file is part of my research software package. This is free software:
19// you can redistribute it and/or modify it under the terms of the GNU
20// General Public License as published by the Free Software Foundation,
21// either version 3 of the License, or (at your option) any later version.
22//
23// This software is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26// GNU General Public License for more details.
27//
28// You should have received a copy of the GNU General Public License
29// along with this software; see the file "license.txt". If not,
30// please, see <https://www.gnu.org/licenses/>.
31
32// Started in January 2002. Last revision: October 24, 2013.
33
34
35#ifndef _CHOMP_CUBES_CELLVAR_H_
36#define _CHOMP_CUBES_CELLVAR_H_
37
38#include "chomp/system/config.h"
47#include "chomp/cubes/cubevar.h"
49
50#include <iostream>
51#include <fstream>
52#include <cstdlib>
53
54namespace chomp {
55namespace homology {
56
57
58// --------------------------------------------------
59// ------- CubicalCell with allocated memory --------
60// --------------------------------------------------
61
66template <class coordtype>
68{
69public:
71 typedef coordtype CoordType;
72
74 static const int MaxDim = // MaxBasDim;
77
80
83
85 tCellVar ();
86
88 tCellVar (const coordtype *c1, const coordtype *c2,
89 int spcdim, int celldim = -1);
90
93 const tCubeVar<coordtype> &q2);
94
97 tCellVar (const tCubeVar<coordtype> &q, int facedim);
98
100 explicit tCellVar (const tCubeVar<coordtype> &q);
101
104 tCellVar (const tCellVar<coordtype> &q, int offset, int ncoords);
105
107 tCellVar (const tCellVar<coordtype> &c);
108
111
113 int dim () const;
114
116 int spacedim () const;
117
119 coordtype *leftcoord (coordtype *c) const;
120
122 coordtype *rightcoord (coordtype *c) const;
123
125 int_t hashkey1 () const;
126
128 int_t hashkey2 () const;
129
131 static const char *name ();
132
134 static const char *pluralname ();
135
141 {
142 BitProduct = 0x01, // unset => two vertices
143 BitSpace = 0x02
144 };
145
147 static int OutputBits;
148
149 // --- friends: ---
150
152 friend inline int operator == (const tCellVar<coordtype> &c1,
153 const tCellVar<coordtype> &c2)
154 {
155 return ((c1. n == c2. n) && ((c1. n == 0) ||
156 thesame (c1. tab, c2. tab, c1. spacedim ())));
157 } /* operator == */
158
159private:
161 coordtype *tab;
162
167
169 void initialize (const coordtype *c1, const coordtype *c2,
170 int spcdim, int celldim = -1);
171
172}; /* class tCellVar */
173
174// --------------------------------------------------
175
176template<class coordtype>
178
179// --------------------------------------------------
180
181template <class coordtype>
182inline void tCellVar<coordtype>::initialize (const coordtype *c1,
183 const coordtype *c2, int spcdim, int)
184{
185 // test the validity of the dimension
186 if (spcdim <= 0)
187 throw "Non-positive dimension of the space.";
188 else if (spcdim >= MaxDim)
189 throw "Too high dimension of a cell.";
190
191 // initialize the internal number with the space dimension
192 n = static_cast<int_t> (spcdim) << NumBits;
193
194 // prepare a table for coordinates if necessary
195 tab = new coordtype [spcdim];
196 if (!tab)
197 throw "Not enough memory to create a cell.";
198
199 // copy the left corner coordinates to the cell
200 PointBase::wrapcopy (tab, c1, spcdim);
201
202 // prepare wrapped right coordinates for the detection of cell bits
203 coordtype r [MaxDim];
204 PointBase::wrapcopy (r, c2, spcdim);
205
206 // set the bits of the cell
207 for (int i = 0; i < spcdim; ++ i)
208 {
209 if (tab [i] != r [i])
210 n |= (static_cast<int_t> (1) << i);
211 }
212 return;
213} /* tCellVar::initialize */
214
215template <class coordtype>
217: tab (0), n (0)
218{
219 return;
220} /* tCellVar::tCellVar */
221
222template <class coordtype>
224 (const coordtype *c1, const coordtype *c2, int spcdim, int celldim)
225{
226 initialize (c1, c2, spcdim, celldim);
227 return;
228} /* tCellVar::tCellVar */
229
230template <class coordtype>
231inline int tCellVar<coordtype>::dim () const
232{
233 int count = 0;
234 for (int i = 0; i < NumBits; ++ i)
235 {
236 if (n & (static_cast<int_t> (1) << i))
237 ++ count;
238 }
239 return count;
240} /* tCellVar::dim */
241
242template <class coordtype>
244{
245 return static_cast<int> (n >> NumBits);
246} /* tCellVar::spacedim */
247
248template <class coordtype>
250 int offset, int ncoords)
251{
252 int spcdim = static_cast<int> (q. n >> NumBits);
253 if ((offset < 0) || (ncoords <= 0) || (offset + ncoords > spcdim))
254 throw "Wrong cell projection requested.";
255 coordtype right [MaxDim];
256 q. rightcoord (right);
257 initialize (q. tab + offset, right + offset, ncoords);
258// *** More efficient [unfinished, yet] ***
259// tab = new coordtype [spcdim];
260// if (!tab)
261// throw "Not enough memory to create a projected cell.";
262// for (int i = 0; i < spcdim; ++ i)
263// tab [i] = q. tab [i];
264// [...]
265 return;
266} /* tCellVar::tCellVar */
267
268template <class coordtype>
270{
271 n = q. n;
272 tab = new coordtype [spacedim ()];
273 if (!tab)
274 throw "Not enough memory to copy a cubical cell.";
275 copycoord (tab, q. tab, spacedim ());
276 return;
277} /* tCellVar::tCellVar */
278
279template <class coordtype>
281 (const tCellVar<coordtype> &q)
282{
283 // determine the new dimension
284 int d = q. spacedim ();
285
286 // allocate a new table if the dimension is to be changed
287 if (d != spacedim ())
288 {
289 if (tab)
290 delete [] tab;
291 tab = new coordtype [d];
292 if (!tab)
293 throw "Not enough memory to copy a cubical cell.";
294 }
295
296 // copy the data of the cubical cell
297 copycoord (tab, q. tab, d);
298 n = q. n;
299
300 return *this;
301} /* tCellVar::operator = */
302
303template <class coordtype>
304inline coordtype *tCellVar<coordtype>::leftcoord (coordtype *c) const
305{
306 if (!c)
307 throw "Null pointer to save left coordinates.";
308 int d = spacedim ();
309 for (int i = 0; i < d; ++ i)
310 c [i] = tab [i];
311 return c;
312} /* tCellVar::leftcoord */
313
314template <class coordtype>
315inline coordtype *tCellVar<coordtype>::rightcoord (coordtype *c) const
316{
317 if (!c)
318 throw "Null pointer to save right coordinates.";
319 int d = spacedim ();
320 for (int i = 0; i < d; ++ i)
321 {
322 c [i] = tab [i];
323 if (n & (static_cast<int_t> (1) << i))
324 ++ (c [i]);
325 }
327 return c;
328} /* tCellVar::rightcoord */
329
330template <class coordtype>
332{
333 switch (spacedim ())
334 {
335 case 0:
336 return 0;
337 case 1:
338 return (static_cast<int_t> (tab [0]) << 12) ^ n;
339 case 2:
340 return (((static_cast<int_t> (tab [0])) << 18) +
341 ((static_cast<int_t> (tab [1])) << 6)) ^ n;
342 default:
343 return (((static_cast<int_t> (tab [0])) << 18) +
344 ((static_cast<int_t> (tab [1])) << 6) +
345 ((static_cast<int_t> (tab [2])) >> 6)) ^ n;
346 }
347} /* tCellVar::hashkey1 */
348
349template <class coordtype>
351{
352 return c. hashkey1 ();
353} /* hashkey1 */
354
355template <class coordtype>
357{
358 int d = spacedim ();
359 switch (d)
360 {
361 case 0:
362 return 0;
363 case 1:
364 return (static_cast<int_t> (tab [0]) << 3) ^ (n << 5);
365 case 2:
366 return ((static_cast<int_t> (tab [0]) >> 1) +
367 (static_cast<int_t> (tab [1]) << 13)) ^ (n << 5);
368 default:
369 return (((static_cast<int_t> (tab [d - 1])) << 20) +
370 ((static_cast<int_t> (tab [d - 2])) << 9) +
371 ((static_cast<int_t> (tab [d - 3])) >> 1)) ^
372 (n << 5);
373 }
374} /* tCellVar::hashkey2 */
375
376template <class coordtype>
378{
379 return c. hashkey2 ();
380} /* hashkey2 */
381
382template <class coordtype>
384{
385 return "cubical cell";
386} /* tCellVar::name */
387
388template <class coordtype>
390{
391 return "cubical cells";
392} /* tCellVar::pluralname */
393
394// --------------------------------------------------
395
396template <class coordtype>
398 (const tCubeVar<coordtype> &q1, const tCubeVar<coordtype> &q2)
399{
400 // prepare tables for coordinates of the cubical cell
401 coordtype left [MaxDim];
402 coordtype right [MaxDim];
403
404 // get the coordinates of minimal vertices of both cubes
405 coordtype c1 [MaxDim];
406 q1. coord (c1);
407 coordtype c2 [MaxDim];
408 q2. coord (c2);
409
410 // get the dimension of the space and check for consistency
411 int spcdim = q1. dim ();
412 if (spcdim != q2. dim ())
413 throw "Trying to intersect cubes of different dimension.";
414
415 // calculate the coordinates of both vertices of the cubical cell
416 // and the dimension of the cubical cell
417 int celldim = 0;
418 const coordtype *wrap = PointBase::getwrapping (spcdim);
419 for (int i = 0; i < spcdim; ++ i)
420 {
421 if (c1 [i] == c2 [i])
422 {
423 left [i] = c1 [i];
424 right [i] = c1 [i];
425 ++ right [i];
426 ++ celldim;
427 }
428 else if ((c1 [i] - c2 [i] == -1) || (wrap && wrap [i] &&
429 (c1 [i] - c2 [i] == wrap [i] - 1)))
430 {
431 left [i] = c2 [i];
432 right [i] = c2 [i];
433 }
434 else if ((c1 [i] - c2 [i] == 1) || (wrap && wrap [i] &&
435 (c1 [i] - c2 [i] == -wrap [i] + 1)))
436 {
437 left [i] = c1 [i];
438 right [i] = c1 [i];
439 }
440 else
441 throw "The cubes do not intersect.";
442 }
443
444 // initialize the data of the cube
445 initialize (left, right, spcdim, celldim);
446
447 return;
448} /* tCellVar::tCellVar */
449
450template <class coordtype>
452// NOTE: This function is not as efficient as it could be. Instead of using
453// the "initialize" function, it should create the data structures itself.
454{
455 // get the coordinates of minimal vertex of the cube
456 const coordtype *left = q. tab + 1;
457
458 // get the dimension of the space and of the cell
459 int d = q. dim ();
460
461 // prepare a table for coordinates of the other vertex of the cell
462 coordtype right [MaxDim];
463
464 // calculate the coordinates of other vertex of the cell
465 for (int i = 0; i < d; ++ i)
466 right [i] = left [i] + 1;
467
468 // initialize the cell
469 initialize (left, right, d, d);
470 return;
471} /* tCellVar::tCellVar */
472
473template <class coordtype>
475 int facedim)
476// NOTE: This function is not as efficient as it could be. Instead of using
477// the "initialize" function, it should create the data structures itself.
478{
479 // get the coordinates of minimal vertex of the cube
480 const coordtype *left = q. tab + 1;
481
482 // prepare a table for coordinates of the other vertex of the cell
483 coordtype right [MaxDim];
484
485 // get the dimension of the space and of the cell
486 int d = q. dim ();
487
488 // calculate the coordinates of other vertex of the cell
489 for (int i = 0; i < d; ++ i)
490 right [i] = left [i] + ((i < facedim) ? 1 : 0);
491
492 // initialize the cell
493 initialize (left, right, d, facedim);
494 return;
495} /* tCellVar::tCellVar */
496
497// --------------------------------------------------
498
500template <class coordtype>
502 const tCellVar<coordtype> &c2)
503{
504 return !(c1 == c2);
505} /* operator != */
506
507// --------------------------------------------------
508
510template <class coordtype>
511inline tCellVar<coordtype> operator *
512 (const tCellVar<coordtype> &c1,
513 const tCellVar<coordtype> &c2)
514{
515 // get the underlying space dimensions for both cells
516 int d1 = c1. spacedim (), d2 = c2. spacedim ();
517 if (d1 + d2 >= tCellVar<coordtype>::MaxDim)
518 throw "Too high dimension of a Cartesian product of cells.";
519
520 // prepare arrays for the coordinates of the cell to create
521 coordtype left [tCellVar<coordtype>::MaxDim];
522 coordtype right [tCellVar<coordtype>::MaxDim];
523
524 // extract the coordinates of the first cell
525 c1. leftcoord (left);
526 c1. rightcoord (right);
527
528 // extract the coordinates of the second cell
529 c2. leftcoord (left + d1);
530 c2. rightcoord (right + d1);
531
532 // create the Cartesian product of the cells
533 return tCellVar<coordtype> (left, right, d1 + d2,
534 c1. dim () + c2. dim ());
535} /* operator * */
536
538template <class coordtype>
539inline std::ostream &operator << (std::ostream &out,
540 const tCellVar<coordtype> &c)
541{
542 return WriteCubicalCell (out, c);
543} /* operator << */
544
546template <class coordtype>
547inline std::istream &operator >> (std::istream &in,
549{
550 return ReadCubicalCell (in, c);
551} /* operator >> */
552
553// --------------------------------------------------
554
556template <class coordtype>
558 int i, bool onlyexisting)
559{
560 return CubicalBoundaryCell (q, i, onlyexisting);
561} /* boundarycell */
562
564template <class coordtype>
566 int i)
567{
568 return CubicalBoundaryCell (q, i);
569} /* boundarycell */
570
572template <class coordtype>
574{
575 return CubicalBoundaryLength (q);
576} /* boundarylength */
577
579template <class coordtype>
580inline int boundarycoef (const tCellVar<coordtype> &q, int i)
581{
582 return CubicalBoundaryCoef (q, i);
583} /* boundarycoef */
584
585
586} // namespace homology
587} // namespace chomp
588
589#endif // _CHOMP_CUBES_CELLVAR_H_
590
592
This file contains the definition of a bitfield class which works an array of bits.
This file contains the definition of some functions that are common for all types of cubical cells,...
This file contains classes and functions related to algebraic chain complexes and chain maps,...
This class defines cubical cell in R^n with edges parallel to the axes and with size 0 or 1 in each d...
Definition: cellvar.h:68
friend int operator==(const tCellVar< coordtype > &c1, const tCellVar< coordtype > &c2)
Verifies if two cells are identical.
Definition: cellvar.h:152
tCubeVar< coordtype >::PointBase PointBase
The point base (for wrapping and tabulating coordinates).
Definition: cellvar.h:82
int_t hashkey2() const
Return shashing key no. 2 required by the hashing set template.
Definition: cellvar.h:356
static int OutputBits
The output bits which determine how a cell is written.
Definition: cellvar.h:147
int_t n
High 6 bits = the dimension of the space.
Definition: cellvar.h:166
coordtype * tab
The lower left etc. corner of the cell.
Definition: cellvar.h:161
static const int MaxDim
The maximal dimension of a cell.
Definition: cellvar.h:74
coordtype * leftcoord(coordtype *c) const
Fills in the given table with the right corner coordinates.
Definition: cellvar.h:304
OutputBitValues
Bit masks that define how to output the cell.
Definition: cellvar.h:141
tCellVar()
The constructor of an empty cubical cell.
Definition: cellvar.h:216
coordtype CoordType
The type of the coordinates.
Definition: cellvar.h:71
int dim() const
Returns the dimension of the cubical cell.
Definition: cellvar.h:231
tCellVar< coordtype > & operator=(const tCellVar< coordtype > &c)
The assignment operator.
Definition: cellvar.h:281
static const char * pluralname()
Returns the plural name of the objects represented by this class.
Definition: cellvar.h:389
void initialize(const coordtype *c1, const coordtype *c2, int spcdim, int celldim=-1)
Initializes a new cell given its two corners.
Definition: cellvar.h:182
static const char * name()
Returns the name of the objects represented by this class.
Definition: cellvar.h:383
coordtype * rightcoord(coordtype *c) const
Fills in the given table with the right corner coordinates.
Definition: cellvar.h:315
int spacedim() const
Returns the dimension of the embedding space of the cubical cell.
Definition: cellvar.h:243
int_t hashkey1() const
Returns hashing key no. 1 required by the hashing set template.
Definition: cellvar.h:331
static const int MaxSpaceDim
The maximal dimension of the embedding space of a cell.
Definition: cellvar.h:79
This class defines a hypercube in R^n with edges parallel to the axes and with size 1 in each directi...
Definition: cubevar.h:72
static void wrapcoord(coordtype *c, int dim)
Wraps the given coordinates if necessary.
Definition: pointbas.h:311
static const coordtype * getwrapping(int d)
Returns the space wrapping for the given dimension.
Definition: pointbas.h:299
static void wrapcopy(coordtype *dest, const coordtype *src, int dim)
Copies the coordinates and wraps them if necessary.
Definition: pointbas.h:322
This class is a simplified version of the point base class used only for the space wrapping support.
Definition: pointbas.h:471
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This file defines full cubes whose embedding space dimension is not known apriori and therefore the a...
This file contains a definition of a general geometric complex which represents a polyhedron.
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This file contains the definition of the container "hashedset" which can be used to represent a set o...
This file defines a class "integer" which represents the ring of integers or the field of integers mo...
int CubicalBoundaryCoef(const celltype &q, int i)
Returns the i-th coefficient in the boundary of a cubical cell.
Definition: cellmain.h:157
const int MaxBasDim
The maximal dimension that can be used if the high bits of an integer store the value of the dimensio...
Definition: pointbas.h:89
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
Definition: bincube.h:907
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
Definition: bincube.h:1036
celltype CubicalBoundaryCell(const celltype &q, int i, bool onlyexisting)
Returns the i-th boundary element of a cell.
Definition: cellmain.h:72
const int NumBits
The number of bits in an integer number that remain to be used for other purposes,...
Definition: pointbas.h:71
int boundarylength(const tCellBase< coordtype > &q)
Returns the length of the boundary of a cell.
Definition: cellbase.h:501
int thesame(const coordtype *c1, const coordtype *c2, int dim)
Compare two points. Returns true iff they have the same coordinates.
Definition: pointset.h:98
std::ostream & WriteCubicalCell(std::ostream &out, const celltype &c)
Writes a cubical cell to the output stream in the text form.
Definition: cellmain.h:173
tCellBase< coordtype > boundarycell(const tCellBase< coordtype > &q, int i, bool onlyexisting)
Computes the i-th boundary element of a cell.
Definition: cellbase.h:485
bool operator!=(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:794
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
Definition: bincube.h:914
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
Definition: bincube.h:1029
void copycoord(coordtype *destination, const coordtype *source, int dim)
Copies the coordinates of one point to another.
Definition: pointset.h:108
std::istream & ReadCubicalCell(std::istream &in, celltype &c)
Reads a cubical cell form the input text stream.
Definition: cellmain.h:235
int CubicalBoundaryLength(const celltype &q)
Returns the length of the boundary of a cubical cell.
Definition: cellmain.h:150
int boundarycoef(const tCellBase< coordtype > &q, int i)
Returns the i-th coefficient in the boundary of a cell.
Definition: cellbase.h:508
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51
This file contains the definition of a point base class which is used for indexing all the points (n-...
This file contains the definition of a set of n-dimensional points with integer coordinates and sever...
This file contains some useful functions related to the text input/output procedures.