/src/MapServer/src/mapgeomutil.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * $Id$ |
3 | | * |
4 | | * Project: MapServer |
5 | | * Purpose: Rendering utility functions |
6 | | * Author: Thomas Bonfort and the MapServer team. |
7 | | * |
8 | | ****************************************************************************** |
9 | | * Copyright (c) 1996-2011 Regents of the University of Minnesota. |
10 | | * |
11 | | * Permission is hereby granted, free of charge, to any person obtaining a |
12 | | * copy of this software and associated documentation files (the "Software"), |
13 | | * to deal in the Software without restriction, including without limitation |
14 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
15 | | * and/or sell copies of the Software, and to permit persons to whom the |
16 | | * Software is furnished to do so, subject to the following conditions: |
17 | | * |
18 | | * The above copyright notice and this permission notice shall be included in |
19 | | * all copies of this Software or works derived from this Software. |
20 | | * |
21 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
22 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
24 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
26 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
27 | | * DEALINGS IN THE SOFTWARE. |
28 | | *****************************************************************************/ |
29 | | |
30 | | #include "mapserver.h" |
31 | | #include "renderers/agg/include/agg_arc.h" |
32 | | #include "renderers/agg/include/agg_basics.h" |
33 | | |
34 | | shapeObj *msRasterizeArc(double x0, double y0, double radius, double startAngle, |
35 | 0 | double endAngle, int isSlice) { |
36 | 0 | static int allocated_size = 100; |
37 | 0 | shapeObj *shape = (shapeObj *)calloc(1, sizeof(shapeObj)); |
38 | 0 | MS_CHECK_ALLOC(shape, sizeof(shapeObj), NULL); |
39 | 0 | mapserver::arc arc(x0, y0, radius, radius, startAngle * MS_DEG_TO_RAD, |
40 | 0 | endAngle * MS_DEG_TO_RAD, true); |
41 | 0 | arc.approximation_scale(1); |
42 | 0 | arc.rewind(1); |
43 | 0 | msInitShape(shape); |
44 | |
|
45 | 0 | lineObj *line = (lineObj *)calloc(1, sizeof(lineObj)); |
46 | 0 | if (!line) { |
47 | 0 | msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", |
48 | 0 | "msRasterizeArc()", __FILE__, __LINE__, |
49 | 0 | (unsigned int)sizeof(lineObj)); |
50 | 0 | free(shape); |
51 | 0 | return NULL; |
52 | 0 | } |
53 | 0 | shape->line = line; |
54 | 0 | shape->numlines = 1; |
55 | 0 | line->point = (pointObj *)calloc(allocated_size, sizeof(pointObj)); |
56 | 0 | if (!line->point) { |
57 | 0 | msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", |
58 | 0 | "msRasterizeArc()", __FILE__, __LINE__, |
59 | 0 | (unsigned int)(allocated_size * sizeof(pointObj))); |
60 | 0 | free(line); |
61 | 0 | free(shape); |
62 | 0 | return NULL; |
63 | 0 | } |
64 | | |
65 | 0 | line->numpoints = 0; |
66 | |
|
67 | 0 | double x, y; |
68 | | |
69 | | // first segment from center to first point of arc |
70 | 0 | if (isSlice) { |
71 | 0 | line->point[0].x = x0; |
72 | 0 | line->point[0].y = y0; |
73 | 0 | line->numpoints = 1; |
74 | 0 | } |
75 | 0 | while (arc.vertex(&x, &y) != mapserver::path_cmd_stop) { |
76 | 0 | if (line->numpoints == allocated_size) { |
77 | 0 | allocated_size *= 2; |
78 | 0 | line->point = |
79 | 0 | (pointObj *)realloc(line->point, allocated_size * sizeof(pointObj)); |
80 | 0 | if (!line->point) { |
81 | 0 | msSetError(MS_MEMERR, "%s: %d: Out of memory allocating %u bytes.\n", |
82 | 0 | "msRasterizeArc()", __FILE__, __LINE__, |
83 | 0 | (unsigned int)(allocated_size * sizeof(pointObj))); |
84 | 0 | free(line); |
85 | 0 | free(shape); |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 0 | } |
89 | 0 | line->point[line->numpoints].x = x; |
90 | 0 | line->point[line->numpoints].y = y; |
91 | 0 | line->numpoints++; |
92 | 0 | } |
93 | | |
94 | | // make sure the shape is closed |
95 | 0 | if (line->point[line->numpoints - 1].x != line->point[0].x || |
96 | 0 | line->point[line->numpoints - 1].y != line->point[0].y) { |
97 | 0 | if (line->numpoints == allocated_size) { |
98 | 0 | allocated_size *= 2; |
99 | 0 | line->point = (pointObj *)msSmallRealloc( |
100 | 0 | line->point, allocated_size * sizeof(pointObj)); |
101 | 0 | } |
102 | 0 | line->point[line->numpoints].x = line->point[0].x; |
103 | 0 | line->point[line->numpoints].y = line->point[0].y; |
104 | 0 | line->numpoints++; |
105 | 0 | } |
106 | |
|
107 | 0 | return shape; |
108 | 0 | } |