The Original CHomP Software
cellbase.h
Go to the documentation of this file.
1
3
16
17// Copyright (C) 1997-2020 by Pawel Pilarczyk.
18//
19// This file is part of my research software package. This is free software:
20// you can redistribute it and/or modify it under the terms of the GNU
21// General Public License as published by the Free Software Foundation,
22// either version 3 of the License, or (at your option) any later version.
23//
24// This software is distributed in the hope that it will be useful,
25// but WITHOUT ANY WARRANTY; without even the implied warranty of
26// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27// GNU General Public License for more details.
28//
29// You should have received a copy of the GNU General Public License
30// along with this software; see the file "license.txt". If not,
31// please, see <https://www.gnu.org/licenses/>.
32
33// Started in January 2002. Last revision: October 24, 2013.
34
35
36#ifndef _CHOMP_CUBES_CELLBASE_H_
37#define _CHOMP_CUBES_CELLBASE_H_
38
39#include "chomp/system/config.h"
50
51#include <iostream>
52#include <fstream>
53#include <cstdlib>
54
55namespace chomp {
56namespace homology {
57
58
59// --------------------------------------------------
60// ----------- CubicalCell with PointBase -----------
61// --------------------------------------------------
62
67template <class coordtype>
69{
70public:
72 typedef coordtype CoordType;
73
76
79
82
84 tCellBase ();
85
87 tCellBase (const coordtype *c1, const coordtype *c2,
88 int spcdim, int celldim = -1);
89
92 const tCubeBase<coordtype> &q2);
93
96 tCellBase (const tCubeBase<coordtype> &q, int facedim);
97
99 explicit tCellBase (const tCubeBase<coordtype> &q);
100
103 tCellBase (const tCellBase<coordtype> &q, int offset, int ncoords);
104
107
109 tCellBase<coordtype> &operator =
110 (const tCellBase<coordtype> &c);
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 tCellBase<coordtype> &c1,
153 const tCellBase<coordtype> &c2)
154 {
155 return (c1. n1 == c2. n1) && (c1. n2 == c2. n2);
156 } /* operator == */
157
158private:
160 int_t num1 () const;
161
163 int_t num2 () const;
164
168
172
174 void initialize (const coordtype *c1, const coordtype *c2,
175 int spcdim, int celldim = -1);
176
177}; /* class tCellBase */
178
179// --------------------------------------------------
180
181template<class coordtype>
183
184// --------------------------------------------------
185
186template <class coordtype>
188{
189 return (n1 & NumMask);
190} /* tCellBase::num1 */
191
192template <class coordtype>
194{
195 return (n2 & NumMask);
196} /* tCellBase::num2 */
197
198template <class coordtype>
199inline void tCellBase<coordtype>::initialize (const coordtype *c1,
200 const coordtype *c2, int spcdim, int celldim)
201{
202 // test the validity of the dimension
203 if (spcdim <= 0)
204 throw "Non-positive dimension of the space.";
205 else if (spcdim >= MaxDim)
206 throw "Too high dimension of a cell.";
207
208 // get the number of the left vertex
209 n1 = PointBase::number (c1, spcdim);
210 if (n1 < 0)
211 throw "Negative number of the left vertex.";
212
213 // get the number of the right vertex
214 n2 = PointBase::number (c2, spcdim);
215 if (n2 < 0)
216 throw "Negative number of the right vertex.";
217
218 // calculate the dimension of the cubical cell if it is unknown
219 if (celldim < 0)
220 {
221 celldim = 0;
222 c1 = PointBase::coord (n1, spcdim);
223 c2 = PointBase::coord (n2, spcdim);
224 for (int i = 0; i < spcdim; ++ i)
225 {
226 if (c2 [i] != c1 [i])
227 ++ celldim;
228 }
229 }
230
231 // add the space dimension and the cell dimension to the numbers
232 n1 |= (static_cast<int_t> (spcdim) << NumBits);
233 n2 |= (static_cast<int_t> (celldim) << NumBits);
234 return;
235} /* tCellBase::initialize */
236
237// --------------------------------------------------
238
239template <class coordtype>
241: n1 (0), n2 (0)
242{
243 return;
244} /* tCellBase::tCellBase */
245
246template <class coordtype>
248 (const coordtype *c1, const coordtype *c2, int spcdim, int celldim)
249{
250 initialize (c1, c2, spcdim, celldim);
251 return;
252} /* tCellBase::tCellBase */
253
254template <class coordtype>
256 (const tCubeBase<coordtype> &q1, const tCubeBase<coordtype> &q2)
257{
258 // get the dimension of the space and check for consistency
259 int spcdim = q1. dim ();
260 if (spcdim != q2. dim ())
261 throw "Trying to intersect cubes of different dimension.";
262
263 // get the coordinates of minimal vertices of both cubes
264 coordtype c1 [MaxDim];
265 q1. coord (c1);
266 coordtype c2 [MaxDim];
267 q2. coord (c2);
268
269 // prepare tables for the new coordinates of the cubical cell
270 coordtype left [MaxDim];
271 coordtype right [MaxDim];
272
273 // compute the new coordinates of the cubical cell
274 int celldim = CommonCell (left, right, c1, c2, spcdim,
275 PointBase::getwrapping (spcdim));
276
277 // create the cell as computed
278 initialize (left, right, spcdim, celldim);
279
280 return;
281} /* tCellBase::tCellBase */
282
283template <class coordtype>
285 int facedim)
286: n1 (q. n)
287{
288 // check the dimensions
289 int spcdim = static_cast<int> (n1 >> NumBits);
290 if (facedim < 0)
291 throw "Negative dimension of a face requested.";
292 if (facedim > spcdim)
293 throw "Too high dimension of a face requested.";
294
295 // create the opposite corner of the cell
296 const coordtype *c1 = PointBase::coord (num1 (), spcdim);
297 coordtype c2 [MaxDim];
298 for (int i = 0; i < spcdim; ++ i)
299 c2 [i] = c1 [i] + ((i < facedim) ? 1 : 0);
300
301 // determine the number of the opposite corner and add cell dimension
302 n2 = PointBase::number (c2, spcdim) |
303 (static_cast<int_t> (facedim) << NumBits);
304 return;
305} /* tCellBase::tCellBase */
306
307template <class coordtype>
309: n1 (q. n)
310{
311 // create the opposite corner of the cell
312 int spcdim = static_cast<int> (n1 >> NumBits);
313 const coordtype *c1 = PointBase::coord (num1 (), spcdim);
314 coordtype c2 [MaxDim];
315 for (int i = 0; i < spcdim; ++ i)
316 c2 [i] = c1 [i] + 1;
317
318 // determine the number of the opposite corner and add cell dimension
319 n2 = PointBase::number (c2, spcdim) |
320 (static_cast<int_t> (spcdim) << NumBits);
321 return;
322} /* tCellBase::tCellBase */
323
324template <class coordtype>
326 int offset, int ncoords)
327{
328 int spcdim = static_cast<int> (q. n1 >> NumBits);
329 if ((offset < 0) || (ncoords <= 0) || (offset + ncoords > spcdim))
330 throw "Wrong cell projection requested.";
331 const coordtype *c1 = PointBase::coord (q. n1 & NumMask, spcdim);
332 const coordtype *c2 = PointBase::coord (q. n2 & NumMask, spcdim);
333 initialize (c1 + offset, c2 + offset, ncoords);
334 return;
335} /* tCellBase::tCellBase */
336
337template <class coordtype>
339: n1 (q. n1), n2 (q. n2)
340{
341 return;
342} /* tCellBase::tCellBase */
343
344template <class coordtype>
346 (const tCellBase<coordtype> &q)
347{
348 n1 = q. n1;
349 n2 = q. n2;
350 return *this;
351} /* tCellBase::operator = */
352
353template <class coordtype>
354inline int tCellBase<coordtype>::dim () const
355{
356 return static_cast<int> (n2 >> NumBits);
357} /* tCellBase::dim */
358
359template <class coordtype>
361{
362 return (n1 >> NumBits);
363} /* tCellBase::spacedim */
364
365template <class coordtype>
366inline coordtype *tCellBase<coordtype>::leftcoord (coordtype *c) const
367{
368 const coordtype *leftc = PointBase::coord (num1 (), spacedim ());
369 if (!c)
370 throw "Null pointer to save left coordinates.";
371 copycoord (c, leftc, spacedim ());
372 return c;
373} /* tCellBase::leftcoord */
374
375template <class coordtype>
376inline coordtype *tCellBase<coordtype>::rightcoord (coordtype *c) const
377{
378 const coordtype *rightc = PointBase::coord (num2 (), spacedim ());
379 if (!c)
380 throw "Null pointer to save right coordinates.";
381 copycoord (c, rightc, spacedim ());
382 return c;
383} /* tCellBase::rightcoord */
384
385template <class coordtype>
387{
388 return ((n1 ^ 0x55555555u) << 17) ^ ((n1 ^ 0xAAAAAAAAu) << 7) ^
389 ((n2 ^ 0xAAAAAAAAu) >> 7);
390} /* tCellBase::hashkey1 */
391
392template <class coordtype>
394{
395 return c. hashkey1 ();
396} /* hashkey1 */
397
398template <class coordtype>
400{
401 return ((n2 ^ 0xAAAAAAAAu) << 21) ^ ((n2 ^ 0x55555555u) << 10) ^
402 ((n1 ^ 0x55555555u) >> 9);
403} /* tCellBase::hashkey2 */
404
405template <class coordtype>
407{
408 return c. hashkey2 ();
409} /* hashkey2 */
410
411template <class coordtype>
413{
414 return "cubical cell";
415} /* tCellBase::name */
416
417template <class coordtype>
419{
420 return "cubical cells";
421} /* tCellBase::pluralname */
422
423// --------------------------------------------------
424
426template <class coordtype>
428 const tCellBase<coordtype> &c2)
429{
430 return !(c1 == c2);
431} /* operator != */
432
433// --------------------------------------------------
434
436template <class coordtype>
437inline tCellBase<coordtype> operator *
438 (const tCellBase<coordtype> &c1,
439 const tCellBase<coordtype> &c2)
440{
441 // get the underlying space dimensions for both cells
442 int d1 = c1. spacedim (), d2 = c2. spacedim ();
443 if (d1 + d2 >= tCellBase<coordtype>::MaxDim)
444 throw "Too high dimension of a Cartesian product of cells.";
445
446 // prepare arrays for the coordinates of the cell to create
447 coordtype left [tCellBase<coordtype>::MaxDim];
448 coordtype right [tCellBase<coordtype>::MaxDim];
449
450 // extract the coordinates of the first cell
451 c1. leftcoord (left);
452 c1. rightcoord (right);
453
454 // extract the coordinates of the second cell
455 c2. leftcoord (left + d1);
456 c2. rightcoord (right + d1);
457
458 // create the Cartesian product of the cells
459 return tCellBase<coordtype> (left, right, d1 + d2,
460 c1. dim () + c2. dim ());
461} /* operator * */
462
464template <class coordtype>
465inline std::ostream &operator << (std::ostream &out,
466 const tCellBase<coordtype> &c)
467{
468 return WriteCubicalCell (out, c);
469} /* operator << */
470
472template <class coordtype>
473inline std::istream &operator >> (std::istream &in,
475{
476 return ReadCubicalCell (in, c);
477} /* operator >> */
478
479// --------------------------------------------------
480
484template <class coordtype>
486 int i, bool onlyexisting)
487{
488 return CubicalBoundaryCell (q, i, onlyexisting);
489} /* boundarycell */
490
492template <class coordtype>
494 int i)
495{
496 return CubicalBoundaryCell (q, i);
497} /* boundarycell */
498
500template <class coordtype>
502{
503 return CubicalBoundaryLength (q);
504} /* boundarylength */
505
507template <class coordtype>
508inline int boundarycoef (const tCellBase<coordtype> &q, int i)
509{
510 return CubicalBoundaryCoef (q, i);
511} /* boundarycoef */
512
513
514} // namespace homology
515} // namespace chomp
516
517#endif // _CHOMP_CUBES_CELLBASE_H_
518
520
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: cellbase.h:69
friend int operator==(const tCellBase< coordtype > &c1, const tCellBase< coordtype > &c2)
Verifies if two cells are identical.
Definition: cellbase.h:152
coordtype * leftcoord(coordtype *c) const
Fills in the given table with the left corner coordinates.
Definition: cellbase.h:366
tCubeBase< coordtype >::PointBase PointBase
The point base (for wrapping and tabulating coordinates).
Definition: cellbase.h:81
static const int MaxDim
The maximal dimension of a cell.
Definition: cellbase.h:75
int_t num1() const
Returns the first number of the cubical cell vertices.
Definition: cellbase.h:187
int dim() const
Returns the dimension of the cubical cell.
Definition: cellbase.h:354
static const char * name()
Returns the name of the objects represented by this class.
Definition: cellbase.h:412
void initialize(const coordtype *c1, const coordtype *c2, int spcdim, int celldim=-1)
Initializes a new cell given its two corners.
Definition: cellbase.h:199
static int OutputBits
The output bits which determine how a cell is written.
Definition: cellbase.h:147
int_t n2
The right vertex: high 6 bits = cube dim, the remaining bits form the number of the point in an appro...
Definition: cellbase.h:171
int_t hashkey1() const
Returns hashing key no. 1 required by the hashing set template.
Definition: cellbase.h:386
coordtype CoordType
The type of the coordinates.
Definition: cellbase.h:72
coordtype * rightcoord(coordtype *c) const
Fills in the given table with the right corner coordinates.
Definition: cellbase.h:376
int_t n1
The left vertex: high 6 bits = space dim, the remaining bits form the number of the point in an appro...
Definition: cellbase.h:167
tCellBase()
The constructor of an empty cubical cell.
Definition: cellbase.h:240
OutputBitValues
Bit masks that define how to output the cell.
Definition: cellbase.h:141
int_t num2() const
Returns the second number of the cubical cell vertices.
Definition: cellbase.h:193
int_t hashkey2() const
Return shashing key no. 2 required by the hashing set template.
Definition: cellbase.h:399
static const int MaxSpaceDim
The maximal dimension of the embedding space of a cell.
Definition: cellbase.h:78
int spacedim() const
Returns the dimension of the embedding space.
Definition: cellbase.h:360
static const char * pluralname()
Returns the plural name of the objects represented by this class.
Definition: cellbase.h:418
This class defines a hypercube in R^n with edges parallel to the axes and with size 1 in each directi...
Definition: cubebase.h:72
This class keeps a common set of points which are indexed for the use of other classes,...
Definition: pointbas.h:102
static const coordtype * coord(int_t number, int d)
Retrieves the coordinates of the given point.
Definition: pointbas.h:183
static const coordtype * getwrapping(int d)
Returns the space wrapping for the given dimension.
Definition: pointbas.h:299
static int_t number(const coordtype *c, int d)
Returns the number of the point with given coordinates.
Definition: pointbas.h:211
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This file contains the definition of full cubes which use a cube base class for indexing all the poss...
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
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
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
int CommonCell(coordtype *left, coordtype *right, const coordtype *c1, const coordtype *c2, int spcdim, const coordtype *wrap=0)
Computes the left and right corner of a cell which is the intersection of the two given cubes.
Definition: cellmain.h:328
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
const int_t NumMask
The mask of the bits remaining after the dimension bits are excluded.
Definition: pointbas.h:77
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.