Add test for bug #448
[geos.git] / src / precision / CommonBitsOp.cpp
blob249e40113d4177fa45bafd0388e39e3eedb12894
1 /**********************************************************************
2 * $Id$
4 * GEOS - Geometry Engine Open Source
5 * http://geos.refractions.net
7 * Copyright (C) 2005-2006 Refractions Research Inc.
8 * Copyright (C) 2001-2002 Vivid Solutions Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 **********************************************************************/
17 #include <geos/precision/CommonBitsOp.h>
18 #include <geos/precision/CommonBitsRemover.h>
19 #include <geos/geom/Geometry.h>
21 #include <vector>
22 #include <string>
23 #include <memory>
24 #include <cassert>
26 #ifndef GEOS_DEBUG
27 #define GEOS_DEBUG 0
28 #endif
30 #ifdef GEOS_DEBUG
31 #include <iostream>
32 #endif
34 using namespace std;
35 using namespace geos::geom;
37 namespace geos {
38 namespace precision { // geos.precision
40 /*public*/
41 CommonBitsOp::CommonBitsOp()
43 returnToOriginalPrecision(true)
46 #if GEOS_DEBUG
47 std::cerr << "CommonBitsOp[" << this
48 << "]::CommonBitsOp()" << std::endl;
49 #endif
52 /*public*/
53 CommonBitsOp::CommonBitsOp(bool nReturnToOriginalPrecision)
55 returnToOriginalPrecision(nReturnToOriginalPrecision)
57 #if GEOS_DEBUG
58 std::cerr << "CommonBitsOp[" << this
59 << "]::CommonBitsOp(bool "
60 << nReturnToOriginalPrecision << ")"
61 << std::endl;
62 #endif
65 /*public*/
66 Geometry*
67 CommonBitsOp::intersection(
68 const Geometry* geom0,
69 const Geometry* geom1)
71 auto_ptr<Geometry> rgeom0;
72 auto_ptr<Geometry> rgeom1;
73 removeCommonBits(geom0, geom1, rgeom0, rgeom1);
74 return computeResultPrecision(rgeom0->intersection(rgeom1.get()));
77 /*public*/
78 Geometry*
79 CommonBitsOp::Union(
80 const Geometry* geom0,
81 const Geometry* geom1)
83 auto_ptr<Geometry> rgeom0;
84 auto_ptr<Geometry> rgeom1;
85 removeCommonBits(geom0, geom1, rgeom0, rgeom1);
86 return computeResultPrecision(rgeom0->Union(rgeom1.get()));
89 /*public*/
90 Geometry*
91 CommonBitsOp::difference(
92 const Geometry* geom0,
93 const Geometry* geom1)
95 auto_ptr<Geometry> rgeom0;
96 auto_ptr<Geometry> rgeom1;
97 removeCommonBits(geom0, geom1, rgeom0, rgeom1);
98 return computeResultPrecision(rgeom0->difference(rgeom1.get()));
101 /*public*/
102 Geometry*
103 CommonBitsOp::symDifference(
104 const Geometry* geom0,
105 const Geometry* geom1)
107 auto_ptr<Geometry> rgeom0;
108 auto_ptr<Geometry> rgeom1;
109 removeCommonBits(geom0, geom1, rgeom0, rgeom1);
110 return computeResultPrecision(rgeom0->symDifference(rgeom1.get()));
113 /*public*/
114 Geometry*
115 CommonBitsOp::buffer(const Geometry* geom0, double distance)
117 auto_ptr<Geometry> rgeom0(removeCommonBits(geom0));
118 return computeResultPrecision(rgeom0->buffer(distance));
121 /*public*/
122 Geometry*
123 CommonBitsOp::computeResultPrecision(Geometry* result)
125 assert(cbr.get());
126 if (returnToOriginalPrecision)
127 cbr->addCommonBits(result);
128 return result;
131 /*private*/
132 Geometry*
133 CommonBitsOp::removeCommonBits(const Geometry* geom0)
135 cbr.reset(new CommonBitsRemover());
136 cbr->add(geom0);
138 #if GEOS_DEBUG
139 const Coordinate& commonCoord = cbr->getCommonCoordinate();
140 cerr << "CommonBitsRemover bits: " << commonCoord.x << ", " << commonCoord.y << endl;
141 #endif
143 Geometry* geom=cbr->removeCommonBits(geom0->clone());
144 return geom;
147 /*private*/
148 void
149 CommonBitsOp::removeCommonBits(
150 const geom::Geometry* geom0,
151 const geom::Geometry* geom1,
152 std::auto_ptr<geom::Geometry>& rgeom0,
153 std::auto_ptr<geom::Geometry>& rgeom1)
156 cbr.reset(new CommonBitsRemover());
158 cbr->add(geom0);
159 cbr->add(geom1);
161 #if GEOS_DEBUG
162 const Coordinate& commonCoord = cbr->getCommonCoordinate();
163 cerr << "CommonBitsRemover bits: " << commonCoord.x << ", " << commonCoord.y << endl;
164 #endif
166 rgeom0.reset(cbr->removeCommonBits(geom0->clone()));
167 rgeom1.reset(cbr->removeCommonBits(geom1->clone()));
171 } // namespace geos.precision
172 } // namespace geos
174 /**********************************************************************
175 * $Log$
176 * Revision 1.10 2006/04/13 23:28:26 strk
177 * "always build before commit" (forgot a closing paren)
179 * Revision 1.9 2006/04/13 23:23:52 strk
180 * fixed bug in binary ops failing to consistently reduce operands.
182 * Revision 1.8 2006/04/07 08:27:12 strk
183 * debugging blocks
185 * Revision 1.7 2006/04/06 14:36:51 strk
186 * Cleanup in geos::precision namespace (leaks plugged, auto_ptr use, ...)
188 * Revision 1.6 2006/03/23 09:17:19 strk
189 * precision.h header split, minor optimizations
191 **********************************************************************/