source: experimental/TerrainTest/rampedEllipsoidTechnique.cpp @ 272

Last change on this file since 272 was 272, checked in by Torben Dannhauer, 13 years ago
File size: 5.4 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * OpenSceneGraph Public License for more details.
15*/
16
17#include "rampedEllipsoidTechnique.h"
18
19
20rampedEllipsoidTechnique::rampedEllipsoidTechnique()
21{
22        _height = 0.0;
23        _rampWidthFactor = 0.4; // 10% of ROI is ramp.
24}
25
26rampedEllipsoidTechnique::~rampedEllipsoidTechnique()
27{
28}
29
30void rampedEllipsoidTechnique::modifyHeightfield(region& modificationROI, osg::HeightField* h, region tileExtends)
31{
32        //OSG_NOTIFY( osg::ALWAYS ) << "rampedEllipsoidTechnique::modifyHeightfield()" << std::endl;
33        //OSG_NOTIFY( osg::ALWAYS ) << "LAT: " << tileExtends._lat_min << " | " << tileExtends._lat_max << std::endl;
34        //OSG_NOTIFY( osg::ALWAYS ) << "LON: " << tileExtends._lon_min << " | " << tileExtends._lon_max << std::endl;
35
36        // Determine modificationROI without ramp
37        double deltaLatPerSide = modificationROI.delta_lat() * _rampWidthFactor / 2.0;
38        double deltaLonPerSide = modificationROI.delta_lon() * _rampWidthFactor / 2.0;
39        region coreROI = modificationROI;
40        coreROI._lat_min+=deltaLatPerSide;
41        coreROI._lat_max-=deltaLatPerSide;
42        coreROI._lon_min+=deltaLonPerSide;
43        coreROI._lon_max-=deltaLonPerSide;
44
45        // Calculate colum start/end and row start/end of affected vertices
46        int Y_start=0, Y_startRamp=0, Y_startCore=0, Y_endCore=0, Y_endRamp=0, Y_end=h->getNumRows();           // Lat
47        int X_start=0, X_startRamp=0, X_startCore=0, X_endCore=0, X_endRamp=0, X_end=h->getNumColumns();        // Lon
48
49        // Lat
50        Y_startRamp = round((modificationROI._lat_min-tileExtends._lat_min) / h->getYInterval());
51        Y_startCore = round((coreROI._lat_min-tileExtends._lat_min) / h->getYInterval());
52        Y_endCore   = round((coreROI._lat_max-tileExtends._lat_min) / h->getYInterval());
53        Y_endRamp   = round((modificationROI._lat_max-tileExtends._lat_min) / h->getYInterval());
54
55        clampValue(Y_startRamp, Y_start, Y_end);
56        clampValue(Y_startCore, Y_start, Y_end);
57        clampValue(Y_endCore, Y_start, Y_end);
58        clampValue(Y_endRamp, Y_start, Y_end);
59
60        // Lon
61        X_startRamp = round((modificationROI._lon_min-tileExtends._lon_min) / h->getXInterval());
62        X_startCore = round((coreROI._lon_min-tileExtends._lon_min) / h->getXInterval());
63        X_endCore   = round((coreROI._lon_max-tileExtends._lon_min) / h->getXInterval());
64        X_endRamp   = round((modificationROI._lon_max-tileExtends._lon_min) / h->getXInterval());
65
66        clampValue(X_startRamp, X_start, X_end);
67        clampValue(X_startCore, X_start, X_end);
68        clampValue(X_endCore, X_start, X_end);
69        clampValue(X_endRamp, X_start, X_end);
70
71
72        // Modify height value of affected vertices in the core ROI
73        for(int x=X_startRamp;x<X_endRamp;x++)
74        {
75                for(int y=Y_startRamp;y<Y_endRamp;y++)
76                {
77                        // Calculate vertex position in global coordinate system (lat/lon)
78                        double vertex_lat = y*h->getYInterval()+tileExtends._lat_min;
79                        double vertex_lon = x*h->getXInterval()+tileExtends._lon_min;
80                        // Calculate vertex delta h: h_dest - h_org
81                        double height_org = h->getHeight(x,y);
82                        double delta_h = _height - height_org;
83                        // preset offsetvalue to max and let every region try to lower it...
84                        double destination_height = std::numeric_limits<double>::max();
85
86                        // Calculate vertex height according to the region it is in.
87                        if( Y_startCore<=y && y<Y_endCore && X_startCore<=x && x<X_endCore )    // If vertex is inside core: Apply final height
88                        {
89                                        if(_height<destination_height)
90                                        destination_height = _height;
91                        }
92                        if(Y_startRamp<=y && y<Y_startCore) // Vertex is inside Y start ramp
93                        {
94                                double delta_s = abs(modificationROI._lat_min-vertex_lat);
95                                double gradient = delta_h/deltaLatPerSide;
96                                double dest = delta_s*gradient + height_org;
97                                if(dest<destination_height)
98                                        destination_height = dest;                     
99                        }
100                        if(X_startRamp<=x && x<X_startCore)     // Vertex is inside X start ramp
101                        {
102                                double delta_s = abs(modificationROI._lon_min-vertex_lon);
103                                double gradient = delta_h/deltaLonPerSide;
104                                double dest = delta_s*gradient + height_org;
105                                if(dest<destination_height)
106                                        destination_height = dest;
107                        }
108                        if(Y_endCore<=y && y<Y_endRamp)  // Vertex is inside Y end ramp
109                        {
110                                double delta_s = abs(modificationROI._lat_max-vertex_lat);
111                                double gradient = delta_h/deltaLatPerSide;
112                                double dest = delta_s*gradient + height_org;
113                                if(dest<destination_height)
114                                        destination_height = dest;
115                        }
116                        if (X_endCore<=x && x<X_endRamp) // Vertex is inside X end ramp
117                        {
118                                double delta_s = abs(modificationROI._lon_max-vertex_lon);
119                                double gradient = delta_h/deltaLonPerSide;
120                                double dest = delta_s*gradient + height_org;
121                                if(dest<destination_height)
122                                        destination_height = dest;
123                        }
124                       
125                        // Set height to calculated value
126                        h->setHeight( x, y, destination_height);
127                }
128        }
129}
130
Note: See TracBrowser for help on using the repository browser.