File indexing completed on 2024-03-03 05:58:17

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2006 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef CELLS_H
0012 #define CELLS_H
0013 
0014 #include "utils.h"
0015 #include <QRect>
0016 #include <cassert>
0017 #include <map>
0018 
0019 class Point
0020 {
0021 public:
0022     Point();
0023 
0024     short x;
0025     short y;
0026     short prevX;
0027     short prevY;
0028 };
0029 
0030 // Key = cell, data = previous cell, compare = score
0031 typedef std::multimap<unsigned short, Point> TempLabelMap;
0032 
0033 /**
0034 @short Used for mapping out connections
0035 */
0036 const short startCellPos = -(1 << 14);
0037 class Cell
0038 {
0039 public:
0040     Cell();
0041     /**
0042      * Resets bestScore, prevX, prevY, addedToLabels, it, permanent for
0043      * each cell.
0044      */
0045     void reset();
0046 
0047     /**
0048      * 'Penalty' of using the cell from CNItem.
0049      */
0050     unsigned short CIpenalty;
0051     /**
0052      * 'Penalty' of using the cell from Connector.
0053      */
0054     unsigned short Cpenalty;
0055     /**
0056      * Best (lowest) score so far, _the_ best if it is permanent.
0057      */
0058     unsigned short bestScore;
0059     /**
0060      * Which cell this came from, (startCellPos,startCellPos) if originating
0061      * cell.
0062      */
0063     short prevX, prevY;
0064     /**
0065      * Whether the score can be improved on.
0066      */
0067     bool permanent;
0068     /**
0069      * Whether the cell has already been added to the list of cells to
0070      * check.
0071      */
0072     bool addedToLabels;
0073     /**
0074      * Pointer to the point in the TempLabelMap.
0075      */
0076     Point *point;
0077     /**
0078      * Number of connectors through that point.
0079      */
0080     unsigned short numCon;
0081 };
0082 
0083 /**
0084 @author David Saxton
0085 */
0086 class Cells
0087 {
0088 public:
0089     Cells(const QRect &canvasRect);
0090     ~Cells();
0091     /**
0092      * Resets bestScore, prevX, prevY, addedToLabels, it, permanent for each cell
0093      */
0094     void reset();
0095 
0096     QRect cellsRect() const
0097     {
0098         return m_cellsRect;
0099     }
0100 
0101     /**
0102      * Returns the cell containing the given position on the canvas.
0103      */
0104     Cell &cellContaining(int x, int y) const
0105     {
0106         return cell(roundDown(x, 8), roundDown(y, 8));
0107     }
0108     /**
0109      * @return if the given cell exists.
0110      */
0111     bool haveCell(int i, int j) const
0112     {
0113         if ((i < m_cellsRect.left()) || (i >= m_cellsRect.right()))
0114             return false;
0115 
0116         if ((j < m_cellsRect.top()) || (j >= m_cellsRect.bottom()))
0117             return false;
0118 
0119         return true;
0120     }
0121     /**
0122      * @return if there is a cell containg the given canvas point.
0123      */
0124     bool haveCellContaing(int x, int y) const
0125     {
0126         return haveCell(roundDown(x, 8), roundDown(y, 8));
0127     }
0128     Cell &cell(int i, int j) const
0129     {
0130         assert(i < m_cellsRect.right());
0131         assert(j < m_cellsRect.bottom());
0132         i -= m_cellsRect.left();
0133         j -= m_cellsRect.top();
0134         return m_cells[i][j];
0135     }
0136 
0137 protected:
0138     void init(const QRect &canvasRect);
0139 
0140     QRect m_cellsRect;
0141 
0142     Cell **m_cells;
0143 
0144 private:
0145     Cells(const Cells &);
0146     Cells &operator=(const Cells &);
0147 };
0148 
0149 #endif