The Original CHomP Software
cellfix.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_CELLFIX_H_
37#define _CHOMP_CUBES_CELLFIX_H_
38
39#include "chomp/system/config.h"
48#include "chomp/cubes/cubefix.h"
50
51#include <iostream>
52#include <fstream>
53#include <cstdlib>
54
55namespace chomp {
56namespace homology {
57
58
59// --------------------------------------------------
60// ----------- CubicalCell with fixed dim -----------
61// --------------------------------------------------
62
67template <int dimfix, class coordtype>
69{
70public:
72 typedef coordtype CoordType;
73
75 static const int MaxDim =
78
80 static const int MaxSpaceDim = dimfix;
81
84
86 tCellFix ();
87
89 tCellFix (const coordtype *c1, const coordtype *c2,
90 int spcdim = 0, int celldim = -1);
91
95
98 tCellFix (const tCubeFix<dimfix,coordtype> &q, int facedim);
99
101 explicit tCellFix (const tCubeFix<dimfix,coordtype> &q);
102
105 template <int dimhigh>
107 int offset, int ncoords);
108
111
115
117 int dim () const;
118
120 int spacedim () const;
121
123 coordtype *leftcoord (coordtype *c) const;
124
126 coordtype *rightcoord (coordtype *c) const;
127
129 int_t hashkey1 () const;
130
132 int_t hashkey2 () const;
133
135 static const char *name ();
136
138 static const char *pluralname ();
139
145 {
146 BitProduct = 0x01, // unset => two vertices
147 BitSpace = 0x02
148 };
149
151 static int OutputBits;
152
153 // --- friends: ---
154
156 template <int _dimfix, class _coordtype>
157 friend inline int operator ==
160 {
161 return ((c1. n == c2. n) && (!memcmp (c1. tab, c2. tab,
162 dimfix * sizeof (coordtype))));
163 } /* operator == */
164
165private:
167 coordtype tab [dimfix];
168
173
175 void initialize (const coordtype *c1, const coordtype *c2,
176 int celldim = -1);
177
178}; /* class tCellFix */
179
180// --------------------------------------------------
181
182template <int dimfix, class coordtype>
184
185// --------------------------------------------------
186
187template <int dimfix, class coordtype>
188inline void tCellFix<dimfix,coordtype>::initialize (const coordtype *c1,
189 const coordtype *c2, int celldim)
190{
191 // copy the left corner coordinates to the cell
192 PointBase::wrapcopy (tab, c1, dimfix);
193
194 // prepare wrapped right coordinates for the detection of cell bits
195 coordtype r [dimfix];
196 PointBase::wrapcopy (r, c2, dimfix);
197
198 // if the dimension of the cell is known, then compute the bits only
199 if (celldim >= 0)
200 {
201 n = static_cast<int_t> (celldim) << NumBits;
202 if (!celldim)
203 return;
204 for (int i = 0; i < dimfix; ++ i)
205 {
206 if (tab [i] != r [i])
207 n |= (static_cast<int_t> (1) << i);
208 }
209 return;
210 }
211
212 // set the bits of the cell and compute its actual dimension
213 n = 0;
214 int dim = 0;
215 for (int i = 0; i < dimfix; ++ i)
216 {
217 if (tab [i] != r [i])
218 {
219 n |= (static_cast<int_t> (1) << i);
220 ++ dim;
221 }
222 }
223 n |= static_cast<int_t> (dim) << NumBits;
224
225 return;
226} /* tCellFix::initialize */
227
228// --------------------------------------------------
229
230template <int dimfix, class coordtype>
232:n (0)
233{
234 if (dimfix > MaxDim)
235 throw "Too high fixed-dim cell dimension.";
236 return;
237} /* tCellFix::tCellFix */
238
239template <int dimfix, class coordtype>
241 (const coordtype *c1, const coordtype *c2, int spcdim, int celldim)
242{
243 // test the validity of the dimension
244 if (spcdim < 0)
245 throw "Negative dimension of the space.";
246 else if ((spcdim > 0) && (spcdim != dimfix))
247 throw "Wrong dimension of a cell.";
248 if (dimfix > MaxDim)
249 throw "Too high fixed-dim cell dimension.";
250
251 // initialize the cell
252 initialize (c1, c2, celldim);
253 return;
254} /* tCellFix::tCellFix */
255
256template <int dimfix, class coordtype>
260{
261 if (dimfix > MaxDim)
262 throw "Too high fixed-dim cell dimension.";
263
264 // get the coordinates of minimal vertices of both cubes
265 coordtype c1 [dimfix];
266 q1. coord (c1);
267 coordtype c2 [dimfix];
268 q2. coord (c2);
269
270 // prepare tables for the new coordinates of the cubical cell
271 coordinate left [dimfix];
272 coordinate right [dimfix];
273
274 // compute the new coordinates of the cubical cell
275 int celldim = CommonCell (left, right, c1, c2, dimfix,
276 PointBase::getwrapping (dimfix));
277
278 // create the cell as computed
279 initialize (left, right, celldim);
280
281 return;
282} /* tCellFix::tCellFix */
283
284template <int dimfix, class coordtype>
286 (const tCubeFix<dimfix,coordtype> &q, int facedim)
287{
288 if (facedim < 0)
289 throw "Negative dimension of a face requested.";
290 if (facedim > dimfix)
291 throw "Too high dimension of a face requested.";
292 if (dimfix > MaxDim)
293 throw "Too high fixed-dim cell dimension.";
294 copycoord (tab, q. tab, dimfix);
295 n = static_cast<int_t> (facedim) << NumBits;
296 for (int i = 0; i < facedim; ++ i)
297 n |= static_cast<int_t> (1) << i;
298 return;
299} /* tCellFix::tCellFix */
300
301template <int dimfix, class coordtype>
304: n ((static_cast<int_t> (dimfix) << NumBits) | NumMask)
305{
306 if (dimfix > MaxDim)
307 throw "Too high fixed-dim cell dimension.";
308 copycoord (tab, q. tab, dimfix);
309 return;
310} /* tCellFix::tCellFix */
311
312template <int dimfix, class coordtype>
313template <int dimhigh>
315 (const tCellFix<dimhigh,coordtype> &q, int offset, int ncoords)
316{
317 if ((offset < 0) || (ncoords <= 0) || (ncoords != dimfix) ||
318 (offset + ncoords > dimhigh))
319 throw "Wrong cell projection requested.";
320 // note: the following can be done in a slightly more efficient way,
321 // without creating the right coordinates and calling "initialize"
322 coordtype right [dimhigh];
323 q. rightcoord (right);
324 initialize (q. tab + offset, right + offset);
325 return;
326} /* tCellFix::tCellFix */
327
328template <int dimfix, class coordtype>
331: n (q. n)
332{
333 copycoord (tab, q. tab, dimfix);
334 return;
335} /* tCellFix::tCellFix */
336
337template <int dimfix, class coordtype>
340{
341 memcpy (tab, q. tab, dimfix * sizeof (coordtype));
342 n = q. n;
343 return *this;
344} /* tCellFix::operator = */
345
346template <int dimfix, class coordtype>
348{
349 return static_cast<int> (n >> NumBits);
350} /* tCellFix::dim */
351
352template <int dimfix, class coordtype>
354{
355 return dimfix;
356} /* tCellFix::spacedim */
357
358template <int dimfix, class coordtype>
359inline coordtype *tCellFix<dimfix,coordtype>::leftcoord (coordtype *c) const
360{
361 if (!c)
362 throw "Null pointer to save left coordinates.";
363 for (int i = 0; i < dimfix; ++ i)
364 c [i] = tab [i];
365 return c;
366} /* tCellFix::leftcoord */
367
368template <int dimfix, class coordtype>
369inline coordtype *tCellFix<dimfix,coordtype>::rightcoord (coordtype *c) const
370{
371 if (!c)
372 throw "Null pointer to save right coordinates.";
373 for (int i = 0; i < dimfix; ++ i)
374 {
375 c [i] = tab [i] +
376 ((n & (static_cast<int_t> (1) << i)) ? 1 : 0);
377 }
378 PointBase::wrapcoord (c, spacedim ());
379 return c;
380} /* tCellFix::rightcoord */
381
382template <int dimfix, class coordtype>
384{
385 switch (dimfix)
386 {
387 case 1:
388 return (static_cast<int_t> (tab [0]) << 12) ^ n;
389 case 2:
390 return (((static_cast<int_t> (tab [0])) << 18) +
391 ((static_cast<int_t> (tab [1])) << 6)) ^ n;
392 default:
393 return (((static_cast<int_t> (tab [0])) << 18) +
394 ((static_cast<int_t> (tab [1])) << 6) +
395 ((static_cast<int_t> (tab [2])) >> 6)) ^ n;
396 }
397} /* tCellFix::hashkey1 */
398
399template <int dimfix, class coordtype>
401{
402 return c. hashkey1 ();
403} /* hashkey1 */
404
405template <int dimfix, class coordtype>
407{
408 switch (dimfix)
409 {
410 case 1:
411 return (static_cast<int_t> (tab [0]) << 3) ^ (n << 5);
412 case 2:
413 return ((static_cast<int_t> (tab [0]) >> 1) +
414 (static_cast<int_t> (tab [1]) << 13)) ^ (n << 5);
415 default:
416 return (((static_cast<int_t> (tab [dimfix - 1])) << 20) +
417 ((static_cast<int_t> (tab [dimfix - 2])) << 9) +
418 ((static_cast<int_t> (tab [dimfix - 3])) >> 1)) ^
419 (n << 5);
420 }
421} /* tCellFix::hashkey2 */
422
423template <int dimfix, class coordtype>
425{
426 return c. hashkey2 ();
427} /* hashkey2 */
428
429template <int dimfix, class coordtype>
431{
432 return "cubical cell";
433} /* tCellFix::name */
434
435template <int dimfix, class coordtype>
437{
438 return "cubical cells";
439} /* tCellFix::pluralname */
440
441// --------------------------------------------------
442
444template <int dimfix, class coordtype>
447{
448 return !(c1 == c2);
449} /* operator != */
450
451// --------------------------------------------------
452
454template <int dim1, int dim2, class coordtype>
456 (const tCellFix<dim1,coordtype> &c1,
457 const tCellFix<dim2,coordtype> &c2)
458{
459 // prepare arrays for the coordinates of the cell to create
460 coordtype left [dim1 + dim2];
461 coordtype right [dim1 + dim2];
462
463 // extract the coordinates of the first cell
464 c1. leftcoord (left);
465 c1. rightcoord (right);
466
467 // extract the coordinates of the second cell
468 c2. leftcoord (left + dim1);
469 c2. rightcoord (right + dim1);
470
471 // create the Cartesian product of the cells
472 return tCellFix<dim1+dim2,coordtype> (left, right,
473 dim1 + dim2, c1. dim () + c2. dim ());
474} /* operator * */
475
477template <int dimfix, class coordtype>
478inline std::ostream &operator << (std::ostream &out,
480{
481 return WriteCubicalCell (out, c);
482} /* operator << */
483
485template <int dimfix, class coordtype>
486inline std::istream &operator >> (std::istream &in,
488{
489 return ReadCubicalCell (in, c);
490} /* operator >> */
491
492// --------------------------------------------------
493
495template <int dimfix, class coordtype>
497 (const tCellFix<dimfix,coordtype> &q, int i, bool onlyexisting)
498{
499 return CubicalBoundaryCell (q, i, onlyexisting);
500} /* boundarycell */
501
503template <int dimfix, class coordtype>
505 (const tCellFix<dimfix,coordtype> &q, int i)
506{
507 return CubicalBoundaryCell (q, i);
508} /* boundarycell */
509
511template <int dimfix, class coordtype>
513{
514 return CubicalBoundaryLength (q);
515} /* boundarylength */
516
518template <int dimfix, class coordtype>
519inline int boundarycoef (const tCellFix<dimfix,coordtype> &q, int i)
520{
521 return CubicalBoundaryCoef (q, i);
522} /* boundarycoef */
523
524
525} // namespace homology
526} // namespace chomp
527
528#endif // _CHOMP_CUBES_CELLFIX_H_
529
531
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: cellfix.h:69
int dim() const
Returns the dimension of the cubical cell.
Definition: cellfix.h:347
tCubeFix< dimfix, coordtype >::PointBase PointBase
The point base (for wrapping and tabulating coordinates).
Definition: cellfix.h:83
OutputBitValues
Bit masks that define how to output the cell.
Definition: cellfix.h:145
static const int MaxSpaceDim
The maximal dimension of the embedding space of a cell.
Definition: cellfix.h:80
static int OutputBits
The output bits which determine how a cell is written.
Definition: cellfix.h:151
coordtype * leftcoord(coordtype *c) const
Fills in the given table with the left corner coordinates.
Definition: cellfix.h:359
coordtype * rightcoord(coordtype *c) const
Fills in the given table with the right corner coordinates.
Definition: cellfix.h:369
coordtype tab[dimfix]
A table with the left coordinate of the cell.
Definition: cellfix.h:167
int spacedim() const
Returns the dimension of the embedding space.
Definition: cellfix.h:353
void initialize(const coordtype *c1, const coordtype *c2, int celldim=-1)
Initializes a new cell, given its two corners.
Definition: cellfix.h:188
tCellFix()
The constructor of an empty cubical cell.
Definition: cellfix.h:231
static const char * pluralname()
Returns the plural name of the objects represented by this class.
Definition: cellfix.h:436
int_t hashkey2() const
Returns hashing key no. 2 required by the hashing set template.
Definition: cellfix.h:406
int_t hashkey1() const
Returns hashing key no. 1 required by the hashing set template.
Definition: cellfix.h:383
static const char * name()
Returns the name of the objects represented by this class.
Definition: cellfix.h:430
static const int MaxDim
The maximal dimension of a cell.
Definition: cellfix.h:75
coordtype CoordType
The type of the coordinates.
Definition: cellfix.h:72
int_t n
High 6 bits = the dimension of the cell (not: space).
Definition: cellfix.h:172
This class defines a hypercube in R^n with edges parallel to the axes and with size 1 in each directi...
Definition: cubefix.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 class defines full cubes in which the embedding space dimension is known apriori.
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
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
short int coordinate
The default type of coordinates.
Definition: pointset.h:63
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.