1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // This file contains the implementation of the ComputeFeatureLookAt function.
27 
28 #include "kml/engine/feature_view.h"
29 #include "kml/base/math_util.h"
30 #include "kml/dom/kml_factory.h"
31 #include "kml/engine/bbox.h"
32 #include "kml/engine/location_util.h"
33 
34 using kmldom::LookAtPtr;
35 using kmldom::KmlFactory;
36 using kmldom::FeaturePtr;
37 
38 namespace kmlengine {
39 
40 // The range of the LookAt that emcompasses the feature's extents depends on
41 // the field of view of the virtual camera. Google Earth has a 60 degree
42 // horizontal FOV (equivalent to looking through a 30mm lens) and that value
43 // is used here.
44 static const double kFieldOfView = 60.0;
45 
46 // To avoid zooming in to far to point features or features that are spatially
47 // small, we clap the computed range to a minimum value. Google Earth has
48 // always used 1 km as its minimum.
49 static const double kMinRange = 1000.0;
50 
ComputeFeatureLookAt(const FeaturePtr & feature)51 LookAtPtr ComputeFeatureLookAt(const FeaturePtr& feature) {
52   Bbox bbox;
53   if (!GetFeatureBounds(feature, &bbox)) {
54     return NULL;
55   }
56   return ComputeBboxLookAt(bbox);
57 }
58 
ComputeBboxLookAt(const Bbox & bbox)59 LookAtPtr ComputeBboxLookAt(const Bbox& bbox) {
60   // The distance from the center point to the northern bound.
61   const double d_north = kmlbase::DistanceBetweenPoints(
62       bbox.GetCenterLat(), bbox.GetCenterLon(),
63       bbox.get_north(), bbox.GetCenterLon());
64   // The distance from the center point to the western bound.
65   const double d_west = kmlbase::DistanceBetweenPoints(
66       bbox.GetCenterLat(), bbox.GetCenterLon(),
67       bbox.GetCenterLat(), bbox.get_west());
68   // The distance from the center point to the north-western bound.
69   const double d_nw_corner = sqrt(pow(d_north, 2) + pow(d_west, 2));
70   // Scale the result by 1.2 to provide a margin around the feature.
71   double range = d_nw_corner * tan(kmlbase::DegToRad(kFieldOfView)) * 1.1;
72   if (range < kMinRange) {
73     range = kMinRange;  // Clamp to minimum range.
74   }
75   LookAtPtr lookat = KmlFactory::GetFactory()->CreateLookAt();
76   lookat->set_longitude(bbox.GetCenterLon());
77   lookat->set_latitude(bbox.GetCenterLat());
78   lookat->set_range(range);
79   lookat->set_altitudemode(kmldom::ALTITUDEMODE_RELATIVETOGROUND);
80   // We don't explcitly set the altitude, tilt or heading, relying instead on
81   // their defaults of 0. This produces a LookAt that is north-up, looking
82   // straight down.
83   return lookat;
84 }
85 
86 }  // end namespace kmlengine
87