| 1 |
// |
| 2 |
// AutoCoast: Automatic Coastline scenery generator for Flight Simulator |
| 3 |
// |
| 4 |
// Copyright (c) 2004, Takuya Murakami. All rights reserved. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions |
| 8 |
// are met: |
| 9 |
// |
| 10 |
// 1. Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
// notice, this list of conditions and the following disclaimer in the |
| 15 |
// documentation and/or other materials provided with the distribution. |
| 16 |
// |
| 17 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
| 21 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
// bbox.cc : Bounding Box |
| 30 |
|
| 31 |
#include "bbox.hpp" |
| 32 |
|
| 33 |
BoundingBox::BoundingBox() |
| 34 |
{ |
| 35 |
xmin = 1.0e+12; xmax = -1.0e+12; |
| 36 |
ymin = 1.0e+12; ymax = -1.0e+12; |
| 37 |
} |
| 38 |
|
| 39 |
gpc_polygon *BoundingBox::createClipPolygon(void) |
| 40 |
{ |
| 41 |
gpc_vertex *vertex = new gpc_vertex[4]; |
| 42 |
vertex[0].x = xmin; vertex[0].y = ymin; |
| 43 |
vertex[1].x = xmax; vertex[1].y = ymin; |
| 44 |
vertex[2].x = xmax; vertex[2].y = ymax; |
| 45 |
vertex[3].x = xmin; vertex[3].y = ymax; |
| 46 |
|
| 47 |
gpc_vertex_list *vlist = new gpc_vertex_list; |
| 48 |
vlist->num_vertices = 4; |
| 49 |
vlist->vertex = vertex; |
| 50 |
|
| 51 |
gpc_polygon *polygon = new gpc_polygon; |
| 52 |
polygon->num_contours = 1; |
| 53 |
polygon->contour = vlist; |
| 54 |
polygon->hole = NULL; |
| 55 |
|
| 56 |
return polygon; |
| 57 |
} |
| 58 |
|
| 59 |
void BoundingBox::destroyClipPolygon(gpc_polygon *polygon) |
| 60 |
{ |
| 61 |
delete polygon->contour->vertex; |
| 62 |
delete polygon->contour; |
| 63 |
delete polygon; |
| 64 |
} |
| 65 |
|
| 66 |
bool BoundingBox::isCrossing(BoundingBox &target) |
| 67 |
{ |
| 68 |
if (target.xmax <= xmin || xmax <= target.xmin || |
| 69 |
target.ymax <= ymin || ymax <= target.ymin) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
void BoundingBox::setpoint(double x, double y) |
| 76 |
{ |
| 77 |
if (xmin > x) xmin = x; |
| 78 |
if (xmax < x) xmax = x; |
| 79 |
|
| 80 |
if (ymin > y) ymin = y; |
| 81 |
if (ymax < y) ymax = y; |
| 82 |
} |
| 83 |
|
| 84 |
void BoundingBox::extend(BoundingBox *target) |
| 85 |
{ |
| 86 |
if (target->xmin < xmin) xmin = target->xmin; |
| 87 |
if (target->xmax > xmax) xmax = target->xmax; |
| 88 |
if (target->ymin < ymin) ymin = target->ymin; |
| 89 |
if (target->ymax > ymax) ymax = target->ymax; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
bool BoundingBox::isInbox(double x, double y) |
| 94 |
{ |
| 95 |
if (xmin <= x && x <= xmax && |
| 96 |
ymin <= y && y <= ymax) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
inline bool SAME(double a, double b) |
| 103 |
{ |
| 104 |
const double MARGIN = 1.0e-7; |
| 105 |
|
| 106 |
if (a - b > -MARGIN && |
| 107 |
a - b < MARGIN) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
bool BoundingBox::compare(BoundingBox &target) |
| 114 |
{ |
| 115 |
if (SAME(xmin, target.xmin) && SAME(xmax, target.xmax) && |
| 116 |
SAME(ymin, target.ymin) && SAME(ymax, target.ymax)) { |
| 117 |
return true; |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
// |
| 123 |
// On the edge of BoundingBox? |
| 124 |
// |
| 125 |
int BoundingBox::checkEdge(double x, double y) |
| 126 |
{ |
| 127 |
int flags = 0; |
| 128 |
|
| 129 |
if (SAME(x, xmin)) flags |= onLeftEdge; |
| 130 |
if (SAME(x, xmax)) flags |= onRightEdge; |
| 131 |
if (SAME(y, ymin)) flags |= onUpperEdge; |
| 132 |
if (SAME(y, ymax)) flags |= onBottomEdge; |
| 133 |
|
| 134 |
return flags; |
| 135 |
} |