1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file tcRaytracedShadowMap.cpp
22 * @brief Shadow map using ray tracing of DEM.
25 #include "tcRaytracedShadowMap.h"
26 #include "tcGeoImageData.h"
31 * Constructors + destructor
34 /// Primary constructor.
35 tcRaytracedShadowMap::tcRaytracedShadowMap(tcChannel
* reference
, tcSrtmModel
* dem
, tcGeoImageData
* imagery
)
36 : tcChannelDem(dem
, imagery
,
37 tr("Raytraced Shadow Map"),
38 tr("Uses ray tracing and elevation data to highlight shadows."))
39 , m_referenceChannel(reference
)
41 , m_texToGeo(imagery
->texToGeo())
45 /// Primary constructor.
46 tcRaytracedShadowMap::tcRaytracedShadowMap(const tcGeo
& resolution
, tcSrtmModel
* dem
, tcGeoImageData
* imagery
)
47 : tcChannelDem(dem
, imagery
,
48 tr("Raytraced Shadow Map"),
49 tr("Uses ray tracing and elevation data to highlight shadows."))
50 , m_referenceChannel(0)
51 , m_resolution(resolution
)
52 , m_texToGeo(imagery
->texToGeo())
57 tcRaytracedShadowMap::~tcRaytracedShadowMap()
62 * Interface for derived class to implement
65 void tcRaytracedShadowMap::roundPortion(double* x1
, double* y1
, double* x2
, double* y2
)
67 //m_referenceChannel->roundPortion(x1,y1,x2,y2);
70 tcAbstractPixelData
* tcRaytracedShadowMap::loadPortion(double x1
, double y1
, double x2
, double y2
, bool changed
)
74 if (0 != m_referenceChannel
)
76 Reference
<tcAbstractPixelData
> channelData
= m_referenceChannel
->loadPortion(x1
, y1
, x2
, y2
, changed
);
77 width
= channelData
->width();
78 height
= channelData
->height();
82 tcGeo diag
= (tcGeo
)(m_texToGeo
* maths::Vector
<2,double>(x1
, y1
) - m_texToGeo
* maths::Vector
<2,double>(x2
,y2
));
83 maths::Vector
<2,double> res
= diag
/ m_resolution
;
84 width
= abs((int)res
[0]);
85 height
= abs((int)res
[1]);
86 // ensure resw and resh are powers of 2
87 for (int i
= 1; i
< 20; ++i
)
89 if (0 == (width
& ~((1<<i
)-1)))
95 for (int i
= 1; i
< 20; ++i
)
97 if (0 == (height
& ~((1<<i
)-1)))
105 // Create a new pixel buffer
106 tcPixelData
<float>* data
= new tcPixelData
<float>(width
, height
);
107 startProcessing(tr("Raytracing"));
108 for (int j
= 0; j
< height
; ++j
)
110 progress((float)j
/(height
-1));
111 for (int i
= 0; i
< width
; ++i
)
113 int index
= j
*width
+ i
;
114 // Transform coordinates
115 maths::Vector
<2,float> coord (x1
+ (x2
-x1
)*i
/ (width
- 1),
116 y1
+ (y2
-y1
)*j
/ (height
- 1));
117 tcGeo geoCoord
= geoAt(coord
);
118 float lit
= litAt(geoCoord
, true);
120 data
->buffer()[index
] = lit
;