summaryrefslogtreecommitdiffstats
path: root/raslib/miterf.icc
blob: dad1b2e5cf8a13ef5e1da633956086e65f38737e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: miterf.icc
 *
 * MODULE: raslib
 * CLASS:  r_MiterFloat
 *
*/

#include "raslib/miterf.hh"
#include "raslib/minterval.hh"
#include "raslib/rmdebug.hh"

#include <math.h>


const int     r_FixedPointNumber::FIXPREC  = 30;
const r_Range r_FixedPointNumber::carryPos = 1<<FIXPREC;
const r_Range r_FixedPointNumber::fracMask = carryPos-1;     
const double  r_FixedPointNumber::fixOne   = pow(2,(double)FIXPREC);

inline 
r_FixedPointNumber::r_FixedPointNumber()
  {
    intPart=0; fracPart=0;
   }
   
inline    
r_FixedPointNumber::r_FixedPointNumber(const double &d)
  { init(d);
   }
   
inline    
r_FixedPointNumber& r_FixedPointNumber::operator=(const r_FixedPointNumber &f)
  {
    intPart  = f.intPart;
    fracPart = f.fracPart;
    return *this;
   }
   
inline 
r_FixedPointNumber& r_FixedPointNumber::operator=(const double &d)
  {
    init(d);
    return *this;
   }
   
inline    
void r_FixedPointNumber::init(const double &d)
  {
    intPart = (r_Range)d;
    
    fracPart= (r_Range)fmod(fixOne*d,fixOne);
    
   }
    
inline     
r_Range r_FixedPointNumber::stepForward(const r_FixedPointNumber &f)
  {
    r_Range oldIntPart = intPart;
    
    intPart+=f.intPart; fracPart+=f.fracPart;
    
    if(fracPart & carryPos)
      {
        intPart++;
	fracPart &= fracMask;
	
       }
     return intPart-oldIntPart;  
   }

inline 
bool r_FixedPointNumber::stepForwardFlag(const r_FixedPointNumber &f)
  {
    intPart+=f.intPart; fracPart+=f.fracPart;
    
    if(fracPart & carryPos)
      {
        intPart++;
	fracPart &= fracMask;
        return true;	
       }
    return false;
   }
   
inline       
r_Range r_FixedPointNumber::getIntPart()
  {
    return intPart;
   }
 
inline             
std::ostream& operator<<(std::ostream &os,r_FixedPointNumber &f)
  { os<<'('<<f.intPart<<':'<<f.fracPart<<')';
    return os;
   }
   
//######################################

inline 
r_MiterFloat::r_MiterFloat(Tile *sourceTile, r_Minterval& sourceDomain, r_Minterval& destDomain)
  {
         dim = sourceDomain.dimension();
    iterDesc = new iter_desc[dim];
    
    r_Bytes cellSize = sourceTile->getType()->getSize();
    firstCell        = sourceTile->getContents();
    
    iter_desc *id = 0;
    const r_Minterval& tileDomain = sourceTile->getDomain();
    
    int i = 0;
    r_Bytes step  = cellSize;
    
    id = iterDesc + dim-1;
    
    for(i=dim-1; i>=0; i--, id-- )
      {
        id->min  = (double)sourceDomain[i].low();
	id->step = (double)(sourceDomain[i].high()-sourceDomain[i].low() +1) / ( destDomain[i].high() - destDomain[i].low()+1);
	id->maxSteps = destDomain[i].high() - destDomain[i].low()+1;
	id->dimStep   = step;
	id->scaleStep = step * id->step.getIntPart();
	firstCell += step* (sourceDomain[i].low() - tileDomain[i].low());
	step      *= (tileDomain[i].high() - tileDomain[i].low() + 1);
       }
    reset();   
   }
   
inline 
r_MiterFloat::~r_MiterFloat()
  {
    if(iterDesc) delete[] iterDesc;
   }

inline 
void r_MiterFloat::reset()
  {
    for(r_Dimension i=0;i<dim;i++)
      {
        iterDesc[i].pos  = iterDesc[i].min;
	iterDesc[i].cell = (char*)firstCell;
	iterDesc[i].countSteps = iterDesc[i].maxSteps;
       }
       
    done=false;   
   }
   
inline 
char* r_MiterFloat::nextCell()
  {
    if(done) return currentCell;
    
    r_Dimension i = dim;
    iter_desc *id = iterDesc + dim -1;
    currentCell   = iterDesc[dim-1].cell;
    
    while( i>0 )
      {
       id->countSteps--;
	 
       if(id->countSteps)
	 {// one more step in this dimension
	  if(id->pos.stepForwardFlag(id->step)) 
	     id->cell += id->dimStep;
	       
	  id->cell += id->scaleStep;
	  break;
	  }
       else
         { // we are finished with this dimension
	  id->pos = id->min;
	  id->countSteps = id-> maxSteps;
	  id--;
	  i--;
	 }     
       }
       
    if(i<dim)
      {
        if(i == 0)
	  done = true;
	else
	  {
	    r_Dimension j;
	    for( j=i; j<dim; j++)
	       iterDesc[j].cell = iterDesc[i-1].cell;
	   }
       }
     return currentCell;       
   }
   
inline 
bool r_MiterFloat::isDone()
  {
    return done;
   }